00001
00081 #include <avr32/io.h>
00082 #include "board.h"
00083 #include "intc.h"
00084 #include "power_clocks_lib.h"
00085 #include "pevc.h"
00086 #include "gpio.h"
00087 #include "usart.h"
00088 #include "print_funcs.h"
00089 #include "pdca.h"
00090 #include "ast.h"
00091 #include "delay.h"
00092
00093
00094 #define FCPU_HZ 60000000
00095 #define FPBA_HZ 60000000
00096
00099
00100 #define STRING_TRANSFER_SIZE 36
00102
00103
00107
00108
00109
00110 #define PDCA_CHANNEL_USART 0
00111 #define PDCA_CHANNEL_IRQ AVR32_PDCA_IRQ_0
00112 #define PEVC_PDCA_SOT_USER AVR32_PEVC_ID_USER_PDCA_0
00113
00114
00115
00116
00117
00118
00119 volatile avr32_pdca_channel_t *pdca_channel;
00120 volatile avr32_pevc_t *ppevc = &AVR32_PEVC;
00121 volatile avr32_pm_t *pm = &AVR32_PM;
00122
00123 static volatile U32 u32PdcaIsr;
00124
00125
00126 unsigned char aDataTransfered[STRING_TRANSFER_SIZE];
00127
00131 #if (defined __GNUC__)
00132 __attribute__((__interrupt__))
00133 #elif (defined __ICCAVR32__)
00134 __interrupt
00135 #endif
00136 static void pdca_int_handler(void)
00137 {
00138 u32PdcaIsr = pdca_channel->isr;
00139 if( u32PdcaIsr & (1<<AVR32_PDCA_ISR0_TRC_OFFSET) )
00140 {
00141
00142 pdca_reload_channel(PDCA_CHANNEL_USART, (void *)aDataTransfered, sizeof( aDataTransfered ));
00143 print_dbg("\n");
00144 }
00145 }
00146
00150 void init_usart(void)
00151 {
00152 init_dbg_rs232(FCPU_HZ);
00153 print_dbg("\x0CPEVC Driver - EXAMPLE 1\r\n");
00154 print_dbg("USART transfert using PEVC, AST and PDCA\r\n");
00155 }
00156
00160 void init_pevc(void)
00161 {
00162
00163 if(FAIL == pevc_channel_configure(ppevc,
00164 PEVC_PDCA_SOT_USER,
00165 AVR32_PEVC_ID_GEN_AST_PER0,
00166 NULL))
00167 {
00168 print_dbg("PEVC channel config failed!!!\r\n");
00169 gpio_clr_gpio_pin(LED2_GPIO);
00170 while(1);
00171 }
00172
00173
00174 pevc_channels_enable(ppevc, 1<<PEVC_PDCA_SOT_USER);
00175
00176 }
00177
00181 void init_pdca(void)
00182 {
00183
00184 static const pdca_channel_options_t PDCA_CH_OPTIONS =
00185 {
00186 .addr = (void *)aDataTransfered,
00187 .pid = AVR32_PDCA_PID_USART2_TX,
00188 .size = 0,
00189 .r_addr = (void *)aDataTransfered,
00190 .r_size = sizeof(aDataTransfered),
00191 .transfer_size = PDCA_TRANSFER_SIZE_BYTE,
00192 .etrig = ENABLED
00193 };
00194
00195 Disable_global_interrupt();
00196
00197
00198 INTC_register_interrupt(&pdca_int_handler, PDCA_CHANNEL_IRQ, AVR32_INTC_INT0);
00199
00200 Enable_global_interrupt();
00201
00202
00203 pdca_init_channel(PDCA_CHANNEL_USART, &PDCA_CH_OPTIONS);
00204 pdca_channel = pdca_get_handler(PDCA_CHANNEL_USART);
00205
00206
00207 pdca_enable_interrupt_transfer_error(PDCA_CHANNEL_USART);
00208 pdca_enable_interrupt_transfer_complete(PDCA_CHANNEL_USART);
00209
00210
00211 PEVC_CHANNELS_ENABLE(ppevc, 1<<PEVC_PDCA_SOT_USER);
00212
00213
00214 pdca_enable(PDCA_CHANNEL_USART);
00215 }
00216
00220 void init_ast(void)
00221 {
00222
00223 avr32_ast_pir0_t pir = {
00224 .insel = 14
00225 };
00226
00227 ast_calendar_t ast_calendar;
00228 ast_calendar.FIELD.sec = 30;
00229 ast_calendar.FIELD.min = 45;
00230 ast_calendar.FIELD.hour = 12;
00231 ast_calendar.FIELD.day = 7;
00232 ast_calendar.FIELD.month= 10;
00233 ast_calendar.FIELD.year = 9;
00234
00235
00236
00237 if (!ast_init_calendar(&AVR32_AST, AST_OSC_32KHZ, AST_PSEL_32KHZ_1HZ, ast_calendar))
00238 {
00239 print_dbg("Error initializing the AST\r\n");
00240 while(1);
00241 }
00242
00243 ast_set_periodic0_value(&AVR32_AST,pir);
00244
00245 ast_enable_periodic0(&AVR32_AST);
00246
00247
00248 AVR32_AST.scr=0xFFFFFFFF;
00249
00250
00251 ast_enable(&AVR32_AST);
00252 }
00253
00256 static void init_sys_clocks(void)
00257 {
00258
00261
00262 static pcl_freq_param_t pcl_freq_param =
00263 {
00264 .cpu_f = FCPU_HZ,
00265 .pba_f = FPBA_HZ,
00266 .osc0_f = FOSC0,
00267 .osc0_startup = OSC0_STARTUP
00268 };
00270
00271
00272 if (pcl_configure_clocks(&pcl_freq_param) != PASS) {
00273 while(1);
00274 }
00275 }
00276
00280 int main(void)
00281 {
00282 int i;
00283
00284
00285 for(i=0;i<sizeof(aDataTransfered);i++)
00286 aDataTransfered[i] = '0' + (i%36);
00287
00288 init_sys_clocks();
00289
00290 delay_init(FCPU_HZ);
00291
00292 init_usart();
00293
00294 gpio_clr_gpio_pin(LED0_GPIO);
00295
00296 init_pevc();
00297
00298 init_ast();
00299
00300 init_pdca();
00301
00302 while(1)
00303 {
00304 gpio_tgl_gpio_pin(LED1_GPIO);
00305 delay_ms(500);
00306 }
00307 }