/**************************************************************************** * * Copyright (c) 2005 Dave Hylands * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * ****************************************************************************/ /** * * @file UART.h * * @brief This file implements the interrupt drive UART class. * ****************************************************************************/ #if !defined( UART_H ) #define UART_H ///< Include Guard // ---- Include Files ------------------------------------------------------- #include #include #include "CBUF.h" #include "Config.h" #if defined( __AVR_LIBC_VERSION__ ) # include #endif #if defined( __cplusplus ) extern "C" { #endif //--------------------------------------------------------------------------- // // To use this header file the following #defines should be made in Config.h // Replace the x by a 0 or a 1. // // CFG_USE_UARTx 0 or 1 Controls if anything relating to that UART is generated // // CFG_UARTx_RX_BUFFER_SIZE How many bytes to put in the Rx buffer. // Setting the buffer size to zero, means that // no buffers will be alllocated and a callback // must be provided to deal with each received // character. // // CFG_UARTx_TX_BUFFER_SIZE How many bytes to put in the Tx buffer. // Setting this to zero will cause polled routines // to be used. // // API Provided: // // int UARTx_GetChar( void ); // int UARTx_IsCharAvailable(); // void UARTx_SetRxHandler( void (*rxHandler)( uint8_t ch )); // int UARTx_PutChar( char ch ); // void UARTx_PutStr( const char *s ); // void UARTx_Write( const void *data, uint8_t len ); // //--------------------------------------------------------------------------- #if !defined( CFG_USE_UART0 ) && !defined( CFG_USE_UART1 ) # error Expecting one of CFG_USE_UART0 or CFG_USE_UART1 to be defined #endif #if CFG_USE_UART0 // These defines allow this file to work with AVRs which only have one UART // (i.e ATMega8, 16, 32) #if !defined( UCSR0A ) #define UCSR0A UCSRA #define UCSR0B UCSRB #define UCSR0C UCSRC #define UBRR0H UBRRH #define UBRR0L UBRRL #define UDR0 UDR #define UDRIE0 UDRIE #define UDRE0 UDRE #define SIG_USART0_RECV SIG_USART_RECV #define SIG_USART0_DATA SIG_USART_DATA #endif //--------------------------------------------------------------------------- #if !defined( CFG_UART0_RX_BUFFER_SIZE ) # error CFG_UART0_RX_BUFFER_SIZE isn't defined. #endif #if (( CFG_UART0_RX_BUFFER_SIZE > 0 ) && (( CFG_UART0_RX_BUFFER_SIZE & ( CFG_UART0_RX_BUFFER_SIZE - 1 )) != 0 )) # error CFG_UART0_RX_BUFFER_SIZE isn't a power of 2. #endif #if ( CFG_UART0_RX_BUFFER_SIZE > 128 ) typedef uint16_t UART0_RxIndex_t; #else typedef uint8_t UART0_RxIndex_t; #endif #if ( CFG_UART0_RX_BUFFER_SIZE > 0 ) typedef struct { UART0_RxIndex_t m_getIdx; UART0_RxIndex_t m_putIdx; uint8_t m_entry[ CFG_UART0_RX_BUFFER_SIZE ]; } UART0_RxBuffer_t; #if defined( __AVR_LIBC_VERSION__ ) int UART0_GetCharStdio( FILE *fs ); #else #define UART0_GetCharStdio UART0_GetChar #endif int UART0_GetChar( void ); #define gUart0RxBuf_SIZE sizeof( gUart0RxBuf.m_entry ) extern volatile UART0_RxBuffer_t gUart0RxBuf; #define UART0_IsCharAvailable() (!CBUF_IsEmpty( gUart0RxBuf )) #else void UART0_SetRxHandler( void (*rxHandler)( uint8_t ch )); #endif //--------------------------------------------------------------------------- #if !defined( CFG_UART0_TX_BUFFER_SIZE ) # error CFG_UART0_TX_BUFFER_SIZE isn't declared. #endif #if (( CFG_UART0_TX_BUFFER_SIZE & ( CFG_UART0_TX_BUFFER_SIZE - 1 )) != 0 ) # error CFG_UART0_TX_BUFFER_SIZE isn't a power of 2. #endif #if ( CFG_UART0_TX_BUFFER_SIZE > 128 ) typedef uint16_t UART0_TxIndex_t; #else typedef uint8_t UART0_TxIndex_t; #endif #if ( CFG_UART0_TX_BUFFER_SIZE > 0 ) typedef struct { UART0_TxIndex_t m_getIdx; UART0_TxIndex_t m_putIdx; uint8_t m_entry[ CFG_UART0_TX_BUFFER_SIZE ]; } UART0_TxBuffer_t; #define gUart0TxBuf_SIZE sizeof( gUart0TxBuf.m_entry ) extern volatile UART0_TxBuffer_t gUart0TxBuf; #endif //--------------------------------------------------------------------------- #if defined( __AVR_LIBC_VERSION__ ) int UART0_PutCharStdio( char ch, FILE *fs ); #else #define UART0_PutCharStdio UART0_PutChar #endif int UART0_PutChar( char ch ); void UART0_PutStr( const char *s ); void UART0_Write( const void *data, uint8_t len ); #endif // CFG_USE_UART0 //=========================================================================== #if CFG_USE_UART1 //--------------------------------------------------------------------------- #if !defined( CFG_UART1_RX_BUFFER_SIZE ) # error CFG_UART1_RX_BUFFER_SIZE isn't defined. #endif #if (( CFG_UART1_RX_BUFFER_SIZE > 0 ) && (( CFG_UART1_RX_BUFFER_SIZE & ( CFG_UART1_RX_BUFFER_SIZE - 1 )) != 0 )) # error CFG_UART1_RX_BUFFER_SIZE isn't a power of 2. #endif #if ( CFG_UART1_RX_BUFFER_SIZE > 128 ) typedef uint16_t UART1_RxIndex_t; #else typedef uint8_t UART1_RxIndex_t; #endif #if ( CFG_UART1_RX_BUFFER_SIZE > 0 ) typedef struct { UART1_RxIndex_t m_getIdx; UART1_RxIndex_t m_putIdx; uint8_t m_entry[ CFG_UART1_RX_BUFFER_SIZE ]; } UART1_RxBuffer_t; #if defined( __AVR_LIBC_VERSION__ ) int UART1_GetCharStdio( FILE *fs ); #else #define UART1_GetCharStdio UART1_GetChar #endif int UART1_GetChar( void ); #define gUart1RxBuf_SIZE sizeof( gUart1RxBuf.m_entry ) extern volatile UART1_RxBuffer_t gUart1RxBuf; #define UART1_IsCharAvailable() (!CBUF_IsEmpty( gUart1RxBuf )) #else void UART1_SetRxHandler( void (*rxHandler)( uint8_t ch )); #endif //--------------------------------------------------------------------------- #if !defined( CFG_UART1_TX_BUFFER_SIZE ) # error CFG_UART1_TX_BUFFER_SIZE isn't declared. #endif #if (( CFG_UART1_TX_BUFFER_SIZE & ( CFG_UART1_TX_BUFFER_SIZE - 1 )) != 0 ) # error CFG_UART1_TX_BUFFER_SIZE isn't a power of 2. #endif #if ( CFG_UART1_TX_BUFFER_SIZE > 128 ) typedef uint16_t UART1_TxIndex_t; #else typedef uint8_t UART1_TxIndex_t; #endif #if ( CFG_UART1_TX_BUFFER_SIZE > 0 ) typedef struct { UART1_TxIndex_t m_getIdx; UART1_TxIndex_t m_putIdx; uint8_t m_entry[ CFG_UART1_TX_BUFFER_SIZE ]; } UART1_TxBuffer_t; #define gUart1TxBuf_SIZE sizeof( gUart1TxBuf.m_entry ) extern volatile UART1_TxBuffer_t gUart1TxBuf; #endif //--------------------------------------------------------------------------- #if defined( __AVR_LIBC_VERSION__ ) int UART1_PutCharStdio( char ch, FILE *fs ); #else #define UART1_PutCharStdio UART1_PutChar #endif int UART1_PutChar( char ch ); void UART1_PutStr( const char *s ); void UART1_Write( const void *data, uint8_t len ); #endif // CFG_USE_UART1 #if defined( __cplusplus ) } #endif /** @} */ #endif // UART_H