CO_Emergency.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /**
  2. * CANopen Emergency protocol.
  3. *
  4. * @file CO_Emergency.h
  5. * @ingroup CO_Emergency
  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_EMERGENCY_H
  26. #define CO_EMERGENCY_H
  27. #include "301/CO_driver.h"
  28. /* default configuration, see CO_config.h */
  29. #ifndef CO_CONFIG_EM
  30. #define CO_CONFIG_EM (CO_CONFIG_EM_PRODUCER)
  31. #endif
  32. #ifndef CO_CONFIG_EM_ERR_STATUS_BITS_COUNT
  33. #define CO_CONFIG_EM_ERR_STATUS_BITS_COUNT (10*8)
  34. #endif
  35. #ifndef CO_CONFIG_ERR_CONDITION_GENERIC
  36. #define CO_CONFIG_ERR_CONDITION_GENERIC (em->errorStatusBits[5] != 0)
  37. #endif
  38. #ifndef CO_CONFIG_ERR_CONDITION_COMMUNICATION
  39. #define CO_CONFIG_ERR_CONDITION_COMMUNICATION (em->errorStatusBits[2] \
  40. || em->errorStatusBits[3])
  41. #endif
  42. #ifndef CO_CONFIG_ERR_CONDITION_MANUFACTURER
  43. #define CO_CONFIG_ERR_CONDITION_MANUFACTURER (em->errorStatusBits[8] \
  44. || em->errorStatusBits[9])
  45. #endif
  46. #ifdef __cplusplus
  47. extern "C" {
  48. #endif
  49. /**
  50. * @defgroup CO_Emergency Emergency
  51. * @ingroup CO_CANopen_301
  52. * @{
  53. *
  54. * CANopen Emergency protocol.
  55. *
  56. * Error control and Emergency is used for control internal error state
  57. * and for sending a CANopen Emergency message.
  58. *
  59. * In case of error condition stack or application calls CO_errorReport()
  60. * function with indication of the error. Specific error condition is reported
  61. * (with CANopen Emergency message) only the first time after it occurs.
  62. * Internal state of the error condition is controlled with
  63. * @ref CO_EM_errorStatusBits. Specific error condition can also be reset by
  64. * CO_errorReset() function. If so, Emergency message is sent with
  65. * CO_EM_NO_ERROR indication.
  66. *
  67. * Some error conditions are informative and some are critical. Critical error
  68. * conditions sets the #CO_errorRegisterBitmask_t.
  69. *
  70. * Latest errors can be read from _Pre Defined Error Field_ (object dictionary,
  71. * index 0x1003). @ref CO_EM_errorStatusBits can also be read form CANopen
  72. * object dictionary.
  73. *
  74. * ###Emergency message contents:
  75. *
  76. * Byte | Description
  77. * -----|-----------------------------------------------------------
  78. * 0..1 | @ref CO_EM_errorCodes.
  79. * 2 | #CO_errorRegisterBitmask_t.
  80. * 3 | Index of error condition (see @ref CO_EM_errorStatusBits).
  81. * 4..7 | Additional argument informative to CO_errorReport() function.
  82. *
  83. * ####Contents of _Pre Defined Error Field_ (object dictionary, index 0x1003):
  84. * bytes 0..3 are equal to bytes 0..3 in the Emergency message.
  85. *
  86. * @see #CO_Default_CAN_ID_t
  87. */
  88. /**
  89. * CANopen Error register.
  90. *
  91. * In object dictionary on index 0x1001.
  92. *
  93. * Error register is calculated from critical internal @ref CO_EM_errorStatusBits.
  94. * Generic and communication bits are calculated in CO_EM_process
  95. * function, device profile or manufacturer specific bits may be calculated
  96. * inside the application.
  97. *
  98. * Internal errors may prevent device to stay in NMT Operational state. Details
  99. * are described in _Error Behavior_ object in Object Dictionary at index 0x1029.
  100. */
  101. typedef enum{
  102. CO_ERR_REG_GENERIC_ERR = 0x01U, /**< bit 0, generic error */
  103. CO_ERR_REG_CURRENT = 0x02U, /**< bit 1, current */
  104. CO_ERR_REG_VOLTAGE = 0x04U, /**< bit 2, voltage */
  105. CO_ERR_REG_TEMPERATURE = 0x08U, /**< bit 3, temperature */
  106. CO_ERR_REG_COMM_ERR = 0x10U, /**< bit 4, communication error (overrun, error state) */
  107. CO_ERR_REG_DEV_PROFILE = 0x20U, /**< bit 5, device profile specific */
  108. CO_ERR_REG_RESERVED = 0x40U, /**< bit 6, reserved (always 0) */
  109. CO_ERR_REG_MANUFACTURER = 0x80U /**< bit 7, manufacturer specific */
  110. }CO_errorRegisterBitmask_t;
  111. /**
  112. * @defgroup CO_EM_errorCodes CANopen Error codes
  113. * @{
  114. *
  115. * Standard error codes according to CiA DS-301 and DS-401.
  116. */
  117. #define CO_EMC_NO_ERROR 0x0000U /**< 0x00xx, error Reset or No Error */
  118. #define CO_EMC_GENERIC 0x1000U /**< 0x10xx, Generic Error */
  119. #define CO_EMC_CURRENT 0x2000U /**< 0x20xx, Current */
  120. #define CO_EMC_CURRENT_INPUT 0x2100U /**< 0x21xx, Current, device input side */
  121. #define CO_EMC_CURRENT_INSIDE 0x2200U /**< 0x22xx, Current inside the device */
  122. #define CO_EMC_CURRENT_OUTPUT 0x2300U /**< 0x23xx, Current, device output side */
  123. #define CO_EMC_VOLTAGE 0x3000U /**< 0x30xx, Voltage */
  124. #define CO_EMC_VOLTAGE_MAINS 0x3100U /**< 0x31xx, Mains Voltage */
  125. #define CO_EMC_VOLTAGE_INSIDE 0x3200U /**< 0x32xx, Voltage inside the device */
  126. #define CO_EMC_VOLTAGE_OUTPUT 0x3300U /**< 0x33xx, Output Voltage */
  127. #define CO_EMC_TEMPERATURE 0x4000U /**< 0x40xx, Temperature */
  128. #define CO_EMC_TEMP_AMBIENT 0x4100U /**< 0x41xx, Ambient Temperature */
  129. #define CO_EMC_TEMP_DEVICE 0x4200U /**< 0x42xx, Device Temperature */
  130. #define CO_EMC_HARDWARE 0x5000U /**< 0x50xx, Device Hardware */
  131. #define CO_EMC_SOFTWARE_DEVICE 0x6000U /**< 0x60xx, Device Software */
  132. #define CO_EMC_SOFTWARE_INTERNAL 0x6100U /**< 0x61xx, Internal Software */
  133. #define CO_EMC_SOFTWARE_USER 0x6200U /**< 0x62xx, User Software */
  134. #define CO_EMC_DATA_SET 0x6300U /**< 0x63xx, Data Set */
  135. #define CO_EMC_ADDITIONAL_MODUL 0x7000U /**< 0x70xx, Additional Modules */
  136. #define CO_EMC_MONITORING 0x8000U /**< 0x80xx, Monitoring */
  137. #define CO_EMC_COMMUNICATION 0x8100U /**< 0x81xx, Communication */
  138. #define CO_EMC_CAN_OVERRUN 0x8110U /**< 0x8110, CAN Overrun (Objects lost) */
  139. #define CO_EMC_CAN_PASSIVE 0x8120U /**< 0x8120, CAN in Error Passive Mode */
  140. #define CO_EMC_HEARTBEAT 0x8130U /**< 0x8130, Life Guard Error or Heartbeat Error */
  141. #define CO_EMC_BUS_OFF_RECOVERED 0x8140U /**< 0x8140, recovered from bus off */
  142. #define CO_EMC_CAN_ID_COLLISION 0x8150U /**< 0x8150, CAN-ID collision */
  143. #define CO_EMC_PROTOCOL_ERROR 0x8200U /**< 0x82xx, Protocol Error */
  144. #define CO_EMC_PDO_LENGTH 0x8210U /**< 0x8210, PDO not processed due to length error */
  145. #define CO_EMC_PDO_LENGTH_EXC 0x8220U /**< 0x8220, PDO length exceeded */
  146. #define CO_EMC_DAM_MPDO 0x8230U /**< 0x8230, DAM MPDO not processed, destination object not available */
  147. #define CO_EMC_SYNC_DATA_LENGTH 0x8240U /**< 0x8240, Unexpected SYNC data length */
  148. #define CO_EMC_RPDO_TIMEOUT 0x8250U /**< 0x8250, RPDO timeout */
  149. #define CO_EMC_TIME_DATA_LENGTH 0x8260U /**< 0x8260, Unexpected TIME data length */
  150. #define CO_EMC_EXTERNAL_ERROR 0x9000U /**< 0x90xx, External Error */
  151. #define CO_EMC_ADDITIONAL_FUNC 0xF000U /**< 0xF0xx, Additional Functions */
  152. #define CO_EMC_DEVICE_SPECIFIC 0xFF00U /**< 0xFFxx, Device specific */
  153. #define CO_EMC401_OUT_CUR_HI 0x2310U /**< 0x2310, DS401, Current at outputs too high (overload) */
  154. #define CO_EMC401_OUT_SHORTED 0x2320U /**< 0x2320, DS401, Short circuit at outputs */
  155. #define CO_EMC401_OUT_LOAD_DUMP 0x2330U /**< 0x2330, DS401, Load dump at outputs */
  156. #define CO_EMC401_IN_VOLT_HI 0x3110U /**< 0x3110, DS401, Input voltage too high */
  157. #define CO_EMC401_IN_VOLT_LOW 0x3120U /**< 0x3120, DS401, Input voltage too low */
  158. #define CO_EMC401_INTERN_VOLT_HI 0x3210U /**< 0x3210, DS401, Internal voltage too high */
  159. #define CO_EMC401_INTERN_VOLT_LO 0x3220U /**< 0x3220, DS401, Internal voltage too low */
  160. #define CO_EMC401_OUT_VOLT_HIGH 0x3310U /**< 0x3310, DS401, Output voltage too high */
  161. #define CO_EMC401_OUT_VOLT_LOW 0x3320U /**< 0x3320, DS401, Output voltage too low */
  162. /** @} */
  163. /**
  164. * @defgroup CO_EM_errorStatusBits Error status bits
  165. * @{
  166. *
  167. * Internal indication of the error condition.
  168. *
  169. * Each error condition is specified by unique index from 0x00 up to 0xFF.
  170. * Variable (from manufacturer section in the Object
  171. * Dictionary) contains up to 0xFF bits (32bytes) for the identification of the
  172. * specific error condition. (Type of the variable is CANopen OCTET_STRING.)
  173. *
  174. * If specific error occurs in the stack or in the application, CO_errorReport()
  175. * sets specific bit in the _Error Status Bits_ variable. If bit was already
  176. * set, function returns without any action. Otherwise it prepares emergency
  177. * message.
  178. *
  179. * CO_errorReport(), CO_errorReset() or CO_isError() functions are called
  180. * with unique index as an argument. (However CO_errorReport(), for example, may
  181. * be used with the same index on multiple places in the code.)
  182. *
  183. * Macros defined below are combination of two constants: index and
  184. * @ref CO_EM_errorCodes. They represents specific error conditions. They are
  185. * used as double argument for CO_errorReport(), CO_errorReset() and
  186. * CO_isError() functions.
  187. *
  188. * Stack uses first 6 bytes of the _Error Status Bits_ variable. Device profile
  189. * or application may define own macros for Error status bits using
  190. * @ref CO_EM_MANUFACTURER_START and @ref CO_EM_MANUFACTURER_END values. Note that
  191. * _Error Status Bits_ must be large enough (up to 32 bytes).
  192. */
  193. #define CO_EM_NO_ERROR 0x00U /**< 0x00, Error Reset or No Error */
  194. #define CO_EM_CAN_BUS_WARNING 0x01U /**< 0x01, communication, info, CAN bus warning limit reached */
  195. #define CO_EM_RXMSG_WRONG_LENGTH 0x02U /**< 0x02, communication, info, Wrong data length of the received CAN message */
  196. #define CO_EM_RXMSG_OVERFLOW 0x03U /**< 0x03, communication, info, Previous received CAN message wasn't processed yet */
  197. #define CO_EM_RPDO_WRONG_LENGTH 0x04U /**< 0x04, communication, info, Wrong data length of received PDO */
  198. #define CO_EM_RPDO_OVERFLOW 0x05U /**< 0x05, communication, info, Previous received PDO wasn't processed yet */
  199. #define CO_EM_CAN_RX_BUS_PASSIVE 0x06U /**< 0x06, communication, info, CAN receive bus is passive */
  200. #define CO_EM_CAN_TX_BUS_PASSIVE 0x07U /**< 0x07, communication, info, CAN transmit bus is passive */
  201. #define CO_EM_NMT_WRONG_COMMAND 0x08U /**< 0x08, communication, info, Wrong NMT command received */
  202. #define CO_EM_TIME_TIMEOUT 0x09U /**< 0x09, communication, info, TIME message timeout */
  203. #define CO_EM_TIME_LENGTH 0x0AU /**< 0x0A, communication, info, Unexpected TIME data length */
  204. #define CO_EM_0B_unused 0x0BU /**< 0x0B, (unused) */
  205. #define CO_EM_0C_unused 0x0CU /**< 0x0C, (unused) */
  206. #define CO_EM_0D_unused 0x0DU /**< 0x0D, (unused) */
  207. #define CO_EM_0E_unused 0x0EU /**< 0x0E, (unused) */
  208. #define CO_EM_0F_unused 0x0FU /**< 0x0F, (unused) */
  209. #define CO_EM_10_unused 0x10U /**< 0x10, (unused) */
  210. #define CO_EM_11_unused 0x11U /**< 0x11, (unused) */
  211. #define CO_EM_CAN_TX_BUS_OFF 0x12U /**< 0x12, communication, critical, CAN transmit bus is off */
  212. #define CO_EM_CAN_RXB_OVERFLOW 0x13U /**< 0x13, communication, critical, CAN module receive buffer has overflowed */
  213. #define CO_EM_CAN_TX_OVERFLOW 0x14U /**< 0x14, communication, critical, CAN transmit buffer has overflowed */
  214. #define CO_EM_TPDO_OUTSIDE_WINDOW 0x15U /**< 0x15, communication, critical, TPDO is outside SYNC window */
  215. #define CO_EM_16_unused 0x16U /**< 0x16, (unused) */
  216. #define CO_EM_17_unused 0x17U /**< 0x17, (unused) */
  217. #define CO_EM_SYNC_TIME_OUT 0x18U /**< 0x18, communication, critical, SYNC message timeout */
  218. #define CO_EM_SYNC_LENGTH 0x19U /**< 0x19, communication, critical, Unexpected SYNC data length */
  219. #define CO_EM_PDO_WRONG_MAPPING 0x1AU /**< 0x1A, communication, critical, Error with PDO mapping */
  220. #define CO_EM_HEARTBEAT_CONSUMER 0x1BU /**< 0x1B, communication, critical, Heartbeat consumer timeout */
  221. #define CO_EM_HB_CONSUMER_REMOTE_RESET 0x1CU /**< 0x1C, communication, critical, Heartbeat consumer detected remote node reset */
  222. #define CO_EM_1D_unused 0x1DU /**< 0x1D, (unused) */
  223. #define CO_EM_1E_unused 0x1EU /**< 0x1E, (unused) */
  224. #define CO_EM_1F_unused 0x1FU /**< 0x1F, (unused) */
  225. #define CO_EM_EMERGENCY_BUFFER_FULL 0x20U /**< 0x20, generic, info, Emergency buffer is full, Emergency message wasn't sent */
  226. #define CO_EM_21_unused 0x21U /**< 0x21, (unused) */
  227. #define CO_EM_MICROCONTROLLER_RESET 0x22U /**< 0x22, generic, info, Microcontroller has just started */
  228. #define CO_EM_23_unused 0x23U /**< 0x23, (unused) */
  229. #define CO_EM_24_unused 0x24U /**< 0x24, (unused) */
  230. #define CO_EM_25_unused 0x25U /**< 0x25, (unused) */
  231. #define CO_EM_26_unused 0x26U /**< 0x26, (unused) */
  232. #define CO_EM_27_unused 0x27U /**< 0x27, (unused) */
  233. #define CO_EM_WRONG_ERROR_REPORT 0x28U /**< 0x28, generic, critical, Wrong parameters to CO_errorReport() function */
  234. #define CO_EM_ISR_TIMER_OVERFLOW 0x29U /**< 0x29, generic, critical, Timer task has overflowed */
  235. #define CO_EM_MEMORY_ALLOCATION_ERROR 0x2AU /**< 0x2A, generic, critical, Unable to allocate memory for objects */
  236. #define CO_EM_GENERIC_ERROR 0x2BU /**< 0x2B, generic, critical, Generic error, test usage */
  237. #define CO_EM_GENERIC_SOFTWARE_ERROR 0x2CU /**< 0x2C, generic, critical, Software error */
  238. #define CO_EM_INCONSISTENT_OBJECT_DICT 0x2DU /**< 0x2D, generic, critical, Object dictionary does not match the software */
  239. #define CO_EM_CALCULATION_OF_PARAMETERS 0x2EU /**< 0x2E, generic, critical, Error in calculation of device parameters */
  240. #define CO_EM_NON_VOLATILE_MEMORY 0x2FU /**< 0x2F, generic, critical, Error with access to non volatile device memory */
  241. #define CO_EM_MANUFACTURER_START 0x30U /**< 0x30, manufacturer, info, This can be used by macros to calculate error codes */
  242. #define CO_EM_MANUFACTURER_END 0xFFU /**< 0xFF, manufacturer, info, This can be used by macros to check error codes */
  243. /** @} */
  244. /**
  245. * Size of internal buffer, whwre emergencies are stored after CO_errorReport().
  246. * Buffer is cleared by CO_EM_process().
  247. */
  248. #define CO_EM_INTERNAL_BUFFER_SIZE 10
  249. /**
  250. * Emergerncy object for CO_errorReport(). It contains error buffer, to which new emergency
  251. * messages are written, when CO_errorReport() is called. This object is included in
  252. * CO_EMpr_t object.
  253. */
  254. typedef struct{
  255. uint8_t *errorStatusBits; /**< From CO_EM_init() */
  256. uint8_t errorStatusBitsSize; /**< From CO_EM_init() */
  257. /** Internal buffer for storing unsent emergency messages.*/
  258. uint8_t buf[CO_EM_INTERNAL_BUFFER_SIZE * 8];
  259. uint8_t *bufEnd; /**< End+1 address of the above buffer */
  260. uint8_t *bufWritePtr; /**< Write pointer in the above buffer */
  261. uint8_t *bufReadPtr; /**< Read pointer in the above buffer */
  262. uint8_t bufFull; /**< True if above buffer is full */
  263. uint8_t wrongErrorReport; /**< Error in arguments to CO_errorReport() */
  264. #if ((CO_CONFIG_EM) & CO_CONFIG_FLAG_CALLBACK_PRE) || defined CO_DOXYGEN
  265. /** From CO_EM_initCallbackPre() or NULL */
  266. void (*pFunctSignalPre)(void *object);
  267. /** From CO_EM_initCallbackPre() or NULL */
  268. void *functSignalObjectPre;
  269. #endif
  270. #if ((CO_CONFIG_EM) & CO_CONFIG_EM_CONSUMER) || defined CO_DOXYGEN
  271. /** From CO_EM_initCallbackRx() or NULL */
  272. void (*pFunctSignalRx)(const uint16_t ident,
  273. const uint16_t errorCode,
  274. const uint8_t errorRegister,
  275. const uint8_t errorBit,
  276. const uint32_t infoCode);
  277. #endif
  278. }CO_EM_t;
  279. /**
  280. * Report error condition.
  281. *
  282. * Function is called on any error condition inside CANopen stack and may also
  283. * be called by application on custom error condition. Emergency message is sent
  284. * after the first occurance of specific error. In case of critical error, device
  285. * will not be able to stay in NMT_OPERATIONAL state.
  286. *
  287. * Function is short and may be used form any task or interrupt.
  288. *
  289. * @param em Emergency object.
  290. * @param errorBit from @ref CO_EM_errorStatusBits.
  291. * @param errorCode from @ref CO_EM_errorCodes.
  292. * @param infoCode 32 bit value is passed to bytes 4...7 of the Emergency message.
  293. * It contains optional additional information inside emergency message.
  294. */
  295. void CO_errorReport(CO_EM_t *em, const uint8_t errorBit, const uint16_t errorCode, const uint32_t infoCode);
  296. /**
  297. * Reset error condition.
  298. *
  299. * Function is called if any error condition is solved. Emergency message is sent
  300. * with @ref CO_EM_errorCodes 0x0000.
  301. *
  302. * Function is short and may be used form any task or interrupt.
  303. *
  304. * @param em Emergency object.
  305. * @param errorBit from @ref CO_EM_errorStatusBits.
  306. * @param infoCode 32 bit value is passed to bytes 4...7 of the Emergency message.
  307. */
  308. void CO_errorReset(CO_EM_t *em, const uint8_t errorBit, const uint32_t infoCode);
  309. /**
  310. * Check specific error condition.
  311. *
  312. * Function returns 1, if specific internal error is present. Otherwise it returns 0.
  313. *
  314. * @param em Emergency object.
  315. * @param errorBit from @ref CO_EM_errorStatusBits.
  316. *
  317. * @return false: Error is not present.
  318. * @return true: Error is present.
  319. */
  320. bool_t CO_isError(CO_EM_t *em, const uint8_t errorBit);
  321. #ifdef CO_DOXYGEN
  322. /** Skip section, if CO_SDOserver.h is not included */
  323. #define CO_SDO_SERVER_H
  324. #endif
  325. #ifdef CO_SDO_SERVER_H
  326. /**
  327. * Error control and Emergency object. It controls internal error state and
  328. * sends emergency message, if error condition was reported. Object is initialized
  329. * by CO_EM_init(). It contains CO_EM_t object.
  330. */
  331. typedef struct{
  332. uint8_t *errorRegister; /**< From CO_EM_init() */
  333. uint32_t *preDefErr; /**< From CO_EM_init() */
  334. uint8_t preDefErrSize; /**< From CO_EM_init() */
  335. uint8_t preDefErrNoOfErrors;/**< Number of active errors in preDefErr */
  336. uint32_t inhibitEmTimer; /**< Internal timer for emergency message */
  337. CO_EM_t *em; /**< CO_EM_t sub object is included here */
  338. uint16_t CANerrorStatusOld;/**< Old CAN error status bitfield */
  339. CO_CANmodule_t *CANdev; /**< From CO_EM_init() */
  340. CO_CANtx_t *CANtxBuff; /**< CAN transmit buffer */
  341. }CO_EMpr_t;
  342. /**
  343. * Initialize Error control and Emergency object.
  344. *
  345. * Function must be called in the communication reset section.
  346. *
  347. * @param emPr This object will be initialized.
  348. * @param em Emergency object defined separately. Will be included in emPr and
  349. * initialized too.
  350. * @param SDO SDO server object.
  351. * @param errorStatusBits Pointer to _Error Status Bits_ array from Object Dictionary
  352. * (manufacturer specific section). See @ref CO_EM_errorStatusBits.
  353. * @param errorStatusBitsSize Total size of the above array. Must be >= 6.
  354. * @param errorRegister Pointer to _Error Register_ (Object dictionary, index 0x1001).
  355. * @param preDefErr Pointer to _Pre defined error field_ array from Object
  356. * dictionary, index 0x1003.
  357. * @param preDefErrSize Size of the above array.
  358. * @param CANdevRx CAN device for Emergency reception.
  359. * @param CANdevRxIdx Index of receive buffer in the above CAN device.
  360. * @param CANdevTx CAN device for Emergency transmission.
  361. * @param CANdevTxIdx Index of transmit buffer in the above CAN device.
  362. * @param CANidTxEM CAN identifier for Emergency message.
  363. *
  364. * @return #CO_ReturnError_t CO_ERROR_NO or CO_ERROR_ILLEGAL_ARGUMENT.
  365. */
  366. CO_ReturnError_t CO_EM_init(
  367. CO_EM_t *em,
  368. CO_EMpr_t *emPr,
  369. CO_SDO_t *SDO,
  370. uint8_t *errorStatusBits,
  371. uint8_t errorStatusBitsSize,
  372. uint8_t *errorRegister,
  373. uint32_t *preDefErr,
  374. uint8_t preDefErrSize,
  375. CO_CANmodule_t *CANdevRx,
  376. uint16_t CANdevRxIdx,
  377. CO_CANmodule_t *CANdevTx,
  378. uint16_t CANdevTxIdx,
  379. uint16_t CANidTxEM);
  380. #if ((CO_CONFIG_EM) & CO_CONFIG_FLAG_CALLBACK_PRE) || defined CO_DOXYGEN
  381. /**
  382. * Initialize Emergency callback function.
  383. *
  384. * Function initializes optional callback function, which should immediately
  385. * start processing of CO_EM_process() function.
  386. * Callback is called from CO_errorReport() or CO_errorReset() function. Those
  387. * functions are fast and may be called from any thread. Callback should
  388. * immediately start mainline thread, which calls CO_EM_process() function.
  389. *
  390. * @param em This object.
  391. * @param object Pointer to object, which will be passed to pFunctSignal(). Can be NULL
  392. * @param pFunctSignal Pointer to the callback function. Not called if NULL.
  393. */
  394. void CO_EM_initCallbackPre(
  395. CO_EM_t *em,
  396. void *object,
  397. void (*pFunctSignal)(void *object));
  398. #endif
  399. #if ((CO_CONFIG_EM) & CO_CONFIG_EM_CONSUMER) || defined CO_DOXYGEN
  400. /**
  401. * Initialize Emergency received callback function.
  402. *
  403. * Function initializes optional callback function, which executes after
  404. * error condition is received.
  405. *
  406. * _ident_ argument from callback contains CAN-ID of the emergency message. If
  407. * _ident_ == 0, then emergency message was sent from this device.
  408. *
  409. * @remark Depending on the CAN driver implementation, this function is called
  410. * inside an ISR or inside a mainline. Must be thread safe.
  411. *
  412. * @param em This object.
  413. * @param pFunctSignalRx Pointer to the callback function. Not called if NULL.
  414. */
  415. void CO_EM_initCallbackRx(
  416. CO_EM_t *em,
  417. void (*pFunctSignalRx)(const uint16_t ident,
  418. const uint16_t errorCode,
  419. const uint8_t errorRegister,
  420. const uint8_t errorBit,
  421. const uint32_t infoCode));
  422. #endif
  423. /**
  424. * Process Error control and Emergency object.
  425. *
  426. * Function must be called cyclically. It verifies some communication errors,
  427. * calculates bit 0 and bit 4 from _Error register_ and sends emergency message
  428. * if necessary.
  429. *
  430. * @param emPr This object.
  431. * @param NMTisPreOrOperational True if this node is NMT_PRE_OPERATIONAL or NMT_OPERATIONAL.
  432. * @param timeDifference_us Time difference from previous function call in [microseconds].
  433. * @param emInhTime _Inhibit time EMCY_ in [100*us] (object dictionary, index 0x1015).
  434. * @param [out] timerNext_us info to OS - see CO_process().
  435. */
  436. void CO_EM_process(
  437. CO_EMpr_t *emPr,
  438. bool_t NMTisPreOrOperational,
  439. uint32_t timeDifference_us,
  440. uint16_t emInhTime,
  441. uint32_t *timerNext_us);
  442. #endif
  443. /** @} */ /* CO_Emergency */
  444. #ifdef __cplusplus
  445. }
  446. #endif /*__cplusplus*/
  447. #endif /* CO_EMERGENCY_H */