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