00001
00086 #include <stddef.h>
00087 #include <stdio.h>
00088 #include <avr32/io.h>
00089 #include "compiler.h"
00090 #include "board.h"
00091 #include "power_clocks_lib.h"
00092 #include "gpio.h"
00093 #include "pm_uc3c.h"
00094 #include "scif_uc3c.h"
00095 #include "intc.h"
00096 #include "can.h"
00097 #include "canif.h"
00098 #if defined (__GNUC__) && defined (__AVR32__)
00099 #include "nlao_cpu.h"
00100 #include "nlao_usart.h"
00101 #endif
00102 #include "usart.h"
00103
00104 #include "conf_can_example.h"
00105
00107 #if defined (__GNUC__) && defined (__AVR32__)
00108 volatile can_msg_t mob_ram_ch0[NB_MOB_CHANNEL] __attribute__ ((section (".hsb_ram_loc")));
00109 #elif defined (__ICCAVR32__)
00110 volatile __no_init can_msg_t mob_ram_ch0[NB_MOB_CHANNEL] @0xA0000000;
00111 #endif
00112
00114 void can_example_prepare_data(U8 mode);
00115
00116 volatile U16 state_machine = 0;
00117
00119 #if defined (__GNUC__)
00120 __attribute__((__interrupt__))
00121 #elif defined (__ICCAVR32__)
00122 __interrupt
00123 #endif
00124 static void usart_int_handler(void)
00125 {
00126 int c;
00127 usart_read_char(STDIO_USART, &c);
00128 switch( c )
00129 {
00130 case '1':
00131 if( state_machine == 1) state_machine = 10;
00132 break;
00133 case '2':
00134 if( state_machine == 1) state_machine = 20;
00135 break;
00136 case '3':
00137 if( state_machine == 1) state_machine = 30;
00138 break;
00139 case 'Y':
00140 case 'y':
00141 if( state_machine == 11) state_machine = 12;
00142 if( state_machine == 21) state_machine = 22;
00143 if( state_machine == 31) state_machine = 32;
00144 break;
00145 case 'N':
00146 case 'n':
00147 if( state_machine == 11 ) state_machine = 0;
00148 if( state_machine == 21) state_machine = 0;
00149 if( state_machine == 31) state_machine = 0;
00150 break;
00151 case 'Q':
00152 case 'q':
00153 if( state_machine == 121) state_machine = 0;
00154 if( state_machine == 131) state_machine = 0;
00155 if( state_machine == 221) state_machine = 0;
00156 if( state_machine == 220 ) state_machine = 0;
00157 if( state_machine == 331) state_machine = 0;
00158 break;
00159 }
00160 }
00162 volatile U8 nb_message_received_on_channel0 = 0;
00164 void can_out_callback_channel0(U8 handle, U8 event)
00165 {
00166 gpio_tgl_gpio_pin(LED0_GPIO);
00167 switch(state_machine)
00168 {
00169
00170 case 120:
00171 appli_tx_msg.status = event;
00172 can_mob_free(0,handle);
00173 state_machine = 121;
00174 break;
00175
00176 case 220:
00177 appli_rx_msg.can_msg->data.u64 = can_get_mob_data(0,handle).u64;
00178 appli_rx_msg.can_msg->id = can_get_mob_id(0,handle);
00179 appli_rx_msg.dlc = can_get_mob_dlc(0,handle);
00180 appli_rx_msg.status = event;
00181 can_mob_free(0,handle);
00182 nb_message_received_on_channel0 = 1;
00183 break;
00184
00185 case 320:
00186 appli_remote_rx_msg.status = event;
00187 can_mob_free(0,handle);
00188 state_machine = 321;
00189 break;
00190 }
00191 }
00192
00195 static void init_exceptions(void)
00196 {
00197 #if defined (__GNUC__) && defined (__AVR32__)
00198
00199 extern void _evba;
00200
00201
00202
00203 Set_system_register(AVR32_EVBA, (int)&_evba);
00204 #endif
00205
00206
00207 Enable_global_exception();
00208 }
00209
00212 static void init_sys_clocks(void)
00213 {
00214
00215 scif_configure_osc_crystalmode(SCIF_OSC0, FOSC0);
00216 scif_enable_osc(SCIF_OSC0, OSC0_STARTUP, true);
00217 pm_set_mclk_source(PM_CLK_SRC_OSC0);
00218
00219
00220 scif_gc_setup(AVR32_SCIF_GCLK_CANIF,
00221 SCIF_GCCTRL_OSC0,
00222 AVR32_SCIF_GC_NO_DIV_CLOCK,
00223 0);
00224
00225 scif_gc_enable(AVR32_SCIF_GCLK_CANIF);
00226
00227 #if defined (__GNUC__) && defined (__AVR32__)
00228
00229 set_cpu_hz(FPBA_HZ);
00230 #endif
00231 }
00232
00235 static void init_stdio(void)
00236 {
00237
00238 static const gpio_map_t STDIO_USART_GPIO_MAP =
00239 {
00240 {STDIO_USART_RX_PIN, STDIO_USART_RX_FUNCTION},
00241 {STDIO_USART_TX_PIN, STDIO_USART_TX_FUNCTION}
00242 };
00243
00244 static const usart_options_t STDIO_USART_OPTIONS =
00245 {
00246 .baudrate = STDIO_USART_BAUDRATE,
00247 .charlength = 8,
00248 .paritytype = USART_NO_PARITY,
00249 .stopbits = USART_1_STOPBIT,
00250 .channelmode = USART_NORMAL_CHMODE
00251 };
00252
00253 gpio_enable_module(STDIO_USART_GPIO_MAP,
00254 sizeof(STDIO_USART_GPIO_MAP) / sizeof(STDIO_USART_GPIO_MAP[0]));
00255
00256 usart_init_rs232(STDIO_USART, &STDIO_USART_OPTIONS, FPBA_HZ);
00257
00258 #if defined (__GNUC__) && defined (__AVR32__)
00259
00260 set_usart_base((void *)STDIO_USART);
00261 #endif
00262 }
00263
00264
00267 static void init_interrupts(void)
00268 {
00269
00270 INTC_init_interrupts();
00271
00272
00273 Enable_global_interrupt();
00274 }
00275
00276
00280 #if defined (__GNUC__) && defined (__AVR32__)
00281 int _init_startup(void)
00282 #elif defined (__ICCAVR32__)
00283 int __low_level_init(void)
00284 #endif
00285 {
00286 init_exceptions();
00287 init_sys_clocks();
00288 init_stdio();
00289 init_interrupts();
00290
00291
00292
00293 return 1;
00294 }
00295
00298 void can_example_prepare_data(U8 mode)
00299 {
00300
00301 if (mode ==0)
00302 {
00303
00304 can_init(0,
00305 ((U32)&mob_ram_ch0[0]),
00306 CANIF_CHANNEL_MODE_NORMAL,
00307 can_out_callback_channel0);
00308
00309 appli_tx_msg.handle = can_mob_alloc(0);
00310
00311
00312 if (appli_tx_msg.handle==CAN_CMD_REFUSED)
00313 {
00314 while(1);
00315 }
00316
00317 can_tx(0,
00318 appli_tx_msg.handle,
00319 appli_tx_msg.dlc,
00320 appli_tx_msg.req_type,
00321 appli_tx_msg.can_msg);
00322 }
00323
00324 else if (mode == 1)
00325 {
00326 can_init(0,
00327 ((U32)&mob_ram_ch0[0]),
00328 CANIF_CHANNEL_MODE_NORMAL,
00329 can_out_callback_channel0);
00330
00331
00332 appli_rx_msg.handle = can_mob_alloc(0);
00333
00334 can_rx(0,
00335 appli_rx_msg.handle,
00336 appli_rx_msg.req_type,
00337 appli_rx_msg.can_msg);
00338 }
00339
00340 else if (mode == 2)
00341 {
00342 can_init(0,
00343 ((U32)&mob_ram_ch0[0]),
00344 CANIF_CHANNEL_MODE_NORMAL,
00345 can_out_callback_channel0);
00346
00347
00348 appli_remote_rx_msg.handle = can_mob_alloc(0);
00349
00350 can_rx(0,
00351 appli_remote_rx_msg.handle,
00352 appli_remote_rx_msg.req_type,
00353 appli_remote_rx_msg.can_msg);
00354 }
00355
00356 }
00357
00358 int main(void)
00359 {
00360
00361
00362 #if defined (__GNUC__) && defined (__AVR32__)
00363 setbuf(stdin, NULL);
00364 #endif
00365 setbuf(stdout, NULL);
00366
00367
00368 Disable_global_interrupt();
00369
00370
00371 INTC_init_interrupts();
00372
00373
00374
00375
00376
00377
00378
00379 INTC_register_interrupt(&usart_int_handler, STDIO_USART_IRQ, STDIO_USART_IRQ_LEVEL);
00380
00381
00382 STDIO_USART->ier = AVR32_USART_IER_RXRDY_MASK;
00383
00384 static const gpio_map_t CAN_GPIO_MAP =
00385 {
00386 {AVR32_CANIF_RXLINE_0_0_PIN, AVR32_CANIF_RXLINE_0_0_FUNCTION},
00387 {AVR32_CANIF_TXLINE_0_0_PIN, AVR32_CANIF_TXLINE_0_0_FUNCTION}
00388 };
00389
00390 gpio_enable_module(CAN_GPIO_MAP,
00391 sizeof(CAN_GPIO_MAP) / sizeof(CAN_GPIO_MAP[0]));
00392
00393
00394 can_init(0,
00395 ((U32)&mob_ram_ch0[0]),
00396 CANIF_CHANNEL_MODE_LISTENING,
00397 can_out_callback_channel0);
00398
00399
00400 Enable_global_interrupt();
00401
00402 while(1)
00403 {
00404
00405
00406
00407 switch( state_machine )
00408 {
00409 case 0:
00410 state_machine = 1;
00411 printf(txt_logo_atmel);
00412 break;
00413
00414 case 1:
00415 break;
00416
00417
00418
00419
00420 case 10:
00421 state_machine = 11;
00422 printf( Demo_Sent_Data );
00423 printf( Data_Sent_Start_or_Not );
00424 break;
00425
00426 case 11:
00427 break;
00428
00429 case 12:
00430 state_machine = 120;
00431 can_example_prepare_data(0);
00432 break;
00433
00434 case 120:
00435 break;
00436
00437 case 121:
00438 if (appli_tx_msg.status == CAN_STATUS_COMPLETED)
00439 {
00440 printf(CAN_Success);
00441 }
00442 else if (appli_tx_msg.status == CAN_STATUS_ERROR)
00443 {
00444 printf(CAN_Error);
00445 }
00446 else if(appli_tx_msg.status == CAN_STATUS_BUSOFF)
00447 {
00448 printf(CAN_BusOFFIRQ);
00449 }
00450 state_machine = 131;
00451 break;
00452
00453 case 131:
00454 break;
00455
00456
00457
00458
00459 case 20:
00460 state_machine = 21;
00461 printf( Data_Sent_Start_or_Not );
00462 break;
00463
00464 case 21:
00465 break;
00466
00467 case 22:
00468 state_machine = 220;
00469 can_example_prepare_data(1);
00470 break;
00471
00472 case 220:
00473 if ((nb_message_received_on_channel0 == 1))
00474 {
00475 nb_message_received_on_channel0 = 0;
00476 if (appli_rx_msg.status == CAN_STATUS_COMPLETED)
00477 {
00478 printf ("-1- RxCAN @ %lu: 0x%lx, L=%d, ",
00479 appli_rx_msg.can_msg->id,
00480 appli_rx_msg.can_msg->id,
00481 appli_rx_msg.dlc);
00482 int i;
00483 for (i=0;i<appli_rx_msg.dlc;i++)
00484 {
00485 printf ("%x",appli_rx_msg.can_msg->data.u8[i]);
00486 if (i<appli_rx_msg.dlc-1) printf ("-");
00487 }
00488 printf("\r\n");
00489
00490
00491 appli_rx_msg.handle = can_mob_alloc(0);
00492
00493 can_rx(0,
00494 appli_rx_msg.handle,
00495 appli_rx_msg.req_type,
00496 appli_rx_msg.can_msg);
00497
00498 }
00499 else if (appli_rx_msg.status == CAN_STATUS_ERROR)
00500 {
00501 state_machine = 221;
00502 printf(CAN_Error);
00503 }
00504 else if(appli_rx_msg.status == CAN_STATUS_BUSOFF)
00505 {
00506 state_machine = 221;
00507 printf(CAN_BusOFFIRQ);
00508 }
00509 }
00510 break;
00511
00512 case 221:
00513 break;
00514
00515
00516
00517
00518
00519 case 30:
00520 state_machine = 31;
00521 printf( Data_Sent_Start_or_Not );
00522 break;
00523
00524 case 31:
00525 break;
00526
00527 case 32:
00528 state_machine = 320;
00529 can_example_prepare_data(2);
00530 break;
00531
00532 case 320:
00533 break;
00534
00535 case 321:
00536 if (appli_remote_rx_msg.status == CAN_STATUS_COMPLETED)
00537 {
00538 printf(CAN_Success);
00539 }
00540 else if (appli_remote_rx_msg.status == CAN_STATUS_ERROR)
00541 {
00542 printf(CAN_Error);
00543 }
00544 else if(appli_remote_rx_msg.status == CAN_STATUS_BUSOFF)
00545 {
00546 printf(CAN_BusOFFIRQ);
00547 }
00548 state_machine = 331;
00549 break;
00550
00551 case 331:
00552 break;
00553
00554
00555
00556
00557 default:
00558 break;
00559 }
00560 }
00561 }