drv_can.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-05-16 shelton first version
  9. * 2023-01-31 shelton add support f425
  10. * 2023-04-08 shelton add support f423
  11. */
  12. #include "drv_can.h"
  13. #ifdef BSP_USING_CAN
  14. #define LOG_TAG "drv_can"
  15. #include <drv_log.h>
  16. #ifdef SOC_SERIES_AT32F403A
  17. /* attention !!! baud calculation example: apbclk / ((ss + bs1 + bs2) * brp), ep: 120 / ((1 + 8 + 3) * 10) = 1MHz*/
  18. /* attention !!! default apbclk 120 mhz */
  19. static const struct at32_baud_rate can_baud_rate_tab[] =
  20. {
  21. {CAN1MBaud, {10 , CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  22. {CAN800kBaud, {15 , CAN_RSAW_2TQ, CAN_BTS1_7TQ, CAN_BTS2_2TQ}},
  23. {CAN500kBaud, {20 , CAN_RSAW_2TQ, CAN_BTS1_9TQ, CAN_BTS2_2TQ}},
  24. {CAN250kBaud, {40 , CAN_RSAW_2TQ, CAN_BTS1_9TQ, CAN_BTS2_2TQ}},
  25. {CAN125kBaud, {80 , CAN_RSAW_2TQ, CAN_BTS1_9TQ, CAN_BTS2_2TQ}},
  26. {CAN100kBaud, {75 , CAN_RSAW_2TQ, CAN_BTS1_13TQ, CAN_BTS2_2TQ}},
  27. {CAN50kBaud, {150, CAN_RSAW_2TQ, CAN_BTS1_13TQ, CAN_BTS2_2TQ}},
  28. {CAN20kBaud, {375, CAN_RSAW_2TQ, CAN_BTS1_13TQ, CAN_BTS2_2TQ}},
  29. {CAN10kBaud, {750, CAN_RSAW_2TQ, CAN_BTS1_13TQ, CAN_BTS2_2TQ}}
  30. };
  31. #endif
  32. #ifdef SOC_SERIES_AT32F407
  33. /* attention !!! baud calculation example: apbclk / ((ss + bs1 + bs2) * brp), ep: 100 / ((1 + 7 + 2) * 10) = 1MHz*/
  34. /* attention !!! default apbclk 100 mhz */
  35. static const struct at32_baud_rate can_baud_rate_tab[] =
  36. {
  37. {CAN1MBaud, {10 , CAN_RSAW_3TQ, CAN_BTS1_7TQ, CAN_BTS2_2TQ}},
  38. {CAN800kBaud, {25, CAN_RSAW_1TQ, CAN_BTS1_3TQ, CAN_BTS2_1TQ}},
  39. {CAN500kBaud, {10, CAN_RSAW_3TQ, CAN_BTS1_16TQ, CAN_BTS2_3TQ}},
  40. {CAN250kBaud, {20, CAN_RSAW_3TQ, CAN_BTS1_16TQ, CAN_BTS2_3TQ}},
  41. {CAN125kBaud, {40, CAN_RSAW_3TQ, CAN_BTS1_16TQ, CAN_BTS2_3TQ}},
  42. {CAN100kBaud, {50, CAN_RSAW_3TQ, CAN_BTS1_16TQ, CAN_BTS2_3TQ}},
  43. {CAN50kBaud, {100, CAN_RSAW_2TQ, CAN_BTS1_16TQ, CAN_BTS2_3TQ}},
  44. {CAN20kBaud, {250, CAN_RSAW_2TQ, CAN_BTS1_16TQ, CAN_BTS2_3TQ}},
  45. {CAN10kBaud, {500, CAN_RSAW_2TQ, CAN_BTS1_16TQ, CAN_BTS2_3TQ}}
  46. };
  47. #endif
  48. #ifdef SOC_SERIES_AT32F413
  49. /* attention !!! baud calculation example: apbclk / ((ss + bs1 + bs2) * brp), ep: 96 / ((1 + 8 + 3) * 8) = 1MHz*/
  50. /* attention !!! default apbclk 96 mhz */
  51. static const struct at32_baud_rate can_baud_rate_tab[] =
  52. {
  53. {CAN1MBaud, {8 , CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  54. {CAN800kBaud, {20, CAN_RSAW_1TQ, CAN_BTS1_3TQ, CAN_BTS2_2TQ}},
  55. {CAN500kBaud, {16, CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  56. {CAN250kBaud, {32, CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  57. {CAN125kBaud, {64, CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  58. {CAN100kBaud, {160, CAN_RSAW_1TQ, CAN_BTS1_3TQ, CAN_BTS2_2TQ}},
  59. {CAN50kBaud, {320, CAN_RSAW_1TQ, CAN_BTS1_3TQ, CAN_BTS2_2TQ}},
  60. {CAN20kBaud, {800, CAN_RSAW_1TQ, CAN_BTS1_3TQ, CAN_BTS2_2TQ}},
  61. {CAN10kBaud, {800, CAN_RSAW_1TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  62. };
  63. #endif
  64. #ifdef SOC_SERIES_AT32F415
  65. /* attention !!! baud calculation example: apbclk / ((ss + bs1 + bs2) * brp), ep: 72 / ((1 + 8 + 3) * 10) = 1MHz*/
  66. /* attention !!! default apbclk 72 mhz */
  67. static const struct at32_baud_rate can_baud_rate_tab[] =
  68. {
  69. {CAN1MBaud, {6 , CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  70. {CAN800kBaud, {10 , CAN_RSAW_2TQ, CAN_BTS1_6TQ, CAN_BTS2_2TQ}},
  71. {CAN500kBaud, {12 , CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  72. {CAN250kBaud, {24 , CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  73. {CAN125kBaud, {48 , CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  74. {CAN100kBaud, {60 , CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  75. {CAN50kBaud, {120, CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  76. {CAN20kBaud, {300, CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  77. {CAN10kBaud, {600, CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}}
  78. };
  79. #endif
  80. #ifdef SOC_SERIES_AT32F423
  81. /* attention !!! baud calculation example: apbclk / ((ss + bs1 + bs2) * brp), ep: 72 / ((1 + 8 + 3) * 10) = 1MHz*/
  82. /* attention !!! default apbclk 72 mhz */
  83. static const struct at32_baud_rate can_baud_rate_tab[] =
  84. {
  85. {CAN1MBaud, {6 , CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  86. {CAN800kBaud, {10 , CAN_RSAW_2TQ, CAN_BTS1_6TQ, CAN_BTS2_2TQ}},
  87. {CAN500kBaud, {12 , CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  88. {CAN250kBaud, {24 , CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  89. {CAN125kBaud, {48 , CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  90. {CAN100kBaud, {60 , CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  91. {CAN50kBaud, {120, CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  92. {CAN20kBaud, {300, CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  93. {CAN10kBaud, {600, CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}}
  94. };
  95. #endif
  96. #ifdef SOC_SERIES_AT32F425
  97. /* attention !!! baud calculation example: apbclk / ((ss + bs1 + bs2) * brp), ep: 96 / ((1 + 8 + 3) * 8) = 1MHz*/
  98. /* attention !!! default apbclk 96 mhz */
  99. static const struct at32_baud_rate can_baud_rate_tab[] =
  100. {
  101. {CAN1MBaud, {8 , CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  102. {CAN800kBaud, {10 , CAN_RSAW_2TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  103. {CAN500kBaud, {16 , CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  104. {CAN250kBaud, {32 , CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  105. {CAN125kBaud, {64 , CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  106. {CAN100kBaud, {80 , CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  107. {CAN50kBaud, {160, CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  108. {CAN20kBaud, {400, CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  109. {CAN10kBaud, {800, CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}}
  110. };
  111. #endif
  112. #ifdef SOC_SERIES_AT32F435
  113. /* attention !!! baud calculation example: apbclk / ((ss + bs1 + bs2) * brp), ep: 144 / ((1 + 8 + 3) * 12) = 1MHz*/
  114. /* attention !!! default apbclk 144 mhz */
  115. static const struct at32_baud_rate can_baud_rate_tab[] =
  116. {
  117. {CAN1MBaud, {12 , CAN_RSAW_3TQ, CAN_BTS1_8TQ, CAN_BTS2_3TQ}},
  118. {CAN800kBaud, {18 , CAN_RSAW_2TQ, CAN_BTS1_7TQ, CAN_BTS2_2TQ}},
  119. {CAN500kBaud, {24 , CAN_RSAW_2TQ, CAN_BTS1_9TQ, CAN_BTS2_2TQ}},
  120. {CAN250kBaud, {48 , CAN_RSAW_2TQ, CAN_BTS1_9TQ, CAN_BTS2_2TQ}},
  121. {CAN125kBaud, {96 , CAN_RSAW_2TQ, CAN_BTS1_9TQ, CAN_BTS2_2TQ}},
  122. {CAN100kBaud, {90 , CAN_RSAW_2TQ, CAN_BTS1_13TQ, CAN_BTS2_2TQ}},
  123. {CAN50kBaud, {180, CAN_RSAW_2TQ, CAN_BTS1_13TQ, CAN_BTS2_2TQ}},
  124. {CAN20kBaud, {450, CAN_RSAW_2TQ, CAN_BTS1_13TQ, CAN_BTS2_2TQ}},
  125. {CAN10kBaud, {900, CAN_RSAW_2TQ, CAN_BTS1_13TQ, CAN_BTS2_2TQ}}
  126. };
  127. #endif
  128. #ifdef SOC_SERIES_AT32F437
  129. /* attention !!! baud calculation example: apbclk / ((ss + bs1 + bs2) * brp), ep: 125 / ((1 + 3 + 1) * 25) = 1MHz*/
  130. /* attention !!! default apbclk 125 mhz */
  131. static const struct at32_baud_rate can_baud_rate_tab[] =
  132. {
  133. {CAN1MBaud, {25 , CAN_RSAW_1TQ, CAN_BTS1_3TQ, CAN_BTS2_1TQ}},
  134. //none
  135. {CAN500kBaud, {25 , CAN_RSAW_2TQ, CAN_BTS1_7TQ, CAN_BTS2_2TQ}},
  136. {CAN250kBaud, {25 , CAN_RSAW_3TQ, CAN_BTS1_16TQ, CAN_BTS2_3TQ}},
  137. {CAN125kBaud, {50 , CAN_RSAW_2TQ, CAN_BTS1_16TQ, CAN_BTS2_3TQ}},
  138. {CAN100kBaud, {125, CAN_RSAW_1TQ, CAN_BTS1_8TQ, CAN_BTS2_1TQ}},
  139. {CAN50kBaud, {125, CAN_RSAW_2TQ, CAN_BTS1_16TQ, CAN_BTS2_3TQ}},
  140. {CAN20kBaud, {625, CAN_RSAW_1TQ, CAN_BTS1_8TQ, CAN_BTS2_1TQ}},
  141. {CAN10kBaud, {625, CAN_RSAW_2TQ, CAN_BTS1_16TQ, CAN_BTS2_3TQ}}
  142. };
  143. #endif
  144. #if defined (SOC_SERIES_AT32F425)
  145. #define CAN1_RX0_IRQ_NUM CAN1_IRQn
  146. #define CAN1_RX1_IRQ_NUM CAN1_IRQn
  147. #define CAN1_TX_IRQ_NUM CAN1_IRQn
  148. #define CAN1_SE_IRQ_NUM CAN1_IRQn
  149. #elif defined (SOC_SERIES_AT32F415) || defined (SOC_SERIES_AT32F435) || \
  150. defined (SOC_SERIES_AT32F437) || defined (SOC_SERIES_AT32F423)
  151. #define CAN1_RX0_IRQ_NUM CAN1_RX0_IRQn
  152. #define CAN1_RX1_IRQ_NUM CAN1_RX1_IRQn
  153. #define CAN1_TX_IRQ_NUM CAN1_TX_IRQn
  154. #define CAN1_SE_IRQ_NUM CAN1_SE_IRQn
  155. #define CAN1_RX0_IRQ_HANDLER CAN1_RX0_IRQHandler
  156. #define CAN1_RX1_IRQ_HANDLER CAN1_RX1_IRQHandler
  157. #define CAN1_TX_IRQ_HANDLER CAN1_TX_IRQHandler
  158. #define CAN1_SE_IRQ_HANDLER CAN1_SE_IRQHandler
  159. #else
  160. #define CAN1_RX0_IRQ_NUM USBFS_L_CAN1_RX0_IRQn
  161. #define CAN1_RX1_IRQ_NUM CAN1_RX1_IRQn
  162. #define CAN1_TX_IRQ_NUM USBFS_H_CAN1_TX_IRQn
  163. #define CAN1_SE_IRQ_NUM CAN1_SE_IRQn
  164. #define CAN1_RX0_IRQ_HANDLER USBFS_L_CAN1_RX0_IRQHandler
  165. #define CAN1_RX1_IRQ_HANDLER CAN1_RX1_IRQHandler
  166. #define CAN1_TX_IRQ_HANDLER USBFS_H_CAN1_TX_IRQHandler
  167. #define CAN1_SE_IRQ_HANDLER CAN1_SE_IRQHandler
  168. #endif
  169. #ifdef BSP_USING_CAN1
  170. static struct at32_can can_instance1 =
  171. {
  172. .name = "can1",
  173. .config.can_x = CAN1,
  174. };
  175. #endif
  176. #ifdef BSP_USING_CAN2
  177. static struct at32_can can_instance2 =
  178. {
  179. .name = "can2",
  180. .config.can_x = CAN2,
  181. };
  182. #endif
  183. static rt_uint32_t get_can_baud_index(rt_uint32_t baud)
  184. {
  185. rt_uint32_t len, index;
  186. len = sizeof(can_baud_rate_tab) / sizeof(can_baud_rate_tab[0]);
  187. for (index = 0; index < len; index++)
  188. {
  189. if (can_baud_rate_tab[index].baud_rate == baud)
  190. return index;
  191. }
  192. /* default baud is CAN1MBaud */
  193. return 0;
  194. }
  195. static rt_err_t _can_config(struct rt_can_device *can, struct can_configure *cfg)
  196. {
  197. struct at32_can *can_instance;
  198. rt_uint32_t baud_index;
  199. RT_ASSERT(can);
  200. RT_ASSERT(cfg);
  201. can_instance = (struct at32_can *)can->parent.user_data;
  202. RT_ASSERT(can_instance);
  203. at32_msp_can_init((void *)can_instance->config.can_x);
  204. baud_index = get_can_baud_index(cfg->baud_rate);
  205. /* get baudrate parameters */
  206. can_baudrate_default_para_init(&can_instance->config.baudrate_init_struct);
  207. can_instance->config.baudrate_init_struct.rsaw_size = can_baud_rate_tab[baud_index].baud_struct.rsaw_size;
  208. can_instance->config.baudrate_init_struct.bts1_size = can_baud_rate_tab[baud_index].baud_struct.bts1_size;
  209. can_instance->config.baudrate_init_struct.bts2_size = can_baud_rate_tab[baud_index].baud_struct.bts2_size;
  210. can_instance->config.baudrate_init_struct.baudrate_div = can_baud_rate_tab[baud_index].baud_struct.baudrate_div;
  211. /* config can baudrate */
  212. if(can_baudrate_set(can_instance->config.can_x, &(can_instance->config.baudrate_init_struct)) != SUCCESS)
  213. {
  214. return -RT_ERROR;
  215. }
  216. /* config can base parameters */
  217. can_default_para_init(&(can_instance->config.base_init_struct));
  218. switch (cfg->mode)
  219. {
  220. case RT_CAN_MODE_NORMAL:
  221. can_instance->config.base_init_struct.mode_selection = CAN_MODE_COMMUNICATE;
  222. break;
  223. case RT_CAN_MODE_LISTEN:
  224. can_instance->config.base_init_struct.mode_selection = CAN_MODE_LISTENONLY;
  225. break;
  226. case RT_CAN_MODE_LOOPBACK:
  227. can_instance->config.base_init_struct.mode_selection = CAN_MODE_LOOPBACK;
  228. break;
  229. case RT_CAN_MODE_LOOPBACKANLISTEN:
  230. can_instance->config.base_init_struct.mode_selection = CAN_MODE_LISTENONLY_LOOPBACK;
  231. break;
  232. }
  233. can_instance->config.base_init_struct.aebo_enable = TRUE;
  234. can_instance->config.base_init_struct.aed_enable = TRUE;
  235. can_instance->config.base_init_struct.prsf_enable = FALSE;
  236. can_instance->config.base_init_struct.mdrsel_selection = CAN_DISCARDING_FIRST_RECEIVED;
  237. can_instance->config.base_init_struct.mmssr_selection = CAN_SENDING_BY_REQUEST;
  238. /* init can base function */
  239. if (can_base_init(can_instance->config.can_x, &(can_instance->config.base_init_struct)) != SUCCESS)
  240. {
  241. return -RT_ERROR;
  242. }
  243. /* config filter parameters */
  244. can_filter_init(can_instance->config.can_x, &can_instance->config.filter_init_struct);
  245. return RT_EOK;
  246. }
  247. static rt_err_t _can_control(struct rt_can_device *can, int cmd, void *arg)
  248. {
  249. rt_uint32_t argval;
  250. struct at32_can *can_instance;
  251. struct rt_can_filter_config *filter_cfg;
  252. RT_ASSERT(can != RT_NULL);
  253. can_instance = (struct at32_can *)can->parent.user_data;
  254. RT_ASSERT(can_instance != RT_NULL);
  255. switch (cmd)
  256. {
  257. case RT_DEVICE_CTRL_CLR_INT:
  258. argval = (rt_uint32_t) arg;
  259. if (argval == RT_DEVICE_FLAG_INT_RX)
  260. {
  261. if (CAN1 == can_instance->config.can_x)
  262. {
  263. nvic_irq_disable(CAN1_RX0_IRQ_NUM);
  264. nvic_irq_disable(CAN1_RX1_IRQ_NUM);
  265. }
  266. #if defined (CAN2)
  267. if (CAN2 == can_instance->config.can_x)
  268. {
  269. nvic_irq_disable(CAN2_RX0_IRQn);
  270. nvic_irq_disable(CAN2_RX1_IRQn);
  271. }
  272. #endif
  273. /* disable interrupt */
  274. can_interrupt_enable(can_instance->config.can_x, CAN_RF0MIEN_INT, FALSE);
  275. can_interrupt_enable(can_instance->config.can_x, CAN_RF0FIEN_INT, FALSE);
  276. can_interrupt_enable(can_instance->config.can_x, CAN_RF0OIEN_INT, FALSE);
  277. can_interrupt_enable(can_instance->config.can_x, CAN_RF1MIEN_INT, FALSE);
  278. can_interrupt_enable(can_instance->config.can_x, CAN_RF1FIEN_INT, FALSE);
  279. can_interrupt_enable(can_instance->config.can_x, CAN_RF1OIEN_INT, FALSE);
  280. }
  281. else if (argval == RT_DEVICE_FLAG_INT_TX)
  282. {
  283. if (CAN1 == can_instance->config.can_x)
  284. {
  285. nvic_irq_disable(CAN1_TX_IRQ_NUM);
  286. }
  287. #if defined (CAN2)
  288. if (CAN2 == can_instance->config.can_x)
  289. {
  290. nvic_irq_disable(CAN2_TX_IRQn);
  291. }
  292. #endif
  293. can_interrupt_enable(can_instance->config.can_x, CAN_TCIEN_INT, FALSE);
  294. }
  295. else if (argval == RT_DEVICE_CAN_INT_ERR)
  296. {
  297. if (CAN1 == can_instance->config.can_x)
  298. {
  299. nvic_irq_disable(CAN1_SE_IRQ_NUM);
  300. }
  301. #if defined (CAN2)
  302. if (CAN2 == can_instance->config.can_x)
  303. {
  304. nvic_irq_disable(CAN2_SE_IRQn);
  305. }
  306. #endif
  307. can_interrupt_enable(can_instance->config.can_x, CAN_EAIEN_INT, FALSE);
  308. can_interrupt_enable(can_instance->config.can_x, CAN_EPIEN_INT, FALSE);
  309. can_interrupt_enable(can_instance->config.can_x, CAN_BOIEN_INT, FALSE);
  310. can_interrupt_enable(can_instance->config.can_x, CAN_ETRIEN_INT, FALSE);
  311. can_interrupt_enable(can_instance->config.can_x, CAN_EOIEN_INT, FALSE);
  312. }
  313. break;
  314. case RT_DEVICE_CTRL_SET_INT:
  315. argval = (rt_uint32_t) arg;
  316. if (argval == RT_DEVICE_FLAG_INT_RX)
  317. {
  318. can_interrupt_enable(can_instance->config.can_x, CAN_RF0MIEN_INT, TRUE);
  319. can_interrupt_enable(can_instance->config.can_x, CAN_RF0FIEN_INT, TRUE);
  320. can_interrupt_enable(can_instance->config.can_x, CAN_RF0OIEN_INT, TRUE);
  321. can_interrupt_enable(can_instance->config.can_x, CAN_RF1MIEN_INT, TRUE);
  322. can_interrupt_enable(can_instance->config.can_x, CAN_RF1FIEN_INT, TRUE);
  323. can_interrupt_enable(can_instance->config.can_x, CAN_RF1OIEN_INT, TRUE);
  324. if (CAN1 == can_instance->config.can_x)
  325. {
  326. nvic_irq_enable(CAN1_RX0_IRQ_NUM, 1, 0);
  327. nvic_irq_enable(CAN1_RX1_IRQ_NUM, 1, 0);
  328. }
  329. #if defined (CAN2)
  330. if (CAN2 == can_instance->config.can_x)
  331. {
  332. nvic_irq_enable(CAN2_RX0_IRQn, 1, 0);
  333. nvic_irq_enable(CAN2_RX1_IRQn, 1, 0);
  334. }
  335. #endif
  336. }
  337. else if (argval == RT_DEVICE_FLAG_INT_TX)
  338. {
  339. can_interrupt_enable(can_instance->config.can_x, CAN_TCIEN_INT, TRUE);
  340. if (CAN1 == can_instance->config.can_x)
  341. {
  342. nvic_irq_enable(CAN1_TX_IRQ_NUM, 1, 0);
  343. }
  344. #if defined (CAN2)
  345. if (CAN2 == can_instance->config.can_x)
  346. {
  347. nvic_irq_enable(CAN2_TX_IRQn, 1, 0);
  348. }
  349. #endif
  350. }
  351. else if (argval == RT_DEVICE_CAN_INT_ERR)
  352. {
  353. can_interrupt_enable(can_instance->config.can_x, CAN_EAIEN_INT, TRUE);
  354. can_interrupt_enable(can_instance->config.can_x, CAN_EPIEN_INT, TRUE);
  355. can_interrupt_enable(can_instance->config.can_x, CAN_BOIEN_INT, TRUE);
  356. can_interrupt_enable(can_instance->config.can_x, CAN_ETRIEN_INT, TRUE);
  357. can_interrupt_enable(can_instance->config.can_x, CAN_EOIEN_INT, TRUE);
  358. if (CAN1 == can_instance->config.can_x)
  359. {
  360. nvic_irq_enable(CAN1_SE_IRQ_NUM, 1, 0);
  361. }
  362. #if defined (CAN2)
  363. if (CAN2 == can_instance->config.can_x)
  364. {
  365. nvic_irq_enable(CAN2_SE_IRQn, 1, 0);
  366. }
  367. #endif
  368. }
  369. break;
  370. case RT_CAN_CMD_SET_FILTER:
  371. {
  372. rt_uint32_t id_h = 0;
  373. rt_uint32_t id_l = 0;
  374. rt_uint32_t mask_h = 0;
  375. rt_uint32_t mask_l = 0;
  376. rt_uint32_t mask_l_tail = 0;
  377. if (RT_NULL == arg)
  378. {
  379. /* default filter config */
  380. can_filter_init(can_instance->config.can_x, &can_instance->config.filter_init_struct);
  381. }
  382. else
  383. {
  384. filter_cfg = (struct rt_can_filter_config *)arg;
  385. /* get default filter */
  386. for (int i = 0; i < filter_cfg->count; i++)
  387. {
  388. if (filter_cfg->items[i].hdr_bank == -1)
  389. {
  390. can_instance->config.filter_init_struct.filter_number = i;
  391. }
  392. else
  393. {
  394. can_instance->config.filter_init_struct.filter_number = filter_cfg->items[i].hdr_bank;
  395. }
  396. /**
  397. * ID | CAN_FxR1[31:24] | CAN_FxR1[23:16] | CAN_FxR1[15:8] | CAN_FxR1[7:0] |
  398. * MASK | CAN_FxR2[31:24] | CAN_FxR1[23:16] | CAN_FxR1[15:8] | CAN_FxR1[7:0] |
  399. * STD ID | STID[10:3] | STDID[2:0] |<- 21bit ->|
  400. * EXT ID | EXTID[28:21] | EXTID[20:13] | EXTID[12:5] | EXTID[4:0] IDE RTR 0|
  401. * @note the 32bit STD ID must << 21 to fill CAN_FxR1[31:21] and EXT ID must << 3,
  402. * -> but the id bit of struct rt_can_filter_item is 29,
  403. * -> so STD id << 18 and EXT id Don't need << 3, when get the high 16bit.
  404. * -> FilterIdHigh : (((STDid << 18) or (EXT id)) >> 13) & 0xFFFF,
  405. * -> FilterIdLow: ((STDid << 18) or (EXT id << 3)) & 0xFFFF.
  406. * @note the mask bit of struct rt_can_filter_item is 32,
  407. * -> FilterMaskIdHigh: (((STD mask << 21) or (EXT mask <<3)) >> 16) & 0xFFFF
  408. * -> FilterMaskIdLow: ((STD mask << 21) or (EXT mask <<3)) & 0xFFFF
  409. */
  410. if (filter_cfg->items[i].mode == CAN_FILTER_MODE_ID_MASK)
  411. {
  412. mask_l_tail = 0x06;
  413. }
  414. else if (filter_cfg->items[i].mode == CAN_FILTER_MODE_ID_LIST)
  415. {
  416. mask_l_tail = (filter_cfg->items[i].ide << 2) |
  417. (filter_cfg->items[i].rtr << 1);
  418. }
  419. if (filter_cfg->items[i].ide == RT_CAN_STDID)
  420. {
  421. id_h = ((filter_cfg->items[i].id << 18) >> 13) & 0xFFFF;
  422. id_l = ((filter_cfg->items[i].id << 18) |
  423. (filter_cfg->items[i].ide << 2) |
  424. (filter_cfg->items[i].rtr << 1)) & 0xFFFF;
  425. mask_h = ((filter_cfg->items[i].mask << 21) >> 16) & 0xFFFF;
  426. mask_l = ((filter_cfg->items[i].mask << 21) | mask_l_tail) & 0xFFFF;
  427. }
  428. else if (filter_cfg->items[i].ide == RT_CAN_EXTID)
  429. {
  430. id_h = (filter_cfg->items[i].id >> 13) & 0xFFFF;
  431. id_l = ((filter_cfg->items[i].id << 3) |
  432. (filter_cfg->items[i].ide << 2) |
  433. (filter_cfg->items[i].rtr << 1)) & 0xFFFF;
  434. mask_h = ((filter_cfg->items[i].mask << 3) >> 16) & 0xFFFF;
  435. mask_l = ((filter_cfg->items[i].mask << 3) | mask_l_tail) & 0xFFFF;
  436. }
  437. can_instance->config.filter_init_struct.filter_id_high = id_h;
  438. can_instance->config.filter_init_struct.filter_id_low = id_l;
  439. can_instance->config.filter_init_struct.filter_mask_high = mask_h;
  440. can_instance->config.filter_init_struct.filter_mask_low = mask_l;
  441. can_instance->config.filter_init_struct.filter_mode = (can_filter_mode_type)filter_cfg->items[i].mode;
  442. /* filter conf */
  443. can_filter_init(can_instance->config.can_x, &can_instance->config.filter_init_struct);
  444. }
  445. }
  446. break;
  447. }
  448. case RT_CAN_CMD_SET_MODE:
  449. argval = (rt_uint32_t) arg;
  450. if (argval != RT_CAN_MODE_NORMAL &&
  451. argval != RT_CAN_MODE_LISTEN &&
  452. argval != RT_CAN_MODE_LOOPBACK &&
  453. argval != RT_CAN_MODE_LOOPBACKANLISTEN)
  454. {
  455. return -RT_ERROR;
  456. }
  457. if (argval != can_instance->device.config.mode)
  458. {
  459. can_instance->device.config.mode = argval;
  460. return _can_config(&can_instance->device, &can_instance->device.config);
  461. }
  462. break;
  463. case RT_CAN_CMD_SET_BAUD:
  464. argval = (rt_uint32_t) arg;
  465. if (argval != CAN1MBaud &&
  466. argval != CAN800kBaud &&
  467. argval != CAN500kBaud &&
  468. argval != CAN250kBaud &&
  469. argval != CAN125kBaud &&
  470. argval != CAN100kBaud &&
  471. argval != CAN50kBaud &&
  472. argval != CAN20kBaud &&
  473. argval != CAN10kBaud)
  474. {
  475. return -RT_ERROR;
  476. }
  477. if (argval != can_instance->device.config.baud_rate)
  478. {
  479. can_instance->device.config.baud_rate = argval;
  480. return _can_config(&can_instance->device, &can_instance->device.config);
  481. }
  482. break;
  483. case RT_CAN_CMD_SET_PRIV:
  484. argval = (rt_uint32_t) arg;
  485. if (argval != RT_CAN_MODE_PRIV &&
  486. argval != RT_CAN_MODE_NOPRIV)
  487. {
  488. return -RT_ERROR;
  489. }
  490. if (argval != can_instance->device.config.privmode)
  491. {
  492. can_instance->device.config.privmode = argval;
  493. return _can_config(&can_instance->device, &can_instance->device.config);
  494. }
  495. break;
  496. case RT_CAN_CMD_GET_STATUS:
  497. {
  498. rt_uint32_t errtype;
  499. errtype = can_instance->config.can_x->ests;
  500. can_instance->device.status.rcverrcnt = errtype >> 24;
  501. can_instance->device.status.snderrcnt = (errtype >> 16 & 0xFF);
  502. can_instance->device.status.lasterrtype = errtype & 0x70;
  503. can_instance->device.status.errcode = errtype & 0x07;
  504. rt_memcpy(arg, &can_instance->device.status, sizeof(can_instance->device.status));
  505. }
  506. break;
  507. }
  508. return RT_EOK;
  509. }
  510. static int _can_sendmsg(struct rt_can_device *can, const void *buf, rt_uint32_t box_num)
  511. {
  512. struct can_config *hcan;
  513. hcan = &((struct at32_can *) can->parent.user_data)->config;
  514. struct rt_can_msg *pmsg = (struct rt_can_msg *) buf;
  515. can_tx_message_type tx_message;
  516. /* check select mailbox is empty */
  517. switch (box_num)
  518. {
  519. case CAN_TX_MAILBOX0:
  520. if (hcan->can_x->tsts_bit.tm0ef != 1)
  521. {
  522. /* return function status */
  523. return -RT_ERROR;
  524. }
  525. break;
  526. case CAN_TX_MAILBOX1:
  527. if (hcan->can_x->tsts_bit.tm1ef != 1)
  528. {
  529. /* return function status */
  530. return -RT_ERROR;
  531. }
  532. break;
  533. case CAN_TX_MAILBOX2:
  534. if (hcan->can_x->tsts_bit.tm2ef != 1)
  535. {
  536. /* return function status */
  537. return -RT_ERROR;
  538. }
  539. break;
  540. default:
  541. RT_ASSERT(0);
  542. break;
  543. }
  544. if (RT_CAN_STDID == pmsg->ide)
  545. {
  546. tx_message.id_type = CAN_ID_STANDARD;
  547. tx_message.standard_id = pmsg->id;
  548. }
  549. else
  550. {
  551. tx_message.id_type = CAN_ID_EXTENDED;
  552. tx_message.extended_id = pmsg->id;
  553. }
  554. if (RT_CAN_DTR == pmsg->rtr)
  555. {
  556. tx_message.frame_type = CAN_TFT_DATA;
  557. }
  558. else
  559. {
  560. tx_message.frame_type = CAN_TFT_REMOTE;
  561. }
  562. /* set up the dlc */
  563. tx_message.dlc = pmsg->len & 0x0FU;
  564. /* set up the data field */
  565. tx_message.data[0] = (uint32_t)pmsg->data[0];
  566. tx_message.data[1] = (uint32_t)pmsg->data[1];
  567. tx_message.data[2] = (uint32_t)pmsg->data[2];
  568. tx_message.data[3] = (uint32_t)pmsg->data[3];
  569. tx_message.data[4] = (uint32_t)pmsg->data[4];
  570. tx_message.data[5] = (uint32_t)pmsg->data[5];
  571. tx_message.data[6] = (uint32_t)pmsg->data[6];
  572. tx_message.data[7] = (uint32_t)pmsg->data[7];
  573. can_message_transmit(hcan->can_x, &tx_message);
  574. return RT_EOK;
  575. }
  576. static int _can_recvmsg(struct rt_can_device *can, void *buf, rt_uint32_t fifo)
  577. {
  578. struct can_config *hcan;
  579. hcan = &((struct at32_can *) can->parent.user_data)->config;
  580. struct rt_can_msg *pmsg = (struct rt_can_msg *) buf;
  581. can_rx_message_type rx_message;
  582. RT_ASSERT(can);
  583. /* get data */
  584. can_message_receive(hcan->can_x, (can_rx_fifo_num_type)fifo, &rx_message);
  585. pmsg->data[0] = rx_message.data[0];
  586. pmsg->data[1] = rx_message.data[1];
  587. pmsg->data[2] = rx_message.data[2];
  588. pmsg->data[3] = rx_message.data[3];
  589. pmsg->data[4] = rx_message.data[4];
  590. pmsg->data[5] = rx_message.data[5];
  591. pmsg->data[6] = rx_message.data[6];
  592. pmsg->data[7] = rx_message.data[7];
  593. pmsg->len = rx_message.dlc;
  594. if (rx_message.id_type == CAN_ID_STANDARD)
  595. {
  596. pmsg->id = rx_message.standard_id;
  597. pmsg->ide = RT_CAN_STDID;
  598. }
  599. else
  600. {
  601. pmsg->id = rx_message.extended_id;
  602. pmsg->ide = RT_CAN_EXTID;
  603. }
  604. pmsg->rtr = rx_message.frame_type;
  605. pmsg->hdr_index = rx_message.filter_index;
  606. return RT_EOK;
  607. }
  608. static const struct rt_can_ops _can_ops =
  609. {
  610. _can_config,
  611. _can_control,
  612. _can_sendmsg,
  613. _can_recvmsg,
  614. };
  615. static void _can_rx_isr(struct rt_can_device *can, rt_uint32_t fifo)
  616. {
  617. struct can_config *hcan;
  618. RT_ASSERT(can);
  619. hcan = &((struct at32_can *) can->parent.user_data)->config;
  620. switch (fifo)
  621. {
  622. case CAN_RX_FIFO0:
  623. /* save to user list */
  624. if (can_receive_message_pending_get(hcan->can_x, CAN_RX_FIFO0) && \
  625. can_flag_get(hcan->can_x, CAN_RF0MN_FLAG))
  626. {
  627. rt_hw_can_isr(can, RT_CAN_EVENT_RX_IND | fifo << 8);
  628. }
  629. /* check full flag for fifo0 */
  630. if (can_flag_get(hcan->can_x, CAN_RF0FF_FLAG) == SET)
  631. {
  632. /* clear fifo0 full flag */
  633. can_flag_clear(hcan->can_x, CAN_RF0FF_FLAG);
  634. }
  635. /* check overrun flag for fifo0 */
  636. if (can_flag_get(hcan->can_x, CAN_RF0OF_FLAG) == SET)
  637. {
  638. /* clear fifo0 overrun flag */
  639. can_flag_clear(hcan->can_x, CAN_RF0OF_FLAG);
  640. rt_hw_can_isr(can, RT_CAN_EVENT_RXOF_IND | fifo << 8);
  641. }
  642. break;
  643. case CAN_RX_FIFO1:
  644. /* save to user list */
  645. if (can_receive_message_pending_get(hcan->can_x, CAN_RX_FIFO1) && \
  646. can_flag_get(hcan->can_x, CAN_RF1MN_FLAG))
  647. {
  648. rt_hw_can_isr(can, RT_CAN_EVENT_RX_IND | fifo << 8);
  649. }
  650. /* check full flag for fifo1 */
  651. if (can_flag_get(hcan->can_x, CAN_RF1FF_FLAG) == SET)
  652. {
  653. /* clear fifo1 full flag */
  654. can_flag_clear(hcan->can_x, CAN_RF1FF_FLAG);
  655. }
  656. /* check overrun flag for fifo1 */
  657. if (can_flag_get(hcan->can_x, CAN_RF1OF_FLAG) == SET)
  658. {
  659. /* clear fifo1 overrun flag */
  660. can_flag_clear(hcan->can_x, CAN_RF1OF_FLAG);
  661. rt_hw_can_isr(can, RT_CAN_EVENT_RXOF_IND | fifo << 8);
  662. }
  663. break;
  664. }
  665. }
  666. #ifdef BSP_USING_CAN1
  667. /**
  668. * @brief this function handles can1 tx interrupts. transmit fifo0/1/2 is empty can trigger this interrupt
  669. */
  670. void CAN1_TX_IRQ_HANDLER(void)
  671. {
  672. rt_interrupt_enter();
  673. struct can_config *hcan;
  674. hcan = &can_instance1.config;
  675. if (can_flag_get(hcan->can_x, CAN_TM0TCF_FLAG) == SET)
  676. {
  677. if (hcan->can_x->tsts_bit.tm0tsf == 1)
  678. {
  679. rt_hw_can_isr(&can_instance1.device, RT_CAN_EVENT_TX_DONE | 0 << 8);
  680. }
  681. else
  682. {
  683. rt_hw_can_isr(&can_instance1.device, RT_CAN_EVENT_TX_FAIL | 0 << 8);
  684. }
  685. /* write 0 to clear transmission status flag */
  686. can_flag_clear(hcan->can_x, CAN_TM0TCF_FLAG);
  687. }
  688. else if (can_flag_get(hcan->can_x, CAN_TM1TCF_FLAG) == SET)
  689. {
  690. if (hcan->can_x->tsts_bit.tm1tsf == 1)
  691. {
  692. rt_hw_can_isr(&can_instance1.device, RT_CAN_EVENT_TX_DONE | 1 << 8);
  693. }
  694. else
  695. {
  696. rt_hw_can_isr(&can_instance1.device, RT_CAN_EVENT_TX_FAIL | 1 << 8);
  697. }
  698. /* write 0 to clear transmission status flag */
  699. can_flag_clear(hcan->can_x, CAN_TM1TCF_FLAG);
  700. }
  701. else if (can_flag_get(hcan->can_x, CAN_TM2TCF_FLAG) == SET)
  702. {
  703. if (hcan->can_x->tsts_bit.tm2tsf == 1)
  704. {
  705. rt_hw_can_isr(&can_instance1.device, RT_CAN_EVENT_TX_DONE | 2 << 8);
  706. }
  707. else
  708. {
  709. rt_hw_can_isr(&can_instance1.device, RT_CAN_EVENT_TX_FAIL | 2 << 8);
  710. }
  711. /* write 0 to clear transmission status flag */
  712. can_flag_clear(hcan->can_x, CAN_TM2TCF_FLAG);
  713. }
  714. rt_interrupt_leave();
  715. }
  716. /**
  717. * @brief this function handles can1 rx0 interrupts.
  718. */
  719. void CAN1_RX0_IRQ_HANDLER(void)
  720. {
  721. rt_interrupt_enter();
  722. _can_rx_isr(&can_instance1.device, CAN_RX_FIFO0);
  723. rt_interrupt_leave();
  724. }
  725. /**
  726. * @brief this function handles can1 rx1 interrupts.
  727. */
  728. void CAN1_RX1_IRQ_HANDLER(void)
  729. {
  730. rt_interrupt_enter();
  731. _can_rx_isr(&can_instance1.device, CAN_RX_FIFO1);
  732. rt_interrupt_leave();
  733. }
  734. /**
  735. * @brief this function handles can1 sce interrupts.
  736. */
  737. void CAN1_SE_IRQ_HANDLER(void)
  738. {
  739. rt_uint32_t errtype;
  740. struct can_config *hcan;
  741. hcan = &can_instance1.config;
  742. errtype = hcan->can_x->ests;
  743. rt_interrupt_enter();
  744. switch ((errtype & 0x70) >> 4)
  745. {
  746. case RT_CAN_BUS_BIT_PAD_ERR:
  747. can_instance1.device.status.bitpaderrcnt++;
  748. break;
  749. case RT_CAN_BUS_FORMAT_ERR:
  750. can_instance1.device.status.formaterrcnt++;
  751. break;
  752. case RT_CAN_BUS_ACK_ERR:/* attention !!! test ack err's unit is transmit unit */
  753. can_instance1.device.status.ackerrcnt++;
  754. if (!(can_instance1.config.can_x->tsts_bit.tm0tsf == 1))
  755. rt_hw_can_isr(&can_instance1.device, RT_CAN_EVENT_TX_FAIL | 0 << 8);
  756. else if (!(can_instance1.config.can_x->tsts_bit.tm1tsf == 1))
  757. rt_hw_can_isr(&can_instance1.device, RT_CAN_EVENT_TX_FAIL | 1 << 8);
  758. else if (!(can_instance1.config.can_x->tsts_bit.tm2tsf == 1))
  759. rt_hw_can_isr(&can_instance1.device, RT_CAN_EVENT_TX_FAIL | 2 << 8);
  760. break;
  761. case RT_CAN_BUS_IMPLICIT_BIT_ERR:
  762. case RT_CAN_BUS_EXPLICIT_BIT_ERR:
  763. can_instance1.device.status.biterrcnt++;
  764. break;
  765. case RT_CAN_BUS_CRC_ERR:
  766. can_instance1.device.status.crcerrcnt++;
  767. break;
  768. }
  769. can_instance1.device.status.lasterrtype = errtype & 0x70;
  770. can_instance1.device.status.rcverrcnt = errtype >> 24;
  771. can_instance1.device.status.snderrcnt = (errtype >> 16 & 0xFF);
  772. can_instance1.device.status.errcode = errtype & 0x07;
  773. /* clear error flags */
  774. can_flag_clear(hcan->can_x, CAN_ETR_FLAG);
  775. rt_interrupt_leave();
  776. }
  777. #endif
  778. #if defined (SOC_SERIES_AT32F425)
  779. void CAN1_IRQHandler(void)
  780. {
  781. CAN1_TX_IRQ_HANDLER();
  782. CAN1_RX0_IRQ_HANDLER();
  783. CAN1_RX1_IRQ_HANDLER();
  784. CAN1_SE_IRQ_HANDLER();
  785. }
  786. #endif
  787. #ifdef BSP_USING_CAN2
  788. /**
  789. * @brief this function handles can2 tx interrupts.
  790. */
  791. void CAN2_TX_IRQHandler(void)
  792. {
  793. rt_interrupt_enter();
  794. struct can_config *hcan;
  795. hcan = &can_instance2.config;
  796. if (can_flag_get(hcan->can_x, CAN_TM0TCF_FLAG) == SET)
  797. {
  798. if (hcan->can_x->tsts_bit.tm0tsf == 1)
  799. {
  800. rt_hw_can_isr(&can_instance2.device, RT_CAN_EVENT_TX_DONE | 0 << 8);
  801. }
  802. else
  803. {
  804. rt_hw_can_isr(&can_instance2.device, RT_CAN_EVENT_TX_FAIL | 0 << 8);
  805. }
  806. /* write 0 to clear transmission status flag rqcpx */
  807. can_flag_clear(hcan->can_x, CAN_TM0TCF_FLAG);
  808. }
  809. else if (can_flag_get(hcan->can_x, CAN_TM1TCF_FLAG) == SET)
  810. {
  811. if (hcan->can_x->tsts_bit.tm1tsf == 1)
  812. {
  813. rt_hw_can_isr(&can_instance2.device, RT_CAN_EVENT_TX_DONE | 1 << 8);
  814. }
  815. else
  816. {
  817. rt_hw_can_isr(&can_instance2.device, RT_CAN_EVENT_TX_FAIL | 1 << 8);
  818. }
  819. /* write 0 to clear transmission status flag rqcpx */
  820. can_flag_clear(hcan->can_x, CAN_TM1TCF_FLAG);
  821. }
  822. else if (can_flag_get(hcan->can_x, CAN_TM2TCF_FLAG) == SET)
  823. {
  824. if (hcan->can_x->tsts_bit.tm2tsf == 1)
  825. {
  826. rt_hw_can_isr(&can_instance2.device, RT_CAN_EVENT_TX_DONE | 2 << 8);
  827. }
  828. else
  829. {
  830. rt_hw_can_isr(&can_instance2.device, RT_CAN_EVENT_TX_FAIL | 2 << 8);
  831. }
  832. /* write 0 to clear transmission status flag rqcpx */
  833. can_flag_clear(hcan->can_x, CAN_TM2TCF_FLAG);
  834. }
  835. rt_interrupt_leave();
  836. }
  837. /**
  838. * @brief this function handles can2 rx0 interrupts.
  839. */
  840. void CAN2_RX0_IRQHandler(void)
  841. {
  842. rt_interrupt_enter();
  843. _can_rx_isr(&can_instance2.device, CAN_RX_FIFO0);
  844. rt_interrupt_leave();
  845. }
  846. /**
  847. * @brief this function handles can2 rx1 interrupts.
  848. */
  849. void CAN2_RX1_IRQHandler(void)
  850. {
  851. rt_interrupt_enter();
  852. _can_rx_isr(&can_instance2.device, CAN_RX_FIFO1);
  853. rt_interrupt_leave();
  854. }
  855. /**
  856. * @brief this function handles can2 sce interrupts.
  857. */
  858. void CAN2_SE_IRQHandler(void)
  859. {
  860. rt_uint32_t errtype;
  861. struct can_config *hcan;
  862. hcan = &can_instance2.config;
  863. errtype = hcan->can_x->ests;
  864. rt_interrupt_enter();
  865. switch ((errtype & 0x70) >> 4)
  866. {
  867. case RT_CAN_BUS_BIT_PAD_ERR:
  868. can_instance2.device.status.bitpaderrcnt++;
  869. break;
  870. case RT_CAN_BUS_FORMAT_ERR:
  871. can_instance2.device.status.formaterrcnt++;
  872. break;
  873. case RT_CAN_BUS_ACK_ERR:
  874. can_instance2.device.status.ackerrcnt++;
  875. if (!(can_instance2.config.can_x->tsts_bit.tm0tsf == 1))
  876. rt_hw_can_isr(&can_instance2.device, RT_CAN_EVENT_TX_FAIL | 0 << 8);
  877. else if (!(can_instance2.config.can_x->tsts_bit.tm1tsf == 1))
  878. rt_hw_can_isr(&can_instance2.device, RT_CAN_EVENT_TX_FAIL | 1 << 8);
  879. else if (!(can_instance2.config.can_x->tsts_bit.tm2tsf == 1))
  880. rt_hw_can_isr(&can_instance2.device, RT_CAN_EVENT_TX_FAIL | 2 << 8);
  881. break;
  882. case RT_CAN_BUS_IMPLICIT_BIT_ERR:
  883. case RT_CAN_BUS_EXPLICIT_BIT_ERR:
  884. can_instance2.device.status.biterrcnt++;
  885. break;
  886. case RT_CAN_BUS_CRC_ERR:
  887. can_instance2.device.status.crcerrcnt++;
  888. break;
  889. }
  890. can_instance2.device.status.lasterrtype = errtype & 0x70;
  891. can_instance2.device.status.rcverrcnt = errtype >> 24;
  892. can_instance2.device.status.snderrcnt = (errtype >> 16 & 0xFF);
  893. can_instance2.device.status.errcode = errtype & 0x07;
  894. /* clear error flags */
  895. can_flag_clear(hcan->can_x, CAN_ETR_FLAG);
  896. rt_interrupt_leave();
  897. }
  898. #endif
  899. int rt_hw_can_init(void)
  900. {
  901. struct can_configure config = CANDEFAULTCONFIG;
  902. config.privmode = RT_CAN_MODE_NOPRIV;
  903. config.ticks = 50;
  904. #ifdef RT_CAN_USING_HDR
  905. config.maxhdr = 14;
  906. #endif
  907. /* config default filter */
  908. can_filter_init_type filter_conf;
  909. can_filter_default_para_init(&filter_conf);
  910. filter_conf.filter_activate_enable = TRUE;
  911. filter_conf.filter_bit = CAN_FILTER_32BIT;
  912. #ifdef BSP_USING_CAN1
  913. filter_conf.filter_number = 0;
  914. can_instance1.config.filter_init_struct = filter_conf;
  915. can_instance1.device.config = config;
  916. /* register can1 device */
  917. rt_hw_can_register(&can_instance1.device,
  918. can_instance1.name,
  919. &_can_ops,
  920. &can_instance1);
  921. #endif /* BSP_USING_CAN1 */
  922. #ifdef BSP_USING_CAN2
  923. filter_conf.filter_number = 0;
  924. can_instance2.config.filter_init_struct = filter_conf;
  925. can_instance2.device.config = config;
  926. /* register can2 device */
  927. rt_hw_can_register(&can_instance2.device,
  928. can_instance2.name,
  929. &_can_ops,
  930. &can_instance2);
  931. #endif /* BSP_USING_CAN2 */
  932. return 0;
  933. }
  934. INIT_BOARD_EXPORT(rt_hw_can_init);
  935. #endif /* BSP_USING_CAN */