CANopen.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /**
  2. * Main CANopenNode file.
  3. *
  4. * @file CANopen.h
  5. * @ingroup CO_CANopen
  6. * @author Janez Paternoster
  7. * @author Uwe Kindler
  8. * @copyright 2010 - 2020 Janez Paternoster
  9. *
  10. * This file is part of CANopenNode, an opensource CANopen Stack.
  11. * Project home page is <https://github.com/CANopenNode/CANopenNode>.
  12. * For more information on CANopen see <http://www.can-cia.org/>.
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the "License");
  15. * you may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * http://www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */
  26. #ifndef CANopen_H
  27. #define CANopen_H
  28. #include "301/CO_driver.h"
  29. #include "301/CO_SDOserver.h"
  30. #include "301/CO_SDOclient.h"
  31. #include "301/CO_Emergency.h"
  32. #include "301/CO_NMT_Heartbeat.h"
  33. #include "301/CO_TIME.h"
  34. #include "301/CO_SYNC.h"
  35. #include "301/CO_PDO.h"
  36. #include "301/CO_HBconsumer.h"
  37. #include "303/CO_LEDs.h"
  38. #include "304/CO_GFC.h"
  39. #include "304/CO_SRDO.h"
  40. #include "305/CO_LSSslave.h"
  41. #include "305/CO_LSSmaster.h"
  42. #include "309/CO_gateway_ascii.h"
  43. #include "extra/CO_trace.h"
  44. #include "CO_OD.h"
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /**
  49. * @defgroup CO_CANopen CANopen
  50. * @{
  51. *
  52. * CANopenNode is free and open source implementation of CANopen communication
  53. * protocol.
  54. *
  55. * CANopen is the internationally standardized (EN 50325-4) (CiA DS-301)
  56. * CAN-based higher-layer protocol for embedded control system. For more
  57. * information on CANopen see http://www.can-cia.org/
  58. *
  59. * CANopenNode homepage is https://github.com/CANopenNode/CANopenNode
  60. *
  61. * CANopen.h file combines Object dictionary (CO_OD) and all other CANopen
  62. * source files. Configuration information are read from CO_OD.h file.
  63. * CO_OD.h/.c files defines CANopen Object Dictionary and are generated by
  64. * external tool.
  65. * CANopen.h file contains most common configuration of CANopenNode objects
  66. * and can also be a template for custom, more complex configurations.
  67. *
  68. * Licensed under the Apache License, Version 2.0 (the "License");
  69. * you may not use this file except in compliance with the License.
  70. * You may obtain a copy of the License at
  71. *
  72. * https://www.apache.org/licenses/LICENSE-2.0
  73. *
  74. * Unless required by applicable law or agreed to in writing, software
  75. * distributed under the License is distributed on an "AS IS" BASIS,
  76. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  77. * See the License for the specific language governing permissions and
  78. * limitations under the License.
  79. * @}
  80. */
  81. /**
  82. * @defgroup CO_CANopen_301 CANopen_301
  83. * @{
  84. *
  85. * CANopen application layer and communication profile (CiA 301 v4.2.0)
  86. *
  87. * Definitions of data types, encoding rules, object dictionary objects and
  88. * CANopen communication services and protocols.
  89. * @}
  90. */
  91. /**
  92. * @defgroup CO_CANopen_303 CANopen_303
  93. * @{
  94. *
  95. * CANopen recommendation for indicator specification (CiA 303-3 v1.4.0)
  96. *
  97. * Description of communication related indicators - green and red LED diodes.
  98. * @}
  99. */
  100. /**
  101. * @defgroup CO_CANopen_304 CANopen_304
  102. * @{
  103. *
  104. * CANopen Safety (EN 50325­-5:2010 (formerly CiA 304))
  105. *
  106. * Standard defines the usage of Safety Related Data Objects (SRDO) and the GFC.
  107. * This is an additional protocol (to SDO, PDO) to exchange data.
  108. * The meaning of "security" here refers not to security (crypto) but to data consistency.
  109. * @}
  110. */
  111. /**
  112. * @defgroup CO_CANopen_305 CANopen_305
  113. * @{
  114. *
  115. * CANopen layer setting services (LSS) and protocols (CiA 305 DSP v3.0.0)
  116. *
  117. * Inquire or change three parameters on a CANopen device with LSS slave
  118. * capability by a CANopen device with LSS master capability via the CAN
  119. * network: the settings of Node-ID of the CANopen device, bit timing
  120. * parameters of the physical layer (bit rate) or LSS address compliant to the
  121. * identity object (1018h).
  122. * @}
  123. */
  124. /**
  125. * @defgroup CO_CANopen_309 CANopen_309
  126. * @{
  127. *
  128. * CANopen access from other networks (CiA 309)
  129. *
  130. * Standard defines the services and protocols to interface CANopen networks to
  131. * other networks. Standard is organized as follows:
  132. * - Part 1: General principles and services
  133. * - Part 2: Modbus/TCP mapping
  134. * - Part 3: ASCII mapping
  135. * - Part 4: Amendment 7 to Fieldbus Integration into PROFINET IO
  136. * @}
  137. */
  138. /**
  139. * @defgroup CO_CANopen_extra CANopen_extra
  140. * @{
  141. *
  142. * Additional non-standard objects related to CANopenNode.
  143. * @}
  144. */
  145. /**
  146. * @addtogroup CO_CANopen
  147. * @{
  148. */
  149. #ifdef CO_DOXYGEN
  150. /**
  151. * @defgroup CO_NO_OBJ CANopen configuration
  152. *
  153. * Definitions specify, which and how many CANopenNode communication objects
  154. * will be used in current configuration. Usage of some objects is mandatory and
  155. * is fixed. Others are defined in CO_OD.h.
  156. * @{
  157. */
  158. /* Definitions valid only for documentation. */
  159. /** Number of NMT objects, fixed to 1 slave(CANrx) */
  160. #define CO_NO_NMT (1)
  161. /** Number of NMT master objects, 0 or 1 master(CANtx). It depends on
  162. * @ref CO_CONFIG_NMT setting. */
  163. #define CO_NO_NMT_MST (0 - 1)
  164. /** Number of SYNC objects, 0 or 1 (consumer(CANrx) + producer(CANtx)) */
  165. #define CO_NO_SYNC (0 - 1)
  166. /** Number of Emergency producer objects, fixed to 1 producer(CANtx) */
  167. #define CO_NO_EMERGENCY (1)
  168. /** Number of Emergency consumer objects, 0 or 1 consumer(CANrx). It depends on
  169. * @ref CO_CONFIG_EM setting. */
  170. #define CO_NO_EM_CONS (0 - 1)
  171. /** Number of Time-stamp objects, 0 or 1 (consumer(CANrx) + producer(CANtx)) */
  172. #define CO_NO_TIME (0 - 1)
  173. /** Number of GFC objects, 0 or 1 (consumer(CANrx) + producer(CANtx)) */
  174. #define CO_NO_GFC (0 - 1)
  175. /** Number of SRDO objects, 0 to 64 (consumer(CANrx) + producer(CANtx)) */
  176. #define CO_NO_RPDO (0 - 64)
  177. /** Number of RPDO objects, 1 to 512 consumers (CANrx) */
  178. #define CO_NO_RPDO (1 - 512)
  179. /** Number of TPDO objects, 1 to 512 producers (CANtx) */
  180. #define CO_NO_TPDO (1 - 512)
  181. /** Number of SDO server objects, from 1 to 128 (CANrx + CANtx) */
  182. #define CO_NO_SDO_SERVER (1 - 128)
  183. /** Number of SDO client objects, from 0 to 128 (CANrx + CANtx) */
  184. #define CO_NO_SDO_CLIENT (0 - 128)
  185. /** Number of HB producer objects, fixed to 1 producer(CANtx) */
  186. #define CO_NO_HB_PROD (1)
  187. /** Number of HB consumer objects, from 0 to 127 consumers(CANrx) */
  188. #define CO_NO_HB_CONS (0 - 127)
  189. /** Number of LSS slave objects, 0 or 1 (CANrx + CANtx). It depends on
  190. * @ref CO_CONFIG_LSS setting. */
  191. #define CO_NO_LSS_SLAVE (0 - 1)
  192. /** Number of LSS master objects, 0 or 1 (CANrx + CANtx). It depends on
  193. * @ref CO_CONFIG_LSS setting. */
  194. #define CO_NO_LSS_MASTER (0 - 1)
  195. /** Number of Trace objects, 0 to many */
  196. #define CO_NO_TRACE (0 - )
  197. /** @} */
  198. #else /* CO_DOXYGEN */
  199. /* Valid Definitions for program. */
  200. /* NMT slave count (fixed) */
  201. #define CO_NO_NMT 1
  202. /* NMT master count depends on stack configuration */
  203. #if (CO_CONFIG_NMT) & CO_CONFIG_NMT_MASTER
  204. #define CO_NO_NMT_MST 1
  205. #else
  206. #define CO_NO_NMT_MST 0
  207. #endif
  208. /* Emergency consumer depends on stack configuration */
  209. #if (CO_CONFIG_EM) & CO_CONFIG_EM_CONSUMER
  210. #define CO_NO_EM_CONS 1
  211. #else
  212. #define CO_NO_EM_CONS 0
  213. #endif
  214. /* Heartbeat producer count (fixed) */
  215. #define CO_NO_HB_PROD 1
  216. /* Heartbeat consumer count depends on Object Dictionary configuration */
  217. #ifdef ODL_consumerHeartbeatTime_arrayLength
  218. #define CO_NO_HB_CONS ODL_consumerHeartbeatTime_arrayLength
  219. #else
  220. #define CO_NO_HB_CONS 0
  221. #endif
  222. /* LSS slave count depends on stack configuration */
  223. #if (CO_CONFIG_LSS) & CO_CONFIG_LSS_SLAVE
  224. #define CO_NO_LSS_SLAVE 1
  225. #else
  226. #define CO_NO_LSS_SLAVE 0
  227. #endif
  228. /* LSS master count depends on stack configuration */
  229. #if (CO_CONFIG_LSS) & CO_CONFIG_LSS_MASTER
  230. #define CO_NO_LSS_MASTER 1
  231. #else
  232. #define CO_NO_LSS_MASTER 0
  233. #endif
  234. #endif /* CO_DOXYGEN */
  235. /**
  236. * CANopen object with pointers to all CANopenNode objects.
  237. */
  238. typedef struct {
  239. bool_t nodeIdUnconfigured; /**< True in unconfigured LSS slave */
  240. CO_CANmodule_t *CANmodule[1]; /**< CAN module objects */
  241. CO_SDO_t *SDO[CO_NO_SDO_SERVER]; /**< SDO object */
  242. CO_EM_t *em; /**< Emergency report object */
  243. CO_EMpr_t *emPr; /**< Emergency process object */
  244. CO_NMT_t *NMT; /**< NMT object */
  245. #if CO_NO_SYNC == 1 || defined CO_DOXYGEN
  246. CO_SYNC_t *SYNC; /**< SYNC object */
  247. #endif
  248. #if CO_NO_TIME == 1 || defined CO_DOXYGEN
  249. CO_TIME_t *TIME; /**< TIME object */
  250. #endif
  251. CO_RPDO_t *RPDO[CO_NO_RPDO]; /**< RPDO objects */
  252. CO_TPDO_t *TPDO[CO_NO_TPDO]; /**< TPDO objects */
  253. CO_HBconsumer_t *HBcons; /**< Heartbeat consumer object*/
  254. #if CO_NO_SDO_CLIENT != 0 || defined CO_DOXYGEN
  255. CO_SDOclient_t *SDOclient[CO_NO_SDO_CLIENT]; /**< SDO client object */
  256. #endif
  257. #if ((CO_CONFIG_LEDS) & CO_CONFIG_LEDS_ENABLE) || defined CO_DOXYGEN
  258. CO_LEDs_t *LEDs; /**< LEDs object */
  259. #endif
  260. #if CO_NO_GFC != 0 || defined CO_DOXYGEN
  261. CO_GFC_t *GFC; /**< GFC objects */
  262. #endif
  263. #if CO_NO_SRDO != 0 || defined CO_DOXYGEN
  264. CO_SRDOGuard_t *SRDOGuard; /**< SRDO objects */
  265. CO_SRDO_t *SRDO[CO_NO_SRDO]; /**< SRDO objects */
  266. #endif
  267. #if CO_NO_LSS_SLAVE == 1 || defined CO_DOXYGEN
  268. CO_LSSslave_t *LSSslave; /**< LSS slave object */
  269. #endif
  270. #if CO_NO_LSS_MASTER == 1 || defined CO_DOXYGEN
  271. CO_LSSmaster_t *LSSmaster; /**< LSS master object */
  272. #endif
  273. #if ((CO_CONFIG_GTW) & CO_CONFIG_GTW_ASCII) || defined CO_DOXYGEN
  274. CO_GTWA_t *gtwa; /**< Gateway-ascii object (CiA309-3) */
  275. #endif
  276. #if CO_NO_TRACE > 0 || defined CO_DOXYGEN
  277. CO_trace_t *trace[CO_NO_TRACE]; /**< Trace object for recording variables */
  278. #endif
  279. } CO_t;
  280. /** CANopen object */
  281. extern CO_t *CO;
  282. /**
  283. * Allocate and initialize memory for CANopen object
  284. *
  285. * Function must be called the first time, after program starts.
  286. *
  287. * @remark
  288. * With some microcontrollers it is necessary to specify Heap size within
  289. * linker configuration.
  290. *
  291. * @param [out] heapMemoryUsed Information about heap memory used. Ignored if
  292. * NULL.
  293. *
  294. * @return #CO_ReturnError_t: CO_ERROR_NO, CO_ERROR_ILLEGAL_ARGUMENT,
  295. * CO_ERROR_OUT_OF_MEMORY
  296. */
  297. CO_ReturnError_t CO_new(uint32_t *heapMemoryUsed);
  298. /**
  299. * Delete CANopen object and free memory. Must be called at program exit.
  300. *
  301. * @param CANptr Pointer to the user-defined CAN base structure, passed to
  302. * CO_CANmodule_init().
  303. */
  304. void CO_delete(void *CANptr);
  305. /**
  306. * Initialize CAN driver
  307. *
  308. * Function must be called in the communication reset section.
  309. *
  310. * @param CANptr Pointer to the user-defined CAN base structure, passed to
  311. * CO_CANmodule_init().
  312. * @param bitRate CAN bit rate.
  313. * @return #CO_ReturnError_t: CO_ERROR_NO, CO_ERROR_ILLEGAL_ARGUMENT,
  314. * CO_ERROR_ILLEGAL_BAUDRATE, CO_ERROR_OUT_OF_MEMORY
  315. */
  316. CO_ReturnError_t CO_CANinit(void *CANptr,
  317. uint32_t bitRate);
  318. #if CO_NO_LSS_SLAVE == 1 || defined CO_DOXYGEN
  319. /**
  320. * Initialize CANopen LSS slave
  321. *
  322. * Function must be called before CO_CANopenInit.
  323. *
  324. * See #CO_LSSslave_init() for description of parameters.
  325. *
  326. * @param [in,out] pendingNodeID Pending node ID or 0xFF(unconfigured)
  327. * @param [in,out] pendingBitRate Pending bit rate of the CAN interface
  328. * @return #CO_ReturnError_t: CO_ERROR_NO, CO_ERROR_ILLEGAL_ARGUMENT
  329. */
  330. CO_ReturnError_t CO_LSSinit(uint8_t *pendingNodeID,
  331. uint16_t *pendingBitRate);
  332. #endif /* CO_NO_LSS_SLAVE == 1 */
  333. /**
  334. * Initialize CANopenNode.
  335. *
  336. * Function must be called in the communication reset section.
  337. *
  338. * @param nodeId CANopen Node ID (1 ... 127) or 0xFF(unconfigured). In the
  339. * CANopen initialization it is the same as pendingBitRate from CO_LSSinit().
  340. * If it is unconfigured, then some CANopen objects will not be initialized nor
  341. * processed.
  342. * @return #CO_ReturnError_t: CO_ERROR_NO, CO_ERROR_ILLEGAL_ARGUMENT
  343. */
  344. CO_ReturnError_t CO_CANopenInit(uint8_t nodeId);
  345. /**
  346. * Process CANopen objects.
  347. *
  348. * Function must be called cyclically. It processes all "asynchronous" CANopen
  349. * objects.
  350. *
  351. * @param co CANopen object.
  352. * @param timeDifference_us Time difference from previous function call in
  353. * microseconds.
  354. * @param [out] timerNext_us info to OS - maximum delay time after this function
  355. * should be called next time in [microseconds]. Value can be used for OS
  356. * sleep time. Initial value must be set to maximum interval time.
  357. * Output will be equal or lower to initial value. Calculation is based
  358. * on various timers which expire in known time. Parameter should be
  359. * used in combination with callbacks configured with
  360. * CO_***_initCallbackPre() functions. Those callbacks should also
  361. * trigger calling of CO_process() function. Parameter is ignored if
  362. * NULL. See also @ref CO_CONFIG_FLAG_CALLBACK_PRE configuration macro.
  363. *
  364. * @return #CO_NMT_reset_cmd_t from CO_NMT_process().
  365. */
  366. CO_NMT_reset_cmd_t CO_process(CO_t *co,
  367. uint32_t timeDifference_us,
  368. uint32_t *timerNext_us);
  369. #if CO_NO_SYNC == 1 || defined CO_DOXYGEN
  370. /**
  371. * Process CANopen SYNC objects.
  372. *
  373. * Function must be called cyclically from real time thread with constant
  374. * interval (1ms typically). It processes SYNC CANopen objects.
  375. *
  376. * @param co CANopen object.
  377. * @param timeDifference_us Time difference from previous function call in
  378. * microseconds.
  379. * @param [out] timerNext_us info to OS - see CO_process().
  380. *
  381. * @return True, if CANopen SYNC message was just received or transmitted.
  382. */
  383. bool_t CO_process_SYNC(CO_t *co,
  384. uint32_t timeDifference_us,
  385. uint32_t *timerNext_us);
  386. #endif /* CO_NO_SYNC == 1 */
  387. /**
  388. * Process CANopen RPDO objects.
  389. *
  390. * Function must be called cyclically from real time thread with constant.
  391. * interval (1ms typically). It processes receive PDO CANopen objects.
  392. *
  393. * @param co CANopen object.
  394. * @param syncWas True, if CANopen SYNC message was just received or
  395. * transmitted.
  396. */
  397. void CO_process_RPDO(CO_t *co, bool_t syncWas);
  398. /**
  399. * Process CANopen TPDO objects.
  400. *
  401. * Function must be called cyclically from real time thread with constant.
  402. * interval (1ms typically). It processes transmit PDO CANopen objects.
  403. *
  404. * @param co CANopen object.
  405. * @param syncWas True, if CANopen SYNC message was just received or
  406. * transmitted.
  407. * @param timeDifference_us Time difference from previous function call in
  408. * microseconds.
  409. * @param [out] timerNext_us info to OS - see CO_process().
  410. */
  411. void CO_process_TPDO(CO_t *co,
  412. bool_t syncWas,
  413. uint32_t timeDifference_us,
  414. uint32_t *timerNext_us);
  415. #if CO_NO_SRDO != 0 || defined CO_DOXYGEN
  416. /**
  417. * Process CANopen SRDO objects.
  418. *
  419. * Function must be called cyclically from real time thread with constant.
  420. * interval (1ms typically). It processes receive SRDO CANopen objects.
  421. *
  422. * @param co CANopen object.
  423. * @param timeDifference_us Time difference from previous function call in
  424. * microseconds.
  425. * @param [out] timerNext_us info to OS - see CO_process().
  426. */
  427. void CO_process_SRDO(CO_t *co,
  428. uint32_t timeDifference_us,
  429. uint32_t *timerNext_us);
  430. #endif /* CO_NO_SRDO != 0 */
  431. /** @} */ /* CO_CANopen */
  432. #ifdef __cplusplus
  433. }
  434. #endif /* __cplusplus */
  435. #endif /* CANopen_H */