uart  1.4
Serial stream for Xmega/Wrapper for UART-driver Xmega
 All Files Functions Variables Macros
uart Documentation

Introduction

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 Atmelusart_driver.c, usart_driver.h.h, avr_compiler.h

Wrapper

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:

  • one for receiving: ISR(USARTxn_RXC_vect)
  • one for transmitting ISR(USARTxn_DRE_vect)

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();

Streams

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

UART-Driver from Atmel

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.

Setup

To use this UART wrapper:

  • Place usart_driver.c, usart_driver.h and avr_compiler.h from AVR1307.zip in your project folder.
  • Place uart.c and uart.h in your project folder.
  • Add uart_driver.c to your project.
  • Add uart.c to your project.
  • Change possibly the macrodefinitions USART_RX_BUFFER_SIZE and USART_TX_BUFFER_SIZE in uart_driver.h
  • Define the necessary ENABLE_UART_xn macro(s)
  • Include the uart.h in your c-file
  • Initialize the UART(s) with init_uart() or init_uart_levels()
  • Change PMIC and enable the interrupt mechanism
  • Now you can use uart_getc, uart_putc and uart_puts in your program.

To use streams for USART0 port F:

  • Place usart_driver.c, usart_driver.h and avr_compiler.h from AVR1307.zip in your project folder.
  • Place uart.c, stream.c, uart.h and stream.h in your project folder.
  • Add uart_driver.c to your project.
  • Add uart.c and stream.c to your project.
  • Change possibly the macrodefinitions USART_RX_BUFFER_SIZE and USART_TX_BUFFER_SIZE in uart_driver.h
  • Include the stream.h in your c-file
  • Initialize the streams with init_stream()
  • Enable the global interrupt mechanism
  • Now you can use printf, scanf, etc. in your program.
Note
With the internal RC-oscillators 2 MHz or 32 MHz, it may be necessary to calibrate this internal clock.

Contact

Author
Wim Dolman (w.e.dolman@hva.nl)
Version
1.4
Latest version can be found here: http://dolman-wim.nl/xmega/libraries/index.php
Date
03-10-2016

License

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.