stm32f4xx_can.c 57 KB

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