uart
1.4
Serial stream for Xmega/Wrapper for UART-driver Xmega
|
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... | |
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>
int8_t calc_bscale | ( | uint32_t | f_cpu, |
uint32_t | baud, | ||
uint8_t | clk2x | ||
) |
Determines the scale factor BSCALE.
f_cpu | system clock (F_CPU) |
baud | desired baud rate |
clk2x | clock 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.
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 |