Subversion Repositories HomeAutomation

Rev

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

  1. /**
  2.  * @defgroup stdcan StdCan Library
  3.  * @code #include <drivers/can/stdcan.h> @endcode
  4.  *
  5.  * @brief High level functions for bus communication and standard node behaviour.
  6.  *
  7.  * This library provides a higher level interface to the CAN bus than the raw driver/BIOS
  8.  * interface, as well as functions for easy implementation of required node behaviour.
  9.  *
  10.  * It implements receive and transmit FIFOs of configurable lengths, Application Startup
  11.  * message at init, and Heartbeat transmission.
  12.  *
  13.  * @author  Andreas Fritiofson
  14.  * @date    2007-10-24
  15.  */
  16.  
  17. /**@{*/
  18.  
  19. //#error StdCan: For discussion only! Do not use in application code.
  20. //#warning StdCan: Not thouroughly tested yet.
  21.  
  22. #ifndef STDCAN_H_
  23. #define STDCAN_H_
  24.  
  25. #include <config.h>
  26.  
  27. #include <drivers/can/canid.h>
  28. #include <inttypes.h>
  29.  
  30. #ifndef STDCAN_FILTER
  31. #define STDCAN_FILTER 0
  32. #endif
  33. #ifndef STDCAN_RX_QUEUE_SIZE
  34. #define STDCAN_RX_QUEUE_SIZE 8
  35. #endif
  36. #ifndef STDCAN_TX_QUEUE_SIZE
  37. #define STDCAN_TX_QUEUE_SIZE 1
  38. #endif
  39.  
  40. /**
  41.  * @brief Return values.
  42.  *
  43.  * Most StdCan functions return one of these values.
  44.  */
  45. typedef enum {
  46.     StdCan_Ret_OK, /**< Operation completed successfully. */
  47.     StdCan_Ret_Full, /**< Failed to send because transmit queue was full. */
  48.     StdCan_Ret_Empty, /**< Failed to read because receive queue was empty. */
  49.     StdCan_Ret_Fail, /**< General error. */
  50.     StdCan_Ret_DataErr /**< Malformed data. */
  51. } StdCan_Ret_t;
  52.  
  53. /**
  54.  * @brief Message structure.
  55.  *
  56.  * Stores a CAN message frame. Flags that are unsupported have been
  57.  * excluded (RTR and standard id).
  58.  */
  59. typedef struct {
  60. #ifdef MODULE_APPLICATION
  61.     union {
  62.         uint32_t Id;    /**< CAN extended ID (29 bits). */
  63.         struct {
  64.             uint8_t Command;
  65.             uint8_t ModuleId;
  66.             uint8_t ModuleType;
  67.             uint8_t ClassAndDirection;
  68.         } Header;
  69.     };
  70. #else
  71.     unsigned long Id; /**< CAN extended ID (29 bits). */
  72. #endif
  73.     char Length; /**< Data length [0,8]. */
  74.     unsigned char Data[8]; /**< Data array. Only the first \c Length elements are valid. */
  75. #if (STDCAN_FILTER)
  76.     unsigned char Match; /**< Filter id that matched this message. */
  77. #endif
  78. } StdCan_Msg_t;
  79.  
  80. #define StdCan_Ret_class(Header) (uint8_t)((Header.ClassAndDirection >> 1) & 0x0F)
  81. #define StdCan_Set_class(Header, CLASS) Header.ClassAndDirection &= 0x01; Header.ClassAndDirection |= (CLASS << 1);
  82. #define StdCan_Ret_direction(Header) (uint8_t)(Header.ClassAndDirection & 0x01)
  83. #define StdCan_Set_direction(Header, DIR) Header.ClassAndDirection &= 0xfe; Header.ClassAndDirection |= DIR;
  84.  
  85. #define NODE_HW_ID_BYTE3 (uint8_t)((BIOS_GetHwId()>>24)&0xff)
  86. #define NODE_HW_ID_BYTE2 (uint8_t)((BIOS_GetHwId()>>16)&0xff)
  87. #define NODE_HW_ID_BYTE1 (uint8_t)((BIOS_GetHwId()>>8)&0xff)
  88. #define NODE_HW_ID_BYTE0 (uint8_t)((BIOS_GetHwId())&0xff)
  89.  
  90. /**
  91.  * @brief Node descriptor.
  92.  *
  93.  * Describes the node and the application running on it.
  94.  * TODO: Discuss the purpose and layout of Node_Desc_t.
  95.  */
  96. typedef struct {
  97.     unsigned short Type; /**< Application type (documented elsewhere). */
  98.     unsigned short Version; /**< Application version number (application defined). */
  99.     unsigned char Id; /**< Node ID. */
  100. } Node_Desc_t;
  101.  
  102. //(why have constats passed as parameters? they are defined at compiletime /arune)
  103.  
  104.  
  105. /**
  106.  * @brief Initialize StdCan.
  107.  *
  108.  * Initializes the StdCan and lower layers and sends an Application Startup
  109.  * Message.
  110.  *
  111.  * @param node_desc
  112.  *      Pointer to node descriptor, containing information about the type and
  113.  *      version of the application that should be announced in startup and
  114.  *      heartbeat messages.
  115.  */
  116. #ifdef MODULE_APPLICATION
  117. StdCan_Ret_t StdCan_Init(void);
  118. #else
  119. StdCan_Ret_t StdCan_Init(Node_Desc_t* node_desc);
  120. #endif
  121. /**
  122.  * @brief Get a message.
  123.  *
  124.  * Retrieves a message from the receive queue.
  125.  *
  126.  * @param[out] msg
  127.  *      Pointer to message stucture to be filled with the next message from
  128.  *      the queue.
  129.  */
  130. StdCan_Ret_t StdCan_Get(StdCan_Msg_t* msg);
  131.  
  132. /**
  133.  * @brief Queue size.
  134.  *
  135.  * Returns the number of messages pending in the receive queue.
  136.  *
  137.  * @retval
  138.  *      Number of messages in the receive queue.
  139.  */
  140. unsigned char StdCan_Get_Pending(void);
  141.  
  142. /**
  143.  * @brief Send a message.
  144.  *
  145.  * Puts a message on the transmit queue.
  146.  *
  147.  * @param[in] msg
  148.  *      Pointer to message to send.
  149.  */
  150. StdCan_Ret_t StdCan_Put(StdCan_Msg_t* msg);
  151.  
  152. /**
  153.  * @brief Send a Heartbeat.
  154.  *
  155.  * Puts a Heartbeat message on the transmit queue. Takes an \c uint8_t and
  156.  * returns \c void in order to be able to use it as a \c Timer callback
  157.  * directly, i.e.
  158.  * @code
  159.  * Timer_SetTimeout(APP_HEARTBEAT_TIMER, STDCAN_HEARTBEAT_PERIOD, TimerTypeFreeRunning, StdCan_SendHeartbeat);
  160.  * @endcode
  161.  * If the transmit queue is full this function will silently fail.
  162.  *
  163.  * @param n
  164.  *      Dummy value.
  165.  */
  166. void StdCan_SendHeartbeat(uint8_t n);
  167.  
  168. /**
  169.  * @brief Enable a message acceptance filter.
  170.  *
  171.  * Allows incoming messages to be accepted or rejected by matching their id.
  172.  * Message is accepted if @code (message_id ^ id) & mask @endcode is zero for any
  173.  * of the active filters.
  174.  *
  175.  * @param filter
  176.  *      Filter slot to activate (filter < STDCAN_NUM_FILTERS).
  177.  * @param id
  178.  *      Match if id matches the message id in all bit locations that are
  179.  *      not masked.
  180.  * @param mask
  181.  *      Each bit specifies if the corresponding id bit must match (mask[n] = 1)
  182.  *      or is Don't Care (mask[n] = 0).
  183.  */
  184. StdCan_Ret_t StdCan_EnableFilter(unsigned char filter, unsigned long id, unsigned long mask);
  185.  
  186. /**
  187.  * @brief Disable a message acceptance filter.
  188.  *
  189.  * Disables a filter (marks it at not matching any messages).
  190.  *
  191.  * @param filter
  192.  *      Filter slot to disable (filter < STDCAN_NUM_FILTERS).
  193.  */
  194. StdCan_Ret_t StdCan_DisableFilter(unsigned char filter);
  195.  
  196. /**@}*/
  197.  
  198. #endif /*STDCAN_H_*/
  199.