AVR1500: Xplain training – XMEGA™ Basics
AVR1502: Xplain training – XMEGA Direct Memory Access Controller
• Software prerequisites
Atmel
WinAVR/GCC 20100110 or later
• Hardware prerequisites
Xplain evaluation board
JTAGICE mkII
• Estimated completion time:
1.5 hours
1 Introduction
The USART (Universal Synchronous Asynchronous Receiver Transmitter) is the
key element in serial communications between computers, terminals and other
devices.
This training covers basic setup and use of the Atmel XMEGA USART and the
three tasks will demonstrate how to use the USART I polling-mode, interrupt mode
and how to use the DMAC (Direct Memory Access Controller) to transfer data
without CPU interaction.
®
AVR® Studio® 4.18 or later
8-bit
Microcontrollers
Application Note
Rev. 8319A-AVR-06/10
2 Overview
http://www.BDTIC.com/ATMEL
This training covers some of the Atmel XMEGA USART basic features:
Task 1: Polled mode
The first task shows how to set up the USART in polling mode. Some characters will
be transferred in loop-back mode.
Task 2: Interrupt mode
This task shows how to use a driver to set up the USART. The driver has a ring buffer
that makes life easier for the developer. Also the hardware buffer is shown in this
task.
Task 3: DMAC
Atmel XMEGA introduces Direct Memory Access Controller (DMAC) for 8-bit
processors. With the USART, DMAC is very useful allowing data to flow with nearly
no CPU intervention. This task will show how to set up the USART with the DMAC.
2
AVR1510
8319A-AVR-06/10
3 Task 1: USART in polling mode
http://www.BDTIC.com/ATMEL
Using polling mode with the USART is especially useful when for example debugging
the application or when steps in the program are expected to happen synchronously.
In this task, we will set up the Xplain evaluation board to send data from USART to
another USART. This is a good way to test the USART.
The goal for this task is that you know how to:
• Set up the USART in polling mode
• Send some characters in loop-back mode
• Verify that the transmission was successful
1. Start Atmel AVR Studio and open the project file Polled_Usart.aps in the XMEGAUSART folder
2. On the Xplain evaluation board, connect a jumper or cable between pins PD2 and
PD3
AVR1510
3.1 Baud rate
The Baud rate is calculated by using the peripheral frequency (f
BSEL as parameters. The target frequency and peripheral frequency on the Atmel
XMEGA is set to 2 MHz default. The BSEL bits are setting the baud rate, and the
BSCALE is adding even more functionality, but is ignored at this stage. By setting
BSCALE to 0, the BSEL can be found by Equation 3-1.
-
Equation 3
BSEL
3. Calculate and find the BSEL value for f
4. Verify that the calculated value is the same as in task1.c
5. Compile the project and verify that there are no errors or warnings
1. Equation for Calculating BSEL Value
f
BSCALE
PER
162
⋅
f
BAUD
=
1
−
BAUD
= 9600
), the BSCALE and
PER
8319A-AVR-06/10
3
3.2 Debugging the Polled USART
http://www.BDTIC.com/ATMEL
6. Look through the code and see comments. Try to understand what happens
7. Build the project and start a debug session (click the Play icon)
8. Add a breakpoint on the while-loop as seen in Figure 3-1
9. Add watche
10. Run the code (press F5)
11. Confirm that the
12. Single step (F11) and check that no transfer error occurred (LEDs light up)
Figure 3-1: Add break-point in the last while-loop
s to the
Rx_Buf and the Tx_Buf
Rx_Buf and the Tx_Buf are equal
4
AVR1510
8319A-AVR-06/10
4 Task 2: USART in interrupt mode
http://www.BDTIC.com/ATMEL
USART in interrupt mode will free CPU cycles since the microcontroller doesn’t have
to poll the transmit register to see if it is empty or poll the receive register to see if it
contains new data. This way, the microcontroller can do other and more useful things
than waiting, and increase use of its performance.
New to Atmel XMEGA are the three byte hardware buffers to keep data. The
advantage of this buffer is to reduce occurrences of buffer overflow. Buffer overflow
can occur, for instance, when interrupt service routines (ISRs) with higher priority are
starving out
A driver is used to set up a USART in interrupt mode. The driver also has a ring buffer
implemented which will be explored briefly.
1
other ISRs with lower priority.
AVR1510
As seen from Figure 4-1, the application will send
via the cable to the receiving USART. At the receiver, the driver sends the data to the
application.
Figure 4-1: Data flow in task 2
The goal for this task is to:
• Know how to set up the Atmel XMEGA USART in interrupt mode
• Understand how to use a driver for the setup
• Take a quick look at the ring buffer in the driver
• Understand how the hardware buffer works
data to the driver which transfers it
1. Locate and open the project
2. Connect a jumper between Tx and Rx on PORTD, that is PD2 and PD3
3. Look through the code (
4. Compile the code and assure that there are no warnings or errors
5. Start the debugging session
1
The high priority interrupts are running so frequently that the lower priority interrupt
routines never get time (CPU cycles) to run.
8319A-AVR-06/10
InterruptControlled.aps
task2.c) and try to understand what happens
5
http://www.BDTIC.com/ATMEL
6. Step into
driver file
7. Try to understand how the driver sets up the registers. Step out of the driver file,
press Shift+F11
8. Locate the
USART_InterruptDriver_Initialize() (press F11) and access the
receiveArray[] buffer and add a watch, see Figure 4-2
Figure 4-2. Add a
9. Now, run the code for a while by pressing F5
10. After a short while, break the execution (press Ctrl+F5)
11. Now, take a look at
correctly?
12. Reset the debug session (Shift+F5) and place break-points to the ISR routines.
Run the program and see if it acts as expected
13. Open the
USART_TX_BUFFER_SIZE to 2
14. In
Task2.c, set NUM_BYTES to 7 and recompile the project
15. In the Atmel AVR Studio menu, press Debug->Remove all Breakpoints
16. Run the program for a while (press F5) and break the execution (Ctrl+F5)
Why cannot the program run to completion? (Hint: What sizes are the software ring
buffer and the hardware buffer and what is the size of the array to send.)
watch to the buffer pointers
receiveArray[]. Have the characters been transferred
usart_driver.h and set both USART_RX_BUFFER_SIZE and
6
AVR1510
8319A-AVR-06/10
5 Task 3: USART using DMAC
http://www.BDTIC.com/ATMEL
Atmel XMEGA introduces DMA controller for 8-bit microcontrollers. Using a DMA
controller will offload the CPU when handling data transmission and help increase the
performance of the microcontroller significantly.
This task will show how to set up the USART with the DMAC driver. Figure 5-1
illustrates the data flow in this task using DMA.
AVR1510
The goal for this task is tha
• Set up the USART using DMAC
• Set up the DMAC to read data from SRAM
• Set up the DMAC to write data to USART
• Set up the DMAC to read data from USART
• Set up the DMAC to write data to SRAM
Figure 5-1. Data flow in the DMA example
1. Open the project file
2. Connect a jumper between Tx and Rx on PORTD, that is PD2 and PD3
t you know how to:
USART_DMA.aps in Atmel AVR Studio and open task3.c
8319A-AVR-06/10
3. Study how the Transmit channel is set up (
a.
Tx_Buf is the input for the DMA transmit channel
b. The DMA is set up to increase the address of
c. The USART data register is the output for the DMA transmit channel
d. The DMA is set up to keep the address to the data register fixed during
transmission. Why?
e. Are you able to verify the correct trigger source (Data Register Empty)
in the Atmel XMEGA manual? (0x6C)
4. Study how the Receive channel is set up (
a. The USART data register is the input for the DMA receive channel
b.
Rx_Buf is the output for the DMA receive channel
c. The DMA is set up to have fixed receive data register address and to
increase the address of
d. Are you able to verify that the correct trigger source (Receive
complete) is used in the XMEGA manual?
Rx_Buf. Why?
SetupTransmitChannel)
Tx_Buf. Why?
SetupReceiveChannel)
7
5. Build the project and start a debug session (click the Play icon)
http://www.BDTIC.com/ATMEL
6. Put a breakpoint on the first line after the DMA has completed (
“
). Run the code (press F5)
7. What is shown on the LEDs? Was the CPU able to increment the variable i while
waiting for the DMA to complete?
8. Put a breakpoint on the line
9. Run the code(press F5)
10. Check the status of the LEDs in comparison with the code; do the LEDs blink as
expected?
11. Add watches to
contain characters from a to t, see Figure 5-2.
Figure 5-2. Verif
Rx_Buf and Tx_Buf and compare them, are they equal and
DMA transmission
y
LEDPORT.OUT = Rx_Buf[i];
“LEDPORT.OUT = …
8
AVR1510
8319A-AVR-06/10
6 Summary
http://www.BDTIC.com/ATMEL
7 Resources
Here are some of the high-lights from this training:
• USART in Polling mode
• USART in interrupt mode
• USART driver
• USART software ring buffer
• USART hardware buffer
• USART DMA
• Atmel XMEGA Manual and Datasheets
o http://www.atmel.com/xmega
• Atmel AVR Studio with help files
o http://www.atmel.com/products/AVR/
• WINAVR GCC compiler
o http://winavr.sourceforge.net/
• Atmel IAR Embedded Workbench
ohttp://www.iar.com/
®
compiler
AVR1510
8 Atmel Technical Support Center
Atmel has several support channels available:
• Web portal: http://support.atmel.no/
• Email: avr@atmel.com
• Email: avr32@atmel.com
Please register on the web portal to gain access to the following services:
• Access to a rich FAQ database
• Easy submission of technical support requests
• History of all your past support requests
• Register to receive Atmel microcontrollers’ newsletters
• Get information about available trainings and training material
All Atmel microcontrollers
All Atmel AVR products
All AVR32 products
8319A-AVR-06/10
9
http://www.BDTIC.com/ATMEL
Disclaimer
Headquarters International
Atmel Corporation
2325 Orchard Parkway
San Jose, CA 95131
USA
Tel: 1(408) 441-0311
Fax: 1(408) 487-2600
Atmel Asia
Unit 1-5 & 16, 19/F
BEA Tower, Millennium City 5
418 Kwun Tong Road
Kwun Tong, Kowloon
Hong Kong
Tel: (852) 2245-6100
Fax: (852) 2722-1369
Atmel Europe
Le Krebs
8, Rue Jean-Pierre Timbaud
BP 309
78054 Saint-Quentin-enYvelines Cedex
France
Tel: (33) 1-30-60-70-00
Fax: (33) 1-30-60-71-11
Atmel Japan
9F, Tonetsu Shinkawa Bldg.
1-24-8 Shinkawa
Chuo-ku, Tokyo 104-0033
Japan
Tel: (81) 3-3523-3551
Fax: (81) 3-3523-7581
Product Contact
Web Site
www.atmel.com
Disclaimer: The information in this document is provided in connection with Atmel products. No license, express or implied, by estoppel or otherwise, to any
intellectual property right is granted by this document or in connection with the sale of Atmel products. EXCEPT AS SET FORTH IN ATMEL’S TERMS AND
CONDITIONS OF SALE LOCATED ON ATMEL’S WEB SITE, ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS, IMPLIED
OR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTY OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
CONSEQUENTIAL, PUNITIVE, SPECIAL OR INCIDENTAL DAMAGES (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF PROFITS,
BUSINESS INTERRUPTION, OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT, EVEN IF ATMEL HAS
BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Atmel makes no representations or warranties with respect to the accuracy or completeness of the
contents of this document and reserves the right to make changes to specifications and product descriptions at any time without notice. Atmel does not make any
commitment to update the information contained herein. Unless specifically provided otherwise, Atmel products are not suitable for, and shall not be used in,
automotive applications. Atmel’s products are not intended, authorized, or warranted for use as components in applications intended to support or sustain life.