bxcan.c 49 KB

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