drv_can.c 38 KB

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