Atmel AT32UC3
Home - Developer - Contact us

 

Up

Introduction

Literature

Chapter 1: Start

Chapter 2: Clocks

Chapter 3: Port

Chapter 4: Interrupt

Chapter 5: Timer

Chapter 6: Real Time

Chapter 7: Serial Link

 

Lecture Material

Application_7
Application_7a

Chapter 7

The only way to communicate with the outside world in chapters 1 to 6 were the push-buttons and LEDs on the STK600-board. The board contains also the receiver and transmitter for a serial link to a host computer. They are connected to a 9-pin DSUB connector on the board. To connect the driver to the AT32UC3A3256=processor, connect the RXD- and TXD-pins on the 'RS232 SPARE'-connector to pins:


RS232 connection (from AT32UC3A3 datasheet).

Connect the RTS- and CTS-pins on 'RS232 SPARE'-connector to oneanother since no hardware handshaking drivers are included on the STK600 board. Connect the RS232-connector on the STK600 with an RS232 cable to the host computer - if necessary via a USB-to-Serial interface. To communicate with the AT32UC3A3256 , use any serial terminal software on the host computer.

In the programs 'Applicatio_7' and 'Application_7a' it is assumed that the terminal does not add a '\r' (carrage return) when it receives a new line character '\n'. A carrage return at the terminal should not add a '\n'; this is done in the main program.

USART

There are 4 USARTs (Universal Synchronous Asynchronous Receiver Transmitter) built into the AT32UC3A3256. They allow for quite a number of different protocolls (RS232, RS485, ISO7816 (only USART1), IrDA, SPI, LIN and test modes). In this chapter we will use the RS232 protocoll on USART0 in interrupt driven mode.

In preparation for the use of the USART we have to:

  1. switch pin PA03 and PA04 to USART0 mode,
  2. configure the Baud Rate Generator for the right Baud rate,
  3. select the operating mode (RS232 'Normal' mode: mode 0),
  4. initialize the interrupt handling, and
  5. enable the receiver and transmitter.

ad 1) For pins PA03 and PA04 the port number is NR = 0 and the bit-position number is NB = 3 and 4. In the AVR32 definitions these pins in USART-function are named AVR32_USART0_RXD_0_0_PIN and AVR32_USART0_TXD_0_0_PIN. The pin initialization looks like this:

	gpio_port->gperc = 1 << AVR32_USART0_RXD_0_0_PIN;		// enable for peripheral function
	gpio_port->gperc = 1 << AVR32_USART0_TXD_0_0_PIN;
	gpio_port->pmr0c = 1 << AVR32_USART0_RXD_0_0_PIN;		// select function A: USART0
	gpio_port->pmr1c = 1 << AVR32_USART0_TXD_0_0_PIN;
	

ad 2) We assume asynchronous operation with a baud rate of typically 9600. The clock input is connected to the PBA-clock, here 30MHz, and we use 16x oversampling. The baud rate is then calculated as:

baud rate = 30,000,000 / (16 * cd) = 1,875,000 / cd

For some typical baud rates the cd-value is listed below:

baud ratecd
12001563
2400781
4800391
9600195
1920098
3840049
5760033

We will use 9600 baud as default value, thus cd is initialized with a value of 195: the function usart0_set_baudrate is called with a parameter '0' which sets the default value. The cd-value is written into the Baud Rate Generator Register brgr.

ad 3) Due to the versatility of the USART, the initialization of the RS232 operating mode is quite extensive. The initialization needs to be done with the global interrupts being disabled.

// Disable all interrupts
usart0->idr = 0xffffffff;
usart0->csr;								
// Clear mode, time-out and transmitter time-guard register that could cause unpredictable behavior after reset.
usart0->mr = 0;
usart0->rtor = 0;
usart0->ttgr = 0;
	
// Shutdown TX and RX, reset status bits and turn off DTR and RTS:
// reset the receiver (RSTRX), reset the transmitter (RSTTX), reset status bits (RSTSTA),
// reset iterations (RSTIT, only ISO7816), reset 'non acknowledge' (RSTNACK) and
// set DTR and RTS high.
 usart0->cr = AVR32_USART_CR_RSTRX_MASK   |
			 AVR32_USART_CR_RSTTX_MASK   |
			 AVR32_USART_CR_RSTSTA_MASK  |
			 AVR32_USART_CR_RSTIT_MASK   |
			 AVR32_USART_CR_RSTNACK_MASK |
			 AVR32_USART_CR_DTRDIS_MASK  |
			 AVR32_USART_CR_RTSDIS_MASK;
// character length 8
usart0->mr |= AVR32_USART_CHRL_8 << AVR32_USART_MR_CHRL_OFFSET;
// no parity and normal channel mode
usart0->mr |= AVR32_USART_PAR_NONE << AVR32_USART_MR_PAR_OFFSET |  AVR32_USART_MR_CHMODE_NORMAL << AVR32_USART_MR_CHMODE_OFFSET;
// 1 stop bit
usart0->mr |= AVR32_USART_MR_NBSTOP_1 << AVR32_USART_MR_NBSTOP_OFFSET;
// set normal mode
usart0->mr = (usart0->mr & ~AVR32_USART_MR_MODE_MASK) | AVR32_USART_MR_MODE_NORMAL << AVR32_USART_MR_MODE_OFFSET;
	
// USCLKS = 0 (CLK_USART), SYNC = 0 (asynchron), OVER = 0 (oversampling 16x)
usart0->mr &= ~(AVR32_USART_MR_USCLKS_MASK | AVR32_USART_MR_SYNC_MASK | AVR32_USART_MR_OVER_MASK);
usart0->mr |= AVR32_USART_MR_USCLKS_MCK << AVR32_USART_MR_USCLKS_OFFSET | AVR32_USART_MR_OVER_X16 << AVR32_USART_MR_OVER_OFFSET;
	

ad 4) The interrupt service routine usart_0_int_handler() is registered as a priority level 0 interrupt.

INTC_register_interrupt(&usart_0_int_handler, AVR32_USART0_IRQ, AVR32_INTC_INT0);

It is called when the USART-receiver has received data or when the USART-transmitter buffer is empty. In both cases data are sent to the user receiver-buffer or fetched from the user transmitter-buffer. If no further data are available for transmission, the USART transmitter interrupt is disabled. Once new data are available, the USART transmitter interrupt will be enabled again and data will be transmitted.

In the initialization of the USART, only the receiver interrupt will be enabled, since no transmitter data are available yet.

ad 5) In a final initialization step the receiver and transmitter of the USART are enabled:

usart0->cr = AVR32_USART_CR_RXEN_MASK | AVR32_USART_CR_TXEN_MASK;

Programs

Application_7 uses direct register programming, which makes the hardware behind the program much more transparent. But it also requires more lines of code since the USART incorporates a number of different protocols. In addtion to the initialization and interrupt service routine in file 'usart.c' and 'usart.h' in the 'support'-directory, some service routines are included to handle the user receiver and transmitter buffer.

The 'main'-program contains a very simple demonstration of the interrupt driven operation of the USART. First a message "This is a Application 7." followed by a CRLF is sent to the host. All incoming characters are reflected to the host.

Application_7a makes use of the ASF software supplied by Atmel in order to create this small program to demonstrate the USART serial link. We will still use our 'clock.c' program in a support directory to setup the system clocks. The following steps have to be performed:

After compilation starting the program will send the message 'This is Application 7a.' to the terminal.

info@tsseshop.com