can.c File Reference


Detailed Description

CAN Service for AVR32 UC3.

This file contains basic functions for the AVR32 CAN, with support for all modes, settings and clock speeds.

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

Definition in file can.c.

#include "avr32/io.h"
#include "can.h"
#include "compiler.h"
#include "preprocessor.h"
#include "intc.h"
#include "canif.h"

Go to the source code of this file.

Functions

static void can0_int_busoff_handler (void)
static void can0_int_cerr_handler (void)
static void can0_int_rx_handler (void)
static void can0_int_tx_handler (void)
static void can0_int_wakeup_handler (void)
static void can1_int_busoff_handler (void)
static void can1_int_cerr_handler (void)
static void can1_int_rx_handler (void)
static void can1_int_tx_handler (void)
static void can1_int_wakeup_handler (void)
void can_clear_status (U8 ch, U8 mob)
U8 can_enable_interrupt (U8 ch)
Union64 can_get_mob_data (U8 ch, U8 handle)
 Get Mob data from a selected MOB.
U8 can_get_mob_dlc (U8 ch, U8 handle)
 Get Mob dlc from a selected MOB.
U32 can_get_mob_id (U8 ch, U8 handle)
 Get Mob ID from a selected MOB.
U8 can_init (U8 ch, U32 can_msg_ram_add, U8 operating_mode, void(*can_msg_callback_channel)(U8 handle, U8 event))
U8 can_mob_alloc (U8 ch)
 Allocate one MOB in a specific CAN Channel.
U8 can_mob_free (U8 ch, U8 handle)
 Free one specific MOB in a specific CAN Channel.
U8 can_mob_get_status (U8 ch, U8 handle)
 Get Mob status from a selected MOB.
U8 can_rx (U8 ch, U8 handle, U8 req_type, const can_msg_t *can_msg)
 Start Reception.
U8 can_tx (U8 ch, U8 handle, U8 dlc, U8 req_type, const can_msg_t *can_msg)
 Start Transmission.

Variables

struct {
   void(*   can_msg_callback_channel0 )(U8, U8)
   void(*   can_msg_callback_channel1 )(U8, U8)
can_lib_params
U32 can_mob_alloc_vector0 = 0
 Dynamic Mob Allocation State Vector for Channel 0.
U32 can_mob_alloc_vector1 = 0
 Dynamic Mob Allocation State Vector for Channel 1.


Function Documentation

static void can0_int_busoff_handler ( void   )  [static]

Definition at line 115 of file can.c.

References can_lib_params, CAN_STATUS_BUSOFF, and CANIF_clr_interrupt_status.

Referenced by can_enable_interrupt().

00116 {
00117     CANIF_clr_interrupt_status(0);
00118     can_lib_params.can_msg_callback_channel0(0xFF,CAN_STATUS_BUSOFF);   
00119 }

static void can0_int_cerr_handler ( void   )  [static]

Definition at line 126 of file can.c.

References can_lib_params, CAN_STATUS_ERROR, and CANIF_clr_interrupt_status.

Referenced by can_enable_interrupt().

00127 {
00128   CANIF_clr_interrupt_status(0);
00129   can_lib_params.can_msg_callback_channel0(0xFF,CAN_STATUS_ERROR);  
00130 }

static void can0_int_rx_handler ( void   )  [static]

Definition at line 98 of file can.c.

References can_lib_params, CAN_STATUS_COMPLETED, CANIF_mob_clear_rxok_status, CANIF_mob_clear_status, and CANIF_mob_get_mob_rxok.

Referenced by can_enable_interrupt().

00099 {
00100   U8 handle;
00101   handle = CANIF_mob_get_mob_rxok(0) ;
00102   if (handle != 0x20)
00103   {
00104     CANIF_mob_clear_rxok_status(0,handle);
00105     CANIF_mob_clear_status(0,handle); //   and reset MOb status
00106   }
00107   can_lib_params.can_msg_callback_channel0(handle,CAN_STATUS_COMPLETED);
00108 }

static void can0_int_tx_handler ( void   )  [static]

Definition at line 81 of file can.c.

References can_lib_params, CAN_STATUS_COMPLETED, CANIF_mob_clear_status, CANIF_mob_clear_txok_status, and CANIF_mob_get_mob_txok.

Referenced by can_enable_interrupt().

00082 {
00083   U8 handle;
00084   handle = CANIF_mob_get_mob_txok(0);
00085   if (handle != 0x20)
00086   {
00087     CANIF_mob_clear_txok_status(0,handle);
00088     CANIF_mob_clear_status(0,handle); //   and reset MOb status
00089   }
00090   can_lib_params.can_msg_callback_channel0(handle,CAN_STATUS_COMPLETED);
00091 }

static void can0_int_wakeup_handler ( void   )  [static]

Definition at line 137 of file can.c.

References can_lib_params, CAN_STATUS_WAKEUP, and CANIF_clr_interrupt_status.

Referenced by can_enable_interrupt().

00138 {
00139   CANIF_clr_interrupt_status(0);
00140   can_lib_params.can_msg_callback_channel0(0xFF,CAN_STATUS_WAKEUP); 
00141 }

static void can1_int_busoff_handler ( void   )  [static]

Definition at line 182 of file can.c.

References can_lib_params, CAN_STATUS_BUSOFF, and CANIF_clr_interrupt_status.

Referenced by can_enable_interrupt().

00183 {
00184     CANIF_clr_interrupt_status(1);
00185     can_lib_params.can_msg_callback_channel1(0xFF,CAN_STATUS_BUSOFF);   
00186 }

static void can1_int_cerr_handler ( void   )  [static]

Definition at line 193 of file can.c.

References can_lib_params, CAN_STATUS_ERROR, and CANIF_clr_interrupt_status.

Referenced by can_enable_interrupt().

00194 {
00195   CANIF_clr_interrupt_status(1);
00196   can_lib_params.can_msg_callback_channel1(0xFF,CAN_STATUS_ERROR);  
00197 }

static void can1_int_rx_handler ( void   )  [static]

Definition at line 165 of file can.c.

References can_lib_params, CAN_STATUS_COMPLETED, CANIF_mob_clear_rxok_status, CANIF_mob_clear_status, and CANIF_mob_get_mob_rxok.

Referenced by can_enable_interrupt().

00166 {
00167   U8 handle;
00168   handle = CANIF_mob_get_mob_rxok(1) ;
00169   if (handle != 0x20)
00170   {
00171     CANIF_mob_clear_rxok_status(1,handle);
00172     CANIF_mob_clear_status(1,handle); //   and reset MOb status
00173   }
00174   can_lib_params.can_msg_callback_channel1(handle,CAN_STATUS_COMPLETED);
00175 }

static void can1_int_tx_handler ( void   )  [static]

Definition at line 148 of file can.c.

References can_lib_params, CAN_STATUS_COMPLETED, CANIF_mob_clear_status, CANIF_mob_clear_txok_status, and CANIF_mob_get_mob_txok.

Referenced by can_enable_interrupt().

00149 {
00150   U8 handle;
00151   handle = CANIF_mob_get_mob_txok(1) ;
00152   if (handle != 0x20)
00153   { 
00154     CANIF_mob_clear_txok_status(1,handle);
00155     CANIF_mob_clear_status(1,handle); //   and reset MOb status
00156   }
00157   can_lib_params.can_msg_callback_channel1(handle,CAN_STATUS_COMPLETED);
00158 }

static void can1_int_wakeup_handler ( void   )  [static]

Definition at line 204 of file can.c.

References can_lib_params, CAN_STATUS_WAKEUP, and CANIF_clr_interrupt_status.

Referenced by can_enable_interrupt().

00205 {
00206   CANIF_clr_interrupt_status(1);
00207   can_lib_params.can_msg_callback_channel1(0xFF,CAN_STATUS_WAKEUP); 
00208 }

void can_clear_status ( U8  ch,
U8  mob 
)

Definition at line 459 of file can.c.

References CANIF_mob_clear_status.

00461 {
00462   CANIF_mob_clear_status(ch,mob);
00463 }

U8 can_enable_interrupt ( U8  ch  ) 

Definition at line 210 of file can.c.

References CAN0_INT_BOFF_LEVEL, can0_int_busoff_handler(), can0_int_cerr_handler(), CAN0_INT_ERR_LEVEL, can0_int_rx_handler(), CAN0_INT_RX_LEVEL, can0_int_tx_handler(), CAN0_INT_TX_LEVEL, CAN0_INT_WAKE_UP_LEVEL, can0_int_wakeup_handler(), CAN1_INT_BOFF_LEVEL, can1_int_busoff_handler(), can1_int_cerr_handler(), CAN1_INT_ERR_LEVEL, can1_int_rx_handler(), CAN1_INT_RX_LEVEL, can1_int_tx_handler(), CAN1_INT_TX_LEVEL, CAN1_INT_WAKE_UP_LEVEL, can1_int_wakeup_handler(), CAN_CMD_ACCEPTED, CAN_CMD_REFUSED, and CANIF_enable_interrupt.

Referenced by can_init().

00211 {
00212   if ((ch > 1))
00213         return  CAN_CMD_REFUSED;
00214         
00215   if (ch==0)
00216   {
00217     INTC_register_interrupt(&can0_int_tx_handler, AVR32_CANIF_TXOK_IRQ_0, CAN0_INT_TX_LEVEL);
00218     INTC_register_interrupt(&can0_int_rx_handler, AVR32_CANIF_RXOK_IRQ_0, CAN0_INT_RX_LEVEL);
00219     INTC_register_interrupt(&can0_int_busoff_handler, AVR32_CANIF_BUS_OFF_IRQ_0, CAN0_INT_BOFF_LEVEL);
00220     INTC_register_interrupt(&can0_int_cerr_handler, AVR32_CANIF_ERROR_IRQ_0, CAN0_INT_ERR_LEVEL);
00221     INTC_register_interrupt(&can0_int_wakeup_handler, AVR32_CANIF_WAKE_UP_IRQ_0, CAN0_INT_WAKE_UP_LEVEL);
00222     CANIF_enable_interrupt(ch);
00223   }
00224   else if (ch == 1)
00225   {
00226     INTC_register_interrupt(&can1_int_tx_handler, AVR32_CANIF_TXOK_IRQ_1, CAN1_INT_TX_LEVEL);
00227     INTC_register_interrupt(&can1_int_rx_handler, AVR32_CANIF_RXOK_IRQ_1, CAN1_INT_RX_LEVEL);
00228     INTC_register_interrupt(&can1_int_busoff_handler, AVR32_CANIF_BUS_OFF_IRQ_1, CAN1_INT_BOFF_LEVEL);
00229     INTC_register_interrupt(&can1_int_cerr_handler, AVR32_CANIF_ERROR_IRQ_1, CAN1_INT_ERR_LEVEL);
00230     INTC_register_interrupt(&can1_int_wakeup_handler, AVR32_CANIF_WAKE_UP_IRQ_1, CAN1_INT_WAKE_UP_LEVEL);
00231     CANIF_enable_interrupt(ch);  
00232   }
00233   
00234   return  CAN_CMD_ACCEPTED;
00235 }

Union64 can_get_mob_data ( U8  ch,
U8  handle 
)

Get Mob data from a selected MOB.

Parameters:
ch CAN channel selected 0 (CAN Channel 0) 1 (CAN Channel 1)
handle CAN MOB number
Returns:
Union64 See compiler.h file for type return definition

Definition at line 420 of file can.c.

References CANIF_mob_get_ptr_data.

Referenced by can_out_callback_channel0().

00422 {
00423     return ((CANIF_mob_get_ptr_data(ch,handle)->data));
00424 }           

U8 can_get_mob_dlc ( U8  ch,
U8  handle 
)

Get Mob dlc from a selected MOB.

Parameters:
ch CAN channel selected 0 (CAN Channel 0) 1 (CAN Channel 1)
handle CAN MOB number
Returns:
U8 Return the DLC

Definition at line 426 of file can.c.

References CANIF_mob_get_dlc.

Referenced by can_out_callback_channel0().

00428 {
00429     return (CANIF_mob_get_dlc(ch,handle));  
00430 }

U32 can_get_mob_id ( U8  ch,
U8  handle 
)

Get Mob ID from a selected MOB.

Parameters:
ch CAN channel selected 0 (CAN Channel 0) 1 (CAN Channel 1)
handle CAN MOB number
Returns:
U32 Return the ID

Definition at line 432 of file can.c.

References CANIF_get_ext_id.

Referenced by can_out_callback_channel0().

00434 {
00435     return (CANIF_get_ext_id(ch,handle));  
00436 }

U8 can_init ( U8  ch,
U32  can_msg_ram_add,
U8  operating_mode,
void(*)(U8 handle, U8 event)  can_msg_callback_channel 
)

Definition at line 237 of file can.c.

References CAN_CMD_ACCEPTED, CAN_CMD_REFUSED, can_enable_interrupt(), can_lib_params, CANIF_bit_timing, CANIF_channel_enable_status, CANIF_CHANNEL_MODE_LISTENING, CANIF_CHANNEL_MODE_LOOPBACK, CANIF_CHANNEL_MODE_NORMAL, canif_clear_all_mob(), CANIF_clr_overrun_mode, CANIF_clr_reset, CANIF_enable, CANIF_set_channel_mode, CANIF_set_overrun_mode, CANIF_set_ram_add, CANIF_set_reset, and NB_MOB_CHANNEL.

Referenced by main().

00241 {
00242    if ( ch > 1)
00243          return  CAN_CMD_REFUSED;
00244 
00245    // Initialize CAN channel
00246    CANIF_set_reset(ch);
00247    while(CANIF_channel_enable_status(ch));
00248    CANIF_clr_reset(ch);
00249    
00250    CANIF_set_ram_add(ch,(unsigned long) can_msg_ram_add);
00251    if ((CANIF_bit_timing(ch))==0) return (0);
00252    switch(operating_mode)
00253    {
00254     case CANIF_CHANNEL_MODE_NORMAL:
00255       CANIF_set_channel_mode(ch,0);
00256       CANIF_clr_overrun_mode(ch);
00257       break;
00258     case CANIF_CHANNEL_MODE_LISTENING:
00259       CANIF_set_channel_mode(ch,1);
00260       CANIF_set_overrun_mode(ch);
00261       break;
00262     case CANIF_CHANNEL_MODE_LOOPBACK:
00263       CANIF_set_channel_mode(ch,2);    
00264       CANIF_clr_overrun_mode(ch);
00265       break;      
00266    }
00267    canif_clear_all_mob(ch,NB_MOB_CHANNEL);
00268    CANIF_enable(ch);
00269    while(!CANIF_channel_enable_status(ch));
00270    
00271 #ifdef CAN_LIB_UNDER_INTERRUPT
00272    switch(ch)
00273    {
00274     case 0:
00275         can_lib_params.can_msg_callback_channel0     = can_msg_callback_channel;
00276         break;
00277     case 1:
00278         can_lib_params.can_msg_callback_channel1     = can_msg_callback_channel;
00279         break;
00280    }
00281     can_enable_interrupt(ch);
00282 #endif
00283    
00284    return CAN_CMD_ACCEPTED;
00285 }

U8 can_mob_alloc ( U8  ch  ) 

Allocate one MOB in a specific CAN Channel.

Parameters:
ch CAN channel selected 0 (CAN Channel 0) 1 (CAN Channel 1)
Returns:
U8 CAN_CMD_REFUSED or the number of the MOB selected

Definition at line 288 of file can.c.

References CAN_CMD_REFUSED, can_mob_alloc_vector0, can_mob_alloc_vector1, CANIF_clr_mob, and NB_MOB_CHANNEL.

Referenced by main().

00289 {
00290   if ((ch > 1))
00291         return  CAN_CMD_REFUSED;  
00292   
00293     if(ch==0)
00294     {
00295           int i;
00296           for (i=0;i<NB_MOB_CHANNEL;i++)
00297           {
00298             if (!((can_mob_alloc_vector0>>i)&0x01))
00299             {
00300               can_mob_alloc_vector0|=(1<<i);
00301               CANIF_clr_mob(0,i);  
00302               return i;
00303             }
00304           }
00305           return CAN_CMD_REFUSED;
00306     }
00307     else{
00308           int i;
00309           for (i=0;i<NB_MOB_CHANNEL;i++)
00310           {
00311             if (!((can_mob_alloc_vector1>>i)&0x01))
00312             {
00313               can_mob_alloc_vector1|=(1<<i);
00314               CANIF_clr_mob(1,i);  
00315               return i;
00316             }
00317           }
00318           return CAN_CMD_REFUSED;      
00319     }
00320 }

U8 can_mob_free ( U8  ch,
U8  handle 
)

Free one specific MOB in a specific CAN Channel.

Parameters:
ch CAN channel selected 0 (CAN Channel 0) 1 (CAN Channel 1)
handle CAN MOB number
Returns:
U8 CAN_CMD_REFUSED or CAN_CMD_ACCEPTED

Definition at line 322 of file can.c.

References CAN_CMD_ACCEPTED, CAN_CMD_REFUSED, can_mob_alloc_vector0, can_mob_alloc_vector1, and NB_MOB_CHANNEL.

Referenced by can_out_callback_channel0().

00323 {
00324   if ((ch > 1)||
00325       (handle > (NB_MOB_CHANNEL-1)))
00326         return  CAN_CMD_REFUSED;  
00327   switch(ch)
00328   {
00329     case 0: 
00330       can_mob_alloc_vector0 &=  (~(1<<handle));
00331       break;
00332     case 1: 
00333       can_mob_alloc_vector1 &=  (~(1<<handle));
00334       break;
00335   }
00336   return CAN_CMD_ACCEPTED;
00337 }

U8 can_mob_get_status ( U8  ch,
U8  handle 
)

Get Mob status from a selected MOB.

Parameters:
ch CAN channel selected 0 (CAN Channel 0) 1 (CAN Channel 1)
handle CAN MOB number
Returns:
U8 CAN_CMD_REFUSED or CAN_STATUS_ERROR or CAN_STATUS_COMPLETED or CAN_STATUS_NOT_COMPLETED

Definition at line 438 of file can.c.

References CAN_CMD_REFUSED, CAN_STATUS_COMPLETED, CAN_STATUS_ERROR, CAN_STATUS_NOT_COMPLETED, CANIF_get_interrupt_error_status, CANIF_mob_get_status, MOB_RX_COMPLETED, MOB_RX_COMPLETED_DLCW, MOB_TX_COMPLETED, and NB_MOB_CHANNEL.

00440 {
00441     U8 status;
00442     if ((ch > 1)||
00443         (handle > (NB_MOB_CHANNEL-1)) )
00444         return  CAN_CMD_REFUSED;
00445 
00446     status = CANIF_get_interrupt_error_status(ch);
00447     if (status!=0)
00448       return CAN_STATUS_ERROR;
00449 
00450     status = CANIF_mob_get_status(ch,handle);
00451     if ( (status & MOB_RX_COMPLETED) ||
00452          (status & MOB_TX_COMPLETED) ||
00453          (status & MOB_RX_COMPLETED_DLCW) )  
00454          return CAN_STATUS_COMPLETED;   
00455     
00456     return CAN_STATUS_NOT_COMPLETED;
00457 }                                                                              

U8 can_rx ( U8  ch,
U8  handle,
U8  req_type,
const can_msg_t can_msg 
)

Start Reception.

Parameters:
ch CAN channel selected 0 (CAN Channel 0) 1 (CAN Channel 1)
handle CAN MOB number
req_type CAN_DATA_FRAME or CAN_REMOTE_FRAME
can_msg CAN Message
Returns:
U8 CAN_CMD_REFUSED or CAN_CMD_ACCEPTED

Definition at line 382 of file can.c.

References CAN_CMD_ACCEPTED, CAN_CMD_REFUSED, CAN_REMOTE_FRAME, CANIF_config_rx, CANIF_mob_enable, CANIF_mob_enable_interrupt, CANIF_mob_set_automode, CANIF_set_data, CANIF_set_ext_id, CANIF_set_ext_idmask, CANIF_set_rtr, CANIF_set_rtrmask, CANIF_set_std_id, CANIF_set_std_idmask, can_msg_t::id, can_msg_t::id_mask, can_msg_t::ide_bit, and NB_MOB_CHANNEL.

Referenced by main().

00386 {
00387     if ((ch > 1) ||
00388         (handle > (NB_MOB_CHANNEL-1)))
00389         return  CAN_CMD_REFUSED;
00390     if (can_msg->ide_bit){
00391         CANIF_set_ext_id(ch,
00392                       handle,
00393                       can_msg->id);
00394         CANIF_set_ext_idmask(ch,
00395                           handle,
00396                           can_msg->id_mask);        
00397     }
00398     else { 
00399         CANIF_set_std_id(ch,
00400                       handle,
00401                       can_msg->id);
00402         CANIF_set_std_idmask(ch,
00403                           handle,
00404                           can_msg->id_mask);        
00405     }
00406     if (req_type == CAN_REMOTE_FRAME){
00407             CANIF_set_rtr(ch,handle);  
00408             CANIF_set_rtrmask(ch,handle);            
00409             CANIF_mob_set_automode(ch,handle);
00410             CANIF_set_data(ch,handle,((can_msg_t *)can_msg)->data.u64);
00411     }    
00412     CANIF_config_rx(ch,handle);
00413     CANIF_mob_enable(ch,handle);
00414 #ifdef CAN_LIB_UNDER_INTERRUPT          
00415     CANIF_mob_enable_interrupt(ch,handle);
00416 #endif 
00417     return CAN_CMD_ACCEPTED;    
00418 }   

U8 can_tx ( U8  ch,
U8  handle,
U8  dlc,
U8  req_type,
const can_msg_t can_msg 
)

Start Transmission.

Parameters:
ch CAN channel selected 0 (CAN Channel 0) 1 (CAN Channel 1)
handle CAN MOB number
dlc Datalength
req_type CAN_DATA_FRAME or CAN_REMOTE_FRAME
can_msg CAN Message
Returns:
U8 CAN_CMD_REFUSED or CAN_CMD_ACCEPTED

Definition at line 339 of file can.c.

References CAN_CMD_ACCEPTED, CAN_CMD_REFUSED, CAN_REMOTE_FRAME, CANIF_config_tx, CANIF_mob_enable, CANIF_mob_enable_interrupt, CANIF_mob_set_automode, CANIF_mob_set_dlc, CANIF_set_data, CANIF_set_ext_id, CANIF_set_ext_idmask, CANIF_set_rtr, CANIF_set_rtrmask, CANIF_set_std_id, CANIF_set_std_idmask, can_msg_t::id, can_msg_t::id_mask, can_msg_t::ide_bit, and NB_MOB_CHANNEL.

00344 {
00345     if ((ch > 1) ||
00346         (handle > (NB_MOB_CHANNEL-1)) ||
00347         (dlc > 8))
00348         return  CAN_CMD_REFUSED;
00349     
00350     if (can_msg->ide_bit){        
00351              CANIF_set_ext_id(ch,
00352                             handle,
00353                             can_msg->id);
00354              
00355              CANIF_set_ext_idmask(ch,
00356                                 handle,
00357                                 can_msg->id_mask);
00358     }
00359     else { 
00360              CANIF_set_std_id(ch,
00361                             handle,
00362                             can_msg->id);
00363              CANIF_set_std_idmask(ch,
00364                                 handle,
00365                                 can_msg->id_mask);
00366     }
00367     CANIF_mob_set_dlc(ch,handle,dlc);
00368     if (req_type == CAN_REMOTE_FRAME){
00369             CANIF_set_rtr(ch,handle);  
00370             CANIF_set_rtrmask(ch,handle);            
00371             CANIF_mob_set_automode(ch,handle);
00372     }
00373     CANIF_set_data(ch,handle,((can_msg_t *)can_msg)->data.u64);
00374     CANIF_config_tx(ch,handle);
00375     CANIF_mob_enable(ch,handle);
00376 #ifdef CAN_LIB_UNDER_INTERRUPT          
00377     CANIF_mob_enable_interrupt(ch,handle);
00378 #endif 
00379     return CAN_CMD_ACCEPTED;                   
00380 }           


Variable Documentation

struct { ... } can_lib_params [static]

Dynamic Mob Allocation State Vector for Channel 0.

Definition at line 60 of file can.c.

Referenced by can_mob_alloc(), and can_mob_free().

Dynamic Mob Allocation State Vector for Channel 1.

Definition at line 63 of file can.c.

Referenced by can_mob_alloc(), and can_mob_free().

void(* can_msg_callback_channel0)(U8, U8)

void(* can_msg_callback_channel1)(U8, U8)


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