stm32f4xx_can.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_can.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 30-September-2011
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the Controller area network (CAN) peripheral:
  9. * - Initialization and Configuration
  10. * - CAN Frames Transmission
  11. * - CAN Frames Reception
  12. * - Operation modes switch
  13. * - Error management
  14. * - Interrupts and flags
  15. *
  16. * @verbatim
  17. *
  18. * ===================================================================
  19. * How to use this driver
  20. * ===================================================================
  21. * 1. Enable the CAN controller interface clock using
  22. * RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); for CAN1
  23. * and RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, ENABLE); for CAN2
  24. * @note In case you are using CAN2 only, you have to enable the CAN1 clock.
  25. *
  26. * 2. CAN pins configuration
  27. * - Enable the clock for the CAN GPIOs using the following function:
  28. * RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOx, ENABLE);
  29. * - Connect the involved CAN pins to AF9 using the following function
  30. * GPIO_PinAFConfig(GPIOx, GPIO_PinSourcex, GPIO_AF_CANx);
  31. * - Configure these CAN pins in alternate function mode by calling
  32. * the function GPIO_Init();
  33. *
  34. * 3. Initialise and configure the CAN using CAN_Init() and
  35. * CAN_FilterInit() functions.
  36. *
  37. * 4. Transmit the desired CAN frame using CAN_Transmit() function.
  38. *
  39. * 5. Check the transmission of a CAN frame using CAN_TransmitStatus()
  40. * function.
  41. *
  42. * 6. Cancel the transmission of a CAN frame using CAN_CancelTransmit()
  43. * function.
  44. *
  45. * 7. Receive a CAN frame using CAN_Recieve() function.
  46. *
  47. * 8. Release the receive FIFOs using CAN_FIFORelease() function.
  48. *
  49. * 9. Return the number of pending received frames using
  50. * CAN_MessagePending() function.
  51. *
  52. * 10. To control CAN events you can use one of the following two methods:
  53. * - Check on CAN flags using the CAN_GetFlagStatus() function.
  54. * - Use CAN interrupts through the function CAN_ITConfig() at
  55. * initialization phase and CAN_GetITStatus() function into
  56. * interrupt routines to check if the event has occurred or not.
  57. * After checking on a flag you should clear it using CAN_ClearFlag()
  58. * function. And after checking on an interrupt event you should
  59. * clear it using CAN_ClearITPendingBit() function.
  60. *
  61. *
  62. * @endverbatim
  63. *
  64. ******************************************************************************
  65. * @attention
  66. *
  67. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  68. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  69. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  70. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  71. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  72. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  73. *
  74. * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  75. ******************************************************************************
  76. */
  77. /* Includes ------------------------------------------------------------------*/
  78. #include "stm32f4xx_can.h"
  79. #include "stm32f4xx_rcc.h"
  80. /** @addtogroup STM32F4xx_StdPeriph_Driver
  81. * @{
  82. */
  83. /** @defgroup CAN
  84. * @brief CAN driver modules
  85. * @{
  86. */
  87. /* Private typedef -----------------------------------------------------------*/
  88. /* Private define ------------------------------------------------------------*/
  89. /* CAN Master Control Register bits */
  90. #define MCR_DBF ((uint32_t)0x00010000) /* software master reset */
  91. /* CAN Mailbox Transmit Request */
  92. #define TMIDxR_TXRQ ((uint32_t)0x00000001) /* Transmit mailbox request */
  93. /* CAN Filter Master Register bits */
  94. #define FMR_FINIT ((uint32_t)0x00000001) /* Filter init mode */
  95. /* Time out for INAK bit */
  96. #define INAK_TIMEOUT ((uint32_t)0x0000FFFF)
  97. /* Time out for SLAK bit */
  98. #define SLAK_TIMEOUT ((uint32_t)0x0000FFFF)
  99. /* Flags in TSR register */
  100. #define CAN_FLAGS_TSR ((uint32_t)0x08000000)
  101. /* Flags in RF1R register */
  102. #define CAN_FLAGS_RF1R ((uint32_t)0x04000000)
  103. /* Flags in RF0R register */
  104. #define CAN_FLAGS_RF0R ((uint32_t)0x02000000)
  105. /* Flags in MSR register */
  106. #define CAN_FLAGS_MSR ((uint32_t)0x01000000)
  107. /* Flags in ESR register */
  108. #define CAN_FLAGS_ESR ((uint32_t)0x00F00000)
  109. /* Mailboxes definition */
  110. #define CAN_TXMAILBOX_0 ((uint8_t)0x00)
  111. #define CAN_TXMAILBOX_1 ((uint8_t)0x01)
  112. #define CAN_TXMAILBOX_2 ((uint8_t)0x02)
  113. #define CAN_MODE_MASK ((uint32_t) 0x00000003)
  114. /* Private macro -------------------------------------------------------------*/
  115. /* Private variables ---------------------------------------------------------*/
  116. /* Private function prototypes -----------------------------------------------*/
  117. /* Private functions ---------------------------------------------------------*/
  118. static ITStatus CheckITStatus(uint32_t CAN_Reg, uint32_t It_Bit);
  119. /** @defgroup CAN_Private_Functions
  120. * @{
  121. */
  122. /** @defgroup CAN_Group1 Initialization and Configuration functions
  123. * @brief Initialization and Configuration functions
  124. *
  125. @verbatim
  126. ===============================================================================
  127. Initialization and Configuration functions
  128. ===============================================================================
  129. This section provides functions allowing to
  130. - Initialize the CAN peripherals : Prescaler, operating mode, the maximum number
  131. of time quanta to perform resynchronization, the number of time quanta in
  132. Bit Segment 1 and 2 and many other modes.
  133. Refer to @ref CAN_InitTypeDef for more details.
  134. - Configures the CAN reception filter.
  135. - Select the start bank filter for slave CAN.
  136. - Enables or disables the Debug Freeze mode for CAN
  137. - Enables or disables the CAN Time Trigger Operation communication mode
  138. @endverbatim
  139. * @{
  140. */
  141. /**
  142. * @brief Deinitializes the CAN peripheral registers to their default reset values.
  143. * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
  144. * @retval None.
  145. */
  146. void CAN_DeInit(CAN_TypeDef* CANx)
  147. {
  148. /* Check the parameters */
  149. assert_param(IS_CAN_ALL_PERIPH(CANx));
  150. if (CANx == CAN1)
  151. {
  152. /* Enable CAN1 reset state */
  153. RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN1, ENABLE);
  154. /* Release CAN1 from reset state */
  155. RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN1, DISABLE);
  156. }
  157. else
  158. {
  159. /* Enable CAN2 reset state */
  160. RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN2, ENABLE);
  161. /* Release CAN2 from reset state */
  162. RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN2, DISABLE);
  163. }
  164. }
  165. /**
  166. * @brief Initializes the CAN peripheral according to the specified
  167. * parameters in the CAN_InitStruct.
  168. * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
  169. * @param CAN_InitStruct: pointer to a CAN_InitTypeDef structure that contains
  170. * the configuration information for the CAN peripheral.
  171. * @retval Constant indicates initialization succeed which will be
  172. * CAN_InitStatus_Failed or CAN_InitStatus_Success.
  173. */
  174. uint8_t CAN_Init(CAN_TypeDef* CANx, CAN_InitTypeDef* CAN_InitStruct)
  175. {
  176. uint8_t InitStatus = CAN_InitStatus_Failed;
  177. uint32_t wait_ack = 0x00000000;
  178. /* Check the parameters */
  179. assert_param(IS_CAN_ALL_PERIPH(CANx));
  180. assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TTCM));
  181. assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_ABOM));
  182. assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_AWUM));
  183. assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_NART));
  184. assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_RFLM));
  185. assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TXFP));
  186. assert_param(IS_CAN_MODE(CAN_InitStruct->CAN_Mode));
  187. assert_param(IS_CAN_SJW(CAN_InitStruct->CAN_SJW));
  188. assert_param(IS_CAN_BS1(CAN_InitStruct->CAN_BS1));
  189. assert_param(IS_CAN_BS2(CAN_InitStruct->CAN_BS2));
  190. assert_param(IS_CAN_PRESCALER(CAN_InitStruct->CAN_Prescaler));
  191. /* Exit from sleep mode */
  192. CANx->MCR &= (~(uint32_t)CAN_MCR_SLEEP);
  193. /* Request initialisation */
  194. CANx->MCR |= CAN_MCR_INRQ ;
  195. /* Wait the acknowledge */
  196. while (((CANx->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) && (wait_ack != INAK_TIMEOUT))
  197. {
  198. wait_ack++;
  199. }
  200. /* Check acknowledge */
  201. if ((CANx->MSR & CAN_MSR_INAK) != CAN_MSR_INAK)
  202. {
  203. InitStatus = CAN_InitStatus_Failed;
  204. }
  205. else
  206. {
  207. /* Set the time triggered communication mode */
  208. if (CAN_InitStruct->CAN_TTCM == ENABLE)
  209. {
  210. CANx->MCR |= CAN_MCR_TTCM;
  211. }
  212. else
  213. {
  214. CANx->MCR &= ~(uint32_t)CAN_MCR_TTCM;
  215. }
  216. /* Set the automatic bus-off management */
  217. if (CAN_InitStruct->CAN_ABOM == ENABLE)
  218. {
  219. CANx->MCR |= CAN_MCR_ABOM;
  220. }
  221. else
  222. {
  223. CANx->MCR &= ~(uint32_t)CAN_MCR_ABOM;
  224. }
  225. /* Set the automatic wake-up mode */
  226. if (CAN_InitStruct->CAN_AWUM == ENABLE)
  227. {
  228. CANx->MCR |= CAN_MCR_AWUM;
  229. }
  230. else
  231. {
  232. CANx->MCR &= ~(uint32_t)CAN_MCR_AWUM;
  233. }
  234. /* Set the no automatic retransmission */
  235. if (CAN_InitStruct->CAN_NART == ENABLE)
  236. {
  237. CANx->MCR |= CAN_MCR_NART;
  238. }
  239. else
  240. {
  241. CANx->MCR &= ~(uint32_t)CAN_MCR_NART;
  242. }
  243. /* Set the receive FIFO locked mode */
  244. if (CAN_InitStruct->CAN_RFLM == ENABLE)
  245. {
  246. CANx->MCR |= CAN_MCR_RFLM;
  247. }
  248. else
  249. {
  250. CANx->MCR &= ~(uint32_t)CAN_MCR_RFLM;
  251. }
  252. /* Set the transmit FIFO priority */
  253. if (CAN_InitStruct->CAN_TXFP == ENABLE)
  254. {
  255. CANx->MCR |= CAN_MCR_TXFP;
  256. }
  257. else
  258. {
  259. CANx->MCR &= ~(uint32_t)CAN_MCR_TXFP;
  260. }
  261. /* Set the bit timing register */
  262. CANx->BTR = (uint32_t)((uint32_t)CAN_InitStruct->CAN_Mode << 30) | \
  263. ((uint32_t)CAN_InitStruct->CAN_SJW << 24) | \
  264. ((uint32_t)CAN_InitStruct->CAN_BS1 << 16) | \
  265. ((uint32_t)CAN_InitStruct->CAN_BS2 << 20) | \
  266. ((uint32_t)CAN_InitStruct->CAN_Prescaler - 1);
  267. /* Request leave initialisation */
  268. CANx->MCR &= ~(uint32_t)CAN_MCR_INRQ;
  269. /* Wait the acknowledge */
  270. wait_ack = 0;
  271. while (((CANx->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) && (wait_ack != INAK_TIMEOUT))
  272. {
  273. wait_ack++;
  274. }
  275. /* ...and check acknowledged */
  276. if ((CANx->MSR & CAN_MSR_INAK) == CAN_MSR_INAK)
  277. {
  278. InitStatus = CAN_InitStatus_Failed;
  279. }
  280. else
  281. {
  282. InitStatus = CAN_InitStatus_Success ;
  283. }
  284. }
  285. /* At this step, return the status of initialization */
  286. return InitStatus;
  287. }
  288. /**
  289. * @brief Configures the CAN reception filter according to the specified
  290. * parameters in the CAN_FilterInitStruct.
  291. * @param CAN_FilterInitStruct: pointer to a CAN_FilterInitTypeDef structure that
  292. * contains the configuration information.
  293. * @retval None
  294. */
  295. void CAN_FilterInit(CAN_FilterInitTypeDef* CAN_FilterInitStruct)
  296. {
  297. uint32_t filter_number_bit_pos = 0;
  298. /* Check the parameters */
  299. assert_param(IS_CAN_FILTER_NUMBER(CAN_FilterInitStruct->CAN_FilterNumber));
  300. assert_param(IS_CAN_FILTER_MODE(CAN_FilterInitStruct->CAN_FilterMode));
  301. assert_param(IS_CAN_FILTER_SCALE(CAN_FilterInitStruct->CAN_FilterScale));
  302. assert_param(IS_CAN_FILTER_FIFO(CAN_FilterInitStruct->CAN_FilterFIFOAssignment));
  303. assert_param(IS_FUNCTIONAL_STATE(CAN_FilterInitStruct->CAN_FilterActivation));
  304. filter_number_bit_pos = ((uint32_t)1) << CAN_FilterInitStruct->CAN_FilterNumber;
  305. /* Initialisation mode for the filter */
  306. CAN1->FMR |= FMR_FINIT;
  307. /* Filter Deactivation */
  308. CAN1->FA1R &= ~(uint32_t)filter_number_bit_pos;
  309. /* Filter Scale */
  310. if (CAN_FilterInitStruct->CAN_FilterScale == CAN_FilterScale_16bit)
  311. {
  312. /* 16-bit scale for the filter */
  313. CAN1->FS1R &= ~(uint32_t)filter_number_bit_pos;
  314. /* First 16-bit identifier and First 16-bit mask */
  315. /* Or First 16-bit identifier and Second 16-bit identifier */
  316. CAN1->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR1 =
  317. ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdLow) << 16) |
  318. (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdLow);
  319. /* Second 16-bit identifier and Second 16-bit mask */
  320. /* Or Third 16-bit identifier and Fourth 16-bit identifier */
  321. CAN1->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR2 =
  322. ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdHigh) << 16) |
  323. (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdHigh);
  324. }
  325. if (CAN_FilterInitStruct->CAN_FilterScale == CAN_FilterScale_32bit)
  326. {
  327. /* 32-bit scale for the filter */
  328. CAN1->FS1R |= filter_number_bit_pos;
  329. /* 32-bit identifier or First 32-bit identifier */
  330. CAN1->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR1 =
  331. ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdHigh) << 16) |
  332. (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdLow);
  333. /* 32-bit mask or Second 32-bit identifier */
  334. CAN1->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR2 =
  335. ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdHigh) << 16) |
  336. (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdLow);
  337. }
  338. /* Filter Mode */
  339. if (CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdMask)
  340. {
  341. /*Id/Mask mode for the filter*/
  342. CAN1->FM1R &= ~(uint32_t)filter_number_bit_pos;
  343. }
  344. else /* CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdList */
  345. {
  346. /*Identifier list mode for the filter*/
  347. CAN1->FM1R |= (uint32_t)filter_number_bit_pos;
  348. }
  349. /* Filter FIFO assignment */
  350. if (CAN_FilterInitStruct->CAN_FilterFIFOAssignment == CAN_Filter_FIFO0)
  351. {
  352. /* FIFO 0 assignation for the filter */
  353. CAN1->FFA1R &= ~(uint32_t)filter_number_bit_pos;
  354. }
  355. if (CAN_FilterInitStruct->CAN_FilterFIFOAssignment == CAN_Filter_FIFO1)
  356. {
  357. /* FIFO 1 assignation for the filter */
  358. CAN1->FFA1R |= (uint32_t)filter_number_bit_pos;
  359. }
  360. /* Filter activation */
  361. if (CAN_FilterInitStruct->CAN_FilterActivation == ENABLE)
  362. {
  363. CAN1->FA1R |= filter_number_bit_pos;
  364. }
  365. /* Leave the initialisation mode for the filter */
  366. CAN1->FMR &= ~FMR_FINIT;
  367. }
  368. /**
  369. * @brief Fills each CAN_InitStruct member with its default value.
  370. * @param CAN_InitStruct: pointer to a CAN_InitTypeDef structure which ill be initialized.
  371. * @retval None
  372. */
  373. void CAN_StructInit(CAN_InitTypeDef* CAN_InitStruct)
  374. {
  375. /* Reset CAN init structure parameters values */
  376. /* Initialize the time triggered communication mode */
  377. CAN_InitStruct->CAN_TTCM = DISABLE;
  378. /* Initialize the automatic bus-off management */
  379. CAN_InitStruct->CAN_ABOM = DISABLE;
  380. /* Initialize the automatic wake-up mode */
  381. CAN_InitStruct->CAN_AWUM = DISABLE;
  382. /* Initialize the no automatic retransmission */
  383. CAN_InitStruct->CAN_NART = DISABLE;
  384. /* Initialize the receive FIFO locked mode */
  385. CAN_InitStruct->CAN_RFLM = DISABLE;
  386. /* Initialize the transmit FIFO priority */
  387. CAN_InitStruct->CAN_TXFP = DISABLE;
  388. /* Initialize the CAN_Mode member */
  389. CAN_InitStruct->CAN_Mode = CAN_Mode_Normal;
  390. /* Initialize the CAN_SJW member */
  391. CAN_InitStruct->CAN_SJW = CAN_SJW_1tq;
  392. /* Initialize the CAN_BS1 member */
  393. CAN_InitStruct->CAN_BS1 = CAN_BS1_4tq;
  394. /* Initialize the CAN_BS2 member */
  395. CAN_InitStruct->CAN_BS2 = CAN_BS2_3tq;
  396. /* Initialize the CAN_Prescaler member */
  397. CAN_InitStruct->CAN_Prescaler = 1;
  398. }
  399. /**
  400. * @brief Select the start bank filter for slave CAN.
  401. * @param CAN_BankNumber: Select the start slave bank filter from 1..27.
  402. * @retval None
  403. */
  404. void CAN_SlaveStartBank(uint8_t CAN_BankNumber)
  405. {
  406. /* Check the parameters */
  407. assert_param(IS_CAN_BANKNUMBER(CAN_BankNumber));
  408. /* Enter Initialisation mode for the filter */
  409. CAN1->FMR |= FMR_FINIT;
  410. /* Select the start slave bank */
  411. CAN1->FMR &= (uint32_t)0xFFFFC0F1 ;
  412. CAN1->FMR |= (uint32_t)(CAN_BankNumber)<<8;
  413. /* Leave Initialisation mode for the filter */
  414. CAN1->FMR &= ~FMR_FINIT;
  415. }
  416. /**
  417. * @brief Enables or disables the DBG Freeze for CAN.
  418. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  419. * @param NewState: new state of the CAN peripheral.
  420. * This parameter can be: ENABLE (CAN reception/transmission is frozen
  421. * during debug. Reception FIFOs can still be accessed/controlled normally)
  422. * or DISABLE (CAN is working during debug).
  423. * @retval None
  424. */
  425. void CAN_DBGFreeze(CAN_TypeDef* CANx, FunctionalState NewState)
  426. {
  427. /* Check the parameters */
  428. assert_param(IS_CAN_ALL_PERIPH(CANx));
  429. assert_param(IS_FUNCTIONAL_STATE(NewState));
  430. if (NewState != DISABLE)
  431. {
  432. /* Enable Debug Freeze */
  433. CANx->MCR |= MCR_DBF;
  434. }
  435. else
  436. {
  437. /* Disable Debug Freeze */
  438. CANx->MCR &= ~MCR_DBF;
  439. }
  440. }
  441. /**
  442. * @brief Enables or disables the CAN Time TriggerOperation communication mode.
  443. * @note DLC must be programmed as 8 in order Time Stamp (2 bytes) to be
  444. * sent over the CAN bus.
  445. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  446. * @param NewState: Mode new state. This parameter can be: ENABLE or DISABLE.
  447. * When enabled, Time stamp (TIME[15:0]) value is sent in the last two
  448. * data bytes of the 8-byte message: TIME[7:0] in data byte 6 and TIME[15:8]
  449. * in data byte 7.
  450. * @retval None
  451. */
  452. void CAN_TTComModeCmd(CAN_TypeDef* CANx, FunctionalState NewState)
  453. {
  454. /* Check the parameters */
  455. assert_param(IS_CAN_ALL_PERIPH(CANx));
  456. assert_param(IS_FUNCTIONAL_STATE(NewState));
  457. if (NewState != DISABLE)
  458. {
  459. /* Enable the TTCM mode */
  460. CANx->MCR |= CAN_MCR_TTCM;
  461. /* Set TGT bits */
  462. CANx->sTxMailBox[0].TDTR |= ((uint32_t)CAN_TDT0R_TGT);
  463. CANx->sTxMailBox[1].TDTR |= ((uint32_t)CAN_TDT1R_TGT);
  464. CANx->sTxMailBox[2].TDTR |= ((uint32_t)CAN_TDT2R_TGT);
  465. }
  466. else
  467. {
  468. /* Disable the TTCM mode */
  469. CANx->MCR &= (uint32_t)(~(uint32_t)CAN_MCR_TTCM);
  470. /* Reset TGT bits */
  471. CANx->sTxMailBox[0].TDTR &= ((uint32_t)~CAN_TDT0R_TGT);
  472. CANx->sTxMailBox[1].TDTR &= ((uint32_t)~CAN_TDT1R_TGT);
  473. CANx->sTxMailBox[2].TDTR &= ((uint32_t)~CAN_TDT2R_TGT);
  474. }
  475. }
  476. /**
  477. * @}
  478. */
  479. /** @defgroup CAN_Group2 CAN Frames Transmission functions
  480. * @brief CAN Frames Transmission functions
  481. *
  482. @verbatim
  483. ===============================================================================
  484. CAN Frames Transmission functions
  485. ===============================================================================
  486. This section provides functions allowing to
  487. - Initiate and transmit a CAN frame message (if there is an empty mailbox).
  488. - Check the transmission status of a CAN Frame
  489. - Cancel a transmit request
  490. @endverbatim
  491. * @{
  492. */
  493. /**
  494. * @brief Initiates and transmits a CAN frame message.
  495. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  496. * @param TxMessage: pointer to a structure which contains CAN Id, CAN DLC and CAN data.
  497. * @retval The number of the mailbox that is used for transmission or
  498. * CAN_TxStatus_NoMailBox if there is no empty mailbox.
  499. */
  500. uint8_t CAN_Transmit(CAN_TypeDef* CANx, CanTxMsg* TxMessage)
  501. {
  502. uint8_t transmit_mailbox = 0;
  503. /* Check the parameters */
  504. assert_param(IS_CAN_ALL_PERIPH(CANx));
  505. assert_param(IS_CAN_IDTYPE(TxMessage->IDE));
  506. assert_param(IS_CAN_RTR(TxMessage->RTR));
  507. assert_param(IS_CAN_DLC(TxMessage->DLC));
  508. /* Select one empty transmit mailbox */
  509. if ((CANx->TSR&CAN_TSR_TME0) == CAN_TSR_TME0)
  510. {
  511. transmit_mailbox = 0;
  512. }
  513. else if ((CANx->TSR&CAN_TSR_TME1) == CAN_TSR_TME1)
  514. {
  515. transmit_mailbox = 1;
  516. }
  517. else if ((CANx->TSR&CAN_TSR_TME2) == CAN_TSR_TME2)
  518. {
  519. transmit_mailbox = 2;
  520. }
  521. else
  522. {
  523. transmit_mailbox = CAN_TxStatus_NoMailBox;
  524. }
  525. if (transmit_mailbox != CAN_TxStatus_NoMailBox)
  526. {
  527. /* Set up the Id */
  528. CANx->sTxMailBox[transmit_mailbox].TIR &= TMIDxR_TXRQ;
  529. if (TxMessage->IDE == CAN_Id_Standard)
  530. {
  531. assert_param(IS_CAN_STDID(TxMessage->StdId));
  532. CANx->sTxMailBox[transmit_mailbox].TIR |= ((TxMessage->StdId << 21) | \
  533. TxMessage->RTR);
  534. }
  535. else
  536. {
  537. assert_param(IS_CAN_EXTID(TxMessage->ExtId));
  538. CANx->sTxMailBox[transmit_mailbox].TIR |= ((TxMessage->ExtId << 3) | \
  539. TxMessage->IDE | \
  540. TxMessage->RTR);
  541. }
  542. /* Set up the DLC */
  543. TxMessage->DLC &= (uint8_t)0x0000000F;
  544. CANx->sTxMailBox[transmit_mailbox].TDTR &= (uint32_t)0xFFFFFFF0;
  545. CANx->sTxMailBox[transmit_mailbox].TDTR |= TxMessage->DLC;
  546. /* Set up the data field */
  547. CANx->sTxMailBox[transmit_mailbox].TDLR = (((uint32_t)TxMessage->Data[3] << 24) |
  548. ((uint32_t)TxMessage->Data[2] << 16) |
  549. ((uint32_t)TxMessage->Data[1] << 8) |
  550. ((uint32_t)TxMessage->Data[0]));
  551. CANx->sTxMailBox[transmit_mailbox].TDHR = (((uint32_t)TxMessage->Data[7] << 24) |
  552. ((uint32_t)TxMessage->Data[6] << 16) |
  553. ((uint32_t)TxMessage->Data[5] << 8) |
  554. ((uint32_t)TxMessage->Data[4]));
  555. /* Request transmission */
  556. CANx->sTxMailBox[transmit_mailbox].TIR |= TMIDxR_TXRQ;
  557. }
  558. return transmit_mailbox;
  559. }
  560. /**
  561. * @brief Checks the transmission status of a CAN Frame.
  562. * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
  563. * @param TransmitMailbox: the number of the mailbox that is used for transmission.
  564. * @retval CAN_TxStatus_Ok if the CAN driver transmits the message,
  565. * CAN_TxStatus_Failed in an other case.
  566. */
  567. uint8_t CAN_TransmitStatus(CAN_TypeDef* CANx, uint8_t TransmitMailbox)
  568. {
  569. uint32_t state = 0;
  570. /* Check the parameters */
  571. assert_param(IS_CAN_ALL_PERIPH(CANx));
  572. assert_param(IS_CAN_TRANSMITMAILBOX(TransmitMailbox));
  573. switch (TransmitMailbox)
  574. {
  575. case (CAN_TXMAILBOX_0):
  576. state = CANx->TSR & (CAN_TSR_RQCP0 | CAN_TSR_TXOK0 | CAN_TSR_TME0);
  577. break;
  578. case (CAN_TXMAILBOX_1):
  579. state = CANx->TSR & (CAN_TSR_RQCP1 | CAN_TSR_TXOK1 | CAN_TSR_TME1);
  580. break;
  581. case (CAN_TXMAILBOX_2):
  582. state = CANx->TSR & (CAN_TSR_RQCP2 | CAN_TSR_TXOK2 | CAN_TSR_TME2);
  583. break;
  584. default:
  585. state = CAN_TxStatus_Failed;
  586. break;
  587. }
  588. switch (state)
  589. {
  590. /* transmit pending */
  591. case (0x0): state = CAN_TxStatus_Pending;
  592. break;
  593. /* transmit failed */
  594. case (CAN_TSR_RQCP0 | CAN_TSR_TME0): state = CAN_TxStatus_Failed;
  595. break;
  596. case (CAN_TSR_RQCP1 | CAN_TSR_TME1): state = CAN_TxStatus_Failed;
  597. break;
  598. case (CAN_TSR_RQCP2 | CAN_TSR_TME2): state = CAN_TxStatus_Failed;
  599. break;
  600. /* transmit succeeded */
  601. case (CAN_TSR_RQCP0 | CAN_TSR_TXOK0 | CAN_TSR_TME0):state = CAN_TxStatus_Ok;
  602. break;
  603. case (CAN_TSR_RQCP1 | CAN_TSR_TXOK1 | CAN_TSR_TME1):state = CAN_TxStatus_Ok;
  604. break;
  605. case (CAN_TSR_RQCP2 | CAN_TSR_TXOK2 | CAN_TSR_TME2):state = CAN_TxStatus_Ok;
  606. break;
  607. default: state = CAN_TxStatus_Failed;
  608. break;
  609. }
  610. return (uint8_t) state;
  611. }
  612. /**
  613. * @brief Cancels a transmit request.
  614. * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
  615. * @param Mailbox: Mailbox number.
  616. * @retval None
  617. */
  618. void CAN_CancelTransmit(CAN_TypeDef* CANx, uint8_t Mailbox)
  619. {
  620. /* Check the parameters */
  621. assert_param(IS_CAN_ALL_PERIPH(CANx));
  622. assert_param(IS_CAN_TRANSMITMAILBOX(Mailbox));
  623. /* abort transmission */
  624. switch (Mailbox)
  625. {
  626. case (CAN_TXMAILBOX_0): CANx->TSR |= CAN_TSR_ABRQ0;
  627. break;
  628. case (CAN_TXMAILBOX_1): CANx->TSR |= CAN_TSR_ABRQ1;
  629. break;
  630. case (CAN_TXMAILBOX_2): CANx->TSR |= CAN_TSR_ABRQ2;
  631. break;
  632. default:
  633. break;
  634. }
  635. }
  636. /**
  637. * @}
  638. */
  639. /** @defgroup CAN_Group3 CAN Frames Reception functions
  640. * @brief CAN Frames Reception functions
  641. *
  642. @verbatim
  643. ===============================================================================
  644. CAN Frames Reception functions
  645. ===============================================================================
  646. This section provides functions allowing to
  647. - Receive a correct CAN frame
  648. - Release a specified receive FIFO (2 FIFOs are available)
  649. - Return the number of the pending received CAN frames
  650. @endverbatim
  651. * @{
  652. */
  653. /**
  654. * @brief Receives a correct CAN frame.
  655. * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
  656. * @param FIFONumber: Receive FIFO number, CAN_FIFO0 or CAN_FIFO1.
  657. * @param RxMessage: pointer to a structure receive frame which contains CAN Id,
  658. * CAN DLC, CAN data and FMI number.
  659. * @retval None
  660. */
  661. void CAN_Receive(CAN_TypeDef* CANx, uint8_t FIFONumber, CanRxMsg* RxMessage)
  662. {
  663. /* Check the parameters */
  664. assert_param(IS_CAN_ALL_PERIPH(CANx));
  665. assert_param(IS_CAN_FIFO(FIFONumber));
  666. /* Get the Id */
  667. RxMessage->IDE = (uint8_t)0x04 & CANx->sFIFOMailBox[FIFONumber].RIR;
  668. if (RxMessage->IDE == CAN_Id_Standard)
  669. {
  670. RxMessage->StdId = (uint32_t)0x000007FF & (CANx->sFIFOMailBox[FIFONumber].RIR >> 21);
  671. }
  672. else
  673. {
  674. RxMessage->ExtId = (uint32_t)0x1FFFFFFF & (CANx->sFIFOMailBox[FIFONumber].RIR >> 3);
  675. }
  676. RxMessage->RTR = (uint8_t)0x02 & CANx->sFIFOMailBox[FIFONumber].RIR;
  677. /* Get the DLC */
  678. RxMessage->DLC = (uint8_t)0x0F & CANx->sFIFOMailBox[FIFONumber].RDTR;
  679. /* Get the FMI */
  680. RxMessage->FMI = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDTR >> 8);
  681. /* Get the data field */
  682. RxMessage->Data[0] = (uint8_t)0xFF & CANx->sFIFOMailBox[FIFONumber].RDLR;
  683. RxMessage->Data[1] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDLR >> 8);
  684. RxMessage->Data[2] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDLR >> 16);
  685. RxMessage->Data[3] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDLR >> 24);
  686. RxMessage->Data[4] = (uint8_t)0xFF & CANx->sFIFOMailBox[FIFONumber].RDHR;
  687. RxMessage->Data[5] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDHR >> 8);
  688. RxMessage->Data[6] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDHR >> 16);
  689. RxMessage->Data[7] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDHR >> 24);
  690. /* Release the FIFO */
  691. /* Release FIFO0 */
  692. if (FIFONumber == CAN_FIFO0)
  693. {
  694. CANx->RF0R |= CAN_RF0R_RFOM0;
  695. }
  696. /* Release FIFO1 */
  697. else /* FIFONumber == CAN_FIFO1 */
  698. {
  699. CANx->RF1R |= CAN_RF1R_RFOM1;
  700. }
  701. }
  702. /**
  703. * @brief Releases the specified receive FIFO.
  704. * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
  705. * @param FIFONumber: FIFO to release, CAN_FIFO0 or CAN_FIFO1.
  706. * @retval None
  707. */
  708. void CAN_FIFORelease(CAN_TypeDef* CANx, uint8_t FIFONumber)
  709. {
  710. /* Check the parameters */
  711. assert_param(IS_CAN_ALL_PERIPH(CANx));
  712. assert_param(IS_CAN_FIFO(FIFONumber));
  713. /* Release FIFO0 */
  714. if (FIFONumber == CAN_FIFO0)
  715. {
  716. CANx->RF0R |= CAN_RF0R_RFOM0;
  717. }
  718. /* Release FIFO1 */
  719. else /* FIFONumber == CAN_FIFO1 */
  720. {
  721. CANx->RF1R |= CAN_RF1R_RFOM1;
  722. }
  723. }
  724. /**
  725. * @brief Returns the number of pending received messages.
  726. * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
  727. * @param FIFONumber: Receive FIFO number, CAN_FIFO0 or CAN_FIFO1.
  728. * @retval NbMessage : which is the number of pending message.
  729. */
  730. uint8_t CAN_MessagePending(CAN_TypeDef* CANx, uint8_t FIFONumber)
  731. {
  732. uint8_t message_pending=0;
  733. /* Check the parameters */
  734. assert_param(IS_CAN_ALL_PERIPH(CANx));
  735. assert_param(IS_CAN_FIFO(FIFONumber));
  736. if (FIFONumber == CAN_FIFO0)
  737. {
  738. message_pending = (uint8_t)(CANx->RF0R&(uint32_t)0x03);
  739. }
  740. else if (FIFONumber == CAN_FIFO1)
  741. {
  742. message_pending = (uint8_t)(CANx->RF1R&(uint32_t)0x03);
  743. }
  744. else
  745. {
  746. message_pending = 0;
  747. }
  748. return message_pending;
  749. }
  750. /**
  751. * @}
  752. */
  753. /** @defgroup CAN_Group4 CAN Operation modes functions
  754. * @brief CAN Operation modes functions
  755. *
  756. @verbatim
  757. ===============================================================================
  758. CAN Operation modes functions
  759. ===============================================================================
  760. This section provides functions allowing to select the CAN Operation modes
  761. - sleep mode
  762. - normal mode
  763. - initialization mode
  764. @endverbatim
  765. * @{
  766. */
  767. /**
  768. * @brief Selects the CAN Operation mode.
  769. * @param CAN_OperatingMode: CAN Operating Mode.
  770. * This parameter can be one of @ref CAN_OperatingMode_TypeDef enumeration.
  771. * @retval status of the requested mode which can be
  772. * - CAN_ModeStatus_Failed: CAN failed entering the specific mode
  773. * - CAN_ModeStatus_Success: CAN Succeed entering the specific mode
  774. */
  775. uint8_t CAN_OperatingModeRequest(CAN_TypeDef* CANx, uint8_t CAN_OperatingMode)
  776. {
  777. uint8_t status = CAN_ModeStatus_Failed;
  778. /* Timeout for INAK or also for SLAK bits*/
  779. uint32_t timeout = INAK_TIMEOUT;
  780. /* Check the parameters */
  781. assert_param(IS_CAN_ALL_PERIPH(CANx));
  782. assert_param(IS_CAN_OPERATING_MODE(CAN_OperatingMode));
  783. if (CAN_OperatingMode == CAN_OperatingMode_Initialization)
  784. {
  785. /* Request initialisation */
  786. CANx->MCR = (uint32_t)((CANx->MCR & (uint32_t)(~(uint32_t)CAN_MCR_SLEEP)) | CAN_MCR_INRQ);
  787. /* Wait the acknowledge */
  788. while (((CANx->MSR & CAN_MODE_MASK) != CAN_MSR_INAK) && (timeout != 0))
  789. {
  790. timeout--;
  791. }
  792. if ((CANx->MSR & CAN_MODE_MASK) != CAN_MSR_INAK)
  793. {
  794. status = CAN_ModeStatus_Failed;
  795. }
  796. else
  797. {
  798. status = CAN_ModeStatus_Success;
  799. }
  800. }
  801. else if (CAN_OperatingMode == CAN_OperatingMode_Normal)
  802. {
  803. /* Request leave initialisation and sleep mode and enter Normal mode */
  804. CANx->MCR &= (uint32_t)(~(CAN_MCR_SLEEP|CAN_MCR_INRQ));
  805. /* Wait the acknowledge */
  806. while (((CANx->MSR & CAN_MODE_MASK) != 0) && (timeout!=0))
  807. {
  808. timeout--;
  809. }
  810. if ((CANx->MSR & CAN_MODE_MASK) != 0)
  811. {
  812. status = CAN_ModeStatus_Failed;
  813. }
  814. else
  815. {
  816. status = CAN_ModeStatus_Success;
  817. }
  818. }
  819. else if (CAN_OperatingMode == CAN_OperatingMode_Sleep)
  820. {
  821. /* Request Sleep mode */
  822. CANx->MCR = (uint32_t)((CANx->MCR & (uint32_t)(~(uint32_t)CAN_MCR_INRQ)) | CAN_MCR_SLEEP);
  823. /* Wait the acknowledge */
  824. while (((CANx->MSR & CAN_MODE_MASK) != CAN_MSR_SLAK) && (timeout!=0))
  825. {
  826. timeout--;
  827. }
  828. if ((CANx->MSR & CAN_MODE_MASK) != CAN_MSR_SLAK)
  829. {
  830. status = CAN_ModeStatus_Failed;
  831. }
  832. else
  833. {
  834. status = CAN_ModeStatus_Success;
  835. }
  836. }
  837. else
  838. {
  839. status = CAN_ModeStatus_Failed;
  840. }
  841. return (uint8_t) status;
  842. }
  843. /**
  844. * @brief Enters the Sleep (low power) mode.
  845. * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
  846. * @retval CAN_Sleep_Ok if sleep entered, CAN_Sleep_Failed otherwise.
  847. */
  848. uint8_t CAN_Sleep(CAN_TypeDef* CANx)
  849. {
  850. uint8_t sleepstatus = CAN_Sleep_Failed;
  851. /* Check the parameters */
  852. assert_param(IS_CAN_ALL_PERIPH(CANx));
  853. /* Request Sleep mode */
  854. CANx->MCR = (((CANx->MCR) & (uint32_t)(~(uint32_t)CAN_MCR_INRQ)) | CAN_MCR_SLEEP);
  855. /* Sleep mode status */
  856. if ((CANx->MSR & (CAN_MSR_SLAK|CAN_MSR_INAK)) == CAN_MSR_SLAK)
  857. {
  858. /* Sleep mode not entered */
  859. sleepstatus = CAN_Sleep_Ok;
  860. }
  861. /* return sleep mode status */
  862. return (uint8_t)sleepstatus;
  863. }
  864. /**
  865. * @brief Wakes up the CAN peripheral from sleep mode .
  866. * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
  867. * @retval CAN_WakeUp_Ok if sleep mode left, CAN_WakeUp_Failed otherwise.
  868. */
  869. uint8_t CAN_WakeUp(CAN_TypeDef* CANx)
  870. {
  871. uint32_t wait_slak = SLAK_TIMEOUT;
  872. uint8_t wakeupstatus = CAN_WakeUp_Failed;
  873. /* Check the parameters */
  874. assert_param(IS_CAN_ALL_PERIPH(CANx));
  875. /* Wake up request */
  876. CANx->MCR &= ~(uint32_t)CAN_MCR_SLEEP;
  877. /* Sleep mode status */
  878. while(((CANx->MSR & CAN_MSR_SLAK) == CAN_MSR_SLAK)&&(wait_slak!=0x00))
  879. {
  880. wait_slak--;
  881. }
  882. if((CANx->MSR & CAN_MSR_SLAK) != CAN_MSR_SLAK)
  883. {
  884. /* wake up done : Sleep mode exited */
  885. wakeupstatus = CAN_WakeUp_Ok;
  886. }
  887. /* return wakeup status */
  888. return (uint8_t)wakeupstatus;
  889. }
  890. /**
  891. * @}
  892. */
  893. /** @defgroup CAN_Group5 CAN Bus Error management functions
  894. * @brief CAN Bus Error management functions
  895. *
  896. @verbatim
  897. ===============================================================================
  898. CAN Bus Error management functions
  899. ===============================================================================
  900. This section provides functions allowing to
  901. - Return the CANx's last error code (LEC)
  902. - Return the CANx Receive Error Counter (REC)
  903. - Return the LSB of the 9-bit CANx Transmit Error Counter(TEC).
  904. @note If TEC is greater than 255, The CAN is in bus-off state.
  905. @note if REC or TEC are greater than 96, an Error warning flag occurs.
  906. @note if REC or TEC are greater than 127, an Error Passive Flag occurs.
  907. @endverbatim
  908. * @{
  909. */
  910. /**
  911. * @brief Returns the CANx's last error code (LEC).
  912. * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
  913. * @retval Error code:
  914. * - CAN_ERRORCODE_NoErr: No Error
  915. * - CAN_ERRORCODE_StuffErr: Stuff Error
  916. * - CAN_ERRORCODE_FormErr: Form Error
  917. * - CAN_ERRORCODE_ACKErr : Acknowledgment Error
  918. * - CAN_ERRORCODE_BitRecessiveErr: Bit Recessive Error
  919. * - CAN_ERRORCODE_BitDominantErr: Bit Dominant Error
  920. * - CAN_ERRORCODE_CRCErr: CRC Error
  921. * - CAN_ERRORCODE_SoftwareSetErr: Software Set Error
  922. */
  923. uint8_t CAN_GetLastErrorCode(CAN_TypeDef* CANx)
  924. {
  925. uint8_t errorcode=0;
  926. /* Check the parameters */
  927. assert_param(IS_CAN_ALL_PERIPH(CANx));
  928. /* Get the error code*/
  929. errorcode = (((uint8_t)CANx->ESR) & (uint8_t)CAN_ESR_LEC);
  930. /* Return the error code*/
  931. return errorcode;
  932. }
  933. /**
  934. * @brief Returns the CANx Receive Error Counter (REC).
  935. * @note In case of an error during reception, this counter is incremented
  936. * by 1 or by 8 depending on the error condition as defined by the CAN
  937. * standard. After every successful reception, the counter is
  938. * decremented by 1 or reset to 120 if its value was higher than 128.
  939. * When the counter value exceeds 127, the CAN controller enters the
  940. * error passive state.
  941. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  942. * @retval CAN Receive Error Counter.
  943. */
  944. uint8_t CAN_GetReceiveErrorCounter(CAN_TypeDef* CANx)
  945. {
  946. uint8_t counter=0;
  947. /* Check the parameters */
  948. assert_param(IS_CAN_ALL_PERIPH(CANx));
  949. /* Get the Receive Error Counter*/
  950. counter = (uint8_t)((CANx->ESR & CAN_ESR_REC)>> 24);
  951. /* Return the Receive Error Counter*/
  952. return counter;
  953. }
  954. /**
  955. * @brief Returns the LSB of the 9-bit CANx Transmit Error Counter(TEC).
  956. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  957. * @retval LSB of the 9-bit CAN Transmit Error Counter.
  958. */
  959. uint8_t CAN_GetLSBTransmitErrorCounter(CAN_TypeDef* CANx)
  960. {
  961. uint8_t counter=0;
  962. /* Check the parameters */
  963. assert_param(IS_CAN_ALL_PERIPH(CANx));
  964. /* Get the LSB of the 9-bit CANx Transmit Error Counter(TEC) */
  965. counter = (uint8_t)((CANx->ESR & CAN_ESR_TEC)>> 16);
  966. /* Return the LSB of the 9-bit CANx Transmit Error Counter(TEC) */
  967. return counter;
  968. }
  969. /**
  970. * @}
  971. */
  972. /** @defgroup CAN_Group6 Interrupts and flags management functions
  973. * @brief Interrupts and flags management functions
  974. *
  975. @verbatim
  976. ===============================================================================
  977. Interrupts and flags management functions
  978. ===============================================================================
  979. This section provides functions allowing to configure the CAN Interrupts and
  980. to get the status and clear flags and Interrupts pending bits.
  981. The CAN provides 14 Interrupts sources and 15 Flags:
  982. ===============
  983. Flags :
  984. ===============
  985. The 15 flags can be divided on 4 groups:
  986. A. Transmit Flags
  987. -----------------------
  988. CAN_FLAG_RQCP0,
  989. CAN_FLAG_RQCP1,
  990. CAN_FLAG_RQCP2 : Request completed MailBoxes 0, 1 and 2 Flags
  991. Set when when the last request (transmit or abort) has
  992. been performed.
  993. B. Receive Flags
  994. -----------------------
  995. CAN_FLAG_FMP0,
  996. CAN_FLAG_FMP1 : FIFO 0 and 1 Message Pending Flags
  997. set to signal that messages are pending in the receive
  998. FIFO.
  999. These Flags are cleared only by hardware.
  1000. CAN_FLAG_FF0,
  1001. CAN_FLAG_FF1 : FIFO 0 and 1 Full Flags
  1002. set when three messages are stored in the selected
  1003. FIFO.
  1004. CAN_FLAG_FOV0
  1005. CAN_FLAG_FOV1 : FIFO 0 and 1 Overrun Flags
  1006. set when a new message has been received and passed
  1007. the filter while the FIFO was full.
  1008. C. Operating Mode Flags
  1009. -----------------------
  1010. CAN_FLAG_WKU : Wake up Flag
  1011. set to signal that a SOF bit has been detected while
  1012. the CAN hardware was in Sleep mode.
  1013. CAN_FLAG_SLAK : Sleep acknowledge Flag
  1014. Set to signal that the CAN has entered Sleep Mode.
  1015. D. Error Flags
  1016. -----------------------
  1017. CAN_FLAG_EWG : Error Warning Flag
  1018. Set when the warning limit has been reached (Receive
  1019. Error Counter or Transmit Error Counter greater than 96).
  1020. This Flag is cleared only by hardware.
  1021. CAN_FLAG_EPV : Error Passive Flag
  1022. Set when the Error Passive limit has been reached
  1023. (Receive Error Counter or Transmit Error Counter
  1024. greater than 127).
  1025. This Flag is cleared only by hardware.
  1026. CAN_FLAG_BOF : Bus-Off Flag
  1027. set when CAN enters the bus-off state. The bus-off
  1028. state is entered on TEC overflow, greater than 255.
  1029. This Flag is cleared only by hardware.
  1030. CAN_FLAG_LEC : Last error code Flag
  1031. set If a message has been transferred (reception or
  1032. transmission) with error, and the error code is hold.
  1033. ===============
  1034. Interrupts :
  1035. ===============
  1036. The 14 interrupts can be divided on 4 groups:
  1037. A. Transmit interrupt
  1038. -----------------------
  1039. CAN_IT_TME : Transmit mailbox empty Interrupt
  1040. if enabled, this interrupt source is pending when
  1041. no transmit request are pending for Tx mailboxes.
  1042. B. Receive Interrupts
  1043. -----------------------
  1044. CAN_IT_FMP0,
  1045. CAN_IT_FMP1 : FIFO 0 and FIFO1 message pending Interrupts
  1046. if enabled, these interrupt sources are pending when
  1047. messages are pending in the receive FIFO.
  1048. The corresponding interrupt pending bits are cleared
  1049. only by hardware.
  1050. CAN_IT_FF0,
  1051. CAN_IT_FF1 : FIFO 0 and FIFO1 full Interrupts
  1052. if enabled, these interrupt sources are pending when
  1053. three messages are stored in the selected FIFO.
  1054. CAN_IT_FOV0,
  1055. CAN_IT_FOV1 : FIFO 0 and FIFO1 overrun Interrupts
  1056. if enabled, these interrupt sources are pending when
  1057. a new message has been received and passed the filter
  1058. while the FIFO was full.
  1059. C. Operating Mode Interrupts
  1060. -------------------------------
  1061. CAN_IT_WKU : Wake-up Interrupt
  1062. if enabled, this interrupt source is pending when
  1063. a SOF bit has been detected while the CAN hardware was
  1064. in Sleep mode.
  1065. CAN_IT_SLK : Sleep acknowledge Interrupt
  1066. if enabled, this interrupt source is pending when
  1067. the CAN has entered Sleep Mode.
  1068. D. Error Interrupts
  1069. -----------------------
  1070. CAN_IT_EWG : Error warning Interrupt
  1071. if enabled, this interrupt source is pending when
  1072. the warning limit has been reached (Receive Error
  1073. Counter or Transmit Error Counter=96).
  1074. CAN_IT_EPV : Error passive Interrupt
  1075. if enabled, this interrupt source is pending when
  1076. the Error Passive limit has been reached (Receive
  1077. Error Counter or Transmit Error Counter>127).
  1078. CAN_IT_BOF : Bus-off Interrupt
  1079. if enabled, this interrupt source is pending when
  1080. CAN enters the bus-off state. The bus-off state is
  1081. entered on TEC overflow, greater than 255.
  1082. This Flag is cleared only by hardware.
  1083. CAN_IT_LEC : Last error code Interrupt
  1084. if enabled, this interrupt source is pending when
  1085. a message has been transferred (reception or
  1086. transmission) with error, and the error code is hold.
  1087. CAN_IT_ERR : Error Interrupt
  1088. if enabled, this interrupt source is pending when
  1089. an error condition is pending.
  1090. Managing the CAN controller events :
  1091. ------------------------------------
  1092. The user should identify which mode will be used in his application to manage
  1093. the CAN controller events: Polling mode or Interrupt mode.
  1094. 1. In the Polling Mode it is advised to use the following functions:
  1095. - CAN_GetFlagStatus() : to check if flags events occur.
  1096. - CAN_ClearFlag() : to clear the flags events.
  1097. 2. In the Interrupt Mode it is advised to use the following functions:
  1098. - CAN_ITConfig() : to enable or disable the interrupt source.
  1099. - CAN_GetITStatus() : to check if Interrupt occurs.
  1100. - CAN_ClearITPendingBit() : to clear the Interrupt pending Bit (corresponding Flag).
  1101. @note This function has no impact on CAN_IT_FMP0 and CAN_IT_FMP1 Interrupts
  1102. pending bits since there are cleared only by hardware.
  1103. @endverbatim
  1104. * @{
  1105. */
  1106. /**
  1107. * @brief Enables or disables the specified CANx interrupts.
  1108. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  1109. * @param CAN_IT: specifies the CAN interrupt sources to be enabled or disabled.
  1110. * This parameter can be:
  1111. * @arg CAN_IT_TME: Transmit mailbox empty Interrupt
  1112. * @arg CAN_IT_FMP0: FIFO 0 message pending Interrupt
  1113. * @arg CAN_IT_FF0: FIFO 0 full Interrupt
  1114. * @arg CAN_IT_FOV0: FIFO 0 overrun Interrupt
  1115. * @arg CAN_IT_FMP1: FIFO 1 message pending Interrupt
  1116. * @arg CAN_IT_FF1: FIFO 1 full Interrupt
  1117. * @arg CAN_IT_FOV1: FIFO 1 overrun Interrupt
  1118. * @arg CAN_IT_WKU: Wake-up Interrupt
  1119. * @arg CAN_IT_SLK: Sleep acknowledge Interrupt
  1120. * @arg CAN_IT_EWG: Error warning Interrupt
  1121. * @arg CAN_IT_EPV: Error passive Interrupt
  1122. * @arg CAN_IT_BOF: Bus-off Interrupt
  1123. * @arg CAN_IT_LEC: Last error code Interrupt
  1124. * @arg CAN_IT_ERR: Error Interrupt
  1125. * @param NewState: new state of the CAN interrupts.
  1126. * This parameter can be: ENABLE or DISABLE.
  1127. * @retval None
  1128. */
  1129. void CAN_ITConfig(CAN_TypeDef* CANx, uint32_t CAN_IT, FunctionalState NewState)
  1130. {
  1131. /* Check the parameters */
  1132. assert_param(IS_CAN_ALL_PERIPH(CANx));
  1133. assert_param(IS_CAN_IT(CAN_IT));
  1134. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1135. if (NewState != DISABLE)
  1136. {
  1137. /* Enable the selected CANx interrupt */
  1138. CANx->IER |= CAN_IT;
  1139. }
  1140. else
  1141. {
  1142. /* Disable the selected CANx interrupt */
  1143. CANx->IER &= ~CAN_IT;
  1144. }
  1145. }
  1146. /**
  1147. * @brief Checks whether the specified CAN flag is set or not.
  1148. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  1149. * @param CAN_FLAG: specifies the flag to check.
  1150. * This parameter can be one of the following values:
  1151. * @arg CAN_FLAG_RQCP0: Request MailBox0 Flag
  1152. * @arg CAN_FLAG_RQCP1: Request MailBox1 Flag
  1153. * @arg CAN_FLAG_RQCP2: Request MailBox2 Flag
  1154. * @arg CAN_FLAG_FMP0: FIFO 0 Message Pending Flag
  1155. * @arg CAN_FLAG_FF0: FIFO 0 Full Flag
  1156. * @arg CAN_FLAG_FOV0: FIFO 0 Overrun Flag
  1157. * @arg CAN_FLAG_FMP1: FIFO 1 Message Pending Flag
  1158. * @arg CAN_FLAG_FF1: FIFO 1 Full Flag
  1159. * @arg CAN_FLAG_FOV1: FIFO 1 Overrun Flag
  1160. * @arg CAN_FLAG_WKU: Wake up Flag
  1161. * @arg CAN_FLAG_SLAK: Sleep acknowledge Flag
  1162. * @arg CAN_FLAG_EWG: Error Warning Flag
  1163. * @arg CAN_FLAG_EPV: Error Passive Flag
  1164. * @arg CAN_FLAG_BOF: Bus-Off Flag
  1165. * @arg CAN_FLAG_LEC: Last error code Flag
  1166. * @retval The new state of CAN_FLAG (SET or RESET).
  1167. */
  1168. FlagStatus CAN_GetFlagStatus(CAN_TypeDef* CANx, uint32_t CAN_FLAG)
  1169. {
  1170. FlagStatus bitstatus = RESET;
  1171. /* Check the parameters */
  1172. assert_param(IS_CAN_ALL_PERIPH(CANx));
  1173. assert_param(IS_CAN_GET_FLAG(CAN_FLAG));
  1174. if((CAN_FLAG & CAN_FLAGS_ESR) != (uint32_t)RESET)
  1175. {
  1176. /* Check the status of the specified CAN flag */
  1177. if ((CANx->ESR & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
  1178. {
  1179. /* CAN_FLAG is set */
  1180. bitstatus = SET;
  1181. }
  1182. else
  1183. {
  1184. /* CAN_FLAG is reset */
  1185. bitstatus = RESET;
  1186. }
  1187. }
  1188. else if((CAN_FLAG & CAN_FLAGS_MSR) != (uint32_t)RESET)
  1189. {
  1190. /* Check the status of the specified CAN flag */
  1191. if ((CANx->MSR & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
  1192. {
  1193. /* CAN_FLAG is set */
  1194. bitstatus = SET;
  1195. }
  1196. else
  1197. {
  1198. /* CAN_FLAG is reset */
  1199. bitstatus = RESET;
  1200. }
  1201. }
  1202. else if((CAN_FLAG & CAN_FLAGS_TSR) != (uint32_t)RESET)
  1203. {
  1204. /* Check the status of the specified CAN flag */
  1205. if ((CANx->TSR & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
  1206. {
  1207. /* CAN_FLAG is set */
  1208. bitstatus = SET;
  1209. }
  1210. else
  1211. {
  1212. /* CAN_FLAG is reset */
  1213. bitstatus = RESET;
  1214. }
  1215. }
  1216. else if((CAN_FLAG & CAN_FLAGS_RF0R) != (uint32_t)RESET)
  1217. {
  1218. /* Check the status of the specified CAN flag */
  1219. if ((CANx->RF0R & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
  1220. {
  1221. /* CAN_FLAG is set */
  1222. bitstatus = SET;
  1223. }
  1224. else
  1225. {
  1226. /* CAN_FLAG is reset */
  1227. bitstatus = RESET;
  1228. }
  1229. }
  1230. else /* If(CAN_FLAG & CAN_FLAGS_RF1R != (uint32_t)RESET) */
  1231. {
  1232. /* Check the status of the specified CAN flag */
  1233. if ((uint32_t)(CANx->RF1R & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
  1234. {
  1235. /* CAN_FLAG is set */
  1236. bitstatus = SET;
  1237. }
  1238. else
  1239. {
  1240. /* CAN_FLAG is reset */
  1241. bitstatus = RESET;
  1242. }
  1243. }
  1244. /* Return the CAN_FLAG status */
  1245. return bitstatus;
  1246. }
  1247. /**
  1248. * @brief Clears the CAN's pending flags.
  1249. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  1250. * @param CAN_FLAG: specifies the flag to clear.
  1251. * This parameter can be one of the following values:
  1252. * @arg CAN_FLAG_RQCP0: Request MailBox0 Flag
  1253. * @arg CAN_FLAG_RQCP1: Request MailBox1 Flag
  1254. * @arg CAN_FLAG_RQCP2: Request MailBox2 Flag
  1255. * @arg CAN_FLAG_FF0: FIFO 0 Full Flag
  1256. * @arg CAN_FLAG_FOV0: FIFO 0 Overrun Flag
  1257. * @arg CAN_FLAG_FF1: FIFO 1 Full Flag
  1258. * @arg CAN_FLAG_FOV1: FIFO 1 Overrun Flag
  1259. * @arg CAN_FLAG_WKU: Wake up Flag
  1260. * @arg CAN_FLAG_SLAK: Sleep acknowledge Flag
  1261. * @arg CAN_FLAG_LEC: Last error code Flag
  1262. * @retval None
  1263. */
  1264. void CAN_ClearFlag(CAN_TypeDef* CANx, uint32_t CAN_FLAG)
  1265. {
  1266. uint32_t flagtmp=0;
  1267. /* Check the parameters */
  1268. assert_param(IS_CAN_ALL_PERIPH(CANx));
  1269. assert_param(IS_CAN_CLEAR_FLAG(CAN_FLAG));
  1270. if (CAN_FLAG == CAN_FLAG_LEC) /* ESR register */
  1271. {
  1272. /* Clear the selected CAN flags */
  1273. CANx->ESR = (uint32_t)RESET;
  1274. }
  1275. else /* MSR or TSR or RF0R or RF1R */
  1276. {
  1277. flagtmp = CAN_FLAG & 0x000FFFFF;
  1278. if ((CAN_FLAG & CAN_FLAGS_RF0R)!=(uint32_t)RESET)
  1279. {
  1280. /* Receive Flags */
  1281. CANx->RF0R = (uint32_t)(flagtmp);
  1282. }
  1283. else if ((CAN_FLAG & CAN_FLAGS_RF1R)!=(uint32_t)RESET)
  1284. {
  1285. /* Receive Flags */
  1286. CANx->RF1R = (uint32_t)(flagtmp);
  1287. }
  1288. else if ((CAN_FLAG & CAN_FLAGS_TSR)!=(uint32_t)RESET)
  1289. {
  1290. /* Transmit Flags */
  1291. CANx->TSR = (uint32_t)(flagtmp);
  1292. }
  1293. else /* If((CAN_FLAG & CAN_FLAGS_MSR)!=(uint32_t)RESET) */
  1294. {
  1295. /* Operating mode Flags */
  1296. CANx->MSR = (uint32_t)(flagtmp);
  1297. }
  1298. }
  1299. }
  1300. /**
  1301. * @brief Checks whether the specified CANx interrupt has occurred or not.
  1302. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  1303. * @param CAN_IT: specifies the CAN interrupt source to check.
  1304. * This parameter can be one of the following values:
  1305. * @arg CAN_IT_TME: Transmit mailbox empty Interrupt
  1306. * @arg CAN_IT_FMP0: FIFO 0 message pending Interrupt
  1307. * @arg CAN_IT_FF0: FIFO 0 full Interrupt
  1308. * @arg CAN_IT_FOV0: FIFO 0 overrun Interrupt
  1309. * @arg CAN_IT_FMP1: FIFO 1 message pending Interrupt
  1310. * @arg CAN_IT_FF1: FIFO 1 full Interrupt
  1311. * @arg CAN_IT_FOV1: FIFO 1 overrun Interrupt
  1312. * @arg CAN_IT_WKU: Wake-up Interrupt
  1313. * @arg CAN_IT_SLK: Sleep acknowledge Interrupt
  1314. * @arg CAN_IT_EWG: Error warning Interrupt
  1315. * @arg CAN_IT_EPV: Error passive Interrupt
  1316. * @arg CAN_IT_BOF: Bus-off Interrupt
  1317. * @arg CAN_IT_LEC: Last error code Interrupt
  1318. * @arg CAN_IT_ERR: Error Interrupt
  1319. * @retval The current state of CAN_IT (SET or RESET).
  1320. */
  1321. ITStatus CAN_GetITStatus(CAN_TypeDef* CANx, uint32_t CAN_IT)
  1322. {
  1323. ITStatus itstatus = RESET;
  1324. /* Check the parameters */
  1325. assert_param(IS_CAN_ALL_PERIPH(CANx));
  1326. assert_param(IS_CAN_IT(CAN_IT));
  1327. /* check the interrupt enable bit */
  1328. if((CANx->IER & CAN_IT) != RESET)
  1329. {
  1330. /* in case the Interrupt is enabled, .... */
  1331. switch (CAN_IT)
  1332. {
  1333. case CAN_IT_TME:
  1334. /* Check CAN_TSR_RQCPx bits */
  1335. itstatus = CheckITStatus(CANx->TSR, CAN_TSR_RQCP0|CAN_TSR_RQCP1|CAN_TSR_RQCP2);
  1336. break;
  1337. case CAN_IT_FMP0:
  1338. /* Check CAN_RF0R_FMP0 bit */
  1339. itstatus = CheckITStatus(CANx->RF0R, CAN_RF0R_FMP0);
  1340. break;
  1341. case CAN_IT_FF0:
  1342. /* Check CAN_RF0R_FULL0 bit */
  1343. itstatus = CheckITStatus(CANx->RF0R, CAN_RF0R_FULL0);
  1344. break;
  1345. case CAN_IT_FOV0:
  1346. /* Check CAN_RF0R_FOVR0 bit */
  1347. itstatus = CheckITStatus(CANx->RF0R, CAN_RF0R_FOVR0);
  1348. break;
  1349. case CAN_IT_FMP1:
  1350. /* Check CAN_RF1R_FMP1 bit */
  1351. itstatus = CheckITStatus(CANx->RF1R, CAN_RF1R_FMP1);
  1352. break;
  1353. case CAN_IT_FF1:
  1354. /* Check CAN_RF1R_FULL1 bit */
  1355. itstatus = CheckITStatus(CANx->RF1R, CAN_RF1R_FULL1);
  1356. break;
  1357. case CAN_IT_FOV1:
  1358. /* Check CAN_RF1R_FOVR1 bit */
  1359. itstatus = CheckITStatus(CANx->RF1R, CAN_RF1R_FOVR1);
  1360. break;
  1361. case CAN_IT_WKU:
  1362. /* Check CAN_MSR_WKUI bit */
  1363. itstatus = CheckITStatus(CANx->MSR, CAN_MSR_WKUI);
  1364. break;
  1365. case CAN_IT_SLK:
  1366. /* Check CAN_MSR_SLAKI bit */
  1367. itstatus = CheckITStatus(CANx->MSR, CAN_MSR_SLAKI);
  1368. break;
  1369. case CAN_IT_EWG:
  1370. /* Check CAN_ESR_EWGF bit */
  1371. itstatus = CheckITStatus(CANx->ESR, CAN_ESR_EWGF);
  1372. break;
  1373. case CAN_IT_EPV:
  1374. /* Check CAN_ESR_EPVF bit */
  1375. itstatus = CheckITStatus(CANx->ESR, CAN_ESR_EPVF);
  1376. break;
  1377. case CAN_IT_BOF:
  1378. /* Check CAN_ESR_BOFF bit */
  1379. itstatus = CheckITStatus(CANx->ESR, CAN_ESR_BOFF);
  1380. break;
  1381. case CAN_IT_LEC:
  1382. /* Check CAN_ESR_LEC bit */
  1383. itstatus = CheckITStatus(CANx->ESR, CAN_ESR_LEC);
  1384. break;
  1385. case CAN_IT_ERR:
  1386. /* Check CAN_MSR_ERRI bit */
  1387. itstatus = CheckITStatus(CANx->MSR, CAN_MSR_ERRI);
  1388. break;
  1389. default:
  1390. /* in case of error, return RESET */
  1391. itstatus = RESET;
  1392. break;
  1393. }
  1394. }
  1395. else
  1396. {
  1397. /* in case the Interrupt is not enabled, return RESET */
  1398. itstatus = RESET;
  1399. }
  1400. /* Return the CAN_IT status */
  1401. return itstatus;
  1402. }
  1403. /**
  1404. * @brief Clears the CANx's interrupt pending bits.
  1405. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  1406. * @param CAN_IT: specifies the interrupt pending bit to clear.
  1407. * This parameter can be one of the following values:
  1408. * @arg CAN_IT_TME: Transmit mailbox empty Interrupt
  1409. * @arg CAN_IT_FF0: FIFO 0 full Interrupt
  1410. * @arg CAN_IT_FOV0: FIFO 0 overrun Interrupt
  1411. * @arg CAN_IT_FF1: FIFO 1 full Interrupt
  1412. * @arg CAN_IT_FOV1: FIFO 1 overrun Interrupt
  1413. * @arg CAN_IT_WKU: Wake-up Interrupt
  1414. * @arg CAN_IT_SLK: Sleep acknowledge Interrupt
  1415. * @arg CAN_IT_EWG: Error warning Interrupt
  1416. * @arg CAN_IT_EPV: Error passive Interrupt
  1417. * @arg CAN_IT_BOF: Bus-off Interrupt
  1418. * @arg CAN_IT_LEC: Last error code Interrupt
  1419. * @arg CAN_IT_ERR: Error Interrupt
  1420. * @retval None
  1421. */
  1422. void CAN_ClearITPendingBit(CAN_TypeDef* CANx, uint32_t CAN_IT)
  1423. {
  1424. /* Check the parameters */
  1425. assert_param(IS_CAN_ALL_PERIPH(CANx));
  1426. assert_param(IS_CAN_CLEAR_IT(CAN_IT));
  1427. switch (CAN_IT)
  1428. {
  1429. case CAN_IT_TME:
  1430. /* Clear CAN_TSR_RQCPx (rc_w1)*/
  1431. CANx->TSR = CAN_TSR_RQCP0|CAN_TSR_RQCP1|CAN_TSR_RQCP2;
  1432. break;
  1433. case CAN_IT_FF0:
  1434. /* Clear CAN_RF0R_FULL0 (rc_w1)*/
  1435. CANx->RF0R = CAN_RF0R_FULL0;
  1436. break;
  1437. case CAN_IT_FOV0:
  1438. /* Clear CAN_RF0R_FOVR0 (rc_w1)*/
  1439. CANx->RF0R = CAN_RF0R_FOVR0;
  1440. break;
  1441. case CAN_IT_FF1:
  1442. /* Clear CAN_RF1R_FULL1 (rc_w1)*/
  1443. CANx->RF1R = CAN_RF1R_FULL1;
  1444. break;
  1445. case CAN_IT_FOV1:
  1446. /* Clear CAN_RF1R_FOVR1 (rc_w1)*/
  1447. CANx->RF1R = CAN_RF1R_FOVR1;
  1448. break;
  1449. case CAN_IT_WKU:
  1450. /* Clear CAN_MSR_WKUI (rc_w1)*/
  1451. CANx->MSR = CAN_MSR_WKUI;
  1452. break;
  1453. case CAN_IT_SLK:
  1454. /* Clear CAN_MSR_SLAKI (rc_w1)*/
  1455. CANx->MSR = CAN_MSR_SLAKI;
  1456. break;
  1457. case CAN_IT_EWG:
  1458. /* Clear CAN_MSR_ERRI (rc_w1) */
  1459. CANx->MSR = CAN_MSR_ERRI;
  1460. /* @note the corresponding Flag is cleared by hardware depending on the CAN Bus status*/
  1461. break;
  1462. case CAN_IT_EPV:
  1463. /* Clear CAN_MSR_ERRI (rc_w1) */
  1464. CANx->MSR = CAN_MSR_ERRI;
  1465. /* @note the corresponding Flag is cleared by hardware depending on the CAN Bus status*/
  1466. break;
  1467. case CAN_IT_BOF:
  1468. /* Clear CAN_MSR_ERRI (rc_w1) */
  1469. CANx->MSR = CAN_MSR_ERRI;
  1470. /* @note the corresponding Flag is cleared by hardware depending on the CAN Bus status*/
  1471. break;
  1472. case CAN_IT_LEC:
  1473. /* Clear LEC bits */
  1474. CANx->ESR = RESET;
  1475. /* Clear CAN_MSR_ERRI (rc_w1) */
  1476. CANx->MSR = CAN_MSR_ERRI;
  1477. break;
  1478. case CAN_IT_ERR:
  1479. /*Clear LEC bits */
  1480. CANx->ESR = RESET;
  1481. /* Clear CAN_MSR_ERRI (rc_w1) */
  1482. CANx->MSR = CAN_MSR_ERRI;
  1483. /* @note BOFF, EPVF and EWGF Flags are cleared by hardware depending on the CAN Bus status*/
  1484. break;
  1485. default:
  1486. break;
  1487. }
  1488. }
  1489. /**
  1490. * @}
  1491. */
  1492. /**
  1493. * @brief Checks whether the CAN interrupt has occurred or not.
  1494. * @param CAN_Reg: specifies the CAN interrupt register to check.
  1495. * @param It_Bit: specifies the interrupt source bit to check.
  1496. * @retval The new state of the CAN Interrupt (SET or RESET).
  1497. */
  1498. static ITStatus CheckITStatus(uint32_t CAN_Reg, uint32_t It_Bit)
  1499. {
  1500. ITStatus pendingbitstatus = RESET;
  1501. if ((CAN_Reg & It_Bit) != (uint32_t)RESET)
  1502. {
  1503. /* CAN_IT is set */
  1504. pendingbitstatus = SET;
  1505. }
  1506. else
  1507. {
  1508. /* CAN_IT is reset */
  1509. pendingbitstatus = RESET;
  1510. }
  1511. return pendingbitstatus;
  1512. }
  1513. /**
  1514. * @}
  1515. */
  1516. /**
  1517. * @}
  1518. */
  1519. /**
  1520. * @}
  1521. */
  1522. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/