![]() |
uart
1.4
Serial stream for Xmega/Wrapper for UART-driver Xmega
|
This documents the UART wrapper for the UART-driver of Atmel for Xmega devices and documents the stream for the HvA-Xmegaboard.
The wrapper is build around the USART-driver of Atmel The stream uses the wrapper to make a default stream to USART0 of port F.
Application | main.c , ... |
Streams: init_stream() ,printf() ,scanf() | stream.c , stream.h |
Wrapper: init_uart() ,uart_putc() ,uart_getc() | uart.c , uart.h |
Drivers from Atmel | usart_driver.c , usart_driver.h.h , avr_compiler.h |
The style of this wrapper is based on the UART library from Peter Fleury (http://homepage.hispeed.ch/peterfleury/) for the ATmega devices.
The application note 1307 "Using the XMEGA USART" with the desription of the USART-driver can be found at http://www.atmel.com/Images/doc8049.pdf.
For every buffered UART the USART-driver of Atmel needs a special datstructure USART_data_t and two Interrupt Service Routine:
It is not reasonable to define both ISR's for all USART's. It is not possible to make an ISR with a variable as interruptvector. So it is not possible to define a generic ISR in uart.c.
The solution choosen is to define the ISR's conditionally in uart.h. If a variable ENABLE_UART_xn is defined before uart.h is included the datastructure uartxn and the ISR's ISR(USARTxn_RXC_vect) and ISR(USARTxn_DRE_vect) are defined foor UART n of port x .
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>
For UART 1 of port C the testcondition is in uart.h is now true and a datastructure UARTC1, ISR(USARTC1_DRE_vect) and ISR(USARTC1_RXC_vect) is defined.
The UARTC1 still needs to be initialized. If init_uart is used the interrupt levels are low. For other interruptlevel init_uart_levels can be used. For the enabled UART C1 this defines the baud rate 115200 bps:
init_uart(&uartC1, &USARTC1, F_CPU, 115200, 0); PMIC.CTRL |= PMIC_LOLVLEN_bm; sei();
With stream.c and stream.h the standard streams are connected to USART0 of port F. It can directly used with the HvA-Xmegaboard.
To use the standard io-functions you need only to include stream.h, to call the initialization function init_stream(), to enable the interrupt mechanism.
#include "stream.h" init_stream(F_CPU); sei(); printf("print some text to USART0 of port F");
The linker needs: usart_driver.c, uart.c en stream.h
A zip-file with the uart_driver.c and uart_driver.h from Atmel can be found here: http://www.atmel.com/Images/AVR1307.zip.
To use this UART wrapper:
To use streams for USART0 port F:
This library is free software. You can redistribute it and/or modify it. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY.