stm32f0xx_can.c 57 KB

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