AVR32 Software Framework Drivers: USART

As we get into AVR32 programming it’s unavoidable to use drivers from the software framework. First because we are not going to reinvent the wheel every time we want to start a new project, then because they are fully tested and documented. Moreover, they provide code-compatibility between several AVR32 families (UC3A, UC3B, UC3L, AP7…)
I thought that a good first approach to AVR32 software framework was using and understanding the USART. The USART is essential for those who don´t have a JTAG for debugging (I know, it’s not too expensive, but may be for a student or somebody that just want to test the platform) because you can send debug content to your Mac/PC using the serial port (this means using the USART in UART mode).
Our first program will be an UART echo program. This means that it will echo every character sent from the PC. We will learn a few things about AVR32 and how to code with this example, more than be can imagine at first.
First of all, ensure you have the lastest version of AVR32Studio and AVR32 Software Framework. Both available and for free at http://www.atmel.com/products/AVR/. Inside AVR32Studio create a “New AVR32 C project from template”.

In the template you will choose your board/mcu configuration. In this case, I will use EVK1101 as the board, and AT32UC3B0256 ad the MCU.

Once the project is configured we will select the Software Framework drivers that we will use.

Once the Software Framework drivers are selected, replace your main.c with main.c (USART)
Let’s take a close look to the code:
First of all we will include the header files from the framework that we will use, this includes the AVR32 IO definition, GPIO, USART and Power Manager.

// Include Files
#include <avr32/io.h>
// EVK1101
#include "board.h"
// Drivers
#include "power_clocks_lib.h"
#include "gpio.h"
#include "usart.h"

then we will define the frequency for the PBA, which USART we will use, and which GPIO pins we will be connected to that UART

#define TARGET_PBACLK_FREQ_HZ FOSC0  // PBA clock target frequency, in Hz
// Usart Pins compatible with EVK1101
#define MY_USART               (&AVR32_USART1)
#define MY_USART_RX_PIN        AVR32_USART1_RXD_0_0_PIN
#define MY_USART_RX_FUNCTION   AVR32_USART1_RXD_0_0_FUNCTION
#define MY_USART_TX_PIN        AVR32_USART1_TXD_0_0_PIN
#define MY_USART_TX_FUNCTION   AVR32_USART1_TXD_0_0_FUNCTION
#define MY_USART_CLOCK_MASK    AVR32_USART1_CLK_PBA
#define MY_PDCA_CLOCK_HSB      AVR32_PDCA_CLK_HSB
#define MY_PDCA_CLOCK_PB       AVR32_PDCA_CLK_PBA

in the main function we will configure the GPIO pins. this is done by creating a variable of type gpio_map_t which includes the pin and its function.

	static const gpio_map_t MY_USART_GPIO_MAP = {
			{MY_USART_RX_PIN, MY_USART_RX_FUNCTION},
			{MY_USART_TX_PIN, MY_USART_TX_FUNCTION} };

then we set up the UART parameters, this includes the baudrate, parity and stop among others.

	static const usart_options_t MY_USART_OPTIONS = { .baudrate= 57600, .charlength= 8,
			.paritytype= USART_NO_PARITY, .stopbits = USART_1_STOPBIT,
			.channelmode = USART_NORMAL_CHMODE };

next we configure the Power Clock in order to use the crystal connected to OSC0.

	pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP);

We need to tell the GPIO module that it must assign the GPIO pins to the USART

	gpio_enable_module(MY_USART_GPIO_MAP,sizeof(MY_USART_GPIO_MAP)/sizeof(MY_USART_GPIO_MAP[0]));

For enabling the UART the framework provides a function for initializing in RS232 mode

	usart_init_rs232(MY_USART,&MY_USART_OPTIONS,TARGET_PBACLK_FREQ_HZ);

And last, but not least, you can see in this few lines of code how to write and read from the UART

	usart_write_line(MY_USART, "This is an echo program)n");
	while(1) {
		c = usart_getchar(MY_USART);
		usart_putchar(MY_USART,c);
	}