Subversion Repositories HomeAutomation

Rev

Rev 789 | Rev 1205 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1.  
  2. #include "stdcan.h"
  3. #if defined(_AVRLIB_BIOS_)
  4. #include <bios.h>
  5. #define Can_Send BIOS_CanSend
  6. #else
  7. #include <drivers/can/mcp2515/mcp2515.h>
  8. #endif
  9.  
  10. #if (STDCAN_TX_QUEUE_SIZE > 1)
  11. #error StdCan: Tx queue size longer than one msg not yet implemented.
  12. #endif
  13.  
  14. /**
  15.  * The receive queue.
  16.  */
  17. StdCan_Msg_t RxQ[STDCAN_RX_QUEUE_SIZE];
  18. unsigned char RxQ_Rd_idx; /**< Receive queue read index. */
  19. unsigned char RxQ_Wr_idx; /**< Receive queue write index. */
  20. unsigned char RxQ_Len; /**< Number of messages in receive queue. */
  21.  
  22. #if (STDCAN_TX_QUEUE_SIZE > 1)
  23. /**
  24.  * The transmit queue.
  25.  */
  26. StdCan_Msg_t TxQ[STDCAN_TX_QUEUE_SIZE];
  27. unsigned char TxQ_Rd_idx; /**< Transmit queue read index. */
  28. unsigned char TxQ_Wr_idx; /**< Transmit queue write index. */
  29. unsigned char TxQ_Len; /**< Number of messages in transmit queue. */
  30. #endif
  31.  
  32. #if (STDCAN_FILTER)
  33. /**
  34.  * The message acceptance filters.
  35.  */
  36. typedef struct {
  37.     char Active; /**< True if this filter should be used. */
  38.     unsigned long Id; /**< Match if id matches the message id in all bit locations that are not masked. */
  39.     unsigned long Mask; /**< Each bit specifies if the corresponding id bit must match (mask[n] = 1) or is Don't Care (mask[n] = 0). */
  40. } StdCan_Filter_t;
  41.  
  42. StdCan_Filter_t RxFilters[STDCAN_NUM_FILTERS];
  43. #endif
  44.  
  45. /**
  46.  * CAN message callback.
  47.  * Callback for when a message is received from the lower
  48.  * layer (BIOS or CAN driver).
  49.  */
  50. void Can_Process(Can_Message_t* msg)
  51. {
  52.     unsigned char n;
  53.     /* Check if there is room on the queue. */
  54.     if (RxQ_Len < STDCAN_RX_QUEUE_SIZE) {
  55.        
  56. #if (STDCAN_FILTER)
  57.         /* Try to match each filter in turn. */
  58.         for (n = 0; n < STDCAN_NUM_FILTERS; n++) {
  59.             if (RxFilters[n].Active && !((msg->Id ^ RxFilters[n].Id) & RxFilters[n].Mask)) {
  60.                 RxQ[RxQ_Wr_idx].Match = n;
  61.                 break;
  62.             }
  63.         }
  64.        
  65.         if (n == STDCAN_NUM_FILTERS) return; // No match found, discard message.
  66. #endif
  67.         /* Copy the message from lower layer into the queue. */
  68.         RxQ[RxQ_Wr_idx].Id     = msg->Id;
  69.         RxQ[RxQ_Wr_idx].Length = msg->DataLength;
  70.         //TODO: This should be guarded against invalid DataLength.
  71.         for (n = 0; n < RxQ[RxQ_Wr_idx].Length; n++) {
  72.             RxQ[RxQ_Wr_idx].Data[n] = msg->Data.bytes[n];
  73.         }
  74.         /* Increment write index and queue length. */
  75.         if (++RxQ_Wr_idx >= STDCAN_RX_QUEUE_SIZE) RxQ_Wr_idx = 0;
  76.         RxQ_Len++;
  77.     } else {
  78.         /* Overflow, just drop the new message. In the future, some
  79.          * form of priority scheme could be used to drop another
  80.          * message in the queue.
  81.          */
  82.     }
  83. }
  84.  
  85. #ifdef MODULE_APPLICATION
  86. StdCan_Ret_t StdCan_Init(void)
  87. {
  88. #else
  89. StdCan_Ret_t StdCan_Init(Node_Desc_t* node_desc)
  90. {
  91. #endif
  92.     StdCan_Ret_t retval;
  93.    
  94.     /* Reset all queue variables. */
  95.     RxQ_Rd_idx = 0;
  96.     RxQ_Wr_idx = 0;
  97.     RxQ_Len    = 0;
  98. #if (STDCAN_TX_QUEUE_SIZE > 1)
  99.     TxQ_Rd_idx = 0;
  100.     TxQ_Wr_idx = 0;
  101.     TxQ_Len    = 0;
  102. #endif
  103.    
  104. #if defined(_AVRLIB_BIOS_)
  105.     /* Initialize BIOS' interface for CAN. */
  106.     BIOS_CanCallback = Can_Process;
  107.     retval = StdCan_Ret_OK;
  108. #else
  109.     /* Initialize CAN driver. */
  110.     if (Can_Init() == CAN_OK)
  111.         retval = StdCan_Ret_OK;
  112.     else
  113.         retval = StdCan_Ret_Fail;
  114. #endif
  115.    
  116.     //TODO: Do something with the Node Descriptor.
  117.     //(why have constats passed as parameters? they are defined at compiletime /arune)
  118. #if defined(_AVRLIB_BIOS_)
  119.     /* TODO: When a Tx queue is implemented, the startup message should
  120.      * be sent via StdCan_Put instead of directly to lower layer.
  121.      */
  122.     Can_Message_t Startup;
  123.    
  124.     /* Set up Startup message format. */
  125.     Startup.ExtendedFlag = 1;
  126.     Startup.RemoteFlag = 0;
  127.     Startup.DataLength = 4;
  128. #ifdef MODULE_APPLICATION
  129.     Startup.Id = (CAN_NMT << CAN_SHIFT_CLASS) | (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE);
  130.     Startup.Data.bytes[0] = NODE_HW_ID&0xff;
  131.     Startup.Data.bytes[1] = (NODE_HW_ID>>8)&0xff;
  132.     Startup.Data.bytes[2] = (NODE_HW_ID>>16)&0xff;
  133.     Startup.Data.bytes[3] = (NODE_HW_ID>>24)&0xff;
  134. #else
  135.     Startup.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
  136.     Startup.Data.bytes[1] = APP_TYPE&0xff;
  137.     Startup.Data.bytes[0] = (APP_TYPE>>8)&0xff;
  138.     Startup.Data.bytes[3] = APP_VERSION&0xff;
  139.     Startup.Data.bytes[2] = (APP_VERSION>>8)&0xff;
  140. #endif
  141.     /* Try to send it. */
  142.     Can_Send(&Startup);
  143. #endif
  144.    
  145.     return retval;
  146. }
  147.  
  148. StdCan_Ret_t StdCan_Get(StdCan_Msg_t* msg)
  149. {
  150.     unsigned char n;
  151.    
  152.     /* Check if there's a message waiting. */
  153.     if (RxQ_Len) {
  154.        
  155.         /* Copy message to user buffer. */
  156.         msg->Id     = RxQ[RxQ_Rd_idx].Id;
  157.         msg->Length = RxQ[RxQ_Rd_idx].Length;
  158.         //TODO: Consider a mempcy() of the entire message.
  159.         for (n = 0; n < RxQ[RxQ_Rd_idx].Length; n++) {
  160.             msg->Data[n] = RxQ[RxQ_Rd_idx].Data[n];
  161.         }
  162.        
  163.         /* Increment read index and decrease queue length. */
  164.         if (++RxQ_Rd_idx >= STDCAN_RX_QUEUE_SIZE) RxQ_Rd_idx = 0;
  165.         RxQ_Len--;
  166.        
  167.         return StdCan_Ret_OK;
  168.     } else {
  169.         /* Queue is empty. */
  170.         return StdCan_Ret_Empty;
  171.     }
  172. }
  173.  
  174. unsigned char StdCan_Get_Pending(void)
  175. {
  176.     return RxQ_Len;
  177. }
  178.  
  179. StdCan_Ret_t StdCan_Put(StdCan_Msg_t* msg)
  180. {
  181.     Can_Message_t Can_Msg;
  182.     unsigned char n;
  183.    
  184.     /* Validate message. */
  185.     if ((unsigned)msg->Length > 8) return StdCan_Ret_DataErr;
  186.    
  187.     /* Copy message directly to lower layer until a proper
  188.      * queue has been implemented.
  189.      * TODO: Implement a proper queue. This requires TX interrupt
  190.      * support in the driver and BIOS.
  191.      */
  192.     Can_Msg.ExtendedFlag = 1;
  193.     Can_Msg.RemoteFlag = 0;
  194.     Can_Msg.Id = msg->Id;
  195.     Can_Msg.DataLength = msg->Length;
  196.     for (n = 0; n < msg->Length; n++) {
  197.         Can_Msg.Data.bytes[n] = msg->Data[n];
  198.     }
  199.  
  200.     if (Can_Send(&Can_Msg) == CAN_OK) {
  201. #ifdef MODULE_APPLICATION
  202.         Can_Process(&Can_Msg);
  203. #endif
  204.         return StdCan_Ret_OK;
  205.     }
  206.     else
  207.         return StdCan_Ret_Full;
  208. }
  209.  
  210. void StdCan_SendHeartbeat(uint8_t n)
  211. {
  212.     /* TODO: When a Tx queue is implemented, the heartbeat should
  213.      * be sent via StdCan_Put instead of directly to lower layer.
  214.      */
  215.     Can_Message_t Heartbeat;
  216.    
  217.     /* Set up Heartbeat message format. */
  218.     Heartbeat.ExtendedFlag = 1;
  219.     Heartbeat.RemoteFlag = 0;
  220. #ifdef MODULE_APPLICATION
  221.     Heartbeat.DataLength = 4;
  222.     Heartbeat.Id = (CAN_NMT << CAN_SHIFT_CLASS) | (CAN_NMT_HEARTBEAT << CAN_SHIFT_NMT_TYPE);
  223.     Heartbeat.Data.bytes[0] = NODE_HW_ID&0xff;
  224.     Heartbeat.Data.bytes[1] = (NODE_HW_ID>>8)&0xff;
  225.     Heartbeat.Data.bytes[2] = (NODE_HW_ID>>16)&0xff;
  226.     Heartbeat.Data.bytes[3] = (NODE_HW_ID>>24)&0xff;
  227. #else
  228.    
  229.    
  230.     Heartbeat.DataLength = 0;
  231.     Heartbeat.Id = (CAN_NMT << CAN_SHIFT_CLASS)
  232.                  | (CAN_NMT_HEARTBEAT << CAN_SHIFT_NMT_TYPE)
  233.                  | (NODE_ID << CAN_SHIFT_NMT_SID);
  234. #endif
  235.    
  236.     /* Try to send it. */
  237.     Can_Send(&Heartbeat);
  238. }
  239.  
  240. #if (STDCAN_FILTER)
  241. StdCan_Ret_t StdCan_EnableFilter(unsigned char filter, unsigned long id, unsigned long mask)
  242. {
  243.     if (filter < STDCAN_NUM_FILTERS) {
  244.         RxFilters[filter].Id = id;
  245.         RxFilters[filter].Mask = mask;
  246.         RxFilters[filter].Active = 1;
  247.         return StdCan_Ret_OK;
  248.     } else {
  249.         return StdCan_Ret_DataErr;
  250.     }
  251. }
  252.  
  253. StdCan_Ret_t StdCan_DisableFilter(unsigned char filter)
  254. {
  255.     if (filter < STDCAN_NUM_FILTERS) {
  256.         RxFilters[filter].Active = 0;
  257.         return StdCan_Ret_OK;
  258.     } else {
  259.         return StdCan_Ret_DataErr;
  260.     }
  261. }
  262. #endif
  263.