# interrupt_usart_example.c File Reference

## Detailed Description

Example of an interrupt-driven USART communication.

This file contains an example using the USART module with interrupts.

It will display a string on the screen. Refer to the usart_options structure in main for configuration options for your terminal.

Please make sure that the correct jumper(s) is/are set on your development board. Refer to your hardware reference guide if necessary.

- Compiler: IAR EWAVR32 and GNU GCC for AVR32
- Supported devices: All AVR32 devices with an INTC module can be used.
- AppNote:

Author:
  Atmel Corporation: http://www.atmel.com
  Support and FAQ: http://support.atmel.no/

Definition in file interrupt_usart_example.c.

`#include <avr32/io.h>`
`#include "compiler.h"`
`#include "board.h"`
`#include "print_funcs.h"`
`#include "intc.h"`
`#include "pm_at32ap7000.h"`
`#include "gpio.h"`
`#include "usart.h"`

Go to the source code of this file.

## Defines
USART Settings
#define | EXAMPLE_USART (&AVR32_USART1)
#define | EXAMPLE_USART_BAUDRATE 115200
#define | EXAMPLE_USART_IRQ AVR32_USART1_IRQ
#define | EXAMPLE_USART_RX_FUNCTION AVR32_USART1_RXD_0_FUNCTION
#define | EXAMPLE_USART_RX_PIN AVR32_USART1_RXD_0_PIN
#define | EXAMPLE_USART_TX_FUNCTION AVR32_USART1_TXD_0_FUNCTION
#define | EXAMPLE_USART_TX_PIN AVR32_USART1_TXD_0_PIN
## Functions
int | main (void)
| The main function.
static void | usart_int_handler (void)
| The USART interrupt handler.

## Define Documentation

#define EXAMPLE_USART (&AVR32_USART1)

Definition at line 174 of file interrupt_usart_example.c.

Referenced by main(), and usart_int_handler().

#define EXAMPLE_USART_BAUDRATE 115200

Definition at line 180 of file interrupt_usart_example.c.

Referenced by main().

#define EXAMPLE_USART_IRQ AVR32_USART1_IRQ

Definition at line 179 of file interrupt_usart_example.c.

Referenced by main().

#define EXAMPLE_USART_RX_FUNCTION AVR32_USART1_RXD_0_FUNCTION

Definition at line 176 of file interrupt_usart_example.c.

Referenced by main().

#define EXAMPLE_USART_RX_PIN AVR32_USART1_RXD_0_PIN

Definition at line 175 of file interrupt_usart_example.c.

Referenced by main().

#define EXAMPLE_USART_TX_FUNCTION AVR32_USART1_TXD_0_FUNCTION

Definition at line 178 of file interrupt_usart_example.c.

Referenced by main().

#define EXAMPLE_USART_TX_PIN AVR32_USART1_TXD_0_PIN

Definition at line 177 of file interrupt_usart_example.c.

Referenced by main().

## Function Documentation

int main | ( | void |  | ) |

The main function.

It sets up the USART module on EXAMPLE_USART. The terminal settings are 57600 8N1. Then it sets up the interrupt handler and waits for a USART interrupt to trigger.

Definition at line 234 of file interrupt_usart_example.c.

References EXAMPLE_USART, EXAMPLE_USART_BAUDRATE, EXAMPLE_USART_IRQ, EXAMPLE_USART_RX_FUNCTION, EXAMPLE_USART_RX_PIN, EXAMPLE_USART_TX_FUNCTION, EXAMPLE_USART_TX_PIN, INTC_init_interrupts(), INTC_register_interrupt(), and usart_int_handler().

    00235 {
    00236   static const gpio_map_t USART_GPIO_MAP =
    00237   {
    00238     {EXAMPLE_USART_RX_PIN, EXAMPLE_USART_RX_FUNCTION},
    00239     {EXAMPLE_USART_TX_PIN, EXAMPLE_USART_TX_FUNCTION}
    00240   };
    00241
    00242   // USART options.
    00243   static const usart_options_t USART_OPTIONS =
    00244   {
    00245     .baudrate     = EXAMPLE_USART_BAUDRATE,
    00246     .charlength   = 8,
    00247     .paritytype   = USART_NO_PARITY,
    00248     .stopbits     = USART_1_STOPBIT,
    00249     .channelmode  = USART_NORMAL_CHMODE
    00250   };
    00251
    00252 #if BOARD == EVK1100 || BOARD == EVK1101 || BOARD == EVK1104 || BOARD == EVK1105
    00253
    00254   // Switch main clock to external oscillator 0 (crystal).
    00255   pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);
    00256
    00257 #elif BOARD == STK1000
    00258   pm_reset();
    00259 #endif
    00260
    00261   // Assign GPIO to USART.
    00262   gpio_enable_module(USART_GPIO_MAP,
    00263                      sizeof(USART_GPIO_MAP) / sizeof(USART_GPIO_MAP[0]));
    00264
    00265   // Initialize USART in RS232 mode.
    00266   usart_init_rs232(EXAMPLE_USART, &USART_OPTIONS, FOSC0);
    00267   print(EXAMPLE_USART, ".: Using interrupts with the USART :.\n\n");
    00268
    00269   // Disable all interrupts.
    00270   Disable_global_interrupt();
    00271
    00272   // Initialize interrupt vectors.
    00273   INTC_init_interrupts();
    00274
    00275   // Register the USART interrupt handler to the interrupt controller.
    00276   // usart_int_handler is the interrupt handler to register.
    00277   // EXAMPLE_USART_IRQ is the IRQ of the interrupt handler to register.
    00278   // AVR32_INTC_INT0 is the interrupt priority level to assign to the group of
    00279   // this IRQ.
    00280   // void INTC_register_interrupt(__int_handler handler, unsigned int irq, unsigned int int_lev);
    00281   INTC_register_interrupt(&usart_int_handler, EXAMPLE_USART_IRQ, AVR32_INTC_INT0);
    00282
    00283   // Enable USART Rx interrupt.
    00284   EXAMPLE_USART->ier = AVR32_USART_IER_RXRDY_MASK;
    00285   print(EXAMPLE_USART, "Type a character to use the interrupt handler.\n"
    00286                        "It will show up on your screen.\n\n");
    00287
    00288   // Enable all interrupts.
    00289   Enable_global_interrupt();
    00290
    00291   while (TRUE);
    00292 }

static void usart_int_handler | ( | void |  | ) | `[static]`

The USART interrupt handler.

Note:
  The `__attribute__((__interrupt__))' (under GNU GCC for AVR32) and `__interrupt' (under IAR Embedded Workbench for Atmel AVR32) C function attributes are used to manage the `rete' instruction.

Definition at line 204 of file interrupt_usart_example.c.

References EXAMPLE_USART.

Referenced by main().

    00205 {
    00206   int c;
    00207
    00208   // In the code line below, the interrupt priority level does not need to be
    00209   // explicitly masked as it is already because we are within the interrupt
    00210   // handler.
    00211   // The USART Rx interrupt flag is cleared by side effect when reading the
    00212   // received character.
    00213   // Waiting until the interrupt has actually been cleared is here useless as
    00214   // the call to usart_write_char will take enough time for this before the
    00215   // interrupt handler is leaved and the interrupt priority level is unmasked by
    00216   // the CPU.
    00217   usart_read_char(EXAMPLE_USART, &c);
    00218
    00219   // Print the received character to USART.
    00220   // It is a simple echo, so there will be no translation of '\r' to "\r\n". The
    00221   // connected terminal has to be configured accordingly to send '\n' after
    00222   // '\r'.
    00223   usart_write_char(EXAMPLE_USART, c);
    00224 }