void uart_select(int uart_num)
{
switch(uart_num)
{
case 1://uart0
uart_init (UART0_BASE_PTR, core_clk_khz, TERMINAL_BAUD);
break;
case 2://uart1,uart0
uart_init (UART0_BASE_PTR, core_clk_khz, TERMINAL_BAUD);
uart_init (UART1_BASE_PTR, core_clk_khz, TERMINAL_BAUD);
break;
case 3://uart0,uart1,uart2
uart_init (UART0_BASE_PTR, core_clk_khz, TERMINAL_BAUD);
uart_init (UART1_BASE_PTR, core_clk_khz, TERMINAL_BAUD);
uart_init (UART2_BASE_PTR, periph_clk_khz, TERMINAL_BAUD);
break;
default:
printf("value overflow !!!\n");
break;
}
}
void uart_init (UART_MemMapPtr uartch, int sysclk, int baud)
{
register uint16 sbr, brfa;
uint8 temp;
//使能引脚,配置复用引脚为TXD功能
if (TERM_PORT == UART0_BASE_PTR)
{
PORTB_PCR17 = PORT_PCR_MUX(0x3);
PORTB_PCR16 = PORT_PCR_MUX(0x3);
}
else if (TERM_PORT == UART1_BASE_PTR)
{
PORTE_PCR0 = PORT_PCR_MUX(0x3);
PORTE_PCR1 = PORT_PCR_MUX(0x3);
}
else if (TERM_PORT == UART2_BASE_PTR)
{
PORTD_PCR3 = PORT_PCR_MUX(0x3);
PORTD_PCR2 = PORT_PCR_MUX(0x3);
}
//使能串口时钟
if(uartch == UART0_BASE_PTR)
SIM_SCGC4 |= SIM_SCGC4_UART0_MASK;
else
if (uartch == UART1_BASE_PTR)
SIM_SCGC4 |= SIM_SCGC4_UART1_MASK;
else
if (uartch == UART2_BASE_PTR)
SIM_SCGC4 |= SIM_SCGC4_UART2_MASK;
//禁止发送接收
UART_C2_REG(uartch) &= ~(UART_C2_TE_MASK
| UART_C2_RE_MASK );
//配置成8位,无校验模式
UART_C1_REG(uartch) = 0;
//计算波特率 串口0、1为系统时钟 其他串口为外设时钟
sbr = (uint16)((sysclk*1000)/(baud * 16));
/* Save off the current value of the UARTx_BDH except for the SBR field */
temp = UART_BDH_REG(uartch) & ~(UART_BDH_SBR(0x1F));
UART_BDH_REG(uartch) = temp | UART_BDH_SBR(((sbr & 0x1F00) >> 8));
UART_BDL_REG(uartch) = (uint8)(sbr & UART_BDL_SBR_MASK);
brfa = (((sysclk*32000)/(baud * 16)) - (sbr * 32));
temp = UART_C4_REG(uartch) & ~(UART_C4_BRFA(0x1F));
UART_C4_REG(uartch) = temp | UART_C4_BRFA(brfa);
//使能发送接收
UART_C2_REG(uartch) |= (UART_C2_TE_MASK
| UART_C2_RE_MASK );
}
//-------------------------------------------------------------------------*
//函数名 : uart_putchar 发送单个字节*
//功 能: 串行发送1个字节 *
//参 数: uartch: 串口号 *
// ch: 要发送的字节 *
//返 回: 无 *
//说 明:
//-------------------------------------------------------------------------*
void uart_putchar (UART_MemMapPtr channel, char ch)
{
//等待发送缓冲区空
while(!(UART_S1_REG(channel) & UART_S1_TDRE_MASK));
//发送数据ch
UART_D_REG(channel) = (uint8)ch;
}
//-------------------------------------------------------------------------*
//函数名 : uart_getchar *
//功 能: 串行接受1个字节 *
//参 数: uartch: 串口号 *
// ch: 接收到的字节 *
//返 回: 成功:1; 失败:0 *
//说 明: *
//-------------------------------------------------------------------------*
char uart_getchar (UART_MemMapPtr channel, uint8 *ch)
{
uint32 k;
for(k = 0; k < 0xfbb; k ++)
{
if((UART_S1_REG(channel) & UART_S1_RDRF_MASK) != 0)
{
*ch = UART_D_REG(channel);
return 1;
}
}
if(k >= 0xfbbb)
{
return 0;
}
return 0;
}
//-------------------------------------------------------------------------*
//函数名 : uart_sendN *
//功 能: 串行 接收n个字节 *
//参 数: uartch: 串口号 *
// buff: 发送缓冲区 *
// len: 发送长度 *
//返 回: 无 *
//说 明: *
//-------------------------------------------------------------------------*
void uart_sendN (UART_MemMapPtr channel, uint8 *buff, uint16 len)
{
int i;
for(i = 0; i < len; i ++)
{
uart_putchar(channel, buff[i]);
}
}
//-------------------------------------------------------------------------*
//函数名 : uart_reN *
//功 能: 串行 接收n个字节 *
//参 数: uartch: 串口号 *
// buff: 接收缓冲区 *
// len: 接收长度 *
//返 回: 1: 成功;0: 失败 *
//说 明: *
//-------------------------------------------------------------------------*
uint8 uart_reN(UART_MemMapPtr channel, uint8 *buff, uint16 len)
{
uint16 m = 0;
while(m < len)
{
if(0 == uart_getchar (channel,&buff[m]))
return 0;
else m ++;
}
return 1;
}
//-------------------------------------------------------------------------*
//函数名 : enableuartreint *
//功 能: 开串口接收中断 *
//参 数: uartch: 串口号 *
// irqno: 对应irq号 *
//返 回: 无 *
//说 明: *
//-------------------------------------------------------------------------*
void enableuartreint(UART_MemMapPtr uartch,uint8 irqno)
{
UART_C2_REG(uartch)|=UART_C2_RIE_MASK; //开放UART接收中断
enable_irq(irqno); //开接收引脚的IRQ中断
}
//-------------------------------------------------------------------------*
//函数名 : disableuartreint *
//功 能: 关串口接收中断 *
//参 数: uartch: 串口号 *
// irqno: 对应irq号 *
//返 回: 无 *
//说 明: *
//-------------------------------------------------------------------------*
void disableuartreint(UART_MemMapPtr uartch,uint8 irqno)
{
UART_C2_REG(uartch)&=~UART_C2_RIE_MASK; //禁止UART接收中断
disable_irq(irqno); //关接收引脚的IRQ中断
}
int uart_getchar_present (UART_MemMapPtr channel)
{
return (UART_S1_REG(channel) & UART_S1_RDRF_MASK);
}
|