CANDriver.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. CAN driver class for ESP32
  3. */
  4. #include <Arduino.h>
  5. #include "CANDriver.h"
  6. #include <xtensa/hal.h>
  7. #include <freertos/FreeRTOS.h>
  8. #include "freertos/task.h"
  9. #include "freertos/queue.h"
  10. #include "freertos/semphr.h"
  11. #include "esp_err.h"
  12. #include "esp_log.h"
  13. #include "driver/twai.h"
  14. #define CAN1_TX_IRQ_Handler ESP32_CAN1_TX_HANDLER
  15. #define CAN1_RX0_IRQ_Handler ESP32_CAN1_RX0_HANDLER
  16. #define CAN1_RX1_IRQ_Handler ESP32_CAN1_RX1_HANDLER
  17. #define CAN2_TX_IRQ_Handler ESP32_CAN2_TX_HANDLER
  18. #define CAN2_RX0_IRQ_Handler ESP32_CAN2_RX0_HANDLER
  19. #define CAN2_RX1_IRQ_Handler ESP32_CAN2_RX1_HANDLER
  20. // from canard.h
  21. #define CANARD_CAN_FRAME_EFF (1UL << 31U) ///< Extended frame format
  22. #define CANARD_CAN_FRAME_RTR (1UL << 30U) ///< Remote transmission (not used by UAVCAN)
  23. #define CANARD_CAN_FRAME_ERR (1UL << 29U) ///< Error frame (not used by UAVCAN)
  24. // constructor
  25. CANDriver::CANDriver()
  26. {}
  27. void CANDriver::init(uint32_t bitrate)
  28. {
  29. init_bus(bitrate);
  30. }
  31. #define TX_GPIO_NUM GPIO_NUM_47
  32. #define RX_GPIO_NUM GPIO_NUM_38
  33. static const twai_general_config_t g_config = {.mode = TWAI_MODE_NORMAL, .tx_io = TX_GPIO_NUM, .rx_io = RX_GPIO_NUM, \
  34. .clkout_io = TWAI_IO_UNUSED, .bus_off_io = TWAI_IO_UNUSED, \
  35. .tx_queue_len = 5, .rx_queue_len = 5, \
  36. .alerts_enabled = TWAI_ALERT_NONE, .clkout_divider = 0, \
  37. .intr_flags = ESP_INTR_FLAG_LEVEL2};
  38. static const twai_timing_config_t t_config = TWAI_TIMING_CONFIG_1MBITS();
  39. static const twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
  40. void CANDriver::init_once(bool enable_irq)
  41. {
  42. if (twai_driver_install(&g_config, &t_config, &f_config) == ESP_OK)
  43. {
  44. Serial.printf("CAN/TWAI Driver installed\n");
  45. }
  46. else
  47. {
  48. Serial.printf("Failed to install CAN/TWAI driver\n");
  49. return;
  50. }
  51. //Reconfigure alerts to detect rx-related stuff only...
  52. uint32_t alerts_to_enable = TWAI_ALERT_RX_DATA | TWAI_ALERT_RX_QUEUE_FULL;
  53. if (twai_reconfigure_alerts(alerts_to_enable, NULL) == ESP_OK) {
  54. Serial.printf("CAN/TWAI Alerts reconfigured\n");
  55. } else {
  56. Serial.printf("Failed to reconfigure CAN/TWAI alerts");
  57. }
  58. //Start TWAI driver
  59. if (twai_start() == ESP_OK)
  60. {
  61. Serial.printf("CAN/TWAI Driver started\n");
  62. }
  63. else
  64. {
  65. Serial.printf("Failed to start CAN/TWAI driver\n");
  66. return;
  67. }
  68. }
  69. bool CANDriver::init_bus(const uint32_t _bitrate)
  70. {
  71. bitrate = _bitrate;
  72. init_once(true);
  73. Timings timings;
  74. if (!computeTimings(bitrate, timings)) {
  75. return false;
  76. }
  77. Serial.printf("Timings: presc=%u sjw=%u bs1=%u bs2=%u",
  78. unsigned(timings.prescaler), unsigned(timings.sjw), unsigned(timings.bs1), unsigned(timings.bs2));
  79. return true;
  80. }
  81. bool CANDriver::computeTimings(uint32_t target_bitrate, Timings& out_timings)
  82. {
  83. if (target_bitrate < 1) {
  84. return false;
  85. }
  86. /*
  87. * Hardware configuration
  88. */
  89. const uint32_t pclk = 100000;
  90. static const int MaxBS1 = 16;
  91. static const int MaxBS2 = 8;
  92. /*
  93. * Ref. "Automatic Baudrate Detection in CANopen Networks", U. Koppe, MicroControl GmbH & Co. KG
  94. * CAN in Automation, 2003
  95. *
  96. * According to the source, optimal quanta per bit are:
  97. * Bitrate Optimal Maximum
  98. * 1000 kbps 8 10
  99. * 500 kbps 16 17
  100. * 250 kbps 16 17
  101. * 125 kbps 16 17
  102. */
  103. const int max_quanta_per_bit = (target_bitrate >= 1000000) ? 10 : 17;
  104. static const int MaxSamplePointLocation = 900;
  105. /*
  106. * Computing (prescaler * BS):
  107. * BITRATE = 1 / (PRESCALER * (1 / PCLK) * (1 + BS1 + BS2)) -- See the Reference Manual
  108. * BITRATE = PCLK / (PRESCALER * (1 + BS1 + BS2)) -- Simplified
  109. * let:
  110. * BS = 1 + BS1 + BS2 -- Number of time quanta per bit
  111. * PRESCALER_BS = PRESCALER * BS
  112. * ==>
  113. * PRESCALER_BS = PCLK / BITRATE
  114. */
  115. const uint32_t prescaler_bs = pclk / target_bitrate;
  116. /*
  117. * Searching for such prescaler value so that the number of quanta per bit is highest.
  118. */
  119. uint8_t bs1_bs2_sum = uint8_t(max_quanta_per_bit - 1);
  120. while ((prescaler_bs % (1 + bs1_bs2_sum)) != 0) {
  121. if (bs1_bs2_sum <= 2) {
  122. return false; // No solution
  123. }
  124. bs1_bs2_sum--;
  125. }
  126. const uint32_t prescaler = prescaler_bs / (1 + bs1_bs2_sum);
  127. if ((prescaler < 1U) || (prescaler > 1024U)) {
  128. return false; // No solution
  129. }
  130. /*
  131. * Now we have a constraint: (BS1 + BS2) == bs1_bs2_sum.
  132. * We need to find the values so that the sample point is as close as possible to the optimal value.
  133. *
  134. * Solve[(1 + bs1)/(1 + bs1 + bs2) == 7/8, bs2] (* Where 7/8 is 0.875, the recommended sample point location *)
  135. * {{bs2 -> (1 + bs1)/7}}
  136. *
  137. * Hence:
  138. * bs2 = (1 + bs1) / 7
  139. * bs1 = (7 * bs1_bs2_sum - 1) / 8
  140. *
  141. * Sample point location can be computed as follows:
  142. * Sample point location = (1 + bs1) / (1 + bs1 + bs2)
  143. *
  144. * Since the optimal solution is so close to the maximum, we prepare two solutions, and then pick the best one:
  145. * - With rounding to nearest
  146. * - With rounding to zero
  147. */
  148. struct BsPair {
  149. uint8_t bs1;
  150. uint8_t bs2;
  151. uint16_t sample_point_permill;
  152. BsPair() :
  153. bs1(0),
  154. bs2(0),
  155. sample_point_permill(0)
  156. { }
  157. BsPair(uint8_t bs1_bs2_sum, uint8_t arg_bs1) :
  158. bs1(arg_bs1),
  159. bs2(uint8_t(bs1_bs2_sum - bs1)),
  160. sample_point_permill(uint16_t(1000 * (1 + bs1) / (1 + bs1 + bs2)))
  161. {}
  162. bool isValid() const
  163. {
  164. return (bs1 >= 1) && (bs1 <= MaxBS1) && (bs2 >= 1) && (bs2 <= MaxBS2);
  165. }
  166. };
  167. // First attempt with rounding to nearest
  168. BsPair solution(bs1_bs2_sum, uint8_t(((7 * bs1_bs2_sum - 1) + 4) / 8));
  169. if (solution.sample_point_permill > MaxSamplePointLocation) {
  170. // Second attempt with rounding to zero
  171. solution = BsPair(bs1_bs2_sum, uint8_t((7 * bs1_bs2_sum - 1) / 8));
  172. }
  173. if ((target_bitrate != (pclk / (prescaler * (1 + solution.bs1 + solution.bs2)))) || !solution.isValid()) {
  174. return false;
  175. }
  176. Serial.printf("Timings: quanta/bit: %d, sample point location: %.1f%%",
  177. int(1 + solution.bs1 + solution.bs2), float(solution.sample_point_permill) / 10.F);
  178. out_timings.prescaler = uint16_t(prescaler - 1U);
  179. out_timings.sjw = 0; // Which means one
  180. out_timings.bs1 = uint8_t(solution.bs1 - 1);
  181. out_timings.bs2 = uint8_t(solution.bs2 - 1);
  182. return true;
  183. }
  184. bool CANDriver::send(const CANFrame &frame)
  185. {
  186. if (frame.isErrorFrame() || frame.dlc > 8) {
  187. return false;
  188. }
  189. twai_message_t message {};
  190. message.identifier = frame.id;
  191. message.extd = frame.isExtended() ? 1 : 0;
  192. message.data_length_code = frame.dlc;
  193. memcpy(message.data, frame.data, 8);
  194. esp_err_t sts = twai_transmit(&message, portMAX_DELAY);
  195. ESP_ERROR_CHECK(sts);
  196. return (sts == ESP_OK);
  197. }
  198. bool CANDriver::receive(CANFrame &out_frame)
  199. {
  200. #define MAX_RECV_MSGS_PER_SEC 200
  201. twai_message_t message {};
  202. esp_err_t recverr = twai_receive(&message, pdMS_TO_TICKS(1000/MAX_RECV_MSGS_PER_SEC));
  203. if (recverr != ESP_OK) {
  204. return false;
  205. }
  206. memcpy(out_frame.data, message.data, 8);// copy new data
  207. out_frame.dlc = message.data_length_code;
  208. out_frame.id = message.identifier;
  209. if (message.extd) {
  210. out_frame.id |= CANARD_CAN_FRAME_EFF;
  211. }
  212. if (out_frame.id & CANFrame::FlagERR) { // same as a message.isErrorFrame() if done later.
  213. return false;
  214. }
  215. return true;
  216. }