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.
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 "power_clocks_lib.h"
#include "gpio.h"
#include "usart.h"
Go to the source code of this file.
Defines | |
USART Settings | |
#define | EXAMPLE_TARGET_DFLL_FREQ_HZ 96000000 |
#define | EXAMPLE_TARGET_MCUCLK_FREQ_HZ 12000000 |
#define | EXAMPLE_TARGET_PBACLK_FREQ_HZ 12000000 |
#define | EXAMPLE_USART (&AVR32_USART3) |
#define | EXAMPLE_USART_BAUDRATE 57600 |
#define | EXAMPLE_USART_IRQ AVR32_USART3_IRQ |
#define | EXAMPLE_USART_RX_FUNCTION AVR32_USART3_RXD_0_0_FUNCTION |
#define | EXAMPLE_USART_RX_PIN AVR32_USART3_RXD_0_0_PIN |
#define | EXAMPLE_USART_TX_FUNCTION AVR32_USART3_TXD_0_0_FUNCTION |
#define | EXAMPLE_USART_TX_PIN AVR32_USART3_TXD_0_0_PIN |
Functions | |
int | main (void) |
The main function. | |
static void | usart_int_handler (void) |
The USART interrupt handler. | |
Variables | |
Parameters to pcl_configure_clocks(). | |
static scif_gclk_opt_t | gc_dfllif_ref_opt = { SCIF_GCCTRL_SLOWCLOCK, 0, OFF } |
static pcl_freq_param_t | pcl_dfll_freq_param |
#define EXAMPLE_TARGET_DFLL_FREQ_HZ 96000000 |
Definition at line 229 of file interrupt_usart_example.c.
#define EXAMPLE_TARGET_MCUCLK_FREQ_HZ 12000000 |
Definition at line 230 of file interrupt_usart_example.c.
#define EXAMPLE_TARGET_PBACLK_FREQ_HZ 12000000 |
#define EXAMPLE_USART (&AVR32_USART3) |
Definition at line 222 of file interrupt_usart_example.c.
Referenced by main(), and usart_int_handler().
#define EXAMPLE_USART_BAUDRATE 57600 |
#define EXAMPLE_USART_IRQ AVR32_USART3_IRQ |
#define EXAMPLE_USART_RX_FUNCTION AVR32_USART3_RXD_0_0_FUNCTION |
#define EXAMPLE_USART_RX_PIN AVR32_USART3_RXD_0_0_PIN |
#define EXAMPLE_USART_TX_FUNCTION AVR32_USART3_TXD_0_0_FUNCTION |
#define EXAMPLE_USART_TX_PIN AVR32_USART3_TXD_0_0_PIN |
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 292 of file interrupt_usart_example.c.
References EXAMPLE_TARGET_PBACLK_FREQ_HZ, 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(), pcl_dfll_freq_param, and usart_int_handler().
00293 { 00294 static const gpio_map_t USART_GPIO_MAP = 00295 { 00296 {EXAMPLE_USART_RX_PIN, EXAMPLE_USART_RX_FUNCTION}, 00297 {EXAMPLE_USART_TX_PIN, EXAMPLE_USART_TX_FUNCTION} 00298 }; 00299 00300 // USART options. 00301 static const usart_options_t USART_OPTIONS = 00302 { 00303 .baudrate = EXAMPLE_USART_BAUDRATE, 00304 .charlength = 8, 00305 .paritytype = USART_NO_PARITY, 00306 .stopbits = USART_1_STOPBIT, 00307 .channelmode = USART_NORMAL_CHMODE 00308 }; 00309 00310 #if BOARD == EVK1100 || BOARD == EVK1101 || BOARD == UC3C_EK || BOARD == EVK1104 || BOARD == EVK1105 || BOARD == STK600_RCUC3L0 00311 00312 // Configure Osc0 in crystal mode (i.e. use of an external crystal source, with 00313 // frequency FOSC0) with an appropriate startup time then switch the main clock 00314 // source to Osc0. 00315 pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP); 00316 00317 #elif BOARD == STK1000 00318 pm_reset(); 00319 #elif BOARD == UC3L_EK 00320 // Note: on the AT32UC3L-EK board, there is no crystal/external clock connected 00321 // to the OSC0 pinout XIN0/XOUT0. We shall then program the DFLL and switch the 00322 // main clock source to the DFLL. 00323 pcl_configure_clocks(&pcl_dfll_freq_param); 00324 // Note: since it is dynamically computing the appropriate field values of the 00325 // configuration registers from the parameters structure, this function is not 00326 // optimal in terms of code size. For a code size optimal solution, it is better 00327 // to create a new function from pcl_configure_clocks_dfll0() and modify it 00328 // to use preprocessor computation from pre-defined target frequencies. 00329 #endif 00330 00331 // Assign GPIO to USART. 00332 gpio_enable_module(USART_GPIO_MAP, 00333 sizeof(USART_GPIO_MAP) / sizeof(USART_GPIO_MAP[0])); 00334 00335 // Initialize USART in RS232 mode. 00336 usart_init_rs232(EXAMPLE_USART, &USART_OPTIONS, EXAMPLE_TARGET_PBACLK_FREQ_HZ); 00337 print(EXAMPLE_USART, ".: Using interrupts with the USART :.\n\n"); 00338 00339 // Disable all interrupts. 00340 Disable_global_interrupt(); 00341 00342 // Initialize interrupt vectors. 00343 INTC_init_interrupts(); 00344 00345 // Register the USART interrupt handler to the interrupt controller. 00346 // usart_int_handler is the interrupt handler to register. 00347 // EXAMPLE_USART_IRQ is the IRQ of the interrupt handler to register. 00348 // AVR32_INTC_INT0 is the interrupt priority level to assign to the group of 00349 // this IRQ. 00350 // void INTC_register_interrupt(__int_handler handler, unsigned int irq, unsigned int int_level); 00351 INTC_register_interrupt(&usart_int_handler, EXAMPLE_USART_IRQ, AVR32_INTC_INT0); 00352 00353 // Enable USART Rx interrupt. 00354 EXAMPLE_USART->ier = AVR32_USART_IER_RXRDY_MASK; 00355 print(EXAMPLE_USART, "Type a character to use the interrupt handler.\n" 00356 "It will show up on your screen.\n\n"); 00357 00358 // Enable all interrupts. 00359 Enable_global_interrupt(); 00360 00361 // We have nothing left to do in the main, so we may switch to a device sleep 00362 // mode: we just need to be sure that the USART module will be still be active 00363 // in the chosen sleep mode. The sleep mode to use is the FROZEN sleep mode: 00364 // in this mode the PB clocks are still active (so the USART module which is 00365 // on the Peripheral Bus will still be active while the CPU and HSB will be 00366 // stopped). 00367 // -- 00368 // Modules communicating with external circuits should normally be disabled 00369 // before entering a sleep mode that will stop the module operation: this is not 00370 // the case for the FROZEN sleep mode. 00371 // -- 00372 // When the USART interrupt occurs, this will wake the CPU up which will then 00373 // execute the interrupt handler code then come back to the while(1) loop below 00374 // to execute the sleep instruction again. 00375 00376 while(1) 00377 { 00378 // If there is a chance that any PB write operations are incomplete, the CPU 00379 // should perform a read operation from any register on the PB bus before 00380 // executing the sleep instruction. 00381 AVR32_INTC.ipr[0]; // Dummy read 00382 00383 // Go to FROZEN sleep mode. 00384 SLEEP(AVR32_PM_SMODE_FROZEN); 00385 // When the device wakes up due to an interrupt, once the interrupt is serviced, 00386 // go back into FROZEN sleep mode. 00387 } 00388 }
static void usart_int_handler | ( | void | ) | [static] |
The USART interrupt handler.
Definition at line 262 of file interrupt_usart_example.c.
References EXAMPLE_USART.
Referenced by main().
00263 { 00264 int c; 00265 00266 // In the code line below, the interrupt priority level does not need to be 00267 // explicitly masked as it is already because we are within the interrupt 00268 // handler. 00269 // The USART Rx interrupt flag is cleared by side effect when reading the 00270 // received character. 00271 // Waiting until the interrupt has actually been cleared is here useless as 00272 // the call to usart_write_char will take enough time for this before the 00273 // interrupt handler is leaved and the interrupt priority level is unmasked by 00274 // the CPU. 00275 usart_read_char(EXAMPLE_USART, &c); 00276 00277 // Print the received character to USART. 00278 // It is a simple echo, so there will be no translation of '\r' to "\r\n". The 00279 // connected terminal has to be configured accordingly to send '\n' after 00280 // '\r'. 00281 usart_write_char(EXAMPLE_USART, c); 00282 }
scif_gclk_opt_t gc_dfllif_ref_opt = { SCIF_GCCTRL_SLOWCLOCK, 0, OFF } [static] |
Definition at line 236 of file interrupt_usart_example.c.
pcl_freq_param_t pcl_dfll_freq_param [static] |
Initial value:
{ .main_clk_src = PCL_MC_DFLL0, .cpu_f = EXAMPLE_TARGET_MCUCLK_FREQ_HZ, .pba_f = EXAMPLE_TARGET_PBACLK_FREQ_HZ, .pbb_f = EXAMPLE_TARGET_PBACLK_FREQ_HZ, .dfll_f = EXAMPLE_TARGET_DFLL_FREQ_HZ, .pextra_params = &gc_dfllif_ref_opt }
Definition at line 237 of file interrupt_usart_example.c.
Referenced by main().