📄 uart.c
字号:
/*! * \file uart.c * \brief UART Driver * * This driver provides an initialization routine as well as the basic * UART I/O functions * * \version $Revision: 1.3 $ * \author Michael Norman */#include "common.h"#include "uart.h"/********************************************************************//*! * \brief UART Initiatization * \param info UART_INFO structure containing configuration information * \return None * * This routine initializes the UARTs of the MCF5445x based on the * configuration information passed in a UART_INFO structure. * * \warning If UART channel 2 is selected, the Timer pins will be used * for the UART functions. This will disable the use of all * external Timer pins. * * \todo support for external clock * \todo stop bit parameter support * \todo mode selection parameter (normal, echo, loopback) * \todo all hardware flow control modes * \todo interrupt capability */voiduart_init (UART_INFO *info){ register uint16 ubgs; uint8 parity, stop_bits; ASSERT(info->dbits <= 8 && info->dbits >= 5); /* Enable the proper UART pins in the GPIO module */ switch (info->chan) { case 0: MCF_GPIO_PAR_UART |= 0 | MCF_GPIO_PAR_UART_U0TXD_U0TXD | MCF_GPIO_PAR_UART_U0RXD_U0RXD; if (info->flow) { MCF_GPIO_PAR_UART |= 0 | MCF_GPIO_PAR_UART_U0CTS_U0CTS | MCF_GPIO_PAR_UART_U0RTS_U0RTS; } break; case 1: MCF_GPIO_PAR_UART |= 0 | MCF_GPIO_PAR_UART_U1TXD_U1TXD | MCF_GPIO_PAR_UART_U1RXD_U1RXD; if (info->flow) { MCF_GPIO_PAR_UART |= 0 | MCF_GPIO_PAR_UART_U1CTS_U1CTS | MCF_GPIO_PAR_UART_U1RTS_U1RTS; } break; case 2: MCF_GPIO_PAR_TIMER = 0 | MCF_GPIO_PAR_TIMER_T3IN_U2RXD | MCF_GPIO_PAR_TIMER_T2IN_U2TXD; if (info->flow) { MCF_GPIO_PAR_TIMER = MCF_GPIO_PAR_TIMER & MCF_GPIO_PAR_TIMER_T1IN_MASK & MCF_GPIO_PAR_TIMER_T0IN_MASK | MCF_GPIO_PAR_TIMER_T1IN_U2CTS | MCF_GPIO_PAR_TIMER_T0IN_U2RTS; } break; default: ASSERT(FALSE); } /* Reset Transmitter */ MCF_UART_UCR(info->chan) = MCF_UART_UCR_RESET_TX; /* Reset Receiver */ MCF_UART_UCR(info->chan) = MCF_UART_UCR_RESET_RX; /* Reset Mode Register */ MCF_UART_UCR(info->chan) = MCF_UART_UCR_RESET_MR; /* Determine UMR1 Parity Settings */ switch (info->parity) { case UART_PARITY_EVEN: parity = MCF_UART_UMR_PM_EVEN; break; case UART_PARITY_ODD: parity = MCF_UART_UMR_PM_ODD; break; case UART_PARITY_LOW: parity = MCF_UART_UMR_PM_FORCE_LO; break; case UART_PARITY_HIGH: parity = MCF_UART_UMR_PM_FORCE_HI; break; case UART_PARTIY_NONE: parity = MCF_UART_UMR_PM_NONE; break; case UART_PARITY_MD_DATA: parity = MCF_UART_UMR_PM_MULTI_DATA; break; case UART_PARITY_MD_ADDR: parity = MCF_UART_UMR_PM_MULTI_ADDR; break; default: ASSERT(FALSE); } /* Setup Mode Register 1 */ MCF_UART_UMR(info->chan) = (0 | parity | (info->ffull ? MCF_UART_UMR_RXIRQ : 0) | (info->flow ? MCF_UART_UMR_RXRTS : 0) | MCF_UART_UMR_BC(info->dbits - 5)); /* Determine the UMR2 Stop Bit Settings */ //todo /* Setup Mode Register 2 */ MCF_UART_UMR(info->chan) = (0 | MCF_UART_UMR_CM_NORMAL | MCF_UART_UMR_SB_STOP_BITS_1); /* Set Rx and Tx baud by timer */ MCF_UART_UCSR(info->chan) = (0 | MCF_UART_UCSR_RCS_SYS_CLK | MCF_UART_UCSR_TCS_SYS_CLK); /* Mask UART dma interrupts*/ MCF_UART_UIMR(info->chan) = 0 | (info->dma ? MCF_UART_UIMR_RXRDY_FU : 0); /* Calculate baud settings */ ubgs = (uint16)(info->clkfreq / (info->baud * 32)); MCF_UART_UBG1(info->chan) = (uint8)((ubgs & 0xFF00) >> 8); MCF_UART_UBG2(info->chan) = (uint8)(ubgs & 0x00FF); /* * Enable receiver and transmitter */ MCF_UART_UCR(info->chan) = (0 | MCF_UART_UCR_TX_ENABLED | MCF_UART_UCR_RX_ENABLED);}/********************************************************************//*! * \brief Get character from UART * \param channel UART channel * \return The received character * * Poll the UART receive FIFO until a character is present. Return * a single character. */charuart_getchar (int channel){ /* Wait until character has been received */ while (!(MCF_UART_USR(channel) & MCF_UART_USR_RXRDY)) ; return MCF_UART_URB(channel);}/********************************************************************//*! * \brief Sent a character out the UART * \param channel UART channel * \param ch Character to send * \return None * * Poll the UART transmit FIFO until there is room for a character. * Put a character into the Tx FIFO. */voiduart_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;}/********************************************************************//*! * \brief Check for a received character * \param channel UART channel * \return TRUE if a character is present, FALSE otherwise * * Poll the UART receive FIFO and determine if a character is present. */intuart_getchar_present (int channel){ return (MCF_UART_USR(channel) & MCF_UART_USR_RXRDY);}/********************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -