/*! \file uart2.c \brief Dual UART driver with buffer support. */ //***************************************************************************** // // File Name : 'usart.c' // Title : Dual UART driver with buffer support // Author : Pascal Stang - Copyright (C) 2000-2004 // Created : 11/20/2000 // Revised : 07/04/2004 // Version : 1.0 // Target MCU : ATMEL AVR Series // Editor Tabs: 4 // // Description : This is a UART driver for AVR-series processors with two // hardware UARTs such as the mega161 and mega128 // // This code is distributed under the GNU Public License // which can be found at http://www.gnu.org/licenses/gpl.txt // //***************************************************************************** #include #include #include "buffer.h" #include "usart.h" // UART global variables // flag variables volatile u08 uartReadyTx[2]; volatile u08 uartBufferedTx[2]; // receive and transmit buffers cBuffer uartRxBuffer[2]; cBuffer uartTxBuffer[2]; unsigned short uartRxOverflow[2]; #ifndef UART_BUFFER_EXTERNAL_RAM // using internal ram, // automatically allocate space in ram for each buffer static char uart0RxData[UART0_RX_BUFFER_SIZE]; static char uart0TxData[UART0_TX_BUFFER_SIZE]; static char uart1RxData[UART1_RX_BUFFER_SIZE]; static char uart1TxData[UART1_TX_BUFFER_SIZE]; #endif typedef void (*voidFuncPtru08)(unsigned char); volatile static voidFuncPtru08 UartRxFunc[2]; void uartInit(void) { // initialize both uarts uart0Init(); uart1Init(); } void uart0Init(void) { // initialize the buffers uart0InitBuffers(); // initialize user receive handlers UartRxFunc[0] = 0; // enable RxD/TxD and interrupts outb(UCSR0B, BV(RXCIE)|BV(TXCIE)|BV(RXEN)|BV(TXEN)); // set default baud rate uartSetBaudRate(0, UART0_DEFAULT_BAUD_RATE); // initialize states uartReadyTx[0] = TRUE; uartBufferedTx[0] = FALSE; // clear overflow count uartRxOverflow[0] = 0; // enable interrupts sei(); } void uart1Init(void) { // initialize the buffers uart1InitBuffers(); // initialize user receive handlers UartRxFunc[1] = 0; // enable RxD/TxD and interrupts outb(UCSR1B, BV(RXCIE)|BV(TXCIE)|BV(RXEN)|BV(TXEN)); // set default baud rate uartSetBaudRate(1, UART1_DEFAULT_BAUD_RATE); // initialize states uartReadyTx[1] = TRUE; uartBufferedTx[1] = FALSE; // clear overflow count uartRxOverflow[1] = 0; // enable interrupts sei(); } void uart0InitBuffers(void) { #ifndef UART_BUFFER_EXTERNAL_RAM // initialize the UART0 buffers bufferInit(&uartRxBuffer[0], uart0RxData, UART0_RX_BUFFER_SIZE); bufferInit(&uartTxBuffer[0], uart0TxData, UART0_TX_BUFFER_SIZE); #else // initialize the UART0 buffers bufferInit(&uartRxBuffer[0], (u08*) UART0_RX_BUFFER_ADDR, UART0_RX_BUFFER_SIZE); bufferInit(&uartTxBuffer[0], (u08*) UART0_TX_BUFFER_ADDR, UART0_TX_BUFFER_SIZE); #endif } void uart1InitBuffers(void) { #ifndef UART_BUFFER_EXTERNAL_RAM // initialize the UART1 buffers bufferInit(&uartRxBuffer[1], uart1RxData, UART1_RX_BUFFER_SIZE); bufferInit(&uartTxBuffer[1], uart1TxData, UART1_TX_BUFFER_SIZE); #else // initialize the UART1 buffers bufferInit(&uartRxBuffer[1], (u08*) UART1_RX_BUFFER_ADDR, UART1_RX_BUFFER_SIZE); bufferInit(&uartTxBuffer[1], (u08*) UART1_TX_BUFFER_ADDR, UART1_TX_BUFFER_SIZE); #endif } void uartSetRxHandler(u08 nUart, void (*rx_func)(unsigned char c)) { // make sure the uart number is within bounds if(nUart < 2) { // set the receive interrupt to run the supplied user function UartRxFunc[nUart] = rx_func; } } void uartSetBaudRate(u08 nUart, u32 baudrate) { // calculate division factor for requested baud rate, and set it u16 bauddiv = ((F_CPU+(baudrate*8L))/(baudrate*16L)-1); if(nUart) { outb(UBRR1L, bauddiv); #ifdef UBRR1H outb(UBRR1H, bauddiv>>8); #endif } else { outb(UBRR0L, bauddiv); #ifdef UBRR0H outb(UBRR0H, bauddiv>>8); #endif } } cBuffer* uartGetRxBuffer(u08 nUart) { // return rx buffer pointer return &uartRxBuffer[nUart]; } cBuffer* uartGetTxBuffer(u08 nUart) { // return tx buffer pointer return &uartTxBuffer[nUart]; } void uartSendByte(u08 nUart, u08 txData) { // wait for the transmitter to be ready // while(!uartReadyTx[nUart]); // send byte if(nUart) { while(!(UCSR1A & (1<