can.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. // Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include "freertos/FreeRTOS.h"
  14. #include "freertos/portmacro.h"
  15. #include "freertos/task.h"
  16. #include "freertos/queue.h"
  17. #include "freertos/semphr.h"
  18. #include "esp_types.h"
  19. #include "esp_log.h"
  20. #include "esp_intr_alloc.h"
  21. #include "soc/dport_reg.h"
  22. #include "soc/can_struct.h"
  23. #include "driver/gpio.h"
  24. #include "driver/periph_ctrl.h"
  25. #include "driver/can.h"
  26. /* ---------------------------- Definitions --------------------------------- */
  27. //Internal Macros
  28. #define CAN_CHECK(cond, ret_val) ({ \
  29. if (!(cond)) { \
  30. return (ret_val); \
  31. } \
  32. })
  33. #define CAN_CHECK_FROM_CRIT(cond, ret_val) ({ \
  34. if (!(cond)) { \
  35. CAN_EXIT_CRITICAL(); \
  36. return ret_val; \
  37. } \
  38. })
  39. #define CAN_SET_FLAG(var, mask) ((var) |= (mask))
  40. #define CAN_RESET_FLAG(var, mask) ((var) &= ~(mask))
  41. #define CAN_TAG "CAN"
  42. //Driver default config/values
  43. #define DRIVER_DEFAULT_EWL 96 //Default Error Warning Limit value
  44. #define DRIVER_DEFAULT_TEC 0 //TX Error Counter starting value
  45. #define DRIVER_DEFAULT_REC 0 //RX Error Counter starting value
  46. #define DRIVER_DEFAULT_CLKOUT_DIV 14 //APB CLK divided by two
  47. #define DRIVER_DEFAULT_INTERRUPTS 0xE7 //Exclude data overrun
  48. #define DRIVER_DEFAULT_ERR_PASS_CNT 128 //Error counter threshold for error passive
  49. //Command Bit Masks
  50. #define CMD_TX_REQ 0x01 //Transmission Request
  51. #define CMD_ABORT_TX 0x02 //Abort Transmission
  52. #define CMD_RELEASE_RX_BUFF 0x04 //Release Receive Buffer
  53. #define CMD_CLR_DATA_OVRN 0x08 //Clear Data Overrun
  54. #define CMD_SELF_RX_REQ 0x10 //Self Reception Request
  55. #define CMD_TX_SINGLE_SHOT 0x03 //Single Shot Transmission
  56. #define CMD_SELF_RX_SINGLE_SHOT 0x12 //Single Shot Self Reception
  57. //Control flags
  58. #define CTRL_FLAG_STOPPED 0x001 //CAN peripheral in stopped state
  59. #define CTRL_FLAG_RECOVERING 0x002 //Bus is undergoing bus recovery
  60. #define CTRL_FLAG_ERR_WARN 0x004 //TEC or REC is >= error warning limit
  61. #define CTRL_FLAG_ERR_PASSIVE 0x008 //TEC or REC is >= 128
  62. #define CTRL_FLAG_BUS_OFF 0x010 //Bus-off due to TEC >= 256
  63. #define CTRL_FLAG_TX_BUFF_OCCUPIED 0x020 //Transmit buffer is occupied
  64. #define CTRL_FLAG_SELF_TEST 0x040 //Configured to Self Test Mode
  65. #define CTRL_FLAG_LISTEN_ONLY 0x080 //Configured to Listen Only Mode
  66. //Constants use for frame formatting and parsing
  67. #define FRAME_MAX_LEN 13 //EFF with 8 bytes of data
  68. #define FRAME_MAX_DATA_LEN 8 //Max data bytes allowed in CAN2.0
  69. #define FRAME_EXTD_ID_LEN 4 //EFF ID requires 4 bytes (29bit)
  70. #define FRAME_STD_ID_LEN 2 //SFF ID requires 2 bytes (11bit)
  71. #define FRAME_INFO_LEN 1 //Frame info requires 1 byte
  72. #define ALERT_LOG_LEVEL_WARNING CAN_ALERT_ARB_LOST //Alerts above and including this level use ESP_LOGW
  73. #define ALERT_LOG_LEVEL_ERROR CAN_ALERT_TX_FAILED //Alerts above and including this level use ESP_LOGE
  74. /* ------------------ Typedefs, structures, and variables ------------------- */
  75. /* Formatted frame structure has identical layout as TX/RX buffer registers.
  76. This allows for direct copy to/from TX/RX buffer. The two reserved bits in TX
  77. buffer are used in the frame structure to store the self_reception and
  78. single_shot flags. */
  79. typedef union {
  80. struct {
  81. struct {
  82. uint8_t dlc: 4; //Data length code (0 to 8) of the frame
  83. uint8_t self_reception: 1; //This frame should be transmitted using self reception command
  84. uint8_t single_shot: 1; //This frame should be transmitted using single shot command
  85. uint8_t rtr: 1; //This frame is a remote transmission request
  86. uint8_t frame_format: 1; //Format of the frame (1 = extended, 0 = standard)
  87. };
  88. union {
  89. struct {
  90. uint8_t id[FRAME_STD_ID_LEN]; //11 bit standard frame identifier
  91. uint8_t data[FRAME_MAX_DATA_LEN]; //Data bytes (0 to 8)
  92. uint8_t reserved8[2];
  93. } standard;
  94. struct {
  95. uint8_t id[FRAME_EXTD_ID_LEN]; //29 bit extended frame identifier
  96. uint8_t data[FRAME_MAX_DATA_LEN]; //Data bytes (0 to 8)
  97. } extended;
  98. };
  99. };
  100. uint8_t bytes[FRAME_MAX_LEN];
  101. } can_frame_t;
  102. //Control structure for CAN driver
  103. typedef struct {
  104. //Control and status members
  105. uint32_t control_flags;
  106. uint32_t rx_missed_count;
  107. uint32_t tx_failed_count;
  108. uint32_t arb_lost_count;
  109. uint32_t bus_error_count;
  110. intr_handle_t isr_handle;
  111. //TX and RX
  112. QueueHandle_t tx_queue;
  113. QueueHandle_t rx_queue;
  114. int tx_msg_count;
  115. int rx_msg_count;
  116. //Alerts
  117. SemaphoreHandle_t alert_semphr;
  118. uint32_t alerts_enabled;
  119. uint32_t alerts_triggered;
  120. } can_obj_t;
  121. static can_obj_t *p_can_obj = NULL;
  122. static portMUX_TYPE can_spinlock = portMUX_INITIALIZER_UNLOCKED;
  123. #define CAN_ENTER_CRITICAL() portENTER_CRITICAL(&can_spinlock)
  124. #define CAN_EXIT_CRITICAL() portEXIT_CRITICAL(&can_spinlock)
  125. /* ------------------- Configuration Register Functions---------------------- */
  126. static inline esp_err_t can_enter_reset_mode()
  127. {
  128. /* Enter reset mode (required to write to configuration registers). Reset mode
  129. also prevents all CAN activity on the current module and is automatically
  130. set upon entering a BUS-OFF condition. */
  131. CAN.mode_reg.reset = 1; //Set reset mode bit
  132. CAN_CHECK(CAN.mode_reg.reset == 1, ESP_ERR_INVALID_STATE); //Check bit was set
  133. return ESP_OK;
  134. }
  135. static inline esp_err_t can_exit_reset_mode()
  136. {
  137. /* Exiting reset mode will return the CAN module to operating mode. Reset mode
  138. must also be exited in order to trigger BUS-OFF recovery sequence. */
  139. CAN.mode_reg.reset = 0; //Exit reset mode
  140. CAN_CHECK(CAN.mode_reg.reset == 0, ESP_ERR_INVALID_STATE); //Check bit was reset
  141. return ESP_OK;
  142. }
  143. static inline void can_config_pelican()
  144. {
  145. //Use PeliCAN address layout. Exposes extra registers
  146. CAN.clock_divider_reg.can_mode = 1;
  147. }
  148. static inline void can_config_mode(can_mode_t mode)
  149. {
  150. //Configure CAN mode of operation
  151. can_mode_reg_t mode_reg;
  152. mode_reg.val = CAN.mode_reg.val; //Get current value of mode register
  153. if (mode == CAN_MODE_NO_ACK) {
  154. mode_reg.self_test = 1;
  155. mode_reg.listen_only = 0;
  156. } else if (mode == CAN_MODE_LISTEN_ONLY) {
  157. mode_reg.self_test = 0;
  158. mode_reg.listen_only = 1;
  159. } else {
  160. //Default to normal operating mode
  161. mode_reg.self_test = 0;
  162. mode_reg.listen_only = 0;
  163. }
  164. CAN.mode_reg.val = mode_reg.val; //Write back modified value to register
  165. }
  166. static inline void can_config_interrupts(uint32_t interrupts)
  167. {
  168. //Enable interrupt sources
  169. CAN.interrupt_enable_reg.val = interrupts;
  170. }
  171. static inline void can_config_bus_timing(uint32_t brp, uint32_t sjw, uint32_t tseg_1, uint32_t tseg_2, bool triple_sampling)
  172. {
  173. /* Configure bus/bit timing of CAN peripheral.
  174. - BRP (even from 2 to 128) divide APB to CAN system clock (T_scl)
  175. - SJW (1 to 4) is number of T_scl to shorten/lengthen for bit synchronization
  176. - TSEG_1 (1 to 16) is number of T_scl in a bit time before sample point
  177. - TSEG_2 (1 to 8) is number of T_scl in a bit time after sample point
  178. - triple_sampling will cause each bit time to be sampled 3 times*/
  179. can_bus_tim_0_reg_t timing_reg_0;
  180. can_bus_tim_1_reg_t timing_reg_1;
  181. timing_reg_0.baud_rate_prescaler = (brp / 2) - 1;
  182. timing_reg_0.sync_jump_width = sjw - 1;
  183. timing_reg_1.time_seg_1 = tseg_1 - 1;
  184. timing_reg_1.time_seg_2 = tseg_2 - 1;
  185. timing_reg_1.sampling = triple_sampling;
  186. CAN.bus_timing_0_reg.val = timing_reg_0.val;
  187. CAN.bus_timing_1_reg.val = timing_reg_1.val;
  188. }
  189. static inline void can_config_error(int err_warn_lim, int rx_err_cnt, int tx_err_cnt)
  190. {
  191. /* Set error warning limit, RX error counter, and TX error counter. Note that
  192. forcibly setting RX/TX error counters will incur the expected status changes
  193. and interrupts as soon as reset mode exits. */
  194. if (err_warn_lim >= 0 && err_warn_lim <= UINT8_MAX) {
  195. //Defaults to 96 after hardware reset.
  196. CAN.error_warning_limit_reg.byte = err_warn_lim;
  197. }
  198. if (rx_err_cnt >= 0 && rx_err_cnt <= UINT8_MAX) {
  199. //Defaults to 0 after hardware reset.
  200. CAN.rx_error_counter_reg.byte = rx_err_cnt;
  201. }
  202. if (tx_err_cnt >= 0 && tx_err_cnt <= UINT8_MAX) {
  203. //Defaults to 0 after hardware reset, and 127 after BUS-OFF event
  204. CAN.tx_error_counter_reg.byte = tx_err_cnt;
  205. }
  206. }
  207. static inline void can_config_acceptance_filter(uint32_t code, uint32_t mask, bool single_filter)
  208. {
  209. //Set filter mode
  210. CAN.mode_reg.acceptance_filter = (single_filter) ? 1 : 0;
  211. //Swap code and mask to match big endian registers
  212. uint32_t code_swapped = __builtin_bswap32(code);
  213. uint32_t mask_swapped = __builtin_bswap32(mask);
  214. for (int i = 0; i < 4; i++) {
  215. CAN.acceptance_filter.code_reg[i].byte = ((code_swapped >> (i * 8)) & 0xFF);
  216. CAN.acceptance_filter.mask_reg[i].byte = ((mask_swapped >> (i * 8)) & 0xFF);
  217. }
  218. }
  219. static inline void can_config_clk_out(uint32_t divider)
  220. {
  221. /* Configure CLKOUT. CLKOUT is a pre-scaled version of APB CLK. Divider can be
  222. 1, or any even number from 2 to 14. Set to out of range value (0) to disable
  223. CLKOUT. */
  224. can_clk_div_reg_t clock_divider_reg;
  225. clock_divider_reg.val = CAN.clock_divider_reg.val;
  226. if (divider >= 2 && divider <= 14) {
  227. clock_divider_reg.clock_off = 0;
  228. clock_divider_reg.clock_divider = (divider / 2) - 1;
  229. } else if (divider == 1) {
  230. clock_divider_reg.clock_off = 0;
  231. clock_divider_reg.clock_divider = 7;
  232. } else {
  233. clock_divider_reg.clock_off = 1;
  234. clock_divider_reg.clock_divider = 0;
  235. }
  236. CAN.clock_divider_reg.val = clock_divider_reg.val;
  237. }
  238. /* ---------------------- Runtime Register Functions------------------------- */
  239. static inline void can_set_command(uint8_t commands)
  240. {
  241. CAN.command_reg.val = commands;
  242. }
  243. static void can_set_tx_buffer_and_transmit(can_frame_t *frame)
  244. {
  245. //Copy frame structure into TX buffer registers
  246. for (int i = 0; i < FRAME_MAX_LEN; i++) {
  247. CAN.tx_rx_buffer[i].val = frame->bytes[i];
  248. }
  249. //Set correct transmit command
  250. uint8_t command;
  251. if (frame->self_reception) {
  252. command = (frame->single_shot) ? CMD_SELF_RX_SINGLE_SHOT : CMD_SELF_RX_REQ;
  253. } else {
  254. command = (frame->single_shot) ? CMD_TX_SINGLE_SHOT : CMD_TX_REQ;
  255. }
  256. can_set_command(command);
  257. }
  258. static inline uint32_t can_get_status()
  259. {
  260. return CAN.status_reg.val;
  261. }
  262. static inline uint32_t can_get_interrupt_reason()
  263. {
  264. return CAN.interrupt_reg.val;
  265. }
  266. static inline uint32_t can_get_arbitration_lost_capture()
  267. {
  268. return CAN.arbitration_lost_captue_reg.val;
  269. //Todo: ALC read only to re-arm arb lost interrupt. Add function to decode ALC
  270. }
  271. static inline uint32_t can_get_error_code_capture()
  272. {
  273. return CAN.error_code_capture_reg.val;
  274. //Todo: ECC read only to re-arm bus error interrupt. Add function to decode ECC
  275. }
  276. static inline void can_get_error_counters(uint32_t *tx_error_cnt, uint32_t *rx_error_cnt)
  277. {
  278. if (tx_error_cnt != NULL) {
  279. *tx_error_cnt = CAN.tx_error_counter_reg.byte;
  280. }
  281. if (rx_error_cnt != NULL) {
  282. *rx_error_cnt = CAN.rx_error_counter_reg.byte;
  283. }
  284. }
  285. static inline void can_get_rx_buffer_and_clear(can_frame_t *frame)
  286. {
  287. //Copy RX buffer registers into frame structure
  288. for (int i = 0; i < FRAME_MAX_LEN; i++) {
  289. frame->bytes[i] = CAN.tx_rx_buffer[i].val;
  290. }
  291. //Clear RX buffer
  292. can_set_command(CMD_RELEASE_RX_BUFF);
  293. }
  294. static inline uint32_t can_get_rx_message_counter()
  295. {
  296. return CAN.rx_message_counter_reg.val;
  297. }
  298. /* -------------------- Interrupt and Alert Handlers ------------------------ */
  299. static void can_alert_handler(uint32_t alert_code, int *alert_req)
  300. {
  301. if (p_can_obj->alerts_enabled & alert_code) {
  302. //Signify alert has occurred
  303. CAN_SET_FLAG(p_can_obj->alerts_triggered, alert_code);
  304. *alert_req = 1;
  305. if (p_can_obj->alerts_enabled & CAN_ALERT_AND_LOG) {
  306. if (alert_code >= ALERT_LOG_LEVEL_ERROR) {
  307. ESP_EARLY_LOGE(CAN_TAG, "Alert %d", alert_code);
  308. } else if (alert_code >= ALERT_LOG_LEVEL_WARNING) {
  309. ESP_EARLY_LOGW(CAN_TAG, "Alert %d", alert_code);
  310. } else {
  311. ESP_EARLY_LOGI(CAN_TAG, "Alert %d", alert_code);
  312. }
  313. }
  314. }
  315. }
  316. static void can_intr_handler_err_warn(can_status_reg_t *status, BaseType_t *task_woken, int *alert_req)
  317. {
  318. if (status->bus) {
  319. if (status->error) {
  320. //Bus-Off condition. TEC should set and held at 127, REC should be 0, reset mode entered
  321. CAN_SET_FLAG(p_can_obj->control_flags, CTRL_FLAG_BUS_OFF);
  322. /* Note: REC is still allowed to increase during bus-off. REC > err_warn
  323. can prevent "bus recovery complete" interrupt from occurring. Set to
  324. listen only mode to freeze REC. */
  325. can_config_mode(CAN_MODE_LISTEN_ONLY);
  326. can_alert_handler(CAN_ALERT_BUS_OFF, alert_req);
  327. } else {
  328. //Bus-recovery in progress. TEC has dropped below error warning limit
  329. can_alert_handler(CAN_ALERT_RECOVERY_IN_PROGRESS, alert_req);
  330. }
  331. } else {
  332. if (status->error) {
  333. //TEC or REC surpassed error warning limit
  334. CAN_SET_FLAG(p_can_obj->control_flags, CTRL_FLAG_ERR_WARN);
  335. can_alert_handler(CAN_ALERT_ABOVE_ERR_WARN, alert_req);
  336. } else if (p_can_obj->control_flags & CTRL_FLAG_RECOVERING) {
  337. //Bus recovery complete.
  338. can_enter_reset_mode();
  339. //Reset and set flags to the equivalent of the stopped state
  340. CAN_RESET_FLAG(p_can_obj->control_flags, CTRL_FLAG_RECOVERING | CTRL_FLAG_ERR_WARN |
  341. CTRL_FLAG_ERR_PASSIVE | CTRL_FLAG_BUS_OFF |
  342. CTRL_FLAG_TX_BUFF_OCCUPIED);
  343. CAN_SET_FLAG(p_can_obj->control_flags, CTRL_FLAG_STOPPED);
  344. can_alert_handler(CAN_ALERT_BUS_RECOVERED, alert_req);
  345. } else {
  346. //TEC and REC are both below error warning
  347. CAN_RESET_FLAG(p_can_obj->control_flags, CTRL_FLAG_ERR_WARN);
  348. can_alert_handler(CAN_ALERT_BELOW_ERR_WARN, alert_req);
  349. }
  350. }
  351. }
  352. static void can_intr_handler_err_passive(int *alert_req)
  353. {
  354. uint32_t tec, rec;
  355. can_get_error_counters(&tec, &rec);
  356. if (tec >= DRIVER_DEFAULT_ERR_PASS_CNT || rec >= DRIVER_DEFAULT_ERR_PASS_CNT) {
  357. //Entered error passive
  358. CAN_SET_FLAG(p_can_obj->control_flags, CTRL_FLAG_ERR_PASSIVE);
  359. can_alert_handler(CAN_ALERT_ERR_PASS, alert_req);
  360. } else {
  361. //Returned to error active
  362. CAN_RESET_FLAG(p_can_obj->control_flags, CTRL_FLAG_ERR_PASSIVE);
  363. can_alert_handler(CAN_ALERT_ERR_ACTIVE, alert_req);
  364. }
  365. }
  366. static void can_intr_handler_bus_err(int *alert_req)
  367. {
  368. // ECC register is read to re-arm bus error interrupt. ECC is not used
  369. (void) can_get_error_code_capture();
  370. p_can_obj->bus_error_count++;
  371. can_alert_handler(CAN_ALERT_BUS_ERROR, alert_req);
  372. }
  373. static void can_intr_handler_arb_lost(int *alert_req)
  374. {
  375. //ALC register is read to re-arm arb lost interrupt. ALC is not used
  376. (void) can_get_arbitration_lost_capture();
  377. p_can_obj->arb_lost_count++;
  378. can_alert_handler(CAN_ALERT_ARB_LOST, alert_req);
  379. }
  380. static void can_intr_handler_rx(BaseType_t *task_woken, int *alert_req)
  381. {
  382. can_rx_msg_cnt_reg_t msg_count_reg;
  383. msg_count_reg.val = can_get_rx_message_counter();
  384. for (int i = 0; i < msg_count_reg.rx_message_counter; i++) {
  385. can_frame_t frame;
  386. can_get_rx_buffer_and_clear(&frame);
  387. //Copy frame into RX Queue
  388. if (xQueueSendFromISR(p_can_obj->rx_queue, &frame, task_woken) == pdTRUE) {
  389. p_can_obj->rx_msg_count++;
  390. } else {
  391. p_can_obj->rx_missed_count++;
  392. can_alert_handler(CAN_ALERT_RX_QUEUE_FULL, alert_req);
  393. }
  394. }
  395. }
  396. static void can_intr_handler_tx(can_status_reg_t *status, int *alert_req)
  397. {
  398. //Handle previously transmitted frame
  399. if (status->tx_complete) {
  400. can_alert_handler(CAN_ALERT_TX_SUCCESS, alert_req);
  401. } else {
  402. p_can_obj->tx_failed_count++;
  403. can_alert_handler(CAN_ALERT_TX_FAILED, alert_req);
  404. }
  405. //Update TX message count
  406. p_can_obj->tx_msg_count--;
  407. configASSERT(p_can_obj->tx_msg_count >= 0); //Sanity check
  408. //Check if there are more frames to transmit
  409. if (p_can_obj->tx_msg_count > 0 && p_can_obj->tx_queue != NULL) {
  410. can_frame_t frame;
  411. configASSERT(xQueueReceiveFromISR(p_can_obj->tx_queue, &frame, NULL) == pdTRUE);
  412. can_set_tx_buffer_and_transmit(&frame);
  413. } else {
  414. //No more frames to transmit
  415. CAN_RESET_FLAG(p_can_obj->control_flags, CTRL_FLAG_TX_BUFF_OCCUPIED);
  416. can_alert_handler(CAN_ALERT_TX_IDLE, alert_req);
  417. }
  418. }
  419. static void can_intr_handler_main(void *arg)
  420. {
  421. BaseType_t task_woken = pdFALSE;
  422. int alert_req = 0;
  423. can_status_reg_t status;
  424. can_intr_reg_t intr_reason;
  425. CAN_ENTER_CRITICAL();
  426. status.val = can_get_status();
  427. intr_reason.val = (p_can_obj != NULL) ? can_get_interrupt_reason() : 0; //Incase intr occurs whilst driver is being uninstalled
  428. //Handle error counter related interrupts
  429. if (intr_reason.err_warn) {
  430. //Triggers when Bus-Status or Error-status bits change
  431. can_intr_handler_err_warn(&status, &task_woken, &alert_req);
  432. }
  433. if (intr_reason.err_passive) {
  434. //Triggers when entering/returning error passive/active state
  435. can_intr_handler_err_passive(&alert_req);
  436. }
  437. //Handle other error interrupts
  438. if (intr_reason.bus_err) {
  439. //Triggers when an error (Bit, Stuff, CRC, Form, ACK) occurs on the CAN bus
  440. can_intr_handler_bus_err(&alert_req);
  441. }
  442. if (intr_reason.arb_lost) {
  443. //Triggers when arbitration is lost
  444. can_intr_handler_arb_lost(&alert_req);
  445. }
  446. //Todo: Check data overrun bug where interrupt does not trigger even when enabled
  447. //Handle TX/RX interrupts
  448. if (intr_reason.rx) {
  449. //Triggers when RX buffer has one or more frames. Disabled if RX Queue length = 0
  450. can_intr_handler_rx(&task_woken, &alert_req);
  451. }
  452. if (intr_reason.tx) {
  453. //Triggers when TX buffer becomes free after a transmission
  454. can_intr_handler_tx(&status, &alert_req);
  455. }
  456. /* Todo: Check possible bug where transmitting self reception request then
  457. clearing rx buffer will cancel the transmission. */
  458. CAN_EXIT_CRITICAL();
  459. if (p_can_obj->alert_semphr != NULL && alert_req) {
  460. //Give semaphore if alerts were triggered
  461. xSemaphoreGiveFromISR(p_can_obj->alert_semphr, &task_woken);
  462. }
  463. if (task_woken == pdTRUE) {
  464. portYIELD_FROM_ISR();
  465. }
  466. }
  467. /* ---------------------- Frame and GPIO functions ------------------------- */
  468. static void can_format_frame(uint32_t id, uint8_t dlc, const uint8_t *data, uint32_t flags, can_frame_t *tx_frame)
  469. {
  470. /* This function encodes a message into a frame structure. The frame structure has
  471. an identical layout to the TX buffer, allowing the frame structure to be directly
  472. copied into TX buffer. */
  473. //Set frame information
  474. tx_frame->dlc = dlc;
  475. tx_frame->rtr = (flags & CAN_MSG_FLAG_RTR) ? 1 : 0;
  476. tx_frame->frame_format = (flags & CAN_MSG_FLAG_EXTD) ? 1 : 0;
  477. tx_frame->self_reception = (flags & CAN_MSG_FLAG_SELF) ? 1 : 0;
  478. tx_frame->single_shot = (flags & CAN_MSG_FLAG_SS) ? 1 : 0;
  479. //Set ID
  480. int id_len = (flags & CAN_MSG_FLAG_EXTD) ? FRAME_EXTD_ID_LEN : FRAME_STD_ID_LEN;
  481. uint8_t *id_buffer = (flags & CAN_MSG_FLAG_EXTD) ? tx_frame->extended.id : tx_frame->standard.id;
  482. //Split ID into 4 or 2 bytes, and turn into big-endian with left alignment (<< 3 or 5)
  483. uint32_t id_temp = (flags & CAN_MSG_FLAG_EXTD) ? __builtin_bswap32((id & CAN_EXTD_ID_MASK) << 3) : //((id << 3) >> 8*(3-i))
  484. __builtin_bswap16((id & CAN_STD_ID_MASK) << 5); //((id << 5) >> 8*(1-i))
  485. for (int i = 0; i < id_len; i++) {
  486. id_buffer[i] = (id_temp >> (8 * i)) & 0xFF; //Copy big-endian ID byte by byte
  487. }
  488. //Set Data.
  489. uint8_t *data_buffer = (flags & CAN_MSG_FLAG_EXTD) ? tx_frame->extended.data : tx_frame->standard.data;
  490. for (int i = 0; (i < dlc) && (i < FRAME_MAX_DATA_LEN); i++) { //Handle case where dlc is > 8
  491. data_buffer[i] = data[i];
  492. }
  493. }
  494. static void can_parse_frame(can_frame_t *rx_frame, uint32_t *id, uint8_t *dlc, uint8_t *data, uint32_t *flags)
  495. {
  496. //This function decodes a frame structure into it's constituent components.
  497. //Copy frame information
  498. *dlc = rx_frame->dlc;
  499. *flags = 0;
  500. *flags |= (rx_frame->dlc > FRAME_MAX_DATA_LEN) ? CAN_MSG_FLAG_DLC_NON_COMP : 0;
  501. *flags |= (rx_frame->rtr) ? CAN_MSG_FLAG_RTR : 0;
  502. *flags |= (rx_frame->frame_format) ? CAN_MSG_FLAG_EXTD : 0;
  503. //Copy ID
  504. int id_len = (rx_frame->frame_format) ? FRAME_EXTD_ID_LEN : FRAME_STD_ID_LEN;
  505. uint8_t *id_buffer = (rx_frame->frame_format) ? rx_frame->extended.id : rx_frame->standard.id;
  506. uint32_t id_temp = 0;
  507. for (int i = 0; i < id_len; i++) {
  508. id_temp |= id_buffer[i] << (8 * i); //Copy big-endian ID byte by byte
  509. }
  510. //Revert endianness of 4 or 2 byte ID, and shift into 29 or 11 bit ID
  511. id_temp = (rx_frame->frame_format) ? (__builtin_bswap32(id_temp) >> 3) : //((byte[i] << 8*(3-i)) >> 3)
  512. (__builtin_bswap16(id_temp) >> 5); //((byte[i] << 8*(1-i)) >> 5)
  513. *id = id_temp & ((rx_frame->frame_format) ? CAN_EXTD_ID_MASK : CAN_STD_ID_MASK);
  514. //Copy data
  515. uint8_t *data_buffer = (rx_frame->frame_format) ? rx_frame->extended.data : rx_frame->standard.data;
  516. for (int i = 0; (i < rx_frame->dlc) && (i < FRAME_MAX_DATA_LEN); i++) {
  517. data[i] = data_buffer[i];
  518. }
  519. //Set remaining bytes of data to 0
  520. for (int i = rx_frame->dlc; i < FRAME_MAX_DATA_LEN; i++) {
  521. data[i] = 0;
  522. }
  523. }
  524. static void can_configure_gpio(gpio_num_t tx, gpio_num_t rx, gpio_num_t clkout, gpio_num_t bus_status)
  525. {
  526. //Set TX pin
  527. gpio_set_pull_mode(tx, GPIO_FLOATING);
  528. gpio_matrix_out(tx, CAN_TX_IDX, false, false);
  529. gpio_pad_select_gpio(tx);
  530. //Set RX pin
  531. gpio_set_pull_mode(rx, GPIO_FLOATING);
  532. gpio_matrix_in(rx, CAN_RX_IDX, false);
  533. gpio_pad_select_gpio(rx);
  534. //Configure output clock pin (Optional)
  535. if (clkout >= 0 && clkout < GPIO_NUM_MAX) {
  536. gpio_set_pull_mode(clkout, GPIO_FLOATING);
  537. gpio_matrix_out(clkout, CAN_CLKOUT_IDX, false, false);
  538. gpio_pad_select_gpio(clkout);
  539. }
  540. //Configure bus status pin (Optional)
  541. if (bus_status >= 0 && bus_status < GPIO_NUM_MAX) {
  542. gpio_set_pull_mode(bus_status, GPIO_FLOATING);
  543. gpio_matrix_out(bus_status, CAN_BUS_OFF_ON_IDX, false, false);
  544. gpio_pad_select_gpio(bus_status);
  545. }
  546. }
  547. /* ---------------------------- Public Functions ---------------------------- */
  548. esp_err_t can_driver_install(const can_general_config_t *g_config, const can_timing_config_t *t_config, const can_filter_config_t *f_config)
  549. {
  550. //Check arguments and state
  551. CAN_CHECK(p_can_obj == NULL, ESP_ERR_INVALID_STATE); //Check is driver is already installed
  552. CAN_CHECK(g_config != NULL, ESP_ERR_INVALID_ARG);
  553. CAN_CHECK(t_config != NULL, ESP_ERR_INVALID_ARG);
  554. CAN_CHECK(f_config != NULL, ESP_ERR_INVALID_ARG);
  555. CAN_CHECK(g_config->rx_queue_len > 0, ESP_ERR_INVALID_ARG);
  556. CAN_CHECK(g_config->tx_io >= 0 && g_config->tx_io < GPIO_NUM_MAX, ESP_ERR_INVALID_ARG);
  557. CAN_CHECK(g_config->rx_io >= 0 && g_config->rx_io < GPIO_NUM_MAX, ESP_ERR_INVALID_ARG);
  558. esp_err_t ret;
  559. //Initialize CAN object
  560. p_can_obj = calloc(1, sizeof(can_obj_t));
  561. CAN_CHECK(p_can_obj != NULL, ESP_ERR_NO_MEM);
  562. p_can_obj->tx_queue = (g_config->tx_queue_len > 0) ? xQueueCreate(g_config->tx_queue_len, sizeof(can_frame_t)) : NULL;
  563. p_can_obj->rx_queue = xQueueCreate(g_config->rx_queue_len, sizeof(can_frame_t));
  564. p_can_obj->alert_semphr = xSemaphoreCreateBinary();
  565. if ((g_config->tx_queue_len > 0 && p_can_obj->tx_queue == NULL) ||
  566. p_can_obj->rx_queue == NULL || p_can_obj->alert_semphr == NULL) {
  567. ret = ESP_ERR_NO_MEM;
  568. goto err;
  569. }
  570. p_can_obj->control_flags = CTRL_FLAG_STOPPED;
  571. p_can_obj->control_flags |= (g_config->mode == CAN_MODE_NO_ACK) ? CTRL_FLAG_SELF_TEST : 0;
  572. p_can_obj->control_flags |= (g_config->mode == CAN_MODE_LISTEN_ONLY) ? CTRL_FLAG_LISTEN_ONLY : 0;
  573. p_can_obj->tx_msg_count = 0;
  574. p_can_obj->rx_msg_count = 0;
  575. p_can_obj->tx_failed_count = 0;
  576. p_can_obj->rx_missed_count = 0;
  577. p_can_obj->arb_lost_count = 0;
  578. p_can_obj->bus_error_count = 0;
  579. p_can_obj->alerts_enabled = g_config->alerts_enabled;
  580. p_can_obj->alerts_triggered = 0;
  581. CAN_ENTER_CRITICAL();
  582. //Initialize CAN peripheral
  583. periph_module_enable(PERIPH_CAN_MODULE); //Enable APB CLK to CAN peripheral
  584. configASSERT(can_enter_reset_mode() == ESP_OK); //Must enter reset mode to write to config registers
  585. can_config_pelican(); //Use PeliCAN addresses
  586. /* Note: REC is allowed to increase even in reset mode. Listen only mode
  587. will freeze REC. The desired mode will be set when can_start() is called. */
  588. can_config_mode(CAN_MODE_LISTEN_ONLY);
  589. can_config_interrupts(DRIVER_DEFAULT_INTERRUPTS);
  590. can_config_bus_timing(t_config->brp, t_config->sjw, t_config->tseg_1, t_config->tseg_2, t_config->triple_sampling);
  591. can_config_error(DRIVER_DEFAULT_EWL, DRIVER_DEFAULT_REC, DRIVER_DEFAULT_TEC);
  592. can_config_acceptance_filter(f_config->acceptance_code, f_config->acceptance_mask, f_config->single_filter);
  593. can_config_clk_out(g_config->clkout_divider);
  594. //Allocate GPIO and Interrupts
  595. can_configure_gpio(g_config->tx_io, g_config->rx_io, g_config->clkout_io, g_config->bus_off_io);
  596. (void) can_get_interrupt_reason(); //Read interrupt reg to clear it before allocating ISR
  597. ESP_ERROR_CHECK(esp_intr_alloc(ETS_CAN_INTR_SOURCE, 0, can_intr_handler_main, NULL, &p_can_obj->isr_handle));
  598. CAN_EXIT_CRITICAL();
  599. //Todo: Allow interrupt to be registered to specified CPU
  600. //CAN module is still in reset mode, users need to call can_start() afterwards
  601. return ESP_OK;
  602. err:
  603. //Cleanup and return error
  604. if (p_can_obj != NULL) {
  605. if (p_can_obj->tx_queue != NULL) {
  606. vQueueDelete(p_can_obj->tx_queue);
  607. p_can_obj->tx_queue = NULL;
  608. }
  609. if (p_can_obj->rx_queue != NULL) {
  610. vQueueDelete(p_can_obj->rx_queue);
  611. p_can_obj->rx_queue = NULL;
  612. }
  613. if (p_can_obj->alert_semphr != NULL) {
  614. vSemaphoreDelete(p_can_obj->alert_semphr);
  615. p_can_obj->alert_semphr = NULL;
  616. }
  617. free(p_can_obj);
  618. }
  619. return ret;
  620. }
  621. esp_err_t can_driver_uninstall()
  622. {
  623. //Check state
  624. CAN_ENTER_CRITICAL();
  625. CAN_CHECK_FROM_CRIT(p_can_obj != NULL, ESP_ERR_INVALID_STATE);
  626. CAN_CHECK_FROM_CRIT(p_can_obj->control_flags & (CTRL_FLAG_STOPPED | CTRL_FLAG_BUS_OFF), ESP_ERR_INVALID_STATE);
  627. //Clear registers
  628. configASSERT(can_enter_reset_mode() == ESP_OK); //Enter reset mode to stop any CAN bus activity
  629. (void) can_get_interrupt_reason();
  630. (void) can_get_arbitration_lost_capture();
  631. (void) can_get_error_code_capture();
  632. ESP_ERROR_CHECK(esp_intr_free(p_can_obj->isr_handle)); //Free interrupt
  633. periph_module_disable(PERIPH_CAN_MODULE); //Disable CAN peripheral
  634. //Delete queues, semaphores
  635. if (p_can_obj->tx_queue != NULL) {
  636. vQueueDelete(p_can_obj->tx_queue);
  637. }
  638. vQueueDelete(p_can_obj->rx_queue);
  639. vSemaphoreDelete(p_can_obj->alert_semphr);
  640. free(p_can_obj); //Free can driver object
  641. CAN_EXIT_CRITICAL();
  642. return ESP_OK;
  643. }
  644. esp_err_t can_start()
  645. {
  646. //Check state
  647. CAN_ENTER_CRITICAL();
  648. CAN_CHECK_FROM_CRIT(p_can_obj != NULL, ESP_ERR_INVALID_STATE);
  649. CAN_CHECK_FROM_CRIT(p_can_obj->control_flags & CTRL_FLAG_STOPPED, ESP_ERR_INVALID_STATE);
  650. //Reset RX queue, and RX message count
  651. xQueueReset(p_can_obj->rx_queue);
  652. p_can_obj->rx_msg_count = 0;
  653. configASSERT(can_enter_reset_mode() == ESP_OK); //Should already be in bus-off mode, set again to make sure
  654. //Currently in listen only mode, need to set to mode specified by configuration
  655. can_mode_t mode;
  656. if (p_can_obj->control_flags & CTRL_FLAG_SELF_TEST) {
  657. mode = CAN_MODE_NO_ACK;
  658. } else if (p_can_obj->control_flags & CTRL_FLAG_LISTEN_ONLY) {
  659. mode = CAN_MODE_LISTEN_ONLY;
  660. } else {
  661. mode = CAN_MODE_NORMAL;
  662. }
  663. can_config_mode(mode); //Set mode
  664. (void) can_get_interrupt_reason(); //Clear interrupt register
  665. configASSERT(can_exit_reset_mode() == ESP_OK);
  666. CAN_RESET_FLAG(p_can_obj->control_flags, CTRL_FLAG_STOPPED);
  667. CAN_EXIT_CRITICAL();
  668. return ESP_OK;
  669. }
  670. esp_err_t can_stop()
  671. {
  672. //Check state
  673. CAN_ENTER_CRITICAL();
  674. CAN_CHECK_FROM_CRIT(p_can_obj != NULL, ESP_ERR_INVALID_STATE);
  675. CAN_CHECK_FROM_CRIT(!(p_can_obj->control_flags & (CTRL_FLAG_STOPPED | CTRL_FLAG_BUS_OFF)), ESP_ERR_INVALID_STATE);
  676. //Clear interrupts and reset flags
  677. configASSERT(can_enter_reset_mode() == ESP_OK);
  678. (void) can_get_interrupt_reason(); //Read interrupt register to clear interrupts
  679. can_config_mode(CAN_MODE_LISTEN_ONLY); //Set to listen only mode to freeze REC
  680. CAN_RESET_FLAG(p_can_obj->control_flags, CTRL_FLAG_TX_BUFF_OCCUPIED);
  681. CAN_SET_FLAG(p_can_obj->control_flags, CTRL_FLAG_STOPPED);
  682. //Reset TX Queue and message count
  683. if (p_can_obj->tx_queue != NULL) {
  684. xQueueReset(p_can_obj->tx_queue);
  685. }
  686. p_can_obj->tx_msg_count = 0;
  687. CAN_EXIT_CRITICAL();
  688. return ESP_OK;
  689. }
  690. esp_err_t can_transmit(const can_message_t *message, TickType_t ticks_to_wait)
  691. {
  692. //Check arguments
  693. CAN_CHECK(p_can_obj != NULL, ESP_ERR_INVALID_STATE);
  694. CAN_CHECK(message != NULL, ESP_ERR_INVALID_ARG);
  695. CAN_CHECK((message->data_length_code <= FRAME_MAX_DATA_LEN) || (message->flags & CAN_MSG_FLAG_DLC_NON_COMP), ESP_ERR_INVALID_ARG);
  696. CAN_ENTER_CRITICAL();
  697. //Check State
  698. CAN_CHECK_FROM_CRIT(!(p_can_obj->control_flags & CTRL_FLAG_LISTEN_ONLY), ESP_ERR_NOT_SUPPORTED);
  699. CAN_CHECK_FROM_CRIT(!(p_can_obj->control_flags & (CTRL_FLAG_STOPPED | CTRL_FLAG_BUS_OFF)), ESP_ERR_INVALID_STATE);
  700. //Format frame
  701. esp_err_t ret = ESP_FAIL;
  702. can_frame_t tx_frame;
  703. can_format_frame(message->identifier, message->data_length_code, message->data, message->flags, &tx_frame);
  704. //Check if frame can be sent immediately
  705. if ((p_can_obj->tx_msg_count == 0) && !(p_can_obj->control_flags & CTRL_FLAG_TX_BUFF_OCCUPIED)) {
  706. //No other frames waiting to transmit. Bypass queue and transmit immediately
  707. can_set_tx_buffer_and_transmit(&tx_frame);
  708. p_can_obj->tx_msg_count++;
  709. CAN_SET_FLAG(p_can_obj->control_flags, CTRL_FLAG_TX_BUFF_OCCUPIED);
  710. ret = ESP_OK;
  711. }
  712. CAN_EXIT_CRITICAL();
  713. if (ret != ESP_OK) {
  714. if (p_can_obj->tx_queue == NULL) {
  715. //TX Queue is disabled and TX buffer is occupied, message was not sent
  716. ret = ESP_FAIL;
  717. } else if (xQueueSend(p_can_obj->tx_queue, &tx_frame, ticks_to_wait) == pdTRUE) {
  718. //Copied to TX Queue
  719. CAN_ENTER_CRITICAL();
  720. if (p_can_obj->control_flags & (CTRL_FLAG_STOPPED | CTRL_FLAG_STOPPED)) {
  721. //TX queue was reset (due to stop/bus_off), remove copied frame from queue to prevent transmission
  722. configASSERT(xQueueReceive(p_can_obj->tx_queue, &tx_frame, 0) == pdTRUE);
  723. ret = ESP_ERR_INVALID_STATE;
  724. } else if ((p_can_obj->tx_msg_count == 0) && !(p_can_obj->control_flags & CTRL_FLAG_TX_BUFF_OCCUPIED)) {
  725. //TX buffer was freed during copy, manually trigger transmission
  726. configASSERT(xQueueReceive(p_can_obj->tx_queue, &tx_frame, 0) == pdTRUE);
  727. can_set_tx_buffer_and_transmit(&tx_frame);
  728. p_can_obj->tx_msg_count++;
  729. CAN_SET_FLAG(p_can_obj->control_flags, CTRL_FLAG_TX_BUFF_OCCUPIED);
  730. ret = ESP_OK;
  731. } else {
  732. //Frame was copied to queue, waiting to be transmitted
  733. p_can_obj->tx_msg_count++;
  734. ret = ESP_OK;
  735. }
  736. CAN_EXIT_CRITICAL();
  737. } else {
  738. //Timed out waiting for free space on TX queue
  739. ret = ESP_ERR_TIMEOUT;
  740. }
  741. }
  742. return ret;
  743. }
  744. esp_err_t can_receive(can_message_t *message, TickType_t ticks_to_wait)
  745. {
  746. //Check arguments and state
  747. CAN_CHECK(p_can_obj != NULL, ESP_ERR_INVALID_STATE);
  748. CAN_CHECK(message != NULL, ESP_ERR_INVALID_ARG);
  749. //Get frame from RX Queue or RX Buffer
  750. can_frame_t rx_frame;
  751. if (xQueueReceive(p_can_obj->rx_queue, &rx_frame, ticks_to_wait) != pdTRUE) {
  752. return ESP_ERR_TIMEOUT;
  753. }
  754. CAN_ENTER_CRITICAL();
  755. p_can_obj->rx_msg_count--;
  756. CAN_EXIT_CRITICAL();
  757. //Decode frame
  758. can_parse_frame(&rx_frame, &(message->identifier), &(message->data_length_code), message->data, &(message->flags));
  759. return ESP_OK;
  760. }
  761. esp_err_t can_read_alerts(uint32_t *alerts, TickType_t ticks_to_wait)
  762. {
  763. //Check arguments and state
  764. CAN_CHECK(p_can_obj != NULL, ESP_ERR_INVALID_STATE);
  765. CAN_CHECK(alerts != NULL, ESP_ERR_INVALID_ARG);
  766. //Wait for an alert to occur
  767. if (xSemaphoreTake(p_can_obj->alert_semphr, ticks_to_wait) == pdTRUE) {
  768. CAN_ENTER_CRITICAL();
  769. *alerts = p_can_obj->alerts_triggered;
  770. p_can_obj->alerts_triggered = 0; //Clear triggered alerts
  771. CAN_EXIT_CRITICAL();
  772. return ESP_OK;
  773. } else {
  774. *alerts = 0;
  775. return ESP_ERR_TIMEOUT;
  776. }
  777. }
  778. esp_err_t can_reconfigure_alerts(uint32_t alerts_enabled, uint32_t *current_alerts)
  779. {
  780. CAN_CHECK(p_can_obj != NULL, ESP_ERR_INVALID_STATE);
  781. CAN_ENTER_CRITICAL();
  782. uint32_t cur_alerts;
  783. cur_alerts = can_read_alerts(&cur_alerts, 0); //Clear any unhandled alerts
  784. p_can_obj->alerts_enabled = alerts_enabled; //Update enabled alerts
  785. CAN_EXIT_CRITICAL();
  786. if (current_alerts != NULL) {
  787. *current_alerts = cur_alerts;
  788. }
  789. return ESP_OK;
  790. }
  791. esp_err_t can_initiate_recovery()
  792. {
  793. CAN_ENTER_CRITICAL();
  794. //Check state
  795. CAN_CHECK_FROM_CRIT(p_can_obj != NULL, ESP_ERR_INVALID_STATE);
  796. CAN_CHECK_FROM_CRIT(p_can_obj->control_flags & CTRL_FLAG_BUS_OFF, ESP_ERR_INVALID_STATE);
  797. CAN_CHECK_FROM_CRIT(!(p_can_obj->control_flags & CTRL_FLAG_RECOVERING), ESP_ERR_INVALID_STATE);
  798. //Reset TX Queue/Counters
  799. if (p_can_obj->tx_queue != NULL) {
  800. xQueueReset(p_can_obj->tx_queue);
  801. }
  802. p_can_obj->tx_msg_count = 0;
  803. CAN_RESET_FLAG(p_can_obj->control_flags, CTRL_FLAG_TX_BUFF_OCCUPIED);
  804. CAN_SET_FLAG(p_can_obj->control_flags, CTRL_FLAG_RECOVERING);
  805. //Trigger start of recovery process
  806. configASSERT(can_exit_reset_mode() == ESP_OK);
  807. CAN_EXIT_CRITICAL();
  808. return ESP_OK;
  809. }
  810. esp_err_t can_get_status_info(can_status_info_t *status_info)
  811. {
  812. //Check parameters and state
  813. CAN_CHECK(p_can_obj != NULL, ESP_ERR_INVALID_STATE);
  814. CAN_CHECK(status_info != NULL, ESP_ERR_INVALID_ARG);
  815. CAN_ENTER_CRITICAL();
  816. uint32_t tec, rec;
  817. can_get_error_counters(&tec, &rec);
  818. status_info->tx_error_counter = tec;
  819. status_info->rx_error_counter = rec;
  820. status_info->msgs_to_tx = p_can_obj->tx_msg_count;
  821. status_info->msgs_to_rx = p_can_obj->rx_msg_count;
  822. status_info->tx_failed_count = p_can_obj->tx_failed_count;
  823. status_info->rx_missed_count = p_can_obj->rx_missed_count;
  824. status_info->arb_lost_count = p_can_obj->arb_lost_count;
  825. status_info->bus_error_count = p_can_obj->bus_error_count;
  826. if (p_can_obj->control_flags & CTRL_FLAG_RECOVERING) {
  827. status_info->state = CAN_STATE_RECOVERING;
  828. } else if (p_can_obj->control_flags & CTRL_FLAG_BUS_OFF) {
  829. status_info->state = CAN_STATE_BUS_OFF;
  830. } else if (p_can_obj->control_flags & CTRL_FLAG_STOPPED) {
  831. status_info->state = CAN_STATE_STOPPED;
  832. } else {
  833. status_info->state = CAN_STATE_RUNNING;
  834. }
  835. CAN_EXIT_CRITICAL();
  836. return ESP_OK;
  837. }