pevc_example3.c File Reference


Detailed Description

PEVC example 3 application for AVR32 using PEVC IO as generator and DMA has 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 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 314 of file pevc_example3.c.

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

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

void init_pevc ( void   ) 

init pevc. Toggle led when Busy pin change

Definition at line 250 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.

00251 {
00252   
00253   // PEVC Event Shaper options.
00254   static const pevc_evs_opt_t PEVC_EVS_OPTIONS = 
00255   {
00256     .igfdr = 0x0A,            // Set the IGF clock to be clk_slow*2pow11
00257     .igf = PEVC_EVS_IGF_OFF,  // Input Glitch Filter off
00258     .evf = PEVC_EVS_EVF_ON,   // Enable Event on falling edge
00259     .evr = PEVC_EVS_EVR_ON    // Enable Event on rising edge
00260   };
00261   
00262   // Need to Setup GPIO ahs PEVC PAD EVT
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   // Assign GPIO pins to USART0.
00284   gpio_enable_module(PEVC_GPIO_MAP,
00285                      sizeof(PEVC_GPIO_MAP) / sizeof(PEVC_GPIO_MAP[0]));
00286  
00287   Disable_global_interrupt();
00288   
00289   // Register the PEVC interrupt handlers to the interrupt controller.
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   // Assign PEVC PAD 2 to PDCA
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   // Enable the PEVC trigger interrupt.  
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   // Enable the PEVC channel "PDCA CHANNEL 0/1 ONE-ITEM-TRANSFER"
00310   PEVC_CHANNELS_ENABLE(ppevc, 1<<PEVC_PDCA_SOT_USER);
00311     
00312 }

int main ( void   ) 

Definition at line 351 of file pevc_example3.c.

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

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

static void pdca_int_handler ( void   )  [static]

PDCA Interrupts handler.

Definition at line 165 of file pevc_example3.c.

References aDataTransfered, pdca_channel, PDCA_CHANNEL_USART, and u32PdcaIsr.

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

static void pevc_ovr_int_handler ( void   )  [static]

PEVC overrun Interrupt handler.

Definition at line 233 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().

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     { // An overrun on the channel Id=i occured.
00239       // Clear the interrupt
00240       pevc_channel_clear_overrun_interrupt(ppevc, i);
00241       touch_detect=TRUE;
00242       gpio_tgl_gpio_pin(LED1_GPIO);
00243     }
00244  }
00245 }

static void pevc_trg_int_handler ( void   )  [static]

PEVC trigger Interrupt handler.

Definition at line 209 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().

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

static void twi_init ( void   )  [static]

Initializes the TWI for AT42QT1060.

Definition at line 178 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().

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


Variable Documentation

unsigned char aDataTransfered[STRING_TRANSFER_SIZE]

Definition at line 156 of file pevc_example3.c.

volatile avr32_pdca_channel_t* pdca_channel

Definition at line 121 of file pevc_example3.c.

volatile avr32_pevc_t* ppevc = &AVR32_PEVC

Definition at line 120 of file pevc_example3.c.

volatile Bool touch_detect = FALSE

Definition at line 119 of file pevc_example3.c.

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

volatile U32 u32PdcaIsr [static]

Definition at line 123 of file pevc_example3.c.


Generated on Thu Dec 17 19:58:51 2009 for AVR32 - PEVC Driver Example 3 by  doxygen 1.5.5