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