uart  1.4
Serial stream for Xmega/Wrapper for UART-driver Xmega
 All Files Functions Variables Macros
uart.h File Reference

Wrapper for UART-driver of Atmel for Xmega. More...

#include "avr_compiler.h"
#include "usart_driver.h"

Go to the source code of this file.

Macros

#define UART_NO_DATA   0x0100
 Macro UART_NO_DATA is returned by uart_getc when no data is present.
 

Functions

uint16_t calc_bsel (uint32_t f_cpu, uint32_t baud, int8_t scale, uint8_t clk2x)
 Calculates the baud rate value BSEL. More...
 
uint16_t uart_getc (USART_data_t *uart)
 Get a byte from the circular receive buffer. More...
 
void uart_putc (USART_data_t *uart, uint8_t data)
 Write a byte to the circular transmit buffer. More...
 
void uart_puts (USART_data_t *uart, char *s)
 Write a string to the circulair transmit buffer. More...
 
void set_usart_txrx_direction (USART_t *usart)
 Set direction for the transmit and receive pin. More...
 
void init_uart (USART_data_t *uart, USART_t *usart, uint32_t f_cpu, uint32_t baud, uint8_t clk2x)
 Initializes the UART. More...
 
void init_uart_levels (USART_data_t *uart, USART_t *usart, uint32_t f_cpu, uint32_t baud, uint8_t clk2x, USART_RXCINTLVL_t rxcIntLevel, USART_DREINTLVL_t dreIntLevel)
 Initializes the UART. More...
 
 ISR (USARTC0_RXC_vect)
 Interrupt Service Routine for receiving with UARTC0. This ISR is only defined if the macro ENABLE_UART_C0 is defined.
 
 ISR (USARTC0_DRE_vect)
 Interrupt Service Routine for transmitting with UARTC0. This ISR is only defined if the macro ENABLE_UART_C0 is defined.
 
 ISR (USARTC1_RXC_vect)
 Interrupt Service Routine for receiving with UARTC1. This ISR is only defined if the macro ENABLE_UART_C1 is defined.
 
 ISR (USARTC1_DRE_vect)
 Interrupt Service Routine for transmitting with UARTC1. This ISR is only defined if the macro ENABLE_UART_C1 is defined.
 
 ISR (USARTD0_RXC_vect)
 Interrupt Service Routine for receiving with UARTD0. This ISR is only defined if the macro ENABLE_UART_D0 is defined.
 
 ISR (USARTD0_DRE_vect)
 Interrupt Service Routine for transmitting with UARTD0. This ISR is only defined if the macro ENABLE_UART_D0 is defined.
 
 ISR (USARTD1_RXC_vect)
 Interrupt Service Routine for receiving with UARTD1. This ISR is only defined if the macro ENABLE_UART_D1 is defined.
 
 ISR (USARTD1_DRE_vect)
 Interrupt Service Routine for transmitting with UARTD1. This ISR is only defined if the macro ENABLE_UART_D1 is defined.
 
 ISR (USARTE0_RXC_vect)
 Interrupt Service Routine for receiving with UARTE0. This ISR is only defined if the macro ENABLE_UART_E0 is defined.
 
 ISR (USARTE0_DRE_vect)
 Interrupt Service Routine for transmitting with UARTE0. This ISR is only defined if the macro ENABLE_UART_E0 is defined.
 
 ISR (USARTE1_RXC_vect)
 Interrupt Service Routine for receiving with UARTE1. This ISR is only defined if the macro ENABLE_UART_E1 is defined.
 
 ISR (USARTE1_DRE_vect)
 Interrupt Service Routine for transmitting with UARTE1. This ISR is only defined if the macro ENABLE_UART_E1 is defined.
 
 ISR (USARTF0_RXC_vect)
 Interrupt Service Routine for receiving with UARTF0. This ISR is only defined if the macro ENABLE_UART_F0 is defined.
 
 ISR (USARTF0_DRE_vect)
 Interrupt Service Routine for transmitting with UARTF0. This ISR is only defined if the macro ENABLE_UART_F0 is defined.
 
 ISR (USARTF1_RXC_vect)
 Interrupt Service Routine for receiving with UARTF1. This ISR is only defined if the macro ENABLE_UART_F1 is defined.
 
 ISR (USARTF1_DRE_vect)
 Interrupt Service Routine for transmitting with UARTF1. This ISR is only defined if the macro ENABLE_UART_F1 is defined.
 

Variables

USART_data_t uartC0
 Global declaration uart with databuffers for UARTC0. This variable is only defined if the macro ENABLE_UART_C0 is defined.
 
USART_data_t uartC1
 Global declaration uart with databuffers for UARTC1. This variable is only defined if the macro ENABLE_UART_C0 is defined.
 
USART_data_t uartD0
 Global declaration uart with databuffers for UARTD0. This variable is only defined if the macro ENABLE_UART_D0 is defined.
 
USART_data_t uartD1
 Global declaration uart with databuffers for UARTD1. This variable is only defined if the macro ENABLE_UART_D1 is defined.
 
USART_data_t uartE0
 Global declaration uart with databuffers for UARTE0. This variable is only defined if the macro ENABLE_UART_E0 is defined.
 
USART_data_t uartE1
 Global declaration uart with databuffers for UARTE1. This variable is only defined if the macro ENABLE_UART_E1 is defined.
 
USART_data_t uartF0
 Global declaration uart with databuffers for UARTF0. This variable is only defined if the macro ENABLE_UART_F0 is defined.
 
USART_data_t uartF1
 Global declaration uart with databuffers for UARTF1. This variable is only defined if the macro ENABLE_UART_F1 is defined.
 

Detailed Description

Wrapper for UART-driver of Atmel for Xmega.

Author
Wim Dolman (w.e.dolman@hva.nl)
Date
29-11-2013
Version
1.1

The files uart.c and uart.h uses the UART-driver uart_driver.c and uart_driver.h from Atmel, see http://www.atmel.com/Images/doc8049.pdf and http://www.atmel.com/Images/AVR1307.zip.

It is a kind of wrapper with the same style as the UART-drivers from Peter Fleury (http://jump.to/fleury) for the ATmega.

For every buffered UART we need two Interrupt Service Routine: one for receiving and one for transmitting For each UART there is a condition. If the condition is true the two ISR's will be compiled, else they will be skipped. The condition for UART uart_id depends on a macro ENABLE_UART_uart_id

To use, for example, UARTC1 define in the code just before including uart.h a macro ENABLE_UART_C1:

   #define ENABLE_UART_C1 1
   #include <uart.h>
Note
An AVR-project can use multiple UART's. One shoud take care that in different source files there are no multiple ENABLE_UART_uart_id definitions for the same UART.

Function Documentation

uint16_t calc_bsel ( uint32_t  f_cpu,
uint32_t  baud,
int8_t  scale,
uint8_t  clk2x 
)

Calculates the baud rate value BSEL.

Parameters
f_cpusystem clock (F_CPU)
bauddesired baud rate
scalescale factor (BSCALE)
clk2xclock speed double (1 for double, 0 for no double)

It calculates the baud selection value BSEL from the system clock, the baud rate, the scale factor and a boolean for clock doubling.

The formula to calculate BSEL is:

\begin{eqnarray*} \mbox{BSCALE}>=0\quad &:& \quad \mbox{BSEL} = \frac{f_{\mbox{cpu}}}{N\ 2^{\mbox{BSCALE}}\ f_{\mbox{baud}}} - 1 \\[3pt] \mbox{BSCALE}<0\quad &:& \quad \mbox{BSEL} = \frac{1}{2^{\mbox{BSCALE}}}\ \left( \frac{f_{\mbox{cpu}}}{N\ f_{\mbox{baud}}} - 1 \right) \end{eqnarray*}

N is a factor which is 16 with no clock doubling and 8 with clock doubling

Returns
the calculated BSEL
void init_uart ( USART_data_t *  uart,
USART_t *  usart,
uint32_t  f_cpu,
uint32_t  baud,
uint8_t  clk2x 
)

Initializes the UART.

Parameters
uartpointer to a UART datastructure with buffers
usartpointer to a UART datastructure
f_cpusystem clock (F_CPU)
bauddesired baud rate
clk2xclock speed double (1 for double, 0 for no double)

It calculates the scale factor BSCALE and the baud selection value BSEL. It selects what USART module to use and it initializes receive and transmit buffer. It initializes the USART module and sets the direction of TXD and RXD pin. The interrupt levels of the DRE interrupt function and the RXC interrupt function are both set to a low level.

Returns
void
void init_uart_levels ( USART_data_t *  uart,
USART_t *  usart,
uint32_t  f_cpu,
uint32_t  baud,
uint8_t  clk2x,
USART_RXCINTLVL_t  rxcIntLevel,
USART_DREINTLVL_t  dreIntLevel 
)

Initializes the UART.

Parameters
uartpointer to a UART datastructure with buffers
usartpointer to a UART datastructure (UART module)
f_cpusystem clock (F_CPU)
bauddesired baud rate
clk2xclock speed double (1 for double, 0 for no double)
rxcIntLevelRXC interrupt level
dreIntLevelDRE interrupt level

It calculates the scale factor BSCALE and the baud selection value BSEL. It selects what USART module to use and it initializes receive and transmit buffer. It initializes the USART module and sets the direction of TXD and RXD pin. The interrupt level of the DRE interrupt function and the RXC interrupt function are set to dreIntLevel and rxcIntLevel.

Returns
void
void set_usart_txrx_direction ( USART_t *  usart)

Set direction for the transmit and receive pin.

Parameters
usartpointer to UART datastructure (UART module)
Returns
void
uint16_t uart_getc ( USART_data_t *  uart)

Get a byte from the circular receive buffer.

Parameters
uartpointer to UART datastructure with buffers
Returns
received byte from circulair buffer (low byte) or UART_NO_DATA if buffer is empty
void uart_putc ( USART_data_t *  uart,
uint8_t  data 
)

Write a byte to the circular transmit buffer.

Parameters
uartpointer to UART datastructure with buffers
databyte to be written
Returns
void
void uart_puts ( USART_data_t *  uart,
char *  s 
)

Write a string to the circulair transmit buffer.

Parameters
uartpointer to UART datastructure with buffers
spointer to string to be written
Returns
void