00001
00083
00084 #include <avr32/io.h>
00085 #include <stdio.h>
00086 #include "compiler.h"
00087 #include "print_funcs.h"
00088 #include "board.h"
00089 #include "power_clocks_lib.h"
00090 #include "twim.h"
00091 #include "gpio.h"
00092 #include "pdca.h"
00093 #include "intc.h"
00094 #include "cycle_counter.h"
00095 #include "flashc.h"
00096 #include "at42qt1060.h"
00097 #include "conf_at42qt1060.h"
00098 #include "pevc.h"
00099
00100 #define FCPU_HZ 60000000
00101 #define FPBA_HZ 60000000
00102
00103
00104
00105
00106 #define STRING_TRANSFER_SIZE 36
00107
00109
00110
00111 #define PDCA_CHANNEL_USART 0
00112 #define PDCA_CHANNEL_IRQ AVR32_PDCA_IRQ_0
00113 #define PEVC_PDCA_SOT_USER AVR32_PEVC_ID_USER_PDCA_0
00114
00115
00116
00117
00118
00119 volatile Bool touch_detect = FALSE;
00120 volatile avr32_pevc_t* ppevc = &AVR32_PEVC;
00121 volatile avr32_pdca_channel_t *pdca_channel;
00122
00123 static volatile U32 u32PdcaIsr;
00124
00127 static void init_sys_clocks(void)
00128 {
00129 scif_osc32_opt_t opt =
00130 {
00131 .mode = SCIF_OSC_MODE_2PIN_CRYSTAL,
00132 .startup = AVR32_SCIF_OSCCTRL32_STARTUP_0_RCOSC
00133 };
00134
00137
00138 static pcl_freq_param_t pcl_freq_param =
00139 {
00140 .cpu_f = FCPU_HZ,
00141 .pba_f = FPBA_HZ,
00142 .osc0_f = FOSC0,
00143 .osc0_startup = OSC0_STARTUP
00144 };
00146
00147
00148 if (pcl_configure_clocks(&pcl_freq_param) != PASS) {
00149 while(1);
00150 }
00151
00152 scif_start_osc32(&opt,true);
00153 }
00154
00155
00156 unsigned char aDataTransfered[STRING_TRANSFER_SIZE];
00160 #if (defined __GNUC__)
00161 __attribute__((__interrupt__))
00162 #elif (defined __ICCAVR32__)
00163 __interrupt
00164 #endif
00165 static void pdca_int_handler(void)
00166 {
00167 u32PdcaIsr = pdca_channel->isr;
00168 if( u32PdcaIsr & (1<<AVR32_PDCA_ISR0_TRC_OFFSET) )
00169 {
00170
00171 pdca_reload_channel(PDCA_CHANNEL_USART, (void *)aDataTransfered, sizeof( aDataTransfered ));
00172 print_dbg("\n");
00173 }
00174 }
00175
00178 static void twi_init(void)
00179 {
00180 const gpio_map_t AT42QT1060_TWI_GPIO_MAP =
00181 {
00182 {AT42QT1060_TWI_SCL_PIN, AT42QT1060_TWI_SCL_FUNCTION},
00183 {AT42QT1060_TWI_SDA_PIN, AT42QT1060_TWI_SDA_FUNCTION}
00184 };
00185
00186 const twi_options_t AT42QT1060_TWI_OPTIONS =
00187 {
00188 .pba_hz = FPBA_HZ,
00189 .speed = AT42QT1060_TWI_MASTER_SPEED,
00190 .chip = AT42QT1060_TWI_ADDRESS
00191 };
00192
00193
00194 gpio_enable_module(AT42QT1060_TWI_GPIO_MAP,
00195 sizeof(AT42QT1060_TWI_GPIO_MAP) / sizeof(AT42QT1060_TWI_GPIO_MAP[0]));
00196
00197 twi_master_init(AT42QT1060_TWI, &AT42QT1060_TWI_OPTIONS);
00198
00199 }
00200
00204 #if (defined __GNUC__)
00205 __attribute__((__interrupt__))
00206 #elif (defined __ICCAVR32__)
00207 __interrupt
00208 #endif
00209 static void pevc_trg_int_handler(void)
00210 {
00211 int i=0;
00212 for(i=0;i<PEVC_NUMBER_OF_EVENT_USERS;i++) {
00213 if(TRUE == pevc_channel_is_trigger_interrupt_raised(ppevc, i))
00214 {
00215
00216
00217 pevc_channel_clear_trigger_interrupt(ppevc, i);
00218 touch_detect=TRUE;
00219 gpio_tgl_gpio_pin(LED0_GPIO);
00220 }
00221 }
00222 }
00223
00224
00228 #if (defined __GNUC__)
00229 __attribute__((__interrupt__))
00230 #elif (defined __ICCAVR32__)
00231 __interrupt
00232 #endif
00233 static void pevc_ovr_int_handler(void)
00234 {
00235 int i=0;
00236 for(i=0;i<PEVC_NUMBER_OF_EVENT_USERS;i++) {
00237 if(TRUE == pevc_channel_is_overrun_interrupt_raised(ppevc, i))
00238 {
00239
00240 pevc_channel_clear_overrun_interrupt(ppevc, i);
00241 touch_detect=TRUE;
00242 gpio_tgl_gpio_pin(LED1_GPIO);
00243 }
00244 }
00245 }
00246
00250 void init_pevc(void)
00251 {
00252
00253
00254 static const pevc_evs_opt_t PEVC_EVS_OPTIONS =
00255 {
00256 .igfdr = 0x0A,
00257 .igf = PEVC_EVS_IGF_OFF,
00258 .evf = PEVC_EVS_EVF_ON,
00259 .evr = PEVC_EVS_EVR_ON
00260 };
00261
00262
00263 static const gpio_map_t PEVC_GPIO_MAP =
00264 {
00265 {AVR32_PEVC_PAD_EVT_0_0_PIN , AVR32_PEVC_PAD_EVT_0_0_FUNCTION},
00266 {AVR32_PEVC_PAD_EVT_1_0_PIN , AVR32_PEVC_PAD_EVT_1_0_FUNCTION},
00267 {AVR32_PEVC_PAD_EVT_2_0_PIN , AVR32_PEVC_PAD_EVT_2_0_FUNCTION},
00268 {AVR32_PEVC_PAD_EVT_3_0_PIN , AVR32_PEVC_PAD_EVT_3_0_FUNCTION},
00269 {AVR32_PEVC_PAD_EVT_4_0_PIN , AVR32_PEVC_PAD_EVT_4_0_FUNCTION},
00270 {AVR32_PEVC_PAD_EVT_5_0_PIN , AVR32_PEVC_PAD_EVT_5_0_FUNCTION},
00271 {AVR32_PEVC_PAD_EVT_6_0_PIN , AVR32_PEVC_PAD_EVT_6_0_FUNCTION},
00272 {AVR32_PEVC_PAD_EVT_7_0_PIN , AVR32_PEVC_PAD_EVT_7_0_FUNCTION},
00273 {AVR32_PEVC_PAD_EVT_8_0_PIN , AVR32_PEVC_PAD_EVT_8_0_FUNCTION},
00274 {AVR32_PEVC_PAD_EVT_9_0_PIN , AVR32_PEVC_PAD_EVT_9_0_FUNCTION},
00275 {AVR32_PEVC_PAD_EVT_10_0_PIN, AVR32_PEVC_PAD_EVT_10_0_FUNCTION},
00276 {AVR32_PEVC_PAD_EVT_11_0_PIN, AVR32_PEVC_PAD_EVT_11_0_FUNCTION},
00277 {AVR32_PEVC_PAD_EVT_12_0_PIN, AVR32_PEVC_PAD_EVT_12_0_FUNCTION},
00278 {AVR32_PEVC_PAD_EVT_13_0_PIN, AVR32_PEVC_PAD_EVT_13_0_FUNCTION},
00279 {AVR32_PEVC_PAD_EVT_14_0_PIN, AVR32_PEVC_PAD_EVT_14_0_FUNCTION},
00280 {AVR32_PEVC_PAD_EVT_15_0_PIN, AVR32_PEVC_PAD_EVT_15_0_FUNCTION}
00281 };
00282
00283
00284 gpio_enable_module(PEVC_GPIO_MAP,
00285 sizeof(PEVC_GPIO_MAP) / sizeof(PEVC_GPIO_MAP[0]));
00286
00287 Disable_global_interrupt();
00288
00289
00290 INTC_register_interrupt(&pevc_trg_int_handler, AVR32_PEVC_TRIGGER_IRQ, AVR32_INTC_INT0);
00291 INTC_register_interrupt(&pevc_ovr_int_handler, AVR32_PEVC_OVERRUN_IRQ, AVR32_INTC_INT0);
00292
00293 Enable_global_interrupt();
00294
00295
00296 if(FAIL == pevc_channel_configure(ppevc, PEVC_PDCA_SOT_USER,
00297 AVR32_PEVC_ID_GEN_PAD_2,
00298 &PEVC_EVS_OPTIONS))
00299 {
00300 print_dbg("PEVC channel config failed!!!\r\n");
00301 gpio_clr_gpio_pin(LED1_GPIO);
00302 while(1);
00303 }
00304
00305
00306 pevc_channels_enable_trigger_interrupt(ppevc, 1<<AVR32_PEVC_ID_USER_PDCA_0);
00307 pevc_channels_enable_overrun_interrupt(ppevc, 1<<AVR32_PEVC_ID_USER_PDCA_0);
00308
00309
00310 PEVC_CHANNELS_ENABLE(ppevc, 1<<PEVC_PDCA_SOT_USER);
00311
00312 }
00313
00314 void init_pdca(void)
00315 {
00316
00317 static const pdca_channel_options_t PDCA_CH_OPTIONS =
00318 {
00319 .addr = (void *)aDataTransfered,
00320 .pid = AVR32_PDCA_PID_USART2_TX,
00321 .size = 0,
00322 .r_addr = (void *)aDataTransfered,
00323 .r_size = sizeof(aDataTransfered),
00324 .transfer_size = PDCA_TRANSFER_SIZE_BYTE,
00325 .etrig = ENABLED
00326 };
00327
00328 Disable_global_interrupt();
00329
00330
00331 INTC_register_interrupt(&pdca_int_handler, PDCA_CHANNEL_IRQ, AVR32_INTC_INT0);
00332
00333 Enable_global_interrupt();
00334
00335
00336
00337
00338
00339 pdca_init_channel(PDCA_CHANNEL_USART, &PDCA_CH_OPTIONS);
00340 pdca_channel = pdca_get_handler(PDCA_CHANNEL_USART);
00341
00342
00343 pdca_enable_interrupt_transfer_error(PDCA_CHANNEL_USART);
00344 pdca_enable_interrupt_transfer_complete(PDCA_CHANNEL_USART);
00345
00346
00347 pdca_enable(PDCA_CHANNEL_USART);
00348 }
00349
00350
00351 int main(void)
00352 {
00353 int i;
00354
00355 t_cpu_time timeout;
00356
00357 init_sys_clocks();
00358 init_dbg_rs232(FPBA_HZ);
00359 print_dbg("\x0CPEVC Dirver - EXAMPLE 3\r\n");
00360 print_dbg("AVR32 UC3 - PEVC Sofware event example\r\n");
00361
00362 INTC_init_interrupts();
00363
00364
00365 for(i=0;i<sizeof(aDataTransfered);i++)
00366 aDataTransfered[i] = '0' + (i%36);
00367
00368
00369 gpio_set_gpio_pin(LED0_GPIO);
00370 gpio_set_gpio_pin(LED1_GPIO);
00371 gpio_set_gpio_pin(LED2_GPIO);
00372 gpio_set_gpio_pin(LED3_GPIO);
00373 twi_init();
00374
00375
00376
00377
00378
00379
00380 cpu_delay_ms(230, FCPU_HZ);
00381
00382 at42qt1060_init(FCPU_HZ);
00383
00384 cpu_set_timeout( cpu_ms_2_cy(1000, FCPU_HZ), &timeout);
00385
00386 init_pevc();
00387
00388 init_pdca();
00389
00390 while(TRUE)
00391 {
00392
00393 if(touch_detect)
00394 {
00395 touch_detect = FALSE;
00396 at42qt1060_read_reg(AT42QT1060_DETECTION_STATUS);
00397 at42qt1060_read_reg(AT42QT1060_INPUT_PORT_STATUS);
00398 }
00399 cpu_delay_ms(500, FCPU_HZ);
00400 gpio_tgl_gpio_pin(LED3_GPIO);
00401 }
00402
00403 return 0;
00404 }