Subversion Repositories HomeAutomation

Rev

Rev 690 | Rev 869 | 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. StdCan_Ret_t StdCan_Init(Node_Desc_t* node_desc)
  86. {
  87.     StdCan_Ret_t retval;
  88.    
  89.     /* Reset all queue variables. */
  90.     RxQ_Rd_idx = 0;
  91.     RxQ_Wr_idx = 0;
  92.     RxQ_Len    = 0;
  93. #if (STDCAN_TX_QUEUE_SIZE > 1)
  94.     TxQ_Rd_idx = 0;
  95.     TxQ_Wr_idx = 0;
  96.     TxQ_Len    = 0;
  97. #endif
  98.    
  99. #if defined(_AVRLIB_BIOS_)
  100.     /* Initialize BIOS' interface for CAN. */
  101.     BIOS_CanCallback = Can_Process;
  102.     retval = StdCan_Ret_OK;
  103. #else
  104.     /* Initialize CAN driver. */
  105.     if (Can_Init() == CAN_OK)
  106.         retval = StdCan_Ret_OK;
  107.     else
  108.         retval = StdCan_Ret_Fail;
  109. #endif
  110.    
  111.     //TODO: Do something with the Node Descriptor.
  112.    
  113.     return retval;
  114. }
  115.  
  116. StdCan_Ret_t StdCan_Get(StdCan_Msg_t* msg)
  117. {
  118.     unsigned char n;
  119.    
  120.     /* Check if there's a message waiting. */
  121.     if (RxQ_Len) {
  122.        
  123.         /* Copy message to user buffer. */
  124.         msg->Id     = RxQ[RxQ_Rd_idx].Id;
  125.         msg->Length = RxQ[RxQ_Rd_idx].Length;
  126.         //TODO: Consider a mempcy() of the entire message.
  127.         for (n = 0; n < RxQ[RxQ_Rd_idx].Length; n++) {
  128.             msg->Data[n] = RxQ[RxQ_Rd_idx].Data[n];
  129.         }
  130.        
  131.         /* Increment read index and decrease queue length. */
  132.         if (++RxQ_Rd_idx >= STDCAN_RX_QUEUE_SIZE) RxQ_Rd_idx = 0;
  133.         RxQ_Len--;
  134.        
  135.         return StdCan_Ret_OK;
  136.     } else {
  137.         /* Queue is empty. */
  138.         return StdCan_Ret_Empty;
  139.     }
  140. }
  141.  
  142. StdCan_Ret_t StdCan_Put(StdCan_Msg_t* msg)
  143. {
  144.     Can_Message_t Can_Msg;
  145.     unsigned char n;
  146.    
  147.     /* Validate message. */
  148.     if ((unsigned)msg->Length > 8) return StdCan_Ret_DataErr;
  149.    
  150.     /* Copy message directly to lower layer until a proper
  151.      * queue has been implemented.
  152.      * TODO: Implement a proper queue. This requires TX interrupt
  153.      * support in the driver and BIOS.
  154.      */
  155.     Can_Msg.ExtendedFlag = 1;
  156.     Can_Msg.RemoteFlag = 0;
  157.     Can_Msg.Id = msg->Id;
  158.     Can_Msg.DataLength = msg->Length;
  159.     for (n = 0; n < msg->Length; n++) {
  160.         Can_Msg.Data.bytes[n] = msg->Data[n];
  161.     }
  162.    
  163.     if (Can_Send(&Can_Msg) == CAN_OK)
  164.         return StdCan_Ret_OK;
  165.     else
  166.         return StdCan_Ret_Full;
  167. }
  168.  
  169. void StdCan_SendHeartbeat(uint8_t n)
  170. {
  171.     /* TODO: When a Tx queue is implemented, the heartbeat should
  172.      * be sent via StdCan_Put instead of directly to lower layer.
  173.      */
  174.     Can_Message_t Heartbeat;
  175.    
  176.     /* Set up Heartbeat message format. */
  177.     Heartbeat.ExtendedFlag = 1;
  178.     Heartbeat.RemoteFlag = 0;
  179.     Heartbeat.DataLength = 0;
  180.     Heartbeat.Id = (CAN_NMT << CAN_SHIFT_CLASS)
  181.                  | (CAN_NMT_HEARTBEAT << CAN_SHIFT_NMT_TYPE)
  182.                  | (NODE_ID << CAN_SHIFT_NMT_SID);
  183.    
  184.     /* Try to send it. */
  185.     Can_Send(&Heartbeat);
  186. }
  187.  
  188. #if (STDCAN_FILTER)
  189. StdCan_Ret_t StdCan_EnableFilter(unsigned char filter, unsigned long id, unsigned long mask)
  190. {
  191.     if (filter < STDCAN_NUM_FILTERS) {
  192.         RxFilters[filter].Id = id;
  193.         RxFilters[filter].Mask = mask;
  194.         RxFilters[filter].Active = 1;
  195.         return StdCan_Ret_OK;
  196.     } else {
  197.         return StdCan_Ret_DataErr;
  198.     }
  199. }
  200.  
  201. StdCan_Ret_t StdCan_DisableFilter(unsigned char filter)
  202. {
  203.     if (filter < STDCAN_NUM_FILTERS) {
  204.         RxFilters[filter].Active = 0;
  205.         return StdCan_Ret_OK;
  206.     } else {
  207.         return StdCan_Ret_DataErr;
  208.     }
  209. }
  210. #endif
  211.