CANDriver.cpp 8.1 KB

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