drv_can_v2.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-08-30 shelton first version
  9. * 2024-12-18 shelton add support f455/f456 and f457
  10. */
  11. #include "drv_can_v2.h"
  12. #include "drv_config.h"
  13. #ifdef BSP_USING_CAN_V2
  14. #define LOG_TAG "drv_can"
  15. #include <drv_log.h>
  16. #ifdef RT_CAN_USING_CANFD
  17. static const uint8_t dlc_to_bytes[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 20, 24, 32, 48, 64};
  18. #endif
  19. #ifdef SOC_SERIES_AT32M412
  20. /* attention !!! baud calculation example: apbclk / ((bs1 + bs2) * bittime_div), ep: 180 / ((45 + 135) * 1) = 1MHz*/
  21. /* attention !!! default apbclk 180 mhz */
  22. static const struct at32_baud_rate can_baud_rate_tab[] =
  23. {
  24. {CAN1MBaud, 1 , 45, 135, 45},
  25. {CAN800kBaud, 1 , 45, 180, 45},
  26. {CAN500kBaud, 1 , 90, 270, 90},
  27. {CAN250kBaud, 2 , 90, 270, 90},
  28. {CAN125kBaud, 3 , 120, 360, 120},
  29. {CAN100kBaud, 5 , 90, 270, 90},
  30. {CAN50kBaud, 9 , 100, 300, 100},
  31. {CAN20kBaud, 18, 125, 375, 125},
  32. {CAN10kBaud, 30, 120, 480, 120},
  33. };
  34. #endif
  35. #ifdef SOC_SERIES_AT32M416
  36. /* attention !!! baud calculation example: apbclk / ((bs1 + bs2) * bittime_div), ep: 180 / ((45 + 135) * 1) = 1MHz*/
  37. /* attention !!! default apbclk 180 mhz */
  38. static const struct at32_baud_rate can_baud_rate_tab[] =
  39. {
  40. {CAN1MBaud, 1 , 45, 135, 45},
  41. {CAN800kBaud, 1 , 45, 180, 45},
  42. {CAN500kBaud, 1 , 90, 270, 90},
  43. {CAN250kBaud, 2 , 90, 270, 90},
  44. {CAN125kBaud, 3 , 120, 360, 120},
  45. {CAN100kBaud, 5 , 90, 270, 90},
  46. {CAN50kBaud, 9 , 100, 300, 100},
  47. {CAN20kBaud, 18, 125, 375, 125},
  48. {CAN10kBaud, 30, 120, 480, 120},
  49. };
  50. #ifdef RT_CAN_USING_CANFD
  51. /* attention !!! baud calculation example: apbclk / ((bs1 + bs2) * bittime_div), ep: 180 / ((45 + 135) * 1) = 1MHz*/
  52. /* attention !!! default apbclk 180 mhz, ssoffset default value is 'seg1 + 1', baud_rate_fd.div must equal to baud_rate.div */
  53. static const struct at32_baud_rate_fd canfd_baud_rate_tab[] =
  54. {
  55. {CANFD_DATA_1MBaud, 1 , 45, 135, 45, 136},
  56. {CANFD_DATA_2MBaud, 1 , 18, 72, 18, 73},
  57. {CANFD_DATA_3MBaud, 1 , 15, 45, 15, 46},
  58. {CANFD_DATA_4MBaud, 1 , 9, 36, 9, 37},
  59. {CANFD_DATA_5MBaud, 1 , 9, 27, 9, 28},
  60. {CANFD_DATA_6MBaud, 1 , 6, 24, 6, 25},
  61. };
  62. #endif
  63. #endif
  64. #ifdef SOC_SERIES_AT32F455
  65. /* attention !!! baud calculation example: apbclk / ((bs1 + bs2) * bittime_div), ep: 192 / ((48 + 144) * 1) = 1MHz*/
  66. /* attention !!! default apbclk 192 mhz */
  67. static const struct at32_baud_rate can_baud_rate_tab[] =
  68. {
  69. {CAN1MBaud, 1 , 48, 144, 48},
  70. {CAN800kBaud, 1 , 60, 180, 60},
  71. {CAN500kBaud, 1 , 96, 288, 96},
  72. {CAN250kBaud, 2 , 96, 288, 96},
  73. {CAN125kBaud, 3 , 128, 384, 128},
  74. {CAN100kBaud, 4 , 120, 260, 120},
  75. {CAN50kBaud, 8, 120, 260, 120},
  76. {CAN20kBaud, 16, 150, 450, 150},
  77. {CAN10kBaud, 24, 200, 600, 200},
  78. };
  79. #endif
  80. #ifdef SOC_SERIES_AT32F456
  81. /* attention !!! baud calculation example: apbclk / ((bs1 + bs2) * bittime_div), ep: 192 / ((48 + 144) * 1) = 1MHz*/
  82. /* attention !!! default apbclk 192 mhz */
  83. static const struct at32_baud_rate can_baud_rate_tab[] =
  84. {
  85. {CAN1MBaud, 1 , 48, 144, 48},
  86. {CAN800kBaud, 1 , 60, 180, 60},
  87. {CAN500kBaud, 1 , 96, 288, 96},
  88. {CAN250kBaud, 2 , 96, 288, 96},
  89. {CAN125kBaud, 3 , 128, 384, 128},
  90. {CAN100kBaud, 4 , 120, 260, 120},
  91. {CAN50kBaud, 8, 120, 260, 120},
  92. {CAN20kBaud, 16, 150, 450, 150},
  93. {CAN10kBaud, 24, 200, 600, 200},
  94. };
  95. #ifdef RT_CAN_USING_CANFD
  96. /* attention !!! baud calculation example: apbclk / ((bs1 + bs2) * bittime_div), ep: 192 / ((48 + 144) * 1) = 1MHz*/
  97. /* attention !!! default apbclk 192 mhz, ssoffset default value is 'seg1 + 1', baud_rate_fd.div must equal to baud_rate.div */
  98. static const struct at32_baud_rate_fd canfd_baud_rate_tab[] =
  99. {
  100. {CANFD_DATA_1MBaud, 1 , 48, 144, 48, 145},
  101. {CANFD_DATA_2MBaud, 1 , 24, 72, 24, 25},
  102. {CANFD_DATA_3MBaud, 1 , 16, 48, 16, 49},
  103. {CANFD_DATA_4MBaud, 1 , 12, 36, 12, 37},
  104. {CANFD_DATA_6MBaud, 1 , 12, 24, 12, 25},
  105. };
  106. #endif
  107. #endif
  108. #ifdef SOC_SERIES_AT32F457
  109. /* attention !!! baud calculation example: apbclk / ((bs1 + bs2) * bittime_div), ep: 192 / ((48 + 144) * 1) = 1MHz*/
  110. /* attention !!! default apbclk 192 mhz */
  111. static const struct at32_baud_rate can_baud_rate_tab[] =
  112. {
  113. {CAN1MBaud, 1 , 48, 144, 48},
  114. {CAN800kBaud, 1 , 60, 180, 60},
  115. {CAN500kBaud, 1 , 96, 288, 96},
  116. {CAN250kBaud, 2 , 96, 288, 96},
  117. {CAN125kBaud, 3 , 128, 384, 128},
  118. {CAN100kBaud, 4 , 120, 260, 120},
  119. {CAN50kBaud, 8, 120, 260, 120},
  120. {CAN20kBaud, 16, 150, 450, 150},
  121. {CAN10kBaud, 24, 200, 600, 200},
  122. };
  123. #ifdef RT_CAN_USING_CANFD
  124. /* attention !!! baud calculation example: apbclk / ((bs1 + bs2) * bittime_div), ep: 192 / ((48 + 144) * 1) = 1MHz*/
  125. /* attention !!! default apbclk 192 mhz, ssoffset default value is 'seg1 + 1', baud_rate_fd.div must equal to baud_rate.div */
  126. static const struct at32_baud_rate_fd canfd_baud_rate_tab[] =
  127. {
  128. {CANFD_DATA_1MBaud, 1 , 48, 144, 48, 145},
  129. {CANFD_DATA_2MBaud, 1 , 24, 72, 24, 25},
  130. {CANFD_DATA_3MBaud, 1 , 16, 48, 16, 49},
  131. {CANFD_DATA_4MBaud, 1 , 12, 36, 12, 37},
  132. {CANFD_DATA_6MBaud, 1 , 12, 24, 12, 25},
  133. };
  134. #endif
  135. #endif
  136. enum {
  137. #ifdef BSP_USING_CAN1
  138. CAN1_INDEX,
  139. #endif
  140. #ifdef BSP_USING_CAN2
  141. CAN2_INDEX,
  142. #endif
  143. #ifdef BSP_USING_CAN3
  144. CAN3_INDEX,
  145. #endif
  146. };
  147. static struct at32_can can_config[] = {
  148. #ifdef BSP_USING_CAN1
  149. CAN1_CONFIG,
  150. #endif
  151. #ifdef BSP_USING_CAN2
  152. CAN2_CONFIG,
  153. #endif
  154. #ifdef BSP_USING_CAN3
  155. CAN3_CONFIG,
  156. #endif
  157. };
  158. static rt_uint32_t get_can_baud_index(rt_uint32_t baud)
  159. {
  160. rt_uint32_t len, index;
  161. len = sizeof(can_baud_rate_tab) / sizeof(can_baud_rate_tab[0]);
  162. for (index = 0; index < len; index++)
  163. {
  164. if (can_baud_rate_tab[index].baud_rate == baud)
  165. return index;
  166. }
  167. /* default baud is CAN1MBaud */
  168. return 0;
  169. }
  170. #ifdef RT_CAN_USING_CANFD
  171. static rt_uint32_t get_canfd_baud_index(rt_uint32_t baud)
  172. {
  173. rt_uint32_t len, index;
  174. len = sizeof(canfd_baud_rate_tab) / sizeof(canfd_baud_rate_tab[0]);
  175. for (index = 0; index < len; index++)
  176. {
  177. if (canfd_baud_rate_tab[index].baud_rate == baud)
  178. return index;
  179. }
  180. /* default baud is CAN1MBaud */
  181. return 0;
  182. }
  183. #endif
  184. static rt_err_t _can_config(struct rt_can_device *can, struct can_configure *cfg)
  185. {
  186. rt_uint32_t baud_index;
  187. can_mode_type can_mode;
  188. can_transmit_status_type statues;
  189. RT_ASSERT(can);
  190. RT_ASSERT(cfg);
  191. struct at32_can *can_instance = rt_container_of(can, struct at32_can, device);
  192. RT_ASSERT(can_instance);
  193. at32_msp_can_init((void *)can_instance->can_x);
  194. /* config can baudrate */
  195. do {
  196. can_transmit_status_get(can_instance->can_x, &statues);
  197. }
  198. while((statues.current_tstat != CAN_TSTAT_IDLE) || (can_stb_status_get(can_instance->can_x) != CAN_STB_STATUS_EMPTY) || \
  199. (can_rxbuf_status_get(can_instance->can_x) != CAN_RXBUF_STATUS_EMPTY));
  200. can_software_reset(can_instance->can_x, TRUE);
  201. #ifdef RT_CAN_USING_CANFD
  202. can_instance->enable_canfd = cfg->enable_canfd;
  203. if (cfg->use_bit_timing != 0U)
  204. {
  205. can_instance->can_x->lbtcfg_bit.presc = cfg->can_timing.prescaler - 1;
  206. can_instance->can_x->actime_bit.ac_seg_1 = cfg->can_timing.num_seg1 - 2;
  207. can_instance->can_x->actime_bit.ac_seg_2 = cfg->can_timing.num_seg2 - 1;
  208. can_instance->can_x->actime_bit.ac_sjw = cfg->can_timing.num_sjw - 1;
  209. can_instance->can_x->fdtime_bit.fd_seg_1 = cfg->canfd_timing.num_seg1 - 2;
  210. can_instance->can_x->fdtime_bit.fd_seg_2 = cfg->canfd_timing.num_seg2 - 1;
  211. can_instance->can_x->fdtime_bit.fd_sjw = cfg->canfd_timing.num_sjw - 1;
  212. can_instance->can_x->lbtcfg_bit.fd_sspoff = cfg->canfd_timing.num_sspoff;
  213. }
  214. else if(cfg->baud_rate_fd != 0U)
  215. {
  216. baud_index = get_canfd_baud_index(cfg->baud_rate_fd);
  217. /* get baudrate parameters */
  218. can_instance->can_x->fdtime_bit.fd_seg_1 = canfd_baud_rate_tab[baud_index].div - 1;
  219. can_instance->can_x->fdtime_bit.fd_seg_1 = canfd_baud_rate_tab[baud_index].bts1_size - 2;
  220. can_instance->can_x->fdtime_bit.fd_seg_2 = canfd_baud_rate_tab[baud_index].bts2_size - 1;
  221. can_instance->can_x->fdtime_bit.fd_sjw = canfd_baud_rate_tab[baud_index].rsaw_size - 1;
  222. can_instance->can_x->lbtcfg_bit.fd_sspoff = canfd_baud_rate_tab[baud_index].ssoffset;
  223. }
  224. else
  225. #endif
  226. {
  227. baud_index = get_can_baud_index(cfg->baud_rate);
  228. /* get baudrate parameters */
  229. can_instance->can_x->lbtcfg_bit.presc = can_baud_rate_tab[baud_index].div - 1;
  230. can_instance->can_x->actime_bit.ac_seg_1 = can_baud_rate_tab[baud_index].bts1_size - 2;
  231. can_instance->can_x->actime_bit.ac_seg_2 = can_baud_rate_tab[baud_index].bts2_size - 1;
  232. can_instance->can_x->actime_bit.ac_sjw = can_baud_rate_tab[baud_index].rsaw_size - 1;
  233. }
  234. can_stb_transmit_mode_set(can_instance->can_x, CAN_STB_TRANSMIT_BY_FIFO);
  235. can_software_reset(can_instance->can_x, FALSE);
  236. /* config can base parameters */
  237. switch (cfg->mode)
  238. {
  239. case RT_CAN_MODE_NORMAL:
  240. can_mode = CAN_MODE_COMMUNICATE;
  241. break;
  242. case RT_CAN_MODE_LISTEN:
  243. can_mode = CAN_MODE_LISTENONLY;
  244. break;
  245. case RT_CAN_MODE_LOOPBACK:
  246. can_mode = CAN_MODE_EXT_LOOPBACK;
  247. break;
  248. case RT_CAN_MODE_LOOPBACKANLISTEN:
  249. can_mode = CAN_MODE_LISTENONLY_EXT;
  250. break;
  251. }
  252. can_mode_set(can_instance->can_x, can_mode);
  253. can_retransmission_limit_set(can_instance->can_x, CAN_RE_TRANS_TIMES_UNLIMIT);
  254. can_rearbitration_limit_set(can_instance->can_x, CAN_RE_ARBI_TIMES_UNLIMIT);
  255. can_rxbuf_warning_set(can_instance->can_x, 3);
  256. return RT_EOK;
  257. }
  258. static rt_err_t _can_control(struct rt_can_device *can, int cmd, void *arg)
  259. {
  260. rt_uint32_t argval;
  261. rt_uint32_t errtype;
  262. rt_uint8_t filter_number = 0;
  263. can_transmit_status_type statues;
  264. struct rt_can_filter_config *filter_cfg;
  265. RT_ASSERT(can != RT_NULL);
  266. struct at32_can *can_instance = rt_container_of(can, struct at32_can, device);
  267. RT_ASSERT(can_instance != RT_NULL);
  268. switch (cmd)
  269. {
  270. case RT_DEVICE_CTRL_CLR_INT:
  271. argval = (rt_uint32_t) arg;
  272. if (argval == RT_DEVICE_FLAG_INT_RX)
  273. {
  274. nvic_irq_disable(can_instance->rx_irqn);
  275. /* disable interrupt */
  276. can_interrupt_enable(can_instance->can_x, CAN_RAFIE_INT, FALSE);
  277. can_interrupt_enable(can_instance->can_x, CAN_RFIE_INT, FALSE);
  278. can_interrupt_enable(can_instance->can_x, CAN_ROIE_INT, FALSE);
  279. can_interrupt_enable(can_instance->can_x, CAN_RIE_INT, FALSE);
  280. }
  281. else if (argval == RT_DEVICE_FLAG_INT_TX)
  282. {
  283. nvic_irq_disable(can_instance->tx_irqn);
  284. /* disable interrupt */
  285. can_interrupt_enable(can_instance->can_x, CAN_TSIE_INT, FALSE);
  286. can_interrupt_enable(can_instance->can_x, CAN_TPIE_INT, FALSE);
  287. can_interrupt_enable(can_instance->can_x, CAN_AIE_INT, FALSE);
  288. can_interrupt_enable(can_instance->can_x, CAN_ALIE_INT, FALSE);
  289. }
  290. else if (argval == RT_DEVICE_CAN_INT_ERR)
  291. {
  292. nvic_irq_disable(can_instance->err_irqn);
  293. /* disable interrupt */
  294. can_interrupt_enable(can_instance->can_x, CAN_EIE_INT, FALSE);
  295. can_interrupt_enable(can_instance->can_x, CAN_BEIE_INT, FALSE);
  296. can_interrupt_enable(can_instance->can_x, CAN_EPIE_INT, FALSE);
  297. }
  298. break;
  299. case RT_DEVICE_CTRL_SET_INT:
  300. argval = (rt_uint32_t) arg;
  301. if (argval == RT_DEVICE_FLAG_INT_RX)
  302. {
  303. /* enable interrupt */
  304. can_interrupt_enable(can_instance->can_x, CAN_RAFIE_INT, TRUE);
  305. can_interrupt_enable(can_instance->can_x, CAN_RFIE_INT, TRUE);
  306. can_interrupt_enable(can_instance->can_x, CAN_ROIE_INT, TRUE);
  307. can_interrupt_enable(can_instance->can_x, CAN_RIE_INT, TRUE);
  308. nvic_irq_enable(can_instance->rx_irqn, 1, 0);
  309. }
  310. else if (argval == RT_DEVICE_FLAG_INT_TX)
  311. {
  312. /* enable interrupt */
  313. can_interrupt_enable(can_instance->can_x, CAN_TSIE_INT, TRUE);
  314. can_interrupt_enable(can_instance->can_x, CAN_TPIE_INT, TRUE);
  315. can_interrupt_enable(can_instance->can_x, CAN_AIE_INT, TRUE);
  316. can_interrupt_enable(can_instance->can_x, CAN_ALIE_INT, TRUE);
  317. nvic_irq_enable(can_instance->tx_irqn, 1, 0);
  318. }
  319. else if (argval == RT_DEVICE_CAN_INT_ERR)
  320. {
  321. /* enable interrupt */
  322. can_interrupt_enable(can_instance->can_x, CAN_EIE_INT, TRUE);
  323. can_interrupt_enable(can_instance->can_x, CAN_BEIE_INT, TRUE);
  324. can_interrupt_enable(can_instance->can_x, CAN_EPIE_INT, TRUE);
  325. nvic_irq_enable(can_instance->err_irqn, 1, 0);
  326. }
  327. break;
  328. case RT_CAN_CMD_SET_FILTER:
  329. if (RT_NULL == arg)
  330. {
  331. /* default filter config */
  332. can_filter_default_para_init(&can_instance->filter_init_struct);
  333. }
  334. else
  335. {
  336. filter_cfg = (struct rt_can_filter_config *)arg;
  337. /* get default filter */
  338. for (int i = 0; i < filter_cfg->count; i++)
  339. {
  340. if (filter_cfg->items[i].hdr_bank == -1)
  341. {
  342. filter_number = i;
  343. }
  344. else
  345. {
  346. filter_number = filter_cfg->items[i].hdr_bank;
  347. }
  348. can_instance->filter_init_struct.code_para.id = filter_cfg->items[i].id;
  349. can_instance->filter_init_struct.code_para.id_type = (can_identifier_type)filter_cfg->items[i].ide;
  350. can_instance->filter_init_struct.mask_para.id = (~(filter_cfg->items[i].mask) & ~(filter_cfg->items[i].id & filter_cfg->items[i].mask));
  351. can_instance->filter_init_struct.mask_para.id_type = FALSE;
  352. can_instance->filter_init_struct.mask_para.data_length = 0xF;
  353. can_instance->filter_init_struct.mask_para.frame_type = TRUE;
  354. can_instance->filter_init_struct.mask_para.recv_frame = TRUE;
  355. /* filter configuration */
  356. do {
  357. can_transmit_status_get(can_instance->can_x, &statues);
  358. }
  359. while((statues.current_tstat != CAN_TSTAT_IDLE) || (can_stb_status_get(can_instance->can_x) != CAN_STB_STATUS_EMPTY) || \
  360. (can_rxbuf_status_get(can_instance->can_x) != CAN_RXBUF_STATUS_EMPTY));
  361. can_software_reset(can_instance->can_x, TRUE);
  362. can_filter_set(can_instance->can_x, (can_filter_type)filter_number, &can_instance->filter_init_struct);
  363. can_software_reset(can_instance->can_x, FALSE);
  364. can_filter_enable(can_instance->can_x, (can_filter_type)filter_number, TRUE);
  365. }
  366. }
  367. break;
  368. case RT_CAN_CMD_SET_MODE:
  369. argval = (rt_uint32_t) arg;
  370. if (argval != RT_CAN_MODE_NORMAL &&
  371. argval != RT_CAN_MODE_LISTEN &&
  372. argval != RT_CAN_MODE_LOOPBACK &&
  373. argval != RT_CAN_MODE_LOOPBACKANLISTEN)
  374. {
  375. return -RT_ERROR;
  376. }
  377. if (argval != can_instance->device.config.mode)
  378. {
  379. can_instance->device.config.mode = argval;
  380. return _can_config(&can_instance->device, &can_instance->device.config);
  381. }
  382. break;
  383. case RT_CAN_CMD_SET_BAUD:
  384. argval = (rt_uint32_t) arg;
  385. if (argval != CAN1MBaud &&
  386. argval != CAN800kBaud &&
  387. argval != CAN500kBaud &&
  388. argval != CAN250kBaud &&
  389. argval != CAN125kBaud &&
  390. argval != CAN100kBaud &&
  391. argval != CAN50kBaud &&
  392. argval != CAN20kBaud &&
  393. argval != CAN10kBaud)
  394. {
  395. return -RT_ERROR;
  396. }
  397. if (argval != can_instance->device.config.baud_rate)
  398. {
  399. can_instance->device.config.baud_rate = argval;
  400. return _can_config(&can_instance->device, &can_instance->device.config);
  401. }
  402. break;
  403. case RT_CAN_CMD_SET_PRIV:
  404. argval = (rt_uint32_t) arg;
  405. if (argval != RT_CAN_MODE_PRIV &&
  406. argval != RT_CAN_MODE_NOPRIV)
  407. {
  408. return -RT_ERROR;
  409. }
  410. if (argval != can_instance->device.config.privmode)
  411. {
  412. can_instance->device.config.privmode = argval;
  413. return _can_config(&can_instance->device, &can_instance->device.config);
  414. }
  415. break;
  416. case RT_CAN_CMD_GET_STATUS:
  417. errtype = can_instance->can_x->err;
  418. can_instance->device.status.rcverrcnt = errtype >> 24;
  419. can_instance->device.status.snderrcnt = ((errtype >> 16) & 0xFF);
  420. can_instance->device.status.lasterrtype = ((errtype >> 13) & 0x7);
  421. rt_memcpy(arg, &can_instance->device.status, sizeof(can_instance->device.status));
  422. break;
  423. #ifdef RT_CAN_USING_CANFD
  424. case RT_CAN_CMD_SET_CANFD:
  425. if(can_instance->enable_canfd != argval)
  426. {
  427. can_instance->enable_canfd = (rt_uint32_t) argval;
  428. }
  429. break;
  430. case RT_CAN_CMD_SET_BAUD_FD:
  431. argval = (rt_uint32_t) arg;
  432. if (argval != CANFD_DATA_1MBaud &&
  433. argval != CANFD_DATA_2MBaud &&
  434. argval != CANFD_DATA_3MBaud &&
  435. argval != CANFD_DATA_4MBaud &&
  436. argval != CANFD_DATA_5MBaud &&
  437. argval != CANFD_DATA_6MBaud)
  438. {
  439. return -RT_ERROR;
  440. }
  441. if (argval != can_instance->device.config.baud_rate_fd)
  442. {
  443. can_instance->device.config.baud_rate_fd = argval;
  444. return _can_config(&can_instance->device, &can_instance->device.config);
  445. }
  446. break;
  447. case RT_CAN_CMD_SET_BITTIMING:
  448. {
  449. struct rt_can_bit_timing_config *timing_configs = (struct rt_can_bit_timing_config*)arg;
  450. if ((timing_configs == RT_NULL) || (timing_configs->count < 1) || (timing_configs->count > 2))
  451. {
  452. return -RT_ERROR;
  453. }
  454. if (timing_configs->count != 0U)
  455. {
  456. can_instance->device.config.can_timing = timing_configs->items[0];
  457. }
  458. if (timing_configs->count == 2)
  459. {
  460. can_instance->device.config.canfd_timing = timing_configs->items[1];
  461. }
  462. _can_config(&can_instance->device, &can_instance->device.config);
  463. }
  464. break;
  465. #endif
  466. }
  467. return RT_EOK;
  468. }
  469. static rt_ssize_t _can_sendmsg(struct rt_can_device *can, const void *buf, rt_uint32_t box_num)
  470. {
  471. rt_uint8_t copy_length = 0;
  472. can_txbuf_type tx_message;
  473. struct at32_can *can_instance = rt_container_of(can, struct at32_can, device);
  474. struct rt_can_msg *pmsg = (struct rt_can_msg *) buf;
  475. tx_message.id = pmsg->id;
  476. if (RT_CAN_STDID == pmsg->ide)
  477. {
  478. tx_message.id_type = CAN_ID_STANDARD;
  479. }
  480. else
  481. {
  482. tx_message.id_type = CAN_ID_EXTENDED;
  483. }
  484. if (RT_CAN_DTR == pmsg->rtr)
  485. {
  486. tx_message.frame_type = CAN_FRAME_DATA;
  487. }
  488. else
  489. {
  490. tx_message.frame_type = CAN_FRAME_REMOTE;
  491. }
  492. #ifdef RT_CAN_USING_CANFD
  493. if (pmsg->fd_frame != 0)
  494. {
  495. tx_message.fd_format = CAN_FORMAT_FD;
  496. tx_message.fd_rate_switch = (can_rate_switch_type)pmsg->brs;
  497. RT_ASSERT(pmsg->len <= 15);
  498. }
  499. else
  500. #endif
  501. {
  502. RT_ASSERT(pmsg->len <= 8);
  503. }
  504. #ifdef RT_CAN_USING_CANFD
  505. copy_length = dlc_to_bytes[pmsg->len];
  506. #else
  507. copy_length = pmsg->len;
  508. #endif
  509. /* set up the data field */
  510. rt_memcpy(&tx_message.data[0], &pmsg->data[0], copy_length);
  511. /* set up the dlc */
  512. tx_message.data_length = (can_data_length_type)pmsg->len;
  513. can_txbuf_write(can_instance->can_x, CAN_TXBUF_STB, &tx_message);
  514. can_txbuf_transmit(can_instance->can_x, CAN_TRANSMIT_STB_ALL);
  515. return RT_EOK;
  516. }
  517. static rt_ssize_t _can_recvmsg(struct rt_can_device *can, void *buf, rt_uint32_t fifo)
  518. {
  519. can_rxbuf_type rx_message;
  520. struct at32_can *can_instance = rt_container_of(can, struct at32_can, device);
  521. struct rt_can_msg *pmsg = (struct rt_can_msg *) buf;
  522. /* get data */
  523. can_rxbuf_read(can_instance->can_x, &rx_message);
  524. #ifdef RT_CAN_USING_CANFD
  525. pmsg->len = dlc_to_bytes[rx_message.data_length];
  526. pmsg->fd_frame = rx_message.fd_format;
  527. pmsg->brs = rx_message.fd_rate_switch;
  528. #else
  529. if(rx_message.data_length > 8)
  530. pmsg->len = 8;
  531. else
  532. pmsg->len = rx_message.data_length;
  533. #endif
  534. pmsg->id = rx_message.id;
  535. /* get the data field */
  536. rt_memcpy(&pmsg->data[0], &rx_message.data[0], pmsg->len);
  537. if (rx_message.id_type == CAN_ID_STANDARD)
  538. {
  539. pmsg->ide = RT_CAN_STDID;
  540. }
  541. else
  542. {
  543. pmsg->ide = RT_CAN_EXTID;
  544. }
  545. pmsg->rtr = rx_message.frame_type;
  546. return RT_EOK;
  547. }
  548. static const struct rt_can_ops _can_ops =
  549. {
  550. _can_config,
  551. _can_control,
  552. _can_sendmsg,
  553. _can_recvmsg,
  554. };
  555. static void _can_rx_isr(struct rt_can_device *can)
  556. {
  557. struct at32_can *can_instance = rt_container_of(can, struct at32_can, device);
  558. /* rx_buffer had data be received */
  559. if(can_interrupt_flag_get(can_instance->can_x, CAN_RIF_FLAG) != RESET)
  560. {
  561. can_flag_clear(can_instance->can_x, CAN_RIF_FLAG);
  562. rt_hw_can_isr(can, RT_CAN_EVENT_RX_IND);
  563. }
  564. /* rx_buffer almost full */
  565. if(can_interrupt_flag_get(can_instance->can_x, CAN_RAFIF_FLAG) != RESET)
  566. {
  567. can_flag_clear(can_instance->can_x, CAN_RAFIF_FLAG);
  568. }
  569. /* rx_buffer full */
  570. if(can_interrupt_flag_get(can_instance->can_x, CAN_RFIF_FLAG) != RESET)
  571. {
  572. can_flag_clear(can_instance->can_x, CAN_RFIF_FLAG);
  573. }
  574. /* rx_buffer overflow */
  575. if(can_interrupt_flag_get(can_instance->can_x, CAN_ROIF_FLAG) != RESET)
  576. {
  577. can_flag_clear(can_instance->can_x, CAN_ROIF_FLAG);
  578. rt_hw_can_isr(can, RT_CAN_EVENT_RXOF_IND);
  579. }
  580. }
  581. static void _can_tx_isr(struct rt_can_device *can)
  582. {
  583. struct at32_can *can_instance = rt_container_of(can, struct at32_can, device);
  584. /* transmit buffer be completed */
  585. if(can_interrupt_flag_get(can_instance->can_x, CAN_TSIF_FLAG) != RESET)
  586. {
  587. can_flag_clear(can_instance->can_x, CAN_TSIF_FLAG);
  588. rt_hw_can_isr(can, RT_CAN_EVENT_TX_DONE);
  589. }
  590. }
  591. void _can_err_isr(struct rt_can_device *can)
  592. {
  593. rt_uint32_t errtype;
  594. struct at32_can *can_instance = rt_container_of(can, struct at32_can, device);
  595. errtype = can_instance->can_x->err;
  596. switch (((errtype >> 13) & 0x7))
  597. {
  598. case RT_CAN_BUS_BIT_PAD_ERR:
  599. can_instance->device.status.bitpaderrcnt++;
  600. break;
  601. case RT_CAN_BUS_FORMAT_ERR:
  602. can_instance->device.status.formaterrcnt++;
  603. break;
  604. case RT_CAN_BUS_ACK_ERR:
  605. can_instance->device.status.ackerrcnt++;
  606. rt_hw_can_isr(&can_instance->device, RT_CAN_EVENT_TX_FAIL);
  607. case RT_CAN_BUS_IMPLICIT_BIT_ERR:
  608. case RT_CAN_BUS_EXPLICIT_BIT_ERR:
  609. can_instance->device.status.biterrcnt++;
  610. break;
  611. case RT_CAN_BUS_CRC_ERR:
  612. can_instance->device.status.crcerrcnt++;
  613. break;
  614. }
  615. can_instance->device.status.rcverrcnt = errtype >> 24;
  616. can_instance->device.status.snderrcnt = (errtype >> 16 & 0xFF);
  617. can_instance->device.status.lasterrtype = (errtype >> 13 & 0x7);
  618. can_flag_clear(can_instance->can_x, CAN_BEIF_FLAG);
  619. }
  620. #ifdef BSP_USING_CAN1
  621. void CAN1_TX_IRQHandler(void) {
  622. rt_interrupt_enter();
  623. _can_tx_isr(&can_config[CAN1_INDEX].device);
  624. rt_interrupt_leave();
  625. }
  626. void CAN1_RX_IRQHandler(void)
  627. {
  628. /* enter interrupt */
  629. rt_interrupt_enter();
  630. _can_rx_isr(&can_config[CAN1_INDEX].device);
  631. /* leave interrupt */
  632. rt_interrupt_leave();
  633. }
  634. void CAN1_ERR_IRQHandler(void)
  635. {
  636. /* enter interrupt */
  637. rt_interrupt_enter();
  638. _can_err_isr(&can_config[CAN1_INDEX].device);
  639. /* leave interrupt */
  640. rt_interrupt_leave();
  641. }
  642. #endif
  643. #ifdef BSP_USING_CAN2
  644. void CAN2_TX_IRQHandler(void) {
  645. rt_interrupt_enter();
  646. _can_tx_isr(&can_config[CAN2_INDEX].device);
  647. rt_interrupt_leave();
  648. }
  649. void CAN2_RX_IRQHandler(void)
  650. {
  651. /* enter interrupt */
  652. rt_interrupt_enter();
  653. _can_rx_isr(&can_config[CAN2_INDEX].device);
  654. /* leave interrupt */
  655. rt_interrupt_leave();
  656. }
  657. void CAN2_ERR_IRQHandler(void)
  658. {
  659. /* enter interrupt */
  660. rt_interrupt_enter();
  661. _can_err_isr(&can_config[CAN2_INDEX].device);
  662. /* leave interrupt */
  663. rt_interrupt_leave();
  664. }
  665. #endif
  666. #ifdef BSP_USING_CAN3
  667. void CAN3_TX_IRQHandler(void) {
  668. rt_interrupt_enter();
  669. _can_tx_isr(&can_config[CAN3_INDEX].device);
  670. rt_interrupt_leave();
  671. }
  672. void CAN3_RX_IRQHandler(void)
  673. {
  674. /* enter interrupt */
  675. rt_interrupt_enter();
  676. _can_rx_isr(&can_config[CAN3_INDEX].device);
  677. /* leave interrupt */
  678. rt_interrupt_leave();
  679. }
  680. void CAN3_ERR_IRQHandler(void)
  681. {
  682. /* enter interrupt */
  683. rt_interrupt_enter();
  684. _can_err_isr(&can_config[CAN3_INDEX].device);
  685. /* leave interrupt */
  686. rt_interrupt_leave();
  687. }
  688. #endif
  689. int rt_hw_can_init(void)
  690. {
  691. rt_size_t obj_num;
  692. int index;
  693. obj_num = sizeof(can_config) / sizeof(struct at32_can);
  694. struct can_configure config = CANDEFAULTCONFIG;
  695. config.privmode = RT_CAN_MODE_NOPRIV;
  696. config.ticks = 50;
  697. #ifdef RT_CAN_USING_HDR
  698. config.maxhdr = 16;
  699. #endif
  700. for (index = 0; index < obj_num; index++) {
  701. /* config default filter */
  702. can_filter_config_type filter_config;
  703. can_filter_default_para_init(&filter_config);
  704. can_config[index].filter_init_struct = filter_config;
  705. can_config[index].device.config = config;
  706. rt_hw_can_register(&can_config[index].device,
  707. can_config[index].name,
  708. &_can_ops,
  709. &can_config[index]);
  710. }
  711. return 0;
  712. }
  713. INIT_BOARD_EXPORT(rt_hw_can_init);
  714. #endif /* BSP_USING_CAN_V2 */