can.h

00001 /***************************************************************************
00002                           can.h  -
00003               CAN control functions .
00004                              -------------------
00005     begin                : Tue Oct 14 2003
00006     email                : support@emacinc.com
00007  ***************************************************************************/
00008 
00009 
00015 
00016 #ifndef CAN_H
00017 #define CAN_H
00018 
00019 #include <gnu_defs.h>
00020 #include <string.h>
00021 
00022 
00023 /*This is the function around which all CAN register references are 
00024  * wrapped. It takes the base CAN address from the variable "base" 
00025  * in the current context.
00026  */
00027 static inline u8 *caddr(int x, volatile u8 *base){
00028     return ((u8 *)(x + (unsigned long)base));
00029 }
00030 
00031 #define MODULE_OFFSET(x) *(caddr(x,base))
00032 #define MARRAY_OFFSET(x)  (caddr(x,base))
00033 
00034 #define CANCTL0     MODULE_OFFSET(0x0)
00035     #define INITRQ  0x01
00036     #define CSWAI   0x20
00037 
00038 #define CANCTL1     MODULE_OFFSET(0x1)
00039     #define CANE    0x80
00040     #define CLKSRC  0x40
00041     #define LISTEN  0x10
00042     #define LOOPB   0x20
00043     #define INITAK  0x01
00044 
00045 #define CANBTR0     MODULE_OFFSET(0x2)
00046 #define CANBTR1     MODULE_OFFSET(0x3)
00047 #define CANRFLG     MODULE_OFFSET(0x4)
00048     #define CSCIF   0x40
00049     #define RXF 0x01
00050     #define OVRIF   0x02    
00051     
00052 #define CANRIER     MODULE_OFFSET(0x5)
00053     #define WUPIE   0x80
00054     #define RXFIE   0x01
00055 
00056 #define CANTFLG     MODULE_OFFSET(0x6)
00057 #define CANTIER     MODULE_OFFSET(0x7)
00058 #define CANTARQ     MODULE_OFFSET(0x8)
00059 #define CANTAAK     MODULE_OFFSET(0x9) 
00060 #define CANTBSEL    MODULE_OFFSET(0xa)
00061 #define CANIDAC     MODULE_OFFSET(0xb)
00062 #define CANRXERR    MODULE_OFFSET(0xe)
00063 #define CANTXERR    MODULE_OFFSET(0xf)
00064 #define CANIDAR0    MODULE_OFFSET(0x10)
00065 #define CANIDAR1    MODULE_OFFSET(0x11)
00066 #define CANIDAR2    MODULE_OFFSET(0x12)
00067 #define CANIDAR3    MODULE_OFFSET(0x13)
00068 #define CANIDMR0    MODULE_OFFSET(0x14)
00069 #define CANIDMR1    MODULE_OFFSET(0x15)
00070 #define CANIDMR2    MODULE_OFFSET(0x16)
00071 #define CANIDMR3    MODULE_OFFSET(0x17)
00072 #define CANIDAR4    MODULE_OFFSET(0x18)
00073 #define CANIDAR5    MODULE_OFFSET(0x19)
00074 #define CANIDAR6    MODULE_OFFSET(0x1a)
00075 #define CANIDAR7    MODULE_OFFSET(0x1b)
00076 #define CANIDMR4    MODULE_OFFSET(0x1c)
00077 #define CANIDMR5    MODULE_OFFSET(0x1d)
00078 #define CANIDMR6    MODULE_OFFSET(0x1e)
00079 #define CANIDMR7    MODULE_OFFSET(0x1f)
00080 
00081 #define CANRXFG     MARRAY_OFFSET(0x20)
00082 #define CANTXFG     MARRAY_OFFSET(0x30)
00083 
00084 //status macros
00085 #define CAN_RUNNING      CANCTL1&INITAK
00086 #define CAN_RXBUF_FULL   CANRFLG&RXF 
00087 #define CANOVERRUN   CANRFLG&OVRIF
00088 
00089 #define QUEUE_LENGTH    16      /*rx messages queue*/
00090 #define CANTXQ_LENGTH   5       /*tx message queue*/
00091 
00092 #define CAN_MSG_LENGTH  8
00093 
00094 #define MSG_RTR     (1<<0)      
00095 #define RTR     0x10
00096 
00099 typedef struct canmsg{
00100     // flags, indicating or controlling special message properties 
00101     u8 flags;
00102     u16 id;  
00103     u8 length;
00104     u8 data[CAN_MSG_LENGTH]; 
00105 } canmsg;
00106 
00119 typedef struct canqueue{
00120     int in;
00121     int out;
00122     int size;
00123     int messages;
00124     canmsg *queue;
00125 }canqueue; 
00126 
00132 typedef struct can_module{  
00133     unsigned long long id;
00134     unsigned long long mask;
00135     unsigned long baud;
00136     int index;
00137     volatile u8 *base;
00138     canqueue tx,rx;
00139 }can_module;
00140 
00145 static inline void CAN_EMPTYQUEUE(can_module *CAN){
00146     CAN->tx.messages = CAN->tx.in = CAN->tx.out = CAN->rx.messages =
00147      CAN->rx.in = CAN->rx.out = 0;
00148 }
00149 
00153 static inline canmsg *CAN_READ_BUFFER(canqueue *q){
00154     return (&q->queue[q->out]);
00155 }
00156 
00160 static inline canmsg *CAN_WRITE_BUFFER(canqueue *q){
00161     return (&q->queue[q->in]);
00162 }
00163 
00168 static inline void CAN_ENQEUE(canqueue *q){
00169     q->in++;
00170     if(q->in%q->size)q->in=0;
00171     q->messages++;
00172 }
00173 
00178 static inline void CAN_RX_REMOVE(canqueue *q){
00179     q->out++;
00180     if(q->out%q->size)q->out=0;
00181     q->messages--;
00182 }
00183 
00188 static inline void CAN_PUSH(canqueue *q,canmsg *msg){
00189     memcpy(CAN_WRITE_BUFFER(q),msg,sizeof(canmsg));
00190     CAN_ENQEUE(q);
00191 }
00196 static inline void CAN_POP(canmsg *msg,canqueue *q){
00197     memcpy(msg,CAN_READ_BUFFER(q),sizeof(canmsg));
00198     CAN_RX_REMOVE(q);
00199 }
00200 
00206 static inline void CAN_TX_ENABLE(can_module *CAN){
00207     volatile u8 *base = CAN->base;
00208     CANTIER=7;
00209 }
00211 static inline void CAN_TX_DISABLE(can_module *CAN){
00212     volatile u8 *base = CAN->base;
00213     CANTIER=0;
00214 }
00217 static inline void CAN_RX_ENABLE(can_module *CAN){
00218     volatile u8 *base = CAN->base;
00219     CANRIER|=RXFIE; 
00220 }
00223 static inline void CAN_RX_DISABLE(can_module *CAN){
00224     volatile u8 *base = CAN->base;
00225     CANRIER&=~RXFIE; 
00226 }
00227 
00232 static inline void can_print_message(int comport,canmsg *msg){
00233     ser_printf(comport,"canmsg %u\r\n",(long)msg->id);
00234     ser_printf(comport,"flags-> %u\r\n",(long)msg->flags);
00235     {int i;
00236         ser_printf(comport,"data: ");
00237         for (i=0;i<msg->length;i++) 
00238             ser_printf(comport,"%u ",(long)msg->data[i]);
00239     }ser_printf(comport,"\r\n");
00240 }
00241 
00246 can_module * __attribute__((far))CAN_device(int index);
00247 
00253 int __attribute__((far))CANStart(can_module *CAN);
00254 
00256 #endif

Generated on Fri Mar 3 17:25:45 2006 for EMAC-HCS12-SDK by  doxygen 1.4.6