ATmega8GPSLoggerBasismodul  20131018
UART Library

Interrupt UART library using the built-in UART with transmit and receive circular buffers. More...

Macros

#define UART_BAUD_SELECT(baudRate, xtalCpu)   ((xtalCpu)/((baudRate)*16l)-1)
 UART Baudrate Expression. More...
 
#define UART_BAUD_SELECT_DOUBLE_SPEED(baudRate, xtalCpu)   (((xtalCpu)/((baudRate)*8l)-1)|0x8000)
 UART Baudrate Expression for ATmega double speed mode. More...
 
#define UART_RX_BUFFER_SIZE   64
 
#define UART_TX_BUFFER_SIZE   64
 
#define UART_FRAME_ERROR   0x0800 /* Framing Error by UART */
 
#define UART_OVERRUN_ERROR   0x0400 /* Overrun condition by UART */
 
#define UART_BUFFER_OVERFLOW   0x0200 /* receive ringbuffer overflow */
 
#define UART_NO_DATA   0x0100 /* no receive data available */
 
#define uart_puts_P(__s)   uart_puts_p(PSTR(__s))
 Macro to automatically put a string constant into program memory. More...
 
#define uart1_puts_P(__s)   uart1_puts_p(PSTR(__s))
 Macro to automatically put a string constant into program memory. More...
 

Functions

void uart_init (unsigned int baudrate)
 Initialize UART and set baudrate. More...
 
unsigned int uart_getc (void)
 Get received byte from ringbuffer. More...
 
void uart_putc (unsigned char data)
 Put byte to ringbuffer for transmitting via UART. More...
 
void uart_puts (const char *s)
 Put string to ringbuffer for transmitting via UART. More...
 
void uart_puts_p (const char *progmem_s)
 Put string from program memory to ringbuffer for transmitting via UART. More...
 
void uart1_init (unsigned int baudrate)
 Initialize USART1 (only available on selected ATmegas) More...
 
unsigned int uart1_getc (void)
 Get received byte of USART1 from ringbuffer. (only available on selected ATmega) More...
 
void uart1_putc (unsigned char data)
 Put byte to ringbuffer for transmitting via USART1 (only available on selected ATmega) More...
 
void uart1_puts (const char *s)
 Put string to ringbuffer for transmitting via USART1 (only available on selected ATmega) More...
 
void uart1_puts_p (const char *s)
 Put string from program memory to ringbuffer for transmitting via USART1 (only available on selected ATmega) More...
 

Detailed Description

Interrupt UART library using the built-in UART with transmit and receive circular buffers.

#include <uart.h>

This library can be used to transmit and receive data through the built in UART.

An interrupt is generated when the UART has finished transmitting or receiving a byte. The interrupt handling routines use circular buffers for buffering received and transmitted data.

The UART_RX_BUFFER_SIZE and UART_TX_BUFFER_SIZE constants define the size of the circular buffers in bytes. Note that these constants must be a power of 2. You may need to adapt this constants to your target and your application by adding CDEFS += -DUART_RX_BUFFER_SIZE=nn -DUART_RX_BUFFER_SIZE=nn to your Makefile.

Note
Based on Atmel Application Note AVR306
Author
Peter Fleury pfleu.nosp@m.ry@g.nosp@m.mx.ch http://jump.to/fleury

Macro Definition Documentation

#define uart1_puts_P (   __s)    uart1_puts_p(PSTR(__s))

Macro to automatically put a string constant into program memory.

Definition at line 186 of file uart.h.

#define UART_BAUD_SELECT (   baudRate,
  xtalCpu 
)    ((xtalCpu)/((baudRate)*16l)-1)

UART Baudrate Expression.

constants and macros

Parameters
xtalCpusystem clock in Mhz, e.g. 4000000L for 4Mhz
baudRatebaudrate in bps, e.g. 1200, 2400, 9600

Definition at line 67 of file uart.h.

Referenced by gps_init().

#define UART_BAUD_SELECT_DOUBLE_SPEED (   baudRate,
  xtalCpu 
)    (((xtalCpu)/((baudRate)*8l)-1)|0x8000)

UART Baudrate Expression for ATmega double speed mode.

Parameters
xtalCpusystem clock in Mhz, e.g. 4000000L for 4Mhz
baudRatebaudrate in bps, e.g. 1200, 2400, 9600

Definition at line 73 of file uart.h.

#define UART_BUFFER_OVERFLOW   0x0200 /* receive ringbuffer overflow */

Definition at line 95 of file uart.h.

Referenced by gps_get_char(), and SIGNAL().

#define UART_FRAME_ERROR   0x0800 /* Framing Error by UART */

test if the size of the circular buffers fits into SRAM

high byte error return code of uart_getc()

Definition at line 93 of file uart.h.

#define UART_NO_DATA   0x0100 /* no receive data available */

Definition at line 96 of file uart.h.

Referenced by gps_get_char(), and uart_getc().

#define UART_OVERRUN_ERROR   0x0400 /* Overrun condition by UART */

Definition at line 94 of file uart.h.

#define uart_puts_P (   __s)    uart_puts_p(PSTR(__s))

Macro to automatically put a string constant into program memory.

Definition at line 171 of file uart.h.

#define UART_RX_BUFFER_SIZE   64

Size of the circular receive buffer, must be power of 2

Definition at line 78 of file uart.h.

#define UART_TX_BUFFER_SIZE   64

Size of the circular transmit buffer, must be power of 2

Definition at line 82 of file uart.h.

Function Documentation

unsigned int uart1_getc ( void  )

Get received byte of USART1 from ringbuffer. (only available on selected ATmega)

See also
uart_getc
void uart1_init ( unsigned int  baudrate)

Initialize USART1 (only available on selected ATmegas)

See also
uart_init
void uart1_putc ( unsigned char  data)

Put byte to ringbuffer for transmitting via USART1 (only available on selected ATmega)

See also
uart_putc
void uart1_puts ( const char *  s)

Put string to ringbuffer for transmitting via USART1 (only available on selected ATmega)

See also
uart_puts
void uart1_puts_p ( const char *  s)

Put string from program memory to ringbuffer for transmitting via USART1 (only available on selected ATmega)

See also
uart_puts_p
unsigned int uart_getc ( void  )

Get received byte from ringbuffer.

Returns in the lower byte the received character and in the higher byte the last receive error. UART_NO_DATA is returned when no data is available.

Returns
lower byte: received byte from ringbuffer
higher byte: last receive status
  • 0 successfully received data from UART
  • UART_NO_DATA
    no receive data available
  • UART_BUFFER_OVERFLOW
    Receive ringbuffer overflow. We are not reading the receive buffer fast enough, one or more received character have been dropped
  • UART_OVERRUN_ERROR
    Overrun condition by UART. A character already present in the UART UDR register was not read by the interrupt handler before the next character arrived, one or more received characters have been dropped.
  • UART_FRAME_ERROR
    Framing Error by UART

uart_getc() return byte from ringbuffer Returns: lower byte: received byte from ringbuffer higher byte: last receive error

< no data available

calculate / store buffer index

get data from receive buffer

Definition at line 390 of file uart.c.

References UART_NO_DATA, and UART_RX_BUFFER_MASK.

Referenced by gps_get_char().

Here is the caller graph for this function:

void uart_init ( unsigned int  baudrate)

Initialize UART and set baudrate.

function prototypes

Parameters
baudrateSpecify baudrate using macro UART_BAUD_SELECT()

uart_init() initialize UART and set baudrate

Parameters
[in]baudrateusing macro UART_BAUD_SELECT()

Definition at line 313 of file uart.c.

Referenced by gps_init().

Here is the caller graph for this function:

void uart_putc ( unsigned char  data)

Put byte to ringbuffer for transmitting via UART.

Parameters
databyte to be transmitted

uart_putc() write byte to ringbuffer for transmitting via UART

Parameters
[in]databyte to be transmitted

wait for free space in buffer

enable UDRE interrupt

Definition at line 416 of file uart.c.

References UART_TX_BUFFER_MASK.

Referenced by uart_puts(), and uart_puts_p().

Here is the caller graph for this function:

void uart_puts ( const char *  s)

Put string to ringbuffer for transmitting via UART.

The string is buffered by the uart library in a circular buffer and one character at a time is transmitted to the UART using interrupts. Blocks if it can not write the whole string into the circular buffer.

Parameters
sstring to be transmitted

uart_puts() transmit string to UART

Parameters
[in]sstring to be transmitted

Definition at line 440 of file uart.c.

References uart_putc().

Here is the call graph for this function:

void uart_puts_p ( const char *  progmem_s)

Put string from program memory to ringbuffer for transmitting via UART.

The string is buffered by the uart library in a circular buffer and one character at a time is transmitted to the UART using interrupts. Blocks if it can not write the whole string into the circular buffer.

Parameters
progmem_sprogram memory string to be transmitted
See also
uart_puts_P

uart_puts_p() transmit string from program memory to UART

Parameters
[in]progmem_sprogram memory string to be transmitted

Definition at line 452 of file uart.c.

References uart_putc().

Here is the call graph for this function: