Definition in file can_example2.c.
#include <stddef.h>
#include <stdio.h>
#include <avr32/io.h>
#include "compiler.h"
#include "board.h"
#include "power_clocks_lib.h"
#include "gpio.h"
#include "pm_uc3c.h"
#include "scif_uc3c.h"
#include "intc.h"
#include "can.h"
#include "canif.h"
#include "nlao_cpu.h"
#include "nlao_usart.h"
#include "usart.h"
#include "conf_can_example.h"
Go to the source code of this file.
Functions | |
int | _init_startup (void) |
Low-level initialization routine called during startup, before the main function. | |
void | can_example_prepare_data (U8 mode) |
Local function to prepare RX and TX buffers. | |
void | can_out_callback_channel0 (U8 handle, U8 event) |
Call Back called by can_drv. | |
static void | init_exceptions (void) |
Initializes MCU exceptions. | |
static void | init_interrupts (void) |
Initializes MCU interrupts. | |
static void | init_stdio (void) |
Initializes STDIO. | |
static void | init_sys_clocks (void) |
Initializes the MCU system clocks. | |
int | main (void) |
static void | usart_int_handler (void) |
The USART interrupt handler. | |
Variables | |
volatile can_msg_t | mob_ram_ch0 [NB_MOB_CHANNEL] |
Local allocation for MOB buffer in HSB_RAM. | |
volatile U8 | nb_message_received_on_channel0 = 0 |
Define the number of message received on CAN Channel 0. | |
volatile U16 | state_machine = 0 |
int _init_startup | ( | void | ) |
Low-level initialization routine called during startup, before the main function.
Definition at line 281 of file can_example2.c.
References init_exceptions(), init_interrupts(), init_stdio(), and init_sys_clocks().
00285 { 00286 init_exceptions(); 00287 init_sys_clocks(); 00288 init_stdio(); 00289 init_interrupts(); 00290 00291 // EWAVR32: Request initialization of data segments. 00292 // GCC: Don't-care value. 00293 return 1; 00294 }
void can_example_prepare_data | ( | U8 | mode | ) |
Local function to prepare RX and TX buffers.
Prepare Data on CAN Bus.
Definition at line 298 of file can_example2.c.
References appli_remote_rx_msg, appli_rx_msg, appli_tx_msg, CAN_CMD_REFUSED, can_init(), can_mob_alloc(), can_mob_t::can_msg, can_out_callback_channel0(), can_rx(), can_tx(), CANIF_CHANNEL_MODE_NORMAL, can_mob_t::dlc, can_mob_t::handle, and can_mob_t::req_type.
Referenced by main().
00299 { 00300 // Prepare Transmission Mode 00301 if (mode ==0) 00302 { 00303 // Initialize channel 0 00304 can_init(0, 00305 ((U32)&mob_ram_ch0[0]), 00306 CANIF_CHANNEL_MODE_NORMAL, 00307 can_out_callback_channel0); 00308 // Allocate one mob for TX 00309 appli_tx_msg.handle = can_mob_alloc(0); 00310 00311 // Check return if no mob are available 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 // Prepare Reception Mode 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 // Allocate one mob for RX 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 // Prepare Remote Reception Mode 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 // Allocate one mob for RX 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 }
void can_out_callback_channel0 | ( | U8 | handle, | |
U8 | event | |||
) |
Call Back called by can_drv.
Definition at line 164 of file can_example2.c.
References appli_remote_rx_msg, appli_rx_msg, appli_tx_msg, can_get_mob_data(), can_get_mob_dlc(), can_get_mob_id(), can_mob_free(), can_mob_t::can_msg, can_msg_t::data, can_mob_t::dlc, can_msg_t::id, and can_mob_t::status.
Referenced by can_example_prepare_data(), and main().
00165 { 00166 gpio_tgl_gpio_pin(LED0_GPIO); 00167 switch(state_machine) 00168 { 00169 // Transmission Only 00170 case 120: 00171 appli_tx_msg.status = event; 00172 can_mob_free(0,handle); 00173 state_machine = 121; 00174 break; 00175 // Reception Only 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 // Remote Reception Only 00185 case 320: 00186 appli_remote_rx_msg.status = event; 00187 can_mob_free(0,handle); 00188 state_machine = 321; 00189 break; 00190 } 00191 }
static void init_exceptions | ( | void | ) | [static] |
Initializes MCU exceptions.
Definition at line 195 of file can_example2.c.
Referenced by _init_startup().
00196 { 00197 #if defined (__GNUC__) && defined (__AVR32__) 00198 // Import the Exception Vector Base Address. 00199 extern void _evba; 00200 00201 // Load the Exception Vector Base Address in the corresponding system 00202 // register. 00203 Set_system_register(AVR32_EVBA, (int)&_evba); 00204 #endif 00205 00206 // Enable exceptions globally. 00207 Enable_global_exception(); 00208 }
static void init_interrupts | ( | void | ) | [static] |
Initializes MCU interrupts.
Definition at line 267 of file can_example2.c.
Referenced by _init_startup().
00268 { 00269 // Initialize interrupt handling. 00270 INTC_init_interrupts(); 00271 00272 // Enable interrupts globally. 00273 Enable_global_interrupt(); 00274 }
static void init_stdio | ( | void | ) | [static] |
Initializes STDIO.
Definition at line 235 of file can_example2.c.
References FPBA_HZ, STDIO_USART, STDIO_USART_BAUDRATE, STDIO_USART_RX_FUNCTION, STDIO_USART_RX_PIN, STDIO_USART_TX_FUNCTION, and STDIO_USART_TX_PIN.
Referenced by _init_startup().
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 // Initialize the USART used for STDIO. 00260 set_usart_base((void *)STDIO_USART); 00261 #endif 00262 }
static void init_sys_clocks | ( | void | ) | [static] |
Initializes the MCU system clocks.
Definition at line 212 of file can_example2.c.
References FPBA_HZ.
Referenced by _init_startup().
00213 { 00214 // Switch the main clock to OSC0 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 // Setup the generic clock for CAN 00220 scif_gc_setup(AVR32_SCIF_GCLK_CANIF, 00221 SCIF_GCCTRL_OSC0, 00222 AVR32_SCIF_GC_NO_DIV_CLOCK, 00223 0); 00224 // Now enable the generic clock 00225 scif_gc_enable(AVR32_SCIF_GCLK_CANIF); 00226 00227 #if defined (__GNUC__) && defined (__AVR32__) 00228 // Give the used PBA clock frequency to Newlib, so it can work properly. 00229 set_cpu_hz(FPBA_HZ); 00230 #endif 00231 }
int main | ( | void | ) |
Definition at line 358 of file can_example2.c.
References appli_remote_rx_msg, appli_rx_msg, appli_tx_msg, CAN_BusOFFIRQ, CAN_Error, can_example_prepare_data(), can_init(), can_mob_alloc(), can_mob_t::can_msg, can_out_callback_channel0(), can_rx(), CAN_STATUS_BUSOFF, CAN_STATUS_COMPLETED, CAN_STATUS_ERROR, CAN_Success, CANIF_CHANNEL_MODE_LISTENING, can_msg_t::data, Data_Sent_Start_or_Not, Demo_Sent_Data, can_mob_t::dlc, can_mob_t::handle, can_msg_t::id, can_mob_t::req_type, can_mob_t::status, STDIO_USART, STDIO_USART_IRQ, STDIO_USART_IRQ_LEVEL, txt_logo_atmel, and usart_int_handler().
00359 { 00360 00361 // Configure standard I/O streams as unbuffered. 00362 #if defined (__GNUC__) && defined (__AVR32__) 00363 setbuf(stdin, NULL); 00364 #endif 00365 setbuf(stdout, NULL); 00366 00367 // Disable all interrupts. 00368 Disable_global_interrupt(); 00369 00370 // Initialize interrupt vectors. 00371 INTC_init_interrupts(); 00372 00373 // Register the USART interrupt handler to the interrupt controller. 00374 // usart_int_handler is the interrupt handler to register. 00375 // EXAMPLE_USART_IRQ is the IRQ of the interrupt handler to register. 00376 // AVR32_INTC_INT0 is the interrupt priority level to assign to the group of 00377 // this IRQ. 00378 // void INTC_register_interrupt(__int_handler handler, unsigned int irq, unsigned int int_level); 00379 INTC_register_interrupt(&usart_int_handler, STDIO_USART_IRQ, STDIO_USART_IRQ_LEVEL); 00380 00381 // Enable USART Rx interrupt. 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 // Assign GPIO to CAN. 00390 gpio_enable_module(CAN_GPIO_MAP, 00391 sizeof(CAN_GPIO_MAP) / sizeof(CAN_GPIO_MAP[0])); 00392 00393 // Initialize channel 0 00394 can_init(0, 00395 ((U32)&mob_ram_ch0[0]), 00396 CANIF_CHANNEL_MODE_LISTENING, 00397 can_out_callback_channel0); 00398 00399 // Enable all interrupts. 00400 Enable_global_interrupt(); 00401 00402 while(1) 00403 { 00404 //--------------------------------------------------------------------------- 00405 //--- C O R E O F C A N - DEMO --- 00406 //--------------------------------------------------------------------------- 00407 switch( state_machine ) 00408 { 00409 case 0: 00410 state_machine = 1; 00411 printf(txt_logo_atmel); 00412 break; 00413 00414 case 1: // wait input (1,2,3 choice) 00415 break; 00416 00417 //-------------------------------------------------------------- 00418 //--- Case 1 : Transmission --- 00419 //-------------------------------------------------------------- 00420 case 10: // "1" be pressed 00421 state_machine = 11; 00422 printf( Demo_Sent_Data ); 00423 printf( Data_Sent_Start_or_Not ); 00424 break; 00425 00426 case 11: // wait input (y,n choice) 00427 break; 00428 00429 case 12: // "y or Y" be pressed, 00430 state_machine = 120; 00431 can_example_prepare_data(0); 00432 break; 00433 00434 case 120: //Idle state 00435 break; 00436 00437 case 121: // Print Status 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: // wait input 00454 break; 00455 00456 //-------------------------------------------------------------- 00457 //--- Case 2 : Reception --- 00458 //-------------------------------------------------------------- 00459 case 20: // "2" be pressed 00460 state_machine = 21; 00461 printf( Data_Sent_Start_or_Not ); 00462 break; 00463 00464 case 21: // wait input (y,n choice) 00465 break; 00466 00467 case 22: // "y or Y" be pressed, 00468 state_machine = 220; 00469 can_example_prepare_data(1); 00470 break; 00471 00472 case 220: //Idle state 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 // Allocate one mob for RX 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: // wait input 00513 break; 00514 00515 //-------------------------------------------------------------- 00516 //--- Case 3 : Remote Reception --- 00517 //-------------------------------------------------------------- 00518 00519 case 30: // "3" be pressed 00520 state_machine = 31; 00521 printf( Data_Sent_Start_or_Not ); 00522 break; 00523 00524 case 31: // wait input (y,n choice) 00525 break; 00526 00527 case 32: // "y or Y" be pressed, 00528 state_machine = 320; 00529 can_example_prepare_data(2); 00530 break; 00531 00532 case 320: //Idle state 00533 break; 00534 00535 case 321: // Print Status 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: // wait input 00552 break; 00553 00554 //-------------------------------------------------------------- 00555 //--- Default --- 00556 //-------------------------------------------------------------- 00557 default: 00558 break; 00559 } 00560 } 00561 }
static void usart_int_handler | ( | void | ) | [static] |
The USART interrupt handler.
Definition at line 124 of file can_example2.c.
References STDIO_USART.
Referenced by main().
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 }
volatile can_msg_t mob_ram_ch0[NB_MOB_CHANNEL] |
volatile U8 nb_message_received_on_channel0 = 0 |
Define the number of message received on CAN Channel 0.
Definition at line 162 of file can_example2.c.
volatile U16 state_machine = 0 |
Definition at line 116 of file can_example2.c.