uart
1.4
Serial stream for Xmega/Wrapper for UART-driver Xmega
|
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. | |
Wrapper for UART-driver of Atmel for Xmega.
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>
uint16_t calc_bsel | ( | uint32_t | f_cpu, |
uint32_t | baud, | ||
int8_t | scale, | ||
uint8_t | clk2x | ||
) |
Calculates the baud rate value BSEL.
f_cpu | system clock (F_CPU) |
baud | desired baud rate |
scale | scale factor (BSCALE) |
clk2x | clock 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:
N is a factor which is 16 with no clock doubling and 8 with clock doubling
void init_uart | ( | USART_data_t * | uart, |
USART_t * | usart, | ||
uint32_t | f_cpu, | ||
uint32_t | baud, | ||
uint8_t | clk2x | ||
) |
Initializes the UART.
uart | pointer to a UART datastructure with buffers |
usart | pointer to a UART datastructure |
f_cpu | system clock (F_CPU) |
baud | desired baud rate |
clk2x | clock 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.
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.
uart | pointer to a UART datastructure with buffers |
usart | pointer to a UART datastructure (UART module) |
f_cpu | system clock (F_CPU) |
baud | desired baud rate |
clk2x | clock speed double (1 for double, 0 for no double) |
rxcIntLevel | RXC interrupt level |
dreIntLevel | DRE 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.
void set_usart_txrx_direction | ( | USART_t * | usart | ) |
Set direction for the transmit and receive pin.
usart | pointer to UART datastructure (UART module) |
uint16_t uart_getc | ( | USART_data_t * | uart | ) |
Get a byte from the circular receive buffer.
uart | pointer to UART datastructure with buffers |
void uart_putc | ( | USART_data_t * | uart, |
uint8_t | data | ||
) |
Write a byte to the circular transmit buffer.
uart | pointer to UART datastructure with buffers |
data | byte to be written |
void uart_puts | ( | USART_data_t * | uart, |
char * | s | ||
) |
Write a string to the circulair transmit buffer.
uart | pointer to UART datastructure with buffers |
s | pointer to string to be written |