stm32f10x_can.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. /**
  2. ******************************************************************************
  3. * @file stm32f10x_can.c
  4. * @author MCD Application Team
  5. * @version V3.3.0
  6. * @date 04/16/2010
  7. * @brief This file provides all the CAN firmware functions.
  8. ******************************************************************************
  9. * @copy
  10. *
  11. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17. *
  18. * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
  19. */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "stm32f10x_can.h"
  22. #include "stm32f10x_rcc.h"
  23. /** @addtogroup STM32F10x_StdPeriph_Driver
  24. * @{
  25. */
  26. /** @defgroup CAN
  27. * @brief CAN driver modules
  28. * @{
  29. */
  30. /** @defgroup CAN_Private_TypesDefinitions
  31. * @{
  32. */
  33. /**
  34. * @}
  35. */
  36. /** @defgroup CAN_Private_Defines
  37. * @{
  38. */
  39. /* CAN Master Control Register bits */
  40. #define MCR_INRQ ((uint32_t)0x00000001) /* Initialization request */
  41. #define MCR_SLEEP ((uint32_t)0x00000002) /* Sleep mode request */
  42. #define MCR_TXFP ((uint32_t)0x00000004) /* Transmit FIFO priority */
  43. #define MCR_RFLM ((uint32_t)0x00000008) /* Receive FIFO locked mode */
  44. #define MCR_NART ((uint32_t)0x00000010) /* No automatic retransmission */
  45. #define MCR_AWUM ((uint32_t)0x00000020) /* Automatic wake up mode */
  46. #define MCR_ABOM ((uint32_t)0x00000040) /* Automatic bus-off management */
  47. #define MCR_TTCM ((uint32_t)0x00000080) /* time triggered communication */
  48. #define MCR_RESET ((uint32_t)0x00008000) /* time triggered communication */
  49. #define MCR_DBF ((uint32_t)0x00010000) /* software master reset */
  50. /* CAN Master Status Register bits */
  51. #define MSR_INAK ((uint32_t)0x00000001) /* Initialization acknowledge */
  52. #define MSR_WKUI ((uint32_t)0x00000008) /* Wake-up interrupt */
  53. #define MSR_SLAKI ((uint32_t)0x00000010) /* Sleep acknowledge interrupt */
  54. /* CAN Transmit Status Register bits */
  55. #define TSR_RQCP0 ((uint32_t)0x00000001) /* Request completed mailbox0 */
  56. #define TSR_TXOK0 ((uint32_t)0x00000002) /* Transmission OK of mailbox0 */
  57. #define TSR_ABRQ0 ((uint32_t)0x00000080) /* Abort request for mailbox0 */
  58. #define TSR_RQCP1 ((uint32_t)0x00000100) /* Request completed mailbox1 */
  59. #define TSR_TXOK1 ((uint32_t)0x00000200) /* Transmission OK of mailbox1 */
  60. #define TSR_ABRQ1 ((uint32_t)0x00008000) /* Abort request for mailbox1 */
  61. #define TSR_RQCP2 ((uint32_t)0x00010000) /* Request completed mailbox2 */
  62. #define TSR_TXOK2 ((uint32_t)0x00020000) /* Transmission OK of mailbox2 */
  63. #define TSR_ABRQ2 ((uint32_t)0x00800000) /* Abort request for mailbox2 */
  64. #define TSR_TME0 ((uint32_t)0x04000000) /* Transmit mailbox 0 empty */
  65. #define TSR_TME1 ((uint32_t)0x08000000) /* Transmit mailbox 1 empty */
  66. #define TSR_TME2 ((uint32_t)0x10000000) /* Transmit mailbox 2 empty */
  67. /* CAN Receive FIFO 0 Register bits */
  68. #define RF0R_FULL0 ((uint32_t)0x00000008) /* FIFO 0 full */
  69. #define RF0R_FOVR0 ((uint32_t)0x00000010) /* FIFO 0 overrun */
  70. #define RF0R_RFOM0 ((uint32_t)0x00000020) /* Release FIFO 0 output mailbox */
  71. /* CAN Receive FIFO 1 Register bits */
  72. #define RF1R_FULL1 ((uint32_t)0x00000008) /* FIFO 1 full */
  73. #define RF1R_FOVR1 ((uint32_t)0x00000010) /* FIFO 1 overrun */
  74. #define RF1R_RFOM1 ((uint32_t)0x00000020) /* Release FIFO 1 output mailbox */
  75. /* CAN Error Status Register bits */
  76. #define ESR_EWGF ((uint32_t)0x00000001) /* Error warning flag */
  77. #define ESR_EPVF ((uint32_t)0x00000002) /* Error passive flag */
  78. #define ESR_BOFF ((uint32_t)0x00000004) /* Bus-off flag */
  79. /* CAN Mailbox Transmit Request */
  80. #define TMIDxR_TXRQ ((uint32_t)0x00000001) /* Transmit mailbox request */
  81. /* CAN Filter Master Register bits */
  82. #define FMR_FINIT ((uint32_t)0x00000001) /* Filter init mode */
  83. /* Time out for INAK bit */
  84. #define INAK_TimeOut ((uint32_t)0x0000FFFF)
  85. /* Time out for SLAK bit */
  86. #define SLAK_TimeOut ((uint32_t)0x0000FFFF)
  87. /**
  88. * @}
  89. */
  90. /** @defgroup CAN_Private_Macros
  91. * @{
  92. */
  93. /**
  94. * @}
  95. */
  96. /** @defgroup CAN_Private_Variables
  97. * @{
  98. */
  99. /**
  100. * @}
  101. */
  102. /** @defgroup CAN_Private_FunctionPrototypes
  103. * @{
  104. */
  105. static ITStatus CheckITStatus(uint32_t CAN_Reg, uint32_t It_Bit);
  106. /**
  107. * @}
  108. */
  109. /** @defgroup CAN_Private_Functions
  110. * @{
  111. */
  112. /**
  113. * @brief Deinitializes the CAN peripheral registers to their default reset values.
  114. * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
  115. * @retval None.
  116. */
  117. void CAN_DeInit(CAN_TypeDef* CANx)
  118. {
  119. /* Check the parameters */
  120. assert_param(IS_CAN_ALL_PERIPH(CANx));
  121. if (CANx == CAN1)
  122. {
  123. /* Enable CAN1 reset state */
  124. RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN1, ENABLE);
  125. /* Release CAN1 from reset state */
  126. RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN1, DISABLE);
  127. }
  128. else
  129. {
  130. /* Enable CAN2 reset state */
  131. RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN2, ENABLE);
  132. /* Release CAN2 from reset state */
  133. RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN2, DISABLE);
  134. }
  135. }
  136. /**
  137. * @brief Initializes the CAN peripheral according to the specified
  138. * parameters in the CAN_InitStruct.
  139. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  140. * @param CAN_InitStruct: pointer to a CAN_InitTypeDef structure that
  141. * contains the configuration information for the CAN peripheral.
  142. * @retval Constant indicates initialization succeed which will be
  143. * CANINITFAILED or CANINITOK.
  144. */
  145. uint8_t CAN_Init(CAN_TypeDef* CANx, CAN_InitTypeDef* CAN_InitStruct)
  146. {
  147. uint8_t InitStatus = CANINITFAILED;
  148. uint32_t wait_ack = 0x00000000;
  149. /* Check the parameters */
  150. assert_param(IS_CAN_ALL_PERIPH(CANx));
  151. assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TTCM));
  152. assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_ABOM));
  153. assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_AWUM));
  154. assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_NART));
  155. assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_RFLM));
  156. assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TXFP));
  157. assert_param(IS_CAN_MODE(CAN_InitStruct->CAN_Mode));
  158. assert_param(IS_CAN_SJW(CAN_InitStruct->CAN_SJW));
  159. assert_param(IS_CAN_BS1(CAN_InitStruct->CAN_BS1));
  160. assert_param(IS_CAN_BS2(CAN_InitStruct->CAN_BS2));
  161. assert_param(IS_CAN_PRESCALER(CAN_InitStruct->CAN_Prescaler));
  162. /* exit from sleep mode */
  163. CANx->MCR &= ~MCR_SLEEP;
  164. /* Request initialisation */
  165. CANx->MCR |= MCR_INRQ ;
  166. /* Wait the acknowledge */
  167. while (((CANx->MSR & MSR_INAK) != MSR_INAK) && (wait_ack != INAK_TimeOut))
  168. {
  169. wait_ack++;
  170. }
  171. /* ...and check acknowledged */
  172. if ((CANx->MSR & MSR_INAK) != MSR_INAK)
  173. {
  174. InitStatus = CANINITFAILED;
  175. }
  176. else
  177. {
  178. /* Set the time triggered communication mode */
  179. if (CAN_InitStruct->CAN_TTCM == ENABLE)
  180. {
  181. CANx->MCR |= MCR_TTCM;
  182. }
  183. else
  184. {
  185. CANx->MCR &= ~MCR_TTCM;
  186. }
  187. /* Set the automatic bus-off management */
  188. if (CAN_InitStruct->CAN_ABOM == ENABLE)
  189. {
  190. CANx->MCR |= MCR_ABOM;
  191. }
  192. else
  193. {
  194. CANx->MCR &= ~MCR_ABOM;
  195. }
  196. /* Set the automatic wake-up mode */
  197. if (CAN_InitStruct->CAN_AWUM == ENABLE)
  198. {
  199. CANx->MCR |= MCR_AWUM;
  200. }
  201. else
  202. {
  203. CANx->MCR &= ~MCR_AWUM;
  204. }
  205. /* Set the no automatic retransmission */
  206. if (CAN_InitStruct->CAN_NART == ENABLE)
  207. {
  208. CANx->MCR |= MCR_NART;
  209. }
  210. else
  211. {
  212. CANx->MCR &= ~MCR_NART;
  213. }
  214. /* Set the receive FIFO locked mode */
  215. if (CAN_InitStruct->CAN_RFLM == ENABLE)
  216. {
  217. CANx->MCR |= MCR_RFLM;
  218. }
  219. else
  220. {
  221. CANx->MCR &= ~MCR_RFLM;
  222. }
  223. /* Set the transmit FIFO priority */
  224. if (CAN_InitStruct->CAN_TXFP == ENABLE)
  225. {
  226. CANx->MCR |= MCR_TXFP;
  227. }
  228. else
  229. {
  230. CANx->MCR &= ~MCR_TXFP;
  231. }
  232. /* Set the bit timing register */
  233. CANx->BTR = (uint32_t)((uint32_t)CAN_InitStruct->CAN_Mode << 30) | ((uint32_t)CAN_InitStruct->CAN_SJW << 24) |
  234. ((uint32_t)CAN_InitStruct->CAN_BS1 << 16) | ((uint32_t)CAN_InitStruct->CAN_BS2 << 20) |
  235. ((uint32_t)CAN_InitStruct->CAN_Prescaler - 1);
  236. /* Request leave initialisation */
  237. CANx->MCR &= ~MCR_INRQ;
  238. /* Wait the acknowledge */
  239. wait_ack = 0x00;
  240. while (((CANx->MSR & MSR_INAK) == MSR_INAK) && (wait_ack != INAK_TimeOut))
  241. {
  242. wait_ack++;
  243. }
  244. /* ...and check acknowledged */
  245. if ((CANx->MSR & MSR_INAK) == MSR_INAK)
  246. {
  247. InitStatus = CANINITFAILED;
  248. }
  249. else
  250. {
  251. InitStatus = CANINITOK ;
  252. }
  253. }
  254. /* At this step, return the status of initialization */
  255. return InitStatus;
  256. }
  257. /**
  258. * @brief Initializes the CAN peripheral according to the specified
  259. * parameters in the CAN_FilterInitStruct.
  260. * @param CAN_FilterInitStruct: pointer to a CAN_FilterInitTypeDef
  261. * structure that contains the configuration information.
  262. * @retval None.
  263. */
  264. void CAN_FilterInit(CAN_FilterInitTypeDef* CAN_FilterInitStruct)
  265. {
  266. uint32_t filter_number_bit_pos = 0;
  267. /* Check the parameters */
  268. assert_param(IS_CAN_FILTER_NUMBER(CAN_FilterInitStruct->CAN_FilterNumber));
  269. assert_param(IS_CAN_FILTER_MODE(CAN_FilterInitStruct->CAN_FilterMode));
  270. assert_param(IS_CAN_FILTER_SCALE(CAN_FilterInitStruct->CAN_FilterScale));
  271. assert_param(IS_CAN_FILTER_FIFO(CAN_FilterInitStruct->CAN_FilterFIFOAssignment));
  272. assert_param(IS_FUNCTIONAL_STATE(CAN_FilterInitStruct->CAN_FilterActivation));
  273. filter_number_bit_pos = ((uint32_t)0x00000001) << CAN_FilterInitStruct->CAN_FilterNumber;
  274. /* Initialisation mode for the filter */
  275. CAN1->FMR |= FMR_FINIT;
  276. /* Filter Deactivation */
  277. CAN1->FA1R &= ~(uint32_t)filter_number_bit_pos;
  278. /* Filter Scale */
  279. if (CAN_FilterInitStruct->CAN_FilterScale == CAN_FilterScale_16bit)
  280. {
  281. /* 16-bit scale for the filter */
  282. CAN1->FS1R &= ~(uint32_t)filter_number_bit_pos;
  283. /* First 16-bit identifier and First 16-bit mask */
  284. /* Or First 16-bit identifier and Second 16-bit identifier */
  285. CAN1->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR1 =
  286. ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdLow) << 16) |
  287. (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdLow);
  288. /* Second 16-bit identifier and Second 16-bit mask */
  289. /* Or Third 16-bit identifier and Fourth 16-bit identifier */
  290. CAN1->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR2 =
  291. ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdHigh) << 16) |
  292. (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdHigh);
  293. }
  294. if (CAN_FilterInitStruct->CAN_FilterScale == CAN_FilterScale_32bit)
  295. {
  296. /* 32-bit scale for the filter */
  297. CAN1->FS1R |= filter_number_bit_pos;
  298. /* 32-bit identifier or First 32-bit identifier */
  299. CAN1->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR1 =
  300. ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdHigh) << 16) |
  301. (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdLow);
  302. /* 32-bit mask or Second 32-bit identifier */
  303. CAN1->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR2 =
  304. ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdHigh) << 16) |
  305. (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdLow);
  306. }
  307. /* Filter Mode */
  308. if (CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdMask)
  309. {
  310. /*Id/Mask mode for the filter*/
  311. CAN1->FM1R &= ~(uint32_t)filter_number_bit_pos;
  312. }
  313. else /* CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdList */
  314. {
  315. /*Identifier list mode for the filter*/
  316. CAN1->FM1R |= (uint32_t)filter_number_bit_pos;
  317. }
  318. /* Filter FIFO assignment */
  319. if (CAN_FilterInitStruct->CAN_FilterFIFOAssignment == CAN_FilterFIFO0)
  320. {
  321. /* FIFO 0 assignation for the filter */
  322. CAN1->FFA1R &= ~(uint32_t)filter_number_bit_pos;
  323. }
  324. if (CAN_FilterInitStruct->CAN_FilterFIFOAssignment == CAN_FilterFIFO1)
  325. {
  326. /* FIFO 1 assignation for the filter */
  327. CAN1->FFA1R |= (uint32_t)filter_number_bit_pos;
  328. }
  329. /* Filter activation */
  330. if (CAN_FilterInitStruct->CAN_FilterActivation == ENABLE)
  331. {
  332. CAN1->FA1R |= filter_number_bit_pos;
  333. }
  334. /* Leave the initialisation mode for the filter */
  335. CAN1->FMR &= ~FMR_FINIT;
  336. }
  337. /**
  338. * @brief Fills each CAN_InitStruct member with its default value.
  339. * @param CAN_InitStruct: pointer to a CAN_InitTypeDef structure which
  340. * will be initialized.
  341. * @retval None.
  342. */
  343. void CAN_StructInit(CAN_InitTypeDef* CAN_InitStruct)
  344. {
  345. /* Reset CAN init structure parameters values */
  346. /* Initialize the time triggered communication mode */
  347. CAN_InitStruct->CAN_TTCM = DISABLE;
  348. /* Initialize the automatic bus-off management */
  349. CAN_InitStruct->CAN_ABOM = DISABLE;
  350. /* Initialize the automatic wake-up mode */
  351. CAN_InitStruct->CAN_AWUM = DISABLE;
  352. /* Initialize the no automatic retransmission */
  353. CAN_InitStruct->CAN_NART = DISABLE;
  354. /* Initialize the receive FIFO locked mode */
  355. CAN_InitStruct->CAN_RFLM = DISABLE;
  356. /* Initialize the transmit FIFO priority */
  357. CAN_InitStruct->CAN_TXFP = DISABLE;
  358. /* Initialize the CAN_Mode member */
  359. CAN_InitStruct->CAN_Mode = CAN_Mode_Normal;
  360. /* Initialize the CAN_SJW member */
  361. CAN_InitStruct->CAN_SJW = CAN_SJW_1tq;
  362. /* Initialize the CAN_BS1 member */
  363. CAN_InitStruct->CAN_BS1 = CAN_BS1_4tq;
  364. /* Initialize the CAN_BS2 member */
  365. CAN_InitStruct->CAN_BS2 = CAN_BS2_3tq;
  366. /* Initialize the CAN_Prescaler member */
  367. CAN_InitStruct->CAN_Prescaler = 1;
  368. }
  369. /**
  370. * @brief Select the start bank filter for slave CAN.
  371. * @note This function applies only to STM32 Connectivity line devices.
  372. * @param CAN_BankNumber: Select the start slave bank filter from 1..27.
  373. * @retval None.
  374. */
  375. void CAN_SlaveStartBank(uint8_t CAN_BankNumber)
  376. {
  377. /* Check the parameters */
  378. assert_param(IS_CAN_BANKNUMBER(CAN_BankNumber));
  379. /* enter Initialisation mode for the filter */
  380. CAN1->FMR |= FMR_FINIT;
  381. /* Select the start slave bank */
  382. CAN1->FMR &= (uint32_t)0xFFFFC0F1 ;
  383. CAN1->FMR |= (uint32_t)(CAN_BankNumber)<<8;
  384. /* Leave Initialisation mode for the filter */
  385. CAN1->FMR &= ~FMR_FINIT;
  386. }
  387. /**
  388. * @brief Enables or disables the specified CAN interrupts.
  389. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  390. * @param CAN_IT: specifies the CAN interrupt sources to be enabled or disabled.
  391. * This parameter can be: CAN_IT_TME, CAN_IT_FMP0, CAN_IT_FF0,
  392. * CAN_IT_FOV0, CAN_IT_FMP1, CAN_IT_FF1,
  393. * CAN_IT_FOV1, CAN_IT_EWG, CAN_IT_EPV,
  394. * CAN_IT_LEC, CAN_IT_ERR, CAN_IT_WKU or
  395. * CAN_IT_SLK.
  396. * @param NewState: new state of the CAN interrupts.
  397. * This parameter can be: ENABLE or DISABLE.
  398. * @retval None.
  399. */
  400. void CAN_ITConfig(CAN_TypeDef* CANx, uint32_t CAN_IT, FunctionalState NewState)
  401. {
  402. /* Check the parameters */
  403. assert_param(IS_CAN_ALL_PERIPH(CANx));
  404. assert_param(IS_CAN_ITConfig(CAN_IT));
  405. assert_param(IS_FUNCTIONAL_STATE(NewState));
  406. if (NewState != DISABLE)
  407. {
  408. /* Enable the selected CAN interrupt */
  409. CANx->IER |= CAN_IT;
  410. }
  411. else
  412. {
  413. /* Disable the selected CAN interrupt */
  414. CANx->IER &= ~CAN_IT;
  415. }
  416. }
  417. /**
  418. * @brief Initiates the transmission of a message.
  419. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  420. * @param TxMessage: pointer to a structure which contains CAN Id, CAN
  421. * DLC and CAN datas.
  422. * @retval The number of the mailbox that is used for transmission
  423. * or CAN_NO_MB if there is no empty mailbox.
  424. */
  425. uint8_t CAN_Transmit(CAN_TypeDef* CANx, CanTxMsg* TxMessage)
  426. {
  427. uint8_t transmit_mailbox = 0;
  428. /* Check the parameters */
  429. assert_param(IS_CAN_ALL_PERIPH(CANx));
  430. assert_param(IS_CAN_IDTYPE(TxMessage->IDE));
  431. assert_param(IS_CAN_RTR(TxMessage->RTR));
  432. assert_param(IS_CAN_DLC(TxMessage->DLC));
  433. /* Select one empty transmit mailbox */
  434. if ((CANx->TSR&TSR_TME0) == TSR_TME0)
  435. {
  436. transmit_mailbox = 0;
  437. }
  438. else if ((CANx->TSR&TSR_TME1) == TSR_TME1)
  439. {
  440. transmit_mailbox = 1;
  441. }
  442. else if ((CANx->TSR&TSR_TME2) == TSR_TME2)
  443. {
  444. transmit_mailbox = 2;
  445. }
  446. else
  447. {
  448. transmit_mailbox = CAN_NO_MB;
  449. }
  450. if (transmit_mailbox != CAN_NO_MB)
  451. {
  452. /* Set up the Id */
  453. CANx->sTxMailBox[transmit_mailbox].TIR &= TMIDxR_TXRQ;
  454. if (TxMessage->IDE == CAN_ID_STD)
  455. {
  456. assert_param(IS_CAN_STDID(TxMessage->StdId));
  457. CANx->sTxMailBox[transmit_mailbox].TIR |= ((TxMessage->StdId << 21) | TxMessage->RTR);
  458. }
  459. else
  460. {
  461. assert_param(IS_CAN_EXTID(TxMessage->ExtId));
  462. CANx->sTxMailBox[transmit_mailbox].TIR |= ((TxMessage->ExtId<<3) | TxMessage->IDE |
  463. TxMessage->RTR);
  464. }
  465. /* Set up the DLC */
  466. TxMessage->DLC &= (uint8_t)0x0000000F;
  467. CANx->sTxMailBox[transmit_mailbox].TDTR &= (uint32_t)0xFFFFFFF0;
  468. CANx->sTxMailBox[transmit_mailbox].TDTR |= TxMessage->DLC;
  469. /* Set up the data field */
  470. CANx->sTxMailBox[transmit_mailbox].TDLR = (((uint32_t)TxMessage->Data[3] << 24) |
  471. ((uint32_t)TxMessage->Data[2] << 16) |
  472. ((uint32_t)TxMessage->Data[1] << 8) |
  473. ((uint32_t)TxMessage->Data[0]));
  474. CANx->sTxMailBox[transmit_mailbox].TDHR = (((uint32_t)TxMessage->Data[7] << 24) |
  475. ((uint32_t)TxMessage->Data[6] << 16) |
  476. ((uint32_t)TxMessage->Data[5] << 8) |
  477. ((uint32_t)TxMessage->Data[4]));
  478. /* Request transmission */
  479. CANx->sTxMailBox[transmit_mailbox].TIR |= TMIDxR_TXRQ;
  480. }
  481. return transmit_mailbox;
  482. }
  483. /**
  484. * @brief Checks the transmission of a message.
  485. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  486. * @param TransmitMailbox: the number of the mailbox that is used for transmission.
  487. * @retval CANTXOK if the CAN driver transmits the message, CANTXFAILED in an other case.
  488. */
  489. uint8_t CAN_TransmitStatus(CAN_TypeDef* CANx, uint8_t TransmitMailbox)
  490. {
  491. /* RQCP, TXOK and TME bits */
  492. uint8_t state = 0;
  493. /* Check the parameters */
  494. assert_param(IS_CAN_ALL_PERIPH(CANx));
  495. assert_param(IS_CAN_TRANSMITMAILBOX(TransmitMailbox));
  496. switch (TransmitMailbox)
  497. {
  498. case (0): state |= (uint8_t)((CANx->TSR & TSR_RQCP0) << 2);
  499. state |= (uint8_t)((CANx->TSR & TSR_TXOK0) >> 0);
  500. state |= (uint8_t)((CANx->TSR & TSR_TME0) >> 26);
  501. break;
  502. case (1): state |= (uint8_t)((CANx->TSR & TSR_RQCP1) >> 6);
  503. state |= (uint8_t)((CANx->TSR & TSR_TXOK1) >> 8);
  504. state |= (uint8_t)((CANx->TSR & TSR_TME1) >> 27);
  505. break;
  506. case (2): state |= (uint8_t)((CANx->TSR & TSR_RQCP2) >> 14);
  507. state |= (uint8_t)((CANx->TSR & TSR_TXOK2) >> 16);
  508. state |= (uint8_t)((CANx->TSR & TSR_TME2) >> 28);
  509. break;
  510. default:
  511. state = CANTXFAILED;
  512. break;
  513. }
  514. switch (state)
  515. {
  516. /* transmit pending */
  517. case (0x0): state = CANTXPENDING;
  518. break;
  519. /* transmit failed */
  520. case (0x5): state = CANTXFAILED;
  521. break;
  522. /* transmit succedeed */
  523. case (0x7): state = CANTXOK;
  524. break;
  525. default:
  526. state = CANTXFAILED;
  527. break;
  528. }
  529. return state;
  530. }
  531. /**
  532. * @brief Cancels a transmit request.
  533. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  534. * @param Mailbox: Mailbox number.
  535. * @retval None.
  536. */
  537. void CAN_CancelTransmit(CAN_TypeDef* CANx, uint8_t Mailbox)
  538. {
  539. /* Check the parameters */
  540. assert_param(IS_CAN_ALL_PERIPH(CANx));
  541. assert_param(IS_CAN_TRANSMITMAILBOX(Mailbox));
  542. /* abort transmission */
  543. switch (Mailbox)
  544. {
  545. case (0): CANx->TSR |= TSR_ABRQ0;
  546. break;
  547. case (1): CANx->TSR |= TSR_ABRQ1;
  548. break;
  549. case (2): CANx->TSR |= TSR_ABRQ2;
  550. break;
  551. default:
  552. break;
  553. }
  554. }
  555. /**
  556. * @brief Releases a FIFO.
  557. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  558. * @param FIFONumber: FIFO to release, CAN_FIFO0 or CAN_FIFO1.
  559. * @retval None.
  560. */
  561. void CAN_FIFORelease(CAN_TypeDef* CANx, uint8_t FIFONumber)
  562. {
  563. /* Check the parameters */
  564. assert_param(IS_CAN_ALL_PERIPH(CANx));
  565. assert_param(IS_CAN_FIFO(FIFONumber));
  566. /* Release FIFO0 */
  567. if (FIFONumber == CAN_FIFO0)
  568. {
  569. CANx->RF0R = RF0R_RFOM0;
  570. }
  571. /* Release FIFO1 */
  572. else /* FIFONumber == CAN_FIFO1 */
  573. {
  574. CANx->RF1R = RF1R_RFOM1;
  575. }
  576. }
  577. /**
  578. * @brief Returns the number of pending messages.
  579. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  580. * @param FIFONumber: Receive FIFO number, CAN_FIFO0 or CAN_FIFO1.
  581. * @retval NbMessage which is the number of pending message.
  582. */
  583. uint8_t CAN_MessagePending(CAN_TypeDef* CANx, uint8_t FIFONumber)
  584. {
  585. uint8_t message_pending=0;
  586. /* Check the parameters */
  587. assert_param(IS_CAN_ALL_PERIPH(CANx));
  588. assert_param(IS_CAN_FIFO(FIFONumber));
  589. if (FIFONumber == CAN_FIFO0)
  590. {
  591. message_pending = (uint8_t)(CANx->RF0R&(uint32_t)0x03);
  592. }
  593. else if (FIFONumber == CAN_FIFO1)
  594. {
  595. message_pending = (uint8_t)(CANx->RF1R&(uint32_t)0x03);
  596. }
  597. else
  598. {
  599. message_pending = 0;
  600. }
  601. return message_pending;
  602. }
  603. /**
  604. * @brief Receives a message.
  605. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  606. * @param FIFONumber: Receive FIFO number, CAN_FIFO0 or CAN_FIFO1.
  607. * @param RxMessage: pointer to a structure receive message which
  608. * contains CAN Id, CAN DLC, CAN datas and FMI number.
  609. * @retval None.
  610. */
  611. void CAN_Receive(CAN_TypeDef* CANx, uint8_t FIFONumber, CanRxMsg* RxMessage)
  612. {
  613. /* Check the parameters */
  614. assert_param(IS_CAN_ALL_PERIPH(CANx));
  615. assert_param(IS_CAN_FIFO(FIFONumber));
  616. /* Get the Id */
  617. RxMessage->IDE = (uint8_t)0x04 & CANx->sFIFOMailBox[FIFONumber].RIR;
  618. if (RxMessage->IDE == CAN_ID_STD)
  619. {
  620. RxMessage->StdId = (uint32_t)0x000007FF & (CANx->sFIFOMailBox[FIFONumber].RIR >> 21);
  621. }
  622. else
  623. {
  624. RxMessage->ExtId = (uint32_t)0x1FFFFFFF & (CANx->sFIFOMailBox[FIFONumber].RIR >> 3);
  625. }
  626. RxMessage->RTR = (uint8_t)0x02 & CANx->sFIFOMailBox[FIFONumber].RIR;
  627. /* Get the DLC */
  628. RxMessage->DLC = (uint8_t)0x0F & CANx->sFIFOMailBox[FIFONumber].RDTR;
  629. /* Get the FMI */
  630. RxMessage->FMI = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDTR >> 8);
  631. /* Get the data field */
  632. RxMessage->Data[0] = (uint8_t)0xFF & CANx->sFIFOMailBox[FIFONumber].RDLR;
  633. RxMessage->Data[1] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDLR >> 8);
  634. RxMessage->Data[2] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDLR >> 16);
  635. RxMessage->Data[3] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDLR >> 24);
  636. RxMessage->Data[4] = (uint8_t)0xFF & CANx->sFIFOMailBox[FIFONumber].RDHR;
  637. RxMessage->Data[5] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDHR >> 8);
  638. RxMessage->Data[6] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDHR >> 16);
  639. RxMessage->Data[7] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDHR >> 24);
  640. /* Release the FIFO */
  641. CAN_FIFORelease(CANx, FIFONumber);
  642. }
  643. /**
  644. * @brief Enables or disables the DBG Freeze for CAN.
  645. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  646. * @param NewState: new state of the CAN peripheral.
  647. * This parameter can be: ENABLE or DISABLE.
  648. * @retval None.
  649. */
  650. void CAN_DBGFreeze(CAN_TypeDef* CANx, FunctionalState NewState)
  651. {
  652. /* Check the parameters */
  653. assert_param(IS_CAN_ALL_PERIPH(CANx));
  654. assert_param(IS_FUNCTIONAL_STATE(NewState));
  655. if (NewState != DISABLE)
  656. {
  657. /* Enable Debug Freeze */
  658. CANx->MCR |= MCR_DBF;
  659. }
  660. else
  661. {
  662. /* Disable Debug Freeze */
  663. CANx->MCR &= ~MCR_DBF;
  664. }
  665. }
  666. /**
  667. * @brief Enters the low power mode.
  668. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  669. * @retval CANSLEEPOK if sleep entered, CANSLEEPFAILED in an other case.
  670. */
  671. uint8_t CAN_Sleep(CAN_TypeDef* CANx)
  672. {
  673. uint8_t sleepstatus = CANSLEEPFAILED;
  674. /* Check the parameters */
  675. assert_param(IS_CAN_ALL_PERIPH(CANx));
  676. /* Request Sleep mode */
  677. CANx->MCR = (((CANx->MCR) & (uint32_t)(~MCR_INRQ)) | MCR_SLEEP);
  678. /* Sleep mode status */
  679. if ((CANx->MSR & (CAN_MSR_SLAK|CAN_MSR_INAK)) == CAN_MSR_SLAK)
  680. {
  681. /* Sleep mode not entered */
  682. sleepstatus = CANSLEEPOK;
  683. }
  684. /* At this step, sleep mode status */
  685. return (uint8_t)sleepstatus;
  686. }
  687. /**
  688. * @brief Wakes the CAN up.
  689. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  690. * @retval CANWAKEUPOK if sleep mode left, CANWAKEUPFAILED in an other case.
  691. */
  692. uint8_t CAN_WakeUp(CAN_TypeDef* CANx)
  693. {
  694. uint32_t wait_slak = SLAK_TimeOut ;
  695. uint8_t wakeupstatus = CANWAKEUPFAILED;
  696. /* Check the parameters */
  697. assert_param(IS_CAN_ALL_PERIPH(CANx));
  698. /* Wake up request */
  699. CANx->MCR &= ~MCR_SLEEP;
  700. /* Sleep mode status */
  701. while(((CANx->MSR & CAN_MSR_SLAK) == CAN_MSR_SLAK)&&(wait_slak!=0x00))
  702. {
  703. wait_slak--;
  704. }
  705. if((CANx->MSR & CAN_MSR_SLAK) != CAN_MSR_SLAK)
  706. {
  707. /* Sleep mode exited */
  708. wakeupstatus = CANWAKEUPOK;
  709. }
  710. /* At this step, sleep mode status */
  711. return (uint8_t)wakeupstatus;
  712. }
  713. /**
  714. * @brief Checks whether the specified CAN flag is set or not.
  715. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  716. * @param CAN_FLAG: specifies the flag to check.
  717. * This parameter can be: CAN_FLAG_EWG, CAN_FLAG_EPV or CAN_FLAG_BOF.
  718. * @retval The new state of CAN_FLAG (SET or RESET).
  719. */
  720. FlagStatus CAN_GetFlagStatus(CAN_TypeDef* CANx, uint32_t CAN_FLAG)
  721. {
  722. FlagStatus bitstatus = RESET;
  723. /* Check the parameters */
  724. assert_param(IS_CAN_ALL_PERIPH(CANx));
  725. assert_param(IS_CAN_FLAG(CAN_FLAG));
  726. /* Check the status of the specified CAN flag */
  727. if ((CANx->ESR & CAN_FLAG) != (uint32_t)RESET)
  728. {
  729. /* CAN_FLAG is set */
  730. bitstatus = SET;
  731. }
  732. else
  733. {
  734. /* CAN_FLAG is reset */
  735. bitstatus = RESET;
  736. }
  737. /* Return the CAN_FLAG status */
  738. return bitstatus;
  739. }
  740. /**
  741. * @brief Clears the CAN's pending flags.
  742. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  743. * @param CAN_FLAG: specifies the flag to clear.
  744. * @retval None.
  745. */
  746. void CAN_ClearFlag(CAN_TypeDef* CANx, uint32_t CAN_FLAG)
  747. {
  748. /* Check the parameters */
  749. assert_param(IS_CAN_ALL_PERIPH(CANx));
  750. assert_param(IS_CAN_FLAG(CAN_FLAG));
  751. /* Clear the selected CAN flags */
  752. CANx->ESR &= ~CAN_FLAG;
  753. }
  754. /**
  755. * @brief Checks whether the specified CAN interrupt has occurred or not.
  756. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  757. * @param CAN_IT: specifies the CAN interrupt source to check.
  758. * This parameter can be: CAN_IT_RQCP0, CAN_IT_RQCP1, CAN_IT_RQCP2,
  759. * CAN_IT_FF0, CAN_IT_FOV0, CAN_IT_FF1,
  760. * CAN_IT_FOV1, CAN_IT_EWG, CAN_IT_EPV,
  761. * CAN_IT_BOF, CAN_IT_WKU or CAN_IT_SLK.
  762. * @retval The new state of CAN_IT (SET or RESET).
  763. */
  764. ITStatus CAN_GetITStatus(CAN_TypeDef* CANx, uint32_t CAN_IT)
  765. {
  766. ITStatus pendingbitstatus = RESET;
  767. /* Check the parameters */
  768. assert_param(IS_CAN_ALL_PERIPH(CANx));
  769. assert_param(IS_CAN_ITStatus(CAN_IT));
  770. switch (CAN_IT)
  771. {
  772. case CAN_IT_RQCP0:
  773. pendingbitstatus = CheckITStatus(CANx->TSR, TSR_RQCP0);
  774. break;
  775. case CAN_IT_RQCP1:
  776. pendingbitstatus = CheckITStatus(CANx->TSR, TSR_RQCP1);
  777. break;
  778. case CAN_IT_RQCP2:
  779. pendingbitstatus = CheckITStatus(CANx->TSR, TSR_RQCP2);
  780. break;
  781. case CAN_IT_FF0:
  782. pendingbitstatus = CheckITStatus(CANx->RF0R, RF0R_FULL0);
  783. break;
  784. case CAN_IT_FOV0:
  785. pendingbitstatus = CheckITStatus(CANx->RF0R, RF0R_FOVR0);
  786. break;
  787. case CAN_IT_FF1:
  788. pendingbitstatus = CheckITStatus(CANx->RF1R, RF1R_FULL1);
  789. break;
  790. case CAN_IT_FOV1:
  791. pendingbitstatus = CheckITStatus(CANx->RF1R, RF1R_FOVR1);
  792. break;
  793. case CAN_IT_EWG:
  794. pendingbitstatus = CheckITStatus(CANx->ESR, ESR_EWGF);
  795. break;
  796. case CAN_IT_EPV:
  797. pendingbitstatus = CheckITStatus(CANx->ESR, ESR_EPVF);
  798. break;
  799. case CAN_IT_BOF:
  800. pendingbitstatus = CheckITStatus(CANx->ESR, ESR_BOFF);
  801. break;
  802. case CAN_IT_SLK:
  803. pendingbitstatus = CheckITStatus(CANx->MSR, MSR_SLAKI);
  804. break;
  805. case CAN_IT_WKU:
  806. pendingbitstatus = CheckITStatus(CANx->MSR, MSR_WKUI);
  807. break;
  808. default :
  809. pendingbitstatus = RESET;
  810. break;
  811. }
  812. /* Return the CAN_IT status */
  813. return pendingbitstatus;
  814. }
  815. /**
  816. * @brief Clears the CAN’s interrupt pending bits.
  817. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  818. * @param CAN_IT: specifies the interrupt pending bit to clear.
  819. * @retval None.
  820. */
  821. void CAN_ClearITPendingBit(CAN_TypeDef* CANx, uint32_t CAN_IT)
  822. {
  823. /* Check the parameters */
  824. assert_param(IS_CAN_ALL_PERIPH(CANx));
  825. assert_param(IS_CAN_ITStatus(CAN_IT));
  826. switch (CAN_IT)
  827. {
  828. case CAN_IT_RQCP0:
  829. CANx->TSR = TSR_RQCP0; /* rc_w1*/
  830. break;
  831. case CAN_IT_RQCP1:
  832. CANx->TSR = TSR_RQCP1; /* rc_w1*/
  833. break;
  834. case CAN_IT_RQCP2:
  835. CANx->TSR = TSR_RQCP2; /* rc_w1*/
  836. break;
  837. case CAN_IT_FF0:
  838. CANx->RF0R = RF0R_FULL0; /* rc_w1*/
  839. break;
  840. case CAN_IT_FOV0:
  841. CANx->RF0R = RF0R_FOVR0; /* rc_w1*/
  842. break;
  843. case CAN_IT_FF1:
  844. CANx->RF1R = RF1R_FULL1; /* rc_w1*/
  845. break;
  846. case CAN_IT_FOV1:
  847. CANx->RF1R = RF1R_FOVR1; /* rc_w1*/
  848. break;
  849. case CAN_IT_EWG:
  850. CANx->ESR &= ~ ESR_EWGF; /* rw */
  851. break;
  852. case CAN_IT_EPV:
  853. CANx->ESR &= ~ ESR_EPVF; /* rw */
  854. break;
  855. case CAN_IT_BOF:
  856. CANx->ESR &= ~ ESR_BOFF; /* rw */
  857. break;
  858. case CAN_IT_WKU:
  859. CANx->MSR = MSR_WKUI; /* rc_w1*/
  860. break;
  861. case CAN_IT_SLK:
  862. CANx->MSR = MSR_SLAKI; /* rc_w1*/
  863. break;
  864. default :
  865. break;
  866. }
  867. }
  868. /**
  869. * @brief Checks whether the CAN interrupt has occurred or not.
  870. * @param CAN_Reg: specifies the CAN interrupt register to check.
  871. * @param It_Bit: specifies the interrupt source bit to check.
  872. * @retval The new state of the CAN Interrupt (SET or RESET).
  873. */
  874. static ITStatus CheckITStatus(uint32_t CAN_Reg, uint32_t It_Bit)
  875. {
  876. ITStatus pendingbitstatus = RESET;
  877. if ((CAN_Reg & It_Bit) != (uint32_t)RESET)
  878. {
  879. /* CAN_IT is set */
  880. pendingbitstatus = SET;
  881. }
  882. else
  883. {
  884. /* CAN_IT is reset */
  885. pendingbitstatus = RESET;
  886. }
  887. return pendingbitstatus;
  888. }
  889. /**
  890. * @}
  891. */
  892. /**
  893. * @}
  894. */
  895. /**
  896. * @}
  897. */
  898. /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/