can.h

00001 #ifndef CAN_H
00002 #define CAN_H
00003 
00004 //Various type getter and setter methods
00005 typedef void(*CAN_RX_SETTER_DOUBLE)(double);
00006 typedef void(*CAN_RX_SETTER_FLOAT)(float);
00007 typedef void(*CAN_RX_SETTER_INT)(int);
00008 typedef void(*CAN_RX_SETTER_SHORT)(short);
00009 
00010 typedef double(*CAN_TX_GETTER_DOUBLE)(void);
00011 typedef float(*CAN_TX_GETTER_FLOAT)(void);
00012 typedef int(*CAN_TX_GETTER_INT)(void);
00013 typedef short(*CAN_TX_GETTER_SHORT)(void);
00014 
00015 //void-void function typedef
00016 typedef void(*CAN_VV_PTR)(void);
00017 
00018 typedef enum can_dispatch_modes{
00019   CAN_DISPATCH_AUTO,
00020   CAN_DISPATCH_MANUAL
00021 } CAN_DISPATCH_MODE;
00022 
00023 
00024 //Value is the index of the CAN channel used in the can_rx and can_tx code
00025 typedef enum can_channels {
00026   CHAN_SSP  = 0,
00027   CHAN_CAN1 = 1,
00028   CHAN_CAN2 = 2,
00029   CHAN_CAN3 = 3,
00030   CHAN_CAN4 = 4,
00031   CHAN_ROUTER = 5
00032 } CAN_CHANNEL;
00033 
00034 //Enumerates the possible CAN Packet layouts
00035 typedef enum can_layout{
00036   CAN_LAYOUT_D,
00037   CAN_LAYOUT_FF,
00038   CAN_LAYOUT_II,
00039   CAN_LAYOUT_FI,
00040   CAN_LAYOUT_ISS  
00041 }CAN_LAYOUT;
00042 
00043 typedef __packed union can_payload{
00044   __packed struct {
00045     double d1;
00046   } d;
00047   __packed struct {
00048     int w1;
00049     int w2;
00050   } w;
00051   __packed struct {
00052     int i1;
00053     int i2;
00054   } i;
00055   __packed struct {
00056     float f1;
00057     float f2;
00058   } f;
00059   __packed struct{
00060     short s1;
00061     short s2;
00062     short s3;
00063     short s4;
00064   } s;
00065   __packed struct{
00066     unsigned char b1;
00067     unsigned char b2;
00068     unsigned char b3;
00069     unsigned char b4;
00070     unsigned char b5;
00071     unsigned char b6;
00072     unsigned char b7;
00073     unsigned char b8;
00074   } b;
00075 } CAN_PAYLOAD;
00076 
00077 //Generic packet descriptor
00078 typedef struct can_frame_descriptor{
00079   int          addr : 11;
00080   CAN_CHANNEL  chan;
00081   char         rtr  :  1;
00082   CAN_LAYOUT   frame_layout;
00083   CAN_VV_PTR   ptr1;
00084   CAN_VV_PTR   ptr2;
00085   CAN_VV_PTR   ptr3;
00086   CAN_VV_PTR   ptr4;
00087   CAN_VV_PTR   ptr5;
00088   CAN_VV_PTR   ptr6;
00089   CAN_VV_PTR   ptr7;
00090   CAN_VV_PTR   ptr8;
00091 } CAN_FRAME_DESC;
00092 
00093 typedef struct can_frame{
00094   CAN_CHANNEL  chan;
00095   int          addr : 11;
00096   int          dlc  :  4;
00097   char         rtr  :  1;
00098   CAN_PAYLOAD  payload;
00099 } CAN_FRAME;
00100 
00101 typedef struct can_ring{
00102   CAN_FRAME  *  buf;
00103   int           buf_len;
00104   volatile int  in_idx;
00105   volatile int  out_idx;
00106 } CAN_RING;
00107 
00108 
00109 typedef struct can_rx_chan_cfg{
00110   volatile unsigned long   * base_addr;
00111   CAN_RING                 * ring;
00112   CAN_FRAME_DESC          ** descriptors;
00113   CAN_DISPATCH_MODE          dispatch_mode;
00114 } CAN_RX_CHAN_CFG;
00115 
00116 typedef struct can_tx_chan_cfg{
00117   volatile unsigned long   * base_addr;
00118   CAN_RING                 * ring;
00119   int                        stalled;
00120 } CAN_TX_CHAN_CFG;
00121 
00122 
00123 //TX DESCRIPTOR SETTING FUNCTIONS
00124 void can_set_tx_descriptor_d(CAN_FRAME_DESC* frame_desc,int addr,CAN_CHANNEL chan,
00125      CAN_TX_GETTER_DOUBLE g_d1
00126      );
00127 void can_set_tx_descriptor_ff(CAN_FRAME_DESC* frame_desc,int addr,CAN_CHANNEL chan,
00128      CAN_TX_GETTER_FLOAT g_f1,CAN_TX_GETTER_FLOAT g_f2
00129      );
00130 void can_set_tx_descriptor_ii(CAN_FRAME_DESC* frame_desc,int addr,CAN_CHANNEL chan,
00131      CAN_TX_GETTER_INT g_i1,CAN_TX_GETTER_INT g_i2
00132      );
00133 void can_set_tx_descriptor_fi(CAN_FRAME_DESC* frame_desc,int addr,CAN_CHANNEL chan,
00134   CAN_TX_GETTER_FLOAT g_f1,CAN_TX_GETTER_INT g_i1
00135   );
00136 void can_set_tx_descriptor_iss(CAN_FRAME_DESC* frame_desc,int addr,CAN_CHANNEL chan,
00137      CAN_TX_GETTER_INT g_i1,CAN_TX_GETTER_SHORT g_s1, CAN_TX_GETTER_SHORT g_s2
00138      );
00139      
00140 void can_set_tx_descriptor_rtr(CAN_FRAME_DESC* frame_desc,int addr,CAN_CHANNEL chan);
00141 
00142 //RX DESCRIPTOR SETTING FUNCTIONS
00143 void can_set_rx_descriptor_d(CAN_FRAME_DESC* frame_desc,int addr,CAN_CHANNEL chan,
00144      CAN_RX_SETTER_DOUBLE s_d1
00145      );
00146 void can_set_rx_descriptor_ff(CAN_FRAME_DESC* frame_desc,int addr,CAN_CHANNEL chan,
00147      CAN_RX_SETTER_FLOAT s_f1,CAN_RX_SETTER_FLOAT s_f2
00148      );
00149 void can_set_rx_descriptor_ii(CAN_FRAME_DESC* frame_desc,int addr,CAN_CHANNEL chan,
00150      CAN_RX_SETTER_INT s_i1,CAN_RX_SETTER_INT s_i2
00151      );
00152      
00153 void can_set_rx_descriptor_fi(CAN_FRAME_DESC* frame_desc,int addr,CAN_CHANNEL chan,
00154   CAN_RX_SETTER_FLOAT s_f1,CAN_RX_SETTER_INT s_i1
00155   );
00156 void can_set_rx_descriptor_iss(CAN_FRAME_DESC* frame_desc,int addr,CAN_CHANNEL chan,
00157      CAN_RX_SETTER_INT s_i1,CAN_RX_SETTER_SHORT s_s1, CAN_RX_SETTER_SHORT s_s2
00158      );  
00159 
00160 
00161 //public function declarations
00162 void can_rx_dispatch_all(void);
00163 unsigned short can_rx_pop_frame(CAN_FRAME * frameptr);
00164 void can_rx1_isr(void) __irq;
00165 void can_rx2_isr(void) __irq;
00166 void can_rx3_isr(void) __irq; 
00167 void can_rx4_isr(void) __irq;
00168 void can_rx1_fiq(void);
00169 void can_rx2_fiq(void);
00170 void can_rx3_fiq(void);
00171 void can_rx4_fiq(void);
00172 void can_tx1(void);
00173 void can_tx2(void);
00174 void can_tx3(void);
00175 void can_tx4(void);
00176 void can_error_isr(void) __irq;
00177 
00178 
00179 //Return a CAN_FRAME with the number of CAN receive or transmit
00180 //on that channel since the previous call.
00181 unsigned short can_rx_get_frame_count_1(CAN_FRAME * frame);
00182 unsigned short can_rx_get_frame_count_2(CAN_FRAME * frame);
00183 unsigned short can_rx_get_frame_count_3(CAN_FRAME * frame);
00184 unsigned short can_rx_get_frame_count_4(CAN_FRAME * frame);
00185 
00186 unsigned short can_tx_get_frame_count_1(CAN_FRAME * frame);
00187 unsigned short can_tx_get_frame_count_2(CAN_FRAME * frame);
00188 unsigned short can_tx_get_frame_count_3(CAN_FRAME * frame);
00189 unsigned short can_tx_get_frame_count_4(CAN_FRAME * frame);
00190 
00191 //Return a CAN_FRAME with the error number on that channel
00192 unsigned short can_get_error_1(CAN_FRAME * frame);
00193 unsigned short can_get_error_2(CAN_FRAME * frame);
00194 unsigned short can_get_error_3(CAN_FRAME * frame);
00195 unsigned short can_get_error_4(CAN_FRAME * frame);
00196 
00197 void can_init_status_callback(
00198   void (* rx_frame_count)(unsigned short),
00199   void (* tx_frame_count)(unsigned short),
00200   void (* error_led)(unsigned short));
00201 
00202 void can_rx_set_descriptors(CAN_FRAME_DESC ** rx_descriptors,CAN_FRAME_DESC ** rtr_descriptors);
00203 void   can_rx_set_chan_cfg(CAN_CHANNEL chan,volatile unsigned long * base_addr, CAN_RING * rx_ring, CAN_DISPATCH_MODE mode);
00204 void   can_tx_set_chan_cfg(CAN_CHANNEL chan,volatile unsigned long * base_addr, CAN_RING * tx_ring);
00205 
00206 int   can_transmit(CAN_FRAME_DESC * fd);
00207 int   can_transmit_alt(CAN_FRAME_DESC * fd,CAN_CHANNEL chan, char rtr);
00208 int   can_transmit_frame(CAN_FRAME * frame);
00209 int   can_tx_now(CAN_CHANNEL chan);
00210 
00211 double can_tx_getter_double_dummy(void);
00212 float  can_tx_getter_float_dummy(void);
00213 int    can_tx_getter_int_dummy(void);
00214 short  can_tx_getter_short_dummy(void);
00215 
00216 void can_rx_setter_double_dummy(double d);
00217 void can_rx_setter_float_dummy(float f);
00218 void can_rx_setter_int_dummy(int i);
00219 void can_rx_setter_short_dummy(short s);
00220 
00221 //........Ring Buffer
00222 void can_ring_init(CAN_RING * ring, CAN_FRAME * frame_buf, int buf_len);
00223 int  can_ring_push(CAN_RING * ring, CAN_FRAME * frame);
00224 int  can_ring_pop(CAN_RING * ring, CAN_FRAME * frame);
00225 
00226 //private function declarations
00227 //........Rx
00228 void can_rx_now(CAN_CHANNEL chan);
00229 void can_rx_dispatch_frame(CAN_FRAME * frame);
00230 void can_rx_dispatch_chan(CAN_CHANNEL chan);
00231 void can_rx_dispatch_all(void);
00232 void can_config_acceptance(void);
00233 
00234 //........Tx
00235 void can_tx_send_next_frame(CAN_CHANNEL chan);
00236 
00237 //........isr
00238 void can_voidint(unsigned short chan);
00239 
00240 unsigned short int can_tx1_push_frame(CAN_FRAME * frameptr);
00241 unsigned short int can_tx2_push_frame(CAN_FRAME * frameptr);
00242 unsigned short int can_tx3_push_frame(CAN_FRAME * frameptr);
00243 unsigned short int can_tx4_push_frame(CAN_FRAME * frameptr);
00244 
00245 
00246 #define CAN_MOD  (0x00)
00247 #define CAN_CMR  (0x04)
00248 #define CAN_GSR  (0x08)
00249 #define CAN_ICR  (0x0C)
00250 #define CAN_IER  (0x10)
00251 #define CAN_BTR  (0x14)
00252 #define CAN_EWL  (0x18)
00253 #define CAN_SR   (0x1C)
00254 #define CAN_RFS  (0x20)
00255 #define CAN_RID  (0x24)
00256 #define CAN_RDA  (0x28)
00257 #define CAN_RDB  (0x2C)
00258 #define CAN_TFI1 (0x30)
00259 #define CAN_TID1 (0x34)
00260 #define CAN_TDA1 (0x38)
00261 #define CAN_TDB1 (0x3C)
00262 #define CAN_TFI2 (0x40)
00263 #define CAN_TID2 (0x44)
00264 #define CAN_TDA2 (0x48)
00265 #define CAN_TDB2 (0x4C)
00266 #define CAN_TFI3 (0x50)
00267 #define CAN_TID3 (0x54)
00268 #define CAN_TDA3 (0x58)
00269 #define CAN_TDB3 (0x5C)
00270 
00271 #define CAN_REG(base,offset) \
00272         (*((volatile unsigned long *) (((volatile unsigned char *)base) + offset)))
00273 
00274 #endif /* CAN_H */
Generated on Tue Jun 29 16:36:14 2010 by  doxygen 1.6.3