can_example1.c File Reference


Detailed Description

CAN Driver Example 1.

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

Definition in file can_example1.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_to_send (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)

Variables

volatile can_msg_t mob_ram_ch0 [NB_MOB_CHANNEL]
 Local allocation for MOB buffer.
volatile U8 nb_message_received_on_channel0 = 0


Function Documentation

int _init_startup ( void   ) 

Low-level initialization routine called during startup, before the main function.

Definition at line 213 of file can_example1.c.

References init_exceptions(), init_interrupts(), init_stdio(), and init_sys_clocks().

00217 {
00218   init_exceptions();
00219   init_sys_clocks();
00220   init_stdio();
00221   init_interrupts();
00222 
00223   // EWAVR32: Request initialization of data segments.
00224   // GCC: Don't-care value.
00225   return 1;
00226 }

void can_example_prepare_data_to_send ( 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.

Definition at line 111 of file can_example1.c.

References appli_rx_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, nb_message_received_on_channel0, and can_mob_t::status.

Referenced by main().

00112 {
00113    gpio_tgl_gpio_pin(LED0_GPIO);
00114 
00115    // Reception Only
00116    appli_rx_msg.can_msg->data.u64 = can_get_mob_data(0,handle).u64;
00117    appli_rx_msg.can_msg->id = can_get_mob_id(0,handle);
00118    appli_rx_msg.dlc = can_get_mob_dlc(0,handle);
00119    appli_rx_msg.status = event;
00120    can_mob_free(0,handle);       
00121    nb_message_received_on_channel0 = 1;       
00122 
00123 }

static void init_exceptions ( void   )  [static]

Initializes MCU exceptions.

Definition at line 127 of file can_example1.c.

Referenced by _init_startup().

00128 {
00129 #if defined (__GNUC__) && defined (__AVR32__)
00130   // Import the Exception Vector Base Address.
00131   extern void _evba;
00132 
00133   // Load the Exception Vector Base Address in the corresponding system
00134   // register.
00135   Set_system_register(AVR32_EVBA, (int)&_evba);
00136 #endif
00137 
00138   // Enable exceptions globally.
00139   Enable_global_exception();
00140 }

static void init_interrupts ( void   )  [static]

Initializes MCU interrupts.

Definition at line 199 of file can_example1.c.

Referenced by _init_startup().

00200 {
00201   // Initialize interrupt handling.
00202   INTC_init_interrupts();
00203 
00204   // Enable interrupts globally.
00205   Enable_global_interrupt();
00206 }

static void init_stdio ( void   )  [static]

Initializes STDIO.

Definition at line 167 of file can_example1.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().

00168 {
00169 
00170   static const gpio_map_t STDIO_USART_GPIO_MAP =
00171   {
00172     {STDIO_USART_RX_PIN, STDIO_USART_RX_FUNCTION},
00173     {STDIO_USART_TX_PIN, STDIO_USART_TX_FUNCTION}
00174   };
00175 
00176   static const usart_options_t STDIO_USART_OPTIONS =
00177   {
00178     .baudrate     = STDIO_USART_BAUDRATE,
00179     .charlength   = 8,
00180     .paritytype   = USART_NO_PARITY,
00181     .stopbits     = USART_1_STOPBIT,
00182     .channelmode  = USART_NORMAL_CHMODE
00183   };
00184 
00185   gpio_enable_module(STDIO_USART_GPIO_MAP,
00186                      sizeof(STDIO_USART_GPIO_MAP) / sizeof(STDIO_USART_GPIO_MAP[0]));
00187   
00188   usart_init_rs232(STDIO_USART, &STDIO_USART_OPTIONS, FPBA_HZ);
00189   
00190 #if defined (__GNUC__) && defined (__AVR32__)
00191   // Initialize the USART used for STDIO.
00192   set_usart_base((void *)STDIO_USART);
00193 #endif
00194 }

static void init_sys_clocks ( void   )  [static]

Initializes the MCU system clocks.

Definition at line 144 of file can_example1.c.

References FPBA_HZ.

Referenced by _init_startup().

00145 {
00146   // Switch the main clock to OSC0 
00147     scif_configure_osc_crystalmode(SCIF_OSC0, FOSC0);
00148     scif_enable_osc(SCIF_OSC0, OSC0_STARTUP, true);
00149     pm_set_mclk_source(PM_CLK_SRC_OSC0);
00150 
00151   // Setup the generic clock for CAN
00152   scif_gc_setup(AVR32_SCIF_GCLK_CANIF, 
00153                 SCIF_GCCTRL_OSC0, 
00154                 AVR32_SCIF_GC_NO_DIV_CLOCK, 
00155                 0);  
00156   // Now enable the generic clock
00157   scif_gc_enable(AVR32_SCIF_GCLK_CANIF);
00158   
00159 #if defined (__GNUC__) && defined (__AVR32__)
00160   // Give the used PBA clock frequency to Newlib (only for GCC), so it can work properly.
00161   set_cpu_hz(FPBA_HZ);
00162 #endif
00163 }

int main ( void   ) 

Definition at line 229 of file can_example1.c.

References appli_rx_msg, can_init(), can_mob_alloc(), can_mob_t::can_msg, can_out_callback_channel0(), can_rx(), CAN_Wakeup, CAN_WakeupD, CANIF_channel_enable_status, CANIF_CHANNEL_MODE_LISTENING, CANIF_CHANNEL_MODE_NORMAL, CANIF_clr_interrupt_status, CANIF_clr_reset, CANIF_disable_wakeup, CANIF_enable_wakeup, can_mob_t::handle, and can_mob_t::req_type.

00230 {
00231   // Configure standard I/O streams as unbuffered.
00232 #if defined (__GNUC__) && defined (__AVR32__)
00233   setbuf(stdin, NULL);
00234 #endif
00235   setbuf(stdout, NULL);
00236   
00237   // Disable all interrupts.
00238   Disable_global_interrupt();
00239 
00240   // Initialize interrupt vectors.
00241   INTC_init_interrupts();
00242 
00243   static const gpio_map_t CAN_GPIO_MAP =
00244   {
00245     {AVR32_CANIF_RXLINE_0_0_PIN, AVR32_CANIF_RXLINE_0_0_FUNCTION},
00246     {AVR32_CANIF_TXLINE_0_0_PIN, AVR32_CANIF_TXLINE_0_0_FUNCTION}
00247   };
00248   // Assign GPIO to CAN.
00249   gpio_enable_module(CAN_GPIO_MAP,
00250                      sizeof(CAN_GPIO_MAP) / sizeof(CAN_GPIO_MAP[0]));
00251                        
00252   // Initialize channel 0
00253   can_init(0,
00254            ((U32)&mob_ram_ch0[0]),
00255            CANIF_CHANNEL_MODE_LISTENING,
00256            can_out_callback_channel0);  
00257 
00258   // Enable all interrupts.
00259   Enable_global_interrupt();
00260 
00261   printf("\nUC3C CAN Examples 1\n");
00262 
00263   printf(CAN_Wakeup);
00264   
00265   // Initialize CAN Channel 0
00266   can_init(0,
00267            ((U32)&mob_ram_ch0[0]),
00268            CANIF_CHANNEL_MODE_NORMAL,
00269            can_out_callback_channel0);
00270            
00271   // Allocate one mob for RX 
00272   appli_rx_msg.handle = can_mob_alloc(0);
00273   
00274   // Initialize RX message
00275   can_rx(0,
00276          appli_rx_msg.handle,
00277          appli_rx_msg.req_type,
00278          appli_rx_msg.can_msg);
00279 
00280   //---------SLEEP MODE PROCEDURE-------------
00281   // Disable CAN Channel 0
00282   CANIF_clr_reset(0);
00283   // Wait CAN Channel 0 is disabled
00284   while(!CANIF_channel_enable_status(0));
00285   // Enable Wake-Up Mode
00286   CANIF_enable_wakeup(0);
00287   // Go to sleep mode.
00288   SLEEP(AVR32_PM_SMODE_STATIC);
00289   // Clear Interrupt Flag
00290   CANIF_clr_interrupt_status(0);
00291   // Disable Wake-Up Mode
00292   CANIF_disable_wakeup(0);
00293   //---------SLEEP MODE PROCEDURE-------------
00294   printf(CAN_WakeupD);    
00295   // Initialize again CAN Channel 0
00296   can_init(0,
00297            ((U32)&mob_ram_ch0[0]),
00298            CANIF_CHANNEL_MODE_NORMAL,
00299            can_out_callback_channel0);
00300            
00301   // Allocate one mob for RX 
00302   appli_rx_msg.handle = can_mob_alloc(0);
00303 
00304   // Initialize RX message  
00305   can_rx(0,
00306          appli_rx_msg.handle,
00307          appli_rx_msg.req_type,
00308          appli_rx_msg.can_msg);
00309         
00310   while(1);
00311 
00312 }


Variable Documentation

volatile can_msg_t mob_ram_ch0[NB_MOB_CHANNEL]

Local allocation for MOB buffer.

Definition at line 103 of file can_example1.c.

Definition at line 109 of file can_example1.c.

Referenced by can_out_callback_channel0().


Generated on Fri Feb 19 02:24:17 2010 for AVR32 - CANIF Driver by  doxygen 1.5.5