📄 uart.c
字号:
/*
* File: uart.c
* Purpose: Provide common ColdFire UART routines for serial IO
*
* Notes:
*
*/
#pragma cplusplus off
#include "common.h"
#include "uart.h"
#include "m5222x_evb.h"
void
uart_init (int uartch, int sysclk, int baud, int settings)
{
/*
* Initialize all three UARTs for serial communications
*/
register uint16 ubgs;
switch (uartch)
{
case 2:
MCF_GPIO_PUCPAR = 0
| MCF_GPIO_PUCPAR_RXD2_RXD2
| MCF_GPIO_PUCPAR_TXD2_TXD2;
break;
case 1:
MCF_GPIO_PUBPAR = 0
| MCF_GPIO_PUBPAR_RXD1_RXD1
| MCF_GPIO_PUBPAR_TXD1_TXD1
| MCF_GPIO_PUBPAR_RTS1_RTS1
| MCF_GPIO_PUBPAR_CTS1_CTS1 ;
break;
case 0:
default:
MCF_GPIO_PUAPAR = 0
| MCF_GPIO_PUAPAR_RXD0_RXD0
| MCF_GPIO_PUAPAR_TXD0_TXD0;
}
/*
* Reset Transmitter
*/
MCF_UART_UCR(uartch) = MCF_UART_UCR_RESET_TX;
/*
* Reset Receiver
*/
MCF_UART_UCR(uartch) = MCF_UART_UCR_RESET_RX;
/*
* Reset Mode Register
*/
MCF_UART_UCR(uartch) = MCF_UART_UCR_RESET_MR;
/*
* No parity, 8-bits per character
*/
MCF_UART_UMR(uartch) = (0
| MCF_UART_UMR_PM_NONE
| MCF_UART_UMR_BC_8);
/*
* No echo or loopback, 1 stop bit
*/
MCF_UART_UMR(uartch) = (0
| MCF_UART_UMR_CM_NORMAL
| MCF_UART_UMR_SB_STOP_BITS_1);
/*
* Set Rx and Tx baud by timer
*/
MCF_UART_UCSR(uartch) = (0
| MCF_UART_UCSR_RCS_SYS_CLK
| MCF_UART_UCSR_TCS_SYS_CLK);
/*
* Mask all UART interrupts
*/
MCF_UART_UIMR(uartch) = 0x02;
/*
* Calculate baud settings
*/
ubgs = (uint16)((sysclk*1000)/(baud * 32));
MCF_UART_UBG1(uartch) = (uint8)((ubgs & 0xFF00) >> 8);
MCF_UART_UBG2(uartch) = (uint8)(ubgs & 0x00FF);
/*
* Enable receiver and transmitter
*/
MCF_UART_UCR(uartch) = (0
| MCF_UART_UCR_TX_ENABLED
| MCF_UART_UCR_RX_ENABLED);
}
/********************************************************************/
/*
* Wait for a character to be received on the specified UART
*
* Return Values:
* the received character
*/
char
uart_getchar (int channel)
{
/* Wait until character has been received */
while (!(MCF_UART_USR(channel) & MCF_UART_USR_RXRDY))
;
return MCF_UART_URB(channel);
}
/********************************************************************/
/*
* Wait for space in the UART Tx FIFO and then send a character
*/
void
uart_putchar (int channel, char ch)
{
/* Wait until space is available in the FIFO */
while (!(MCF_UART_USR(channel) & MCF_UART_USR_TXRDY))
;
/* Send the character */
MCF_UART_UTB(channel) = (uint8)ch;
}
/**********************************
uart send string
**********************************/
void
uart_putstr (int channel, char *str)
{
char t;
unsigned char i,n;
n = strlen(str);
for(i=0; i < n; i++)
{
t = *str++;
uart_putchar ( channel,t);
}
}
/********************************************************************/
/*
* Check to see if a character has been received
*
* Return values:
* 0 No character received
* 1 Character has been received
*/
int
uart_getchar_present (int channel)
{
return (MCF_UART_USR(channel) & MCF_UART_USR_RXRDY);
}
/********************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -