drv_can.c 39 KB

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