gd32f10x_can.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /**
  2. ******************************************************************************
  3. * @brief CAN functions of the firmware library.
  4. ******************************************************************************
  5. */
  6. /* Includes ------------------------------------------------------------------*/
  7. #include "gd32f10x_can.h"
  8. #include "gd32f10x_rcc.h"
  9. /** @addtogroup GD32F10x_Firmware
  10. * @{
  11. */
  12. /** @defgroup CAN
  13. * @brief CAN driver modules
  14. * @{
  15. */
  16. /** @defgroup CAN_Private_Defines
  17. * @{
  18. */
  19. /* CAN Mailbox Transmit Request */
  20. #define CAN_TMIR_TE ((uint32_t)0x00000001)
  21. /* CAN Filter Master Register bits */
  22. #define FMR_FINIT ((uint32_t)0x00000001)
  23. /* Time out for IWS bit */
  24. #define IWS_TIMEOUT ((uint32_t)0x0000FFFF)
  25. /* Time out for SWS bit */
  26. #define SWS_TIMEOUT ((uint32_t)0x0000FFFF)
  27. /* CAN TMIR_StdId masks */
  28. #define CAN_TMIR_StdId_Mask ((uint32_t)0x000007FF)
  29. /* CAN TMIR_ExtId masks */
  30. #define CAN_TMIR_ExtId_Mask ((uint32_t)0x1FFFFFFF)
  31. /* CAN TMIR_ExtId masks */
  32. #define CAN_FLAG_Mask ((uint32_t)0x000FFFFF)
  33. /* Flags in TSTR register */
  34. #define CAN_FLAGS_TSTR ((uint32_t)0x08000000)
  35. /* Flags in RFR1 register */
  36. #define CAN_FLAGS_RFR1 ((uint32_t)0x04000000)
  37. /* Flags in RFR0register */
  38. #define CAN_FLAGS_RFR0 ((uint32_t)0x02000000)
  39. /* Flags in STR register */
  40. #define CAN_FLAGS_STR ((uint32_t)0x01000000)
  41. /* Flags in ER register */
  42. #define CAN_FLAGS_ER ((uint32_t)0x00F00000)
  43. /* Mailboxes definition */
  44. #define CAN_TXMAILBOX_0 ((uint8_t)0x00)
  45. #define CAN_TXMAILBOX_1 ((uint8_t)0x01)
  46. #define CAN_TXMAILBOX_2 ((uint8_t)0x02)
  47. /* CAN Master Control Register bit */
  48. #define MCR_DBF ((uint32_t)0x00010000)
  49. #define CAN_MODE_MASK ((uint32_t)0x00000003)
  50. /**
  51. * @}
  52. */
  53. /** @defgroup CAN_Private_Functions
  54. * @{
  55. */
  56. /**
  57. * @brief Deinitialize the CAN peripheral registers.
  58. * @param CANx: where x:[1,2] to select the CAN peripheral.
  59. * @retval None.
  60. */
  61. void CAN_DeInit(CAN_TypeDef *CANx)
  62. {
  63. if (CANx == CAN1) {
  64. /* Force CAN1 reset state */
  65. RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_CAN1, ENABLE);
  66. /* Release CAN1 from reset state */
  67. RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_CAN1, DISABLE);
  68. } else {
  69. /* Force CAN2 reset state */
  70. RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_CAN2, ENABLE);
  71. /* Release CAN2 from reset state */
  72. RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_CAN2, DISABLE);
  73. }
  74. }
  75. /**
  76. * @brief Initialize the CAN peripheral according to the CAN_InitParaStruct.
  77. * @param CANx:where x:[1,2]
  78. * @param CAN_InitParaStruct: contain the configuration information for the CAN peripheral.
  79. * @retval It will be returned the status of CAN_INITSTATE_FAILED or CAN_INITSTATE_SUCCESS.
  80. */
  81. uint8_t CAN_Init(CAN_TypeDef *CANx, CAN_InitPara *CAN_InitParaStruct)
  82. {
  83. uint32_t wait_ack = 0x00000000;
  84. /* Out of sleep mode */
  85. CANx->CTLR &= (~(uint32_t)CAN_CTLR_SWM);
  86. /* Enable initial working */
  87. CANx->CTLR |= CAN_CTLR_IWM ;
  88. /* Wait the acknowledge */
  89. while (((CANx->STR & CAN_STR_IWS) != CAN_STR_IWS) && (wait_ack != IWS_TIMEOUT)) {
  90. wait_ack++;
  91. }
  92. /* Check whether initial working is success */
  93. if ((CANx->STR & CAN_STR_IWS) != CAN_STR_IWS) {
  94. return CAN_INITSTATE_FAILED;
  95. } else {
  96. /* Set the time triggered communication mode */
  97. if (CAN_InitParaStruct->CAN_TTC == ENABLE) {
  98. CANx->CTLR |= CAN_CTLR_TTC;
  99. } else {
  100. CANx->CTLR &= ~(uint32_t)CAN_CTLR_TTC;
  101. }
  102. /* Set the automatic bus-off management */
  103. if (CAN_InitParaStruct->CAN_ABOR == ENABLE) {
  104. CANx->CTLR |= CAN_CTLR_ABOR;
  105. } else {
  106. CANx->CTLR &= ~(uint32_t)CAN_CTLR_ABOR;
  107. }
  108. /* Set the automatic wake-up mode */
  109. if (CAN_InitParaStruct->CAN_AWK == ENABLE) {
  110. CANx->CTLR |= CAN_CTLR_AWK;
  111. } else {
  112. CANx->CTLR &= ~(uint32_t)CAN_CTLR_AWK;
  113. }
  114. /* Set the automatic retransmission mode */
  115. if (CAN_InitParaStruct->CAN_ARD == ENABLE) {
  116. CANx->CTLR |= CAN_CTLR_ARD;
  117. } else {
  118. CANx->CTLR &= ~(uint32_t)CAN_CTLR_ARD;
  119. }
  120. /* Set receive FIFO overwrite mode */
  121. if (CAN_InitParaStruct->CAN_RFOD == ENABLE) {
  122. CANx->CTLR |= CAN_CTLR_RFOD;
  123. } else {
  124. CANx->CTLR &= ~(uint32_t)CAN_CTLR_RFOD;
  125. }
  126. /* Set the Transmit FIFO order */
  127. if (CAN_InitParaStruct->CAN_TFO == ENABLE) {
  128. CANx->CTLR |= CAN_CTLR_TFO;
  129. } else {
  130. CANx->CTLR &= ~(uint32_t)CAN_CTLR_TFO;
  131. }
  132. /* Set the bit timing register */
  133. CANx->BTR = (uint32_t)((uint32_t)CAN_InitParaStruct->CAN_Mode << 30) | \
  134. ((uint32_t)CAN_InitParaStruct->CAN_SJW << 24) | \
  135. ((uint32_t)CAN_InitParaStruct->CAN_BS1 << 16) | \
  136. ((uint32_t)CAN_InitParaStruct->CAN_BS2 << 20) | \
  137. ((uint32_t)CAN_InitParaStruct->CAN_Prescaler - 1);
  138. /* leave initialisation */
  139. CANx->CTLR &= ~(uint32_t)CAN_CTLR_IWM;
  140. /* Wait the acknowledge */
  141. wait_ack = 0;
  142. while (((CANx->STR & CAN_STR_IWS) == CAN_STR_IWS) && (wait_ack != IWS_TIMEOUT)) {
  143. wait_ack++;
  144. }
  145. /* Check whether qiut initial working mode */
  146. if ((CANx->STR & CAN_STR_IWS) == CAN_STR_IWS) {
  147. return CAN_INITSTATE_FAILED;
  148. } else {
  149. return CAN_INITSTATE_SUCCESS;
  150. }
  151. }
  152. }
  153. /**
  154. * @brief Initialize the CAN peripheral according to the CAN_FilterInitStruct.
  155. * @param CAN_FilterInitParaStruct:contain the information for the CAN peripheral.
  156. * @retval None.
  157. */
  158. void CAN_FilterInit(CAN_FilterInitPara *CAN_FilterInitParaStruct)
  159. {
  160. uint32_t filter_number = 0;
  161. filter_number = ((uint32_t)1) << CAN_FilterInitParaStruct->CAN_FilterNumber;
  162. /* Set filter lock disable */
  163. CAN1->FCTLR |= CAN_FCTLR_FLD;
  164. /* Disable Filter */
  165. CAN1->FWR &= ~(uint32_t)filter_number;
  166. /* Filter Scale */
  167. if (CAN_FilterInitParaStruct->CAN_FilterScale == CAN_FILTERSCALE_16BIT) {
  168. /* Set the filter 16-bit scale*/
  169. CAN1->FSR &= ~(uint32_t)filter_number;
  170. /* First 16-bit list and First 16-bit mask */
  171. /* Or First 16-bit list and Second 16-bit list */
  172. CAN1->FilterRegister[CAN_FilterInitParaStruct->CAN_FilterNumber].FD0R =
  173. ((0x0000FFFF & (uint32_t)CAN_FilterInitParaStruct->CAN_FilterMaskListLow) << 16) |
  174. (0x0000FFFF & (uint32_t)CAN_FilterInitParaStruct->CAN_FilterListLow);
  175. /* Second 16-bit identifier and Second 16-bit mask */
  176. /* Or Third 16-bit identifier and Fourth 16-bit identifier */
  177. CAN1->FilterRegister[CAN_FilterInitParaStruct->CAN_FilterNumber].FD1R =
  178. ((0x0000FFFF & (uint32_t)CAN_FilterInitParaStruct->CAN_FilterMaskListHigh) << 16) |
  179. (0x0000FFFF & (uint32_t)CAN_FilterInitParaStruct->CAN_FilterListHigh);
  180. }
  181. if (CAN_FilterInitParaStruct->CAN_FilterScale == CAN_FILTERSCALE_32BIT) {
  182. /* 32-bit scale for the filter */
  183. CAN1->FSR |= filter_number;
  184. /* 32-bit identifier or First 32-bit identifier */
  185. CAN1->FilterRegister[CAN_FilterInitParaStruct->CAN_FilterNumber].FD0R =
  186. ((0x0000FFFF & (uint32_t)CAN_FilterInitParaStruct->CAN_FilterListHigh) << 16) |
  187. (0x0000FFFF & (uint32_t)CAN_FilterInitParaStruct->CAN_FilterListLow);
  188. /* 32-bit mask or Second 32-bit identifier */
  189. CAN1->FilterRegister[CAN_FilterInitParaStruct->CAN_FilterNumber].FD1R =
  190. ((0x0000FFFF & (uint32_t)CAN_FilterInitParaStruct->CAN_FilterMaskListHigh) << 16) |
  191. (0x0000FFFF & (uint32_t)CAN_FilterInitParaStruct->CAN_FilterMaskListLow);
  192. }
  193. /* Filter Mode */
  194. if (CAN_FilterInitParaStruct->CAN_FilterMode == CAN_FILTERMODE_MASK) {
  195. /*Filter with Mask mode*/
  196. CAN1->FMR &= ~(uint32_t)filter_number;
  197. } else { /* CAN_FilterInitStruct->CAN_FilterMode == CAN_FILTERMODE_LIST */
  198. /*Filter with List mode*/
  199. CAN1->FMR |= (uint32_t)filter_number;
  200. }
  201. /* Filter associated with FIFO */
  202. if (CAN_FilterInitParaStruct->CAN_FilterFIFOAssociation == CAN_FILTER_FIFO0) {
  203. /* Filter associated with FIFO 0 */
  204. CAN1->FAFR &= ~(uint32_t)filter_number;
  205. }
  206. if (CAN_FilterInitParaStruct->CAN_FilterFIFOAssociation == CAN_FILTER_FIFO1) {
  207. /* Filter associated with FIFO 1 */
  208. CAN1->FAFR |= (uint32_t)filter_number;
  209. }
  210. /* Filter working */
  211. if (CAN_FilterInitParaStruct->CAN_FilterWork == ENABLE) {
  212. CAN1->FWR |= filter_number;
  213. }
  214. /* Exit the initialisation mode for the filter */
  215. CAN1->FCTLR &= ~CAN_FCTLR_FLD;
  216. }
  217. /**
  218. * @brief Configure each CAN_InitParaStruct member with default value.
  219. * @param CAN_InitParaStruct: pointer to a CAN_InitPara structure to initialize.
  220. * @retval None.
  221. */
  222. void CAN_StructInit(CAN_InitPara *CAN_InitParaStruct)
  223. {
  224. /* Set the time triggered communication mode */
  225. CAN_InitParaStruct->CAN_TTC = DISABLE;
  226. /* Set the automatic bus-off recovery */
  227. CAN_InitParaStruct->CAN_ABOR = DISABLE;
  228. /* Set the automatic wake up mode */
  229. CAN_InitParaStruct->CAN_AWK = DISABLE;
  230. /* Set the automatic retransmission disable */
  231. CAN_InitParaStruct->CAN_ARD = DISABLE;
  232. /* Set the receive FIFO overwrite mode */
  233. CAN_InitParaStruct->CAN_RFOD = DISABLE;
  234. /* Set the transmit FIFO order */
  235. CAN_InitParaStruct->CAN_TFO = DISABLE;
  236. /* Set the CAN_Mode member */
  237. CAN_InitParaStruct->CAN_Mode = CAN_MODE_NORMAL;
  238. /* Set the CAN_SJW member */
  239. CAN_InitParaStruct->CAN_SJW = CAN_SJW_1TQ;
  240. /* Set the CAN_BS1 member */
  241. CAN_InitParaStruct->CAN_BS1 = CAN_BS1_4TQ;
  242. /* Set the CAN_BS2 member */
  243. CAN_InitParaStruct->CAN_BS2 = CAN_BS2_3TQ;
  244. /* Set the CAN_Prescaler member */
  245. CAN_InitParaStruct->CAN_Prescaler = 1;
  246. }
  247. /**
  248. * @brief Set header bank of CAN2 filter.
  249. * @param CAN2_HeaderBankNumber: Select header bank of CAN2 filter.It can be 1 to 27.
  250. * @retval None.
  251. */
  252. void CAN_HeaderBank(uint8_t CAN_HeaderBankNumber)
  253. {
  254. /* Set filter lock disable */
  255. CAN1->FCTLR |= CAN_FCTLR_FLD;
  256. /* Select filter start number for slave CAN */
  257. CAN1->FCTLR &= (uint32_t)0xFFFFC0F1 ;
  258. CAN1->FCTLR |= (uint32_t)(CAN_HeaderBankNumber) << 8;
  259. /* Filter out the initialization mode */
  260. CAN1->FCTLR |= ~CAN_FCTLR_FLD;
  261. }
  262. /**
  263. * @brief Enable or disable the Debug Freeze for CAN.
  264. * @param CANx: where x :[1,2] for selecting the CAN peripheral.
  265. * @param NewValue: new state of the CAN peripheral.
  266. * This parameter can be: ENABLE or DISABLE.
  267. * @retval None.
  268. */
  269. void CAN_DebugFreeze(CAN_TypeDef *CANx, TypeState NewValue)
  270. {
  271. if (NewValue != DISABLE) {
  272. CANx->CTLR |= CAN_CTLR_DFZ;
  273. } else {
  274. CANx->CTLR &= ~CAN_CTLR_DFZ;
  275. }
  276. }
  277. /**
  278. * @brief Enable or disabe the CAN Time Triggered communication mode.
  279. * @param CANx:where x :[1,2] for selecting the CAN peripheral.
  280. * @param NewValue : Mode new state , This parameter can be: ENABLE or DISABLE.
  281. * @retval None
  282. */
  283. void CAN_TimeTrigComMode_Enable(CAN_TypeDef *CANx, TypeState NewValue)
  284. {
  285. if (NewValue != DISABLE) {
  286. /* Enable the TTC mode */
  287. CANx->CTLR |= CAN_CTLR_TTC;
  288. /* Set TSEbits */
  289. CANx->TxMailBox[0].TMPR |= ((uint32_t)CAN_TMPR0_TSE);
  290. CANx->TxMailBox[1].TMPR |= ((uint32_t)CAN_TMPR1_TSE);
  291. CANx->TxMailBox[2].TMPR |= ((uint32_t)CAN_TMPR2_TSE);
  292. } else {
  293. /* Disable the TTC mode */
  294. CANx->CTLR &= (uint32_t)(~(uint32_t)CAN_CTLR_TTC);
  295. /* Reset TSE bits */
  296. CANx->TxMailBox[0].TMPR &= ((uint32_t)~CAN_TMPR0_TSE);
  297. CANx->TxMailBox[1].TMPR &= ((uint32_t)~CAN_TMPR1_TSE);
  298. CANx->TxMailBox[2].TMPR &= ((uint32_t)~CAN_TMPR2_TSE);
  299. }
  300. }
  301. /**
  302. * @brief Initiate to transmit a message.
  303. * @param CANx: where x:[1,2]to to select the CAN peripheral.
  304. * @param TxMessage: contain CAN Id, CAN DLC and CAN data for the structure.
  305. * @retval The number of the mailbox that is used for transmission
  306. * or CAN_TxState_NoMailBox if there is no empty mailbox.
  307. */
  308. uint8_t CAN_Transmit(CAN_TypeDef *CANx, CanTxMessage *TxMessage)
  309. {
  310. uint8_t transmit_mailbox_number = 0;
  311. /* Select one empty transmit mailbox */
  312. if ((CANx->TSTR & CAN_TSTR_TME0) == CAN_TSTR_TME0) {
  313. transmit_mailbox_number = 0;
  314. } else if ((CANx->TSTR & CAN_TSTR_TME1) == CAN_TSTR_TME1) {
  315. transmit_mailbox_number = 1;
  316. } else if ((CANx->TSTR & CAN_TSTR_TME2) == CAN_TSTR_TME2) {
  317. transmit_mailbox_number = 2;
  318. } else {
  319. transmit_mailbox_number = CAN_TXSTATE_NOMAILBOX;
  320. }
  321. if (transmit_mailbox_number != CAN_TXSTATE_NOMAILBOX) {
  322. /* Set up the Id */
  323. CANx->TxMailBox[transmit_mailbox_number].TMIR &= CAN_TMIR_TE;
  324. if (TxMessage->FF == CAN_FF_STANDARD) {
  325. CANx->TxMailBox[transmit_mailbox_number].TMIR |= ((TxMessage->StdId << 21) | \
  326. TxMessage->FT);
  327. } else {
  328. CANx->TxMailBox[transmit_mailbox_number].TMIR |= ((TxMessage->ExtId << 3) | \
  329. TxMessage->FF | \
  330. TxMessage->FT);
  331. }
  332. /* Set up the DLC */
  333. TxMessage->DLC &= ((uint8_t)CAN_TMPR0_DLC);
  334. CANx->TxMailBox[transmit_mailbox_number].TMPR &= ((uint32_t)~CAN_TMPR0_DLC);
  335. CANx->TxMailBox[transmit_mailbox_number].TMPR |= TxMessage->DLC;
  336. /* Set up the data field */
  337. CANx->TxMailBox[transmit_mailbox_number].TMD0R = (((uint32_t)TxMessage->Data[3] << 24) |
  338. ((uint32_t)TxMessage->Data[2] << 16) |
  339. ((uint32_t)TxMessage->Data[1] << 8) |
  340. ((uint32_t)TxMessage->Data[0]));
  341. CANx->TxMailBox[transmit_mailbox_number].TMD1R = (((uint32_t)TxMessage->Data[7] << 24) |
  342. ((uint32_t)TxMessage->Data[6] << 16) |
  343. ((uint32_t)TxMessage->Data[5] << 8) |
  344. ((uint32_t)TxMessage->Data[4]));
  345. /* Request transmission */
  346. CANx->TxMailBox[transmit_mailbox_number].TMIR |= CAN_TMIR0_TE;
  347. }
  348. return transmit_mailbox_number;
  349. }
  350. /**
  351. * @brief Check the state of message transmission.
  352. * @param CANx: where x:[1,2] to to select the CAN peripheral.
  353. * @param TransmitMailbox: the number of the used transmission mailbox.
  354. * @retval Return CAN_TXSTATE_OK or CAN_TXSTATE_FAILED.
  355. */
  356. uint8_t CAN_TransmitState(CAN_TypeDef *CANx, uint8_t TransmitMailbox)
  357. {
  358. uint32_t state = 0;
  359. switch (TransmitMailbox) {
  360. case (CAN_TXMAILBOX_0):
  361. state = CANx->TSTR & (CAN_TSTR_MTF0 | CAN_TSTR_MTFNE0 | CAN_TSTR_TME0);
  362. break;
  363. case (CAN_TXMAILBOX_1):
  364. state = CANx->TSTR & (CAN_TSTR_MTF1 | CAN_TSTR_MTFNE1 | CAN_TSTR_TME1);
  365. break;
  366. case (CAN_TXMAILBOX_2):
  367. state = CANx->TSTR & (CAN_TSTR_MTF2 | CAN_TSTR_MTFNE2 | CAN_TSTR_TME2);
  368. break;
  369. default:
  370. state = CAN_TXSTATE_FAILED;
  371. break;
  372. }
  373. switch (state) {
  374. /* transmit pending */
  375. case (0x0):
  376. state = CAN_TXSTATE_PENDING;
  377. break;
  378. /* transmit failed */
  379. case (CAN_TSTR_MTF0 | CAN_TSTR_TME0):
  380. state = CAN_TXSTATE_FAILED;
  381. break;
  382. case (CAN_TSTR_MTF1 | CAN_TSTR_TME1):
  383. state = CAN_TXSTATE_FAILED;
  384. break;
  385. case (CAN_TSTR_MTF2 | CAN_TSTR_TME2):
  386. state = CAN_TXSTATE_FAILED;
  387. break;
  388. /* transmit succeeded */
  389. case (CAN_TSTR_MTF0 | CAN_TSTR_MTFNE0 | CAN_TSTR_TME0):
  390. state = CAN_TXSTATE_OK;
  391. break;
  392. case (CAN_TSTR_MTF1 | CAN_TSTR_MTFNE1 | CAN_TSTR_TME1):
  393. state = CAN_TXSTATE_OK;
  394. break;
  395. case (CAN_TSTR_MTF2 | CAN_TSTR_MTFNE2 | CAN_TSTR_TME2):
  396. state = CAN_TXSTATE_OK;
  397. break;
  398. default:
  399. state = CAN_TXSTATE_FAILED;
  400. break;
  401. }
  402. return (uint8_t) state;
  403. }
  404. /**
  405. * @brief Stop a transmit mission.
  406. * @param CANx: where x:[1,2] to to select the CAN peripheral.
  407. * @param Mailbox: Mailbox number.
  408. * @retval None.
  409. */
  410. void CAN_StopTransmit(CAN_TypeDef *CANx, uint8_t Mailbox)
  411. {
  412. /* Stop transmission */
  413. switch (Mailbox) {
  414. case (CAN_TXMAILBOX_0):
  415. CANx->TSTR |= CAN_TSTR_MST0;
  416. break;
  417. case (CAN_TXMAILBOX_1):
  418. CANx->TSTR |= CAN_TSTR_MST1;
  419. break;
  420. case (CAN_TXMAILBOX_2):
  421. CANx->TSTR |= CAN_TSTR_MST2;
  422. break;
  423. default:
  424. break;
  425. }
  426. }
  427. /**
  428. * @brief Receive a message.
  429. * @param CANx: where x:[1,2] to to select the CAN peripheral.
  430. * @param FIFONumber: Receive FIFO number, CAN_FIFO0 or CAN_FIFO1.
  431. * @param RxMessage: a structure receive message which contains
  432. * CAN Id, CAN DLC, CAN datas and FI number.
  433. * @retval None.
  434. */
  435. void CAN_Receive(CAN_TypeDef *CANx, uint8_t FIFONumber, CanRxMessage *RxMessage)
  436. {
  437. /* Get the frame identifier */
  438. RxMessage->FF = (uint8_t)0x04 & CANx->FIFOMailBox[FIFONumber].RFMIR;
  439. if (RxMessage->FF == CAN_FF_STANDARD) {
  440. RxMessage->StdId = CAN_TMIR_StdId_Mask & (CANx->FIFOMailBox[FIFONumber].RFMIR >> 21);
  441. } else {
  442. RxMessage->ExtId = CAN_TMIR_ExtId_Mask & (CANx->FIFOMailBox[FIFONumber].RFMIR >> 3);
  443. }
  444. RxMessage->FT = (uint8_t)0x02 & CANx->FIFOMailBox[FIFONumber].RFMIR;
  445. /* Get the data length code */
  446. RxMessage->DLC = (uint8_t)0x0F & CANx->FIFOMailBox[FIFONumber].RFMPR;
  447. /* Get the filtering index */
  448. RxMessage->FI = (uint8_t)0xFF & (CANx->FIFOMailBox[FIFONumber].RFMPR >> 8);
  449. /* Get FIFO mailbox data */
  450. RxMessage->Data[0] = (uint8_t)0xFF & CANx->FIFOMailBox[FIFONumber].RFMD0R;
  451. RxMessage->Data[1] = (uint8_t)0xFF & (CANx->FIFOMailBox[FIFONumber].RFMD0R >> 8);
  452. RxMessage->Data[2] = (uint8_t)0xFF & (CANx->FIFOMailBox[FIFONumber].RFMD0R >> 16);
  453. RxMessage->Data[3] = (uint8_t)0xFF & (CANx->FIFOMailBox[FIFONumber].RFMD0R >> 24);
  454. RxMessage->Data[4] = (uint8_t)0xFF & CANx->FIFOMailBox[FIFONumber].RFMD1R;
  455. RxMessage->Data[5] = (uint8_t)0xFF & (CANx->FIFOMailBox[FIFONumber].RFMD1R >> 8);
  456. RxMessage->Data[6] = (uint8_t)0xFF & (CANx->FIFOMailBox[FIFONumber].RFMD1R >> 16);
  457. RxMessage->Data[7] = (uint8_t)0xFF & (CANx->FIFOMailBox[FIFONumber].RFMD1R >> 24);
  458. /* Release the FIFO */
  459. if (FIFONumber == CAN_FIFO0) {
  460. CANx->RFR0 |= CAN_RFR0_RFD0;
  461. }
  462. /* FIFONumber == CAN_FIFO1 */
  463. else {
  464. CANx->RFR1 |= CAN_RFR1_RFD1;
  465. }
  466. }
  467. /**
  468. * @brief Dequeue the FIFO.
  469. * @param CANx: where x:[1,2] to to select the CAN peripheral.
  470. * @param FIFONumber: FIFO to dequeue, it can be CAN_FIFO0 or CAN_FIFO1.
  471. * @retval None.
  472. */
  473. void CAN_FIFODequeue(CAN_TypeDef *CANx, uint8_t FIFONumber)
  474. {
  475. if (FIFONumber == CAN_FIFO0) {
  476. /* Release FIFO 0 */
  477. CANx->RFR0 |= CAN_RFR0_RFD0;
  478. } else { /* FIFONumber == CAN_FIFO1 */
  479. /* Release FIFO1 */
  480. CANx->RFR1 |= CAN_RFR1_RFD1;
  481. }
  482. }
  483. /**
  484. * @brief Return the length of receiving messages.
  485. * @param CANx: where x:[1,2] to to select the CAN peripheral.
  486. * @param FIFONumber: Receive FIFO number, CAN_FIFO0 or CAN_FIFO1.
  487. * @retval message_length : which is the Length of pending message.
  488. */
  489. uint8_t CAN_MessageLength(CAN_TypeDef *CANx, uint8_t FIFONumber)
  490. {
  491. uint8_t message_length = 0;
  492. if (FIFONumber == CAN_FIFO0) {
  493. message_length = (uint8_t)(CANx->RFR0 & (uint32_t)0x03);
  494. } else if (FIFONumber == CAN_FIFO1) {
  495. message_length = (uint8_t)(CANx->RFR1 & (uint32_t)0x03);
  496. } else {
  497. message_length = 0;
  498. }
  499. return message_length;
  500. }
  501. /**
  502. * @brief Select the CAN Working mode.
  503. * @param CAN_WorkingMode : CAN Working Mode.It can be the following one.
  504. * @arg CAN_WORKINGMODE_INITIAL
  505. * @arg CAN_WORKINGMODE_NORMAL
  506. * @arg CAN_WORKINGMODE_SLEEP
  507. * @retval state of the requested mode which can be
  508. * @arg CAN_MODESTATE_FAILED
  509. * @arg CAN_MODESTATE_SUCCESS
  510. */
  511. uint8_t CAN_WorkingMode(CAN_TypeDef *CANx, uint8_t CAN_WorkingMode)
  512. {
  513. uint8_t state = CAN_MODESTATE_FAILED;
  514. /* Timeout for IWS or also for SWS bits*/
  515. uint32_t timeout = IWS_TIMEOUT;
  516. if (CAN_WorkingMode == CAN_WORKINGMODE_INITIAL) {
  517. /* Set initialisation */
  518. CANx->CTLR = (uint32_t)((CANx->CTLR & (uint32_t)(~(uint32_t)CAN_CTLR_SWM)) | CAN_CTLR_IWM);
  519. /* Wait the acknowledge */
  520. while (((CANx->STR & CAN_MODE_MASK) != CAN_STR_IWS) && (timeout != 0)) {
  521. timeout--;
  522. }
  523. if ((CANx->STR & CAN_MODE_MASK) != CAN_STR_IWS) {
  524. state = CAN_MODESTATE_FAILED;
  525. } else {
  526. state = CAN_MODESTATE_SUCCESS;
  527. }
  528. } else if (CAN_WorkingMode == CAN_WORKINGMODE_NORMAL) {
  529. /* Enter Normal mode */
  530. CANx->CTLR &= (uint32_t)(~(CAN_CTLR_SWM | CAN_CTLR_IWM));
  531. /* Wait the acknowledge */
  532. while (((CANx->STR & CAN_MODE_MASK) != 0) && (timeout != 0)) {
  533. timeout--;
  534. }
  535. if ((CANx->STR & CAN_MODE_MASK) != 0) {
  536. state = CAN_MODESTATE_FAILED;
  537. } else {
  538. state = CAN_MODESTATE_SUCCESS;
  539. }
  540. } else if (CAN_WorkingMode == CAN_WORKINGMODE_SLEEP) {
  541. /* Set Sleep mode */
  542. CANx->CTLR = (uint32_t)((CANx->CTLR & (uint32_t)(~(uint32_t)CAN_CTLR_IWM)) | CAN_CTLR_SWM);
  543. /* Wait the acknowledge */
  544. while (((CANx->STR & CAN_MODE_MASK) != CAN_STR_SWS) && (timeout != 0)) {
  545. timeout--;
  546. }
  547. if ((CANx->STR & CAN_MODE_MASK) != CAN_STR_SWS) {
  548. state = CAN_MODESTATE_FAILED;
  549. } else {
  550. state = CAN_MODESTATE_SUCCESS;
  551. }
  552. } else {
  553. state = CAN_MODESTATE_FAILED;
  554. }
  555. return (uint8_t) state;
  556. }
  557. /**
  558. * @brief Enter the Sleep mode.
  559. * @param CANx: where x:[1,2] to to select the CAN peripheral.
  560. * @retval state: CAN_SLEEP_OK or CAN_SLEEP_FAILED.
  561. */
  562. uint8_t CAN_EnterSleep(CAN_TypeDef *CANx)
  563. {
  564. /* Set Sleep mode */
  565. CANx->CTLR = (((CANx->CTLR) & (uint32_t)(~(uint32_t)CAN_CTLR_IWM)) | CAN_CTLR_SWM);
  566. if ((CANx->STR & (CAN_STR_SWS | CAN_STR_IWS)) == CAN_STR_SWS) {
  567. /* Sleep mode entered success*/
  568. return (uint8_t)CAN_SLEEP_OK;
  569. } else {
  570. /* Sleep mode entered failure */
  571. return (uint8_t)CAN_SLEEP_FAILED;
  572. }
  573. }
  574. /**
  575. * @brief Wake up CAN.
  576. * @param CANx: where x:[1,2] to to select the CAN peripheral.
  577. * @retval status: CAN_WAKEUP_OK or CAN_WAKEUP_FAILED.
  578. */
  579. uint8_t CAN_WakeUp(CAN_TypeDef *CANx)
  580. {
  581. uint32_t wait_sws = SWS_TIMEOUT;
  582. /*set wake up */
  583. CANx->CTLR &= ~(uint32_t)CAN_CTLR_SWM;
  584. /* Sleep mode state */
  585. while (((CANx->CTLR & CAN_CTLR_SWM) == CAN_CTLR_SWM) && (wait_sws != 0x00)) {
  586. wait_sws--;
  587. }
  588. if ((CANx->CTLR & CAN_CTLR_SWM) != CAN_CTLR_SWM) {
  589. /*Sleep mode exited */
  590. return (uint8_t)CAN_WAKEUP_OK;
  591. } else {
  592. /*Sleep mode exited failure */
  593. return (uint8_t)CAN_WAKEUP_FAILED;
  594. }
  595. }
  596. /**
  597. * @brief Return the CANx's last error type (LEC).
  598. * @param CANx: where x:[1,2] to to select the CAN peripheral.
  599. * @retval CAN_ErrorType: specify the Error type:
  600. * @arg CAN_ERRORTYPE_NOERR
  601. * @arg CAN_ERRORTYPE_STUFFERR
  602. * @arg CAN_ERRORTYPE_FORMERR
  603. * @arg CAN_ERRORTYPE_ACKERR
  604. * @arg CAN_ERRORTYPE_BITRECESSIVEERR
  605. * @arg CAN_ERRORTYPE_BITDOMINANTERR
  606. * @arg CAN_ERRORTYPE_CRCERR
  607. * @arg CAN_ERRORTYPE_SOFTWARESETERR
  608. */
  609. uint8_t CAN_GetErrorType(CAN_TypeDef *CANx)
  610. {
  611. uint8_t error_type = 0;
  612. /* Get the error type*/
  613. error_type = (((uint8_t)CANx->ER) & (uint8_t)CAN_ER_ET);
  614. /* Return the error type*/
  615. return error_type;
  616. }
  617. /**
  618. * @brief Get the Counter of CANx Receive Error(REC).
  619. * @note According to the error condition as defined by the CAN standard,
  620. * the counter of CANx Receive Error is increased by 1 or by 8.
  621. * When CAN received success everytime, the counter is decreased by 1
  622. * or reset to 120 if its value was over than 128.
  623. * When the counter value is over 127, the CAN controller enters the
  624. * error passive state.
  625. * @param CANx: where x:[1,2] to to select the CAN peripheral.
  626. * @retval CAN Receive Error Counter.
  627. */
  628. uint8_t CAN_GetReceiveErrorCounter(CAN_TypeDef *CANx)
  629. {
  630. uint8_t receive_counter = 0;
  631. /* Get the counter of CANx Receive Error*/
  632. receive_counter = (uint8_t)((CANx->ER & CAN_ER_REC) >> 24);
  633. /* Return the Receive Error Counter*/
  634. return receive_counter;
  635. }
  636. /**
  637. * @brief Get the Counter of CANx Transmit Error (TEC).
  638. * @param CANx: where x:[1,2] to to select the CAN peripheral.
  639. * @retval CAN Transmit Error Counter.
  640. */
  641. uint8_t CAN_GetTransmitErrorCounter(CAN_TypeDef *CANx)
  642. {
  643. uint8_t transmit_counter = 0;
  644. /* Get the Counter of CANx Transmit Error(TEC) */
  645. transmit_counter = (uint8_t)((CANx->ER & CAN_ER_TEC) >> 16);
  646. /* Return the Transmit Error Counter*/
  647. return transmit_counter;
  648. }
  649. /**
  650. * @brief Enable or disable the specified CANx interrupts.
  651. * @param CANx: where x:[1,2] to to select the CAN peripheral.
  652. * @param CAN_INT: specify the CAN interrupt sources to be enabled or disabled.
  653. * This parameter can be:
  654. * @arg CAN_INT_TME
  655. * @arg CAN_INT_RFNE0
  656. * @arg CAN_INT_RFF0
  657. * @arg CAN_INT_RFO0
  658. * @arg CAN_INT_RFNE1
  659. * @arg CAN_INT_RFF1
  660. * @arg CAN_INT_RFO1
  661. * @arg CAN_INT_WE
  662. * @arg CAN_INT_PE
  663. * @arg CAN_INT_BOE
  664. * @arg CAN_INT_ET
  665. * @arg CAN_INT_ERR
  666. * @arg CAN_INT_WU
  667. * @arg CAN_INT_SLP
  668. * @param NewValue: new state of the CAN interrupts.
  669. * This parameter can be: ENABLE or DISABLE.
  670. * @retval None.
  671. */
  672. void CAN_INTConfig(CAN_TypeDef *CANx, uint32_t CAN_INT, TypeState NewValue)
  673. {
  674. if (NewValue != DISABLE) {
  675. /* Enable interrupt */
  676. CANx->IER |= CAN_INT;
  677. } else {
  678. /* Disable interrupt */
  679. CANx->IER &= ~CAN_INT;
  680. }
  681. }
  682. /**
  683. * @brief Check whether the specified CAN flag is set or not.
  684. * @param CANx: where x:[1,2] to to select the CAN peripheral.
  685. * @param CAN_FLAG: specify the flag to check.
  686. * This parameter can be one of the following flags:
  687. * @arg CAN_FLAG_WE: Warning error Flag
  688. * @arg CAN_FLAG_PE: Passive error Flag
  689. * @arg CAN_FLAG_BOE: Bus-off error Flag
  690. * @arg CAN_FLAG_MTF0: Mailbox 0 transmit finished Flag
  691. * @arg CAN_FLAG_MTF1: Mailbox 1 transmit finished Flag
  692. * @arg CAN_FLAG_MTF2: Mailbox 2 transmit finished Flag
  693. * @arg CAN_FLAG_RFL0: the length of the receive FIFO0 Flag
  694. * @arg CAN_FLAG_RFF0: Receive FIFO 0 full Flag
  695. * @arg CAN_FLAG_RFO0: Receive FIFO 0 overfull Flag
  696. * @arg CAN_FLAG_RFL1: the length of the receive FIFO1 Flag
  697. * @arg CAN_FLAG_RFF1: Receive FIFO 1 full Flag
  698. * @arg CAN_FLAG_RFO1: Receive FIFO 0 overfull Flag
  699. * @arg CAN_FLAG_WU: Wake up Flag
  700. * @arg CAN_FLAG_SLP: Sleep working state Flag
  701. * @arg CAN_FLAG_ET: Error type Flag
  702. * @retval The new state of CAN_FLAG (SET or RESET).
  703. */
  704. TypeState CAN_GetBitState(CAN_TypeDef *CANx, uint32_t CAN_FLAG)
  705. {
  706. if ((CAN_FLAG & CAN_FLAGS_ER) != (uint32_t)RESET) {
  707. /* Check the state of the specified CAN flag */
  708. if ((CANx->ER & (CAN_FLAG & CAN_FLAG_Mask)) != (uint32_t)RESET) {
  709. /* CAN_FLAG is set */
  710. return SET;
  711. } else {
  712. /* CAN_FLAG is reset */
  713. return RESET;
  714. }
  715. } else if ((CAN_FLAG & CAN_FLAGS_STR) != (uint32_t)RESET) {
  716. /* Check the state of the specified CAN flag */
  717. if ((CANx->STR & (CAN_FLAG & CAN_FLAG_Mask)) != (uint32_t)RESET) {
  718. /* CAN_FLAG is set */
  719. return SET;
  720. } else {
  721. /* CAN_FLAG is reset */
  722. return RESET;
  723. }
  724. } else if ((CAN_FLAG & CAN_FLAGS_TSTR) != (uint32_t)RESET) {
  725. /* Check the state of the specified CAN flag */
  726. if ((CANx->TSTR & (CAN_FLAG & CAN_FLAG_Mask)) != (uint32_t)RESET) {
  727. /* CAN_FLAG is set */
  728. return SET;
  729. } else {
  730. /* CAN_FLAG is reset */
  731. return RESET;
  732. }
  733. } else if ((CAN_FLAG & CAN_FLAGS_RFR0) != (uint32_t)RESET) {
  734. /* Check the state of the specified CAN flag */
  735. if ((CANx->RFR0 & (CAN_FLAG & CAN_FLAG_Mask)) != (uint32_t)RESET) {
  736. /* CAN_FLAG is set */
  737. return SET;
  738. } else {
  739. /* CAN_FLAG is reset */
  740. return RESET;
  741. }
  742. }
  743. /* If(CAN_FLAG & CAN_FLAGS_RFR1 != (uint32_t)RESET) */
  744. else {
  745. /* Check the state of the specified CAN flag */
  746. if ((uint32_t)(CANx->RFR1 & (CAN_FLAG & CAN_FLAG_Mask)) != (uint32_t)RESET) {
  747. /* CAN_FLAG is set */
  748. return SET;
  749. } else {
  750. /* CAN_FLAG is reset */
  751. return RESET;
  752. }
  753. }
  754. }
  755. /**
  756. * @brief Clear the CAN's flags.
  757. * @param CANx: where x:[1,2] to to select the CAN peripheral.
  758. * @param CAN_FLAG: specify the flag to clear.
  759. * This parameter can be one of the following flags:
  760. * @arg CAN_FLAG_MTF0: Mailbox 0 transmit finished Flag
  761. * @arg CAN_FLAG_MTF1: Mailbox 1 transmit finished Flag
  762. * @arg CAN_FLAG_MTF2: Mailbox 2 transmit finished Flag
  763. * @arg CAN_FLAG_RFF0: Receive FIFO 0 full Flag
  764. * @arg CAN_FLAG_RFO0: Receive FIFO 0 overfull Flag
  765. * @arg CAN_FLAG_RFF1: Receive FIFO 1 full Flag
  766. * @arg CAN_FLAG_RFO1: Receive FIFO 0 overfull Flag
  767. * @arg CAN_FLAG_WU: Wake up Flag
  768. * @arg CAN_FLAG_SLP: Sleep working state Flag
  769. * @arg CAN_FLAG_ET: Error type Flag
  770. * @retval None.
  771. */
  772. void CAN_ClearBitState(CAN_TypeDef *CANx, uint32_t CAN_FLAG)
  773. {
  774. uint32_t temp = 0;
  775. /* ER register */
  776. if (CAN_FLAG == CAN_FLAG_ET) {
  777. /* Clear the selected CAN flags */
  778. CANx->ER = (uint32_t)RESET;
  779. }
  780. /* STR or TSTR or RFR0 or RFR1 */
  781. else {
  782. temp = CAN_FLAG & CAN_FLAG_Mask;
  783. if ((CAN_FLAG & CAN_FLAGS_RFR0) != (uint32_t)RESET) {
  784. /* Receive Flags */
  785. CANx->RFR0 = (uint32_t)(temp);
  786. } else if ((CAN_FLAG & CAN_FLAGS_RFR1) != (uint32_t)RESET) {
  787. /* Receive Flags */
  788. CANx->RFR1 = (uint32_t)(temp);
  789. } else if ((CAN_FLAG & CAN_FLAGS_TSTR) != (uint32_t)RESET) {
  790. /* Transmit Flags */
  791. CANx->TSTR = (uint32_t)(temp);
  792. }
  793. /* If((CAN_FLAG & CAN_FLAGS_STR)!=(uint32_t)RESET) */
  794. else {
  795. CANx->STR = (uint32_t)(temp);
  796. }
  797. }
  798. }
  799. /**
  800. * @brief Check whether the specified CANx interrupt has occurred or not.
  801. * @param CANx: where x:[1,2] to to select the CAN peripheral.
  802. * @param CAN_INT: specify the CAN interrupt source to check.
  803. * This parameter can be:
  804. * @arg CAN_INT_TME
  805. * @arg CAN_INT_RFNE0
  806. * @arg CAN_INT_RFF0
  807. * @arg CAN_INT_RFO0
  808. * @arg CAN_INT_RFNE1
  809. * @arg CAN_INT_RFF1
  810. * @arg CAN_INT_RFO1
  811. * @arg CAN_INT_WE
  812. * @arg CAN_INT_PE
  813. * @arg CAN_INT_BOE
  814. * @arg CAN_INT_ET
  815. * @arg CAN_INT_ERR
  816. * @arg CAN_INT_WU
  817. * @arg CAN_INT_SLP
  818. * @retval The current state of CAN_INT (SET or RESET).
  819. */
  820. TypeState CAN_GetIntBitState(CAN_TypeDef *CANx, uint32_t CAN_INT)
  821. {
  822. TypeState intstate = RESET;
  823. /* Get the enable interrupt bit */
  824. if ((CANx->IER & CAN_INT) != RESET) {
  825. switch (CAN_INT) {
  826. case CAN_INT_TME:
  827. /* Check CAN_TSTR_MTFx bits */
  828. intstate = CheckINTState(CANx->TSTR, CAN_TSTR_MTF0 | CAN_TSTR_MTF1 | CAN_TSTR_MTF2);
  829. break;
  830. case CAN_INT_RFNE0:
  831. /* Check CAN_RFR0_RFL0 bit */
  832. intstate = CheckINTState(CANx->RFR0, CAN_RFR0_RFL0);
  833. break;
  834. case CAN_INT_RFF0:
  835. /* Check CAN_RFR0_RFF0 bit */
  836. intstate = CheckINTState(CANx->RFR0, CAN_RFR0_RFF0);
  837. break;
  838. case CAN_INT_RFO0:
  839. /* Check CAN_RFR0_RFO0 bit */
  840. intstate = CheckINTState(CANx->RFR0, CAN_RFR0_RFO0);
  841. break;
  842. case CAN_INT_RFNE1:
  843. /* Check CAN_RFR1_RFL1 bit */
  844. intstate = CheckINTState(CANx->RFR1, CAN_RFR1_RFL1);
  845. break;
  846. case CAN_INT_RFF1:
  847. /* Check CAN_RFR1_RFF1 bit */
  848. intstate = CheckINTState(CANx->RFR1, CAN_RFR1_RFF1);
  849. break;
  850. case CAN_INT_RFO1:
  851. /* Check CAN_RFR1_RFO1 bit */
  852. intstate = CheckINTState(CANx->RFR1, CAN_RFR1_RFO1);
  853. break;
  854. case CAN_INT_WU:
  855. /* Check CAN_STR_WIF bit */
  856. intstate = CheckINTState(CANx->STR, CAN_STR_WIF);
  857. break;
  858. case CAN_INT_SLP:
  859. /* Check CAN_STR_SEIF bit */
  860. intstate = CheckINTState(CANx->STR, CAN_STR_SEIF);
  861. break;
  862. case CAN_INT_WE:
  863. /* Check CAN_INT_WE bit */
  864. intstate = CheckINTState(CANx->ER, CAN_ER_WE);
  865. break;
  866. case CAN_INT_PE:
  867. /* Check CAN_INT_EP bit */
  868. intstate = CheckINTState(CANx->ER, CAN_ER_PE);
  869. break;
  870. case CAN_INT_BOE:
  871. /* Check CAN_ER_BOE bit */
  872. intstate = CheckINTState(CANx->ER, CAN_ER_BOE);
  873. break;
  874. case CAN_INT_ET:
  875. /* Check CAN_ER_ET bit */
  876. intstate = CheckINTState(CANx->ER, CAN_ER_ET);
  877. break;
  878. case CAN_INT_ERR:
  879. /* Check CAN_STR_EIF bit */
  880. intstate = CheckINTState(CANx->STR, CAN_STR_EIF);
  881. break;
  882. default :
  883. /* in case of error, return RESET */
  884. intstate = RESET;
  885. break;
  886. }
  887. } else {
  888. /* in case the Interrupt is not enabled, return RESET */
  889. intstate = RESET;
  890. }
  891. /* Return the CAN_INT status */
  892. return intstate;
  893. }
  894. /**
  895. * @brief Clear the CANx's interrupt pending bits.
  896. * @param CANx: where x:[1,2] to to select the CAN peripheral.
  897. * @param CAN_INT: specify the interrupt pending bit to clear.
  898. * This parameter can be:
  899. * @arg CAN_INT_TME
  900. * @arg CAN_INT_RFF0
  901. * @arg CAN_INT_RFO0
  902. * @arg CAN_INT_RFF1
  903. * @arg CAN_INT_RFO1
  904. * @arg CAN_INT_WE
  905. * @arg CAN_INT_PE
  906. * @arg CAN_INT_BOE
  907. * @arg CAN_INT_ET
  908. * @arg CAN_INT_ERR
  909. * @arg CAN_INT_WU
  910. * @arg CAN_INT_SLP
  911. * @retval None.
  912. */
  913. void CAN_ClearIntBitState(CAN_TypeDef *CANx, uint32_t CAN_INT)
  914. {
  915. switch (CAN_INT) {
  916. case CAN_INT_TME:
  917. /* Clear CAN_TSTR_MTFx (rc_w1)*/
  918. CANx->TSTR = CAN_TSTR_MTF0 | CAN_TSTR_MTF1 | CAN_TSTR_MTF2;
  919. break;
  920. case CAN_INT_RFF0:
  921. /* Clear CAN_RFR0_RFF0 (rc_w1)*/
  922. CANx->RFR0 = CAN_RFR0_RFF0;
  923. break;
  924. case CAN_INT_RFO0:
  925. /* Clear CAN_RFR0_RFO0 (rc_w1)*/
  926. CANx->RFR0 = CAN_RFR0_RFO0;
  927. break;
  928. case CAN_INT_RFF1:
  929. /* Clear CAN_RFR1_RFF1 (rc_w1)*/
  930. CANx->RFR1 = CAN_RFR1_RFF1;
  931. break;
  932. case CAN_INT_RFO1:
  933. /* Clear CAN_RFR1_RFO1 (rc_w1)*/
  934. CANx->RFR1 = CAN_RFR1_RFO1;
  935. break;
  936. case CAN_INT_WU:
  937. /* Clear CAN_STR_WIF (rc_w1)*/
  938. CANx->STR = CAN_STR_WIF;
  939. break;
  940. case CAN_INT_SLP:
  941. /* Clear CAN_STR_SEIF (rc_w1)*/
  942. CANx->STR = CAN_STR_SEIF;
  943. break;
  944. case CAN_INT_WE:
  945. /* Clear CAN_STR_EIF (rc_w1) */
  946. CANx->STR = CAN_STR_EIF;
  947. break;
  948. case CAN_INT_PE:
  949. /* Clear CAN_MSR_ERRI (rc_w1) */
  950. CANx->STR = CAN_STR_EIF;
  951. break;
  952. case CAN_INT_BOE:
  953. /* Clear CAN_STR_EIF (rc_w1) */
  954. CANx->STR = CAN_STR_EIF;
  955. break;
  956. case CAN_INT_ET:
  957. /* Clear ET bits */
  958. CANx->ER = RESET;
  959. /* Clear CAN_STR_EIF (rc_w1) */
  960. CANx->STR = CAN_STR_EIF;
  961. break;
  962. case CAN_INT_ERR:
  963. /*Clear ET bits */
  964. CANx->ER = RESET;
  965. /* Clear CAN_STR_EIF (rc_w1) */
  966. CANx->STR = CAN_STR_EIF;
  967. break;
  968. default :
  969. break;
  970. }
  971. }
  972. /**
  973. * @brief Check whether the CAN interrupt has occurred or not.
  974. * @param CAN_Reg: the register of CAN interrupt to check.
  975. * @param Int_Bit: the bit of interrupt source to check.
  976. * @retval The new state of the CAN Interrupt (SET or RESET).
  977. */
  978. static TypeState CheckINTState(uint32_t CAN_Reg, uint32_t Int_Bit)
  979. {
  980. if ((CAN_Reg & Int_Bit) != (uint32_t)RESET) {
  981. /* CAN_INT is set */
  982. return SET;
  983. } else {
  984. /* CAN_IT is reset */
  985. return RESET;
  986. }
  987. }
  988. /**
  989. * @}
  990. */
  991. /**
  992. * @}
  993. */
  994. /**
  995. * @}
  996. */