bxcan.c 48 KB

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