pevc_example3.c File Reference


Detailed Description

PEVC example 3 application for AVR32 using PEVC IO as generator and PDCA as event user.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file pevc_example3.c.

#include <avr32/io.h>
#include <stdio.h>
#include "compiler.h"
#include "print_funcs.h"
#include "board.h"
#include "power_clocks_lib.h"
#include "twim.h"
#include "gpio.h"
#include "pdca.h"
#include "intc.h"
#include "cycle_counter.h"
#include "flashc.h"
#include "at42qt1060.h"
#include "conf_at42qt1060.h"
#include "pevc.h"

Go to the source code of this file.

Defines

#define FCPU_HZ   60000000
#define FPBA_HZ   60000000
#define PDCA_CHANNEL_IRQ   AVR32_PDCA_IRQ_0
#define PDCA_CHANNEL_USART   0
 The PDCA channel instance for the USART0 Tx.
#define PEVC_PDCA_SOT_USER   AVR32_PEVC_ID_USER_PDCA_0
#define STRING_TRANSFER_SIZE   36

Functions

void init_pdca (void)
void init_pevc (void)
 Init PEVC Toggle LED when the Busy pin change.
int main (void)
static void pdca_int_handler (void)
static void pevc_ovr_int_handler (void)
static void pevc_trg_int_handler (void)
static void twi_init (void)
 Initializes the TWI for AT42QT1060.

Variables

volatile avr32_pdca_channel_t * pdca_channel
volatile avr32_pevc_t * ppevc = &AVR32_PEVC
volatile Bool touch_detect = FALSE
static volatile U32 u32PdcaIsr
System Clock Frequencies
Initializes the MCU system clocks.

unsigned char aDataTransfered [STRING_TRANSFER_SIZE]


Define Documentation

#define FCPU_HZ   60000000

Definition at line 100 of file pevc_example3.c.

#define FPBA_HZ   60000000

Definition at line 101 of file pevc_example3.c.

#define PDCA_CHANNEL_IRQ   AVR32_PDCA_IRQ_0

Definition at line 112 of file pevc_example3.c.

#define PDCA_CHANNEL_USART   0

The PDCA channel instance for the USART0 Tx.

Definition at line 111 of file pevc_example3.c.

#define PEVC_PDCA_SOT_USER   AVR32_PEVC_ID_USER_PDCA_0

Definition at line 113 of file pevc_example3.c.

#define STRING_TRANSFER_SIZE   36

Definition at line 106 of file pevc_example3.c.


Function Documentation

void init_pdca ( void   ) 

Definition at line 308 of file pevc_example3.c.

References aDataTransfered, pdca_channel, PDCA_CHANNEL_IRQ, PDCA_CHANNEL_USART, and pdca_int_handler().

00309 {
00310   // PDCA channel 0/1 options
00311   static const pdca_channel_options_t PDCA_CH_OPTIONS =
00312   {
00313     .addr = (void *)aDataTransfered,          // memory address
00314     .pid = AVR32_PDCA_PID_USART2_TX,          // select peripheral - data are transmit on USART TX line.
00315     .size = 0,                                // transfer counter
00316     .r_addr = (void *)aDataTransfered,        // next memory address
00317     .r_size = sizeof(aDataTransfered),        // next transfer counter
00318     .transfer_size = PDCA_TRANSFER_SIZE_BYTE, // select size of one data packet
00319     .etrig = ENABLED                          // Trigger transfer on event.
00320   };
00321   
00322   Disable_global_interrupt();
00323 
00324   // Register the PDCA interrupt handler to the interrupt controller.
00325   INTC_register_interrupt(&pdca_int_handler, PDCA_CHANNEL_IRQ, AVR32_INTC_INT0);
00326   
00327   Enable_global_interrupt();
00328   
00329   //##
00330   //## PDCA Init.
00331   //##
00332   // Init PDCA channel with the pdca_options.
00333   pdca_init_channel(PDCA_CHANNEL_USART, &PDCA_CH_OPTIONS);
00334   pdca_channel = pdca_get_handler(PDCA_CHANNEL_USART); // For use in the pdca interrupt handler.
00335 
00336   // Enable pdca transfer error interrupt & transfer complete interrupt.
00337   pdca_enable_interrupt_transfer_error(PDCA_CHANNEL_USART);
00338   pdca_enable_interrupt_transfer_complete(PDCA_CHANNEL_USART);  
00339  
00340   // Enable the PDCA.
00341   pdca_enable(PDCA_CHANNEL_USART);
00342 }

void init_pevc ( void   ) 

Init PEVC Toggle LED when the Busy pin change.

Definition at line 245 of file pevc_example3.c.

References AVR32_PEVC_ID_GEN_PAD_2, AVR32_PEVC_ID_USER_PDCA_0, pevc_channel_configure(), PEVC_CHANNELS_ENABLE, pevc_channels_enable_overrun_interrupt(), pevc_channels_enable_trigger_interrupt(), PEVC_EVS_EVF_ON, PEVC_EVS_EVR_ON, PEVC_EVS_IGF_OFF, pevc_ovr_int_handler(), PEVC_PDCA_SOT_USER, pevc_trg_int_handler(), and ppevc.

00246 {
00247   
00248   // PEVC Event Shaper options.
00249   static const pevc_evs_opt_t PEVC_EVS_OPTIONS = 
00250   {
00251     .igfdr = 0x0A,            // Set the IGF clock to be clk_slow*2pow11
00252     .igf = PEVC_EVS_IGF_OFF,  // Input Glitch Filter off
00253     .evf = PEVC_EVS_EVF_ON,   // Enable Event on falling edge
00254     .evr = PEVC_EVS_EVR_ON    // Enable Event on rising edge
00255   };
00256   
00257   // Need to Setup GPIO ahs PEVC PAD EVT
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   // Assign GPIO pins to USART0.
00279   gpio_enable_module(PEVC_GPIO_MAP,
00280                      sizeof(PEVC_GPIO_MAP) / sizeof(PEVC_GPIO_MAP[0]));
00281  
00282   Disable_global_interrupt();
00283   
00284   // Register the PEVC interrupt handlers to the interrupt controller.
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   // Assign PEVC PAD 2 to PDCA
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   // Enable the PEVC trigger interrupt.  
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   // Enable the PEVC channel "PDCA CHANNEL 0/1 ONE-ITEM-TRANSFER"
00305   PEVC_CHANNELS_ENABLE(ppevc, 1<<PEVC_PDCA_SOT_USER);
00306 }

int main ( void   ) 

Definition at line 345 of file pevc_example3.c.

References aDataTransfered, FCPU_HZ, FPBA_HZ, init_pdca(), init_pevc(), touch_detect, and twi_init().

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    // Init the string with a simple recognizable pattern.
00359   for(i=0;i<sizeof(aDataTransfered);i++)
00360     aDataTransfered[i] = '0' + (i%36);
00361 
00362   // Activate LED0 & LED1 & LED2 & LED3 pins in GPIO output mode and switch them off.
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   /* Power up delay: This device needs a maximum of 230ms before it
00371    * can be accessed. Another method would be to wait until the the 
00372    * detect line goes low but this won't work during a debugging session
00373    * since the device is not reset.*/
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     // if a touch is detected we read the status
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 }

static void pdca_int_handler ( void   )  [static]

PDCA Interrupts handler.

Definition at line 161 of file pevc_example3.c.

References aDataTransfered, pdca_channel, PDCA_CHANNEL_USART, and u32PdcaIsr.

00162 {
00163   u32PdcaIsr = pdca_channel->isr;
00164   if( u32PdcaIsr & (1<<AVR32_PDCA_ISR0_TRC_OFFSET) )
00165   {
00166     // Count the number of Transfer Complete interrupts.
00167     pdca_reload_channel(PDCA_CHANNEL_USART, (void *)aDataTransfered, sizeof( aDataTransfered ));
00168     print_dbg("\n");
00169   }
00170 }

static void pevc_ovr_int_handler ( void   )  [static]

PEVC overrun Interrupt handler.

Definition at line 228 of file pevc_example3.c.

References pevc_channel_clear_overrun_interrupt(), pevc_channel_is_overrun_interrupt_raised(), PEVC_NUMBER_OF_EVENT_USERS, ppevc, and touch_detect.

Referenced by init_pevc().

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     { // An overrun on the channel Id=i occured.
00234       // Clear the interrupt
00235       pevc_channel_clear_overrun_interrupt(ppevc, i);
00236       touch_detect=TRUE;
00237       gpio_tgl_gpio_pin(LED1_GPIO);
00238     }
00239  }
00240 }

static void pevc_trg_int_handler ( void   )  [static]

PEVC trigger interrupt handler.

Definition at line 205 of file pevc_example3.c.

References pevc_channel_clear_trigger_interrupt(), pevc_channel_is_trigger_interrupt_raised(), PEVC_NUMBER_OF_EVENT_USERS, ppevc, and touch_detect.

Referenced by init_pevc().

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       // An overrun on the channel Id=i occured.
00212       // Clear the interrupt
00213       pevc_channel_clear_trigger_interrupt(ppevc, i);
00214       touch_detect=TRUE;
00215       gpio_tgl_gpio_pin(LED0_GPIO);  
00216     }
00217  }
00218 }

static void twi_init ( void   )  [static]

Initializes the TWI for AT42QT1060.

Definition at line 174 of file pevc_example3.c.

References AT42QT1060_TWI, AT42QT1060_TWI_ADDRESS, AT42QT1060_TWI_MASTER_SPEED, AT42QT1060_TWI_SCL_FUNCTION, AT42QT1060_TWI_SCL_PIN, AT42QT1060_TWI_SDA_FUNCTION, AT42QT1060_TWI_SDA_PIN, and FPBA_HZ.

Referenced by main().

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   // Assign I/Os to SPI.
00190   gpio_enable_module(AT42QT1060_TWI_GPIO_MAP,
00191     sizeof(AT42QT1060_TWI_GPIO_MAP) / sizeof(AT42QT1060_TWI_GPIO_MAP[0]));
00192   // Initialize as master.
00193   twi_master_init(AT42QT1060_TWI, &AT42QT1060_TWI_OPTIONS);
00194 
00195 }


Variable Documentation

unsigned char aDataTransfered[STRING_TRANSFER_SIZE]

Definition at line 152 of file pevc_example3.c.

volatile avr32_pdca_channel_t* pdca_channel

Definition at line 117 of file pevc_example3.c.

volatile avr32_pevc_t* ppevc = &AVR32_PEVC

Definition at line 116 of file pevc_example3.c.

volatile Bool touch_detect = FALSE

Definition at line 115 of file pevc_example3.c.

Referenced by main(), pevc_ovr_int_handler(), and pevc_trg_int_handler().

volatile U32 u32PdcaIsr [static]

Definition at line 119 of file pevc_example3.c.


Generated on Fri Feb 19 02:25:33 2010 for AVR32 - PEVC Driver Example 3 by  doxygen 1.5.5