- Main Page
- Related Pages
- Files

# interrupt_usart_example.c

Go to the documentation of this file.

    00001 /*This file is prepared for Doxygen automatic documentation generation.*/
    00116 #include <avr32/io.h>
    00117 #include "compiler.h"
    00118 #include "board.h"
    00119 #include "print_funcs.h"
    00120 #include "intc.h"
    00121 #if __GNUC__
    00122 #  if   __AVR32_AP7000__
    00123 #    include "pm_at32ap7000.h"
    00124 #  else
    00125 #    include "pm.h"
    00126 #  endif
    00127 #elif __ICCAVR32__ || __AAVR32__
    00128 #  if   __AT32AP7000__
    00129 #    include "pm_at32ap7000.h"
    00130 #  else
    00131 #    include "pm.h"
    00132 #  endif
    00133 #endif
    00134 #include "gpio.h"
    00135 #include "usart.h"
    00136
    00137
    00140
    00141 #if BOARD == EVK1100
    00142 #  define EXAMPLE_USART               (&AVR32_USART0)
    00143 #  define EXAMPLE_USART_RX_PIN        AVR32_USART0_RXD_0_0_PIN
    00144 #  define EXAMPLE_USART_RX_FUNCTION   AVR32_USART0_RXD_0_0_FUNCTION
    00145 #  define EXAMPLE_USART_TX_PIN        AVR32_USART0_TXD_0_0_PIN
    00146 #  define EXAMPLE_USART_TX_FUNCTION   AVR32_USART0_TXD_0_0_FUNCTION
    00147 #  define EXAMPLE_USART_IRQ           AVR32_USART0_IRQ
    00148 #  define EXAMPLE_USART_BAUDRATE      57600
    00149 #elif BOARD == EVK1101
    00150 #  define EXAMPLE_USART               (&AVR32_USART1)
    00151 #  define EXAMPLE_USART_RX_PIN        AVR32_USART1_RXD_0_0_PIN
    00152 #  define EXAMPLE_USART_RX_FUNCTION   AVR32_USART1_RXD_0_0_FUNCTION
    00153 #  define EXAMPLE_USART_TX_PIN        AVR32_USART1_TXD_0_0_PIN
    00154 #  define EXAMPLE_USART_TX_FUNCTION   AVR32_USART1_TXD_0_0_FUNCTION
    00155 #  define EXAMPLE_USART_IRQ           AVR32_USART1_IRQ
    00156 #  define EXAMPLE_USART_BAUDRATE      57600
    00157 #elif BOARD == EVK1104
    00158 #  define EXAMPLE_USART               (&AVR32_USART1)
    00159 #  define EXAMPLE_USART_RX_PIN        AVR32_USART1_RXD_0_0_PIN
    00160 #  define EXAMPLE_USART_RX_FUNCTION   AVR32_USART1_RXD_0_0_FUNCTION
    00161 #  define EXAMPLE_USART_TX_PIN        AVR32_USART1_TXD_0_0_PIN
    00162 #  define EXAMPLE_USART_TX_FUNCTION   AVR32_USART1_TXD_0_0_FUNCTION
    00163 #  define EXAMPLE_USART_IRQ           AVR32_USART1_IRQ
    00164 #  define EXAMPLE_USART_BAUDRATE      57600
    00165 #elif BOARD == EVK1105
    00166 #  define EXAMPLE_USART               (&AVR32_USART0)
    00167 #  define EXAMPLE_USART_RX_PIN        AVR32_USART0_RXD_0_0_PIN
    00168 #  define EXAMPLE_USART_RX_FUNCTION   AVR32_USART0_RXD_0_0_FUNCTION
    00169 #  define EXAMPLE_USART_TX_PIN        AVR32_USART0_TXD_0_0_PIN
    00170 #  define EXAMPLE_USART_TX_FUNCTION   AVR32_USART0_TXD_0_0_FUNCTION
    00171 #  define EXAMPLE_USART_IRQ           AVR32_USART0_IRQ
    00172 #  define EXAMPLE_USART_BAUDRATE      57600
    00173 #elif BOARD == STK1000
    00174 #  define EXAMPLE_USART               (&AVR32_USART1)
    00175 #  define EXAMPLE_USART_RX_PIN        AVR32_USART1_RXD_0_PIN
    00176 #  define EXAMPLE_USART_RX_FUNCTION   AVR32_USART1_RXD_0_FUNCTION
    00177 #  define EXAMPLE_USART_TX_PIN        AVR32_USART1_TXD_0_PIN
    00178 #  define EXAMPLE_USART_TX_FUNCTION   AVR32_USART1_TXD_0_FUNCTION
    00179 #  define EXAMPLE_USART_IRQ           AVR32_USART1_IRQ
    00180 #  define EXAMPLE_USART_BAUDRATE      115200
    00181 #elif BOARD == NGW100
    00182 #  define EXAMPLE_USART               (&AVR32_USART1)
    00183 #  define EXAMPLE_USART_RX_PIN        AVR32_USART1_RXD_0_PIN
    00184 #  define EXAMPLE_USART_RX_FUNCTION   AVR32_USART1_RXD_0_FUNCTION
    00185 #  define EXAMPLE_USART_TX_PIN        AVR32_USART1_TXD_0_PIN
    00186 #  define EXAMPLE_USART_TX_FUNCTION   AVR32_USART1_TXD_0_FUNCTION
    00187 #  define EXAMPLE_USART_IRQ           AVR32_USART1_IRQ
    00188 #  define EXAMPLE_USART_BAUDRATE      115200
    00189 #endif
    00191
    00192
    00199 #if __GNUC__
    00200 __attribute__((__interrupt__))
    00201 #elif __ICCAVR32__
    00202 __interrupt
    00203 #endif
    00204 static void usart_int_handler(void)
    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 }
    00225
    00226
    00234 int main(void)
    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 }