00001
00122 #include <avr32/io.h>
00123 #include "compiler.h"
00124 #include "board.h"
00125 #include "print_funcs.h"
00126 #include "intc.h"
00127 #if defined (__GNUC__)
00128 # if defined (__AVR32_AP7000__)
00129 # include "pm_at32ap7000.h"
00130 # else
00131 # include "power_clocks_lib.h"
00132 # endif
00133 #elif defined (__ICCAVR32__) || defined (__AAVR32__)
00134 # if defined (__AT32AP7000__)
00135 # include "pm_at32ap7000.h"
00136 # else
00137 # include "power_clocks_lib.h"
00138 # endif
00139 #endif
00140 #include "gpio.h"
00141 #include "usart.h"
00142
00143
00146
00147 #if BOARD == EVK1100
00148 # define EXAMPLE_USART (&AVR32_USART0)
00149 # define EXAMPLE_USART_RX_PIN AVR32_USART0_RXD_0_0_PIN
00150 # define EXAMPLE_USART_RX_FUNCTION AVR32_USART0_RXD_0_0_FUNCTION
00151 # define EXAMPLE_USART_TX_PIN AVR32_USART0_TXD_0_0_PIN
00152 # define EXAMPLE_USART_TX_FUNCTION AVR32_USART0_TXD_0_0_FUNCTION
00153 # define EXAMPLE_USART_IRQ AVR32_USART0_IRQ
00154 # define EXAMPLE_USART_BAUDRATE 57600
00155 # define EXAMPLE_TARGET_PBACLK_FREQ_HZ FOSC0 // PBA clock target frequency, in Hz
00156 #elif BOARD == EVK1101
00157 # define EXAMPLE_USART (&AVR32_USART1)
00158 # define EXAMPLE_USART_RX_PIN AVR32_USART1_RXD_0_0_PIN
00159 # define EXAMPLE_USART_RX_FUNCTION AVR32_USART1_RXD_0_0_FUNCTION
00160 # define EXAMPLE_USART_TX_PIN AVR32_USART1_TXD_0_0_PIN
00161 # define EXAMPLE_USART_TX_FUNCTION AVR32_USART1_TXD_0_0_FUNCTION
00162 # define EXAMPLE_USART_IRQ AVR32_USART1_IRQ
00163 # define EXAMPLE_USART_BAUDRATE 57600
00164 # define EXAMPLE_TARGET_PBACLK_FREQ_HZ FOSC0 // PBA clock target frequency, in Hz
00165 #elif BOARD == UC3C_EK
00166 # define EXAMPLE_USART (&AVR32_USART2)
00167 # define EXAMPLE_USART_RX_PIN AVR32_USART2_RXD_0_1_PIN
00168 # define EXAMPLE_USART_RX_FUNCTION AVR32_USART2_RXD_0_1_FUNCTION
00169 # define EXAMPLE_USART_TX_PIN AVR32_USART2_TXD_0_1_PIN
00170 # define EXAMPLE_USART_TX_FUNCTION AVR32_USART2_TXD_0_1_FUNCTION
00171 # define EXAMPLE_USART_IRQ AVR32_USART2_IRQ
00172 # define EXAMPLE_USART_BAUDRATE 57600
00173 # define EXAMPLE_TARGET_PBACLK_FREQ_HZ FOSC0 // PBA clock target frequency, in Hz
00174 #elif BOARD == EVK1104
00175 # define EXAMPLE_USART (&AVR32_USART1)
00176 # define EXAMPLE_USART_RX_PIN AVR32_USART1_RXD_0_0_PIN
00177 # define EXAMPLE_USART_RX_FUNCTION AVR32_USART1_RXD_0_0_FUNCTION
00178 # define EXAMPLE_USART_TX_PIN AVR32_USART1_TXD_0_0_PIN
00179 # define EXAMPLE_USART_TX_FUNCTION AVR32_USART1_TXD_0_0_FUNCTION
00180 # define EXAMPLE_USART_IRQ AVR32_USART1_IRQ
00181 # define EXAMPLE_USART_BAUDRATE 57600
00182 # define EXAMPLE_TARGET_PBACLK_FREQ_HZ FOSC0 // PBA clock target frequency, in Hz
00183 #elif BOARD == EVK1105
00184 # define EXAMPLE_USART (&AVR32_USART0)
00185 # define EXAMPLE_USART_RX_PIN AVR32_USART0_RXD_0_0_PIN
00186 # define EXAMPLE_USART_RX_FUNCTION AVR32_USART0_RXD_0_0_FUNCTION
00187 # define EXAMPLE_USART_TX_PIN AVR32_USART0_TXD_0_0_PIN
00188 # define EXAMPLE_USART_TX_FUNCTION AVR32_USART0_TXD_0_0_FUNCTION
00189 # define EXAMPLE_USART_IRQ AVR32_USART0_IRQ
00190 # define EXAMPLE_USART_BAUDRATE 57600
00191 # define EXAMPLE_TARGET_PBACLK_FREQ_HZ FOSC0 // PBA clock target frequency, in Hz
00192 #elif BOARD == STK1000
00193 # define EXAMPLE_USART (&AVR32_USART1)
00194 # define EXAMPLE_USART_RX_PIN AVR32_USART1_RXD_0_PIN
00195 # define EXAMPLE_USART_RX_FUNCTION AVR32_USART1_RXD_0_FUNCTION
00196 # define EXAMPLE_USART_TX_PIN AVR32_USART1_TXD_0_PIN
00197 # define EXAMPLE_USART_TX_FUNCTION AVR32_USART1_TXD_0_FUNCTION
00198 # define EXAMPLE_USART_IRQ AVR32_USART1_IRQ
00199 # define EXAMPLE_USART_BAUDRATE 115200
00200 # define EXAMPLE_TARGET_PBACLK_FREQ_HZ FOSC0 // PBA clock target frequency, in Hz
00201 #elif BOARD == NGW100
00202 # define EXAMPLE_USART (&AVR32_USART1)
00203 # define EXAMPLE_USART_RX_PIN AVR32_USART1_RXD_0_PIN
00204 # define EXAMPLE_USART_RX_FUNCTION AVR32_USART1_RXD_0_FUNCTION
00205 # define EXAMPLE_USART_TX_PIN AVR32_USART1_TXD_0_PIN
00206 # define EXAMPLE_USART_TX_FUNCTION AVR32_USART1_TXD_0_FUNCTION
00207 # define EXAMPLE_USART_IRQ AVR32_USART1_IRQ
00208 # define EXAMPLE_USART_BAUDRATE 115200
00209 # define EXAMPLE_TARGET_PBACLK_FREQ_HZ FOSC0 // PBA clock target frequency, in Hz
00210 #elif BOARD == STK600_RCUC3L0
00211 # define EXAMPLE_USART (&AVR32_USART1)
00212 # define EXAMPLE_USART_RX_PIN AVR32_USART1_RXD_0_1_PIN
00213 # define EXAMPLE_USART_RX_FUNCTION AVR32_USART1_RXD_0_1_FUNCTION
00214
00215 # define EXAMPLE_USART_TX_PIN AVR32_USART1_TXD_0_1_PIN
00216 # define EXAMPLE_USART_TX_FUNCTION AVR32_USART1_TXD_0_1_FUNCTION
00217
00218 # define EXAMPLE_USART_IRQ AVR32_USART1_IRQ
00219 # define EXAMPLE_USART_BAUDRATE 57600
00220 # define EXAMPLE_TARGET_PBACLK_FREQ_HZ FOSC0 // PBA clock target frequency, in Hz
00221 #elif BOARD == UC3L_EK
00222 # define EXAMPLE_USART (&AVR32_USART3)
00223 # define EXAMPLE_USART_RX_PIN AVR32_USART3_RXD_0_0_PIN
00224 # define EXAMPLE_USART_RX_FUNCTION AVR32_USART3_RXD_0_0_FUNCTION
00225 # define EXAMPLE_USART_TX_PIN AVR32_USART3_TXD_0_0_PIN
00226 # define EXAMPLE_USART_TX_FUNCTION AVR32_USART3_TXD_0_0_FUNCTION
00227 # define EXAMPLE_USART_IRQ AVR32_USART3_IRQ
00228 # define EXAMPLE_USART_BAUDRATE 57600
00229 # define EXAMPLE_TARGET_DFLL_FREQ_HZ 96000000 // DFLL target frequency, in Hz
00230 # define EXAMPLE_TARGET_MCUCLK_FREQ_HZ 12000000 // MCU clock target frequency, in Hz
00231 # undef EXAMPLE_TARGET_PBACLK_FREQ_HZ
00232 # define EXAMPLE_TARGET_PBACLK_FREQ_HZ 12000000 // PBA clock target frequency, in Hz
00233
00235
00236 static scif_gclk_opt_t gc_dfllif_ref_opt = { SCIF_GCCTRL_SLOWCLOCK, 0, OFF };
00237 static pcl_freq_param_t pcl_dfll_freq_param =
00238 {
00239 .main_clk_src = PCL_MC_DFLL0,
00240 .cpu_f = EXAMPLE_TARGET_MCUCLK_FREQ_HZ,
00241 .pba_f = EXAMPLE_TARGET_PBACLK_FREQ_HZ,
00242 .pbb_f = EXAMPLE_TARGET_PBACLK_FREQ_HZ,
00243 .dfll_f = EXAMPLE_TARGET_DFLL_FREQ_HZ,
00244 .pextra_params = &gc_dfllif_ref_opt
00245 };
00247 #endif
00249
00250
00257 #if defined (__GNUC__)
00258 __attribute__((__interrupt__))
00259 #elif defined(__ICCAVR32__)
00260 __interrupt
00261 #endif
00262 static void usart_int_handler(void)
00263 {
00264 int c;
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275 usart_read_char(EXAMPLE_USART, &c);
00276
00277
00278
00279
00280
00281 usart_write_char(EXAMPLE_USART, c);
00282 }
00283
00284
00292 int main(void)
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
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
00313
00314
00315 pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP);
00316
00317 #elif BOARD == STK1000
00318 pm_reset();
00319 #elif BOARD == UC3L_EK
00320
00321
00322
00323 pcl_configure_clocks(&pcl_dfll_freq_param);
00324
00325
00326
00327
00328
00329 #endif
00330
00331
00332 gpio_enable_module(USART_GPIO_MAP,
00333 sizeof(USART_GPIO_MAP) / sizeof(USART_GPIO_MAP[0]));
00334
00335
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
00340 Disable_global_interrupt();
00341
00342
00343 INTC_init_interrupts();
00344
00345
00346
00347
00348
00349
00350
00351 INTC_register_interrupt(&usart_int_handler, EXAMPLE_USART_IRQ, AVR32_INTC_INT0);
00352
00353
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
00359 Enable_global_interrupt();
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376 while(1)
00377 {
00378
00379
00380
00381 AVR32_INTC.ipr[0];
00382
00383
00384 SLEEP(AVR32_PM_SMODE_FROZEN);
00385
00386
00387 }
00388 }