#ifndef _USART_H #define _USART_H /************************************************************************** * Written by Marcin O'BenY Benka * Version 1.11 (20100224) * * NOTE: * * Buffered USART support. * * IMPORTANT: * This library needs to set following constants to work: * - F_CPU - cpu frequency * - USART_RBUF_SIZE - receive buffer size (only power of 2) * - USART_TBUF_SIZE - transmit buffer size (only power of 2) * - USART_SEND_MAX_LENGTH - max length of transmitted data block * - USART_USE_PORT1 - uses USART1 instead of USART0 * - USART_USE_STATUS - uses status register for received data * - USART_USE_TIMER - enables support for timer-based read timeout **************************************************************************/ /*************************************************************************** * INCLUDES ***************************************************************************/ #include "config.h" #include "global.h" /*************************************************************************** * DEFINITIONS ***************************************************************************/ /* configure first USART port */ #ifndef USART_USE_PORT1 /* for one USART port MCUs*/ #ifdef UCSRA #define USART_UBRRH UBRRH #define USART_UBRRL UBRRL #define USART_UCSRA UCSRA #define USART_UCSRB UCSRB #define USART_UCSRC UCSRC #define USART_UDR UDR #define USART_UDRIE UDRIE /* some Atmegas has got USART_RX_vect others USART_RXC_vect defined */ #ifdef USART_RX_vect #define USART_INT_RX USART_RX_vect #else #define USART_INT_RX USART_RXC_vect #endif /* USART_RX_vect */ #define USART_INT_UDRE USART_UDRE_vect /* for two USART ports MCUs*/ #else #define USART_UBRRH UBRR0H #define USART_UBRRL UBRR0L #define USART_UCSRA UCSR0A #define USART_UCSRB UCSR0B #define USART_UCSRC UCSR0C #define USART_UDR UDR0 #define USART_UDRIE UDRIE0 /* some Atmegas has got USART0_RX_vect others USART0_RXC_vect defined */ #ifdef USART0_RX_vect #define USART_INT_RX USART0_RX_vect #else #define USART_INT_RX USART0_RXC_vect #endif /* USART_RX_vect */ #define USART_INT_UDRE USART0_UDRE_vect #endif /* UCSRA */ /* configure second USART port */ #else #define USART_UBRRH UBRR1H #define USART_UBRRL UBRR1L #define USART_UCSRA UCSR1A #define USART_UCSRB UCSR1B #define USART_UCSRC UCSR1C #define USART_UDR UDR1 #define USART_UDRIE UDRIE1 /* some Atmegas has got USART1_RX_vect others USART1_RXC_vect defined */ #ifdef USART1_RX_vect #define USART_INT_RX USART1_RX_vect #else #define USART_INT_RX USART1_RXC_vect #endif /* USART_RX_vect */ #define USART_INT_UDRE USART1_UDRE_vect #endif /* USART_USE_PORT1 */ /* cpu frequency divided by 100 used for initialization */ #define USART_F_CPU (F_CPU/100) /* buffers masks */ /* NOTE: Works only for values that are power of 2. This is used for fast * modulo in circular transmit/receive buffer. */ #define USART_RMASK (USART_RBUF_SIZE-1) #define USART_TMASK (USART_TBUF_SIZE-1) /* status bits */ #define RX_STAT_DOR 3 #define RX_STAT_FE 4 #define RX_STAT_PE 2 #define RX_STAT_OVR 1 #define TX_STAT_FULL 0 /* usart baud rates */ enum e_usartbaud { /* standard baudrates */ USART_BAUD_9600 = 96, USART_BAUD_19200 = 192, USART_BAUD_38400 = 384, USART_BAUD_57600 = 576, USART_BAUD_115200 = 1152, USART_BAUD_230400 = 2304, USART_BAUD_460800 = 4608, USART_BAUD_921600 = 9216, /* custom baudrates */ USART_BAUD_460000 = 4600, USART_BAUD_920000 = 9200 }; typedef enum e_usartbaud e_usartbaud_t; /* usart parity settings */ enum e_usartparity { USART_PARITY_NONE = 0, USART_PARITY_EVEN = 1, USART_PARITY_ODD = 2 }; typedef enum e_usartparity e_usartparity_t; /* usart status */ enum e_usartstatus { RX_DOR = BV(RX_STAT_DOR), /* rx dataoverrun */ RX_FE = BV(RX_STAT_FE), /* rx frame error */ RX_PE = BV(RX_STAT_PE), /* rx parity error */ RX_OVR = BV(RX_STAT_OVR), /* rx ring buffer overflow */ TX_FULL = BV(TX_STAT_FULL) /* tx full buffer */ }; typedef enum e_usartstatus e_usartstatus_t; #ifdef USART_USE_TIMER /* timeout counter (incremented in sync_timer) */ extern volatile UINT16 usart_timeout; #endif /* USART_USE_TIMER */ /*************************************************************************** * FUNCTIONS ***************************************************************************/ /* read bytes and write them to buffer */ /* ----------------------------------------------------------------------- RETURN VAL : TRUE if receive buffer overflow occured, FALSE otherwise ARGS : NONE * ----------------------------------------------------------------------- */ BOOL usartReceiveBufferOverflow(); #ifndef USART_USE_TIMER /* read bytes and write them to buffer */ /* ----------------------------------------------------------------------- RETURN VAL : pointer to buffer where data was written ARGS : str - pointer to buffer, where data will be written count - bytes to read NOTE : if USART_USE_TIMER isn't declared, function won't have timeout support * ----------------------------------------------------------------------- */ BOOL usartRead(BYTE* str, const UINT16 count); #else /* read bytes and write them to buffer */ /* ----------------------------------------------------------------------- RETURN VAL : pointer to buffer where data was written ARGS : str - pointer to buffer, where data will be written count - bytes to read timeout - timeout in 1/100 of second (less then 65536) NOTE : if USART_USE_TIMER is declared, function will have timer-based timeout support enabled (see sync_timer.h) * ----------------------------------------------------------------------- */ BOOL usartRead(BYTE* str, const UINT16 count, const UINT16 timeout); #endif /* USART_USE_TIMER */ #ifdef USART_USE_STATUS /* read next byte in buffer, be carefull for buffer overflow */ /* ----------------------------------------------------------------------- RETURN VAL : read byte or 0 if buffer is empty ARGS : ok - TRUE on byte read, FALSE on fail * ----------------------------------------------------------------------- */ BYTE usartReadByte(BOOL *ok); #else /* read next byte in buffer, be carefull for buffer overflow */ /* ----------------------------------------------------------------------- RETURN VAL : read byte or 0 if buffer is empty ARGS : NONE * ----------------------------------------------------------------------- */ BYTE usartReadByte(); #endif /* USART_USE_STATUS */ /* unread bytes count */ /* ----------------------------------------------------------------------- RETURN VAL : number of unread bytes in buffer ARGS : NONE * ----------------------------------------------------------------------- */ UINT8 usartUnreadBytes(); /* initialization of USART */ /* ----------------------------------------------------------------------- RETURN VAL : NONE ARGS : baudrate - baudrate divided by 100 or value from e_usartbaud double_speed - enables usart double speed mode two_stop_bits - use two stop bits in frame parity - use parity checking - e_usartparity * ----------------------------------------------------------------------- */ void initUsart(const UINT16 baudrate, const BOOL double_speed, const BOOL two_stop_bits, const UINT8 parity); /* flush bytes waiting in receive buffer (ignore them) */ /* ----------------------------------------------------------------------- RETURN VAL : NONE ARGS : NONE * ----------------------------------------------------------------------- */ void usartFlush(); /* send data */ /* ----------------------------------------------------------------------- RETURN VAL : NONE ARGS : str - string to be sent length - max length of given string (limited to USART_SEND_MAX_LENGTH) * ----------------------------------------------------------------------- */ void usartSend(BYTE *str, const UINT16 length); /* send single byte */ /* ----------------------------------------------------------------------- RETURN VAL : NONE ARGS : b - byte to be transmitted * ----------------------------------------------------------------------- */ void usartSendByte(const BYTE b); /* send string */ /* ----------------------------------------------------------------------- RETURN VAL : NONE ARGS : str - string to be sent length - max length of given string (limited to USART_SEND_MAX_LENGTH) * ----------------------------------------------------------------------- */ void usartSendString(BYTE *str, const UINT16 length); /* send string from program memory */ /* ----------------------------------------------------------------------- RETURN VAL : NONE ARGS : str - string to be sent * ----------------------------------------------------------------------- */ void usartSendString_P(const BYTE *str); #endif /* _USART_H */ /* END */