can.c 42 KB

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