drv_can.c 35 KB

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