bxcan.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578
  1. /*
  2. * File : bxcan.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2015, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2015-05-14 aubrcool@qq.com first version
  13. */
  14. #include <rthw.h>
  15. #include <rtdevice.h>
  16. #include <board.h>
  17. #include <bxcan.h>
  18. #ifdef RT_USING_COMPONENTS_INIT
  19. #include <components.h>
  20. #endif
  21. #ifdef RT_USING_CAN
  22. #ifndef STM32F10X_CL
  23. #define BX_CAN_FMRNUMBER 14
  24. #define BX_CAN2_FMRSTART 7
  25. #else
  26. #define BX_CAN_FMRNUMBER 28
  27. #define BX_CAN2_FMRSTART 14
  28. #endif
  29. #ifdef STM32F10X_HD
  30. #undef USING_BXCAN2
  31. #define CAN1_RX0_IRQn USB_LP_CAN1_RX0_IRQn
  32. #define CAN1_TX_IRQn USB_HP_CAN1_TX_IRQn
  33. #endif
  34. #define BX_CAN_MAX_FILTERS (BX_CAN_FMRNUMBER * 4)
  35. #define BX_CAN_MAX_FILTER_MASKS BX_CAN_MAX_FILTERS
  36. #define BX_CAN_FILTER_MAX_ARRAY_SIZE ((BX_CAN_MAX_FILTERS + 32 - 1) / 32)
  37. struct stm_bxcanfiltermap
  38. {
  39. rt_uint32_t id32mask_cnt;
  40. rt_uint32_t id32bit_cnt;
  41. rt_uint32_t id16mask_cnt;
  42. rt_uint32_t id16bit_cnt;
  43. };
  44. struct stm_bxcanfilter_masks
  45. {
  46. rt_uint32_t id32maskm[BX_CAN_FILTER_MAX_ARRAY_SIZE];
  47. rt_uint32_t id32bitm[BX_CAN_FILTER_MAX_ARRAY_SIZE];
  48. rt_uint32_t id16maskm[BX_CAN_FILTER_MAX_ARRAY_SIZE];
  49. rt_uint32_t id16bitm[BX_CAN_FILTER_MAX_ARRAY_SIZE];
  50. rt_uint32_t id32maskshift[2];
  51. rt_uint32_t id32bitshift[2];
  52. rt_uint32_t id16maskshift[2];
  53. rt_uint32_t id16bitshift[2];
  54. };
  55. struct stm_bxcan
  56. {
  57. CAN_TypeDef *reg;
  58. void *mfrbase;
  59. IRQn_Type sndirq;
  60. IRQn_Type rcvirq0;
  61. IRQn_Type rcvirq1;
  62. IRQn_Type errirq;
  63. struct stm_bxcanfilter_masks filtermask;
  64. rt_uint32_t alocmask[BX_CAN_FILTER_MAX_ARRAY_SIZE];
  65. const rt_uint32_t filtercnt;
  66. const rt_uint32_t fifo1filteroff;
  67. const struct stm_bxcanfiltermap filtermap[2];
  68. };
  69. static void calcfiltermasks(struct stm_bxcan *pbxcan);
  70. static void bxcan1_filter_init(struct rt_can_device *can)
  71. {
  72. rt_uint32_t i;
  73. rt_uint32_t mask;
  74. struct stm_bxcan *pbxcan = (struct stm_bxcan *) can->parent.user_data;
  75. for (i = 0; i < BX_CAN2_FMRSTART; i++)
  76. {
  77. CAN1->FMR |= FMR_FINIT;
  78. mask = 0x01 << (i + 0);
  79. if (i < pbxcan->fifo1filteroff)
  80. {
  81. if (pbxcan->filtermap[0].id32mask_cnt && i < pbxcan->filtermap[0].id32mask_cnt)
  82. {
  83. CAN1->FS1R |= mask;
  84. CAN1->FM1R &= ~mask;
  85. CAN1->FFA1R &= ~mask;
  86. }
  87. else if (pbxcan->filtermap[0].id32bit_cnt &&
  88. i < pbxcan->filtermap[0].id32mask_cnt + pbxcan->filtermap[0].id32bit_cnt / 2)
  89. {
  90. CAN1->FS1R |= mask;
  91. CAN1->FM1R |= mask;
  92. CAN1->FFA1R &= ~mask;
  93. }
  94. else if (pbxcan->filtermap[0].id16mask_cnt &&
  95. i < pbxcan->filtermap[0].id32mask_cnt + pbxcan->filtermap[0].id32bit_cnt / 2
  96. + pbxcan->filtermap[0].id16mask_cnt / 2)
  97. {
  98. CAN1->FS1R &= ~mask;
  99. CAN1->FM1R &= ~mask;
  100. CAN1->FFA1R &= ~mask;
  101. }
  102. else if (pbxcan->filtermap[0].id16bit_cnt &&
  103. i < pbxcan->filtermap[0].id32mask_cnt + pbxcan->filtermap[0].id32bit_cnt / 2
  104. + pbxcan->filtermap[0].id16mask_cnt / 2 + pbxcan->filtermap[0].id16bit_cnt / 4
  105. )
  106. {
  107. CAN1->FS1R &= ~mask;
  108. CAN1->FM1R |= mask;
  109. CAN1->FFA1R &= ~mask;
  110. }
  111. }
  112. else
  113. {
  114. if (pbxcan->filtermap[1].id32mask_cnt &&
  115. i < pbxcan->filtermap[1].id32mask_cnt + pbxcan->fifo1filteroff)
  116. {
  117. CAN1->FS1R |= mask;
  118. CAN1->FM1R &= ~mask;
  119. CAN1->FFA1R |= mask;
  120. }
  121. else if (pbxcan->filtermap[1].id32bit_cnt &&
  122. i < pbxcan->filtermap[1].id32mask_cnt + pbxcan->filtermap[1].id32bit_cnt / 2
  123. + pbxcan->fifo1filteroff)
  124. {
  125. CAN1->FS1R |= mask;
  126. CAN1->FM1R |= mask;
  127. CAN1->FFA1R |= mask;
  128. }
  129. else if (pbxcan->filtermap[1].id16mask_cnt &&
  130. i < pbxcan->filtermap[1].id32mask_cnt + pbxcan->filtermap[1].id32bit_cnt / 2
  131. + pbxcan->filtermap[1].id16mask_cnt / 2 + pbxcan->fifo1filteroff)
  132. {
  133. CAN1->FS1R &= ~mask;
  134. CAN1->FM1R &= ~mask;
  135. CAN1->FFA1R |= mask;
  136. }
  137. else if (pbxcan->filtermap[1].id16bit_cnt &&
  138. i < pbxcan->filtermap[1].id32mask_cnt + pbxcan->filtermap[1].id32bit_cnt / 2
  139. + pbxcan->filtermap[1].id16mask_cnt / 2 + pbxcan->filtermap[1].id16bit_cnt / 4
  140. + pbxcan->fifo1filteroff)
  141. {
  142. CAN1->FS1R &= ~mask;
  143. CAN1->FM1R |= mask;
  144. CAN1->FFA1R |= mask;
  145. }
  146. }
  147. CAN1->sFilterRegister[i].FR1 = 0xFFFFFFFF;
  148. CAN1->sFilterRegister[i].FR2 = 0xFFFFFFFF;
  149. CAN1->FMR &= ~FMR_FINIT;
  150. }
  151. calcfiltermasks(pbxcan);
  152. }
  153. #ifdef USING_BXCAN2
  154. static void bxcan2_filter_init(struct rt_can_device *can)
  155. {
  156. rt_uint32_t i;
  157. rt_uint32_t off;
  158. rt_uint32_t mask;
  159. CAN_SlaveStartBank(BX_CAN2_FMRSTART);
  160. struct stm_bxcan *pbxcan = (struct stm_bxcan *) can->parent.user_data;
  161. for (i = BX_CAN2_FMRSTART; i < BX_CAN_FMRNUMBER; i++)
  162. {
  163. CAN1->FMR |= FMR_FINIT;
  164. mask = 0x01 << (i + 0);
  165. off = i - BX_CAN2_FMRSTART;
  166. if (off < pbxcan->fifo1filteroff)
  167. {
  168. if (pbxcan->filtermap[0].id32mask_cnt && off < pbxcan->filtermap[0].id32mask_cnt)
  169. {
  170. CAN1->FS1R |= mask;
  171. CAN1->FM1R &= ~mask;
  172. CAN1->FFA1R &= ~mask;
  173. }
  174. else if (pbxcan->filtermap[0].id32bit_cnt &&
  175. off < pbxcan->filtermap[0].id32mask_cnt + pbxcan->filtermap[0].id32bit_cnt / 2)
  176. {
  177. CAN1->FS1R |= mask;
  178. CAN1->FM1R |= mask;
  179. CAN1->FFA1R &= ~mask;
  180. }
  181. else if (pbxcan->filtermap[0].id16mask_cnt &&
  182. off < pbxcan->filtermap[0].id32mask_cnt + pbxcan->filtermap[0].id32bit_cnt / 2
  183. + pbxcan->filtermap[0].id16mask_cnt / 2)
  184. {
  185. CAN1->FS1R &= ~mask;
  186. CAN1->FM1R &= ~mask;
  187. CAN1->FFA1R &= ~mask;
  188. }
  189. else if (pbxcan->filtermap[0].id16bit_cnt &&
  190. off < pbxcan->filtermap[0].id32mask_cnt + pbxcan->filtermap[0].id32bit_cnt / 2
  191. + pbxcan->filtermap[0].id16mask_cnt / 2 + pbxcan->filtermap[0].id16bit_cnt / 4
  192. )
  193. {
  194. CAN1->FS1R &= ~mask;
  195. CAN1->FM1R |= mask;
  196. CAN1->FFA1R &= ~mask;
  197. }
  198. }
  199. else
  200. {
  201. if (pbxcan->filtermap[1].id32mask_cnt &&
  202. off < pbxcan->filtermap[1].id32mask_cnt + pbxcan->fifo1filteroff)
  203. {
  204. CAN1->FS1R |= mask;
  205. CAN1->FM1R &= ~mask;
  206. CAN1->FFA1R |= mask;
  207. }
  208. else if (pbxcan->filtermap[1].id32bit_cnt &&
  209. off < pbxcan->filtermap[1].id32mask_cnt + pbxcan->filtermap[1].id32bit_cnt / 2
  210. + pbxcan->fifo1filteroff)
  211. {
  212. CAN1->FS1R |= mask;
  213. CAN1->FM1R |= mask;
  214. CAN1->FFA1R |= mask;
  215. }
  216. else if (pbxcan->filtermap[1].id16mask_cnt &&
  217. off < pbxcan->filtermap[1].id32mask_cnt + pbxcan->filtermap[1].id32bit_cnt / 2
  218. + pbxcan->filtermap[1].id16mask_cnt / 2 + pbxcan->fifo1filteroff)
  219. {
  220. CAN1->FS1R &= ~mask;
  221. CAN1->FM1R &= ~mask;
  222. CAN1->FFA1R |= mask;
  223. }
  224. else if (pbxcan->filtermap[1].id16bit_cnt &&
  225. off < pbxcan->filtermap[1].id32mask_cnt + pbxcan->filtermap[1].id32bit_cnt / 2
  226. + pbxcan->filtermap[1].id16mask_cnt / 2 + pbxcan->filtermap[1].id16bit_cnt / 4
  227. + pbxcan->fifo1filteroff)
  228. {
  229. CAN1->FS1R &= ~mask;
  230. CAN1->FM1R |= mask;
  231. CAN1->FFA1R |= mask;
  232. }
  233. }
  234. CAN1->sFilterRegister[i].FR1 = 0xFFFFFFFF;
  235. CAN1->sFilterRegister[i].FR2 = 0xFFFFFFFF;
  236. CAN1->FMR &= ~FMR_FINIT;
  237. }
  238. calcfiltermasks(pbxcan);
  239. }
  240. #endif
  241. #define BS1SHIFT 16
  242. #define BS2SHIFT 20
  243. #define RRESCLSHIFT 0
  244. #define SJWSHIFT 24
  245. #define BS1MASK ( (0x0F) << BS1SHIFT )
  246. #define BS2MASK ( (0x07) << BS2SHIFT )
  247. #define RRESCLMASK ( 0x3FF << RRESCLSHIFT )
  248. #define SJWMASK ( 0x3 << SJWSHIFT )
  249. #define MK_BKCAN_BAUD(SJW,BS1,BS2,PRES) \
  250. ((SJW << SJWSHIFT) | (BS1 << BS1SHIFT) | (BS2 << BS2SHIFT) | (PRES << RRESCLSHIFT))
  251. static const rt_uint32_t bxcan_baud_rate_tab[] =
  252. {
  253. // 48 M
  254. MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 3),
  255. MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_6tq, CAN_BS2_3tq, 6),
  256. MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 5),
  257. MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 11),
  258. MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 23),
  259. MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 29),
  260. MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 59),
  261. MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_14tq, CAN_BS2_3tq, 149),
  262. MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_16tq, CAN_BS2_8tq, 199),
  263. };
  264. #define BAUD_DATA(TYPE,NO) \
  265. ((bxcan_baud_rate_tab[NO] & TYPE##MASK) >> TYPE##SHIFT)
  266. static void bxcan_init(CAN_TypeDef *pcan, rt_uint32_t baud, rt_uint32_t mode)
  267. {
  268. CAN_InitTypeDef CAN_InitStructure;
  269. CAN_InitStructure.CAN_TTCM = DISABLE;
  270. CAN_InitStructure.CAN_ABOM = ENABLE;
  271. CAN_InitStructure.CAN_AWUM = DISABLE;
  272. CAN_InitStructure.CAN_NART = DISABLE;
  273. CAN_InitStructure.CAN_RFLM = DISABLE;
  274. CAN_InitStructure.CAN_TXFP = ENABLE;
  275. switch (mode)
  276. {
  277. case RT_CAN_MODE_NORMAL:
  278. CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
  279. break;
  280. case RT_CAN_MODE_LISEN:
  281. CAN_InitStructure.CAN_Mode = CAN_Mode_Silent;
  282. break;
  283. case RT_CAN_MODE_LOOPBACK:
  284. CAN_InitStructure.CAN_Mode = CAN_Mode_LoopBack;
  285. break;
  286. case RT_CAN_MODE_LOOPBACKANLISEN:
  287. CAN_InitStructure.CAN_Mode = CAN_Mode_Silent_LoopBack;
  288. break;
  289. }
  290. CAN_InitStructure.CAN_SJW = BAUD_DATA(SJW, baud);
  291. CAN_InitStructure.CAN_BS1 = BAUD_DATA(BS1, baud);
  292. CAN_InitStructure.CAN_BS2 = BAUD_DATA(BS2, baud);
  293. CAN_InitStructure.CAN_Prescaler = BAUD_DATA(RRESCL, baud);
  294. CAN_Init(pcan, &CAN_InitStructure);
  295. }
  296. static void bxcan1_hw_init(void)
  297. {
  298. GPIO_InitTypeDef GPIO_InitStructure;
  299. NVIC_InitTypeDef NVIC_InitStructure;
  300. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  301. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  302. GPIO_Init(GPIOA, &GPIO_InitStructure);
  303. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  304. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  305. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  306. GPIO_Init(GPIOA, &GPIO_InitStructure);
  307. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  308. NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
  309. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x1;
  310. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
  311. NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
  312. NVIC_Init(&NVIC_InitStructure);
  313. NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX1_IRQn;
  314. NVIC_Init(&NVIC_InitStructure);
  315. NVIC_InitStructure.NVIC_IRQChannel = CAN1_TX_IRQn;
  316. NVIC_Init(&NVIC_InitStructure);
  317. }
  318. #ifdef USING_BXCAN2
  319. static void bxcan2_hw_init(void)
  320. {
  321. GPIO_InitTypeDef GPIO_InitStructure;
  322. NVIC_InitTypeDef NVIC_InitStructure;
  323. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  324. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  325. GPIO_Init(GPIOB, &GPIO_InitStructure);
  326. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
  327. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  328. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  329. GPIO_Init(GPIOB, &GPIO_InitStructure);
  330. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  331. NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn;
  332. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x1;
  333. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
  334. NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
  335. NVIC_Init(&NVIC_InitStructure);
  336. NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX1_IRQn;
  337. NVIC_Init(&NVIC_InitStructure);
  338. NVIC_InitStructure.NVIC_IRQChannel = CAN2_TX_IRQn;
  339. NVIC_Init(&NVIC_InitStructure);
  340. }
  341. #endif
  342. static inline rt_err_t bxcan_enter_init(CAN_TypeDef *pcan)
  343. {
  344. uint32_t wait_ack = 0x00000000;
  345. pcan->MCR |= CAN_MCR_INRQ ;
  346. while (((pcan->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) && (wait_ack != INAK_TIMEOUT))
  347. {
  348. wait_ack++;
  349. }
  350. if ((pcan->MSR & CAN_MSR_INAK) != CAN_MSR_INAK)
  351. {
  352. return RT_ERROR;
  353. }
  354. return RT_EOK;
  355. }
  356. static inline rt_err_t bxcan_exit_init(CAN_TypeDef *pcan)
  357. {
  358. uint32_t wait_ack = 0x00000000;
  359. pcan->MCR &= ~(uint32_t)CAN_MCR_INRQ;
  360. while (((pcan->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) && (wait_ack != INAK_TIMEOUT))
  361. {
  362. wait_ack++;
  363. }
  364. if ((pcan->MSR & CAN_MSR_INAK) != CAN_MSR_INAK)
  365. {
  366. return RT_ERROR;
  367. }
  368. return RT_EOK;
  369. }
  370. static rt_err_t bxcan_set_mode(CAN_TypeDef *pcan, rt_uint32_t mode)
  371. {
  372. if (bxcan_enter_init(pcan) != RT_EOK)
  373. {
  374. return RT_ERROR;
  375. }
  376. pcan->BTR &= ~(uint32_t)((uint32_t)0x03 << 30);
  377. switch (mode)
  378. {
  379. case RT_CAN_MODE_NORMAL:
  380. mode = CAN_Mode_Normal;
  381. break;
  382. case RT_CAN_MODE_LISEN:
  383. mode = CAN_Mode_Silent;
  384. break;
  385. case RT_CAN_MODE_LOOPBACK:
  386. mode = CAN_Mode_LoopBack;
  387. break;
  388. case RT_CAN_MODE_LOOPBACKANLISEN:
  389. mode = CAN_Mode_Silent_LoopBack;
  390. break;
  391. }
  392. pcan->BTR |= ~(uint32_t)(mode << 30);
  393. if (bxcan_exit_init(pcan) != RT_EOK)
  394. {
  395. return RT_ERROR;
  396. }
  397. return RT_EOK;
  398. }
  399. static rt_err_t bxcan_set_privmode(CAN_TypeDef *pcan, rt_uint32_t mode)
  400. {
  401. if (bxcan_enter_init(pcan) != RT_EOK)
  402. {
  403. return RT_ERROR;
  404. }
  405. if (mode == ENABLE)
  406. {
  407. pcan->MCR |= CAN_MCR_TXFP;
  408. }
  409. else
  410. {
  411. pcan->MCR &= ~(uint32_t)CAN_MCR_TXFP;
  412. }
  413. if (bxcan_exit_init(pcan) != RT_EOK)
  414. {
  415. return RT_ERROR;
  416. }
  417. return RT_EOK;
  418. }
  419. static rt_err_t bxcan_set_baud_rate(CAN_TypeDef *pcan, rt_uint32_t baud)
  420. {
  421. rt_uint32_t mode;
  422. if (bxcan_enter_init(pcan) != RT_EOK)
  423. {
  424. return RT_ERROR;
  425. }
  426. pcan->BTR = 0;
  427. mode = pcan->BTR & ((rt_uint32_t)0x03 << 30);
  428. pcan->BTR = (mode | \
  429. ((BAUD_DATA(SJW, baud)) << 24) | \
  430. ((BAUD_DATA(BS1, baud)) << 16) | \
  431. ((BAUD_DATA(BS2, baud)) << 20) | \
  432. (BAUD_DATA(RRESCL, baud)));
  433. if (bxcan_exit_init(pcan) != RT_EOK)
  434. {
  435. return RT_ERROR;
  436. }
  437. return RT_EOK;
  438. }
  439. static rt_err_t bxcancalcbaseoff(struct stm_bxcan *pbxcan, rt_int32_t hdr,
  440. rt_int32_t *pbase, rt_int32_t *poff)
  441. {
  442. rt_uint32_t fifo0start, fifo0end;
  443. rt_uint32_t fifo1start, fifo1end;
  444. rt_uint32_t ptr;
  445. fifo0start = 0;
  446. fifo0end = pbxcan->filtermap[0].id32mask_cnt
  447. + pbxcan->filtermap[0].id32bit_cnt
  448. + pbxcan->filtermap[0].id16mask_cnt
  449. + pbxcan->filtermap[0].id16bit_cnt ;
  450. fifo1start = pbxcan->fifo1filteroff * 4;
  451. fifo1end = pbxcan->filtermap[1].id32mask_cnt
  452. + pbxcan->filtermap[1].id32bit_cnt
  453. + pbxcan->filtermap[1].id16mask_cnt
  454. + pbxcan->filtermap[1].id16bit_cnt ;
  455. if (hdr >= fifo0start && hdr < fifo0end)
  456. {
  457. *pbase = 0;
  458. ptr = 0;
  459. }
  460. else if (hdr >= fifo1start && hdr < fifo1end)
  461. {
  462. *pbase = pbxcan->fifo1filteroff;
  463. ptr = 1;
  464. }
  465. else
  466. {
  467. return RT_ERROR;
  468. }
  469. ptr = 0;
  470. if (hdr > pbxcan->filtermap[ptr].id32mask_cnt)
  471. {
  472. hdr -= pbxcan->filtermap[ptr].id32mask_cnt;
  473. *pbase += pbxcan->filtermap[ptr].id32mask_cnt;
  474. }
  475. else
  476. {
  477. *pbase += hdr;
  478. *poff = 0;
  479. return RT_EOK;
  480. }
  481. if (hdr > pbxcan->filtermap[ptr].id32bit_cnt)
  482. {
  483. hdr -= pbxcan->filtermap[ptr].id32bit_cnt;
  484. *pbase += pbxcan->filtermap[ptr].id32bit_cnt / 2;
  485. }
  486. else
  487. {
  488. *pbase += hdr / 2;
  489. *poff = hdr % 2;
  490. return RT_EOK;
  491. }
  492. if (hdr > pbxcan->filtermap[ptr].id16mask_cnt)
  493. {
  494. hdr -= pbxcan->filtermap[ptr].id16mask_cnt;
  495. *pbase += pbxcan->filtermap[ptr].id16mask_cnt / 2;
  496. }
  497. else
  498. {
  499. *pbase += hdr / 2;
  500. *poff = hdr % 2;
  501. return RT_EOK;
  502. }
  503. if (hdr > pbxcan->filtermap[ptr].id16bit_cnt)
  504. {
  505. return RT_ERROR;
  506. }
  507. else
  508. {
  509. *pbase += hdr / 4;
  510. *poff = hdr % 4;
  511. return RT_EOK;
  512. }
  513. }
  514. static void calcandormask(rt_uint32_t *pmask, rt_uint32_t shift, rt_int32_t count)
  515. {
  516. rt_uint32_t tmpmask;
  517. rt_uint32_t tmpmaskarray[BX_CAN_FILTER_MAX_ARRAY_SIZE] = {0,};
  518. rt_int32_t i;
  519. i = 0;
  520. while (count > 0)
  521. {
  522. if (i >= 32)
  523. {
  524. tmpmaskarray[i] = 0xFFFFFFFF;
  525. }
  526. else
  527. {
  528. tmpmaskarray[i] = (0x01 << count) - 1;
  529. }
  530. count -= 32;
  531. i++;
  532. };
  533. count = i;
  534. for (i = 0; i < count && i < BX_CAN_FILTER_MAX_ARRAY_SIZE; i++)
  535. {
  536. tmpmask = tmpmaskarray[i];
  537. pmask[i] |= (rt_uint32_t)(tmpmask << shift);
  538. if (i < BX_CAN_FILTER_MAX_ARRAY_SIZE - 1)
  539. {
  540. pmask[i + 1] |= (rt_uint32_t)(tmpmask >> (32 - shift));
  541. }
  542. }
  543. }
  544. static void calcfiltermasks(struct stm_bxcan *pbxcan)
  545. {
  546. rt_memset(&pbxcan->filtermask, 0, sizeof(pbxcan->filtermask));
  547. pbxcan->filtermask.id32maskshift[0] = 0;
  548. if (pbxcan->filtermap[0].id32mask_cnt)
  549. {
  550. calcandormask(pbxcan->filtermask.id32maskm, pbxcan->filtermask.id32maskshift[0],
  551. pbxcan->filtermap[0].id32mask_cnt);
  552. }
  553. pbxcan->filtermask.id32maskshift[1] = pbxcan->fifo1filteroff * 4;
  554. if (pbxcan->filtermap[1].id32mask_cnt)
  555. {
  556. calcandormask(pbxcan->filtermask.id32maskm, pbxcan->filtermask.id32maskshift[1],
  557. pbxcan->filtermap[1].id32mask_cnt);
  558. }
  559. pbxcan->filtermask.id32bitshift[0] = pbxcan->filtermask.id32maskshift[0] +
  560. pbxcan->filtermap[0].id32mask_cnt;
  561. if (pbxcan->filtermap[0].id32bit_cnt)
  562. {
  563. calcandormask(pbxcan->filtermask.id32bitm, pbxcan->filtermask.id32bitshift[0],
  564. pbxcan->filtermap[0].id32bit_cnt);
  565. }
  566. pbxcan->filtermask.id32bitshift[1] = pbxcan->filtermask.id32maskshift[1] +
  567. pbxcan->filtermap[1].id32mask_cnt;
  568. if (pbxcan->filtermap[1].id32bit_cnt)
  569. {
  570. calcandormask(pbxcan->filtermask.id32bitm, pbxcan->filtermask.id32bitshift[1],
  571. pbxcan->filtermap[1].id32bit_cnt);
  572. }
  573. pbxcan->filtermask.id16maskshift[0] = pbxcan->filtermask.id32bitshift[0] +
  574. pbxcan->filtermap[0].id32bit_cnt;
  575. if (pbxcan->filtermap[0].id16mask_cnt)
  576. {
  577. calcandormask(pbxcan->filtermask.id16maskm, pbxcan->filtermask.id16maskshift[0],
  578. pbxcan->filtermap[0].id16mask_cnt);
  579. }
  580. pbxcan->filtermask.id16maskshift[1] = pbxcan->filtermask.id32bitshift[1] +
  581. pbxcan->filtermap[1].id32bit_cnt;
  582. if (pbxcan->filtermap[1].id16mask_cnt)
  583. {
  584. calcandormask(pbxcan->filtermask.id16maskm, pbxcan->filtermask.id16maskshift[1],
  585. pbxcan->filtermap[1].id16mask_cnt);
  586. }
  587. pbxcan->filtermask.id16bitshift[0] = pbxcan->filtermask.id16maskshift[0] +
  588. pbxcan->filtermap[0].id16mask_cnt;
  589. if (pbxcan->filtermap[0].id16bit_cnt)
  590. {
  591. calcandormask(pbxcan->filtermask.id16bitm, pbxcan->filtermask.id16bitshift[0],
  592. pbxcan->filtermap[0].id16bit_cnt);
  593. }
  594. pbxcan->filtermask.id16bitshift[1] = pbxcan->filtermask.id16maskshift[1] +
  595. pbxcan->filtermap[1].id16mask_cnt;
  596. if (pbxcan->filtermap[1].id16bit_cnt)
  597. {
  598. calcandormask(pbxcan->filtermask.id16bitm, pbxcan->filtermask.id16bitshift[1],
  599. pbxcan->filtermap[1].id16bit_cnt);
  600. }
  601. }
  602. static rt_int32_t bxcanfindfilter(struct stm_bxcan *pbxcan, struct rt_can_filter_item *pitem,
  603. rt_int32_t type, rt_int32_t *base, rt_int32_t *off)
  604. {
  605. rt_int32_t i;
  606. rt_uint32_t bits, thisid, thismask, shift, found;
  607. CAN_FilterRegister_TypeDef *pfilterreg;
  608. found = 0;
  609. switch (type)
  610. {
  611. case 3:
  612. shift = 3;
  613. for (i = 0; i < BX_CAN_MAX_FILTERS; i++)
  614. {
  615. bits = 0x01 << (i & 0x1F);
  616. if (bits & (pbxcan->filtermask.id32maskm[i >> 5] & pbxcan->alocmask[i >> 5]))
  617. {
  618. bxcancalcbaseoff(pbxcan, i, base, off);
  619. pfilterreg = &((CAN_FilterRegister_TypeDef *)pbxcan->mfrbase)[*base];
  620. thisid = (rt_uint32_t)pitem->id << shift;
  621. thismask = (rt_uint32_t)pitem->mask << shift;
  622. if (pitem->ide)
  623. {
  624. thisid |= CAN_ID_EXT;
  625. thismask |= CAN_ID_EXT;
  626. }
  627. if (pitem->rtr)
  628. {
  629. thisid |= CAN_RTR_REMOTE;
  630. thismask |= CAN_RTR_REMOTE;
  631. }
  632. if (pfilterreg->FR1 == thisid && pfilterreg->FR2 == thismask)
  633. {
  634. found = 1;
  635. break;
  636. }
  637. }
  638. }
  639. break;
  640. case 2:
  641. shift = 3;
  642. for (i = 0; i < BX_CAN_MAX_FILTERS; i++)
  643. {
  644. bits = 0x01 << (i % 32);
  645. if (bits & (pbxcan->filtermask.id32bitm[i >> 5] & pbxcan->alocmask[i >> 5]))
  646. {
  647. bxcancalcbaseoff(pbxcan, i, base, off);
  648. pfilterreg = &((CAN_FilterRegister_TypeDef *)pbxcan->mfrbase)[*base];
  649. thisid = (rt_uint32_t)pitem->id << shift;
  650. if (pitem->ide)
  651. {
  652. thisid |= CAN_ID_EXT;
  653. }
  654. if (pitem->rtr)
  655. {
  656. thisid |= CAN_RTR_REMOTE;
  657. }
  658. if ((*off == 0 && pfilterreg->FR1 == thisid) ||
  659. (*off == 1 && pfilterreg->FR2 == thisid)
  660. )
  661. {
  662. found = 1;
  663. break;
  664. }
  665. }
  666. }
  667. break;
  668. case 1:
  669. shift = 5;
  670. for (i = 0; i < BX_CAN_MAX_FILTERS; i++)
  671. {
  672. bits = 0x01 << (i % 32);
  673. if (bits & (pbxcan->filtermask.id16maskm[i >> 5] & pbxcan->alocmask[i >> 5]))
  674. {
  675. bxcancalcbaseoff(pbxcan, i, base, off);
  676. pfilterreg = &((CAN_FilterRegister_TypeDef *)pbxcan->mfrbase)[*base];
  677. thisid = pitem->id << shift;
  678. if (pitem->rtr)
  679. {
  680. thisid |= CAN_RTR_REMOTE << (shift - 2);
  681. }
  682. thismask = pitem->mask << shift;
  683. if (pitem->rtr)
  684. {
  685. thismask |= CAN_RTR_REMOTE << (shift - 2);
  686. }
  687. if (*off == 0 && pfilterreg->FR1 == ((thisid & 0x0000FFFF) | ((thismask & 0x0000FFFF) << 16)) ||
  688. *off == 1 && pfilterreg->FR2 == ((thisid & 0x0000FFFF) | ((thismask & 0x0000FFFF) << 16))
  689. )
  690. {
  691. found = 1;
  692. break;
  693. }
  694. }
  695. }
  696. break;
  697. case 0:
  698. shift = 5;
  699. for (i = 0; i < BX_CAN_MAX_FILTERS; i++)
  700. {
  701. bits = 0x01 << (i % 32);
  702. if (bits & (pbxcan->filtermask.id16bitm[i >> 5] & pbxcan->alocmask[i >> 5]))
  703. {
  704. bxcancalcbaseoff(pbxcan, i, base, off);
  705. pfilterreg = &((CAN_FilterRegister_TypeDef *)pbxcan->mfrbase)[*base];
  706. thisid = pitem->id << shift;
  707. if (pitem->rtr)
  708. {
  709. thisid |= CAN_RTR_REMOTE << (shift - 2);
  710. }
  711. if (*off < 2 && ((rt_uint16_t *)&pfilterreg->FR1)[*off & 0x01] == thisid ||
  712. *off >= 2 && ((rt_uint16_t *)&pfilterreg->FR2)[*off & 0x01] == thisid)
  713. {
  714. found = 1;
  715. break;
  716. }
  717. }
  718. }
  719. break;
  720. }
  721. if (found)
  722. {
  723. return i;
  724. }
  725. return -1;
  726. }
  727. extern int __rt_ffs(int value);
  728. static rt_err_t bxcanallocfilter(rt_uint32_t *pmask, rt_uint32_t *palocmask,
  729. rt_uint32_t count, rt_int32_t *hdr)
  730. {
  731. rt_int32_t i;
  732. for (i = 0; i < count; i++)
  733. {
  734. rt_enter_critical();
  735. if ((pmask[i] & ~palocmask[i]) != 0)
  736. {
  737. *hdr = __rt_ffs(pmask[i] & ~palocmask[i]) - 1 + i * 32;
  738. palocmask[i] |= 0x01 << (*hdr % 0x1F);
  739. rt_exit_critical();
  740. return RT_EOK;
  741. }
  742. rt_exit_critical();
  743. }
  744. if (i >= count)
  745. {
  746. return RT_ENOMEM;
  747. }
  748. return RT_EOK;
  749. }
  750. static rt_err_t bxcanallocnewfilter(struct stm_bxcan *pbxcan, rt_int32_t actived,
  751. rt_int32_t type, rt_int32_t *hdr, rt_int32_t *base, rt_int32_t *off)
  752. {
  753. rt_err_t res;
  754. *hdr = -1;
  755. switch (type)
  756. {
  757. case 0x03:
  758. res = bxcanallocfilter(pbxcan->filtermask.id32maskm, pbxcan->alocmask,
  759. BX_CAN_FILTER_MAX_ARRAY_SIZE, hdr);
  760. break;
  761. case 0x02:
  762. res = bxcanallocfilter(pbxcan->filtermask.id32bitm, pbxcan->alocmask,
  763. BX_CAN_FILTER_MAX_ARRAY_SIZE, hdr);
  764. break;
  765. case 0x01:
  766. res = bxcanallocfilter(pbxcan->filtermask.id16maskm, pbxcan->alocmask,
  767. BX_CAN_FILTER_MAX_ARRAY_SIZE, hdr);
  768. break;
  769. case 0x00:
  770. res = bxcanallocfilter(pbxcan->filtermask.id16bitm, pbxcan->alocmask,
  771. BX_CAN_FILTER_MAX_ARRAY_SIZE, hdr);
  772. break;
  773. }
  774. if (res != RT_EOK || *hdr < 0)
  775. {
  776. return RT_ENOMEM;
  777. }
  778. bxcancalcbaseoff(pbxcan, *hdr, base, off);
  779. return RT_EOK;
  780. }
  781. static rt_err_t bxmodifyfilter(struct stm_bxcan *pbxcan, struct rt_can_filter_item *pitem, rt_uint32_t actived)
  782. {
  783. rt_int32_t fcase;
  784. rt_err_t res;
  785. rt_int32_t hdr, fbase, foff;
  786. fcase = (pitem->mode | (pitem->ide << 1));
  787. hdr = bxcanfindfilter(pbxcan, pitem, fcase, &fbase, &foff);
  788. if (hdr < 0)
  789. {
  790. if (!actived)
  791. {
  792. return RT_EOK;
  793. }
  794. else if (pitem->hdr == -1)
  795. {
  796. res = bxcanallocnewfilter(pbxcan, actived, fcase, &hdr, &fbase, &foff);
  797. if (res != RT_EOK)
  798. {
  799. return res;
  800. }
  801. }
  802. else if (pitem->hdr >= 0)
  803. {
  804. rt_enter_critical();
  805. res = bxcancalcbaseoff(pbxcan, pitem->hdr, &fbase, &foff);
  806. if (res != RT_EOK)
  807. {
  808. return res;
  809. }
  810. hdr = pitem->hdr;
  811. if (actived)
  812. {
  813. pbxcan->alocmask[hdr >> 5] |= 0x01 << (hdr % 0x1F);
  814. }
  815. rt_exit_critical();
  816. }
  817. }
  818. else
  819. {
  820. if (!actived)
  821. {
  822. pitem->hdr = hdr;
  823. }
  824. else if (hdr >= 0 && (pitem->hdr >= 0 || pitem->hdr == -1))
  825. {
  826. pitem->hdr = hdr;
  827. return RT_EBUSY;
  828. }
  829. }
  830. rt_uint32_t ID[2];
  831. rt_uint32_t shift;
  832. rt_uint32_t thisid;
  833. rt_uint32_t thismask;
  834. CAN_FilterInitTypeDef CAN_FilterInitStructure;
  835. pitem->hdr = hdr;
  836. CAN_FilterRegister_TypeDef *pfilterreg = &((CAN_FilterRegister_TypeDef *)pbxcan->mfrbase)[fbase];
  837. ID[0] = pfilterreg->FR1;
  838. ID[1] = pfilterreg->FR2;
  839. CAN_FilterInitStructure.CAN_FilterNumber = (pfilterreg - &CAN1->sFilterRegister[0]);
  840. if (pitem->mode)
  841. {
  842. CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
  843. }
  844. else
  845. {
  846. CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdList;
  847. }
  848. if (pitem->ide)
  849. {
  850. CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
  851. }
  852. else
  853. {
  854. CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_16bit;
  855. }
  856. switch (fcase)
  857. {
  858. case 0x03:
  859. if (actived)
  860. {
  861. shift = 3;
  862. thisid = (rt_uint32_t)pitem->id << shift;
  863. thismask = (rt_uint32_t)pitem->mask << shift;
  864. if (pitem->ide)
  865. {
  866. thisid |= CAN_ID_EXT;
  867. thismask |= CAN_ID_EXT;
  868. }
  869. if (pitem->rtr)
  870. {
  871. thisid |= CAN_RTR_REMOTE;
  872. thismask |= CAN_RTR_REMOTE;
  873. }
  874. ID[0] = thisid;
  875. ID[1] = thismask;
  876. }
  877. else
  878. {
  879. ID[0] = 0xFFFFFFFF;
  880. ID[1] = 0xFFFFFFFF;
  881. }
  882. break;
  883. case 0x02:
  884. if (actived)
  885. {
  886. shift = 3;
  887. thisid = (rt_uint32_t)pitem->id << shift;
  888. if (pitem->ide)
  889. {
  890. thisid |= CAN_ID_EXT;
  891. }
  892. if (pitem->rtr)
  893. {
  894. thisid |= CAN_RTR_REMOTE;
  895. }
  896. ID[foff] = thisid;
  897. }
  898. else
  899. {
  900. ID[foff] = 0xFFFFFFFF;
  901. }
  902. break;
  903. case 0x01:
  904. if (actived)
  905. {
  906. shift = 5;
  907. thisid = pitem->id << shift;
  908. if (pitem->rtr)
  909. {
  910. thisid |= CAN_RTR_REMOTE << (shift - 2);
  911. }
  912. thismask = pitem->mask << shift;
  913. if (pitem->rtr)
  914. {
  915. thismask |= CAN_RTR_REMOTE << (shift - 2);
  916. }
  917. ID[foff] = (thisid & 0x0000FFFF) | ((thismask & 0x0000FFFF) << 16);
  918. }
  919. else
  920. {
  921. ID[foff] = 0xFFFFFFFF;
  922. }
  923. break;
  924. case 0x00:
  925. if (actived)
  926. {
  927. shift = 5;
  928. thisid = pitem->id << shift;
  929. if (pitem->rtr)
  930. {
  931. thisid |= CAN_RTR_REMOTE << (shift - 2);
  932. }
  933. ((rt_uint16_t *) ID)[foff] = thisid;
  934. }
  935. else
  936. {
  937. ((rt_uint16_t *) ID)[foff] = 0xFFFF;
  938. }
  939. break;
  940. }
  941. CAN_FilterInitStructure.CAN_FilterIdHigh = ((ID[1]) & 0x0000FFFF);
  942. CAN_FilterInitStructure.CAN_FilterIdLow = ID[0] & 0x0000FFFF;
  943. CAN_FilterInitStructure.CAN_FilterMaskIdHigh = (ID[1] & 0xFFFF0000) >> 16;
  944. CAN_FilterInitStructure.CAN_FilterMaskIdLow = (ID[0] & 0xFFFF0000) >> 16;
  945. if (fbase >= pbxcan->fifo1filteroff)
  946. {
  947. CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 1;
  948. }
  949. else
  950. {
  951. CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
  952. }
  953. if (ID[0] != 0xFFFFFFFF || ID[1] != 0xFFFFFFFF)
  954. {
  955. CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
  956. }
  957. else
  958. {
  959. CAN_FilterInitStructure.CAN_FilterActivation = DISABLE;
  960. }
  961. if (!actived)
  962. {
  963. rt_enter_critical();
  964. pbxcan->alocmask[hdr >> 5] &= ~(0x01 << (hdr % 0x1F));
  965. rt_exit_critical();
  966. }
  967. CAN_FilterInit(&CAN_FilterInitStructure);
  968. return RT_EOK;
  969. }
  970. static rt_err_t setfilter(struct stm_bxcan *pbxcan, struct rt_can_filter_config *pconfig)
  971. {
  972. struct rt_can_filter_item *pitem = pconfig->items;
  973. rt_uint32_t count = pconfig->count;
  974. rt_err_t res;
  975. while (count)
  976. {
  977. res = bxmodifyfilter(pbxcan, pitem, pconfig->actived);
  978. if (res != RT_EOK)
  979. {
  980. return res;
  981. }
  982. pitem++;
  983. count--;
  984. }
  985. return RT_EOK;
  986. }
  987. static rt_err_t configure(struct rt_can_device *can, struct can_configure *cfg)
  988. {
  989. CAN_TypeDef *pbxcan;
  990. pbxcan = ((struct stm_bxcan *) can->parent.user_data)->reg;
  991. assert_param(IS_CAN_ALL_PERIPH(pbxcan));
  992. if (pbxcan == CAN1)
  993. {
  994. bxcan1_hw_init();
  995. bxcan_init(pbxcan, cfg->baud_rate, can->config.mode);
  996. bxcan1_filter_init(can);
  997. }
  998. else
  999. {
  1000. #ifdef USING_BXCAN2
  1001. bxcan2_hw_init();
  1002. bxcan_init(pbxcan, cfg->baud_rate, can->config.mode);
  1003. bxcan2_filter_init(can);
  1004. #endif
  1005. }
  1006. return RT_EOK;
  1007. }
  1008. static rt_err_t control(struct rt_can_device *can, int cmd, void *arg)
  1009. {
  1010. struct stm_bxcan *pbxcan;
  1011. rt_uint32_t argval;
  1012. NVIC_InitTypeDef NVIC_InitStructure;
  1013. pbxcan = (struct stm_bxcan *) can->parent.user_data;
  1014. assert_param(pbxcan != RT_NULL);
  1015. switch (cmd)
  1016. {
  1017. case RT_DEVICE_CTRL_CLR_INT:
  1018. argval = (rt_uint32_t) arg;
  1019. if (argval == RT_DEVICE_FLAG_INT_RX)
  1020. {
  1021. NVIC_DisableIRQ(pbxcan->rcvirq0);
  1022. NVIC_DisableIRQ(pbxcan->rcvirq1);
  1023. CAN_ITConfig(pbxcan->reg, CAN_IT_FMP0 , DISABLE);
  1024. CAN_ITConfig(pbxcan->reg, CAN_IT_FF0 , DISABLE);
  1025. CAN_ITConfig(pbxcan->reg, CAN_IT_FOV0 , DISABLE);
  1026. CAN_ITConfig(pbxcan->reg, CAN_IT_FMP1 , DISABLE);
  1027. CAN_ITConfig(pbxcan->reg, CAN_IT_FF1 , DISABLE);
  1028. CAN_ITConfig(pbxcan->reg, CAN_IT_FOV1 , DISABLE);
  1029. }
  1030. else if (argval == RT_DEVICE_FLAG_INT_TX)
  1031. {
  1032. NVIC_DisableIRQ(pbxcan->sndirq);
  1033. CAN_ITConfig(pbxcan->reg, CAN_IT_TME, DISABLE);
  1034. }
  1035. else if (argval == RT_DEVICE_CAN_INT_ERR)
  1036. {
  1037. CAN_ITConfig(pbxcan->reg, CAN_IT_BOF , DISABLE);
  1038. CAN_ITConfig(pbxcan->reg, CAN_IT_LEC , DISABLE);
  1039. CAN_ITConfig(pbxcan->reg, CAN_IT_ERR , DISABLE);
  1040. NVIC_DisableIRQ(pbxcan->errirq);
  1041. }
  1042. break;
  1043. case RT_DEVICE_CTRL_SET_INT:
  1044. argval = (rt_uint32_t) arg;
  1045. if (argval == RT_DEVICE_FLAG_INT_RX)
  1046. {
  1047. CAN_ITConfig(pbxcan->reg, CAN_IT_FMP0 , ENABLE);
  1048. CAN_ITConfig(pbxcan->reg, CAN_IT_FF0 , ENABLE);
  1049. CAN_ITConfig(pbxcan->reg, CAN_IT_FOV0 , ENABLE);
  1050. CAN_ITConfig(pbxcan->reg, CAN_IT_FMP1 , ENABLE);
  1051. CAN_ITConfig(pbxcan->reg, CAN_IT_FF1 , ENABLE);
  1052. CAN_ITConfig(pbxcan->reg, CAN_IT_FOV1 , ENABLE);
  1053. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x1;
  1054. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
  1055. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  1056. NVIC_InitStructure.NVIC_IRQChannel = pbxcan->rcvirq0;
  1057. NVIC_Init(&NVIC_InitStructure);
  1058. NVIC_InitStructure.NVIC_IRQChannel = pbxcan->rcvirq1;
  1059. NVIC_Init(&NVIC_InitStructure);
  1060. }
  1061. else if (argval == RT_DEVICE_FLAG_INT_TX)
  1062. {
  1063. CAN_ITConfig(pbxcan->reg, CAN_IT_TME, ENABLE);
  1064. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x1;
  1065. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
  1066. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  1067. NVIC_InitStructure.NVIC_IRQChannel = pbxcan->sndirq;
  1068. NVIC_Init(&NVIC_InitStructure);
  1069. }
  1070. else if (argval == RT_DEVICE_CAN_INT_ERR)
  1071. {
  1072. CAN_ITConfig(pbxcan->reg, CAN_IT_BOF , ENABLE);
  1073. CAN_ITConfig(pbxcan->reg, CAN_IT_LEC , ENABLE);
  1074. CAN_ITConfig(pbxcan->reg, CAN_IT_ERR , ENABLE);
  1075. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x1;
  1076. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
  1077. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  1078. NVIC_InitStructure.NVIC_IRQChannel = pbxcan->errirq;
  1079. NVIC_Init(&NVIC_InitStructure);
  1080. }
  1081. break;
  1082. case RT_CAN_CMD_SET_FILTER:
  1083. return setfilter(pbxcan, (struct rt_can_filter_config *) arg);
  1084. break;
  1085. case RT_CAN_CMD_SET_MODE:
  1086. argval = (rt_uint32_t) arg;
  1087. if (argval != RT_CAN_MODE_NORMAL ||
  1088. argval != RT_CAN_MODE_LISEN ||
  1089. argval != RT_CAN_MODE_LOOPBACK ||
  1090. argval != RT_CAN_MODE_LOOPBACKANLISEN)
  1091. {
  1092. return RT_ERROR;
  1093. }
  1094. if (argval != can->config.mode)
  1095. {
  1096. can->config.mode = argval;
  1097. return bxcan_set_mode(pbxcan->reg, argval);
  1098. }
  1099. break;
  1100. case RT_CAN_CMD_SET_BAUD:
  1101. argval = (rt_uint32_t) arg;
  1102. if (argval != CAN1MBaud &&
  1103. argval != CAN800kBaud &&
  1104. argval != CAN500kBaud &&
  1105. argval != CAN250kBaud &&
  1106. argval != CAN125kBaud &&
  1107. argval != CAN100kBaud &&
  1108. argval != CAN50kBaud &&
  1109. argval != CAN20kBaud &&
  1110. argval != CAN10kBaud)
  1111. {
  1112. return RT_ERROR;
  1113. }
  1114. if (argval != can->config.baud_rate)
  1115. {
  1116. can->config.baud_rate = argval;
  1117. return bxcan_set_baud_rate(pbxcan->reg, argval);
  1118. }
  1119. break;
  1120. case RT_CAN_CMD_SET_PRIV:
  1121. argval = (rt_uint32_t) arg;
  1122. if (argval != RT_CAN_MODE_PRIV ||
  1123. argval != RT_CAN_MODE_NOPRIV)
  1124. {
  1125. return RT_ERROR;
  1126. }
  1127. if (argval != can->config.privmode)
  1128. {
  1129. can->config.privmode = argval;
  1130. return bxcan_set_privmode(pbxcan->reg, argval);
  1131. }
  1132. break;
  1133. case RT_CAN_CMD_GET_STATUS:
  1134. {
  1135. rt_uint32_t errtype;
  1136. errtype = pbxcan->reg->ESR;
  1137. can->status.rcverrcnt = errtype >> 24;
  1138. can->status.snderrcnt = (errtype >> 16 & 0xFF);
  1139. can->status.errcode = errtype & 0x07;
  1140. if (arg != &can->status)
  1141. {
  1142. rt_memcpy(arg, &can->status, sizeof(can->status));
  1143. }
  1144. }
  1145. break;
  1146. }
  1147. return RT_EOK;
  1148. }
  1149. static int sendmsg(struct rt_can_device *can, const void *buf, rt_uint32_t boxno)
  1150. {
  1151. CAN_TypeDef *pbxcan;
  1152. struct rt_can_msg *pmsg = (struct rt_can_msg *) buf;
  1153. pbxcan = ((struct stm_bxcan *) can->parent.user_data)->reg;
  1154. assert_param(IS_CAN_ALL_PERIPH(pbxcan));
  1155. pbxcan->sTxMailBox[boxno].TIR &= TMIDxR_TXRQ;
  1156. if (pmsg->ide == RT_CAN_STDID)
  1157. {
  1158. assert_param(IS_CAN_STDID(pmsg->id));
  1159. pbxcan->sTxMailBox[boxno].TIR |= ((pmsg->id << 21) | \
  1160. (pmsg->rtr << 1));
  1161. }
  1162. else
  1163. {
  1164. assert_param(IS_CAN_EXTID(pmsg->id));
  1165. pbxcan->sTxMailBox[boxno].TIR |= ((pmsg->id << 3) | \
  1166. (pmsg->ide << 2) | \
  1167. (pmsg->rtr << 1));
  1168. }
  1169. pmsg->len &= (uint8_t)0x0000000F;
  1170. pbxcan->sTxMailBox[boxno].TDTR &= (uint32_t)0xFFFFFFF0;
  1171. pbxcan->sTxMailBox[boxno].TDTR |= pmsg->len;
  1172. pbxcan->sTxMailBox[boxno].TDLR = (((uint32_t)pmsg->data[3] << 24) |
  1173. ((uint32_t)pmsg->data[2] << 16) |
  1174. ((uint32_t)pmsg->data[1] << 8) |
  1175. ((uint32_t)pmsg->data[0]));
  1176. if (pmsg->len > 4)
  1177. {
  1178. pbxcan->sTxMailBox[boxno].TDHR = (((uint32_t)pmsg->data[7] << 24) |
  1179. ((uint32_t)pmsg->data[6] << 16) |
  1180. ((uint32_t)pmsg->data[5] << 8) |
  1181. ((uint32_t)pmsg->data[4]));
  1182. }
  1183. pbxcan->sTxMailBox[boxno].TIR |= TMIDxR_TXRQ;
  1184. return RT_EOK;
  1185. }
  1186. static int recvmsg(struct rt_can_device *can, void *buf, rt_uint32_t boxno)
  1187. {
  1188. CAN_TypeDef *pbxcan;
  1189. struct rt_can_msg *pmsg = (struct rt_can_msg *) buf;
  1190. pbxcan = ((struct stm_bxcan *) can->parent.user_data)->reg;
  1191. assert_param(IS_CAN_ALL_PERIPH(pbxcan));
  1192. assert_param(IS_CAN_FIFO(boxno));
  1193. pmsg->ide = ((uint8_t)0x04 & pbxcan->sFIFOMailBox[boxno].RIR) >> 2;
  1194. if (pmsg->ide == CAN_Id_Standard)
  1195. {
  1196. pmsg->id = (uint32_t)0x000007FF & (pbxcan->sFIFOMailBox[boxno].RIR >> 21);
  1197. }
  1198. else
  1199. {
  1200. pmsg->id = (uint32_t)0x1FFFFFFF & (pbxcan->sFIFOMailBox[boxno].RIR >> 3);
  1201. }
  1202. pmsg->rtr = (uint8_t)((0x02 & pbxcan->sFIFOMailBox[boxno].RIR) >> 1);
  1203. pmsg->len = (uint8_t)0x0F & pbxcan->sFIFOMailBox[boxno].RDTR;
  1204. pmsg->data[0] = (uint8_t)0xFF & pbxcan->sFIFOMailBox[boxno].RDLR;
  1205. pmsg->data[1] = (uint8_t)0xFF & (pbxcan->sFIFOMailBox[boxno].RDLR >> 8);
  1206. pmsg->data[2] = (uint8_t)0xFF & (pbxcan->sFIFOMailBox[boxno].RDLR >> 16);
  1207. pmsg->data[3] = (uint8_t)0xFF & (pbxcan->sFIFOMailBox[boxno].RDLR >> 24);
  1208. if (pmsg->len > 4)
  1209. {
  1210. pmsg->data[4] = (uint8_t)0xFF & pbxcan->sFIFOMailBox[boxno].RDHR;
  1211. pmsg->data[5] = (uint8_t)0xFF & (pbxcan->sFIFOMailBox[boxno].RDHR >> 8);
  1212. pmsg->data[6] = (uint8_t)0xFF & (pbxcan->sFIFOMailBox[boxno].RDHR >> 16);
  1213. pmsg->data[7] = (uint8_t)0xFF & (pbxcan->sFIFOMailBox[boxno].RDHR >> 24);
  1214. }
  1215. pmsg->hdr = (uint8_t)0xFF & (pbxcan->sFIFOMailBox[boxno].RDTR >> 8);
  1216. if (boxno) pmsg->hdr += ((struct stm_bxcan *) can->parent.user_data)->fifo1filteroff * 4;
  1217. return RT_EOK;
  1218. }
  1219. static const struct rt_can_ops canops =
  1220. {
  1221. configure,
  1222. control,
  1223. sendmsg,
  1224. recvmsg,
  1225. };
  1226. #ifdef USING_BXCAN1
  1227. static struct stm_bxcan bxcan1data =
  1228. {
  1229. .reg = CAN1,
  1230. .mfrbase = (void *) &CAN1->sFilterRegister[0],
  1231. .sndirq = CAN1_TX_IRQn,
  1232. .rcvirq0 = CAN1_RX0_IRQn,
  1233. .rcvirq1 = CAN1_RX1_IRQn,
  1234. .errirq = CAN1_SCE_IRQn,
  1235. .alocmask = {0, 0},
  1236. .filtercnt = BX_CAN2_FMRSTART,
  1237. .fifo1filteroff = 7,
  1238. .filtermap = {
  1239. [0] = {
  1240. .id32mask_cnt = 0,
  1241. .id32bit_cnt = 0,
  1242. .id16mask_cnt = 2,
  1243. .id16bit_cnt = 24,
  1244. },
  1245. [1] = {
  1246. .id32mask_cnt = 0,
  1247. .id32bit_cnt = 0,
  1248. .id16mask_cnt = 2,
  1249. .id16bit_cnt = 24,
  1250. },
  1251. },
  1252. };
  1253. struct rt_can_device bxcan1;
  1254. void CAN1_RX0_IRQHandler(void)
  1255. {
  1256. if (CAN1->RF0R & 0x03)
  1257. {
  1258. if ((CAN1->RF0R & CAN_RF0R_FOVR0) != 0)
  1259. {
  1260. CAN1->RF0R = CAN_RF0R_FOVR0;
  1261. rt_hw_can_isr(&bxcan1, RT_CAN_EVENT_RXOF_IND | 0 << 8);
  1262. }
  1263. else
  1264. {
  1265. rt_hw_can_isr(&bxcan1, RT_CAN_EVENT_RX_IND | 0 << 8);
  1266. }
  1267. CAN1->RF0R |= CAN_RF0R_RFOM0;
  1268. }
  1269. }
  1270. void CAN1_RX1_IRQHandler(void)
  1271. {
  1272. if (CAN1->RF1R & 0x03)
  1273. {
  1274. if ((CAN1->RF1R & CAN_RF1R_FOVR1) != 0)
  1275. {
  1276. CAN1->RF1R = CAN_RF1R_FOVR1;
  1277. rt_hw_can_isr(&bxcan1, RT_CAN_EVENT_RXOF_IND | 1 << 8);
  1278. }
  1279. else
  1280. {
  1281. rt_hw_can_isr(&bxcan1, RT_CAN_EVENT_RX_IND | 1 << 8);
  1282. }
  1283. CAN1->RF1R |= CAN_RF1R_RFOM1;
  1284. }
  1285. }
  1286. void CAN1_TX_IRQHandler(void)
  1287. {
  1288. rt_uint32_t state;
  1289. if (CAN1->TSR & (CAN_TSR_RQCP0))
  1290. {
  1291. state = CAN1->TSR & (CAN_TSR_RQCP0 | CAN_TSR_TXOK0 | CAN_TSR_TME0);
  1292. CAN1->TSR |= CAN_TSR_RQCP0;
  1293. if (state == (CAN_TSR_RQCP0 | CAN_TSR_TXOK0 | CAN_TSR_TME0))
  1294. {
  1295. rt_hw_can_isr(&bxcan1, RT_CAN_EVENT_TX_DONE | 0 << 8);
  1296. }
  1297. else
  1298. {
  1299. rt_hw_can_isr(&bxcan1, RT_CAN_EVENT_TX_FAIL | 0 << 8);
  1300. }
  1301. }
  1302. if (CAN1->TSR & (CAN_TSR_RQCP1))
  1303. {
  1304. state = CAN1->TSR & (CAN_TSR_RQCP1 | CAN_TSR_TXOK1 | CAN_TSR_TME1);
  1305. CAN1->TSR |= CAN_TSR_RQCP1;
  1306. if (state == (CAN_TSR_RQCP1 | CAN_TSR_TXOK1 | CAN_TSR_TME1))
  1307. {
  1308. rt_hw_can_isr(&bxcan1, RT_CAN_EVENT_TX_DONE | 1 << 8);
  1309. }
  1310. else
  1311. {
  1312. rt_hw_can_isr(&bxcan1, RT_CAN_EVENT_TX_FAIL | 1 << 8);
  1313. }
  1314. }
  1315. if (CAN1->TSR & (CAN_TSR_RQCP2))
  1316. {
  1317. state = CAN1->TSR & (CAN_TSR_RQCP2 | CAN_TSR_TXOK2 | CAN_TSR_TME2);
  1318. CAN1->TSR |= CAN_TSR_RQCP2;
  1319. if (state == (CAN_TSR_RQCP2 | CAN_TSR_TXOK2 | CAN_TSR_TME2))
  1320. {
  1321. rt_hw_can_isr(&bxcan1, RT_CAN_EVENT_TX_DONE | 2 << 8);
  1322. }
  1323. else
  1324. {
  1325. rt_hw_can_isr(&bxcan1, RT_CAN_EVENT_TX_FAIL | 2 << 8);
  1326. }
  1327. }
  1328. }
  1329. void CAN1_SCE_IRQHandler(void)
  1330. {
  1331. rt_uint32_t errtype;
  1332. errtype = CAN1->ESR;
  1333. if (errtype & 0x70 && bxcan1.status.lasterrtype == (errtype & 0x70))
  1334. {
  1335. switch ((errtype & 0x70) >> 4)
  1336. {
  1337. case RT_CAN_BUS_BIT_PAD_ERR:
  1338. bxcan1.status.bitpaderrcnt++;
  1339. break;
  1340. case RT_CAN_BUS_FORMAT_ERR:
  1341. bxcan1.status.formaterrcnt++;
  1342. break;
  1343. case RT_CAN_BUS_ACK_ERR:
  1344. bxcan1.status.ackerrcnt++;
  1345. break;
  1346. case RT_CAN_BUS_IMPLICIT_BIT_ERR:
  1347. case RT_CAN_BUS_EXPLICIT_BIT_ERR:
  1348. bxcan1.status.biterrcnt++;
  1349. break;
  1350. case RT_CAN_BUS_CRC_ERR:
  1351. bxcan1.status.crcerrcnt++;
  1352. break;
  1353. }
  1354. bxcan1.status.lasterrtype = errtype & 0x70;
  1355. CAN1->ESR &= ~0x70;
  1356. }
  1357. bxcan1.status.rcverrcnt = errtype >> 24;
  1358. bxcan1.status.snderrcnt = (errtype >> 16 & 0xFF);
  1359. bxcan1.status.errcode = errtype & 0x07;
  1360. CAN1->MSR |= CAN_MSR_ERRI;
  1361. }
  1362. #endif /*USING_BXCAN1*/
  1363. #ifdef USING_BXCAN2
  1364. static struct stm_bxcan bxcan2data =
  1365. {
  1366. .reg = CAN2,
  1367. .mfrbase = (void *) &CAN1->sFilterRegister[BX_CAN2_FMRSTART],
  1368. .sndirq = CAN2_TX_IRQn,
  1369. .rcvirq0 = CAN2_RX0_IRQn,
  1370. .rcvirq1 = CAN2_RX1_IRQn,
  1371. .errirq = CAN2_SCE_IRQn,
  1372. .alocmask = {0, 0},
  1373. .filtercnt = BX_CAN_FMRNUMBER - BX_CAN2_FMRSTART,
  1374. .fifo1filteroff = 7,
  1375. .filtermap = {
  1376. [0] = {
  1377. .id32mask_cnt = 0,
  1378. .id32bit_cnt = 0,
  1379. .id16mask_cnt = 2,
  1380. .id16bit_cnt = 24,
  1381. },
  1382. [1] = {
  1383. .id32mask_cnt = 0,
  1384. .id32bit_cnt = 0,
  1385. .id16mask_cnt = 2,
  1386. .id16bit_cnt = 24,
  1387. },
  1388. },
  1389. };
  1390. struct rt_can_device bxcan2;
  1391. void CAN2_RX0_IRQHandler(void)
  1392. {
  1393. if (CAN2->RF0R & 0x03)
  1394. {
  1395. if ((CAN2->RF0R & CAN_RF0R_FOVR0) != 0)
  1396. {
  1397. CAN2->RF0R = CAN_RF0R_FOVR0;
  1398. rt_hw_can_isr(&bxcan2, RT_CAN_EVENT_RXOF_IND | 0 << 8);
  1399. }
  1400. else
  1401. {
  1402. rt_hw_can_isr(&bxcan2, RT_CAN_EVENT_RX_IND | 0 << 8);
  1403. }
  1404. CAN2->RF0R |= CAN_RF0R_RFOM0;
  1405. }
  1406. }
  1407. void CAN2_RX1_IRQHandler(void)
  1408. {
  1409. if (CAN2->RF1R & 0x03)
  1410. {
  1411. if ((CAN2->RF1R & CAN_RF1R_FOVR1) != 0)
  1412. {
  1413. CAN2->RF1R = CAN_RF1R_FOVR1;
  1414. rt_hw_can_isr(&bxcan2, RT_CAN_EVENT_RXOF_IND | 1 << 8);
  1415. }
  1416. else
  1417. {
  1418. rt_hw_can_isr(&bxcan2, RT_CAN_EVENT_RX_IND | 1 << 8);
  1419. }
  1420. CAN2->RF1R |= CAN_RF1R_RFOM1;
  1421. }
  1422. }
  1423. void CAN2_TX_IRQHandler(void)
  1424. {
  1425. rt_uint32_t state;
  1426. if (CAN2->TSR & (CAN_TSR_RQCP0))
  1427. {
  1428. state = CAN2->TSR & (CAN_TSR_RQCP0 | CAN_TSR_TXOK0 | CAN_TSR_TME0);
  1429. CAN2->TSR |= CAN_TSR_RQCP0;
  1430. if (state == (CAN_TSR_RQCP0 | CAN_TSR_TXOK0 | CAN_TSR_TME0))
  1431. {
  1432. rt_hw_can_isr(&bxcan2, RT_CAN_EVENT_TX_DONE | 0 << 8);
  1433. }
  1434. else
  1435. {
  1436. rt_hw_can_isr(&bxcan2, RT_CAN_EVENT_TX_FAIL | 0 << 8);
  1437. }
  1438. }
  1439. if (CAN2->TSR & (CAN_TSR_RQCP1))
  1440. {
  1441. state = CAN2->TSR & (CAN_TSR_RQCP1 | CAN_TSR_TXOK1 | CAN_TSR_TME1);
  1442. CAN2->TSR |= CAN_TSR_RQCP1;
  1443. if (state == (CAN_TSR_RQCP1 | CAN_TSR_TXOK1 | CAN_TSR_TME1))
  1444. {
  1445. rt_hw_can_isr(&bxcan2, RT_CAN_EVENT_TX_DONE | 1 << 8);
  1446. }
  1447. else
  1448. {
  1449. rt_hw_can_isr(&bxcan2, RT_CAN_EVENT_TX_FAIL | 1 << 8);
  1450. }
  1451. }
  1452. if (CAN2->TSR & (CAN_TSR_RQCP2))
  1453. {
  1454. state = CAN2->TSR & (CAN_TSR_RQCP2 | CAN_TSR_TXOK2 | CAN_TSR_TME2);
  1455. CAN2->TSR |= CAN_TSR_RQCP2;
  1456. if (state == (CAN_TSR_RQCP2 | CAN_TSR_TXOK2 | CAN_TSR_TME2))
  1457. {
  1458. rt_hw_can_isr(&bxcan2, RT_CAN_EVENT_TX_DONE | 2 << 8);
  1459. }
  1460. else
  1461. {
  1462. rt_hw_can_isr(&bxcan2, RT_CAN_EVENT_TX_FAIL | 2 << 8);
  1463. }
  1464. }
  1465. }
  1466. void CAN2_SCE_IRQHandler(void)
  1467. {
  1468. rt_uint32_t errtype;
  1469. errtype = CAN2->ESR;
  1470. if (errtype & 0x70 && bxcan2.status.lasterrtype == (errtype & 0x70))
  1471. {
  1472. switch ((errtype & 0x70) >> 4)
  1473. {
  1474. case RT_CAN_BUS_BIT_PAD_ERR:
  1475. bxcan2.status.bitpaderrcnt++;
  1476. break;
  1477. case RT_CAN_BUS_FORMAT_ERR:
  1478. bxcan2.status.formaterrcnt++;
  1479. break;
  1480. case RT_CAN_BUS_ACK_ERR:
  1481. bxcan2.status.ackerrcnt++;
  1482. break;
  1483. case RT_CAN_BUS_IMPLICIT_BIT_ERR:
  1484. case RT_CAN_BUS_EXPLICIT_BIT_ERR:
  1485. bxcan2.status.biterrcnt++;
  1486. break;
  1487. case RT_CAN_BUS_CRC_ERR:
  1488. bxcan2.status.crcerrcnt++;
  1489. break;
  1490. }
  1491. bxcan2.status.lasterrtype = errtype & 0x70;
  1492. CAN2->ESR &= ~0x70;
  1493. }
  1494. bxcan2.status.rcverrcnt = errtype >> 24;
  1495. bxcan2.status.snderrcnt = (errtype >> 16 & 0xFF);
  1496. bxcan2.status.errcode = errtype & 0x07;
  1497. CAN2->MSR |= CAN_MSR_ERRI;
  1498. }
  1499. #endif /*USING_BXCAN2*/
  1500. int stm32_bxcan_init(void)
  1501. {
  1502. #ifdef USING_BXCAN1
  1503. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA, ENABLE);
  1504. RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1 , ENABLE);
  1505. CAN_DeInit(CAN1);
  1506. bxcan1.config.baud_rate = CAN1MBaud;
  1507. bxcan1.config.msgboxsz = 16;
  1508. bxcan1.config.sndboxnumber = 3;
  1509. bxcan1.config.mode = RT_CAN_MODE_NORMAL;
  1510. bxcan1.config.privmode = 0;
  1511. bxcan1.config.ticks = 50;
  1512. #ifdef RT_CAN_USING_HDR
  1513. bxcan1.config.maxhdr = BX_CAN2_FMRSTART * 4;
  1514. #endif
  1515. rt_hw_can_register(&bxcan1, "bxcan1", &canops, &bxcan1data);
  1516. #endif
  1517. #ifdef USING_BXCAN2
  1518. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB, ENABLE);
  1519. #ifndef USING_BXCAN1
  1520. RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1 , ENABLE);
  1521. #endif
  1522. RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, ENABLE);
  1523. CAN_DeInit(CAN2);
  1524. bxcan2.config.baud_rate = CAN1MBaud;
  1525. bxcan2.config.msgboxsz = 16;
  1526. bxcan2.config.sndboxnumber = 3;
  1527. bxcan2.config.mode = RT_CAN_MODE_NORMAL;
  1528. bxcan2.config.privmode = 0;
  1529. bxcan2.config.ticks = 50;
  1530. #ifdef RT_CAN_USING_HDR
  1531. bxcan2.config.maxhdr = (BX_CAN_FMRNUMBER - BX_CAN2_FMRSTART) * 4;
  1532. #endif
  1533. rt_hw_can_register(&bxcan2, "bxcan2", &canops, &bxcan2data);
  1534. #endif
  1535. return RT_EOK;
  1536. }
  1537. INIT_BOARD_EXPORT(stm32_bxcan_init);
  1538. #endif /*RT_USING_CAN2*/