CAN.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /**
  2. * @section License
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2017, Thomas Barth, barth-dev.de
  7. * 2017, Jaime Breva, jbreva@nayarsystems.com
  8. * 2018, Michael Wagner, mw@iot-make.de
  9. *
  10. * Permission is hereby granted, free of charge, to any person
  11. * obtaining a copy of this software and associated documentation
  12. * files (the "Software"), to deal in the Software without
  13. * restriction, including without limitation the rights to use, copy,
  14. * modify, merge, publish, distribute, sublicense, and/or sell copies
  15. * of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be
  19. * included in all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. *
  30. */
  31. #include "CAN.h"
  32. #include "freertos/FreeRTOS.h"
  33. #include "freertos/queue.h"
  34. #include "esp_intr.h"
  35. #include "soc/dport_reg.h"
  36. #include <math.h>
  37. #include "driver/gpio.h"
  38. #include "can_regdef.h"
  39. #include "CAN_config.h"
  40. // CAN Filter - no acceptance filter
  41. static CAN_filter_t __filter = { Dual_Mode, 0, 0, 0, 0, 0Xff, 0Xff, 0Xff, 0Xff };
  42. static void CAN_read_frame_phy();
  43. static void CAN_isr(void *arg_p);
  44. static int CAN_write_frame_phy(const CAN_frame_t *p_frame);
  45. static SemaphoreHandle_t sem_tx_complete;
  46. static void CAN_isr(void *arg_p) {
  47. // Interrupt flag buffer
  48. __CAN_IRQ_t interrupt;
  49. BaseType_t higherPriorityTaskWoken = pdFALSE;
  50. // Read interrupt status and clear flags
  51. interrupt = MODULE_CAN->IR.U;
  52. // Handle RX frame available interrupt
  53. if ((interrupt & __CAN_IRQ_RX) != 0)
  54. CAN_read_frame_phy(&higherPriorityTaskWoken);
  55. // Handle TX complete interrupt
  56. // Handle error interrupts.
  57. if ((interrupt & (__CAN_IRQ_TX | __CAN_IRQ_ERR //0x4
  58. | __CAN_IRQ_DATA_OVERRUN // 0x8
  59. | __CAN_IRQ_WAKEUP // 0x10
  60. | __CAN_IRQ_ERR_PASSIVE // 0x20
  61. | __CAN_IRQ_ARB_LOST // 0x40
  62. | __CAN_IRQ_BUS_ERR // 0x80
  63. )) != 0) {
  64. xSemaphoreGiveFromISR(sem_tx_complete, &higherPriorityTaskWoken);
  65. }
  66. // check if any higher priority task has been woken by any handler
  67. if (higherPriorityTaskWoken)
  68. portYIELD_FROM_ISR();
  69. }
  70. static void CAN_read_frame_phy(BaseType_t *higherPriorityTaskWoken) {
  71. // byte iterator
  72. uint8_t __byte_i;
  73. // frame read buffer
  74. CAN_frame_t __frame;
  75. // check if we have a queue. If not, operation is aborted.
  76. if (CAN_cfg.rx_queue == NULL) {
  77. // Let the hardware know the frame has been read.
  78. MODULE_CAN->CMR.B.RRB = 1;
  79. return;
  80. }
  81. // get FIR
  82. __frame.FIR.U = MODULE_CAN->MBX_CTRL.FCTRL.FIR.U;
  83. // check if this is a standard or extended CAN frame
  84. // standard frame
  85. if (__frame.FIR.B.FF == CAN_frame_std) {
  86. // Get Message ID
  87. __frame.MsgID = _CAN_GET_STD_ID;
  88. // deep copy data bytes
  89. for (__byte_i = 0; __byte_i < __frame.FIR.B.DLC; __byte_i++)
  90. __frame.data.u8[__byte_i] = MODULE_CAN->MBX_CTRL.FCTRL.TX_RX.STD.data[__byte_i];
  91. }
  92. // extended frame
  93. else {
  94. // Get Message ID
  95. __frame.MsgID = _CAN_GET_EXT_ID;
  96. // deep copy data bytes
  97. for (__byte_i = 0; __byte_i < __frame.FIR.B.DLC; __byte_i++)
  98. __frame.data.u8[__byte_i] = MODULE_CAN->MBX_CTRL.FCTRL.TX_RX.EXT.data[__byte_i];
  99. }
  100. // send frame to input queue
  101. xQueueSendToBackFromISR(CAN_cfg.rx_queue, &__frame, higherPriorityTaskWoken);
  102. // Let the hardware know the frame has been read.
  103. MODULE_CAN->CMR.B.RRB = 1;
  104. }
  105. static int CAN_write_frame_phy(const CAN_frame_t *p_frame) {
  106. // byte iterator
  107. uint8_t __byte_i;
  108. // copy frame information record
  109. MODULE_CAN->MBX_CTRL.FCTRL.FIR.U = p_frame->FIR.U;
  110. // standard frame
  111. if (p_frame->FIR.B.FF == CAN_frame_std) {
  112. // Write message ID
  113. _CAN_SET_STD_ID(p_frame->MsgID);
  114. // Copy the frame data to the hardware
  115. for (__byte_i = 0; __byte_i < p_frame->FIR.B.DLC; __byte_i++)
  116. MODULE_CAN->MBX_CTRL.FCTRL.TX_RX.STD.data[__byte_i] = p_frame->data.u8[__byte_i];
  117. }
  118. // extended frame
  119. else {
  120. // Write message ID
  121. _CAN_SET_EXT_ID(p_frame->MsgID);
  122. // Copy the frame data to the hardware
  123. for (__byte_i = 0; __byte_i < p_frame->FIR.B.DLC; __byte_i++)
  124. MODULE_CAN->MBX_CTRL.FCTRL.TX_RX.EXT.data[__byte_i] = p_frame->data.u8[__byte_i];
  125. }
  126. // Transmit frame
  127. MODULE_CAN->CMR.B.TR = 1;
  128. return 0;
  129. }
  130. int CAN_init() {
  131. // Time quantum
  132. double __tq;
  133. // enable module
  134. DPORT_SET_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG, DPORT_CAN_CLK_EN);
  135. DPORT_CLEAR_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, DPORT_CAN_RST);
  136. // configure TX pin
  137. gpio_set_level(CAN_cfg.tx_pin_id, 1);
  138. gpio_set_direction(CAN_cfg.tx_pin_id, GPIO_MODE_OUTPUT);
  139. gpio_matrix_out(CAN_cfg.tx_pin_id, CAN_TX_IDX, 0, 0);
  140. gpio_pad_select_gpio(CAN_cfg.tx_pin_id);
  141. // configure RX pin
  142. gpio_set_direction(CAN_cfg.rx_pin_id, GPIO_MODE_INPUT);
  143. gpio_matrix_in(CAN_cfg.rx_pin_id, CAN_RX_IDX, 0);
  144. gpio_pad_select_gpio(CAN_cfg.rx_pin_id);
  145. // set to PELICAN mode
  146. MODULE_CAN->CDR.B.CAN_M = 0x1;
  147. // synchronization jump width is the same for all baud rates
  148. MODULE_CAN->BTR0.B.SJW = 0x1;
  149. // TSEG2 is the same for all baud rates
  150. MODULE_CAN->BTR1.B.TSEG2 = 0x1;
  151. // select time quantum and set TSEG1
  152. switch (CAN_cfg.speed) {
  153. case CAN_SPEED_1000KBPS:
  154. MODULE_CAN->BTR1.B.TSEG1 = 0x4;
  155. __tq = 0.125;
  156. break;
  157. case CAN_SPEED_800KBPS:
  158. MODULE_CAN->BTR1.B.TSEG1 = 0x6;
  159. __tq = 0.125;
  160. break;
  161. case CAN_SPEED_200KBPS:
  162. MODULE_CAN->BTR1.B.TSEG1 = 0xc;
  163. MODULE_CAN->BTR1.B.TSEG2 = 0x5;
  164. __tq = 0.25;
  165. break;
  166. default:
  167. MODULE_CAN->BTR1.B.TSEG1 = 0xc;
  168. __tq = ((float) 1000 / CAN_cfg.speed) / 16;
  169. }
  170. // set baud rate prescaler
  171. MODULE_CAN->BTR0.B.BRP = (uint8_t) round((((APB_CLK_FREQ * __tq) / 2) - 1) / 1000000) - 1;
  172. /* Set sampling
  173. * 1 -> triple; the bus is sampled three times; recommended for low/medium speed buses (class A and B) where
  174. * filtering spikes on the bus line is beneficial 0 -> single; the bus is sampled once; recommended for high speed
  175. * buses (SAE class C)*/
  176. MODULE_CAN->BTR1.B.SAM = 0x1;
  177. // enable all interrupts
  178. MODULE_CAN->IER.U = 0xff;
  179. // Set acceptance filter
  180. MODULE_CAN->MOD.B.AFM = __filter.FM;
  181. MODULE_CAN->MBX_CTRL.ACC.CODE[0] = __filter.ACR0;
  182. MODULE_CAN->MBX_CTRL.ACC.CODE[1] = __filter.ACR1;
  183. MODULE_CAN->MBX_CTRL.ACC.CODE[2] = __filter.ACR2;
  184. MODULE_CAN->MBX_CTRL.ACC.CODE[3] = __filter.ACR3;
  185. MODULE_CAN->MBX_CTRL.ACC.MASK[0] = __filter.AMR0;
  186. MODULE_CAN->MBX_CTRL.ACC.MASK[1] = __filter.AMR1;
  187. MODULE_CAN->MBX_CTRL.ACC.MASK[2] = __filter.AMR2;
  188. MODULE_CAN->MBX_CTRL.ACC.MASK[3] = __filter.AMR3;
  189. // set to normal mode
  190. MODULE_CAN->OCR.B.OCMODE = __CAN_OC_NOM;
  191. // clear error counters
  192. MODULE_CAN->TXERR.U = 0;
  193. MODULE_CAN->RXERR.U = 0;
  194. (void) MODULE_CAN->ECC;
  195. // clear interrupt flags
  196. (void) MODULE_CAN->IR.U;
  197. // install CAN ISR
  198. esp_intr_alloc(ETS_CAN_INTR_SOURCE, 0, CAN_isr, NULL, NULL);
  199. // allocate the tx complete semaphore
  200. sem_tx_complete = xSemaphoreCreateBinary();
  201. // Showtime. Release Reset Mode.
  202. MODULE_CAN->MOD.B.RM = 0;
  203. return 0;
  204. }
  205. int CAN_write_frame(const CAN_frame_t *p_frame) {
  206. if (sem_tx_complete == NULL) {
  207. return -1;
  208. }
  209. // Write the frame to the controller
  210. CAN_write_frame_phy(p_frame);
  211. // wait for the frame tx to complete
  212. xSemaphoreTake(sem_tx_complete, portMAX_DELAY);
  213. return 0;
  214. }
  215. int CAN_stop() {
  216. // enter reset mode
  217. MODULE_CAN->MOD.B.RM = 1;
  218. return 0;
  219. }
  220. int CAN_config_filter(const CAN_filter_t* p_filter) {
  221. __filter.FM = p_filter->FM;
  222. __filter.ACR0 = p_filter->ACR0;
  223. __filter.ACR1 = p_filter->ACR1;
  224. __filter.ACR2 = p_filter->ACR2;
  225. __filter.ACR3 = p_filter->ACR3;
  226. __filter.AMR0 = p_filter->AMR0;
  227. __filter.AMR1 = p_filter->AMR1;
  228. __filter.AMR2 = p_filter->AMR2;
  229. __filter.AMR3 = p_filter->AMR3;
  230. return 0;
  231. }