can.c 42 KB

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