CO_NMT_Heartbeat.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /**
  2. * CANopen Network management and Heartbeat producer protocol.
  3. *
  4. * @file CO_NMT_Heartbeat.h
  5. * @ingroup CO_NMT_Heartbeat
  6. * @author Janez Paternoster
  7. * @copyright 2004 - 2020 Janez Paternoster
  8. *
  9. * This file is part of CANopenNode, an opensource CANopen Stack.
  10. * Project home page is <https://github.com/CANopenNode/CANopenNode>.
  11. * For more information on CANopen see <http://www.can-cia.org/>.
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License");
  14. * you may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. */
  25. #ifndef CO_NMT_HEARTBEAT_H
  26. #define CO_NMT_HEARTBEAT_H
  27. #include "301/CO_driver.h"
  28. #include "301/CO_SDOserver.h"
  29. #include "301/CO_Emergency.h"
  30. /* default configuration, see CO_config.h */
  31. #ifndef CO_CONFIG_NMT
  32. #define CO_CONFIG_NMT (0)
  33. #endif
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. /**
  38. * @defgroup CO_NMT_Heartbeat NMT and Heartbeat
  39. * @ingroup CO_CANopen_301
  40. * @{
  41. *
  42. * CANopen Network management and Heartbeat producer protocol.
  43. *
  44. * CANopen device can be in one of the #CO_NMT_internalState_t
  45. * - Initializing. It is active before CANopen is initialized.
  46. * - Pre-operational. All CANopen objects are active, except PDOs.
  47. * - Operational. Process data objects (PDOs) are active too.
  48. * - Stopped. Only Heartbeat producer and NMT consumer are active.
  49. *
  50. * NMT master can change the internal state of the devices by sending
  51. * #CO_NMT_command_t.
  52. *
  53. * ###NMT message contents:
  54. *
  55. * Byte | Description
  56. * -----|-----------------------------------------------------------
  57. * 0 | #CO_NMT_command_t
  58. * 1 | Node ID. If zero, command addresses all nodes.
  59. *
  60. * ###Heartbeat message contents:
  61. *
  62. * Byte | Description
  63. * -----|-----------------------------------------------------------
  64. * 0 | #CO_NMT_internalState_t
  65. *
  66. * @see #CO_Default_CAN_ID_t
  67. */
  68. /**
  69. * Internal network state of the CANopen node
  70. */
  71. typedef enum{
  72. CO_NMT_UNKNOWN = -1, /**< Device state is unknown (for heartbeat consumer) */
  73. CO_NMT_INITIALIZING = 0, /**< Device is initializing */
  74. CO_NMT_PRE_OPERATIONAL = 127, /**< Device is in pre-operational state */
  75. CO_NMT_OPERATIONAL = 5, /**< Device is in operational state */
  76. CO_NMT_STOPPED = 4 /**< Device is stopped */
  77. }CO_NMT_internalState_t;
  78. /**
  79. * Commands from NMT master.
  80. */
  81. typedef enum{
  82. CO_NMT_ENTER_OPERATIONAL = 1, /**< Start device */
  83. CO_NMT_ENTER_STOPPED = 2, /**< Stop device */
  84. CO_NMT_ENTER_PRE_OPERATIONAL = 128, /**< Put device into pre-operational */
  85. CO_NMT_RESET_NODE = 129, /**< Reset device */
  86. CO_NMT_RESET_COMMUNICATION = 130 /**< Reset CANopen communication on device */
  87. }CO_NMT_command_t;
  88. /**
  89. * Return code for CO_NMT_process() that tells application code what to
  90. * reset.
  91. */
  92. typedef enum{
  93. CO_RESET_NOT = 0,/**< Normal return, no action */
  94. CO_RESET_COMM = 1,/**< Application must provide communication reset. */
  95. CO_RESET_APP = 2,/**< Application must provide complete device reset */
  96. CO_RESET_QUIT = 3 /**< Application must quit, no reset of microcontroller (command is not requested by the stack.) */
  97. }CO_NMT_reset_cmd_t;
  98. /**
  99. * NMT consumer and Heartbeat producer object, initialized by CO_NMT_init()
  100. */
  101. typedef struct{
  102. CO_NMT_internalState_t operatingState; /**< Current NMT operating state. */
  103. CO_NMT_internalState_t operatingStatePrev; /**< Previous NMT operating state. */
  104. uint8_t resetCommand; /**< If different than zero, device will reset */
  105. uint8_t nodeId; /**< CANopen Node ID of this device */
  106. uint32_t HBproducerTimer;/**< Internal timer for HB producer */
  107. uint32_t firstHBTime; /**< From CO_NMT_init() */
  108. CO_EMpr_t *emPr; /**< From CO_NMT_init() */
  109. #if ((CO_CONFIG_NMT) & CO_CONFIG_NMT_MASTER) || defined CO_DOXYGEN
  110. CO_CANmodule_t *NMT_CANdevTx; /**< From CO_NMT_init() */
  111. CO_CANtx_t *NMT_TXbuff; /**< CAN transmit buffer for NMT master message */
  112. #endif
  113. CO_CANmodule_t *HB_CANdevTx; /**< From CO_NMT_init() */
  114. CO_CANtx_t *HB_TXbuff; /**< CAN transmit buffer for heartbeat message */
  115. #if ((CO_CONFIG_NMT) & CO_CONFIG_FLAG_CALLBACK_PRE) || defined CO_DOXYGEN
  116. /** From CO_NMT_initCallbackPre() or NULL */
  117. void (*pFunctSignalPre)(void *object);
  118. /** From CO_NMT_initCallbackPre() or NULL */
  119. void *functSignalObjectPre;
  120. #endif
  121. #if ((CO_CONFIG_NMT) & CO_CONFIG_NMT_CALLBACK_CHANGE) || defined CO_DOXYGEN
  122. void (*pFunctNMT)(CO_NMT_internalState_t state); /**< From CO_NMT_initCallbackChanged() or NULL */
  123. #endif
  124. }CO_NMT_t;
  125. /**
  126. * Initialize NMT and Heartbeat producer object.
  127. *
  128. * Function must be called in the communication reset section.
  129. *
  130. * @param NMT This object will be initialized.
  131. * @param emPr Emergency main object.
  132. * @param nodeId CANopen Node ID of this device.
  133. * @param firstHBTime_ms Time between bootup and first heartbeat message in milliseconds.
  134. * If firstHBTime is greater than _Producer Heartbeat time_
  135. * (object dictionary, index 0x1017), latter is used instead.
  136. * @param NMT_CANdevRx CAN device for NMT reception.
  137. * @param NMT_rxIdx Index of receive buffer in above CAN device.
  138. * @param CANidRxNMT CAN identifier for NMT receive message.
  139. * @param NMT_CANdevTx CAN device for NMT master transmission.
  140. * @param NMT_txIdx Index of transmit buffer in above CAN device.
  141. * @param CANidTxNMT CAN identifier for NMT transmit message.
  142. * @param HB_CANdevTx CAN device for HB transmission.
  143. * @param HB_txIdx Index of transmit buffer in the above CAN device.
  144. * @param CANidTxHB CAN identifier for HB message.
  145. *
  146. * @return #CO_ReturnError_t CO_ERROR_NO or CO_ERROR_ILLEGAL_ARGUMENT.
  147. */
  148. CO_ReturnError_t CO_NMT_init(
  149. CO_NMT_t *NMT,
  150. CO_EMpr_t *emPr,
  151. uint8_t nodeId,
  152. uint16_t firstHBTime_ms,
  153. CO_CANmodule_t *NMT_CANdevRx,
  154. uint16_t NMT_rxIdx,
  155. uint16_t CANidRxNMT,
  156. CO_CANmodule_t *NMT_CANdevTx,
  157. uint16_t NMT_txIdx,
  158. uint16_t CANidTxNMT,
  159. CO_CANmodule_t *HB_CANdevTx,
  160. uint16_t HB_txIdx,
  161. uint16_t CANidTxHB);
  162. #if ((CO_CONFIG_NMT) & CO_CONFIG_FLAG_CALLBACK_PRE) || defined CO_DOXYGEN
  163. /**
  164. * Initialize NMT callback function after message preprocessed.
  165. *
  166. * Function initializes optional callback function, which should immediately
  167. * start processing of CO_NMT_process() function.
  168. * Callback is called after NMT message is received from the CAN bus.
  169. *
  170. * @param NMT This object.
  171. * @param object Pointer to object, which will be passed to pFunctSignal(). Can be NULL
  172. * @param pFunctSignal Pointer to the callback function. Not called if NULL.
  173. */
  174. void CO_NMT_initCallbackPre(
  175. CO_NMT_t *NMT,
  176. void *object,
  177. void (*pFunctSignal)(void *object));
  178. #endif
  179. #if ((CO_CONFIG_NMT) & CO_CONFIG_NMT_CALLBACK_CHANGE) || defined CO_DOXYGEN
  180. /**
  181. * Initialize NMT callback function.
  182. *
  183. * Function initializes optional callback function, which is called after
  184. * NMT State change has occured. Function may wake up external task which
  185. * handles NMT events.
  186. * The first call is made immediately to give the consumer the current NMT state.
  187. *
  188. * @remark Be aware that the callback function is run inside the CAN receive
  189. * function context. Depending on the driver, this might be inside an interrupt!
  190. *
  191. * @param NMT This object.
  192. * @param pFunctNMT Pointer to the callback function. Not called if NULL.
  193. */
  194. void CO_NMT_initCallbackChanged(
  195. CO_NMT_t *NMT,
  196. void (*pFunctNMT)(CO_NMT_internalState_t state));
  197. #endif
  198. /**
  199. * Process received NMT and produce Heartbeat messages.
  200. *
  201. * Function must be called cyclically.
  202. *
  203. * @param NMT This object.
  204. * @param timeDifference_us Time difference from previous function call in [microseconds].
  205. * @param HBtime_ms _Producer Heartbeat time_ (object dictionary, index 0x1017).
  206. * @param NMTstartup _NMT startup behavior_ (object dictionary, index 0x1F80).
  207. * @param errorRegister _Error register_ (object dictionary, index 0x1001).
  208. * @param errorBehavior pointer to _Error behavior_ array (object dictionary, index 0x1029).
  209. * Object controls, if device should leave NMT operational state.
  210. * Length of array must be 6. If pointer is NULL, no calculation is made.
  211. * @param [out] timerNext_us info to OS - see CO_process().
  212. *
  213. * @return #CO_NMT_reset_cmd_t
  214. */
  215. CO_NMT_reset_cmd_t CO_NMT_process(
  216. CO_NMT_t *NMT,
  217. uint32_t timeDifference_us,
  218. uint16_t HBtime_ms,
  219. uint32_t NMTstartup,
  220. uint8_t errorRegister,
  221. const uint8_t errorBehavior[],
  222. uint32_t *timerNext_us);
  223. /**
  224. * Query current NMT state
  225. *
  226. * @param NMT This object.
  227. *
  228. * @return @ref CO_NMT_internalState_t
  229. */
  230. static inline CO_NMT_internalState_t CO_NMT_getInternalState(CO_NMT_t *NMT) {
  231. return (NMT == NULL) ? CO_NMT_INITIALIZING : NMT->operatingState;
  232. }
  233. /**
  234. * Set internal NMT state
  235. *
  236. * Functions sets state directly, without any checking. @ref CO_NMT_process()
  237. * may also switch between states automatically, see @ref CO_NMT_control_t.
  238. *
  239. * @param NMT This object.
  240. * @param state New state.
  241. */
  242. static inline void CO_NMT_setInternalState(CO_NMT_t *NMT,
  243. CO_NMT_internalState_t state)
  244. {
  245. if (NMT != NULL) NMT->operatingState = state;
  246. }
  247. #if ((CO_CONFIG_NMT) & CO_CONFIG_NMT_MASTER) || defined CO_DOXYGEN
  248. /**
  249. * Send NMT master command.
  250. *
  251. * This functionality can only be used from NMT master. There is one exception
  252. * where application from slave node may send NMT master command: If CANopen
  253. * object 0x1F80 has value of **0x2**, then NMT slave shall execute the NMT
  254. * service start remote node (CO_NMT_ENTER_OPERATIONAL) with nodeID set to 0.
  255. *
  256. * @param NMT This object.
  257. * @param command NMT command from CO_NMT_command_t.
  258. * @param nodeID Node ID of the remote node. 0 for all nodes including self.
  259. *
  260. * @return 0: Operation completed successfully.
  261. * @return other: same as CO_CANsend().
  262. */
  263. CO_ReturnError_t CO_NMT_sendCommand(CO_NMT_t *NMT,
  264. CO_NMT_command_t command,
  265. uint8_t nodeID);
  266. #endif /* (CO_CONFIG_NMT) & CO_CONFIG_NMT_MASTER */
  267. /** @} */ /* CO_NMT_Heartbeat */
  268. #ifdef __cplusplus
  269. }
  270. #endif /*__cplusplus*/
  271. #endif /* CO_NMT_HEARTBEAT_H */