如题官方是有使用uart中断发送的例子(非DMA)
我使用的代码如下,无法进行中段发送(但是进行跟踪发现是一直处于TC的状态,但是无法收到发送的数据)
具体代码如下
/*
* File: uart.c
* Purpose: Provide common UART routines for serial IO
*
* Notes:
*
*/
#include "types.h"
#include "uart.h"
uint_32 gu32UARTStatus;
uint_32 gu32UATRxCounter = 0;
uint_8 * gpu8UARTTx;
uint_8 * gpu8UARTRx;
uint_32 gu32TxDataSize;
uint_32 gu32RxCounter = 0;
/********************************************************************/
/*
* Initialize the UART for 8N1 operation, interrupts disabled, and
* no hardware flow-control
*
* NOTE: Since the UARTs are pinned out in multiple locations on most
* Kinetis devices, this driver does not enable UART pin functions.
* The desired pins should be enabled before calling this init function.
*
* Parameters:
* uartch UART channel to initialize
* sysclk UART module Clock in kHz(used to calculate baud)
* baud UART baud rate
*/
void vfnUARTInit (uint_8 u8UARTPort, uint_32 u32SysCLK, uint_32 u32BaudRate)
{
uint_16 u16SBR;
uint_16 u16BRFA;
uint_8 u8temp;
UART_MemMapPtr tUART;
/* Enable the clock to the selected UART */
if(u8UARTPort == UART0)
{
SIM_SCGC4 |= SIM_SCGC4_UART0_MASK;
tUART = UART0_BASE_PTR;
}
else
{
if (u8UARTPort == UART1)
{
SIM_SCGC4 |= SIM_SCGC4_UART1_MASK;
tUART = UART1_BASE_PTR;
}
else
{
if (u8UARTPort == UART2)
{
SIM_SCGC4 |= SIM_SCGC4_UART2_MASK;
tUART = UART2_BASE_PTR;
}
else
{
if(u8UARTPort == UART3)
{
SIM_SCGC4 |= SIM_SCGC4_UART3_MASK;
tUART = UART3_BASE_PTR;
}
else
{
if(u8UARTPort == UART4)
{
SIM_SCGC1 |= SIM_SCGC1_UART4_MASK;
tUART = UART4_BASE_PTR;
}
else
{
SIM_SCGC1 |= SIM_SCGC1_UART5_MASK;
tUART = UART5_BASE_PTR;
}
}
}
}
}
/* Make sure that the transmitter and receiver are disabled while we
* change settings.
*/
UART_C2_REG(tUART) &= ~(UART_C2_TE_MASK
| UART_C2_RE_MASK );
/* Configure the UART for 8-bit mode, no parity */
UART_C1_REG(tUART) = 0; /* We need all default settings, so entire register is cleared */
/* Calculate baud settings */
u16SBR = (uint_16)((u32SysCLK*1000)/(u32BaudRate * 16));
/* Save off the current value of the UARTx_BDH except for the SBR field */
u8temp = UART_BDH_REG(tUART) & ~(UART_BDH_SBR(0x1F));
UART_BDH_REG(tUART) = u8temp | UART_BDH_SBR(((u16SBR & 0x1F00) >> 8));
UART_BDL_REG(tUART) = (uint_8)(u16SBR & UART_BDL_SBR_MASK);
/* Determine if a fractional divider is needed to get closer to the baud rate */
u16BRFA = (((u32SysCLK*32000)/(u32BaudRate * 16)) - (u16SBR * 32));
/* Save off the current value of the UARTx_C4 register except for the BRFA field */
u8temp = UART_C4_REG(tUART) & ~(UART_C4_BRFA(0x1F));
UART_C4_REG(tUART) = u8temp | UART_C4_BRFA(u16BRFA);
gu32UARTStatus = (1 << TX_COMPLETE) | (1 << RX_DATA);
UART_C2_REG(tUART) |= UART_C2_RIE_MASK | UART_C2_RE_MASK | UART_C2_TE_MASK;
/*NVIC*/
enable_irq(37); /* UART 3 */
/* Enable receiver and transmitter */
}
void vfnUARTTx(uint_8 * pu8Data, uint_32 u32DataSize)
{
if((gu32UARTStatus & (gu32UARTStatus & (1 << TX_COMPLETE))))
{
gu32TxDataSize = u32DataSize;
gpu8UARTTx = pu8Data;
gu32UARTStatus &= ~(1 << TX_COMPLETE);
UART3_C2 |= UART_C2_TCIE_MASK;
}
}
void vfnUARTRx(uint_8 * pu8Data, uint_32 u32DataSize)
{
if((gu32UARTStatus & (gu32UARTStatus & (1 << RX_DATA))))
{
gpu8UARTRx = pu8Data;
gu32RxCounter = u32DataSize;
gu32UARTStatus &= ~(1 << RX_DATA);
}
}
void vfnUART3ISR(void)
{
uint_8 u8temp = UART3_S1;
if(UART3_S1 & UART_S1_RDRF_MASK)
{
*gpu8UARTRx = UART3_D;
gpu8UARTRx++;
gu32RxCounter--;
if(!gu32RxCounter)
{
gu32UARTStatus |= (1 << RX_DATA);
}
}
if(UART3_S1 & UART_S1_TC_MASK)
{
if(gu32TxDataSize)
{
UART3_D = *gpu8UARTTx++;
gu32TxDataSize--;
}
else
{
UART3_C2 &= ~UART_C2_TCIE_MASK;
gu32UARTStatus |= (1 << TX_COMPLETE);
}
}
}
/*
* File: uart.h
* Purpose: Provide common ColdFire UART routines for polled serial IO
*
* Notes:
*/
#ifndef __UART_H__
#define __UART_H__
#include "derivative.h"
extern uint_32 gu32UARTStatus;
typedef enum
{
TX_COMPLETE = 0,
RX_DATA,
}eUARTStatus;
#define RX_COMPLETE (gu32UARTStatus & (1 << RX_DATA))
#define TX_END (gu32UARTStatus & (1 << TX_COMPLETE))
#define UART0 0
#define UART1 1
#define UART2 2
#define UART3 3
#define UART4 4
#define UART5 5
#define BAUDRATE_115200 (115200)
/********************************************************************/
//#define PETOOLKIT_FUNCTION
void vfnUARTInit (uint_8 u8UARTPort, uint_32 u32SysCLK, uint_32 u32BaudRate);
void vfnUARTTx(uint_8 * pu8Data, uint_32 u32DataSize);
void vfnUARTRx(uint_8 * pu8Data, uint_32 u32DataSize);
/********************************************************************/
#endif /* __UART_H__ */
|