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

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

#include <avr/io.h>
#include <math.h>
#include "avr_compiler.h"
#include "usart_driver.h"
#include "uart.h"

Functions

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...
 
uint16_t calc_bsel (uint32_t f_cpu, uint32_t baud, int8_t scale, uint8_t clk2x)
 Calculates the baud rate value BSEL. More...
 
int8_t calc_bscale (uint32_t f_cpu, uint32_t baud, uint8_t clk2x)
 Determines the scale factor BSCALE. 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...
 

Detailed Description

Wrapper for UART-driver of Atmel for Xmega.

Author
Wim Dolman (w.e.dolman@hva.nl)
Date
11-12-2015
Version
1.2

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

int8_t calc_bscale ( uint32_t  f_cpu,
uint32_t  baud,
uint8_t  clk2x 
)

Determines the scale factor BSCALE.

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

It determines the scale factor BSCALE from the system clock, the baud rate, and a boolean for clock doubling.

Returns
the scale factor BSCALE
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