You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

6170 lines
204 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_hal_fdcan.c
  4. * @author MCD Application Team
  5. * @brief FDCAN HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Flexible DataRate Controller Area Network
  8. * (FDCAN) peripheral:
  9. * + Initialization and de-initialization functions
  10. * + IO operation functions
  11. * + Peripheral Configuration and Control functions
  12. * + Peripheral State and Error functions
  13. *
  14. @verbatim
  15. ==============================================================================
  16. ##### How to use this driver #####
  17. ==============================================================================
  18. [..]
  19. (#) Initialize the FDCAN peripheral using HAL_FDCAN_Init function.
  20. (#) If needed , configure the reception filters and optional features using
  21. the following configuration functions:
  22. (++) HAL_FDCAN_ConfigClockCalibration
  23. (++) HAL_FDCAN_ConfigFilter
  24. (++) HAL_FDCAN_ConfigGlobalFilter
  25. (++) HAL_FDCAN_ConfigExtendedIdMask
  26. (++) HAL_FDCAN_ConfigRxFifoOverwrite
  27. (++) HAL_FDCAN_ConfigFifoWatermark
  28. (++) HAL_FDCAN_ConfigRamWatchdog
  29. (++) HAL_FDCAN_ConfigTimestampCounter
  30. (++) HAL_FDCAN_EnableTimestampCounter
  31. (++) HAL_FDCAN_DisableTimestampCounter
  32. (++) HAL_FDCAN_ConfigTimeoutCounter
  33. (++) HAL_FDCAN_EnableTimeoutCounter
  34. (++) HAL_FDCAN_DisableTimeoutCounter
  35. (++) HAL_FDCAN_ConfigTxDelayCompensation
  36. (++) HAL_FDCAN_EnableTxDelayCompensation
  37. (++) HAL_FDCAN_DisableTxDelayCompensation
  38. (++) HAL_FDCAN_EnableISOMode
  39. (++) HAL_FDCAN_DisableISOMode
  40. (++) HAL_FDCAN_EnableEdgeFiltering
  41. (++) HAL_FDCAN_DisableEdgeFiltering
  42. (++) HAL_FDCAN_TT_ConfigOperation
  43. (++) HAL_FDCAN_TT_ConfigReferenceMessage
  44. (++) HAL_FDCAN_TT_ConfigTrigger
  45. (#) Start the FDCAN module using HAL_FDCAN_Start function. At this level
  46. the node is active on the bus: it can send and receive messages.
  47. (#) The following Tx control functions can only be called when the FDCAN
  48. module is started:
  49. (++) HAL_FDCAN_AddMessageToTxFifoQ
  50. (++) HAL_FDCAN_EnableTxBufferRequest
  51. (++) HAL_FDCAN_AbortTxRequest
  52. (#) After having submitted a Tx request in Tx Fifo or Queue, it is possible to
  53. get Tx buffer location used to place the Tx request thanks to
  54. HAL_FDCAN_GetLatestTxFifoQRequestBuffer API.
  55. It is then possible to abort later on the corresponding Tx Request using
  56. HAL_FDCAN_AbortTxRequest API.
  57. (#) When a message is received into the FDCAN message RAM, it can be
  58. retrieved using the HAL_FDCAN_GetRxMessage function.
  59. (#) Calling the HAL_FDCAN_Stop function stops the FDCAN module by entering
  60. it to initialization mode and re-enabling access to configuration
  61. registers through the configuration functions listed here above.
  62. (#) All other control functions can be called any time after initialization
  63. phase, no matter if the FDCAN module is started or stoped.
  64. *** Polling mode operation ***
  65. ==============================
  66. [..]
  67. (#) Reception and transmission states can be monitored via the following
  68. functions:
  69. (++) HAL_FDCAN_IsRxBufferMessageAvailable
  70. (++) HAL_FDCAN_IsTxBufferMessagePending
  71. (++) HAL_FDCAN_GetRxFifoFillLevel
  72. (++) HAL_FDCAN_GetTxFifoFreeLevel
  73. *** Interrupt mode operation ***
  74. ================================
  75. [..]
  76. (#) There are two interrupt lines: line 0 and 1.
  77. By default, all interrupts are assigned to line 0. Interrupt lines
  78. can be configured using HAL_FDCAN_ConfigInterruptLines function.
  79. (#) Notifications are activated using HAL_FDCAN_ActivateNotification
  80. function. Then, the process can be controlled through one of the
  81. available user callbacks: HAL_FDCAN_xxxCallback.
  82. *** Callback registration ***
  83. =============================================
  84. The compilation define USE_HAL_FDCAN_REGISTER_CALLBACKS when set to 1
  85. allows the user to configure dynamically the driver callbacks.
  86. Use Function @ref HAL_FDCAN_RegisterCallback() or HAL_FDCAN_RegisterXXXCallback()
  87. to register an interrupt callback.
  88. Function @ref HAL_FDCAN_RegisterCallback() allows to register following callbacks:
  89. (+) TxFifoEmptyCallback : Tx Fifo Empty Callback.
  90. (+) RxBufferNewMessageCallback : Rx Buffer New Message Callback.
  91. (+) HighPriorityMessageCallback : High Priority Message Callback.
  92. (+) TimestampWraparoundCallback : Timestamp Wraparound Callback.
  93. (+) TimeoutOccurredCallback : Timeout Occurred Callback.
  94. (+) ErrorCallback : Error Callback.
  95. (+) MspInitCallback : FDCAN MspInit.
  96. (+) MspDeInitCallback : FDCAN MspDeInit.
  97. This function takes as parameters the HAL peripheral handle, the Callback ID
  98. and a pointer to the user callback function.
  99. For specific callbacks ClockCalibrationCallback, TxEventFifoCallback, RxFifo0Callback, RxFifo1Callback,
  100. TxBufferCompleteCallback, TxBufferAbortCallback, ErrorStatusCallback, TT_ScheduleSyncCallback, TT_TimeMarkCallback,
  101. TT_StopWatchCallback and TT_GlobalTimeCallback, use dedicated register callbacks :
  102. respectively @ref HAL_FDCAN_RegisterClockCalibrationCallback(), @ref HAL_FDCAN_RegisterTxEventFifoCallback(),
  103. @ref HAL_FDCAN_RegisterRxFifo0Callback(), @ref HAL_FDCAN_RegisterRxFifo1Callback(),
  104. @ref HAL_FDCAN_RegisterTxBufferCompleCallback(), @ref HAL_FDCAN_RegisterTxBufferAbortCallback(),
  105. @ref HAL_FDCAN_RegisterErrorStatusCallback(), @ref HAL_FDCAN_TT_RegisterScheduleSyncCallback(),
  106. @ref HAL_FDCAN_TT_RegisterTimeMarkCallback(), @ref HAL_FDCAN_TT_RegisterStopWatchCallback() and
  107. @ref HAL_FDCAN_TT_RegisterGlobalTimeCallback().
  108. Use function @ref HAL_FDCAN_UnRegisterCallback() to reset a callback to the default
  109. weak function.
  110. @ref HAL_FDCAN_UnRegisterCallback takes as parameters the HAL peripheral handle,
  111. and the Callback ID.
  112. This function allows to reset following callbacks:
  113. (+) TxFifoEmptyCallback : Tx Fifo Empty Callback.
  114. (+) RxBufferNewMessageCallback : Rx Buffer New Message Callback.
  115. (+) HighPriorityMessageCallback : High Priority Message Callback.
  116. (+) TimestampWraparoundCallback : Timestamp Wraparound Callback.
  117. (+) TimeoutOccurredCallback : Timeout Occurred Callback.
  118. (+) ErrorCallback : Error Callback.
  119. (+) MspInitCallback : FDCAN MspInit.
  120. (+) MspDeInitCallback : FDCAN MspDeInit.
  121. For specific callbacks ClockCalibrationCallback, TxEventFifoCallback, RxFifo0Callback,
  122. RxFifo1Callback, TxBufferCompleteCallback, TxBufferAbortCallback, TT_ScheduleSyncCallback,
  123. TT_TimeMarkCallback, TT_StopWatchCallback and TT_GlobalTimeCallback, use dedicated
  124. register callbacks : respectively @ref HAL_FDCAN_UnRegisterClockCalibrationCallback(),
  125. @ref HAL_FDCAN_UnRegisterTxEventFifoCallback(), @ref HAL_FDCAN_UnRegisterRxFifo0Callback(),
  126. @ref HAL_FDCAN_UnRegisterRxFifo1Callback(), @ref HAL_FDCAN_UnRegisterTxBufferCompleCallback(),
  127. @ref HAL_FDCAN_UnRegisterTxBufferAbortCallback(), @ref HAL_FDCAN_UnRegisterErrorStatusCallback(),
  128. @ref HAL_FDCAN_TT_UnRegisterScheduleSyncCallback(), @ref HAL_FDCAN_TT_UnRegisterTimeMarkCallback(),
  129. @ref HAL_FDCAN_TT_UnRegisterStopWatchCallback() and @ref HAL_FDCAN_TT_UnRegisterGlobalTimeCallback().
  130. By default, after the @ref HAL_FDCAN_Init() and when the state is HAL_FDCAN_STATE_RESET,
  131. all callbacks are set to the corresponding weak functions:
  132. examples @ref HAL_FDCAN_ErrorCallback().
  133. Exception done for MspInit and MspDeInit functions that are
  134. reset to the legacy weak function in the @ref HAL_FDCAN_Init()/ @ref HAL_FDCAN_DeInit() only when
  135. these callbacks are null (not registered beforehand).
  136. if not, MspInit or MspDeInit are not null, the @ref HAL_FDCAN_Init()/ @ref HAL_FDCAN_DeInit()
  137. keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  138. Callbacks can be registered/unregistered in HAL_FDCAN_STATE_READY state only.
  139. Exception done MspInit/MspDeInit that can be registered/unregistered
  140. in HAL_FDCAN_STATE_READY or HAL_FDCAN_STATE_RESET state,
  141. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  142. In that case first register the MspInit/MspDeInit user callbacks
  143. using @ref HAL_FDCAN_RegisterCallback() before calling @ref HAL_FDCAN_DeInit()
  144. or @ref HAL_FDCAN_Init() function.
  145. When The compilation define USE_HAL_FDCAN_REGISTER_CALLBACKS is set to 0 or
  146. not defined, the callback registration feature is not available and all callbacks
  147. are set to the corresponding weak functions.
  148. @endverbatim
  149. ******************************************************************************
  150. * @attention
  151. *
  152. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  153. * All rights reserved.</center></h2>
  154. *
  155. * This software component is licensed by ST under BSD 3-Clause license,
  156. * the "License"; You may not use this file except in compliance with the
  157. * License. You may obtain a copy of the License at:
  158. * opensource.org/licenses/BSD-3-Clause
  159. *
  160. ******************************************************************************
  161. */
  162. /* Includes ------------------------------------------------------------------*/
  163. #include "stm32h7xx_hal.h"
  164. /** @addtogroup STM32H7xx_HAL_Driver
  165. * @{
  166. */
  167. /** @defgroup FDCAN FDCAN
  168. * @brief FDCAN HAL module driver
  169. * @{
  170. */
  171. #ifdef HAL_FDCAN_MODULE_ENABLED
  172. /* Private typedef -----------------------------------------------------------*/
  173. /* Private define ------------------------------------------------------------*/
  174. /** @addtogroup FDCAN_Private_Constants
  175. * @{
  176. */
  177. #define FDCAN_TIMEOUT_VALUE 10U
  178. #define FDCAN_TX_EVENT_FIFO_MASK (FDCAN_IR_TEFL | FDCAN_IR_TEFF | FDCAN_IR_TEFW | FDCAN_IR_TEFN)
  179. #define FDCAN_RX_FIFO0_MASK (FDCAN_IR_RF0L | FDCAN_IR_RF0F | FDCAN_IR_RF0W | FDCAN_IR_RF0N)
  180. #define FDCAN_RX_FIFO1_MASK (FDCAN_IR_RF1L | FDCAN_IR_RF1F | FDCAN_IR_RF1W | FDCAN_IR_RF1N)
  181. #define FDCAN_ERROR_MASK (FDCAN_IR_ELO | FDCAN_IR_WDI | FDCAN_IR_PEA | FDCAN_IR_PED | FDCAN_IR_ARA)
  182. #define FDCAN_ERROR_STATUS_MASK (FDCAN_IR_EP | FDCAN_IR_EW | FDCAN_IR_BO)
  183. #define FDCAN_TT_SCHEDULE_SYNC_MASK (FDCAN_TTIR_SBC | FDCAN_TTIR_SMC | FDCAN_TTIR_CSM | FDCAN_TTIR_SOG)
  184. #define FDCAN_TT_TIME_MARK_MASK (FDCAN_TTIR_RTMI | FDCAN_TTIR_TTMI)
  185. #define FDCAN_TT_GLOBAL_TIME_MASK (FDCAN_TTIR_GTW | FDCAN_TTIR_GTD)
  186. #define FDCAN_TT_DISTURBING_ERROR_MASK (FDCAN_TTIR_GTE | FDCAN_TTIR_TXU | FDCAN_TTIR_TXO | \
  187. FDCAN_TTIR_SE1 | FDCAN_TTIR_SE2 | FDCAN_TTIR_ELC)
  188. #define FDCAN_TT_FATAL_ERROR_MASK (FDCAN_TTIR_IWT | FDCAN_TTIR_WT | FDCAN_TTIR_AW | FDCAN_TTIR_CER)
  189. #define FDCAN_ELEMENT_MASK_STDID ((uint32_t)0x1FFC0000U) /* Standard Identifier */
  190. #define FDCAN_ELEMENT_MASK_EXTID ((uint32_t)0x1FFFFFFFU) /* Extended Identifier */
  191. #define FDCAN_ELEMENT_MASK_RTR ((uint32_t)0x20000000U) /* Remote Transmission Request */
  192. #define FDCAN_ELEMENT_MASK_XTD ((uint32_t)0x40000000U) /* Extended Identifier */
  193. #define FDCAN_ELEMENT_MASK_ESI ((uint32_t)0x80000000U) /* Error State Indicator */
  194. #define FDCAN_ELEMENT_MASK_TS ((uint32_t)0x0000FFFFU) /* Timestamp */
  195. #define FDCAN_ELEMENT_MASK_DLC ((uint32_t)0x000F0000U) /* Data Length Code */
  196. #define FDCAN_ELEMENT_MASK_BRS ((uint32_t)0x00100000U) /* Bit Rate Switch */
  197. #define FDCAN_ELEMENT_MASK_FDF ((uint32_t)0x00200000U) /* FD Format */
  198. #define FDCAN_ELEMENT_MASK_EFC ((uint32_t)0x00800000U) /* Event FIFO Control */
  199. #define FDCAN_ELEMENT_MASK_MM ((uint32_t)0xFF000000U) /* Message Marker */
  200. #define FDCAN_ELEMENT_MASK_FIDX ((uint32_t)0x7F000000U) /* Filter Index */
  201. #define FDCAN_ELEMENT_MASK_ANMF ((uint32_t)0x80000000U) /* Accepted Non-matching Frame */
  202. #define FDCAN_ELEMENT_MASK_ET ((uint32_t)0x00C00000U) /* Event type */
  203. #define FDCAN_MESSAGE_RAM_SIZE 0x2800U
  204. #define FDCAN_MESSAGE_RAM_END_ADDRESS (SRAMCAN_BASE + FDCAN_MESSAGE_RAM_SIZE - 0x4U) /* The Message RAM has a width of 4 Bytes */
  205. /**
  206. * @}
  207. */
  208. /* Private macro -------------------------------------------------------------*/
  209. /* Private variables ---------------------------------------------------------*/
  210. static const uint8_t DLCtoBytes[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 20, 24, 32, 48, 64};
  211. /* Private function prototypes -----------------------------------------------*/
  212. /** @addtogroup FDCAN_Private_Functions_Prototypes
  213. * @{
  214. */
  215. static HAL_StatusTypeDef FDCAN_CalcultateRamBlockAddresses(FDCAN_HandleTypeDef *hfdcan);
  216. static void FDCAN_CopyMessageToRAM(FDCAN_HandleTypeDef *hfdcan, FDCAN_TxHeaderTypeDef *pTxHeader, uint8_t *pTxData, uint32_t BufferIndex);
  217. /**
  218. * @}
  219. */
  220. /* Exported functions --------------------------------------------------------*/
  221. /** @defgroup FDCAN_Exported_Functions FDCAN Exported Functions
  222. * @{
  223. */
  224. /** @defgroup FDCAN_Exported_Functions_Group1 Initialization and de-initialization functions
  225. * @brief Initialization and Configuration functions
  226. *
  227. @verbatim
  228. ==============================================================================
  229. ##### Initialization and de-initialization functions #####
  230. ==============================================================================
  231. [..] This section provides functions allowing to:
  232. (+) Initialize and configure the FDCAN.
  233. (+) De-initialize the FDCAN.
  234. (+) Enter FDCAN peripheral in power down mode.
  235. (+) Exit power down mode.
  236. (+) Register callbacks.
  237. (+) Unregister callbacks.
  238. @endverbatim
  239. * @{
  240. */
  241. /**
  242. * @brief Initializes the FDCAN peripheral according to the specified
  243. * parameters in the FDCAN_InitTypeDef structure.
  244. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  245. * the configuration information for the specified FDCAN.
  246. * @retval HAL status
  247. */
  248. HAL_StatusTypeDef HAL_FDCAN_Init(FDCAN_HandleTypeDef *hfdcan)
  249. {
  250. uint32_t tickstart;
  251. HAL_StatusTypeDef status;
  252. const uint32_t CvtEltSize[] = {0, 0, 0, 0, 0, 1, 2, 3, 4, 0, 5, 0, 0, 0, 6, 0, 0, 0, 7};
  253. /* Check FDCAN handle */
  254. if (hfdcan == NULL)
  255. {
  256. return HAL_ERROR;
  257. }
  258. /* Check FDCAN instance */
  259. if (hfdcan->Instance == FDCAN1)
  260. {
  261. hfdcan->ttcan = (TTCAN_TypeDef *)((uint32_t)hfdcan->Instance + 0x100U);
  262. }
  263. /* Check function parameters */
  264. assert_param(IS_FDCAN_ALL_INSTANCE(hfdcan->Instance));
  265. assert_param(IS_FDCAN_FRAME_FORMAT(hfdcan->Init.FrameFormat));
  266. assert_param(IS_FDCAN_MODE(hfdcan->Init.Mode));
  267. assert_param(IS_FUNCTIONAL_STATE(hfdcan->Init.AutoRetransmission));
  268. assert_param(IS_FUNCTIONAL_STATE(hfdcan->Init.TransmitPause));
  269. assert_param(IS_FUNCTIONAL_STATE(hfdcan->Init.ProtocolException));
  270. assert_param(IS_FDCAN_NOMINAL_PRESCALER(hfdcan->Init.NominalPrescaler));
  271. assert_param(IS_FDCAN_NOMINAL_SJW(hfdcan->Init.NominalSyncJumpWidth));
  272. assert_param(IS_FDCAN_NOMINAL_TSEG1(hfdcan->Init.NominalTimeSeg1));
  273. assert_param(IS_FDCAN_NOMINAL_TSEG2(hfdcan->Init.NominalTimeSeg2));
  274. if (hfdcan->Init.FrameFormat == FDCAN_FRAME_FD_BRS)
  275. {
  276. assert_param(IS_FDCAN_DATA_PRESCALER(hfdcan->Init.DataPrescaler));
  277. assert_param(IS_FDCAN_DATA_SJW(hfdcan->Init.DataSyncJumpWidth));
  278. assert_param(IS_FDCAN_DATA_TSEG1(hfdcan->Init.DataTimeSeg1));
  279. assert_param(IS_FDCAN_DATA_TSEG2(hfdcan->Init.DataTimeSeg2));
  280. }
  281. assert_param(IS_FDCAN_MAX_VALUE(hfdcan->Init.StdFiltersNbr, 128U));
  282. assert_param(IS_FDCAN_MAX_VALUE(hfdcan->Init.ExtFiltersNbr, 64U));
  283. assert_param(IS_FDCAN_MAX_VALUE(hfdcan->Init.RxFifo0ElmtsNbr, 64U));
  284. if (hfdcan->Init.RxFifo0ElmtsNbr > 0U)
  285. {
  286. assert_param(IS_FDCAN_DATA_SIZE(hfdcan->Init.RxFifo0ElmtSize));
  287. }
  288. assert_param(IS_FDCAN_MAX_VALUE(hfdcan->Init.RxFifo1ElmtsNbr, 64U));
  289. if (hfdcan->Init.RxFifo1ElmtsNbr > 0U)
  290. {
  291. assert_param(IS_FDCAN_DATA_SIZE(hfdcan->Init.RxFifo1ElmtSize));
  292. }
  293. assert_param(IS_FDCAN_MAX_VALUE(hfdcan->Init.RxBuffersNbr, 64U));
  294. if (hfdcan->Init.RxBuffersNbr > 0U)
  295. {
  296. assert_param(IS_FDCAN_DATA_SIZE(hfdcan->Init.RxBufferSize));
  297. }
  298. assert_param(IS_FDCAN_MAX_VALUE(hfdcan->Init.TxEventsNbr, 32U));
  299. assert_param(IS_FDCAN_MAX_VALUE((hfdcan->Init.TxBuffersNbr + hfdcan->Init.TxFifoQueueElmtsNbr), 32U));
  300. if (hfdcan->Init.TxFifoQueueElmtsNbr > 0U)
  301. {
  302. assert_param(IS_FDCAN_TX_FIFO_QUEUE_MODE(hfdcan->Init.TxFifoQueueMode));
  303. }
  304. if ((hfdcan->Init.TxBuffersNbr + hfdcan->Init.TxFifoQueueElmtsNbr) > 0U)
  305. {
  306. assert_param(IS_FDCAN_DATA_SIZE(hfdcan->Init.TxElmtSize));
  307. }
  308. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  309. if (hfdcan->State == HAL_FDCAN_STATE_RESET)
  310. {
  311. /* Allocate lock resource and initialize it */
  312. hfdcan->Lock = HAL_UNLOCKED;
  313. /* Reset callbacks to legacy functions */
  314. hfdcan->ClockCalibrationCallback = HAL_FDCAN_ClockCalibrationCallback; /* Legacy weak ClockCalibrationCallback */
  315. hfdcan->TxEventFifoCallback = HAL_FDCAN_TxEventFifoCallback; /* Legacy weak TxEventFifoCallback */
  316. hfdcan->RxFifo0Callback = HAL_FDCAN_RxFifo0Callback; /* Legacy weak RxFifo0Callback */
  317. hfdcan->RxFifo1Callback = HAL_FDCAN_RxFifo1Callback; /* Legacy weak RxFifo1Callback */
  318. hfdcan->TxFifoEmptyCallback = HAL_FDCAN_TxFifoEmptyCallback; /* Legacy weak TxFifoEmptyCallback */
  319. hfdcan->TxBufferCompleteCallback = HAL_FDCAN_TxBufferCompleteCallback; /* Legacy weak TxBufferCompleteCallback */
  320. hfdcan->TxBufferAbortCallback = HAL_FDCAN_TxBufferAbortCallback; /* Legacy weak TxBufferAbortCallback */
  321. hfdcan->RxBufferNewMessageCallback = HAL_FDCAN_RxBufferNewMessageCallback; /* Legacy weak RxBufferNewMessageCallback */
  322. hfdcan->HighPriorityMessageCallback = HAL_FDCAN_HighPriorityMessageCallback; /* Legacy weak HighPriorityMessageCallback */
  323. hfdcan->TimestampWraparoundCallback = HAL_FDCAN_TimestampWraparoundCallback; /* Legacy weak TimestampWraparoundCallback */
  324. hfdcan->TimeoutOccurredCallback = HAL_FDCAN_TimeoutOccurredCallback; /* Legacy weak TimeoutOccurredCallback */
  325. hfdcan->ErrorCallback = HAL_FDCAN_ErrorCallback; /* Legacy weak ErrorCallback */
  326. hfdcan->ErrorStatusCallback = HAL_FDCAN_ErrorStatusCallback; /* Legacy weak ErrorStatusCallback */
  327. hfdcan->TT_ScheduleSyncCallback = HAL_FDCAN_TT_ScheduleSyncCallback; /* Legacy weak TT_ScheduleSyncCallback */
  328. hfdcan->TT_TimeMarkCallback = HAL_FDCAN_TT_TimeMarkCallback; /* Legacy weak TT_TimeMarkCallback */
  329. hfdcan->TT_StopWatchCallback = HAL_FDCAN_TT_StopWatchCallback; /* Legacy weak TT_StopWatchCallback */
  330. hfdcan->TT_GlobalTimeCallback = HAL_FDCAN_TT_GlobalTimeCallback; /* Legacy weak TT_GlobalTimeCallback */
  331. if (hfdcan->MspInitCallback == NULL)
  332. {
  333. hfdcan->MspInitCallback = HAL_FDCAN_MspInit; /* Legacy weak MspInit */
  334. }
  335. /* Init the low level hardware: CLOCK, NVIC */
  336. hfdcan->MspInitCallback(hfdcan);
  337. }
  338. #else
  339. if (hfdcan->State == HAL_FDCAN_STATE_RESET)
  340. {
  341. /* Allocate lock resource and initialize it */
  342. hfdcan->Lock = HAL_UNLOCKED;
  343. /* Init the low level hardware: CLOCK, NVIC */
  344. HAL_FDCAN_MspInit(hfdcan);
  345. }
  346. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  347. /* Exit from Sleep mode */
  348. CLEAR_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_CSR);
  349. /* Get tick */
  350. tickstart = HAL_GetTick();
  351. /* Check Sleep mode acknowledge */
  352. while ((hfdcan->Instance->CCCR & FDCAN_CCCR_CSA) == FDCAN_CCCR_CSA)
  353. {
  354. if ((HAL_GetTick() - tickstart) > FDCAN_TIMEOUT_VALUE)
  355. {
  356. /* Update error code */
  357. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  358. /* Change FDCAN state */
  359. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  360. return HAL_ERROR;
  361. }
  362. }
  363. /* Request initialisation */
  364. SET_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_INIT);
  365. /* Get tick */
  366. tickstart = HAL_GetTick();
  367. /* Wait until the INIT bit into CCCR register is set */
  368. while ((hfdcan->Instance->CCCR & FDCAN_CCCR_INIT) == 0U)
  369. {
  370. /* Check for the Timeout */
  371. if ((HAL_GetTick() - tickstart) > FDCAN_TIMEOUT_VALUE)
  372. {
  373. /* Update error code */
  374. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  375. /* Change FDCAN state */
  376. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  377. return HAL_ERROR;
  378. }
  379. }
  380. /* Enable configuration change */
  381. SET_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_CCE);
  382. /* Set the no automatic retransmission */
  383. if (hfdcan->Init.AutoRetransmission == ENABLE)
  384. {
  385. CLEAR_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_DAR);
  386. }
  387. else
  388. {
  389. SET_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_DAR);
  390. }
  391. /* Set the transmit pause feature */
  392. if (hfdcan->Init.TransmitPause == ENABLE)
  393. {
  394. SET_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_TXP);
  395. }
  396. else
  397. {
  398. CLEAR_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_TXP);
  399. }
  400. /* Set the Protocol Exception Handling */
  401. if (hfdcan->Init.ProtocolException == ENABLE)
  402. {
  403. CLEAR_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_PXHD);
  404. }
  405. else
  406. {
  407. SET_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_PXHD);
  408. }
  409. /* Set FDCAN Frame Format */
  410. MODIFY_REG(hfdcan->Instance->CCCR, FDCAN_FRAME_FD_BRS, hfdcan->Init.FrameFormat);
  411. /* Reset FDCAN Operation Mode */
  412. CLEAR_BIT(hfdcan->Instance->CCCR, (FDCAN_CCCR_TEST | FDCAN_CCCR_MON | FDCAN_CCCR_ASM));
  413. CLEAR_BIT(hfdcan->Instance->TEST, FDCAN_TEST_LBCK);
  414. /* Set FDCAN Operating Mode:
  415. | Normal | Restricted | Bus | Internal | External
  416. | | Operation | Monitoring | LoopBack | LoopBack
  417. CCCR.TEST | 0 | 0 | 0 | 1 | 1
  418. CCCR.MON | 0 | 0 | 1 | 1 | 0
  419. TEST.LBCK | 0 | 0 | 0 | 1 | 1
  420. CCCR.ASM | 0 | 1 | 0 | 0 | 0
  421. */
  422. if (hfdcan->Init.Mode == FDCAN_MODE_RESTRICTED_OPERATION)
  423. {
  424. /* Enable Restricted Operation mode */
  425. SET_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_ASM);
  426. }
  427. else if (hfdcan->Init.Mode != FDCAN_MODE_NORMAL)
  428. {
  429. if (hfdcan->Init.Mode != FDCAN_MODE_BUS_MONITORING)
  430. {
  431. /* Enable write access to TEST register */
  432. SET_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_TEST);
  433. /* Enable LoopBack mode */
  434. SET_BIT(hfdcan->Instance->TEST, FDCAN_TEST_LBCK);
  435. if (hfdcan->Init.Mode == FDCAN_MODE_INTERNAL_LOOPBACK)
  436. {
  437. SET_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_MON);
  438. }
  439. }
  440. else
  441. {
  442. /* Enable bus monitoring mode */
  443. SET_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_MON);
  444. }
  445. }
  446. else
  447. {
  448. /* Nothing to do: normal mode */
  449. }
  450. /* Set the nominal bit timing register */
  451. hfdcan->Instance->NBTP = ((((uint32_t)hfdcan->Init.NominalSyncJumpWidth - 1U) << FDCAN_NBTP_NSJW_Pos) | \
  452. (((uint32_t)hfdcan->Init.NominalTimeSeg1 - 1U) << FDCAN_NBTP_NTSEG1_Pos) | \
  453. (((uint32_t)hfdcan->Init.NominalTimeSeg2 - 1U) << FDCAN_NBTP_NTSEG2_Pos) | \
  454. (((uint32_t)hfdcan->Init.NominalPrescaler - 1U) << FDCAN_NBTP_NBRP_Pos));
  455. /* If FD operation with BRS is selected, set the data bit timing register */
  456. if (hfdcan->Init.FrameFormat == FDCAN_FRAME_FD_BRS)
  457. {
  458. hfdcan->Instance->DBTP = ((((uint32_t)hfdcan->Init.DataSyncJumpWidth - 1U) << FDCAN_DBTP_DSJW_Pos) | \
  459. (((uint32_t)hfdcan->Init.DataTimeSeg1 - 1U) << FDCAN_DBTP_DTSEG1_Pos) | \
  460. (((uint32_t)hfdcan->Init.DataTimeSeg2 - 1U) << FDCAN_DBTP_DTSEG2_Pos) | \
  461. (((uint32_t)hfdcan->Init.DataPrescaler - 1U) << FDCAN_DBTP_DBRP_Pos));
  462. }
  463. if (hfdcan->Init.TxFifoQueueElmtsNbr > 0U)
  464. {
  465. /* Select between Tx FIFO and Tx Queue operation modes */
  466. SET_BIT(hfdcan->Instance->TXBC, hfdcan->Init.TxFifoQueueMode);
  467. }
  468. /* Configure Tx element size */
  469. if ((hfdcan->Init.TxBuffersNbr + hfdcan->Init.TxFifoQueueElmtsNbr) > 0U)
  470. {
  471. MODIFY_REG(hfdcan->Instance->TXESC, FDCAN_TXESC_TBDS, CvtEltSize[hfdcan->Init.TxElmtSize]);
  472. }
  473. /* Configure Rx FIFO 0 element size */
  474. if (hfdcan->Init.RxFifo0ElmtsNbr > 0U)
  475. {
  476. MODIFY_REG(hfdcan->Instance->RXESC, FDCAN_RXESC_F0DS, (CvtEltSize[hfdcan->Init.RxFifo0ElmtSize] << FDCAN_RXESC_F0DS_Pos));
  477. }
  478. /* Configure Rx FIFO 1 element size */
  479. if (hfdcan->Init.RxFifo1ElmtsNbr > 0U)
  480. {
  481. MODIFY_REG(hfdcan->Instance->RXESC, FDCAN_RXESC_F1DS, (CvtEltSize[hfdcan->Init.RxFifo1ElmtSize] << FDCAN_RXESC_F1DS_Pos));
  482. }
  483. /* Configure Rx buffer element size */
  484. if (hfdcan->Init.RxBuffersNbr > 0U)
  485. {
  486. MODIFY_REG(hfdcan->Instance->RXESC, FDCAN_RXESC_RBDS, (CvtEltSize[hfdcan->Init.RxBufferSize] << FDCAN_RXESC_RBDS_Pos));
  487. }
  488. /* By default operation mode is set to Event-driven communication.
  489. If Time-triggered communication is needed, user should call the
  490. HAL_FDCAN_TT_ConfigOperation function just after the HAL_FDCAN_Init */
  491. if (hfdcan->Instance == FDCAN1)
  492. {
  493. CLEAR_BIT(hfdcan->ttcan->TTOCF, FDCAN_TTOCF_OM);
  494. }
  495. /* Initialize the Latest Tx FIFO/Queue request buffer index */
  496. hfdcan->LatestTxFifoQRequest = 0U;
  497. /* Initialize the error code */
  498. hfdcan->ErrorCode = HAL_FDCAN_ERROR_NONE;
  499. /* Initialize the FDCAN state */
  500. hfdcan->State = HAL_FDCAN_STATE_READY;
  501. /* Calculate each RAM block address */
  502. status = FDCAN_CalcultateRamBlockAddresses(hfdcan);
  503. /* Return function status */
  504. return status;
  505. }
  506. /**
  507. * @brief Deinitializes the FDCAN peripheral registers to their default reset values.
  508. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  509. * the configuration information for the specified FDCAN.
  510. * @retval HAL status
  511. */
  512. HAL_StatusTypeDef HAL_FDCAN_DeInit(FDCAN_HandleTypeDef *hfdcan)
  513. {
  514. /* Check FDCAN handle */
  515. if (hfdcan == NULL)
  516. {
  517. return HAL_ERROR;
  518. }
  519. /* Check function parameters */
  520. assert_param(IS_FDCAN_ALL_INSTANCE(hfdcan->Instance));
  521. /* Stop the FDCAN module: return value is voluntary ignored */
  522. (void)HAL_FDCAN_Stop(hfdcan);
  523. /* Disable Interrupt lines */
  524. CLEAR_BIT(hfdcan->Instance->ILE, (FDCAN_INTERRUPT_LINE0 | FDCAN_INTERRUPT_LINE1));
  525. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  526. if (hfdcan->MspDeInitCallback == NULL)
  527. {
  528. hfdcan->MspDeInitCallback = HAL_FDCAN_MspDeInit; /* Legacy weak MspDeInit */
  529. }
  530. /* DeInit the low level hardware: CLOCK, NVIC */
  531. hfdcan->MspDeInitCallback(hfdcan);
  532. #else
  533. /* DeInit the low level hardware: CLOCK, NVIC */
  534. HAL_FDCAN_MspDeInit(hfdcan);
  535. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  536. /* Reset the FDCAN ErrorCode */
  537. hfdcan->ErrorCode = HAL_FDCAN_ERROR_NONE;
  538. /* Change FDCAN state */
  539. hfdcan->State = HAL_FDCAN_STATE_RESET;
  540. /* Return function status */
  541. return HAL_OK;
  542. }
  543. /**
  544. * @brief Initializes the FDCAN MSP.
  545. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  546. * the configuration information for the specified FDCAN.
  547. * @retval None
  548. */
  549. __weak void HAL_FDCAN_MspInit(FDCAN_HandleTypeDef *hfdcan)
  550. {
  551. /* Prevent unused argument(s) compilation warning */
  552. UNUSED(hfdcan);
  553. /* NOTE : This function Should not be modified, when the callback is needed,
  554. the HAL_FDCAN_MspInit could be implemented in the user file
  555. */
  556. }
  557. /**
  558. * @brief DeInitializes the FDCAN MSP.
  559. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  560. * the configuration information for the specified FDCAN.
  561. * @retval None
  562. */
  563. __weak void HAL_FDCAN_MspDeInit(FDCAN_HandleTypeDef *hfdcan)
  564. {
  565. /* Prevent unused argument(s) compilation warning */
  566. UNUSED(hfdcan);
  567. /* NOTE : This function Should not be modified, when the callback is needed,
  568. the HAL_FDCAN_MspDeInit could be implemented in the user file
  569. */
  570. }
  571. /**
  572. * @brief Enter FDCAN peripheral in sleep mode.
  573. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  574. * the configuration information for the specified FDCAN.
  575. * @retval HAL status
  576. */
  577. HAL_StatusTypeDef HAL_FDCAN_EnterPowerDownMode(FDCAN_HandleTypeDef *hfdcan)
  578. {
  579. uint32_t tickstart;
  580. /* Request clock stop */
  581. SET_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_CSR);
  582. /* Get tick */
  583. tickstart = HAL_GetTick();
  584. /* Wait until FDCAN is ready for power down */
  585. while ((hfdcan->Instance->CCCR & FDCAN_CCCR_CSA) == 0U)
  586. {
  587. if ((HAL_GetTick() - tickstart) > FDCAN_TIMEOUT_VALUE)
  588. {
  589. /* Update error code */
  590. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  591. /* Change FDCAN state */
  592. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  593. return HAL_ERROR;
  594. }
  595. }
  596. /* Return function status */
  597. return HAL_OK;
  598. }
  599. /**
  600. * @brief Exit power down mode.
  601. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  602. * the configuration information for the specified FDCAN.
  603. * @retval HAL status
  604. */
  605. HAL_StatusTypeDef HAL_FDCAN_ExitPowerDownMode(FDCAN_HandleTypeDef *hfdcan)
  606. {
  607. uint32_t tickstart;
  608. /* Reset clock stop request */
  609. CLEAR_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_CSR);
  610. /* Get tick */
  611. tickstart = HAL_GetTick();
  612. /* Wait until FDCAN exits sleep mode */
  613. while ((hfdcan->Instance->CCCR & FDCAN_CCCR_CSA) == FDCAN_CCCR_CSA)
  614. {
  615. if ((HAL_GetTick() - tickstart) > FDCAN_TIMEOUT_VALUE)
  616. {
  617. /* Update error code */
  618. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  619. /* Change FDCAN state */
  620. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  621. return HAL_ERROR;
  622. }
  623. }
  624. /* Enter normal operation */
  625. CLEAR_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_INIT);
  626. /* Return function status */
  627. return HAL_OK;
  628. }
  629. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  630. /**
  631. * @brief Register a FDCAN CallBack.
  632. * To be used instead of the weak predefined callback
  633. * @param hfdcan pointer to a FDCAN_HandleTypeDef structure that contains
  634. * the configuration information for FDCAN module
  635. * @param CallbackID ID of the callback to be registered
  636. * This parameter can be one of the following values:
  637. * @arg @ref HAL_FDCAN_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty callback ID
  638. * @arg @ref HAL_FDCAN_RX_BUFFER_NEW_MSG_CB_ID Rx buffer new message callback ID
  639. * @arg @ref HAL_FDCAN_HIGH_PRIO_MESSAGE_CB_ID High priority message callback ID
  640. * @arg @ref HAL_FDCAN_TIMESTAMP_WRAPAROUND_CB_ID Timestamp wraparound callback ID
  641. * @arg @ref HAL_FDCAN_TIMEOUT_OCCURRED_CB_ID Timeout occurred callback ID
  642. * @arg @ref HAL_FDCAN_ERROR_CALLBACK_CB_ID Error callback ID
  643. * @arg @ref HAL_FDCAN_MSPINIT_CB_ID MspInit callback ID
  644. * @arg @ref HAL_FDCAN_MSPDEINIT_CB_ID MspDeInit callback ID
  645. * @param pCallback pointer to the Callback function
  646. * @retval HAL status
  647. */
  648. HAL_StatusTypeDef HAL_FDCAN_RegisterCallback(FDCAN_HandleTypeDef *hfdcan, HAL_FDCAN_CallbackIDTypeDef CallbackID, void (* pCallback)(FDCAN_HandleTypeDef *_hFDCAN))
  649. {
  650. HAL_StatusTypeDef status = HAL_OK;
  651. if (pCallback == NULL)
  652. {
  653. /* Update the error code */
  654. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  655. return HAL_ERROR;
  656. }
  657. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  658. {
  659. switch (CallbackID)
  660. {
  661. case HAL_FDCAN_TX_FIFO_EMPTY_CB_ID :
  662. hfdcan->TxFifoEmptyCallback = pCallback;
  663. break;
  664. case HAL_FDCAN_RX_BUFFER_NEW_MSG_CB_ID :
  665. hfdcan->RxBufferNewMessageCallback = pCallback;
  666. break;
  667. case HAL_FDCAN_HIGH_PRIO_MESSAGE_CB_ID :
  668. hfdcan->HighPriorityMessageCallback = pCallback;
  669. break;
  670. case HAL_FDCAN_TIMESTAMP_WRAPAROUND_CB_ID :
  671. hfdcan->TimestampWraparoundCallback = pCallback;
  672. break;
  673. case HAL_FDCAN_TIMEOUT_OCCURRED_CB_ID :
  674. hfdcan->TimeoutOccurredCallback = pCallback;
  675. break;
  676. case HAL_FDCAN_ERROR_CALLBACK_CB_ID :
  677. hfdcan->ErrorCallback = pCallback;
  678. break;
  679. case HAL_FDCAN_MSPINIT_CB_ID :
  680. hfdcan->MspInitCallback = pCallback;
  681. break;
  682. case HAL_FDCAN_MSPDEINIT_CB_ID :
  683. hfdcan->MspDeInitCallback = pCallback;
  684. break;
  685. default :
  686. /* Update the error code */
  687. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  688. /* Return error status */
  689. status = HAL_ERROR;
  690. break;
  691. }
  692. }
  693. else if (hfdcan->State == HAL_FDCAN_STATE_RESET)
  694. {
  695. switch (CallbackID)
  696. {
  697. case HAL_FDCAN_MSPINIT_CB_ID :
  698. hfdcan->MspInitCallback = pCallback;
  699. break;
  700. case HAL_FDCAN_MSPDEINIT_CB_ID :
  701. hfdcan->MspDeInitCallback = pCallback;
  702. break;
  703. default :
  704. /* Update the error code */
  705. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  706. /* Return error status */
  707. status = HAL_ERROR;
  708. break;
  709. }
  710. }
  711. else
  712. {
  713. /* Update the error code */
  714. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  715. /* Return error status */
  716. status = HAL_ERROR;
  717. }
  718. return status;
  719. }
  720. /**
  721. * @brief Unregister a FDCAN CallBack.
  722. * FDCAN callback is redirected to the weak predefined callback
  723. * @param hfdcan pointer to a FDCAN_HandleTypeDef structure that contains
  724. * the configuration information for FDCAN module
  725. * @param CallbackID ID of the callback to be unregistered
  726. * This parameter can be one of the following values:
  727. * @arg @ref HAL_FDCAN_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty callback ID
  728. * @arg @ref HAL_FDCAN_RX_BUFFER_NEW_MSG_CB_ID Rx buffer new message callback ID
  729. * @arg @ref HAL_FDCAN_HIGH_PRIO_MESSAGE_CB_ID High priority message callback ID
  730. * @arg @ref HAL_FDCAN_TIMESTAMP_WRAPAROUND_CB_ID Timestamp wraparound callback ID
  731. * @arg @ref HAL_FDCAN_TIMEOUT_OCCURRED_CB_ID Timeout occurred callback ID
  732. * @arg @ref HAL_FDCAN_ERROR_CALLBACK_CB_ID Error callback ID
  733. * @arg @ref HAL_FDCAN_MSPINIT_CB_ID MspInit callback ID
  734. * @arg @ref HAL_FDCAN_MSPDEINIT_CB_ID MspDeInit callback ID
  735. * @retval HAL status
  736. */
  737. HAL_StatusTypeDef HAL_FDCAN_UnRegisterCallback(FDCAN_HandleTypeDef *hfdcan, HAL_FDCAN_CallbackIDTypeDef CallbackID)
  738. {
  739. HAL_StatusTypeDef status = HAL_OK;
  740. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  741. {
  742. switch (CallbackID)
  743. {
  744. case HAL_FDCAN_TX_FIFO_EMPTY_CB_ID :
  745. hfdcan->TxFifoEmptyCallback = HAL_FDCAN_TxFifoEmptyCallback;
  746. break;
  747. case HAL_FDCAN_RX_BUFFER_NEW_MSG_CB_ID :
  748. hfdcan->RxBufferNewMessageCallback = HAL_FDCAN_RxBufferNewMessageCallback;
  749. break;
  750. case HAL_FDCAN_HIGH_PRIO_MESSAGE_CB_ID :
  751. hfdcan->HighPriorityMessageCallback = HAL_FDCAN_HighPriorityMessageCallback;
  752. break;
  753. case HAL_FDCAN_TIMESTAMP_WRAPAROUND_CB_ID :
  754. hfdcan->TimestampWraparoundCallback = HAL_FDCAN_TimestampWraparoundCallback;
  755. break;
  756. case HAL_FDCAN_TIMEOUT_OCCURRED_CB_ID :
  757. hfdcan->TimeoutOccurredCallback = HAL_FDCAN_TimeoutOccurredCallback;
  758. break;
  759. case HAL_FDCAN_ERROR_CALLBACK_CB_ID :
  760. hfdcan->ErrorCallback = HAL_FDCAN_ErrorCallback;
  761. break;
  762. case HAL_FDCAN_MSPINIT_CB_ID :
  763. hfdcan->MspInitCallback = HAL_FDCAN_MspInit;
  764. break;
  765. case HAL_FDCAN_MSPDEINIT_CB_ID :
  766. hfdcan->MspDeInitCallback = HAL_FDCAN_MspDeInit;
  767. break;
  768. default :
  769. /* Update the error code */
  770. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  771. /* Return error status */
  772. status = HAL_ERROR;
  773. break;
  774. }
  775. }
  776. else if (hfdcan->State == HAL_FDCAN_STATE_RESET)
  777. {
  778. switch (CallbackID)
  779. {
  780. case HAL_FDCAN_MSPINIT_CB_ID :
  781. hfdcan->MspInitCallback = HAL_FDCAN_MspInit;
  782. break;
  783. case HAL_FDCAN_MSPDEINIT_CB_ID :
  784. hfdcan->MspDeInitCallback = HAL_FDCAN_MspDeInit;
  785. break;
  786. default :
  787. /* Update the error code */
  788. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  789. /* Return error status */
  790. status = HAL_ERROR;
  791. break;
  792. }
  793. }
  794. else
  795. {
  796. /* Update the error code */
  797. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  798. /* Return error status */
  799. status = HAL_ERROR;
  800. }
  801. return status;
  802. }
  803. /**
  804. * @brief Register Clock Calibration FDCAN Callback
  805. * To be used instead of the weak HAL_FDCAN_ClockCalibrationCallback() predefined callback
  806. * @param hfdcan FDCAN handle
  807. * @param pCallback pointer to the Clock Calibration Callback function
  808. * @retval HAL status
  809. */
  810. HAL_StatusTypeDef HAL_FDCAN_RegisterClockCalibrationCallback(FDCAN_HandleTypeDef *hfdcan, pFDCAN_ClockCalibrationCallbackTypeDef pCallback)
  811. {
  812. HAL_StatusTypeDef status = HAL_OK;
  813. if (pCallback == NULL)
  814. {
  815. /* Update the error code */
  816. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  817. return HAL_ERROR;
  818. }
  819. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  820. {
  821. hfdcan->ClockCalibrationCallback = pCallback;
  822. }
  823. else
  824. {
  825. /* Update the error code */
  826. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  827. /* Return error status */
  828. status = HAL_ERROR;
  829. }
  830. return status;
  831. }
  832. /**
  833. * @brief UnRegister the Clock Calibration FDCAN Callback
  834. * Clock Calibration FDCAN Callback is redirected to the weak HAL_FDCAN_ClockCalibrationCallback() predefined callback
  835. * @param hfdcan FDCAN handle
  836. * @retval HAL status
  837. */
  838. HAL_StatusTypeDef HAL_FDCAN_UnRegisterClockCalibrationCallback(FDCAN_HandleTypeDef *hfdcan)
  839. {
  840. HAL_StatusTypeDef status = HAL_OK;
  841. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  842. {
  843. hfdcan->ClockCalibrationCallback = HAL_FDCAN_ClockCalibrationCallback; /* Legacy weak ClockCalibrationCallback */
  844. }
  845. else
  846. {
  847. /* Update the error code */
  848. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  849. /* Return error status */
  850. status = HAL_ERROR;
  851. }
  852. return status;
  853. }
  854. /**
  855. * @brief Register Tx Event Fifo FDCAN Callback
  856. * To be used instead of the weak HAL_FDCAN_TxEventFifoCallback() predefined callback
  857. * @param hfdcan FDCAN handle
  858. * @param pCallback pointer to the Tx Event Fifo Callback function
  859. * @retval HAL status
  860. */
  861. HAL_StatusTypeDef HAL_FDCAN_RegisterTxEventFifoCallback(FDCAN_HandleTypeDef *hfdcan, pFDCAN_TxEventFifoCallbackTypeDef pCallback)
  862. {
  863. HAL_StatusTypeDef status = HAL_OK;
  864. if (pCallback == NULL)
  865. {
  866. /* Update the error code */
  867. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  868. return HAL_ERROR;
  869. }
  870. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  871. {
  872. hfdcan->TxEventFifoCallback = pCallback;
  873. }
  874. else
  875. {
  876. /* Update the error code */
  877. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  878. /* Return error status */
  879. status = HAL_ERROR;
  880. }
  881. return status;
  882. }
  883. /**
  884. * @brief UnRegister the Tx Event Fifo FDCAN Callback
  885. * Tx Event Fifo FDCAN Callback is redirected to the weak HAL_FDCAN_TxEventFifoCallback() predefined callback
  886. * @param hfdcan FDCAN handle
  887. * @retval HAL status
  888. */
  889. HAL_StatusTypeDef HAL_FDCAN_UnRegisterTxEventFifoCallback(FDCAN_HandleTypeDef *hfdcan)
  890. {
  891. HAL_StatusTypeDef status = HAL_OK;
  892. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  893. {
  894. hfdcan->TxEventFifoCallback = HAL_FDCAN_TxEventFifoCallback; /* Legacy weak TxEventFifoCallback */
  895. }
  896. else
  897. {
  898. /* Update the error code */
  899. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  900. /* Return error status */
  901. status = HAL_ERROR;
  902. }
  903. return status;
  904. }
  905. /**
  906. * @brief Register Rx Fifo 0 FDCAN Callback
  907. * To be used instead of the weak HAL_FDCAN_RxFifo0Callback() predefined callback
  908. * @param hfdcan FDCAN handle
  909. * @param pCallback pointer to the Rx Fifo 0 Callback function
  910. * @retval HAL status
  911. */
  912. HAL_StatusTypeDef HAL_FDCAN_RegisterRxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, pFDCAN_RxFifo0CallbackTypeDef pCallback)
  913. {
  914. HAL_StatusTypeDef status = HAL_OK;
  915. if (pCallback == NULL)
  916. {
  917. /* Update the error code */
  918. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  919. return HAL_ERROR;
  920. }
  921. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  922. {
  923. hfdcan->RxFifo0Callback = pCallback;
  924. }
  925. else
  926. {
  927. /* Update the error code */
  928. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  929. /* Return error status */
  930. status = HAL_ERROR;
  931. }
  932. return status;
  933. }
  934. /**
  935. * @brief UnRegister the Rx Fifo 0 FDCAN Callback
  936. * Rx Fifo 0 FDCAN Callback is redirected to the weak HAL_FDCAN_RxFifo0Callback() predefined callback
  937. * @param hfdcan FDCAN handle
  938. * @retval HAL status
  939. */
  940. HAL_StatusTypeDef HAL_FDCAN_UnRegisterRxFifo0Callback(FDCAN_HandleTypeDef *hfdcan)
  941. {
  942. HAL_StatusTypeDef status = HAL_OK;
  943. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  944. {
  945. hfdcan->RxFifo0Callback = HAL_FDCAN_RxFifo0Callback; /* Legacy weak RxFifo0Callback */
  946. }
  947. else
  948. {
  949. /* Update the error code */
  950. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  951. /* Return error status */
  952. status = HAL_ERROR;
  953. }
  954. return status;
  955. }
  956. /**
  957. * @brief Register Rx Fifo 1 FDCAN Callback
  958. * To be used instead of the weak HAL_FDCAN_RxFifo1Callback() predefined callback
  959. * @param hfdcan FDCAN handle
  960. * @param pCallback pointer to the Rx Fifo 1 Callback function
  961. * @retval HAL status
  962. */
  963. HAL_StatusTypeDef HAL_FDCAN_RegisterRxFifo1Callback(FDCAN_HandleTypeDef *hfdcan, pFDCAN_RxFifo1CallbackTypeDef pCallback)
  964. {
  965. HAL_StatusTypeDef status = HAL_OK;
  966. if (pCallback == NULL)
  967. {
  968. /* Update the error code */
  969. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  970. return HAL_ERROR;
  971. }
  972. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  973. {
  974. hfdcan->RxFifo1Callback = pCallback;
  975. }
  976. else
  977. {
  978. /* Update the error code */
  979. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  980. /* Return error status */
  981. status = HAL_ERROR;
  982. }
  983. return status;
  984. }
  985. /**
  986. * @brief UnRegister the Rx Fifo 1 FDCAN Callback
  987. * Rx Fifo 1 FDCAN Callback is redirected to the weak HAL_FDCAN_RxFifo1Callback() predefined callback
  988. * @param hfdcan FDCAN handle
  989. * @retval HAL status
  990. */
  991. HAL_StatusTypeDef HAL_FDCAN_UnRegisterRxFifo1Callback(FDCAN_HandleTypeDef *hfdcan)
  992. {
  993. HAL_StatusTypeDef status = HAL_OK;
  994. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  995. {
  996. hfdcan->RxFifo1Callback = HAL_FDCAN_RxFifo1Callback; /* Legacy weak RxFifo1Callback */
  997. }
  998. else
  999. {
  1000. /* Update the error code */
  1001. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  1002. /* Return error status */
  1003. status = HAL_ERROR;
  1004. }
  1005. return status;
  1006. }
  1007. /**
  1008. * @brief Register Tx Buffer Complete FDCAN Callback
  1009. * To be used instead of the weak HAL_FDCAN_TxBufferCompleteCallback() predefined callback
  1010. * @param hfdcan FDCAN handle
  1011. * @param pCallback pointer to the Tx Buffer Complete Callback function
  1012. * @retval HAL status
  1013. */
  1014. HAL_StatusTypeDef HAL_FDCAN_RegisterTxBufferCompleteCallback(FDCAN_HandleTypeDef *hfdcan, pFDCAN_TxBufferCompleteCallbackTypeDef pCallback)
  1015. {
  1016. HAL_StatusTypeDef status = HAL_OK;
  1017. if (pCallback == NULL)
  1018. {
  1019. /* Update the error code */
  1020. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  1021. return HAL_ERROR;
  1022. }
  1023. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1024. {
  1025. hfdcan->TxBufferCompleteCallback = pCallback;
  1026. }
  1027. else
  1028. {
  1029. /* Update the error code */
  1030. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  1031. /* Return error status */
  1032. status = HAL_ERROR;
  1033. }
  1034. return status;
  1035. }
  1036. /**
  1037. * @brief UnRegister the Tx Buffer Complete FDCAN Callback
  1038. * Tx Buffer Complete FDCAN Callback is redirected to the weak HAL_FDCAN_TxBufferCompleteCallback() predefined callback
  1039. * @param hfdcan FDCAN handle
  1040. * @retval HAL status
  1041. */
  1042. HAL_StatusTypeDef HAL_FDCAN_UnRegisterTxBufferCompleteCallback(FDCAN_HandleTypeDef *hfdcan)
  1043. {
  1044. HAL_StatusTypeDef status = HAL_OK;
  1045. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1046. {
  1047. hfdcan->TxBufferCompleteCallback = HAL_FDCAN_TxBufferCompleteCallback; /* Legacy weak TxBufferCompleteCallback */
  1048. }
  1049. else
  1050. {
  1051. /* Update the error code */
  1052. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  1053. /* Return error status */
  1054. status = HAL_ERROR;
  1055. }
  1056. return status;
  1057. }
  1058. /**
  1059. * @brief Register Tx Buffer Abort FDCAN Callback
  1060. * To be used instead of the weak HAL_FDCAN_TxBufferAbortCallback() predefined callback
  1061. * @param hfdcan FDCAN handle
  1062. * @param pCallback pointer to the Tx Buffer Abort Callback function
  1063. * @retval HAL status
  1064. */
  1065. HAL_StatusTypeDef HAL_FDCAN_RegisterTxBufferAbortCallback(FDCAN_HandleTypeDef *hfdcan, pFDCAN_TxBufferAbortCallbackTypeDef pCallback)
  1066. {
  1067. HAL_StatusTypeDef status = HAL_OK;
  1068. if (pCallback == NULL)
  1069. {
  1070. /* Update the error code */
  1071. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  1072. return HAL_ERROR;
  1073. }
  1074. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1075. {
  1076. hfdcan->TxBufferAbortCallback = pCallback;
  1077. }
  1078. else
  1079. {
  1080. /* Update the error code */
  1081. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  1082. /* Return error status */
  1083. status = HAL_ERROR;
  1084. }
  1085. return status;
  1086. }
  1087. /**
  1088. * @brief UnRegister the Tx Buffer Abort FDCAN Callback
  1089. * Tx Buffer Abort FDCAN Callback is redirected to the weak HAL_FDCAN_TxBufferAbortCallback() predefined callback
  1090. * @param hfdcan FDCAN handle
  1091. * @retval HAL status
  1092. */
  1093. HAL_StatusTypeDef HAL_FDCAN_UnRegisterTxBufferAbortCallback(FDCAN_HandleTypeDef *hfdcan)
  1094. {
  1095. HAL_StatusTypeDef status = HAL_OK;
  1096. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1097. {
  1098. hfdcan->TxBufferAbortCallback = HAL_FDCAN_TxBufferAbortCallback; /* Legacy weak TxBufferAbortCallback */
  1099. }
  1100. else
  1101. {
  1102. /* Update the error code */
  1103. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  1104. /* Return error status */
  1105. status = HAL_ERROR;
  1106. }
  1107. return status;
  1108. }
  1109. /**
  1110. * @brief Register Error Status FDCAN Callback
  1111. * To be used instead of the weak HAL_FDCAN_ErrorStatusCallback() predefined callback
  1112. * @param hfdcan FDCAN handle
  1113. * @param pCallback pointer to the Error Status Callback function
  1114. * @retval HAL status
  1115. */
  1116. HAL_StatusTypeDef HAL_FDCAN_RegisterErrorStatusCallback(FDCAN_HandleTypeDef *hfdcan, pFDCAN_ErrorStatusCallbackTypeDef pCallback)
  1117. {
  1118. HAL_StatusTypeDef status = HAL_OK;
  1119. if (pCallback == NULL)
  1120. {
  1121. /* Update the error code */
  1122. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  1123. return HAL_ERROR;
  1124. }
  1125. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1126. {
  1127. hfdcan->ErrorStatusCallback = pCallback;
  1128. }
  1129. else
  1130. {
  1131. /* Update the error code */
  1132. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  1133. /* Return error status */
  1134. status = HAL_ERROR;
  1135. }
  1136. return status;
  1137. }
  1138. /**
  1139. * @brief UnRegister the Error Status FDCAN Callback
  1140. * Error Status FDCAN Callback is redirected to the weak HAL_FDCAN_ErrorStatusCallback() predefined callback
  1141. * @param hfdcan FDCAN handle
  1142. * @retval HAL status
  1143. */
  1144. HAL_StatusTypeDef HAL_FDCAN_UnRegisterErrorStatusCallback(FDCAN_HandleTypeDef *hfdcan)
  1145. {
  1146. HAL_StatusTypeDef status = HAL_OK;
  1147. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1148. {
  1149. hfdcan->ErrorStatusCallback = HAL_FDCAN_ErrorStatusCallback; /* Legacy weak ErrorStatusCallback */
  1150. }
  1151. else
  1152. {
  1153. /* Update the error code */
  1154. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  1155. /* Return error status */
  1156. status = HAL_ERROR;
  1157. }
  1158. return status;
  1159. }
  1160. /**
  1161. * @brief Register TT Schedule Synchronization FDCAN Callback
  1162. * To be used instead of the weak HAL_FDCAN_TT_ScheduleSyncCallback() predefined callback
  1163. * @param hfdcan FDCAN handle
  1164. * @param pCallback pointer to the TT Schedule Synchronization Callback function
  1165. * @retval HAL status
  1166. */
  1167. HAL_StatusTypeDef HAL_FDCAN_RegisterTTScheduleSyncCallback(FDCAN_HandleTypeDef *hfdcan, pFDCAN_TT_ScheduleSyncCallbackTypeDef pCallback)
  1168. {
  1169. HAL_StatusTypeDef status = HAL_OK;
  1170. if (pCallback == NULL)
  1171. {
  1172. /* Update the error code */
  1173. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  1174. return HAL_ERROR;
  1175. }
  1176. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1177. {
  1178. hfdcan->TT_ScheduleSyncCallback = pCallback;
  1179. }
  1180. else
  1181. {
  1182. /* Update the error code */
  1183. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  1184. /* Return error status */
  1185. status = HAL_ERROR;
  1186. }
  1187. return status;
  1188. }
  1189. /**
  1190. * @brief UnRegister the TT Schedule Synchronization FDCAN Callback
  1191. * TT Schedule Synchronization Callback is redirected to the weak HAL_FDCAN_TT_ScheduleSyncCallback() predefined callback
  1192. * @param hfdcan FDCAN handle
  1193. * @retval HAL status
  1194. */
  1195. HAL_StatusTypeDef HAL_FDCAN_UnRegisterTTScheduleSyncCallback(FDCAN_HandleTypeDef *hfdcan)
  1196. {
  1197. HAL_StatusTypeDef status = HAL_OK;
  1198. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1199. {
  1200. hfdcan->TT_ScheduleSyncCallback = HAL_FDCAN_TT_ScheduleSyncCallback; /* Legacy weak TT_ScheduleSyncCallback */
  1201. }
  1202. else
  1203. {
  1204. /* Update the error code */
  1205. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  1206. /* Return error status */
  1207. status = HAL_ERROR;
  1208. }
  1209. return status;
  1210. }
  1211. /**
  1212. * @brief Register TT Time Mark FDCAN Callback
  1213. * To be used instead of the weak HAL_FDCAN_TT_TimeMarkCallback() predefined callback
  1214. * @param hfdcan FDCAN handle
  1215. * @param pCallback pointer to the TT Time Mark Callback function
  1216. * @retval HAL status
  1217. */
  1218. HAL_StatusTypeDef HAL_FDCAN_RegisterTTTimeMarkCallback(FDCAN_HandleTypeDef *hfdcan, pFDCAN_TT_TimeMarkCallbackTypeDef pCallback)
  1219. {
  1220. HAL_StatusTypeDef status = HAL_OK;
  1221. if (pCallback == NULL)
  1222. {
  1223. /* Update the error code */
  1224. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  1225. return HAL_ERROR;
  1226. }
  1227. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1228. {
  1229. hfdcan->TT_TimeMarkCallback = pCallback;
  1230. }
  1231. else
  1232. {
  1233. /* Update the error code */
  1234. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  1235. /* Return error status */
  1236. status = HAL_ERROR;
  1237. }
  1238. return status;
  1239. }
  1240. /**
  1241. * @brief UnRegister the TT Time Mark FDCAN Callback
  1242. * TT Time Mark Callback is redirected to the weak HAL_FDCAN_TT_TimeMarkCallback() predefined callback
  1243. * @param hfdcan FDCAN handle
  1244. * @retval HAL status
  1245. */
  1246. HAL_StatusTypeDef HAL_FDCAN_UnRegisterTTTimeMarkCallback(FDCAN_HandleTypeDef *hfdcan)
  1247. {
  1248. HAL_StatusTypeDef status = HAL_OK;
  1249. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1250. {
  1251. hfdcan->TT_TimeMarkCallback = HAL_FDCAN_TT_TimeMarkCallback; /* Legacy weak TT_TimeMarkCallback */
  1252. }
  1253. else
  1254. {
  1255. /* Update the error code */
  1256. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  1257. /* Return error status */
  1258. status = HAL_ERROR;
  1259. }
  1260. return status;
  1261. }
  1262. /**
  1263. * @brief Register TT Stop Watch FDCAN Callback
  1264. * To be used instead of the weak HAL_FDCAN_TT_StopWatchCallback() predefined callback
  1265. * @param hfdcan FDCAN handle
  1266. * @param pCallback pointer to the TT Stop Watch Callback function
  1267. * @retval HAL status
  1268. */
  1269. HAL_StatusTypeDef HAL_FDCAN_RegisterTTStopWatchCallback(FDCAN_HandleTypeDef *hfdcan, pFDCAN_TT_StopWatchCallbackTypeDef pCallback)
  1270. {
  1271. HAL_StatusTypeDef status = HAL_OK;
  1272. if (pCallback == NULL)
  1273. {
  1274. /* Update the error code */
  1275. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  1276. return HAL_ERROR;
  1277. }
  1278. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1279. {
  1280. hfdcan->TT_StopWatchCallback = pCallback;
  1281. }
  1282. else
  1283. {
  1284. /* Update the error code */
  1285. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  1286. /* Return error status */
  1287. status = HAL_ERROR;
  1288. }
  1289. return status;
  1290. }
  1291. /**
  1292. * @brief UnRegister the TT Stop Watch FDCAN Callback
  1293. * TT Stop Watch Callback is redirected to the weak HAL_FDCAN_TT_StopWatchCallback() predefined callback
  1294. * @param hfdcan FDCAN handle
  1295. * @retval HAL status
  1296. */
  1297. HAL_StatusTypeDef HAL_FDCAN_UnRegisterTTStopWatchCallback(FDCAN_HandleTypeDef *hfdcan)
  1298. {
  1299. HAL_StatusTypeDef status = HAL_OK;
  1300. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1301. {
  1302. hfdcan->TT_StopWatchCallback = HAL_FDCAN_TT_StopWatchCallback; /* Legacy weak TT_StopWatchCallback */
  1303. }
  1304. else
  1305. {
  1306. /* Update the error code */
  1307. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  1308. /* Return error status */
  1309. status = HAL_ERROR;
  1310. }
  1311. return status;
  1312. }
  1313. /**
  1314. * @brief Register TT Global Time FDCAN Callback
  1315. * To be used instead of the weak HAL_FDCAN_TT_GlobalTimeCallback() predefined callback
  1316. * @param hfdcan FDCAN handle
  1317. * @param pCallback pointer to the TT Global Time Callback function
  1318. * @retval HAL status
  1319. */
  1320. HAL_StatusTypeDef HAL_FDCAN_RegisterTTGlobalTimeCallback(FDCAN_HandleTypeDef *hfdcan, pFDCAN_TT_GlobalTimeCallbackTypeDef pCallback)
  1321. {
  1322. HAL_StatusTypeDef status = HAL_OK;
  1323. if (pCallback == NULL)
  1324. {
  1325. /* Update the error code */
  1326. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  1327. return HAL_ERROR;
  1328. }
  1329. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1330. {
  1331. hfdcan->TT_GlobalTimeCallback = pCallback;
  1332. }
  1333. else
  1334. {
  1335. /* Update the error code */
  1336. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  1337. /* Return error status */
  1338. status = HAL_ERROR;
  1339. }
  1340. return status;
  1341. }
  1342. /**
  1343. * @brief UnRegister the TT Global Time FDCAN Callback
  1344. * TT Global Time Callback is redirected to the weak HAL_FDCAN_TT_GlobalTimeCallback() predefined callback
  1345. * @param hfdcan FDCAN handle
  1346. * @retval HAL status
  1347. */
  1348. HAL_StatusTypeDef HAL_FDCAN_UnRegisterTTGlobalTimeCallback(FDCAN_HandleTypeDef *hfdcan)
  1349. {
  1350. HAL_StatusTypeDef status = HAL_OK;
  1351. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1352. {
  1353. hfdcan->TT_GlobalTimeCallback = HAL_FDCAN_TT_GlobalTimeCallback; /* Legacy weak TT_GlobalTimeCallback */
  1354. }
  1355. else
  1356. {
  1357. /* Update the error code */
  1358. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_INVALID_CALLBACK;
  1359. /* Return error status */
  1360. status = HAL_ERROR;
  1361. }
  1362. return status;
  1363. }
  1364. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  1365. /**
  1366. * @}
  1367. */
  1368. /** @defgroup FDCAN_Exported_Functions_Group2 Configuration functions
  1369. * @brief FDCAN Configuration functions.
  1370. *
  1371. @verbatim
  1372. ==============================================================================
  1373. ##### Configuration functions #####
  1374. ==============================================================================
  1375. [..] This section provides functions allowing to:
  1376. (+) HAL_FDCAN_ConfigClockCalibration : Configure the FDCAN clock calibration unit
  1377. (+) HAL_FDCAN_GetClockCalibrationState : Get the clock calibration state
  1378. (+) HAL_FDCAN_ResetClockCalibrationState : Reset the clock calibration state
  1379. (+) HAL_FDCAN_GetClockCalibrationCounter : Get the clock calibration counters values
  1380. (+) HAL_FDCAN_ConfigFilter : Configure the FDCAN reception filters
  1381. (+) HAL_FDCAN_ConfigGlobalFilter : Configure the FDCAN global filter
  1382. (+) HAL_FDCAN_ConfigExtendedIdMask : Configure the extended ID mask
  1383. (+) HAL_FDCAN_ConfigRxFifoOverwrite : Configure the Rx FIFO operation mode
  1384. (+) HAL_FDCAN_ConfigFifoWatermark : Configure the FIFO watermark
  1385. (+) HAL_FDCAN_ConfigRamWatchdog : Configure the RAM watchdog
  1386. (+) HAL_FDCAN_ConfigTimestampCounter : Configure the timestamp counter
  1387. (+) HAL_FDCAN_EnableTimestampCounter : Enable the timestamp counter
  1388. (+) HAL_FDCAN_DisableTimestampCounter : Disable the timestamp counter
  1389. (+) HAL_FDCAN_GetTimestampCounter : Get the timestamp counter value
  1390. (+) HAL_FDCAN_ResetTimestampCounter : Reset the timestamp counter to zero
  1391. (+) HAL_FDCAN_ConfigTimeoutCounter : Configure the timeout counter
  1392. (+) HAL_FDCAN_EnableTimeoutCounter : Enable the timeout counter
  1393. (+) HAL_FDCAN_DisableTimeoutCounter : Disable the timeout counter
  1394. (+) HAL_FDCAN_GetTimeoutCounter : Get the timeout counter value
  1395. (+) HAL_FDCAN_ResetTimeoutCounter : Reset the timeout counter to its start value
  1396. (+) HAL_FDCAN_ConfigTxDelayCompensation : Configure the transmitter delay compensation
  1397. (+) HAL_FDCAN_EnableTxDelayCompensation : Enable the transmitter delay compensation
  1398. (+) HAL_FDCAN_DisableTxDelayCompensation : Disable the transmitter delay compensation
  1399. (+) HAL_FDCAN_EnableISOMode : Enable ISO 11898-1 protocol mode
  1400. (+) HAL_FDCAN_DisableISOMode : Disable ISO 11898-1 protocol mode
  1401. (+) HAL_FDCAN_EnableEdgeFiltering : Enable edge filtering during bus integration
  1402. (+) HAL_FDCAN_DisableEdgeFiltering : Disable edge filtering during bus integration
  1403. @endverbatim
  1404. * @{
  1405. */
  1406. /**
  1407. * @brief Configure the FDCAN clock calibration unit according to the specified
  1408. * parameters in the FDCAN_ClkCalUnitTypeDef structure.
  1409. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1410. * the configuration information for the specified FDCAN.
  1411. * @param sCcuConfig pointer to an FDCAN_ClkCalUnitTypeDef structure that
  1412. * contains the clock calibration information
  1413. * @retval HAL status
  1414. */
  1415. HAL_StatusTypeDef HAL_FDCAN_ConfigClockCalibration(FDCAN_HandleTypeDef *hfdcan, FDCAN_ClkCalUnitTypeDef *sCcuConfig)
  1416. {
  1417. /* Check function parameters */
  1418. assert_param(IS_FDCAN_CLOCK_CALIBRATION(sCcuConfig->ClockCalibration));
  1419. if (sCcuConfig->ClockCalibration == FDCAN_CLOCK_CALIBRATION_DISABLE)
  1420. {
  1421. assert_param(IS_FDCAN_CKDIV(sCcuConfig->ClockDivider));
  1422. }
  1423. else
  1424. {
  1425. assert_param(IS_FDCAN_MAX_VALUE(sCcuConfig->MinOscClkPeriods, 0xFFU));
  1426. assert_param(IS_FDCAN_CALIBRATION_FIELD_LENGTH(sCcuConfig->CalFieldLength));
  1427. assert_param(IS_FDCAN_MIN_VALUE(sCcuConfig->TimeQuantaPerBitTime, 4U));
  1428. assert_param(IS_FDCAN_MAX_VALUE(sCcuConfig->TimeQuantaPerBitTime, 0x25U));
  1429. assert_param(IS_FDCAN_MAX_VALUE(sCcuConfig->WatchdogStartValue, 0xFFFFU));
  1430. }
  1431. /* FDCAN1 should be initialized in order to use clock calibration */
  1432. if (hfdcan->Instance != FDCAN1)
  1433. {
  1434. /* Update error code */
  1435. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_PARAM;
  1436. return HAL_ERROR;
  1437. }
  1438. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1439. {
  1440. if (sCcuConfig->ClockCalibration == FDCAN_CLOCK_CALIBRATION_DISABLE)
  1441. {
  1442. /* Bypass clock calibration */
  1443. SET_BIT(FDCAN_CCU->CCFG, FDCANCCU_CCFG_BCC);
  1444. /* Configure clock divider */
  1445. MODIFY_REG(FDCAN_CCU->CCFG, FDCANCCU_CCFG_CDIV, sCcuConfig->ClockDivider);
  1446. }
  1447. else /* sCcuConfig->ClockCalibration == ENABLE */
  1448. {
  1449. /* Clock calibration unit generates time quanta clock */
  1450. CLEAR_BIT(FDCAN_CCU->CCFG, FDCANCCU_CCFG_BCC);
  1451. /* Configure clock calibration unit */
  1452. MODIFY_REG(FDCAN_CCU->CCFG,
  1453. (FDCANCCU_CCFG_TQBT | FDCANCCU_CCFG_CFL | FDCANCCU_CCFG_OCPM),
  1454. ((sCcuConfig->TimeQuantaPerBitTime << FDCANCCU_CCFG_TQBT_Pos) | sCcuConfig->CalFieldLength | (sCcuConfig->MinOscClkPeriods << FDCANCCU_CCFG_OCPM_Pos)));
  1455. /* Configure the start value of the calibration watchdog counter */
  1456. MODIFY_REG(FDCAN_CCU->CWD, FDCANCCU_CWD_WDC, sCcuConfig->WatchdogStartValue);
  1457. }
  1458. /* Return function status */
  1459. return HAL_OK;
  1460. }
  1461. else
  1462. {
  1463. /* Update error code */
  1464. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1465. return HAL_ERROR;
  1466. }
  1467. }
  1468. /**
  1469. * @brief Get the clock calibration state.
  1470. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1471. * the configuration information for the specified FDCAN.
  1472. * @retval State clock calibration state (can be a value of @arg FDCAN_calibration_state)
  1473. */
  1474. uint32_t HAL_FDCAN_GetClockCalibrationState(FDCAN_HandleTypeDef *hfdcan)
  1475. {
  1476. /* Prevent unused argument(s) compilation warning */
  1477. UNUSED(hfdcan);
  1478. return (FDCAN_CCU->CSTAT & FDCANCCU_CSTAT_CALS);
  1479. }
  1480. /**
  1481. * @brief Reset the clock calibration state.
  1482. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1483. * the configuration information for the specified FDCAN.
  1484. * @retval HAL status
  1485. */
  1486. HAL_StatusTypeDef HAL_FDCAN_ResetClockCalibrationState(FDCAN_HandleTypeDef *hfdcan)
  1487. {
  1488. /* FDCAN1 should be initialized in order to use clock calibration */
  1489. if (hfdcan->Instance != FDCAN1)
  1490. {
  1491. /* Update error code */
  1492. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_PARAM;
  1493. return HAL_ERROR;
  1494. }
  1495. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1496. {
  1497. /* Calibration software reset */
  1498. SET_BIT(FDCAN_CCU->CCFG, FDCANCCU_CCFG_SWR);
  1499. /* Return function status */
  1500. return HAL_OK;
  1501. }
  1502. else
  1503. {
  1504. /* Update error code */
  1505. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1506. return HAL_ERROR;
  1507. }
  1508. }
  1509. /**
  1510. * @brief Get the clock calibration counter value.
  1511. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1512. * the configuration information for the specified FDCAN.
  1513. * @param Counter clock calibration counter.
  1514. * This parameter can be a value of @arg FDCAN_calibration_counter.
  1515. * @retval Value clock calibration counter value
  1516. */
  1517. uint32_t HAL_FDCAN_GetClockCalibrationCounter(FDCAN_HandleTypeDef *hfdcan, uint32_t Counter)
  1518. {
  1519. /* Prevent unused argument(s) compilation warning */
  1520. UNUSED(hfdcan);
  1521. /* Check function parameters */
  1522. assert_param(IS_FDCAN_CALIBRATION_COUNTER(Counter));
  1523. if (Counter == FDCAN_CALIB_TIME_QUANTA_COUNTER)
  1524. {
  1525. return ((FDCAN_CCU->CSTAT & FDCANCCU_CSTAT_TQC) >> FDCANCCU_CSTAT_TQC_Pos);
  1526. }
  1527. else if (Counter == FDCAN_CALIB_CLOCK_PERIOD_COUNTER)
  1528. {
  1529. return (FDCAN_CCU->CSTAT & FDCANCCU_CSTAT_OCPC);
  1530. }
  1531. else /* Counter == FDCAN_CALIB_WATCHDOG_COUNTER */
  1532. {
  1533. return ((FDCAN_CCU->CWD & FDCANCCU_CWD_WDV) >> FDCANCCU_CWD_WDV_Pos);
  1534. }
  1535. }
  1536. /**
  1537. * @brief Configure the FDCAN reception filter according to the specified
  1538. * parameters in the FDCAN_FilterTypeDef structure.
  1539. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1540. * the configuration information for the specified FDCAN.
  1541. * @param sFilterConfig pointer to an FDCAN_FilterTypeDef structure that
  1542. * contains the filter configuration information
  1543. * @retval HAL status
  1544. */
  1545. HAL_StatusTypeDef HAL_FDCAN_ConfigFilter(FDCAN_HandleTypeDef *hfdcan, FDCAN_FilterTypeDef *sFilterConfig)
  1546. {
  1547. uint32_t FilterElementW1;
  1548. uint32_t FilterElementW2;
  1549. uint32_t *FilterAddress;
  1550. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  1551. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  1552. {
  1553. /* Check function parameters */
  1554. assert_param(IS_FDCAN_ID_TYPE(sFilterConfig->IdType));
  1555. assert_param(IS_FDCAN_FILTER_CFG(sFilterConfig->FilterConfig));
  1556. if (sFilterConfig->FilterConfig == FDCAN_FILTER_TO_RXBUFFER)
  1557. {
  1558. assert_param(IS_FDCAN_MAX_VALUE(sFilterConfig->RxBufferIndex, 63U));
  1559. assert_param(IS_FDCAN_MAX_VALUE(sFilterConfig->IsCalibrationMsg, 1U));
  1560. }
  1561. if (sFilterConfig->IdType == FDCAN_STANDARD_ID)
  1562. {
  1563. /* Check function parameters */
  1564. assert_param(IS_FDCAN_MAX_VALUE(sFilterConfig->FilterIndex, (hfdcan->Init.StdFiltersNbr - 1U)));
  1565. assert_param(IS_FDCAN_MAX_VALUE(sFilterConfig->FilterID1, 0x7FFU));
  1566. if (sFilterConfig->FilterConfig != FDCAN_FILTER_TO_RXBUFFER)
  1567. {
  1568. assert_param(IS_FDCAN_MAX_VALUE(sFilterConfig->FilterID2, 0x7FFU));
  1569. assert_param(IS_FDCAN_STD_FILTER_TYPE(sFilterConfig->FilterType));
  1570. }
  1571. /* Build filter element */
  1572. if (sFilterConfig->FilterConfig == FDCAN_FILTER_TO_RXBUFFER)
  1573. {
  1574. FilterElementW1 = ((FDCAN_FILTER_TO_RXBUFFER << 27U) |
  1575. (sFilterConfig->FilterID1 << 16U) |
  1576. (sFilterConfig->IsCalibrationMsg << 8U) |
  1577. sFilterConfig->RxBufferIndex);
  1578. }
  1579. else
  1580. {
  1581. FilterElementW1 = ((sFilterConfig->FilterType << 30U) |
  1582. (sFilterConfig->FilterConfig << 27U) |
  1583. (sFilterConfig->FilterID1 << 16U) |
  1584. sFilterConfig->FilterID2);
  1585. }
  1586. /* Calculate filter address */
  1587. FilterAddress = (uint32_t *)(hfdcan->msgRam.StandardFilterSA + (sFilterConfig->FilterIndex * 4U));
  1588. /* Write filter element to the message RAM */
  1589. *FilterAddress = FilterElementW1;
  1590. }
  1591. else /* sFilterConfig->IdType == FDCAN_EXTENDED_ID */
  1592. {
  1593. /* Check function parameters */
  1594. assert_param(IS_FDCAN_MAX_VALUE(sFilterConfig->FilterIndex, (hfdcan->Init.ExtFiltersNbr - 1U)));
  1595. assert_param(IS_FDCAN_MAX_VALUE(sFilterConfig->FilterID1, 0x1FFFFFFFU));
  1596. if (sFilterConfig->FilterConfig != FDCAN_FILTER_TO_RXBUFFER)
  1597. {
  1598. assert_param(IS_FDCAN_MAX_VALUE(sFilterConfig->FilterID2, 0x1FFFFFFFU));
  1599. assert_param(IS_FDCAN_EXT_FILTER_TYPE(sFilterConfig->FilterType));
  1600. }
  1601. /* Build first word of filter element */
  1602. FilterElementW1 = ((sFilterConfig->FilterConfig << 29U) | sFilterConfig->FilterID1);
  1603. /* Build second word of filter element */
  1604. if (sFilterConfig->FilterConfig == FDCAN_FILTER_TO_RXBUFFER)
  1605. {
  1606. FilterElementW2 = sFilterConfig->RxBufferIndex;
  1607. }
  1608. else
  1609. {
  1610. FilterElementW2 = ((sFilterConfig->FilterType << 30U) | sFilterConfig->FilterID2);
  1611. }
  1612. /* Calculate filter address */
  1613. FilterAddress = (uint32_t *)(hfdcan->msgRam.ExtendedFilterSA + (sFilterConfig->FilterIndex * 4U * 2U));
  1614. /* Write filter element to the message RAM */
  1615. *FilterAddress = FilterElementW1;
  1616. FilterAddress++;
  1617. *FilterAddress = FilterElementW2;
  1618. }
  1619. /* Return function status */
  1620. return HAL_OK;
  1621. }
  1622. else
  1623. {
  1624. /* Update error code */
  1625. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  1626. return HAL_ERROR;
  1627. }
  1628. }
  1629. /**
  1630. * @brief Configure the FDCAN global filter.
  1631. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1632. * the configuration information for the specified FDCAN.
  1633. * @param NonMatchingStd Defines how received messages with 11-bit IDs that
  1634. * do not match any element of the filter list are treated.
  1635. * This parameter can be a value of @arg FDCAN_Non_Matching_Frames.
  1636. * @param NonMatchingExt Defines how received messages with 29-bit IDs that
  1637. * do not match any element of the filter list are treated.
  1638. * This parameter can be a value of @arg FDCAN_Non_Matching_Frames.
  1639. * @param RejectRemoteStd Filter or reject all the remote 11-bit IDs frames.
  1640. * This parameter can be a value of @arg FDCAN_Reject_Remote_Frames.
  1641. * @param RejectRemoteExt Filter or reject all the remote 29-bit IDs frames.
  1642. * This parameter can be a value of @arg FDCAN_Reject_Remote_Frames.
  1643. * @retval HAL status
  1644. */
  1645. HAL_StatusTypeDef HAL_FDCAN_ConfigGlobalFilter(FDCAN_HandleTypeDef *hfdcan,
  1646. uint32_t NonMatchingStd,
  1647. uint32_t NonMatchingExt,
  1648. uint32_t RejectRemoteStd,
  1649. uint32_t RejectRemoteExt)
  1650. {
  1651. /* Check function parameters */
  1652. assert_param(IS_FDCAN_NON_MATCHING(NonMatchingStd));
  1653. assert_param(IS_FDCAN_NON_MATCHING(NonMatchingExt));
  1654. assert_param(IS_FDCAN_REJECT_REMOTE(RejectRemoteStd));
  1655. assert_param(IS_FDCAN_REJECT_REMOTE(RejectRemoteExt));
  1656. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1657. {
  1658. /* Configure global filter */
  1659. hfdcan->Instance->GFC = ((NonMatchingStd << FDCAN_GFC_ANFS_Pos) |
  1660. (NonMatchingExt << FDCAN_GFC_ANFE_Pos) |
  1661. (RejectRemoteStd << FDCAN_GFC_RRFS_Pos) |
  1662. (RejectRemoteExt << FDCAN_GFC_RRFE_Pos));
  1663. /* Return function status */
  1664. return HAL_OK;
  1665. }
  1666. else
  1667. {
  1668. /* Update error code */
  1669. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1670. return HAL_ERROR;
  1671. }
  1672. }
  1673. /**
  1674. * @brief Configure the extended ID mask.
  1675. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1676. * the configuration information for the specified FDCAN.
  1677. * @param Mask Extended ID Mask.
  1678. * This parameter must be a number between 0 and 0x1FFFFFFF
  1679. * @retval HAL status
  1680. */
  1681. HAL_StatusTypeDef HAL_FDCAN_ConfigExtendedIdMask(FDCAN_HandleTypeDef *hfdcan, uint32_t Mask)
  1682. {
  1683. /* Check function parameters */
  1684. assert_param(IS_FDCAN_MAX_VALUE(Mask, 0x1FFFFFFFU));
  1685. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1686. {
  1687. /* Configure the extended ID mask */
  1688. hfdcan->Instance->XIDAM = Mask;
  1689. /* Return function status */
  1690. return HAL_OK;
  1691. }
  1692. else
  1693. {
  1694. /* Update error code */
  1695. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1696. return HAL_ERROR;
  1697. }
  1698. }
  1699. /**
  1700. * @brief Configure the Rx FIFO operation mode.
  1701. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1702. * the configuration information for the specified FDCAN.
  1703. * @param RxFifo Rx FIFO.
  1704. * This parameter can be one of the following values:
  1705. * @arg FDCAN_RX_FIFO0: Rx FIFO 0
  1706. * @arg FDCAN_RX_FIFO1: Rx FIFO 1
  1707. * @param OperationMode operation mode.
  1708. * This parameter can be a value of @arg FDCAN_Rx_FIFO_operation_mode.
  1709. * @retval HAL status
  1710. */
  1711. HAL_StatusTypeDef HAL_FDCAN_ConfigRxFifoOverwrite(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo, uint32_t OperationMode)
  1712. {
  1713. /* Check function parameters */
  1714. assert_param(IS_FDCAN_RX_FIFO(RxFifo));
  1715. assert_param(IS_FDCAN_RX_FIFO_MODE(OperationMode));
  1716. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1717. {
  1718. if (RxFifo == FDCAN_RX_FIFO0)
  1719. {
  1720. /* Select FIFO 0 Operation Mode */
  1721. MODIFY_REG(hfdcan->Instance->RXF0C, FDCAN_RXF0C_F0OM, OperationMode);
  1722. }
  1723. else /* RxFifo == FDCAN_RX_FIFO1 */
  1724. {
  1725. /* Select FIFO 1 Operation Mode */
  1726. MODIFY_REG(hfdcan->Instance->RXF1C, FDCAN_RXF1C_F1OM, OperationMode);
  1727. }
  1728. /* Return function status */
  1729. return HAL_OK;
  1730. }
  1731. else
  1732. {
  1733. /* Update error code */
  1734. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1735. return HAL_ERROR;
  1736. }
  1737. }
  1738. /**
  1739. * @brief Configure the FIFO watermark.
  1740. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1741. * the configuration information for the specified FDCAN.
  1742. * @param FIFO select the FIFO to be configured.
  1743. * This parameter can be a value of @arg FDCAN_FIFO_watermark.
  1744. * @param Watermark level for FIFO watermark interrupt.
  1745. * This parameter must be a number between:
  1746. * - 0 and 32, if FIFO is FDCAN_CFG_TX_EVENT_FIFO
  1747. * - 0 and 64, if FIFO is FDCAN_CFG_RX_FIFO0 or FDCAN_CFG_RX_FIFO1
  1748. * @retval HAL status
  1749. */
  1750. HAL_StatusTypeDef HAL_FDCAN_ConfigFifoWatermark(FDCAN_HandleTypeDef *hfdcan, uint32_t FIFO, uint32_t Watermark)
  1751. {
  1752. /* Check function parameters */
  1753. assert_param(IS_FDCAN_FIFO_WATERMARK(FIFO));
  1754. if (FIFO == FDCAN_CFG_TX_EVENT_FIFO)
  1755. {
  1756. assert_param(IS_FDCAN_MAX_VALUE(Watermark, 32U));
  1757. }
  1758. else /* (FIFO == FDCAN_CFG_RX_FIFO0) || (FIFO == FDCAN_CFG_RX_FIFO1) */
  1759. {
  1760. assert_param(IS_FDCAN_MAX_VALUE(Watermark, 64U));
  1761. }
  1762. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1763. {
  1764. /* Set the level for FIFO watermark interrupt */
  1765. if (FIFO == FDCAN_CFG_TX_EVENT_FIFO)
  1766. {
  1767. MODIFY_REG(hfdcan->Instance->TXEFC, FDCAN_TXEFC_EFWM, (Watermark << FDCAN_TXEFC_EFWM_Pos));
  1768. }
  1769. else if (FIFO == FDCAN_CFG_RX_FIFO0)
  1770. {
  1771. MODIFY_REG(hfdcan->Instance->RXF0C, FDCAN_RXF0C_F0WM, (Watermark << FDCAN_RXF0C_F0WM_Pos));
  1772. }
  1773. else /* FIFO == FDCAN_CFG_RX_FIFO1 */
  1774. {
  1775. MODIFY_REG(hfdcan->Instance->RXF1C, FDCAN_RXF1C_F1WM, (Watermark << FDCAN_RXF1C_F1WM_Pos));
  1776. }
  1777. /* Return function status */
  1778. return HAL_OK;
  1779. }
  1780. else
  1781. {
  1782. /* Update error code */
  1783. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1784. return HAL_ERROR;
  1785. }
  1786. }
  1787. /**
  1788. * @brief Configure the RAM watchdog.
  1789. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1790. * the configuration information for the specified FDCAN.
  1791. * @param CounterStartValue Start value of the Message RAM Watchdog Counter,
  1792. * This parameter must be a number between 0x00 and 0xFF,
  1793. * with the reset value of 0x00 the counter is disabled.
  1794. * @retval HAL status
  1795. */
  1796. HAL_StatusTypeDef HAL_FDCAN_ConfigRamWatchdog(FDCAN_HandleTypeDef *hfdcan, uint32_t CounterStartValue)
  1797. {
  1798. /* Check function parameters */
  1799. assert_param(IS_FDCAN_MAX_VALUE(CounterStartValue, 0xFFU));
  1800. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1801. {
  1802. /* Configure the RAM watchdog counter start value */
  1803. MODIFY_REG(hfdcan->Instance->RWD, FDCAN_RWD_WDC, CounterStartValue);
  1804. /* Return function status */
  1805. return HAL_OK;
  1806. }
  1807. else
  1808. {
  1809. /* Update error code */
  1810. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1811. return HAL_ERROR;
  1812. }
  1813. }
  1814. /**
  1815. * @brief Configure the timestamp counter.
  1816. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1817. * the configuration information for the specified FDCAN.
  1818. * @param TimestampPrescaler Timestamp Counter Prescaler.
  1819. * This parameter can be a value of @arg FDCAN_Timestamp_Prescaler.
  1820. * @retval HAL status
  1821. */
  1822. HAL_StatusTypeDef HAL_FDCAN_ConfigTimestampCounter(FDCAN_HandleTypeDef *hfdcan, uint32_t TimestampPrescaler)
  1823. {
  1824. /* Check function parameters */
  1825. assert_param(IS_FDCAN_TIMESTAMP_PRESCALER(TimestampPrescaler));
  1826. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1827. {
  1828. /* Configure prescaler */
  1829. MODIFY_REG(hfdcan->Instance->TSCC, FDCAN_TSCC_TCP, TimestampPrescaler);
  1830. /* Return function status */
  1831. return HAL_OK;
  1832. }
  1833. else
  1834. {
  1835. /* Update error code */
  1836. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1837. return HAL_ERROR;
  1838. }
  1839. }
  1840. /**
  1841. * @brief Enable the timestamp counter.
  1842. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1843. * the configuration information for the specified FDCAN.
  1844. * @param TimestampOperation Timestamp counter operation.
  1845. * This parameter can be a value of @arg FDCAN_Timestamp.
  1846. * @retval HAL status
  1847. */
  1848. HAL_StatusTypeDef HAL_FDCAN_EnableTimestampCounter(FDCAN_HandleTypeDef *hfdcan, uint32_t TimestampOperation)
  1849. {
  1850. /* Check function parameters */
  1851. assert_param(IS_FDCAN_TIMESTAMP(TimestampOperation));
  1852. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1853. {
  1854. /* Enable timestamp counter */
  1855. MODIFY_REG(hfdcan->Instance->TSCC, FDCAN_TSCC_TSS, TimestampOperation);
  1856. /* Return function status */
  1857. return HAL_OK;
  1858. }
  1859. else
  1860. {
  1861. /* Update error code */
  1862. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1863. return HAL_ERROR;
  1864. }
  1865. }
  1866. /**
  1867. * @brief Disable the timestamp counter.
  1868. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1869. * the configuration information for the specified FDCAN.
  1870. * @retval HAL status
  1871. */
  1872. HAL_StatusTypeDef HAL_FDCAN_DisableTimestampCounter(FDCAN_HandleTypeDef *hfdcan)
  1873. {
  1874. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1875. {
  1876. /* Disable timestamp counter */
  1877. CLEAR_BIT(hfdcan->Instance->TSCC, FDCAN_TSCC_TSS);
  1878. /* Return function status */
  1879. return HAL_OK;
  1880. }
  1881. else
  1882. {
  1883. /* Update error code */
  1884. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1885. return HAL_ERROR;
  1886. }
  1887. }
  1888. /**
  1889. * @brief Get the timestamp counter value.
  1890. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1891. * the configuration information for the specified FDCAN.
  1892. * @retval Value Timestamp counter value
  1893. */
  1894. uint16_t HAL_FDCAN_GetTimestampCounter(FDCAN_HandleTypeDef *hfdcan)
  1895. {
  1896. return (uint16_t)(hfdcan->Instance->TSCV);
  1897. }
  1898. /**
  1899. * @brief Reset the timestamp counter to zero.
  1900. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1901. * the configuration information for the specified FDCAN.
  1902. * @retval HAL status
  1903. */
  1904. HAL_StatusTypeDef HAL_FDCAN_ResetTimestampCounter(FDCAN_HandleTypeDef *hfdcan)
  1905. {
  1906. if ((hfdcan->Instance->TSCC & FDCAN_TSCC_TSS) != FDCAN_TIMESTAMP_EXTERNAL)
  1907. {
  1908. /* Reset timestamp counter.
  1909. Actually any write operation to TSCV clears the counter */
  1910. CLEAR_REG(hfdcan->Instance->TSCV);
  1911. }
  1912. else
  1913. {
  1914. /* Update error code.
  1915. Unable to reset external counter */
  1916. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_SUPPORTED;
  1917. return HAL_ERROR;
  1918. }
  1919. /* Return function status */
  1920. return HAL_OK;
  1921. }
  1922. /**
  1923. * @brief Configure the timeout counter.
  1924. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1925. * the configuration information for the specified FDCAN.
  1926. * @param TimeoutOperation Timeout counter operation.
  1927. * This parameter can be a value of @arg FDCAN_Timeout_Operation.
  1928. * @param TimeoutPeriod Start value of the timeout down-counter.
  1929. * This parameter must be a number between 0x0000 and 0xFFFF
  1930. * @retval HAL status
  1931. */
  1932. HAL_StatusTypeDef HAL_FDCAN_ConfigTimeoutCounter(FDCAN_HandleTypeDef *hfdcan, uint32_t TimeoutOperation, uint32_t TimeoutPeriod)
  1933. {
  1934. /* Check function parameters */
  1935. assert_param(IS_FDCAN_TIMEOUT(TimeoutOperation));
  1936. assert_param(IS_FDCAN_MAX_VALUE(TimeoutPeriod, 0xFFFFU));
  1937. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1938. {
  1939. /* Select timeout operation and configure period */
  1940. MODIFY_REG(hfdcan->Instance->TOCC, (FDCAN_TOCC_TOS | FDCAN_TOCC_TOP), (TimeoutOperation | (TimeoutPeriod << FDCAN_TOCC_TOP_Pos)));
  1941. /* Return function status */
  1942. return HAL_OK;
  1943. }
  1944. else
  1945. {
  1946. /* Update error code */
  1947. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1948. return HAL_ERROR;
  1949. }
  1950. }
  1951. /**
  1952. * @brief Enable the timeout counter.
  1953. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1954. * the configuration information for the specified FDCAN.
  1955. * @retval HAL status
  1956. */
  1957. HAL_StatusTypeDef HAL_FDCAN_EnableTimeoutCounter(FDCAN_HandleTypeDef *hfdcan)
  1958. {
  1959. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1960. {
  1961. /* Enable timeout counter */
  1962. SET_BIT(hfdcan->Instance->TOCC, FDCAN_TOCC_ETOC);
  1963. /* Return function status */
  1964. return HAL_OK;
  1965. }
  1966. else
  1967. {
  1968. /* Update error code */
  1969. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1970. return HAL_ERROR;
  1971. }
  1972. }
  1973. /**
  1974. * @brief Disable the timeout counter.
  1975. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1976. * the configuration information for the specified FDCAN.
  1977. * @retval HAL status
  1978. */
  1979. HAL_StatusTypeDef HAL_FDCAN_DisableTimeoutCounter(FDCAN_HandleTypeDef *hfdcan)
  1980. {
  1981. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  1982. {
  1983. /* Disable timeout counter */
  1984. CLEAR_BIT(hfdcan->Instance->TOCC, FDCAN_TOCC_ETOC);
  1985. /* Return function status */
  1986. return HAL_OK;
  1987. }
  1988. else
  1989. {
  1990. /* Update error code */
  1991. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  1992. return HAL_ERROR;
  1993. }
  1994. }
  1995. /**
  1996. * @brief Get the timeout counter value.
  1997. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  1998. * the configuration information for the specified FDCAN.
  1999. * @retval Value Timeout counter value
  2000. */
  2001. uint16_t HAL_FDCAN_GetTimeoutCounter(FDCAN_HandleTypeDef *hfdcan)
  2002. {
  2003. return (uint16_t)(hfdcan->Instance->TOCV);
  2004. }
  2005. /**
  2006. * @brief Reset the timeout counter to its start value.
  2007. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2008. * the configuration information for the specified FDCAN.
  2009. * @retval HAL status
  2010. */
  2011. HAL_StatusTypeDef HAL_FDCAN_ResetTimeoutCounter(FDCAN_HandleTypeDef *hfdcan)
  2012. {
  2013. if ((hfdcan->Instance->TOCC & FDCAN_TOCC_TOS) == FDCAN_TIMEOUT_CONTINUOUS)
  2014. {
  2015. /* Reset timeout counter to start value */
  2016. CLEAR_REG(hfdcan->Instance->TOCV);
  2017. /* Return function status */
  2018. return HAL_OK;
  2019. }
  2020. else
  2021. {
  2022. /* Update error code.
  2023. Unable to reset counter: controlled only by FIFO empty state */
  2024. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_SUPPORTED;
  2025. return HAL_ERROR;
  2026. }
  2027. }
  2028. /**
  2029. * @brief Configure the transmitter delay compensation.
  2030. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2031. * the configuration information for the specified FDCAN.
  2032. * @param TdcOffset Transmitter Delay Compensation Offset.
  2033. * This parameter must be a number between 0x00 and 0x7F.
  2034. * @param TdcFilter Transmitter Delay Compensation Filter Window Length.
  2035. * This parameter must be a number between 0x00 and 0x7F.
  2036. * @retval HAL status
  2037. */
  2038. HAL_StatusTypeDef HAL_FDCAN_ConfigTxDelayCompensation(FDCAN_HandleTypeDef *hfdcan, uint32_t TdcOffset, uint32_t TdcFilter)
  2039. {
  2040. /* Check function parameters */
  2041. assert_param(IS_FDCAN_MAX_VALUE(TdcOffset, 0x7FU));
  2042. assert_param(IS_FDCAN_MAX_VALUE(TdcFilter, 0x7FU));
  2043. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  2044. {
  2045. /* Configure TDC offset and filter window */
  2046. hfdcan->Instance->TDCR = ((TdcFilter << FDCAN_TDCR_TDCF_Pos) | (TdcOffset << FDCAN_TDCR_TDCO_Pos));
  2047. /* Return function status */
  2048. return HAL_OK;
  2049. }
  2050. else
  2051. {
  2052. /* Update error code */
  2053. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  2054. return HAL_ERROR;
  2055. }
  2056. }
  2057. /**
  2058. * @brief Enable the transmitter delay compensation.
  2059. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2060. * the configuration information for the specified FDCAN.
  2061. * @retval HAL status
  2062. */
  2063. HAL_StatusTypeDef HAL_FDCAN_EnableTxDelayCompensation(FDCAN_HandleTypeDef *hfdcan)
  2064. {
  2065. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  2066. {
  2067. /* Enable transmitter delay compensation */
  2068. SET_BIT(hfdcan->Instance->DBTP, FDCAN_DBTP_TDC);
  2069. /* Return function status */
  2070. return HAL_OK;
  2071. }
  2072. else
  2073. {
  2074. /* Update error code */
  2075. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  2076. return HAL_ERROR;
  2077. }
  2078. }
  2079. /**
  2080. * @brief Disable the transmitter delay compensation.
  2081. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2082. * the configuration information for the specified FDCAN.
  2083. * @retval HAL status
  2084. */
  2085. HAL_StatusTypeDef HAL_FDCAN_DisableTxDelayCompensation(FDCAN_HandleTypeDef *hfdcan)
  2086. {
  2087. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  2088. {
  2089. /* Disable transmitter delay compensation */
  2090. CLEAR_BIT(hfdcan->Instance->DBTP, FDCAN_DBTP_TDC);
  2091. /* Return function status */
  2092. return HAL_OK;
  2093. }
  2094. else
  2095. {
  2096. /* Update error code */
  2097. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  2098. return HAL_ERROR;
  2099. }
  2100. }
  2101. /**
  2102. * @brief Enable ISO 11898-1 protocol mode.
  2103. * CAN FD frame format is according to ISO 11898-1 standard.
  2104. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2105. * the configuration information for the specified FDCAN.
  2106. * @retval HAL status
  2107. */
  2108. HAL_StatusTypeDef HAL_FDCAN_EnableISOMode(FDCAN_HandleTypeDef *hfdcan)
  2109. {
  2110. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  2111. {
  2112. /* Disable Non ISO protocol mode */
  2113. CLEAR_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_NISO);
  2114. /* Return function status */
  2115. return HAL_OK;
  2116. }
  2117. else
  2118. {
  2119. /* Update error code */
  2120. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  2121. return HAL_ERROR;
  2122. }
  2123. }
  2124. /**
  2125. * @brief Disable ISO 11898-1 protocol mode.
  2126. * CAN FD frame format is according to Bosch CAN FD specification V1.0.
  2127. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2128. * the configuration information for the specified FDCAN.
  2129. * @retval HAL status
  2130. */
  2131. HAL_StatusTypeDef HAL_FDCAN_DisableISOMode(FDCAN_HandleTypeDef *hfdcan)
  2132. {
  2133. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  2134. {
  2135. /* Enable Non ISO protocol mode */
  2136. SET_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_NISO);
  2137. /* Return function status */
  2138. return HAL_OK;
  2139. }
  2140. else
  2141. {
  2142. /* Update error code */
  2143. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  2144. return HAL_ERROR;
  2145. }
  2146. }
  2147. /**
  2148. * @brief Enable edge filtering during bus integration.
  2149. * Two consecutive dominant tq are required to detect an edge for hard synchronization.
  2150. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2151. * the configuration information for the specified FDCAN.
  2152. * @retval HAL status
  2153. */
  2154. HAL_StatusTypeDef HAL_FDCAN_EnableEdgeFiltering(FDCAN_HandleTypeDef *hfdcan)
  2155. {
  2156. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  2157. {
  2158. /* Enable edge filtering */
  2159. SET_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_EFBI);
  2160. /* Return function status */
  2161. return HAL_OK;
  2162. }
  2163. else
  2164. {
  2165. /* Update error code */
  2166. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  2167. return HAL_ERROR;
  2168. }
  2169. }
  2170. /**
  2171. * @brief Disable edge filtering during bus integration.
  2172. * One dominant tq is required to detect an edge for hard synchronization.
  2173. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2174. * the configuration information for the specified FDCAN.
  2175. * @retval HAL status
  2176. */
  2177. HAL_StatusTypeDef HAL_FDCAN_DisableEdgeFiltering(FDCAN_HandleTypeDef *hfdcan)
  2178. {
  2179. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  2180. {
  2181. /* Disable edge filtering */
  2182. CLEAR_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_EFBI);
  2183. /* Return function status */
  2184. return HAL_OK;
  2185. }
  2186. else
  2187. {
  2188. /* Update error code */
  2189. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  2190. return HAL_ERROR;
  2191. }
  2192. }
  2193. /**
  2194. * @}
  2195. */
  2196. /** @defgroup FDCAN_Exported_Functions_Group3 Control functions
  2197. * @brief Control functions
  2198. *
  2199. @verbatim
  2200. ==============================================================================
  2201. ##### Control functions #####
  2202. ==============================================================================
  2203. [..] This section provides functions allowing to:
  2204. (+) HAL_FDCAN_Start : Start the FDCAN module
  2205. (+) HAL_FDCAN_Stop : Stop the FDCAN module and enable access to configuration registers
  2206. (+) HAL_FDCAN_AddMessageToTxFifoQ : Add a message to the Tx FIFO/Queue and activate the corresponding transmission request
  2207. (+) HAL_FDCAN_AddMessageToTxBuffer : Add a message to a dedicated Tx buffer
  2208. (+) HAL_FDCAN_EnableTxBufferRequest : Enable transmission request
  2209. (+) HAL_FDCAN_GetLatestTxFifoQRequestBuffer : Get Tx buffer index of latest Tx FIFO/Queue request
  2210. (+) HAL_FDCAN_AbortTxRequest : Abort transmission request
  2211. (+) HAL_FDCAN_GetRxMessage : Get an FDCAN frame from the Rx Buffer/FIFO zone into the message RAM
  2212. (+) HAL_FDCAN_GetTxEvent : Get an FDCAN Tx event from the Tx Event FIFO zone into the message RAM
  2213. (+) HAL_FDCAN_GetHighPriorityMessageStatus : Get high priority message status
  2214. (+) HAL_FDCAN_GetProtocolStatus : Get protocol status
  2215. (+) HAL_FDCAN_GetErrorCounters : Get error counter values
  2216. (+) HAL_FDCAN_IsRxBufferMessageAvailable : Check if a new message is received in the selected Rx buffer
  2217. (+) HAL_FDCAN_IsTxBufferMessagePending : Check if a transmission request is pending on the selected Tx buffer
  2218. (+) HAL_FDCAN_GetRxFifoFillLevel : Return Rx FIFO fill level
  2219. (+) HAL_FDCAN_GetTxFifoFreeLevel : Return Tx FIFO free level
  2220. (+) HAL_FDCAN_IsRestrictedOperationMode : Check if the FDCAN peripheral entered Restricted Operation Mode
  2221. (+) HAL_FDCAN_ExitRestrictedOperationMode : Exit Restricted Operation Mode
  2222. @endverbatim
  2223. * @{
  2224. */
  2225. /**
  2226. * @brief Start the FDCAN module.
  2227. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2228. * the configuration information for the specified FDCAN.
  2229. * @retval HAL status
  2230. */
  2231. HAL_StatusTypeDef HAL_FDCAN_Start(FDCAN_HandleTypeDef *hfdcan)
  2232. {
  2233. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  2234. {
  2235. /* Change FDCAN peripheral state */
  2236. hfdcan->State = HAL_FDCAN_STATE_BUSY;
  2237. /* Request leave initialisation */
  2238. CLEAR_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_INIT);
  2239. /* Reset the FDCAN ErrorCode */
  2240. hfdcan->ErrorCode = HAL_FDCAN_ERROR_NONE;
  2241. /* Return function status */
  2242. return HAL_OK;
  2243. }
  2244. else
  2245. {
  2246. /* Update error code */
  2247. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  2248. return HAL_ERROR;
  2249. }
  2250. }
  2251. /**
  2252. * @brief Stop the FDCAN module and enable access to configuration registers.
  2253. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2254. * the configuration information for the specified FDCAN.
  2255. * @retval HAL status
  2256. */
  2257. HAL_StatusTypeDef HAL_FDCAN_Stop(FDCAN_HandleTypeDef *hfdcan)
  2258. {
  2259. uint32_t Counter = 0U;
  2260. if (hfdcan->State == HAL_FDCAN_STATE_BUSY)
  2261. {
  2262. /* Request initialisation */
  2263. SET_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_INIT);
  2264. /* Wait until the INIT bit into CCCR register is set */
  2265. while ((hfdcan->Instance->CCCR & FDCAN_CCCR_INIT) == 0U)
  2266. {
  2267. /* Check for the Timeout */
  2268. if (Counter > FDCAN_TIMEOUT_VALUE)
  2269. {
  2270. /* Update error code */
  2271. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  2272. /* Change FDCAN state */
  2273. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  2274. return HAL_ERROR;
  2275. }
  2276. /* Increment counter */
  2277. Counter++;
  2278. }
  2279. /* Reset counter */
  2280. Counter = 0U;
  2281. /* Exit from Sleep mode */
  2282. CLEAR_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_CSR);
  2283. /* Wait until FDCAN exits sleep mode */
  2284. while ((hfdcan->Instance->CCCR & FDCAN_CCCR_CSA) == FDCAN_CCCR_CSA)
  2285. {
  2286. /* Check for the Timeout */
  2287. if (Counter > FDCAN_TIMEOUT_VALUE)
  2288. {
  2289. /* Update error code */
  2290. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  2291. /* Change FDCAN state */
  2292. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  2293. return HAL_ERROR;
  2294. }
  2295. /* Increment counter */
  2296. Counter++;
  2297. }
  2298. /* Enable configuration change */
  2299. SET_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_CCE);
  2300. /* Reset Latest Tx FIFO/Queue Request Buffer Index */
  2301. hfdcan->LatestTxFifoQRequest = 0U;
  2302. /* Change FDCAN peripheral state */
  2303. hfdcan->State = HAL_FDCAN_STATE_READY;
  2304. /* Return function status */
  2305. return HAL_OK;
  2306. }
  2307. else
  2308. {
  2309. /* Update error code */
  2310. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_STARTED;
  2311. return HAL_ERROR;
  2312. }
  2313. }
  2314. /**
  2315. * @brief Add a message to the Tx FIFO/Queue and activate the corresponding transmission request
  2316. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2317. * the configuration information for the specified FDCAN.
  2318. * @param pTxHeader pointer to a FDCAN_TxHeaderTypeDef structure.
  2319. * @param pTxData pointer to a buffer containing the payload of the Tx frame.
  2320. * @retval HAL status
  2321. */
  2322. HAL_StatusTypeDef HAL_FDCAN_AddMessageToTxFifoQ(FDCAN_HandleTypeDef *hfdcan, FDCAN_TxHeaderTypeDef *pTxHeader, uint8_t *pTxData)
  2323. {
  2324. uint32_t PutIndex;
  2325. /* Check function parameters */
  2326. assert_param(IS_FDCAN_ID_TYPE(pTxHeader->IdType));
  2327. if (pTxHeader->IdType == FDCAN_STANDARD_ID)
  2328. {
  2329. assert_param(IS_FDCAN_MAX_VALUE(pTxHeader->Identifier, 0x7FFU));
  2330. }
  2331. else /* pTxHeader->IdType == FDCAN_EXTENDED_ID */
  2332. {
  2333. assert_param(IS_FDCAN_MAX_VALUE(pTxHeader->Identifier, 0x1FFFFFFFU));
  2334. }
  2335. assert_param(IS_FDCAN_FRAME_TYPE(pTxHeader->TxFrameType));
  2336. assert_param(IS_FDCAN_DLC(pTxHeader->DataLength));
  2337. assert_param(IS_FDCAN_ESI(pTxHeader->ErrorStateIndicator));
  2338. assert_param(IS_FDCAN_BRS(pTxHeader->BitRateSwitch));
  2339. assert_param(IS_FDCAN_FDF(pTxHeader->FDFormat));
  2340. assert_param(IS_FDCAN_EFC(pTxHeader->TxEventFifoControl));
  2341. assert_param(IS_FDCAN_MAX_VALUE(pTxHeader->MessageMarker, 0xFFU));
  2342. if (hfdcan->State == HAL_FDCAN_STATE_BUSY)
  2343. {
  2344. /* Check that the Tx FIFO/Queue has an allocated area into the RAM */
  2345. if ((hfdcan->Instance->TXBC & FDCAN_TXBC_TFQS) == 0U)
  2346. {
  2347. /* Update error code */
  2348. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_PARAM;
  2349. return HAL_ERROR;
  2350. }
  2351. /* Check that the Tx FIFO/Queue is not full */
  2352. if ((hfdcan->Instance->TXFQS & FDCAN_TXFQS_TFQF) != 0U)
  2353. {
  2354. /* Update error code */
  2355. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_FIFO_FULL;
  2356. return HAL_ERROR;
  2357. }
  2358. else
  2359. {
  2360. /* Retrieve the Tx FIFO PutIndex */
  2361. PutIndex = ((hfdcan->Instance->TXFQS & FDCAN_TXFQS_TFQPI) >> FDCAN_TXFQS_TFQPI_Pos);
  2362. /* Add the message to the Tx FIFO/Queue */
  2363. FDCAN_CopyMessageToRAM(hfdcan, pTxHeader, pTxData, PutIndex);
  2364. /* Activate the corresponding transmission request */
  2365. hfdcan->Instance->TXBAR = ((uint32_t)1 << PutIndex);
  2366. /* Store the Latest Tx FIFO/Queue Request Buffer Index */
  2367. hfdcan->LatestTxFifoQRequest = ((uint32_t)1 << PutIndex);
  2368. }
  2369. /* Return function status */
  2370. return HAL_OK;
  2371. }
  2372. else
  2373. {
  2374. /* Update error code */
  2375. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_STARTED;
  2376. return HAL_ERROR;
  2377. }
  2378. }
  2379. /**
  2380. * @brief Add a message to a dedicated Tx buffer
  2381. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2382. * the configuration information for the specified FDCAN.
  2383. * @param pTxHeader pointer to a FDCAN_TxHeaderTypeDef structure.
  2384. * @param pTxData pointer to a buffer containing the payload of the Tx frame.
  2385. * @param BufferIndex index of the buffer to be configured.
  2386. * This parameter can be a value of @arg FDCAN_Tx_location.
  2387. * @retval HAL status
  2388. */
  2389. HAL_StatusTypeDef HAL_FDCAN_AddMessageToTxBuffer(FDCAN_HandleTypeDef *hfdcan, FDCAN_TxHeaderTypeDef *pTxHeader, uint8_t *pTxData, uint32_t BufferIndex)
  2390. {
  2391. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  2392. /* Check function parameters */
  2393. assert_param(IS_FDCAN_ID_TYPE(pTxHeader->IdType));
  2394. if (pTxHeader->IdType == FDCAN_STANDARD_ID)
  2395. {
  2396. assert_param(IS_FDCAN_MAX_VALUE(pTxHeader->Identifier, 0x7FFU));
  2397. }
  2398. else /* pTxHeader->IdType == FDCAN_EXTENDED_ID */
  2399. {
  2400. assert_param(IS_FDCAN_MAX_VALUE(pTxHeader->Identifier, 0x1FFFFFFFU));
  2401. }
  2402. assert_param(IS_FDCAN_FRAME_TYPE(pTxHeader->TxFrameType));
  2403. assert_param(IS_FDCAN_DLC(pTxHeader->DataLength));
  2404. assert_param(IS_FDCAN_ESI(pTxHeader->ErrorStateIndicator));
  2405. assert_param(IS_FDCAN_BRS(pTxHeader->BitRateSwitch));
  2406. assert_param(IS_FDCAN_FDF(pTxHeader->FDFormat));
  2407. assert_param(IS_FDCAN_EFC(pTxHeader->TxEventFifoControl));
  2408. assert_param(IS_FDCAN_MAX_VALUE(pTxHeader->MessageMarker, 0xFFU));
  2409. assert_param(IS_FDCAN_TX_LOCATION(BufferIndex));
  2410. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  2411. {
  2412. /* Check that the selected buffer has an allocated area into the RAM */
  2413. if (POSITION_VAL(BufferIndex) >= ((hfdcan->Instance->TXBC & FDCAN_TXBC_NDTB) >> FDCAN_TXBC_NDTB_Pos))
  2414. {
  2415. /* Update error code */
  2416. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_PARAM;
  2417. return HAL_ERROR;
  2418. }
  2419. /* Check that there is no transmittion request pending for the selected buffer */
  2420. if ((hfdcan->Instance->TXBRP & BufferIndex) != 0U)
  2421. {
  2422. /* Update error code */
  2423. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_PENDING;
  2424. return HAL_ERROR;
  2425. }
  2426. else
  2427. {
  2428. /* Add the message to the Tx buffer */
  2429. FDCAN_CopyMessageToRAM(hfdcan, pTxHeader, pTxData, POSITION_VAL(BufferIndex));
  2430. }
  2431. /* Return function status */
  2432. return HAL_OK;
  2433. }
  2434. else
  2435. {
  2436. /* Update error code */
  2437. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  2438. return HAL_ERROR;
  2439. }
  2440. }
  2441. /**
  2442. * @brief Enable transmission request.
  2443. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2444. * the configuration information for the specified FDCAN.
  2445. * @param BufferIndex buffer index.
  2446. * This parameter can be any combination of @arg FDCAN_Tx_location.
  2447. * @retval HAL status
  2448. */
  2449. HAL_StatusTypeDef HAL_FDCAN_EnableTxBufferRequest(FDCAN_HandleTypeDef *hfdcan, uint32_t BufferIndex)
  2450. {
  2451. if (hfdcan->State == HAL_FDCAN_STATE_BUSY)
  2452. {
  2453. /* Add transmission request */
  2454. hfdcan->Instance->TXBAR = BufferIndex;
  2455. /* Return function status */
  2456. return HAL_OK;
  2457. }
  2458. else
  2459. {
  2460. /* Update error code */
  2461. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_STARTED;
  2462. return HAL_ERROR;
  2463. }
  2464. }
  2465. /**
  2466. * @brief Get Tx buffer index of latest Tx FIFO/Queue request
  2467. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2468. * the configuration information for the specified FDCAN.
  2469. * @retval Tx buffer index of last Tx FIFO/Queue request
  2470. * - Any value of @arg FDCAN_Tx_location if Tx request has been submitted.
  2471. * - 0 if no Tx FIFO/Queue request have been submitted.
  2472. */
  2473. uint32_t HAL_FDCAN_GetLatestTxFifoQRequestBuffer(FDCAN_HandleTypeDef *hfdcan)
  2474. {
  2475. /* Return Last Tx FIFO/Queue Request Buffer */
  2476. return hfdcan->LatestTxFifoQRequest;
  2477. }
  2478. /**
  2479. * @brief Abort transmission request
  2480. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2481. * the configuration information for the specified FDCAN.
  2482. * @param BufferIndex buffer index.
  2483. * This parameter can be any combination of @arg FDCAN_Tx_location.
  2484. * @retval HAL status
  2485. */
  2486. HAL_StatusTypeDef HAL_FDCAN_AbortTxRequest(FDCAN_HandleTypeDef *hfdcan, uint32_t BufferIndex)
  2487. {
  2488. if (hfdcan->State == HAL_FDCAN_STATE_BUSY)
  2489. {
  2490. /* Add cancellation request */
  2491. hfdcan->Instance->TXBCR = BufferIndex;
  2492. /* Return function status */
  2493. return HAL_OK;
  2494. }
  2495. else
  2496. {
  2497. /* Update error code */
  2498. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_STARTED;
  2499. return HAL_ERROR;
  2500. }
  2501. }
  2502. /**
  2503. * @brief Get an FDCAN frame from the Rx Buffer/FIFO zone into the message RAM.
  2504. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2505. * the configuration information for the specified FDCAN.
  2506. * @param RxLocation Location of the received message to be read.
  2507. * This parameter can be a value of @arg FDCAN_Rx_location.
  2508. * @param pRxHeader pointer to a FDCAN_RxHeaderTypeDef structure.
  2509. * @param pRxData pointer to a buffer where the payload of the Rx frame will be stored.
  2510. * @retval HAL status
  2511. */
  2512. HAL_StatusTypeDef HAL_FDCAN_GetRxMessage(FDCAN_HandleTypeDef *hfdcan, uint32_t RxLocation, FDCAN_RxHeaderTypeDef *pRxHeader, uint8_t *pRxData)
  2513. {
  2514. uint32_t *RxAddress;
  2515. uint8_t *pData;
  2516. uint32_t ByteCounter;
  2517. uint32_t GetIndex = 0;
  2518. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  2519. if (state == HAL_FDCAN_STATE_BUSY)
  2520. {
  2521. if (RxLocation == FDCAN_RX_FIFO0) /* Rx element is assigned to the Rx FIFO 0 */
  2522. {
  2523. /* Check that the Rx FIFO 0 has an allocated area into the RAM */
  2524. if ((hfdcan->Instance->RXF0C & FDCAN_RXF0C_F0S) == 0U)
  2525. {
  2526. /* Update error code */
  2527. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_PARAM;
  2528. return HAL_ERROR;
  2529. }
  2530. /* Check that the Rx FIFO 0 is not empty */
  2531. if ((hfdcan->Instance->RXF0S & FDCAN_RXF0S_F0FL) == 0U)
  2532. {
  2533. /* Update error code */
  2534. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_FIFO_EMPTY;
  2535. return HAL_ERROR;
  2536. }
  2537. else
  2538. {
  2539. /* Calculate Rx FIFO 0 element address */
  2540. GetIndex = ((hfdcan->Instance->RXF0S & FDCAN_RXF0S_F0GI) >> FDCAN_RXF0S_F0GI_Pos);
  2541. RxAddress = (uint32_t *)(hfdcan->msgRam.RxFIFO0SA + (GetIndex * hfdcan->Init.RxFifo0ElmtSize * 4U));
  2542. }
  2543. }
  2544. else if (RxLocation == FDCAN_RX_FIFO1) /* Rx element is assigned to the Rx FIFO 1 */
  2545. {
  2546. /* Check that the Rx FIFO 1 has an allocated area into the RAM */
  2547. if ((hfdcan->Instance->RXF1C & FDCAN_RXF1C_F1S) == 0U)
  2548. {
  2549. /* Update error code */
  2550. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_PARAM;
  2551. return HAL_ERROR;
  2552. }
  2553. /* Check that the Rx FIFO 0 is not empty */
  2554. if ((hfdcan->Instance->RXF1S & FDCAN_RXF1S_F1FL) == 0U)
  2555. {
  2556. /* Update error code */
  2557. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_FIFO_EMPTY;
  2558. return HAL_ERROR;
  2559. }
  2560. else
  2561. {
  2562. /* Calculate Rx FIFO 1 element address */
  2563. GetIndex = ((hfdcan->Instance->RXF1S & FDCAN_RXF1S_F1GI) >> FDCAN_RXF1S_F1GI_Pos);
  2564. RxAddress = (uint32_t *)(hfdcan->msgRam.RxFIFO1SA + (GetIndex * hfdcan->Init.RxFifo1ElmtSize * 4U));
  2565. }
  2566. }
  2567. else /* Rx element is assigned to a dedicated Rx buffer */
  2568. {
  2569. /* Check that the selected buffer has an allocated area into the RAM */
  2570. if (RxLocation >= hfdcan->Init.RxBuffersNbr)
  2571. {
  2572. /* Update error code */
  2573. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_PARAM;
  2574. return HAL_ERROR;
  2575. }
  2576. else
  2577. {
  2578. /* Calculate Rx buffer address */
  2579. RxAddress = (uint32_t *)(hfdcan->msgRam.RxBufferSA + (RxLocation * hfdcan->Init.RxBufferSize * 4U));
  2580. }
  2581. }
  2582. /* Retrieve IdType */
  2583. pRxHeader->IdType = *RxAddress & FDCAN_ELEMENT_MASK_XTD;
  2584. /* Retrieve Identifier */
  2585. if (pRxHeader->IdType == FDCAN_STANDARD_ID) /* Standard ID element */
  2586. {
  2587. pRxHeader->Identifier = ((*RxAddress & FDCAN_ELEMENT_MASK_STDID) >> 18);
  2588. }
  2589. else /* Extended ID element */
  2590. {
  2591. pRxHeader->Identifier = (*RxAddress & FDCAN_ELEMENT_MASK_EXTID);
  2592. }
  2593. /* Retrieve RxFrameType */
  2594. pRxHeader->RxFrameType = (*RxAddress & FDCAN_ELEMENT_MASK_RTR);
  2595. /* Retrieve ErrorStateIndicator */
  2596. pRxHeader->ErrorStateIndicator = (*RxAddress & FDCAN_ELEMENT_MASK_ESI);
  2597. /* Increment RxAddress pointer to second word of Rx FIFO element */
  2598. RxAddress++;
  2599. /* Retrieve RxTimestamp */
  2600. pRxHeader->RxTimestamp = (*RxAddress & FDCAN_ELEMENT_MASK_TS);
  2601. /* Retrieve DataLength */
  2602. pRxHeader->DataLength = (*RxAddress & FDCAN_ELEMENT_MASK_DLC);
  2603. /* Retrieve BitRateSwitch */
  2604. pRxHeader->BitRateSwitch = (*RxAddress & FDCAN_ELEMENT_MASK_BRS);
  2605. /* Retrieve FDFormat */
  2606. pRxHeader->FDFormat = (*RxAddress & FDCAN_ELEMENT_MASK_FDF);
  2607. /* Retrieve FilterIndex */
  2608. pRxHeader->FilterIndex = ((*RxAddress & FDCAN_ELEMENT_MASK_FIDX) >> 24);
  2609. /* Retrieve NonMatchingFrame */
  2610. pRxHeader->IsFilterMatchingFrame = ((*RxAddress & FDCAN_ELEMENT_MASK_ANMF) >> 31);
  2611. /* Increment RxAddress pointer to payload of Rx FIFO element */
  2612. RxAddress++;
  2613. /* Retrieve Rx payload */
  2614. pData = (uint8_t *)RxAddress;
  2615. for (ByteCounter = 0; ByteCounter < DLCtoBytes[pRxHeader->DataLength >> 16]; ByteCounter++)
  2616. {
  2617. pRxData[ByteCounter] = pData[ByteCounter];
  2618. }
  2619. if (RxLocation == FDCAN_RX_FIFO0) /* Rx element is assigned to the Rx FIFO 0 */
  2620. {
  2621. /* Acknowledge the Rx FIFO 0 that the oldest element is read so that it increments the GetIndex */
  2622. hfdcan->Instance->RXF0A = GetIndex;
  2623. }
  2624. else if (RxLocation == FDCAN_RX_FIFO1) /* Rx element is assigned to the Rx FIFO 1 */
  2625. {
  2626. /* Acknowledge the Rx FIFO 1 that the oldest element is read so that it increments the GetIndex */
  2627. hfdcan->Instance->RXF1A = GetIndex;
  2628. }
  2629. else /* Rx element is assigned to a dedicated Rx buffer */
  2630. {
  2631. /* Clear the New Data flag of the current Rx buffer */
  2632. if (RxLocation < FDCAN_RX_BUFFER32)
  2633. {
  2634. hfdcan->Instance->NDAT1 = ((uint32_t)1 << RxLocation);
  2635. }
  2636. else /* FDCAN_RX_BUFFER32 <= RxLocation <= FDCAN_RX_BUFFER63 */
  2637. {
  2638. hfdcan->Instance->NDAT2 = ((uint32_t)1 << (RxLocation & 0x1FU));
  2639. }
  2640. }
  2641. /* Return function status */
  2642. return HAL_OK;
  2643. }
  2644. else
  2645. {
  2646. /* Update error code */
  2647. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_STARTED;
  2648. return HAL_ERROR;
  2649. }
  2650. }
  2651. /**
  2652. * @brief Get an FDCAN Tx event from the Tx Event FIFO zone into the message RAM.
  2653. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2654. * the configuration information for the specified FDCAN.
  2655. * @param pTxEvent pointer to a FDCAN_TxEventFifoTypeDef structure.
  2656. * @retval HAL status
  2657. */
  2658. HAL_StatusTypeDef HAL_FDCAN_GetTxEvent(FDCAN_HandleTypeDef *hfdcan, FDCAN_TxEventFifoTypeDef *pTxEvent)
  2659. {
  2660. uint32_t *TxEventAddress;
  2661. uint32_t GetIndex;
  2662. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  2663. /* Check function parameters */
  2664. assert_param(IS_FDCAN_MIN_VALUE(hfdcan->Init.TxEventsNbr, 1U));
  2665. if (state == HAL_FDCAN_STATE_BUSY)
  2666. {
  2667. /* Check that the Tx Event FIFO has an allocated area into the RAM */
  2668. if ((hfdcan->Instance->TXEFC & FDCAN_TXEFC_EFS) == 0U)
  2669. {
  2670. /* Update error code */
  2671. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_PARAM;
  2672. return HAL_ERROR;
  2673. }
  2674. /* Check that the Tx event FIFO is not empty */
  2675. if ((hfdcan->Instance->TXEFS & FDCAN_TXEFS_EFFL) == 0U)
  2676. {
  2677. /* Update error code */
  2678. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_FIFO_EMPTY;
  2679. return HAL_ERROR;
  2680. }
  2681. /* Calculate Tx event FIFO element address */
  2682. GetIndex = ((hfdcan->Instance->TXEFS & FDCAN_TXEFS_EFGI) >> FDCAN_TXEFS_EFGI_Pos);
  2683. TxEventAddress = (uint32_t *)(hfdcan->msgRam.TxEventFIFOSA + (GetIndex * 2U * 4U));
  2684. /* Retrieve IdType */
  2685. pTxEvent->IdType = *TxEventAddress & FDCAN_ELEMENT_MASK_XTD;
  2686. /* Retrieve Identifier */
  2687. if (pTxEvent->IdType == FDCAN_STANDARD_ID) /* Standard ID element */
  2688. {
  2689. pTxEvent->Identifier = ((*TxEventAddress & FDCAN_ELEMENT_MASK_STDID) >> 18U);
  2690. }
  2691. else /* Extended ID element */
  2692. {
  2693. pTxEvent->Identifier = (*TxEventAddress & FDCAN_ELEMENT_MASK_EXTID);
  2694. }
  2695. /* Retrieve RxFrameType */
  2696. pTxEvent->TxFrameType = (*TxEventAddress & FDCAN_ELEMENT_MASK_RTR);
  2697. /* Retrieve ErrorStateIndicator */
  2698. pTxEvent->ErrorStateIndicator = (*TxEventAddress & FDCAN_ELEMENT_MASK_ESI);
  2699. /* Increment TxEventAddress pointer to second word of Tx Event FIFO element */
  2700. TxEventAddress++;
  2701. /* Retrieve RxTimestamp */
  2702. pTxEvent->TxTimestamp = (*TxEventAddress & FDCAN_ELEMENT_MASK_TS);
  2703. /* Retrieve DataLength */
  2704. pTxEvent->DataLength = (*TxEventAddress & FDCAN_ELEMENT_MASK_DLC);
  2705. /* Retrieve BitRateSwitch */
  2706. pTxEvent->BitRateSwitch = (*TxEventAddress & FDCAN_ELEMENT_MASK_BRS);
  2707. /* Retrieve FDFormat */
  2708. pTxEvent->FDFormat = (*TxEventAddress & FDCAN_ELEMENT_MASK_FDF);
  2709. /* Retrieve EventType */
  2710. pTxEvent->EventType = (*TxEventAddress & FDCAN_ELEMENT_MASK_ET);
  2711. /* Retrieve MessageMarker */
  2712. pTxEvent->MessageMarker = ((*TxEventAddress & FDCAN_ELEMENT_MASK_MM) >> 24);
  2713. /* Acknowledge the Tx Event FIFO that the oldest element is read so that it increments the GetIndex */
  2714. hfdcan->Instance->TXEFA = GetIndex;
  2715. /* Return function status */
  2716. return HAL_OK;
  2717. }
  2718. else
  2719. {
  2720. /* Update error code */
  2721. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_STARTED;
  2722. return HAL_ERROR;
  2723. }
  2724. }
  2725. /**
  2726. * @brief Get high priority message status.
  2727. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2728. * the configuration information for the specified FDCAN.
  2729. * @param HpMsgStatus pointer to an FDCAN_HpMsgStatusTypeDef structure.
  2730. * @retval HAL status
  2731. */
  2732. HAL_StatusTypeDef HAL_FDCAN_GetHighPriorityMessageStatus(FDCAN_HandleTypeDef *hfdcan, FDCAN_HpMsgStatusTypeDef *HpMsgStatus)
  2733. {
  2734. HpMsgStatus->FilterList = ((hfdcan->Instance->HPMS & FDCAN_HPMS_FLST) >> FDCAN_HPMS_FLST_Pos);
  2735. HpMsgStatus->FilterIndex = ((hfdcan->Instance->HPMS & FDCAN_HPMS_FIDX) >> FDCAN_HPMS_FIDX_Pos);
  2736. HpMsgStatus->MessageStorage = (hfdcan->Instance->HPMS & FDCAN_HPMS_MSI);
  2737. HpMsgStatus->MessageIndex = (hfdcan->Instance->HPMS & FDCAN_HPMS_BIDX);
  2738. /* Return function status */
  2739. return HAL_OK;
  2740. }
  2741. /**
  2742. * @brief Get protocol status.
  2743. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2744. * the configuration information for the specified FDCAN.
  2745. * @param ProtocolStatus pointer to an FDCAN_ProtocolStatusTypeDef structure.
  2746. * @retval HAL status
  2747. */
  2748. HAL_StatusTypeDef HAL_FDCAN_GetProtocolStatus(FDCAN_HandleTypeDef *hfdcan, FDCAN_ProtocolStatusTypeDef *ProtocolStatus)
  2749. {
  2750. uint32_t StatusReg;
  2751. /* Read the protocol status register */
  2752. StatusReg = READ_REG(hfdcan->Instance->PSR);
  2753. /* Fill the protocol status structure */
  2754. ProtocolStatus->LastErrorCode = (StatusReg & FDCAN_PSR_LEC);
  2755. ProtocolStatus->DataLastErrorCode = ((StatusReg & FDCAN_PSR_DLEC) >> FDCAN_PSR_DLEC_Pos);
  2756. ProtocolStatus->Activity = (StatusReg & FDCAN_PSR_ACT);
  2757. ProtocolStatus->ErrorPassive = ((StatusReg & FDCAN_PSR_EP) >> FDCAN_PSR_EP_Pos);
  2758. ProtocolStatus->Warning = ((StatusReg & FDCAN_PSR_EW) >> FDCAN_PSR_EW_Pos);
  2759. ProtocolStatus->BusOff = ((StatusReg & FDCAN_PSR_BO) >> FDCAN_PSR_BO_Pos);
  2760. ProtocolStatus->RxESIflag = ((StatusReg & FDCAN_PSR_RESI) >> FDCAN_PSR_RESI_Pos);
  2761. ProtocolStatus->RxBRSflag = ((StatusReg & FDCAN_PSR_RBRS) >> FDCAN_PSR_RBRS_Pos);
  2762. ProtocolStatus->RxFDFflag = ((StatusReg & FDCAN_PSR_REDL) >> FDCAN_PSR_REDL_Pos);
  2763. ProtocolStatus->ProtocolException = ((StatusReg & FDCAN_PSR_PXE) >> FDCAN_PSR_PXE_Pos);
  2764. ProtocolStatus->TDCvalue = ((StatusReg & FDCAN_PSR_TDCV) >> FDCAN_PSR_TDCV_Pos);
  2765. /* Return function status */
  2766. return HAL_OK;
  2767. }
  2768. /**
  2769. * @brief Get error counter values.
  2770. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2771. * the configuration information for the specified FDCAN.
  2772. * @param ErrorCounters pointer to an FDCAN_ErrorCountersTypeDef structure.
  2773. * @retval HAL status
  2774. */
  2775. HAL_StatusTypeDef HAL_FDCAN_GetErrorCounters(FDCAN_HandleTypeDef *hfdcan, FDCAN_ErrorCountersTypeDef *ErrorCounters)
  2776. {
  2777. uint32_t CountersReg;
  2778. /* Read the error counters register */
  2779. CountersReg = READ_REG(hfdcan->Instance->ECR);
  2780. /* Fill the error counters structure */
  2781. ErrorCounters->TxErrorCnt = ((CountersReg & FDCAN_ECR_TEC) >> FDCAN_ECR_TEC_Pos);
  2782. ErrorCounters->RxErrorCnt = ((CountersReg & FDCAN_ECR_REC) >> FDCAN_ECR_REC_Pos);
  2783. ErrorCounters->RxErrorPassive = ((CountersReg & FDCAN_ECR_RP) >> FDCAN_ECR_RP_Pos);
  2784. ErrorCounters->ErrorLogging = ((CountersReg & FDCAN_ECR_CEL) >> FDCAN_ECR_CEL_Pos);
  2785. /* Return function status */
  2786. return HAL_OK;
  2787. }
  2788. /**
  2789. * @brief Check if a new message is received in the selected Rx buffer.
  2790. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2791. * the configuration information for the specified FDCAN.
  2792. * @param RxBufferIndex Rx buffer index.
  2793. * This parameter must be a number between 0 and 63.
  2794. * @retval Status
  2795. * - 0 : No new message on RxBufferIndex.
  2796. * - 1 : New message received on RxBufferIndex.
  2797. */
  2798. uint32_t HAL_FDCAN_IsRxBufferMessageAvailable(FDCAN_HandleTypeDef *hfdcan, uint32_t RxBufferIndex)
  2799. {
  2800. /* Check function parameters */
  2801. assert_param(IS_FDCAN_MAX_VALUE(RxBufferIndex, 63U));
  2802. uint32_t NewData1 = hfdcan->Instance->NDAT1;
  2803. uint32_t NewData2 = hfdcan->Instance->NDAT2;
  2804. /* Check new message reception on the selected buffer */
  2805. if (((RxBufferIndex < 32U) && ((NewData1 & (uint32_t)((uint32_t)1 << RxBufferIndex)) == 0U)) ||
  2806. ((RxBufferIndex >= 32U) && ((NewData2 & (uint32_t)((uint32_t)1 << (RxBufferIndex & 0x1FU))) == 0U)))
  2807. {
  2808. return 0;
  2809. }
  2810. /* Clear the New Data flag of the current Rx buffer */
  2811. if (RxBufferIndex < 32U)
  2812. {
  2813. hfdcan->Instance->NDAT1 = ((uint32_t)1 << RxBufferIndex);
  2814. }
  2815. else /* 32 <= RxBufferIndex <= 63 */
  2816. {
  2817. hfdcan->Instance->NDAT2 = ((uint32_t)1 << (RxBufferIndex & 0x1FU));
  2818. }
  2819. return 1;
  2820. }
  2821. /**
  2822. * @brief Check if a transmission request is pending on the selected Tx buffer.
  2823. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2824. * the configuration information for the specified FDCAN.
  2825. * @param TxBufferIndex Tx buffer index.
  2826. * This parameter can be any combination of @arg FDCAN_Tx_location.
  2827. * @retval Status
  2828. * - 0 : No pending transmission request on TxBufferIndex.
  2829. * - 1 : Pending transmission request on TxBufferIndex.
  2830. */
  2831. uint32_t HAL_FDCAN_IsTxBufferMessagePending(FDCAN_HandleTypeDef *hfdcan, uint32_t TxBufferIndex)
  2832. {
  2833. /* Check pending transmittion request on the selected buffer */
  2834. if ((hfdcan->Instance->TXBRP & TxBufferIndex) == 0U)
  2835. {
  2836. return 0;
  2837. }
  2838. return 1;
  2839. }
  2840. /**
  2841. * @brief Return Rx FIFO fill level.
  2842. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2843. * the configuration information for the specified FDCAN.
  2844. * @param RxFifo Rx FIFO.
  2845. * This parameter can be one of the following values:
  2846. * @arg FDCAN_RX_FIFO0: Rx FIFO 0
  2847. * @arg FDCAN_RX_FIFO1: Rx FIFO 1
  2848. * @retval Level Rx FIFO fill level.
  2849. */
  2850. uint32_t HAL_FDCAN_GetRxFifoFillLevel(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo)
  2851. {
  2852. uint32_t FillLevel;
  2853. /* Check function parameters */
  2854. assert_param(IS_FDCAN_RX_FIFO(RxFifo));
  2855. if (RxFifo == FDCAN_RX_FIFO0)
  2856. {
  2857. FillLevel = hfdcan->Instance->RXF0S & FDCAN_RXF0S_F0FL;
  2858. }
  2859. else /* RxFifo == FDCAN_RX_FIFO1 */
  2860. {
  2861. FillLevel = hfdcan->Instance->RXF1S & FDCAN_RXF1S_F1FL;
  2862. }
  2863. /* Return Rx FIFO fill level */
  2864. return FillLevel;
  2865. }
  2866. /**
  2867. * @brief Return Tx FIFO free level: number of consecutive free Tx FIFO
  2868. * elements starting from Tx FIFO GetIndex.
  2869. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2870. * the configuration information for the specified FDCAN.
  2871. * @retval Level Tx FIFO free level.
  2872. */
  2873. uint32_t HAL_FDCAN_GetTxFifoFreeLevel(FDCAN_HandleTypeDef *hfdcan)
  2874. {
  2875. uint32_t FreeLevel;
  2876. FreeLevel = hfdcan->Instance->TXFQS & FDCAN_TXFQS_TFFL;
  2877. /* Return Tx FIFO free level */
  2878. return FreeLevel;
  2879. }
  2880. /**
  2881. * @brief Check if the FDCAN peripheral entered Restricted Operation Mode.
  2882. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2883. * the configuration information for the specified FDCAN.
  2884. * @retval Status
  2885. * - 0 : Normal FDCAN operation.
  2886. * - 1 : Restricted Operation Mode active.
  2887. */
  2888. uint32_t HAL_FDCAN_IsRestrictedOperationMode(FDCAN_HandleTypeDef *hfdcan)
  2889. {
  2890. uint32_t OperationMode;
  2891. /* Get Operation Mode */
  2892. OperationMode = ((hfdcan->Instance->CCCR & FDCAN_CCCR_ASM) >> FDCAN_CCCR_ASM_Pos);
  2893. return OperationMode;
  2894. }
  2895. /**
  2896. * @brief Exit Restricted Operation Mode.
  2897. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2898. * the configuration information for the specified FDCAN.
  2899. * @retval HAL status
  2900. */
  2901. HAL_StatusTypeDef HAL_FDCAN_ExitRestrictedOperationMode(FDCAN_HandleTypeDef *hfdcan)
  2902. {
  2903. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  2904. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  2905. {
  2906. /* Exit Restricted Operation mode */
  2907. CLEAR_BIT(hfdcan->Instance->CCCR, FDCAN_CCCR_ASM);
  2908. /* Return function status */
  2909. return HAL_OK;
  2910. }
  2911. else
  2912. {
  2913. /* Update error code */
  2914. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  2915. return HAL_ERROR;
  2916. }
  2917. }
  2918. /**
  2919. * @}
  2920. */
  2921. /** @defgroup FDCAN_Exported_Functions_Group4 TT Configuration and control functions
  2922. * @brief TT Configuration and control functions
  2923. *
  2924. @verbatim
  2925. ==============================================================================
  2926. ##### TT Configuration and control functions #####
  2927. ==============================================================================
  2928. [..] This section provides functions allowing to:
  2929. (+) HAL_FDCAN_TT_ConfigOperation : Initialize TT operation parameters
  2930. (+) HAL_FDCAN_TT_ConfigReferenceMessage : Configure the reference message
  2931. (+) HAL_FDCAN_TT_ConfigTrigger : Configure the FDCAN trigger
  2932. (+) HAL_FDCAN_TT_SetGlobalTime : Schedule global time adjustment
  2933. (+) HAL_FDCAN_TT_SetClockSynchronization : Schedule TUR numerator update
  2934. (+) HAL_FDCAN_TT_ConfigStopWatch : Configure stop watch source and polarity
  2935. (+) HAL_FDCAN_TT_ConfigRegisterTimeMark : Configure register time mark pulse generation
  2936. (+) HAL_FDCAN_TT_EnableRegisterTimeMarkPulse : Enable register time mark pulse generation
  2937. (+) HAL_FDCAN_TT_DisableRegisterTimeMarkPulse : Disable register time mark pulse generation
  2938. (+) HAL_FDCAN_TT_EnableTriggerTimeMarkPulse : Enable trigger time mark pulse generation
  2939. (+) HAL_FDCAN_TT_DisableTriggerTimeMarkPulse : Disable trigger time mark pulse generation
  2940. (+) HAL_FDCAN_TT_EnableHardwareGapControl : Enable gap control by input pin fdcan1_evt
  2941. (+) HAL_FDCAN_TT_DisableHardwareGapControl : Disable gap control by input pin fdcan1_evt
  2942. (+) HAL_FDCAN_TT_EnableTimeMarkGapControl : Enable gap control (finish only) by register time mark interrupt
  2943. (+) HAL_FDCAN_TT_DisableTimeMarkGapControl : Disable gap control by register time mark interrupt
  2944. (+) HAL_FDCAN_TT_SetNextIsGap : Transmit next reference message with Next_is_Gap = "1"
  2945. (+) HAL_FDCAN_TT_SetEndOfGap : Finish a Gap by requesting start of reference message
  2946. (+) HAL_FDCAN_TT_ConfigExternalSyncPhase : Configure target phase used for external synchronization
  2947. (+) HAL_FDCAN_TT_EnableExternalSynchronization : Synchronize the phase of the FDCAN schedule to an external schedule
  2948. (+) HAL_FDCAN_TT_DisableExternalSynchronization : Disable external schedule synchronization
  2949. (+) HAL_FDCAN_TT_GetOperationStatus : Get TT operation status
  2950. @endverbatim
  2951. * @{
  2952. */
  2953. /**
  2954. * @brief Initialize TT operation parameters.
  2955. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  2956. * the configuration information for the specified FDCAN.
  2957. * @param pTTParams pointer to a FDCAN_TT_ConfigTypeDef structure.
  2958. * @retval HAL status
  2959. */
  2960. HAL_StatusTypeDef HAL_FDCAN_TT_ConfigOperation(FDCAN_HandleTypeDef *hfdcan, FDCAN_TT_ConfigTypeDef *pTTParams)
  2961. {
  2962. uint32_t tickstart;
  2963. uint32_t RAMcounter;
  2964. uint32_t StartAddress;
  2965. /* Check function parameters */
  2966. assert_param(IS_FDCAN_TT_INSTANCE(hfdcan->Instance));
  2967. assert_param(IS_FDCAN_TT_TUR_NUMERATOR(pTTParams->TURNumerator));
  2968. assert_param(IS_FDCAN_TT_TUR_DENOMINATOR(pTTParams->TURDenominator));
  2969. assert_param(IS_FDCAN_TT_TIME_MASTER(pTTParams->TimeMaster));
  2970. assert_param(IS_FDCAN_MAX_VALUE(pTTParams->SyncDevLimit, 7U));
  2971. assert_param(IS_FDCAN_MAX_VALUE(pTTParams->InitRefTrigOffset, 127U));
  2972. assert_param(IS_FDCAN_MAX_VALUE(pTTParams->TriggerMemoryNbr, 64U));
  2973. assert_param(IS_FDCAN_TT_CYCLE_START_SYNC(pTTParams->CycleStartSync));
  2974. assert_param(IS_FDCAN_TT_STOP_WATCH_TRIGGER(pTTParams->StopWatchTrigSel));
  2975. assert_param(IS_FDCAN_TT_EVENT_TRIGGER(pTTParams->EventTrigSel));
  2976. if (pTTParams->TimeMaster == FDCAN_TT_POTENTIAL_MASTER)
  2977. {
  2978. assert_param(IS_FDCAN_TT_BASIC_CYCLES_NUMBER(pTTParams->BasicCyclesNbr));
  2979. }
  2980. if (pTTParams->OperationMode != FDCAN_TT_COMMUNICATION_LEVEL0)
  2981. {
  2982. assert_param(IS_FDCAN_TT_OPERATION(pTTParams->GapEnable));
  2983. assert_param(IS_FDCAN_MAX_VALUE(pTTParams->AppWdgLimit, 255U));
  2984. assert_param(IS_FDCAN_TT_EVENT_TRIGGER_POLARITY(pTTParams->EvtTrigPolarity));
  2985. assert_param(IS_FDCAN_TT_TX_ENABLE_WINDOW(pTTParams->TxEnableWindow));
  2986. assert_param(IS_FDCAN_MAX_VALUE(pTTParams->ExpTxTrigNbr, 4095U));
  2987. }
  2988. if (pTTParams->OperationMode != FDCAN_TT_COMMUNICATION_LEVEL1)
  2989. {
  2990. assert_param(IS_FDCAN_TT_TUR_LEVEL_0_2(pTTParams->TURNumerator, pTTParams->TURDenominator));
  2991. assert_param(IS_FDCAN_TT_EXTERNAL_CLK_SYNC(pTTParams->ExternalClkSync));
  2992. assert_param(IS_FDCAN_TT_GLOBAL_TIME_FILTERING(pTTParams->GlobalTimeFilter));
  2993. assert_param(IS_FDCAN_TT_AUTO_CLK_CALIBRATION(pTTParams->ClockCalibration));
  2994. }
  2995. else
  2996. {
  2997. assert_param(IS_FDCAN_TT_TUR_LEVEL_1(pTTParams->TURNumerator, pTTParams->TURDenominator));
  2998. }
  2999. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  3000. {
  3001. /* Stop local time in order to enable write access to the other bits of TURCF register */
  3002. CLEAR_BIT(hfdcan->ttcan->TURCF, FDCAN_TURCF_ELT);
  3003. /* Get tick */
  3004. tickstart = HAL_GetTick();
  3005. /* Wait until the ELT bit into TURCF register is reset */
  3006. while ((hfdcan->ttcan->TURCF & FDCAN_TURCF_ELT) != 0U)
  3007. {
  3008. /* Check for the Timeout */
  3009. if ((HAL_GetTick() - tickstart) > FDCAN_TIMEOUT_VALUE)
  3010. {
  3011. /* Update error code */
  3012. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  3013. /* Change FDCAN state */
  3014. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  3015. return HAL_ERROR;
  3016. }
  3017. }
  3018. /* Configure TUR (Time Unit Ratio) */
  3019. MODIFY_REG(hfdcan->ttcan->TURCF,
  3020. (FDCAN_TURCF_NCL | FDCAN_TURCF_DC),
  3021. (((pTTParams->TURNumerator - 0x10000U) << FDCAN_TURCF_NCL_Pos) | (pTTParams->TURDenominator << FDCAN_TURCF_DC_Pos)));
  3022. /* Enable local time */
  3023. SET_BIT(hfdcan->ttcan->TURCF, FDCAN_TURCF_ELT);
  3024. /* Configure TT operation */
  3025. MODIFY_REG(hfdcan->ttcan->TTOCF,
  3026. (FDCAN_TTOCF_OM | FDCAN_TTOCF_TM | FDCAN_TTOCF_LDSDL | FDCAN_TTOCF_IRTO),
  3027. (pTTParams->OperationMode | \
  3028. pTTParams->TimeMaster | \
  3029. (pTTParams->SyncDevLimit << FDCAN_TTOCF_LDSDL_Pos) | \
  3030. (pTTParams->InitRefTrigOffset << FDCAN_TTOCF_IRTO_Pos)));
  3031. if (pTTParams->OperationMode != FDCAN_TT_COMMUNICATION_LEVEL0)
  3032. {
  3033. MODIFY_REG(hfdcan->ttcan->TTOCF,
  3034. (FDCAN_TTOCF_GEN | FDCAN_TTOCF_AWL | FDCAN_TTOCF_EVTP),
  3035. (pTTParams->GapEnable | \
  3036. (pTTParams->AppWdgLimit << FDCAN_TTOCF_AWL_Pos) | \
  3037. pTTParams->EvtTrigPolarity));
  3038. }
  3039. if (pTTParams->OperationMode != FDCAN_TT_COMMUNICATION_LEVEL1)
  3040. {
  3041. MODIFY_REG(hfdcan->ttcan->TTOCF,
  3042. (FDCAN_TTOCF_EECS | FDCAN_TTOCF_EGTF | FDCAN_TTOCF_ECC),
  3043. (pTTParams->ExternalClkSync | \
  3044. pTTParams->GlobalTimeFilter | \
  3045. pTTParams->ClockCalibration));
  3046. }
  3047. /* Configure system matrix limits */
  3048. MODIFY_REG(hfdcan->ttcan->TTMLM, FDCAN_TTMLM_CSS, pTTParams->CycleStartSync);
  3049. if (pTTParams->OperationMode != FDCAN_TT_COMMUNICATION_LEVEL0)
  3050. {
  3051. MODIFY_REG(hfdcan->ttcan->TTMLM,
  3052. (FDCAN_TTMLM_TXEW | FDCAN_TTMLM_ENTT),
  3053. (((pTTParams->TxEnableWindow - 1U) << FDCAN_TTMLM_TXEW_Pos) | (pTTParams->ExpTxTrigNbr << FDCAN_TTMLM_ENTT_Pos)));
  3054. }
  3055. if (pTTParams->TimeMaster == FDCAN_TT_POTENTIAL_MASTER)
  3056. {
  3057. MODIFY_REG(hfdcan->ttcan->TTMLM, FDCAN_TTMLM_CCM, pTTParams->BasicCyclesNbr);
  3058. }
  3059. /* Configure input triggers: Stop watch and Event */
  3060. MODIFY_REG(hfdcan->ttcan->TTTS,
  3061. (FDCAN_TTTS_SWTSEL | FDCAN_TTTS_EVTSEL),
  3062. (pTTParams->StopWatchTrigSel | pTTParams->EventTrigSel));
  3063. /* Configure trigger memory start address */
  3064. StartAddress = (hfdcan->msgRam.EndAddress - SRAMCAN_BASE) / 4U;
  3065. MODIFY_REG(hfdcan->ttcan->TTTMC, FDCAN_TTTMC_TMSA, (StartAddress << FDCAN_TTTMC_TMSA_Pos));
  3066. /* Trigger memory elements number */
  3067. MODIFY_REG(hfdcan->ttcan->TTTMC, FDCAN_TTTMC_TME, (pTTParams->TriggerMemoryNbr << FDCAN_TTTMC_TME_Pos));
  3068. /* Recalculate End Address */
  3069. hfdcan->msgRam.TTMemorySA = hfdcan->msgRam.EndAddress;
  3070. hfdcan->msgRam.EndAddress = hfdcan->msgRam.TTMemorySA + (pTTParams->TriggerMemoryNbr * 2U * 4U);
  3071. if (hfdcan->msgRam.EndAddress > FDCAN_MESSAGE_RAM_END_ADDRESS) /* Last address of the Message RAM */
  3072. {
  3073. /* Update error code.
  3074. Message RAM overflow */
  3075. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_PARAM;
  3076. return HAL_ERROR;
  3077. }
  3078. else
  3079. {
  3080. /* Flush the allocated Message RAM area */
  3081. for (RAMcounter = hfdcan->msgRam.TTMemorySA; RAMcounter < hfdcan->msgRam.EndAddress; RAMcounter += 4U)
  3082. {
  3083. *(uint32_t *)(RAMcounter) = 0x00000000;
  3084. }
  3085. }
  3086. /* Return function status */
  3087. return HAL_OK;
  3088. }
  3089. else
  3090. {
  3091. /* Update error code */
  3092. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  3093. return HAL_ERROR;
  3094. }
  3095. }
  3096. /**
  3097. * @brief Configure the reference message.
  3098. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  3099. * the configuration information for the specified FDCAN.
  3100. * @param IdType Identifier Type.
  3101. * This parameter can be a value of @arg FDCAN_id_type.
  3102. * @param Identifier Reference Identifier.
  3103. * This parameter must be a number between:
  3104. * - 0 and 0x7FF, if IdType is FDCAN_STANDARD_ID
  3105. * - 0 and 0x1FFFFFFF, if IdType is FDCAN_EXTENDED_ID
  3106. * @param Payload Enable or disable the additional payload.
  3107. * This parameter can be a value of @arg FDCAN_TT_Reference_Message_Payload.
  3108. * This parameter is ignored in case of time slaves.
  3109. * If this parameter is set to FDCAN_TT_REF_MESSAGE_ADD_PAYLOAD, the
  3110. * following elements are taken from Tx Buffer 0:
  3111. * - MessageMarker
  3112. * - TxEventFifoControl
  3113. * - DataLength
  3114. * - Data Bytes (payload):
  3115. * - bytes 2-8, for Level 1
  3116. * - bytes 5-8, for Level 0 and Level 2
  3117. * @retval HAL status
  3118. */
  3119. HAL_StatusTypeDef HAL_FDCAN_TT_ConfigReferenceMessage(FDCAN_HandleTypeDef *hfdcan, uint32_t IdType, uint32_t Identifier, uint32_t Payload)
  3120. {
  3121. /* Check function parameters */
  3122. assert_param(IS_FDCAN_TT_INSTANCE(hfdcan->Instance));
  3123. assert_param(IS_FDCAN_ID_TYPE(IdType));
  3124. if (IdType == FDCAN_STANDARD_ID)
  3125. {
  3126. assert_param(IS_FDCAN_MAX_VALUE(Identifier, 0x7FFU));
  3127. }
  3128. else /* IdType == FDCAN_EXTENDED_ID */
  3129. {
  3130. assert_param(IS_FDCAN_MAX_VALUE(Identifier, 0x1FFFFFFFU));
  3131. }
  3132. assert_param(IS_FDCAN_TT_REFERENCE_MESSAGE_PAYLOAD(Payload));
  3133. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  3134. {
  3135. /* Configure reference message identifier type, identifier and payload */
  3136. if (IdType == FDCAN_EXTENDED_ID)
  3137. {
  3138. MODIFY_REG(hfdcan->ttcan->TTRMC, (FDCAN_TTRMC_RID | FDCAN_TTRMC_XTD | FDCAN_TTRMC_RMPS), (Payload | IdType | Identifier));
  3139. }
  3140. else /* IdType == FDCAN_STANDARD_ID */
  3141. {
  3142. MODIFY_REG(hfdcan->ttcan->TTRMC, (FDCAN_TTRMC_RID | FDCAN_TTRMC_XTD | FDCAN_TTRMC_RMPS), (Payload | IdType | (Identifier << 18)));
  3143. }
  3144. /* Return function status */
  3145. return HAL_OK;
  3146. }
  3147. else
  3148. {
  3149. /* Update error code */
  3150. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  3151. return HAL_ERROR;
  3152. }
  3153. }
  3154. /**
  3155. * @brief Configure the FDCAN trigger according to the specified
  3156. * parameters in the FDCAN_TriggerTypeDef structure.
  3157. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  3158. * the configuration information for the specified FDCAN.
  3159. * @param sTriggerConfig pointer to an FDCAN_TriggerTypeDef structure that
  3160. * contains the trigger configuration information
  3161. * @retval HAL status
  3162. */
  3163. HAL_StatusTypeDef HAL_FDCAN_TT_ConfigTrigger(FDCAN_HandleTypeDef *hfdcan, FDCAN_TriggerTypeDef *sTriggerConfig)
  3164. {
  3165. uint32_t CycleCode;
  3166. uint32_t MessageNumber;
  3167. uint32_t TriggerElementW1;
  3168. uint32_t TriggerElementW2;
  3169. uint32_t *TriggerAddress;
  3170. /* Check function parameters */
  3171. assert_param(IS_FDCAN_TT_INSTANCE(hfdcan->Instance));
  3172. assert_param(IS_FDCAN_MAX_VALUE(sTriggerConfig->TriggerIndex, 63U));
  3173. assert_param(IS_FDCAN_MAX_VALUE(sTriggerConfig->TimeMark, 0xFFFFU));
  3174. assert_param(IS_FDCAN_TT_REPEAT_FACTOR(sTriggerConfig->RepeatFactor));
  3175. if (sTriggerConfig->RepeatFactor != FDCAN_TT_REPEAT_EVERY_CYCLE)
  3176. {
  3177. assert_param(IS_FDCAN_MAX_VALUE(sTriggerConfig->StartCycle, (sTriggerConfig->RepeatFactor - 1U)));
  3178. }
  3179. assert_param(IS_FDCAN_TT_TM_EVENT_INTERNAL(sTriggerConfig->TmEventInt));
  3180. assert_param(IS_FDCAN_TT_TM_EVENT_EXTERNAL(sTriggerConfig->TmEventExt));
  3181. assert_param(IS_FDCAN_TT_TRIGGER_TYPE(sTriggerConfig->TriggerType));
  3182. assert_param(IS_FDCAN_ID_TYPE(sTriggerConfig->FilterType));
  3183. if ((sTriggerConfig->TriggerType == FDCAN_TT_TX_TRIGGER_SINGLE) ||
  3184. (sTriggerConfig->TriggerType == FDCAN_TT_TX_TRIGGER_CONTINUOUS) ||
  3185. (sTriggerConfig->TriggerType == FDCAN_TT_TX_TRIGGER_ARBITRATION) ||
  3186. (sTriggerConfig->TriggerType == FDCAN_TT_TX_TRIGGER_MERGED))
  3187. {
  3188. assert_param(IS_FDCAN_TX_LOCATION(sTriggerConfig->TxBufferIndex));
  3189. }
  3190. if (sTriggerConfig->TriggerType == FDCAN_TT_RX_TRIGGER)
  3191. {
  3192. if (sTriggerConfig->FilterType == FDCAN_STANDARD_ID)
  3193. {
  3194. assert_param(IS_FDCAN_MAX_VALUE(sTriggerConfig->FilterIndex, 63U));
  3195. }
  3196. else /* sTriggerConfig->FilterType == FDCAN_EXTENDED_ID */
  3197. {
  3198. assert_param(IS_FDCAN_MAX_VALUE(sTriggerConfig->FilterIndex, 127U));
  3199. }
  3200. }
  3201. if (hfdcan->State == HAL_FDCAN_STATE_READY)
  3202. {
  3203. /* Calculate cycle code */
  3204. if (sTriggerConfig->RepeatFactor == FDCAN_TT_REPEAT_EVERY_CYCLE)
  3205. {
  3206. CycleCode = FDCAN_TT_REPEAT_EVERY_CYCLE;
  3207. }
  3208. else /* sTriggerConfig->RepeatFactor != FDCAN_TT_REPEAT_EVERY_CYCLE */
  3209. {
  3210. CycleCode = sTriggerConfig->RepeatFactor + sTriggerConfig->StartCycle;
  3211. }
  3212. /* Build first word of trigger element */
  3213. TriggerElementW1 = ((sTriggerConfig->TimeMark << 16) | \
  3214. (CycleCode << 8) | \
  3215. sTriggerConfig->TmEventInt | \
  3216. sTriggerConfig->TmEventExt | \
  3217. sTriggerConfig->TriggerType);
  3218. /* Select message number depending on trigger type (transmission or reception) */
  3219. if (sTriggerConfig->TriggerType == FDCAN_TT_RX_TRIGGER)
  3220. {
  3221. MessageNumber = sTriggerConfig->FilterIndex;
  3222. }
  3223. else if ((sTriggerConfig->TriggerType == FDCAN_TT_TX_TRIGGER_SINGLE) ||
  3224. (sTriggerConfig->TriggerType == FDCAN_TT_TX_TRIGGER_CONTINUOUS) ||
  3225. (sTriggerConfig->TriggerType == FDCAN_TT_TX_TRIGGER_ARBITRATION) ||
  3226. (sTriggerConfig->TriggerType == FDCAN_TT_TX_TRIGGER_MERGED))
  3227. {
  3228. MessageNumber = POSITION_VAL(sTriggerConfig->TxBufferIndex);
  3229. }
  3230. else
  3231. {
  3232. MessageNumber = 0U;
  3233. }
  3234. /* Build second word of trigger element */
  3235. TriggerElementW2 = ((sTriggerConfig->FilterType >> 7) | (MessageNumber << 16));
  3236. /* Calculate trigger address */
  3237. TriggerAddress = (uint32_t *)(hfdcan->msgRam.TTMemorySA + (sTriggerConfig->TriggerIndex * 4U * 2U));
  3238. /* Write trigger element to the message RAM */
  3239. *TriggerAddress = TriggerElementW1;
  3240. TriggerAddress++;
  3241. *TriggerAddress = TriggerElementW2;
  3242. /* Return function status */
  3243. return HAL_OK;
  3244. }
  3245. else
  3246. {
  3247. /* Update error code */
  3248. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_READY;
  3249. return HAL_ERROR;
  3250. }
  3251. }
  3252. /**
  3253. * @brief Schedule global time adjustment for the next reference message.
  3254. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  3255. * the configuration information for the specified FDCAN.
  3256. * @param TimePreset time preset value.
  3257. * This parameter must be a number between:
  3258. * - 0x0000 and 0x7FFF, Next_Master_Ref_Mark = Current_Master_Ref_Mark + TimePreset
  3259. * or
  3260. * - 0x8001 and 0xFFFF, Next_Master_Ref_Mark = Current_Master_Ref_Mark - (0x10000 - TimePreset)
  3261. * @retval HAL status
  3262. */
  3263. HAL_StatusTypeDef HAL_FDCAN_TT_SetGlobalTime(FDCAN_HandleTypeDef *hfdcan, uint32_t TimePreset)
  3264. {
  3265. uint32_t Counter = 0U;
  3266. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  3267. /* Check function parameters */
  3268. assert_param(IS_FDCAN_TT_INSTANCE(hfdcan->Instance));
  3269. assert_param(IS_FDCAN_TT_TIME_PRESET(TimePreset));
  3270. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  3271. {
  3272. /* Check that the external clock synchronization is enabled */
  3273. if ((hfdcan->ttcan->TTOCF & FDCAN_TTOCF_EECS) != FDCAN_TTOCF_EECS)
  3274. {
  3275. /* Update error code */
  3276. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_SUPPORTED;
  3277. return HAL_ERROR;
  3278. }
  3279. /* Check that no global time preset is pending */
  3280. if ((hfdcan->ttcan->TTOST & FDCAN_TTOST_WGTD) == FDCAN_TTOST_WGTD)
  3281. {
  3282. /* Update error code */
  3283. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_PENDING;
  3284. return HAL_ERROR;
  3285. }
  3286. /* Configure time preset */
  3287. MODIFY_REG(hfdcan->ttcan->TTGTP, FDCAN_TTGTP_TP, (TimePreset << FDCAN_TTGTP_TP_Pos));
  3288. /* Wait until the LCKC bit into TTOCN register is reset */
  3289. while ((hfdcan->ttcan->TTOCN & FDCAN_TTOCN_LCKC) != 0U)
  3290. {
  3291. /* Check for the Timeout */
  3292. if (Counter > FDCAN_TIMEOUT_VALUE)
  3293. {
  3294. /* Update error code */
  3295. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  3296. /* Change FDCAN state */
  3297. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  3298. return HAL_ERROR;
  3299. }
  3300. /* Increment counter */
  3301. Counter++;
  3302. }
  3303. /* Schedule time preset to take effect by the next reference message */
  3304. SET_BIT(hfdcan->ttcan->TTOCN, FDCAN_TTOCN_SGT);
  3305. /* Return function status */
  3306. return HAL_OK;
  3307. }
  3308. else
  3309. {
  3310. /* Update error code */
  3311. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  3312. return HAL_ERROR;
  3313. }
  3314. }
  3315. /**
  3316. * @brief Schedule TUR numerator update for the next reference message.
  3317. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  3318. * the configuration information for the specified FDCAN.
  3319. * @param NewTURNumerator new value of the TUR numerator.
  3320. * This parameter must be a number between 0x10000 and 0x1FFFF.
  3321. * @retval HAL status
  3322. */
  3323. HAL_StatusTypeDef HAL_FDCAN_TT_SetClockSynchronization(FDCAN_HandleTypeDef *hfdcan, uint32_t NewTURNumerator)
  3324. {
  3325. uint32_t Counter = 0U;
  3326. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  3327. /* Check function parameters */
  3328. assert_param(IS_FDCAN_TT_INSTANCE(hfdcan->Instance));
  3329. assert_param(IS_FDCAN_TT_TUR_NUMERATOR(NewTURNumerator));
  3330. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  3331. {
  3332. /* Check that the external clock synchronization is enabled */
  3333. if ((hfdcan->ttcan->TTOCF & FDCAN_TTOCF_EECS) != FDCAN_TTOCF_EECS)
  3334. {
  3335. /* Update error code */
  3336. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_SUPPORTED;
  3337. return HAL_ERROR;
  3338. }
  3339. /* Check that no external clock synchronization is pending */
  3340. if ((hfdcan->ttcan->TTOST & FDCAN_TTOST_WECS) == FDCAN_TTOST_WECS)
  3341. {
  3342. /* Update error code */
  3343. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_PENDING;
  3344. return HAL_ERROR;
  3345. }
  3346. /* Configure new TUR numerator */
  3347. MODIFY_REG(hfdcan->ttcan->TURCF, FDCAN_TURCF_NCL, (NewTURNumerator - 0x10000U));
  3348. /* Wait until the LCKC bit into TTOCN register is reset */
  3349. while ((hfdcan->ttcan->TTOCN & FDCAN_TTOCN_LCKC) != 0U)
  3350. {
  3351. /* Check for the Timeout */
  3352. if (Counter > FDCAN_TIMEOUT_VALUE)
  3353. {
  3354. /* Update error code */
  3355. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  3356. /* Change FDCAN state */
  3357. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  3358. return HAL_ERROR;
  3359. }
  3360. /* Increment counter */
  3361. Counter++;
  3362. }
  3363. /* Schedule TUR numerator update by the next reference message */
  3364. SET_BIT(hfdcan->ttcan->TTOCN, FDCAN_TTOCN_ECS);
  3365. /* Return function status */
  3366. return HAL_OK;
  3367. }
  3368. else
  3369. {
  3370. /* Update error code */
  3371. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  3372. return HAL_ERROR;
  3373. }
  3374. }
  3375. /**
  3376. * @brief Configure stop watch source and polarity.
  3377. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  3378. * the configuration information for the specified FDCAN.
  3379. * @param Source stop watch source.
  3380. * This parameter can be a value of @arg FDCAN_TT_stop_watch_source.
  3381. * @param Polarity stop watch polarity.
  3382. * This parameter can be a value of @arg FDCAN_TT_stop_watch_polarity.
  3383. * @retval HAL status
  3384. */
  3385. HAL_StatusTypeDef HAL_FDCAN_TT_ConfigStopWatch(FDCAN_HandleTypeDef *hfdcan, uint32_t Source, uint32_t Polarity)
  3386. {
  3387. uint32_t Counter = 0U;
  3388. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  3389. /* Check function parameters */
  3390. assert_param(IS_FDCAN_TT_INSTANCE(hfdcan->Instance));
  3391. assert_param(IS_FDCAN_TT_STOP_WATCH_SOURCE(Source));
  3392. assert_param(IS_FDCAN_TT_STOP_WATCH_POLARITY(Polarity));
  3393. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  3394. {
  3395. /* Wait until the LCKC bit into TTOCN register is reset */
  3396. while ((hfdcan->ttcan->TTOCN & FDCAN_TTOCN_LCKC) != 0U)
  3397. {
  3398. /* Check for the Timeout */
  3399. if (Counter > FDCAN_TIMEOUT_VALUE)
  3400. {
  3401. /* Update error code */
  3402. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  3403. /* Change FDCAN state */
  3404. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  3405. return HAL_ERROR;
  3406. }
  3407. /* Increment counter */
  3408. Counter++;
  3409. }
  3410. /* Select stop watch source and polarity */
  3411. MODIFY_REG(hfdcan->ttcan->TTOCN, (FDCAN_TTOCN_SWS | FDCAN_TTOCN_SWP), (Source | Polarity));
  3412. /* Return function status */
  3413. return HAL_OK;
  3414. }
  3415. else
  3416. {
  3417. /* Update error code */
  3418. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  3419. return HAL_ERROR;
  3420. }
  3421. }
  3422. /**
  3423. * @brief Configure register time mark pulse generation.
  3424. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  3425. * the configuration information for the specified FDCAN.
  3426. * @param TimeMarkSource time mark source.
  3427. * This parameter can be a value of @arg FDCAN_TT_time_mark_source.
  3428. * @param TimeMarkValue time mark value (reference).
  3429. * This parameter must be a number between 0 and 0xFFFF.
  3430. * @param RepeatFactor repeat factor of the cycle for which the time mark is valid.
  3431. * This parameter can be a value of @arg FDCAN_TT_Repeat_Factor.
  3432. * @param StartCycle index of the first cycle in which the time mark becomes valid.
  3433. * This parameter is ignored if RepeatFactor is set to FDCAN_TT_REPEAT_EVERY_CYCLE.
  3434. * This parameter must be a number between 0 and RepeatFactor.
  3435. * @retval HAL status
  3436. */
  3437. HAL_StatusTypeDef HAL_FDCAN_TT_ConfigRegisterTimeMark(FDCAN_HandleTypeDef *hfdcan,
  3438. uint32_t TimeMarkSource, uint32_t TimeMarkValue,
  3439. uint32_t RepeatFactor, uint32_t StartCycle)
  3440. {
  3441. uint32_t Counter = 0U;
  3442. uint32_t CycleCode;
  3443. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  3444. /* Check function parameters */
  3445. assert_param(IS_FDCAN_TT_INSTANCE(hfdcan->Instance));
  3446. assert_param(IS_FDCAN_TT_REGISTER_TIME_MARK_SOURCE(TimeMarkSource));
  3447. assert_param(IS_FDCAN_MAX_VALUE(TimeMarkValue, 0xFFFFU));
  3448. assert_param(IS_FDCAN_TT_REPEAT_FACTOR(RepeatFactor));
  3449. if (RepeatFactor != FDCAN_TT_REPEAT_EVERY_CYCLE)
  3450. {
  3451. assert_param(IS_FDCAN_MAX_VALUE(StartCycle, (RepeatFactor - 1U)));
  3452. }
  3453. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  3454. {
  3455. /* Wait until the LCKC bit into TTOCN register is reset */
  3456. while ((hfdcan->ttcan->TTOCN & FDCAN_TTOCN_LCKC) != 0U)
  3457. {
  3458. /* Check for the Timeout */
  3459. if (Counter > FDCAN_TIMEOUT_VALUE)
  3460. {
  3461. /* Update error code */
  3462. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  3463. /* Change FDCAN state */
  3464. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  3465. return HAL_ERROR;
  3466. }
  3467. /* Increment counter */
  3468. Counter++;
  3469. }
  3470. /* Disable the time mark compare function */
  3471. CLEAR_BIT(hfdcan->ttcan->TTOCN, FDCAN_TTOCN_TMC);
  3472. if (TimeMarkSource != FDCAN_TT_REG_TIMEMARK_DIABLED)
  3473. {
  3474. /* Calculate cycle code */
  3475. if (RepeatFactor == FDCAN_TT_REPEAT_EVERY_CYCLE)
  3476. {
  3477. CycleCode = FDCAN_TT_REPEAT_EVERY_CYCLE;
  3478. }
  3479. else /* RepeatFactor != FDCAN_TT_REPEAT_EVERY_CYCLE */
  3480. {
  3481. CycleCode = RepeatFactor + StartCycle;
  3482. }
  3483. Counter = 0U;
  3484. /* Wait until the LCKM bit into TTTMK register is reset */
  3485. while ((hfdcan->ttcan->TTTMK & FDCAN_TTTMK_LCKM) != 0U)
  3486. {
  3487. /* Check for the Timeout */
  3488. if (Counter > FDCAN_TIMEOUT_VALUE)
  3489. {
  3490. /* Update error code */
  3491. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  3492. /* Change FDCAN state */
  3493. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  3494. return HAL_ERROR;
  3495. }
  3496. /* Increment counter */
  3497. Counter++;
  3498. }
  3499. /* Configure time mark value and cycle code */
  3500. hfdcan->ttcan->TTTMK = ((TimeMarkValue << FDCAN_TTTMK_TM_Pos) | (CycleCode << FDCAN_TTTMK_TICC_Pos));
  3501. Counter = 0U;
  3502. /* Wait until the LCKC bit into TTOCN register is reset */
  3503. while ((hfdcan->ttcan->TTOCN & FDCAN_TTOCN_LCKC) != 0U)
  3504. {
  3505. /* Check for the Timeout */
  3506. if (Counter > FDCAN_TIMEOUT_VALUE)
  3507. {
  3508. /* Update error code */
  3509. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  3510. /* Change FDCAN state */
  3511. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  3512. return HAL_ERROR;
  3513. }
  3514. /* Increment counter */
  3515. Counter++;
  3516. }
  3517. /* Update the register time mark compare source */
  3518. MODIFY_REG(hfdcan->ttcan->TTOCN, FDCAN_TTOCN_TMC, TimeMarkSource);
  3519. }
  3520. /* Return function status */
  3521. return HAL_OK;
  3522. }
  3523. else
  3524. {
  3525. /* Update error code */
  3526. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  3527. return HAL_ERROR;
  3528. }
  3529. }
  3530. /**
  3531. * @brief Enable register time mark pulse generation.
  3532. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  3533. * the configuration information for the specified FDCAN.
  3534. * @retval HAL status
  3535. */
  3536. HAL_StatusTypeDef HAL_FDCAN_TT_EnableRegisterTimeMarkPulse(FDCAN_HandleTypeDef *hfdcan)
  3537. {
  3538. uint32_t Counter = 0U;
  3539. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  3540. /* Check function parameters */
  3541. assert_param(IS_FDCAN_TT_INSTANCE(hfdcan->Instance));
  3542. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  3543. {
  3544. /* Wait until the LCKC bit into TTOCN register is reset */
  3545. while ((hfdcan->ttcan->TTOCN & FDCAN_TTOCN_LCKC) != 0U)
  3546. {
  3547. /* Check for the Timeout */
  3548. if (Counter > FDCAN_TIMEOUT_VALUE)
  3549. {
  3550. /* Update error code */
  3551. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  3552. /* Change FDCAN state */
  3553. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  3554. return HAL_ERROR;
  3555. }
  3556. /* Increment counter */
  3557. Counter++;
  3558. }
  3559. /* Enable Register Time Mark Interrupt output on fdcan1_rtp */
  3560. SET_BIT(hfdcan->ttcan->TTOCN, FDCAN_TTOCN_RTIE);
  3561. /* Return function status */
  3562. return HAL_OK;
  3563. }
  3564. else
  3565. {
  3566. /* Update error code */
  3567. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  3568. return HAL_ERROR;
  3569. }
  3570. }
  3571. /**
  3572. * @brief Disable register time mark pulse generation.
  3573. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  3574. * the configuration information for the specified FDCAN.
  3575. * @retval HAL status
  3576. */
  3577. HAL_StatusTypeDef HAL_FDCAN_TT_DisableRegisterTimeMarkPulse(FDCAN_HandleTypeDef *hfdcan)
  3578. {
  3579. uint32_t Counter = 0U;
  3580. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  3581. /* Check function parameters */
  3582. assert_param(IS_FDCAN_TT_INSTANCE(hfdcan->Instance));
  3583. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  3584. {
  3585. /* Wait until the LCKC bit into TTOCN register is reset */
  3586. while ((hfdcan->ttcan->TTOCN & FDCAN_TTOCN_LCKC) != 0U)
  3587. {
  3588. /* Check for the Timeout */
  3589. if (Counter > FDCAN_TIMEOUT_VALUE)
  3590. {
  3591. /* Update error code */
  3592. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  3593. /* Change FDCAN state */
  3594. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  3595. return HAL_ERROR;
  3596. }
  3597. /* Increment counter */
  3598. Counter++;
  3599. }
  3600. /* Disable Register Time Mark Interrupt output on fdcan1_rtp */
  3601. CLEAR_BIT(hfdcan->ttcan->TTOCN, FDCAN_TTOCN_RTIE);
  3602. /* Return function status */
  3603. return HAL_OK;
  3604. }
  3605. else
  3606. {
  3607. /* Update error code */
  3608. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  3609. return HAL_ERROR;
  3610. }
  3611. }
  3612. /**
  3613. * @brief Enable trigger time mark pulse generation.
  3614. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  3615. * the configuration information for the specified FDCAN.
  3616. * @retval HAL status
  3617. */
  3618. HAL_StatusTypeDef HAL_FDCAN_TT_EnableTriggerTimeMarkPulse(FDCAN_HandleTypeDef *hfdcan)
  3619. {
  3620. uint32_t Counter = 0U;
  3621. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  3622. /* Check function parameters */
  3623. assert_param(IS_FDCAN_TT_INSTANCE(hfdcan->Instance));
  3624. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  3625. {
  3626. if ((hfdcan->ttcan->TTOCF & FDCAN_TTOCF_OM) != FDCAN_TT_COMMUNICATION_LEVEL0)
  3627. {
  3628. /* Wait until the LCKC bit into TTOCN register is reset */
  3629. while ((hfdcan->ttcan->TTOCN & FDCAN_TTOCN_LCKC) != 0U)
  3630. {
  3631. /* Check for the Timeout */
  3632. if (Counter > FDCAN_TIMEOUT_VALUE)
  3633. {
  3634. /* Update error code */
  3635. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  3636. /* Change FDCAN state */
  3637. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  3638. return HAL_ERROR;
  3639. }
  3640. /* Increment counter */
  3641. Counter++;
  3642. }
  3643. /* Enable Trigger Time Mark Interrupt output on fdcan1_tmp */
  3644. SET_BIT(hfdcan->ttcan->TTOCN, FDCAN_TTOCN_TTIE);
  3645. /* Return function status */
  3646. return HAL_OK;
  3647. }
  3648. else
  3649. {
  3650. /* Update error code.
  3651. Feature not supported for TT Level 0 */
  3652. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_SUPPORTED;
  3653. return HAL_ERROR;
  3654. }
  3655. }
  3656. else
  3657. {
  3658. /* Update error code */
  3659. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  3660. return HAL_ERROR;
  3661. }
  3662. }
  3663. /**
  3664. * @brief Disable trigger time mark pulse generation.
  3665. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  3666. * the configuration information for the specified FDCAN.
  3667. * @retval HAL status
  3668. */
  3669. HAL_StatusTypeDef HAL_FDCAN_TT_DisableTriggerTimeMarkPulse(FDCAN_HandleTypeDef *hfdcan)
  3670. {
  3671. uint32_t Counter = 0U;
  3672. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  3673. /* Check function parameters */
  3674. assert_param(IS_FDCAN_TT_INSTANCE(hfdcan->Instance));
  3675. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  3676. {
  3677. if ((hfdcan->ttcan->TTOCF & FDCAN_TTOCF_OM) != FDCAN_TT_COMMUNICATION_LEVEL0)
  3678. {
  3679. /* Wait until the LCKC bit into TTOCN register is reset */
  3680. while ((hfdcan->ttcan->TTOCN & FDCAN_TTOCN_LCKC) != 0U)
  3681. {
  3682. /* Check for the Timeout */
  3683. if (Counter > FDCAN_TIMEOUT_VALUE)
  3684. {
  3685. /* Update error code */
  3686. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  3687. /* Change FDCAN state */
  3688. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  3689. return HAL_ERROR;
  3690. }
  3691. /* Increment counter */
  3692. Counter++;
  3693. }
  3694. /* Disable Trigger Time Mark Interrupt output on fdcan1_rtp */
  3695. CLEAR_BIT(hfdcan->ttcan->TTOCN, FDCAN_TTOCN_TTIE);
  3696. /* Return function status */
  3697. return HAL_OK;
  3698. }
  3699. else
  3700. {
  3701. /* Update error code.
  3702. Feature not supported for TT Level 0 */
  3703. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_SUPPORTED;
  3704. return HAL_ERROR;
  3705. }
  3706. }
  3707. else
  3708. {
  3709. /* Update error code */
  3710. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  3711. return HAL_ERROR;
  3712. }
  3713. }
  3714. /**
  3715. * @brief Enable gap control by input pin fdcan1_evt.
  3716. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  3717. * the configuration information for the specified FDCAN.
  3718. * @retval HAL status
  3719. */
  3720. HAL_StatusTypeDef HAL_FDCAN_TT_EnableHardwareGapControl(FDCAN_HandleTypeDef *hfdcan)
  3721. {
  3722. uint32_t Counter = 0U;
  3723. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  3724. /* Check function parameters */
  3725. assert_param(IS_FDCAN_TT_INSTANCE(hfdcan->Instance));
  3726. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  3727. {
  3728. if ((hfdcan->ttcan->TTOCF & FDCAN_TTOCF_OM) != FDCAN_TT_COMMUNICATION_LEVEL0)
  3729. {
  3730. /* Wait until the LCKC bit into TTOCN register is reset */
  3731. while ((hfdcan->ttcan->TTOCN & FDCAN_TTOCN_LCKC) != 0U)
  3732. {
  3733. /* Check for the Timeout */
  3734. if (Counter > FDCAN_TIMEOUT_VALUE)
  3735. {
  3736. /* Update error code */
  3737. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  3738. /* Change FDCAN state */
  3739. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  3740. return HAL_ERROR;
  3741. }
  3742. /* Increment counter */
  3743. Counter++;
  3744. }
  3745. /* Enable gap control by pin fdcan1_evt */
  3746. SET_BIT(hfdcan->ttcan->TTOCN, FDCAN_TTOCN_GCS);
  3747. /* Return function status */
  3748. return HAL_OK;
  3749. }
  3750. else
  3751. {
  3752. /* Update error code.
  3753. Feature not supported for TT Level 0 */
  3754. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_SUPPORTED;
  3755. return HAL_ERROR;
  3756. }
  3757. }
  3758. else
  3759. {
  3760. /* Update error code */
  3761. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  3762. return HAL_ERROR;
  3763. }
  3764. }
  3765. /**
  3766. * @brief Disable gap control by input pin fdcan1_evt.
  3767. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  3768. * the configuration information for the specified FDCAN.
  3769. * @retval HAL status
  3770. */
  3771. HAL_StatusTypeDef HAL_FDCAN_TT_DisableHardwareGapControl(FDCAN_HandleTypeDef *hfdcan)
  3772. {
  3773. uint32_t Counter = 0U;
  3774. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  3775. /* Check function parameters */
  3776. assert_param(IS_FDCAN_TT_INSTANCE(hfdcan->Instance));
  3777. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  3778. {
  3779. if ((hfdcan->ttcan->TTOCF & FDCAN_TTOCF_OM) != FDCAN_TT_COMMUNICATION_LEVEL0)
  3780. {
  3781. /* Wait until the LCKC bit into TTOCN register is reset */
  3782. while ((hfdcan->ttcan->TTOCN & FDCAN_TTOCN_LCKC) != 0U)
  3783. {
  3784. /* Check for the Timeout */
  3785. if (Counter > FDCAN_TIMEOUT_VALUE)
  3786. {
  3787. /* Update error code */
  3788. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  3789. /* Change FDCAN state */
  3790. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  3791. return HAL_ERROR;
  3792. }
  3793. /* Increment counter */
  3794. Counter++;
  3795. }
  3796. /* Disable gap control by pin fdcan1_evt */
  3797. CLEAR_BIT(hfdcan->ttcan->TTOCN, FDCAN_TTOCN_GCS);
  3798. /* Return function status */
  3799. return HAL_OK;
  3800. }
  3801. else
  3802. {
  3803. /* Update error code.
  3804. Feature not supported for TT Level 0 */
  3805. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_SUPPORTED;
  3806. return HAL_ERROR;
  3807. }
  3808. }
  3809. else
  3810. {
  3811. /* Update error code */
  3812. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  3813. return HAL_ERROR;
  3814. }
  3815. }
  3816. /**
  3817. * @brief Enable gap control (finish only) by register time mark interrupt.
  3818. * The next register time mark interrupt (TTIR.RTMI = "1") will finish
  3819. * the Gap and start the reference message.
  3820. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  3821. * the configuration information for the specified FDCAN.
  3822. * @retval HAL status
  3823. */
  3824. HAL_StatusTypeDef HAL_FDCAN_TT_EnableTimeMarkGapControl(FDCAN_HandleTypeDef *hfdcan)
  3825. {
  3826. uint32_t Counter = 0U;
  3827. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  3828. /* Check function parameters */
  3829. assert_param(IS_FDCAN_TT_INSTANCE(hfdcan->Instance));
  3830. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  3831. {
  3832. if ((hfdcan->ttcan->TTOCF & FDCAN_TTOCF_OM) != FDCAN_TT_COMMUNICATION_LEVEL0)
  3833. {
  3834. /* Wait until the LCKC bit into TTOCN register is reset */
  3835. while ((hfdcan->ttcan->TTOCN & FDCAN_TTOCN_LCKC) != 0U)
  3836. {
  3837. /* Check for the Timeout */
  3838. if (Counter > FDCAN_TIMEOUT_VALUE)
  3839. {
  3840. /* Update error code */
  3841. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  3842. /* Change FDCAN state */
  3843. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  3844. return HAL_ERROR;
  3845. }
  3846. /* Increment counter */
  3847. Counter++;
  3848. }
  3849. /* Enable gap control by register time mark interrupt */
  3850. SET_BIT(hfdcan->ttcan->TTOCN, FDCAN_TTOCN_TMG);
  3851. /* Return function status */
  3852. return HAL_OK;
  3853. }
  3854. else
  3855. {
  3856. /* Update error code.
  3857. Feature not supported for TT Level 0 */
  3858. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_SUPPORTED;
  3859. return HAL_ERROR;
  3860. }
  3861. }
  3862. else
  3863. {
  3864. /* Update error code */
  3865. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  3866. return HAL_ERROR;
  3867. }
  3868. }
  3869. /**
  3870. * @brief Disable gap control by register time mark interrupt.
  3871. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  3872. * the configuration information for the specified FDCAN.
  3873. * @retval HAL status
  3874. */
  3875. HAL_StatusTypeDef HAL_FDCAN_TT_DisableTimeMarkGapControl(FDCAN_HandleTypeDef *hfdcan)
  3876. {
  3877. uint32_t Counter = 0U;
  3878. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  3879. /* Check function parameters */
  3880. assert_param(IS_FDCAN_TT_INSTANCE(hfdcan->Instance));
  3881. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  3882. {
  3883. if ((hfdcan->ttcan->TTOCF & FDCAN_TTOCF_OM) != FDCAN_TT_COMMUNICATION_LEVEL0)
  3884. {
  3885. /* Wait until the LCKC bit into TTOCN register is reset */
  3886. while ((hfdcan->ttcan->TTOCN & FDCAN_TTOCN_LCKC) != 0U)
  3887. {
  3888. /* Check for the Timeout */
  3889. if (Counter > FDCAN_TIMEOUT_VALUE)
  3890. {
  3891. /* Update error code */
  3892. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  3893. /* Change FDCAN state */
  3894. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  3895. return HAL_ERROR;
  3896. }
  3897. /* Increment counter */
  3898. Counter++;
  3899. }
  3900. /* Disable gap control by register time mark interrupt */
  3901. CLEAR_BIT(hfdcan->ttcan->TTOCN, FDCAN_TTOCN_TMG);
  3902. /* Return function status */
  3903. return HAL_OK;
  3904. }
  3905. else
  3906. {
  3907. /* Update error code.
  3908. Feature not supported for TT Level 0 */
  3909. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_SUPPORTED;
  3910. return HAL_ERROR;
  3911. }
  3912. }
  3913. else
  3914. {
  3915. /* Update error code */
  3916. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  3917. return HAL_ERROR;
  3918. }
  3919. }
  3920. /**
  3921. * @brief Transmit next reference message with Next_is_Gap = "1".
  3922. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  3923. * the configuration information for the specified FDCAN.
  3924. * @retval HAL status
  3925. */
  3926. HAL_StatusTypeDef HAL_FDCAN_TT_SetNextIsGap(FDCAN_HandleTypeDef *hfdcan)
  3927. {
  3928. uint32_t Counter = 0U;
  3929. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  3930. /* Check function parameters */
  3931. assert_param(IS_FDCAN_TT_INSTANCE(hfdcan->Instance));
  3932. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  3933. {
  3934. /* Check that the node is configured for external event-synchronized TT operation */
  3935. if ((hfdcan->ttcan->TTOCF & FDCAN_TTOCF_GEN) != FDCAN_TTOCF_GEN)
  3936. {
  3937. /* Update error code */
  3938. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_SUPPORTED;
  3939. return HAL_ERROR;
  3940. }
  3941. if ((hfdcan->ttcan->TTOCF & FDCAN_TTOCF_OM) != FDCAN_TT_COMMUNICATION_LEVEL0)
  3942. {
  3943. /* Wait until the LCKC bit into TTOCN register is reset */
  3944. while ((hfdcan->ttcan->TTOCN & FDCAN_TTOCN_LCKC) != 0U)
  3945. {
  3946. /* Check for the Timeout */
  3947. if (Counter > FDCAN_TIMEOUT_VALUE)
  3948. {
  3949. /* Update error code */
  3950. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  3951. /* Change FDCAN state */
  3952. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  3953. return HAL_ERROR;
  3954. }
  3955. /* Increment counter */
  3956. Counter++;
  3957. }
  3958. /* Set Next is Gap */
  3959. SET_BIT(hfdcan->ttcan->TTOCN, FDCAN_TTOCN_NIG);
  3960. /* Return function status */
  3961. return HAL_OK;
  3962. }
  3963. else
  3964. {
  3965. /* Update error code.
  3966. Feature not supported for TT Level 0 */
  3967. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_SUPPORTED;
  3968. return HAL_ERROR;
  3969. }
  3970. }
  3971. else
  3972. {
  3973. /* Update error code */
  3974. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  3975. return HAL_ERROR;
  3976. }
  3977. }
  3978. /**
  3979. * @brief Finish a Gap by requesting start of reference message.
  3980. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  3981. * the configuration information for the specified FDCAN.
  3982. * @retval HAL status
  3983. */
  3984. HAL_StatusTypeDef HAL_FDCAN_TT_SetEndOfGap(FDCAN_HandleTypeDef *hfdcan)
  3985. {
  3986. uint32_t Counter = 0U;
  3987. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  3988. /* Check function parameters */
  3989. assert_param(IS_FDCAN_TT_INSTANCE(hfdcan->Instance));
  3990. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  3991. {
  3992. /* Check that the node is configured for external event-synchronized TT operation */
  3993. if ((hfdcan->ttcan->TTOCF & FDCAN_TTOCF_GEN) != FDCAN_TTOCF_GEN)
  3994. {
  3995. /* Update error code */
  3996. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_SUPPORTED;
  3997. return HAL_ERROR;
  3998. }
  3999. if ((hfdcan->ttcan->TTOCF & FDCAN_TTOCF_OM) != FDCAN_TT_COMMUNICATION_LEVEL0)
  4000. {
  4001. /* Wait until the LCKC bit into TTOCN register is reset */
  4002. while ((hfdcan->ttcan->TTOCN & FDCAN_TTOCN_LCKC) != 0U)
  4003. {
  4004. /* Check for the Timeout */
  4005. if (Counter > FDCAN_TIMEOUT_VALUE)
  4006. {
  4007. /* Update error code */
  4008. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  4009. /* Change FDCAN state */
  4010. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  4011. return HAL_ERROR;
  4012. }
  4013. /* Increment counter */
  4014. Counter++;
  4015. }
  4016. /* Set Finish Gap */
  4017. SET_BIT(hfdcan->ttcan->TTOCN, FDCAN_TTOCN_FGP);
  4018. /* Return function status */
  4019. return HAL_OK;
  4020. }
  4021. else
  4022. {
  4023. /* Update error code.
  4024. Feature not supported for TT Level 0 */
  4025. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_SUPPORTED;
  4026. return HAL_ERROR;
  4027. }
  4028. }
  4029. else
  4030. {
  4031. /* Update error code */
  4032. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  4033. return HAL_ERROR;
  4034. }
  4035. }
  4036. /**
  4037. * @brief Configure target phase used for external synchronization by event
  4038. * trigger input pin fdcan1_evt.
  4039. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  4040. * the configuration information for the specified FDCAN.
  4041. * @param TargetPhase defines target value of cycle time when a rising edge
  4042. * of fdcan1_evt is expected.
  4043. * This parameter must be a number between 0 and 0xFFFF.
  4044. * @retval HAL status
  4045. */
  4046. HAL_StatusTypeDef HAL_FDCAN_TT_ConfigExternalSyncPhase(FDCAN_HandleTypeDef *hfdcan, uint32_t TargetPhase)
  4047. {
  4048. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  4049. /* Check function parameters */
  4050. assert_param(IS_FDCAN_TT_INSTANCE(hfdcan->Instance));
  4051. assert_param(IS_FDCAN_MAX_VALUE(TargetPhase, 0xFFFFU));
  4052. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  4053. {
  4054. /* Check that no external schedule synchronization is pending */
  4055. if ((hfdcan->ttcan->TTOCN & FDCAN_TTOCN_ESCN) == FDCAN_TTOCN_ESCN)
  4056. {
  4057. /* Update error code */
  4058. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_PENDING;
  4059. return HAL_ERROR;
  4060. }
  4061. /* Configure cycle time target phase */
  4062. MODIFY_REG(hfdcan->ttcan->TTGTP, FDCAN_TTGTP_CTP, (TargetPhase << FDCAN_TTGTP_CTP_Pos));
  4063. /* Return function status */
  4064. return HAL_OK;
  4065. }
  4066. else
  4067. {
  4068. /* Update error code */
  4069. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  4070. return HAL_ERROR;
  4071. }
  4072. }
  4073. /**
  4074. * @brief Synchronize the phase of the FDCAN schedule to an external schedule
  4075. * using event trigger input pin fdcan1_evt.
  4076. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  4077. * the configuration information for the specified FDCAN.
  4078. * @retval HAL status
  4079. */
  4080. HAL_StatusTypeDef HAL_FDCAN_TT_EnableExternalSynchronization(FDCAN_HandleTypeDef *hfdcan)
  4081. {
  4082. uint32_t Counter = 0U;
  4083. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  4084. /* Check function parameters */
  4085. assert_param(IS_FDCAN_TT_INSTANCE(hfdcan->Instance));
  4086. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  4087. {
  4088. /* Wait until the LCKC bit into TTOCN register is reset */
  4089. while ((hfdcan->ttcan->TTOCN & FDCAN_TTOCN_LCKC) != 0U)
  4090. {
  4091. /* Check for the Timeout */
  4092. if (Counter > FDCAN_TIMEOUT_VALUE)
  4093. {
  4094. /* Update error code */
  4095. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  4096. /* Change FDCAN state */
  4097. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  4098. return HAL_ERROR;
  4099. }
  4100. /* Increment counter */
  4101. Counter++;
  4102. }
  4103. /* Enable external synchronization */
  4104. SET_BIT(hfdcan->ttcan->TTOCN, FDCAN_TTOCN_ESCN);
  4105. /* Return function status */
  4106. return HAL_OK;
  4107. }
  4108. else
  4109. {
  4110. /* Update error code */
  4111. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  4112. return HAL_ERROR;
  4113. }
  4114. }
  4115. /**
  4116. * @brief Disable external schedule synchronization.
  4117. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  4118. * the configuration information for the specified FDCAN.
  4119. * @retval HAL status
  4120. */
  4121. HAL_StatusTypeDef HAL_FDCAN_TT_DisableExternalSynchronization(FDCAN_HandleTypeDef *hfdcan)
  4122. {
  4123. uint32_t Counter = 0U;
  4124. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  4125. /* Check function parameters */
  4126. assert_param(IS_FDCAN_TT_INSTANCE(hfdcan->Instance));
  4127. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  4128. {
  4129. /* Wait until the LCKC bit into TTOCN register is reset */
  4130. while ((hfdcan->ttcan->TTOCN & FDCAN_TTOCN_LCKC) != 0U)
  4131. {
  4132. /* Check for the Timeout */
  4133. if (Counter > FDCAN_TIMEOUT_VALUE)
  4134. {
  4135. /* Update error code */
  4136. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_TIMEOUT;
  4137. /* Change FDCAN state */
  4138. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  4139. return HAL_ERROR;
  4140. }
  4141. /* Increment counter */
  4142. Counter++;
  4143. }
  4144. /* Disable external synchronization */
  4145. CLEAR_BIT(hfdcan->ttcan->TTOCN, FDCAN_TTOCN_ESCN);
  4146. /* Return function status */
  4147. return HAL_OK;
  4148. }
  4149. else
  4150. {
  4151. /* Update error code */
  4152. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  4153. return HAL_ERROR;
  4154. }
  4155. }
  4156. /**
  4157. * @brief Get TT operation status.
  4158. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  4159. * the configuration information for the specified FDCAN.
  4160. * @param TTOpStatus pointer to an FDCAN_TTOperationStatusTypeDef structure.
  4161. * @retval HAL status
  4162. */
  4163. HAL_StatusTypeDef HAL_FDCAN_TT_GetOperationStatus(FDCAN_HandleTypeDef *hfdcan, FDCAN_TTOperationStatusTypeDef *TTOpStatus)
  4164. {
  4165. uint32_t TTStatusReg;
  4166. /* Check function parameters */
  4167. assert_param(IS_FDCAN_TT_INSTANCE(hfdcan->Instance));
  4168. /* Read the TT operation status register */
  4169. TTStatusReg = READ_REG(hfdcan->ttcan->TTOST);
  4170. /* Fill the TT operation status structure */
  4171. TTOpStatus->ErrorLevel = (TTStatusReg & FDCAN_TTOST_EL);
  4172. TTOpStatus->MasterState = (TTStatusReg & FDCAN_TTOST_MS);
  4173. TTOpStatus->SyncState = (TTStatusReg & FDCAN_TTOST_SYS);
  4174. TTOpStatus->GTimeQuality = ((TTStatusReg & FDCAN_TTOST_QGTP) >> FDCAN_TTOST_QGTP_Pos);
  4175. TTOpStatus->ClockQuality = ((TTStatusReg & FDCAN_TTOST_QCS) >> FDCAN_TTOST_QCS_Pos);
  4176. TTOpStatus->RefTrigOffset = ((TTStatusReg & FDCAN_TTOST_RTO) >> FDCAN_TTOST_RTO_Pos);
  4177. TTOpStatus->GTimeDiscPending = ((TTStatusReg & FDCAN_TTOST_WGTD) >> FDCAN_TTOST_WGTD_Pos);
  4178. TTOpStatus->GapFinished = ((TTStatusReg & FDCAN_TTOST_GFI) >> FDCAN_TTOST_GFI_Pos);
  4179. TTOpStatus->MasterPriority = ((TTStatusReg & FDCAN_TTOST_TMP) >> FDCAN_TTOST_TMP_Pos);
  4180. TTOpStatus->GapStarted = ((TTStatusReg & FDCAN_TTOST_GSI) >> FDCAN_TTOST_GSI_Pos);
  4181. TTOpStatus->WaitForEvt = ((TTStatusReg & FDCAN_TTOST_WFE) >> FDCAN_TTOST_WFE_Pos);
  4182. TTOpStatus->AppWdgEvt = ((TTStatusReg & FDCAN_TTOST_AWE) >> FDCAN_TTOST_AWE_Pos);
  4183. TTOpStatus->ECSPending = ((TTStatusReg & FDCAN_TTOST_WECS) >> FDCAN_TTOST_WECS_Pos);
  4184. TTOpStatus->PhaseLock = ((TTStatusReg & FDCAN_TTOST_SPL) >> FDCAN_TTOST_SPL_Pos);
  4185. /* Return function status */
  4186. return HAL_OK;
  4187. }
  4188. /**
  4189. * @}
  4190. */
  4191. /** @defgroup FDCAN_Exported_Functions_Group5 Interrupts management
  4192. * @brief Interrupts management
  4193. *
  4194. @verbatim
  4195. ==============================================================================
  4196. ##### Interrupts management #####
  4197. ==============================================================================
  4198. [..] This section provides functions allowing to:
  4199. (+) HAL_FDCAN_ConfigInterruptLines : Assign interrupts to either Interrupt line 0 or 1
  4200. (+) HAL_FDCAN_TT_ConfigInterruptLines : Assign TT interrupts to either Interrupt line 0 or 1
  4201. (+) HAL_FDCAN_ActivateNotification : Enable interrupts
  4202. (+) HAL_FDCAN_DeactivateNotification : Disable interrupts
  4203. (+) HAL_FDCAN_TT_ActivateNotification : Enable TT interrupts
  4204. (+) HAL_FDCAN_TT_DeactivateNotification : Disable TT interrupts
  4205. (+) HAL_FDCAN_IRQHandler : Handles FDCAN interrupt request
  4206. @endverbatim
  4207. * @{
  4208. */
  4209. /**
  4210. * @brief Assign interrupts to either Interrupt line 0 or 1.
  4211. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  4212. * the configuration information for the specified FDCAN.
  4213. * @param ITList indicates which interrupts will be assigned to the selected interrupt line.
  4214. * This parameter can be any combination of @arg FDCAN_Interrupts.
  4215. * @param InterruptLine Interrupt line.
  4216. * This parameter can be a value of @arg FDCAN_Interrupt_Line.
  4217. * @retval HAL status
  4218. */
  4219. HAL_StatusTypeDef HAL_FDCAN_ConfigInterruptLines(FDCAN_HandleTypeDef *hfdcan, uint32_t ITList, uint32_t InterruptLine)
  4220. {
  4221. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  4222. /* Check function parameters */
  4223. assert_param(IS_FDCAN_IT(ITList));
  4224. assert_param(IS_FDCAN_IT_LINE(InterruptLine));
  4225. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  4226. {
  4227. /* Assign list of interrupts to the selected line */
  4228. if (InterruptLine == FDCAN_INTERRUPT_LINE0)
  4229. {
  4230. CLEAR_BIT(hfdcan->Instance->ILS, ITList);
  4231. }
  4232. else /* InterruptLine == FDCAN_INTERRUPT_LINE1 */
  4233. {
  4234. SET_BIT(hfdcan->Instance->ILS, ITList);
  4235. }
  4236. /* Return function status */
  4237. return HAL_OK;
  4238. }
  4239. else
  4240. {
  4241. /* Update error code */
  4242. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  4243. return HAL_ERROR;
  4244. }
  4245. }
  4246. /**
  4247. * @brief Assign TT interrupts to either Interrupt line 0 or 1.
  4248. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  4249. * the configuration information for the specified FDCAN.
  4250. * @param TTITList indicates which interrupts will be assigned to the selected interrupt line.
  4251. * This parameter can be any combination of @arg FDCAN_TTInterrupts.
  4252. * @param InterruptLine Interrupt line.
  4253. * This parameter can be a value of @arg FDCAN_Interrupt_Line.
  4254. * @retval HAL status
  4255. */
  4256. HAL_StatusTypeDef HAL_FDCAN_TT_ConfigInterruptLines(FDCAN_HandleTypeDef *hfdcan, uint32_t TTITList, uint32_t InterruptLine)
  4257. {
  4258. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  4259. /* Check function parameters */
  4260. assert_param(IS_FDCAN_TT_INSTANCE(hfdcan->Instance));
  4261. assert_param(IS_FDCAN_TT_IT(TTITList));
  4262. assert_param(IS_FDCAN_IT_LINE(InterruptLine));
  4263. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  4264. {
  4265. /* Assign list of interrupts to the selected line */
  4266. if (InterruptLine == FDCAN_INTERRUPT_LINE0)
  4267. {
  4268. CLEAR_BIT(hfdcan->ttcan->TTILS, TTITList);
  4269. }
  4270. else /* InterruptLine == FDCAN_INTERRUPT_LINE1 */
  4271. {
  4272. SET_BIT(hfdcan->ttcan->TTILS, TTITList);
  4273. }
  4274. /* Return function status */
  4275. return HAL_OK;
  4276. }
  4277. else
  4278. {
  4279. /* Update error code */
  4280. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  4281. return HAL_ERROR;
  4282. }
  4283. }
  4284. /**
  4285. * @brief Enable interrupts.
  4286. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  4287. * the configuration information for the specified FDCAN.
  4288. * @param ActiveITs indicates which interrupts will be enabled.
  4289. * This parameter can be any combination of @arg FDCAN_Interrupts.
  4290. * @param BufferIndexes Tx Buffer Indexes.
  4291. * This parameter can be any combination of @arg FDCAN_Tx_location.
  4292. * This parameter is ignored if ActiveITs does not include one of the following:
  4293. * - FDCAN_IT_TX_COMPLETE
  4294. * - FDCAN_IT_TX_ABORT_COMPLETE
  4295. * @retval HAL status
  4296. */
  4297. HAL_StatusTypeDef HAL_FDCAN_ActivateNotification(FDCAN_HandleTypeDef *hfdcan, uint32_t ActiveITs, uint32_t BufferIndexes)
  4298. {
  4299. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  4300. /* Check function parameters */
  4301. assert_param(IS_FDCAN_IT(ActiveITs));
  4302. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  4303. {
  4304. /* Enable Interrupt lines */
  4305. if ((ActiveITs & hfdcan->Instance->ILS) == 0U)
  4306. {
  4307. /* Enable Interrupt line 0 */
  4308. SET_BIT(hfdcan->Instance->ILE, FDCAN_INTERRUPT_LINE0);
  4309. }
  4310. else if ((ActiveITs & hfdcan->Instance->ILS) == ActiveITs)
  4311. {
  4312. /* Enable Interrupt line 1 */
  4313. SET_BIT(hfdcan->Instance->ILE, FDCAN_INTERRUPT_LINE1);
  4314. }
  4315. else
  4316. {
  4317. /* Enable Interrupt lines 0 and 1 */
  4318. hfdcan->Instance->ILE = (FDCAN_INTERRUPT_LINE0 | FDCAN_INTERRUPT_LINE1);
  4319. }
  4320. if ((ActiveITs & FDCAN_IT_TX_COMPLETE) != 0U)
  4321. {
  4322. /* Enable Tx Buffer Transmission Interrupt to set TC flag in IR register,
  4323. but interrupt will only occure if TC is enabled in IE register */
  4324. SET_BIT(hfdcan->Instance->TXBTIE, BufferIndexes);
  4325. }
  4326. if ((ActiveITs & FDCAN_IT_TX_ABORT_COMPLETE) != 0U)
  4327. {
  4328. /* Enable Tx Buffer Cancellation Finished Interrupt to set TCF flag in IR register,
  4329. but interrupt will only occure if TCF is enabled in IE register */
  4330. SET_BIT(hfdcan->Instance->TXBCIE, BufferIndexes);
  4331. }
  4332. /* Enable the selected interrupts */
  4333. __HAL_FDCAN_ENABLE_IT(hfdcan, ActiveITs);
  4334. /* Return function status */
  4335. return HAL_OK;
  4336. }
  4337. else
  4338. {
  4339. /* Update error code */
  4340. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  4341. return HAL_ERROR;
  4342. }
  4343. }
  4344. /**
  4345. * @brief Disable interrupts.
  4346. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  4347. * the configuration information for the specified FDCAN.
  4348. * @param InactiveITs indicates which interrupts will be disabled.
  4349. * This parameter can be any combination of @arg FDCAN_Interrupts.
  4350. * @retval HAL status
  4351. */
  4352. HAL_StatusTypeDef HAL_FDCAN_DeactivateNotification(FDCAN_HandleTypeDef *hfdcan, uint32_t InactiveITs)
  4353. {
  4354. uint32_t ITLineSelection;
  4355. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  4356. /* Check function parameters */
  4357. assert_param(IS_FDCAN_IT(InactiveITs));
  4358. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  4359. {
  4360. /* Disable the selected interrupts */
  4361. __HAL_FDCAN_DISABLE_IT(hfdcan, InactiveITs);
  4362. if ((InactiveITs & FDCAN_IT_TX_COMPLETE) != 0U)
  4363. {
  4364. /* Disable Tx Buffer Transmission Interrupts */
  4365. CLEAR_REG(hfdcan->Instance->TXBTIE);
  4366. }
  4367. if ((InactiveITs & FDCAN_IT_TX_ABORT_COMPLETE) != 0U)
  4368. {
  4369. /* Disable Tx Buffer Cancellation Finished Interrupt */
  4370. CLEAR_REG(hfdcan->Instance->TXBCIE);
  4371. }
  4372. ITLineSelection = hfdcan->Instance->ILS;
  4373. if ((hfdcan->Instance->IE | ITLineSelection) == ITLineSelection)
  4374. {
  4375. /* Disable Interrupt line 0 */
  4376. CLEAR_BIT(hfdcan->Instance->ILE, FDCAN_INTERRUPT_LINE0);
  4377. }
  4378. if ((hfdcan->Instance->IE & ITLineSelection) == 0U)
  4379. {
  4380. /* Disable Interrupt line 1 */
  4381. CLEAR_BIT(hfdcan->Instance->ILE, FDCAN_INTERRUPT_LINE1);
  4382. }
  4383. /* Return function status */
  4384. return HAL_OK;
  4385. }
  4386. else
  4387. {
  4388. /* Update error code */
  4389. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  4390. return HAL_ERROR;
  4391. }
  4392. }
  4393. /**
  4394. * @brief Enable TT interrupts.
  4395. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  4396. * the configuration information for the specified FDCAN.
  4397. * @param ActiveTTITs indicates which TT interrupts will be enabled.
  4398. * This parameter can be any combination of @arg FDCAN_TTInterrupts.
  4399. * @retval HAL status
  4400. */
  4401. HAL_StatusTypeDef HAL_FDCAN_TT_ActivateNotification(FDCAN_HandleTypeDef *hfdcan, uint32_t ActiveTTITs)
  4402. {
  4403. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  4404. /* Check function parameters */
  4405. assert_param(IS_FDCAN_TT_INSTANCE(hfdcan->Instance));
  4406. assert_param(IS_FDCAN_TT_IT(ActiveTTITs));
  4407. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  4408. {
  4409. /* Enable Interrupt lines */
  4410. if ((ActiveTTITs & hfdcan->ttcan->TTILS) == 0U)
  4411. {
  4412. /* Enable Interrupt line 0 */
  4413. SET_BIT(hfdcan->Instance->ILE, FDCAN_INTERRUPT_LINE0);
  4414. }
  4415. else if ((ActiveTTITs & hfdcan->ttcan->TTILS) == ActiveTTITs)
  4416. {
  4417. /* Enable Interrupt line 1 */
  4418. SET_BIT(hfdcan->Instance->ILE, FDCAN_INTERRUPT_LINE1);
  4419. }
  4420. else
  4421. {
  4422. /* Enable Interrupt lines 0 and 1 */
  4423. hfdcan->Instance->ILE = (FDCAN_INTERRUPT_LINE0 | FDCAN_INTERRUPT_LINE1);
  4424. }
  4425. /* Enable the selected TT interrupts */
  4426. __HAL_FDCAN_TT_ENABLE_IT(hfdcan, ActiveTTITs);
  4427. /* Return function status */
  4428. return HAL_OK;
  4429. }
  4430. else
  4431. {
  4432. /* Update error code */
  4433. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  4434. return HAL_ERROR;
  4435. }
  4436. }
  4437. /**
  4438. * @brief Disable TT interrupts.
  4439. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  4440. * the configuration information for the specified FDCAN.
  4441. * @param InactiveTTITs indicates which TT interrupts will be disabled.
  4442. * This parameter can be any combination of @arg FDCAN_TTInterrupts.
  4443. * @retval HAL status
  4444. */
  4445. HAL_StatusTypeDef HAL_FDCAN_TT_DeactivateNotification(FDCAN_HandleTypeDef *hfdcan, uint32_t InactiveTTITs)
  4446. {
  4447. uint32_t ITLineSelection;
  4448. HAL_FDCAN_StateTypeDef state = hfdcan->State;
  4449. /* Check function parameters */
  4450. assert_param(IS_FDCAN_TT_INSTANCE(hfdcan->Instance));
  4451. assert_param(IS_FDCAN_TT_IT(InactiveTTITs));
  4452. if ((state == HAL_FDCAN_STATE_READY) || (state == HAL_FDCAN_STATE_BUSY))
  4453. {
  4454. /* Disable the selected TT interrupts */
  4455. __HAL_FDCAN_TT_DISABLE_IT(hfdcan, InactiveTTITs);
  4456. ITLineSelection = hfdcan->ttcan->TTILS;
  4457. if ((hfdcan->ttcan->TTIE | ITLineSelection) == ITLineSelection)
  4458. {
  4459. /* Disable Interrupt line 0 */
  4460. CLEAR_BIT(hfdcan->Instance->ILE, FDCAN_INTERRUPT_LINE0);
  4461. }
  4462. if ((hfdcan->ttcan->TTIE & ITLineSelection) == 0U)
  4463. {
  4464. /* Disable Interrupt line 1 */
  4465. CLEAR_BIT(hfdcan->Instance->ILE, FDCAN_INTERRUPT_LINE1);
  4466. }
  4467. /* Return function status */
  4468. return HAL_OK;
  4469. }
  4470. else
  4471. {
  4472. /* Update error code */
  4473. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_INITIALIZED;
  4474. return HAL_ERROR;
  4475. }
  4476. }
  4477. /**
  4478. * @brief Handles FDCAN interrupt request.
  4479. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  4480. * the configuration information for the specified FDCAN.
  4481. * @retval HAL status
  4482. */
  4483. void HAL_FDCAN_IRQHandler(FDCAN_HandleTypeDef *hfdcan)
  4484. {
  4485. uint32_t ClkCalibrationITs;
  4486. uint32_t TxEventFifoITs;
  4487. uint32_t RxFifo0ITs;
  4488. uint32_t RxFifo1ITs;
  4489. uint32_t Errors;
  4490. uint32_t ErrorStatusITs;
  4491. uint32_t TransmittedBuffers;
  4492. uint32_t AbortedBuffers;
  4493. uint32_t TTSchedSyncITs;
  4494. uint32_t TTTimeMarkITs;
  4495. uint32_t TTGlobTimeITs;
  4496. uint32_t TTDistErrors;
  4497. uint32_t TTFatalErrors;
  4498. uint32_t SWTime;
  4499. uint32_t SWCycleCount;
  4500. ClkCalibrationITs = (FDCAN_CCU->IR << 30);
  4501. ClkCalibrationITs &= (FDCAN_CCU->IE << 30);
  4502. TxEventFifoITs = hfdcan->Instance->IR & FDCAN_TX_EVENT_FIFO_MASK;
  4503. TxEventFifoITs &= hfdcan->Instance->IE;
  4504. RxFifo0ITs = hfdcan->Instance->IR & FDCAN_RX_FIFO0_MASK;
  4505. RxFifo0ITs &= hfdcan->Instance->IE;
  4506. RxFifo1ITs = hfdcan->Instance->IR & FDCAN_RX_FIFO1_MASK;
  4507. RxFifo1ITs &= hfdcan->Instance->IE;
  4508. Errors = hfdcan->Instance->IR & FDCAN_ERROR_MASK;
  4509. Errors &= hfdcan->Instance->IE;
  4510. ErrorStatusITs = hfdcan->Instance->IR & FDCAN_ERROR_STATUS_MASK;
  4511. ErrorStatusITs &= hfdcan->Instance->IE;
  4512. /* High Priority Message interrupt management *******************************/
  4513. if (__HAL_FDCAN_GET_IT_SOURCE(hfdcan, FDCAN_IT_RX_HIGH_PRIORITY_MSG) != 0U)
  4514. {
  4515. if (__HAL_FDCAN_GET_FLAG(hfdcan, FDCAN_FLAG_RX_HIGH_PRIORITY_MSG) != 0U)
  4516. {
  4517. /* Clear the High Priority Message flag */
  4518. __HAL_FDCAN_CLEAR_FLAG(hfdcan, FDCAN_FLAG_RX_HIGH_PRIORITY_MSG);
  4519. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  4520. /* Call registered callback*/
  4521. hfdcan->HighPriorityMessageCallback(hfdcan);
  4522. #else
  4523. /* High Priority Message Callback */
  4524. HAL_FDCAN_HighPriorityMessageCallback(hfdcan);
  4525. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  4526. }
  4527. }
  4528. /* Transmission Abort interrupt management **********************************/
  4529. if (__HAL_FDCAN_GET_IT_SOURCE(hfdcan, FDCAN_IT_TX_ABORT_COMPLETE) != 0U)
  4530. {
  4531. if (__HAL_FDCAN_GET_FLAG(hfdcan, FDCAN_FLAG_TX_ABORT_COMPLETE) != 0U)
  4532. {
  4533. /* List of aborted monitored buffers */
  4534. AbortedBuffers = hfdcan->Instance->TXBCF;
  4535. AbortedBuffers &= hfdcan->Instance->TXBCIE;
  4536. /* Clear the Transmission Cancellation flag */
  4537. __HAL_FDCAN_CLEAR_FLAG(hfdcan, FDCAN_FLAG_TX_ABORT_COMPLETE);
  4538. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  4539. /* Call registered callback*/
  4540. hfdcan->TxBufferAbortCallback(hfdcan, AbortedBuffers);
  4541. #else
  4542. /* Transmission Cancellation Callback */
  4543. HAL_FDCAN_TxBufferAbortCallback(hfdcan, AbortedBuffers);
  4544. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  4545. }
  4546. }
  4547. /* Clock calibration unit interrupts management *****************************/
  4548. if (ClkCalibrationITs != 0U)
  4549. {
  4550. /* Clear the Clock Calibration flags */
  4551. __HAL_FDCAN_CLEAR_FLAG(hfdcan, ClkCalibrationITs);
  4552. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  4553. /* Call registered callback*/
  4554. hfdcan->ClockCalibrationCallback(hfdcan, ClkCalibrationITs);
  4555. #else
  4556. /* Clock Calibration Callback */
  4557. HAL_FDCAN_ClockCalibrationCallback(hfdcan, ClkCalibrationITs);
  4558. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  4559. }
  4560. /* Tx event FIFO interrupts management **************************************/
  4561. if (TxEventFifoITs != 0U)
  4562. {
  4563. /* Clear the Tx Event FIFO flags */
  4564. __HAL_FDCAN_CLEAR_FLAG(hfdcan, TxEventFifoITs);
  4565. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  4566. /* Call registered callback*/
  4567. hfdcan->TxEventFifoCallback(hfdcan, TxEventFifoITs);
  4568. #else
  4569. /* Tx Event FIFO Callback */
  4570. HAL_FDCAN_TxEventFifoCallback(hfdcan, TxEventFifoITs);
  4571. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  4572. }
  4573. /* Rx FIFO 0 interrupts management ******************************************/
  4574. if (RxFifo0ITs != 0U)
  4575. {
  4576. /* Clear the Rx FIFO 0 flags */
  4577. __HAL_FDCAN_CLEAR_FLAG(hfdcan, RxFifo0ITs);
  4578. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  4579. /* Call registered callback*/
  4580. hfdcan->RxFifo0Callback(hfdcan, RxFifo0ITs);
  4581. #else
  4582. /* Rx FIFO 0 Callback */
  4583. HAL_FDCAN_RxFifo0Callback(hfdcan, RxFifo0ITs);
  4584. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  4585. }
  4586. /* Rx FIFO 1 interrupts management ******************************************/
  4587. if (RxFifo1ITs != 0U)
  4588. {
  4589. /* Clear the Rx FIFO 1 flags */
  4590. __HAL_FDCAN_CLEAR_FLAG(hfdcan, RxFifo1ITs);
  4591. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  4592. /* Call registered callback*/
  4593. hfdcan->RxFifo1Callback(hfdcan, RxFifo1ITs);
  4594. #else
  4595. /* Rx FIFO 1 Callback */
  4596. HAL_FDCAN_RxFifo1Callback(hfdcan, RxFifo1ITs);
  4597. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  4598. }
  4599. /* Tx FIFO empty interrupt management ***************************************/
  4600. if (__HAL_FDCAN_GET_IT_SOURCE(hfdcan, FDCAN_IT_TX_FIFO_EMPTY) != 0U)
  4601. {
  4602. if (__HAL_FDCAN_GET_FLAG(hfdcan, FDCAN_FLAG_TX_FIFO_EMPTY) != 0U)
  4603. {
  4604. /* Clear the Tx FIFO empty flag */
  4605. __HAL_FDCAN_CLEAR_FLAG(hfdcan, FDCAN_FLAG_TX_FIFO_EMPTY);
  4606. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  4607. /* Call registered callback*/
  4608. hfdcan->TxFifoEmptyCallback(hfdcan);
  4609. #else
  4610. /* Tx FIFO empty Callback */
  4611. HAL_FDCAN_TxFifoEmptyCallback(hfdcan);
  4612. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  4613. }
  4614. }
  4615. /* Transmission Complete interrupt management *******************************/
  4616. if (__HAL_FDCAN_GET_IT_SOURCE(hfdcan, FDCAN_IT_TX_COMPLETE) != 0U)
  4617. {
  4618. if (__HAL_FDCAN_GET_FLAG(hfdcan, FDCAN_FLAG_TX_COMPLETE) != 0U)
  4619. {
  4620. /* List of transmitted monitored buffers */
  4621. TransmittedBuffers = hfdcan->Instance->TXBTO;
  4622. TransmittedBuffers &= hfdcan->Instance->TXBTIE;
  4623. /* Clear the Transmission Complete flag */
  4624. __HAL_FDCAN_CLEAR_FLAG(hfdcan, FDCAN_FLAG_TX_COMPLETE);
  4625. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  4626. /* Call registered callback*/
  4627. hfdcan->TxBufferCompleteCallback(hfdcan, TransmittedBuffers);
  4628. #else
  4629. /* Transmission Complete Callback */
  4630. HAL_FDCAN_TxBufferCompleteCallback(hfdcan, TransmittedBuffers);
  4631. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  4632. }
  4633. }
  4634. /* Rx Buffer New Message interrupt management *******************************/
  4635. if (__HAL_FDCAN_GET_IT_SOURCE(hfdcan, FDCAN_IT_RX_BUFFER_NEW_MESSAGE) != 0U)
  4636. {
  4637. if (__HAL_FDCAN_GET_FLAG(hfdcan, FDCAN_FLAG_RX_BUFFER_NEW_MESSAGE) != 0U)
  4638. {
  4639. /* Clear the Rx Buffer New Message flag */
  4640. __HAL_FDCAN_CLEAR_FLAG(hfdcan, FDCAN_FLAG_RX_BUFFER_NEW_MESSAGE);
  4641. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  4642. /* Call registered callback*/
  4643. hfdcan->RxBufferNewMessageCallback(hfdcan);
  4644. #else
  4645. /* Rx Buffer New Message Callback */
  4646. HAL_FDCAN_RxBufferNewMessageCallback(hfdcan);
  4647. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  4648. }
  4649. }
  4650. /* Timestamp Wraparound interrupt management ********************************/
  4651. if (__HAL_FDCAN_GET_IT_SOURCE(hfdcan, FDCAN_IT_TIMESTAMP_WRAPAROUND) != 0U)
  4652. {
  4653. if (__HAL_FDCAN_GET_FLAG(hfdcan, FDCAN_FLAG_TIMESTAMP_WRAPAROUND) != 0U)
  4654. {
  4655. /* Clear the Timestamp Wraparound flag */
  4656. __HAL_FDCAN_CLEAR_FLAG(hfdcan, FDCAN_FLAG_TIMESTAMP_WRAPAROUND);
  4657. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  4658. /* Call registered callback*/
  4659. hfdcan->TimestampWraparoundCallback(hfdcan);
  4660. #else
  4661. /* Timestamp Wraparound Callback */
  4662. HAL_FDCAN_TimestampWraparoundCallback(hfdcan);
  4663. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  4664. }
  4665. }
  4666. /* Timeout Occurred interrupt management ************************************/
  4667. if (__HAL_FDCAN_GET_IT_SOURCE(hfdcan, FDCAN_IT_TIMEOUT_OCCURRED) != 0U)
  4668. {
  4669. if (__HAL_FDCAN_GET_FLAG(hfdcan, FDCAN_FLAG_TIMEOUT_OCCURRED) != 0U)
  4670. {
  4671. /* Clear the Timeout Occurred flag */
  4672. __HAL_FDCAN_CLEAR_FLAG(hfdcan, FDCAN_FLAG_TIMEOUT_OCCURRED);
  4673. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  4674. /* Call registered callback*/
  4675. hfdcan->TimeoutOccurredCallback(hfdcan);
  4676. #else
  4677. /* Timeout Occurred Callback */
  4678. HAL_FDCAN_TimeoutOccurredCallback(hfdcan);
  4679. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  4680. }
  4681. }
  4682. /* Message RAM access failure interrupt management **************************/
  4683. if (__HAL_FDCAN_GET_IT_SOURCE(hfdcan, FDCAN_IT_RAM_ACCESS_FAILURE) != 0U)
  4684. {
  4685. if (__HAL_FDCAN_GET_FLAG(hfdcan, FDCAN_FLAG_RAM_ACCESS_FAILURE) != 0U)
  4686. {
  4687. /* Clear the Message RAM access failure flag */
  4688. __HAL_FDCAN_CLEAR_FLAG(hfdcan, FDCAN_FLAG_RAM_ACCESS_FAILURE);
  4689. /* Update error code */
  4690. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_RAM_ACCESS;
  4691. }
  4692. }
  4693. /* Error Status interrupts management ***************************************/
  4694. if (ErrorStatusITs != 0U)
  4695. {
  4696. /* Clear the Error flags */
  4697. __HAL_FDCAN_CLEAR_FLAG(hfdcan, ErrorStatusITs);
  4698. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  4699. /* Call registered callback*/
  4700. hfdcan->ErrorStatusCallback(hfdcan, ErrorStatusITs);
  4701. #else
  4702. /* Error Status Callback */
  4703. HAL_FDCAN_ErrorStatusCallback(hfdcan, ErrorStatusITs);
  4704. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  4705. }
  4706. /* Error interrupts management **********************************************/
  4707. if (Errors != 0U)
  4708. {
  4709. /* Clear the Error flags */
  4710. __HAL_FDCAN_CLEAR_FLAG(hfdcan, Errors);
  4711. /* Update error code */
  4712. hfdcan->ErrorCode |= Errors;
  4713. }
  4714. if (hfdcan->Instance == FDCAN1)
  4715. {
  4716. if ((hfdcan->ttcan->TTOCF & FDCAN_TTOCF_OM) != 0U)
  4717. {
  4718. TTSchedSyncITs = hfdcan->ttcan->TTIR & FDCAN_TT_SCHEDULE_SYNC_MASK;
  4719. TTSchedSyncITs &= hfdcan->ttcan->TTIE;
  4720. TTTimeMarkITs = hfdcan->ttcan->TTIR & FDCAN_TT_TIME_MARK_MASK;
  4721. TTTimeMarkITs &= hfdcan->ttcan->TTIE;
  4722. TTGlobTimeITs = hfdcan->ttcan->TTIR & FDCAN_TT_GLOBAL_TIME_MASK;
  4723. TTGlobTimeITs &= hfdcan->ttcan->TTIE;
  4724. TTDistErrors = hfdcan->ttcan->TTIR & FDCAN_TT_DISTURBING_ERROR_MASK;
  4725. TTDistErrors &= hfdcan->ttcan->TTIE;
  4726. TTFatalErrors = hfdcan->ttcan->TTIR & FDCAN_TT_FATAL_ERROR_MASK;
  4727. TTFatalErrors &= hfdcan->ttcan->TTIE;
  4728. /* TT Schedule Synchronization interrupts management **********************/
  4729. if (TTSchedSyncITs != 0U)
  4730. {
  4731. /* Clear the TT Schedule Synchronization flags */
  4732. __HAL_FDCAN_TT_CLEAR_FLAG(hfdcan, TTSchedSyncITs);
  4733. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  4734. /* Call registered callback*/
  4735. hfdcan->TT_ScheduleSyncCallback(hfdcan, TTSchedSyncITs);
  4736. #else
  4737. /* TT Schedule Synchronization Callback */
  4738. HAL_FDCAN_TT_ScheduleSyncCallback(hfdcan, TTSchedSyncITs);
  4739. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  4740. }
  4741. /* TT Time Mark interrupts management *************************************/
  4742. if (TTTimeMarkITs != 0U)
  4743. {
  4744. /* Clear the TT Time Mark flags */
  4745. __HAL_FDCAN_TT_CLEAR_FLAG(hfdcan, TTTimeMarkITs);
  4746. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  4747. /* Call registered callback*/
  4748. hfdcan->TT_TimeMarkCallback(hfdcan, TTTimeMarkITs);
  4749. #else
  4750. /* TT Time Mark Callback */
  4751. HAL_FDCAN_TT_TimeMarkCallback(hfdcan, TTTimeMarkITs);
  4752. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  4753. }
  4754. /* TT Stop Watch interrupt management *************************************/
  4755. if (__HAL_FDCAN_TT_GET_IT_SOURCE(hfdcan, FDCAN_TT_IT_STOP_WATCH) != 0U)
  4756. {
  4757. if (__HAL_FDCAN_TT_GET_FLAG(hfdcan, FDCAN_TT_FLAG_STOP_WATCH) != 0U)
  4758. {
  4759. /* Retrieve Stop watch Time and Cycle count */
  4760. SWTime = ((hfdcan->ttcan->TTCPT & FDCAN_TTCPT_SWV) >> FDCAN_TTCPT_SWV_Pos);
  4761. SWCycleCount = ((hfdcan->ttcan->TTCPT & FDCAN_TTCPT_CCV) >> FDCAN_TTCPT_CCV_Pos);
  4762. /* Clear the TT Stop Watch flag */
  4763. __HAL_FDCAN_TT_CLEAR_FLAG(hfdcan, FDCAN_TT_FLAG_STOP_WATCH);
  4764. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  4765. /* Call registered callback*/
  4766. hfdcan->TT_StopWatchCallback(hfdcan, SWTime, SWCycleCount);
  4767. #else
  4768. /* TT Stop Watch Callback */
  4769. HAL_FDCAN_TT_StopWatchCallback(hfdcan, SWTime, SWCycleCount);
  4770. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  4771. }
  4772. }
  4773. /* TT Global Time interrupts management ***********************************/
  4774. if (TTGlobTimeITs != 0U)
  4775. {
  4776. /* Clear the TT Global Time flags */
  4777. __HAL_FDCAN_TT_CLEAR_FLAG(hfdcan, TTGlobTimeITs);
  4778. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  4779. /* Call registered callback*/
  4780. hfdcan->TT_GlobalTimeCallback(hfdcan, TTGlobTimeITs);
  4781. #else
  4782. /* TT Global Time Callback */
  4783. HAL_FDCAN_TT_GlobalTimeCallback(hfdcan, TTGlobTimeITs);
  4784. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  4785. }
  4786. /* TT Disturbing Error interrupts management ******************************/
  4787. if (TTDistErrors != 0U)
  4788. {
  4789. /* Clear the TT Disturbing Error flags */
  4790. __HAL_FDCAN_TT_CLEAR_FLAG(hfdcan, TTDistErrors);
  4791. /* Update error code */
  4792. hfdcan->ErrorCode |= TTDistErrors;
  4793. }
  4794. /* TT Fatal Error interrupts management ***********************************/
  4795. if (TTFatalErrors != 0U)
  4796. {
  4797. /* Clear the TT Fatal Error flags */
  4798. __HAL_FDCAN_TT_CLEAR_FLAG(hfdcan, TTFatalErrors);
  4799. /* Update error code */
  4800. hfdcan->ErrorCode |= TTFatalErrors;
  4801. }
  4802. }
  4803. }
  4804. if (hfdcan->ErrorCode != HAL_FDCAN_ERROR_NONE)
  4805. {
  4806. #if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
  4807. /* Call registered callback*/
  4808. hfdcan->ErrorCallback(hfdcan);
  4809. #else
  4810. /* Error Callback */
  4811. HAL_FDCAN_ErrorCallback(hfdcan);
  4812. #endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  4813. }
  4814. }
  4815. /**
  4816. * @}
  4817. */
  4818. /** @defgroup FDCAN_Exported_Functions_Group6 Callback functions
  4819. * @brief FDCAN Callback functions
  4820. *
  4821. @verbatim
  4822. ==============================================================================
  4823. ##### Callback functions #####
  4824. ==============================================================================
  4825. [..]
  4826. This subsection provides the following callback functions:
  4827. (+) HAL_FDCAN_ClockCalibrationCallback
  4828. (+) HAL_FDCAN_TxEventFifoCallback
  4829. (+) HAL_FDCAN_RxFifo0Callback
  4830. (+) HAL_FDCAN_RxFifo1Callback
  4831. (+) HAL_FDCAN_TxFifoEmptyCallback
  4832. (+) HAL_FDCAN_TxBufferCompleteCallback
  4833. (+) HAL_FDCAN_TxBufferAbortCallback
  4834. (+) HAL_FDCAN_RxBufferNewMessageCallback
  4835. (+) HAL_FDCAN_HighPriorityMessageCallback
  4836. (+) HAL_FDCAN_TimestampWraparoundCallback
  4837. (+) HAL_FDCAN_TimeoutOccurredCallback
  4838. (+) HAL_FDCAN_ErrorCallback
  4839. (+) HAL_FDCAN_ErrorStatusCallback
  4840. (+) HAL_FDCAN_TT_ScheduleSyncCallback
  4841. (+) HAL_FDCAN_TT_TimeMarkCallback
  4842. (+) HAL_FDCAN_TT_StopWatchCallback
  4843. (+) HAL_FDCAN_TT_GlobalTimeCallback
  4844. @endverbatim
  4845. * @{
  4846. */
  4847. /**
  4848. * @brief Clock Calibration callback.
  4849. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  4850. * the configuration information for the specified FDCAN.
  4851. * @param ClkCalibrationITs indicates which Clock Calibration interrupts are signaled.
  4852. * This parameter can be any combination of @arg FDCAN_Clock_Calibration_Interrupts.
  4853. * @retval None
  4854. */
  4855. __weak void HAL_FDCAN_ClockCalibrationCallback(FDCAN_HandleTypeDef *hfdcan, uint32_t ClkCalibrationITs)
  4856. {
  4857. /* Prevent unused argument(s) compilation warning */
  4858. UNUSED(hfdcan);
  4859. UNUSED(ClkCalibrationITs);
  4860. /* NOTE : This function Should not be modified, when the callback is needed,
  4861. the HAL_FDCAN_ClockCalibrationCallback could be implemented in the user file
  4862. */
  4863. }
  4864. /**
  4865. * @brief Tx Event callback.
  4866. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  4867. * the configuration information for the specified FDCAN.
  4868. * @param TxEventFifoITs indicates which Tx Event FIFO interrupts are signaled.
  4869. * This parameter can be any combination of @arg FDCAN_Tx_Event_Fifo_Interrupts.
  4870. * @retval None
  4871. */
  4872. __weak void HAL_FDCAN_TxEventFifoCallback(FDCAN_HandleTypeDef *hfdcan, uint32_t TxEventFifoITs)
  4873. {
  4874. /* Prevent unused argument(s) compilation warning */
  4875. UNUSED(hfdcan);
  4876. UNUSED(TxEventFifoITs);
  4877. /* NOTE : This function Should not be modified, when the callback is needed,
  4878. the HAL_FDCAN_TxEventFifoCallback could be implemented in the user file
  4879. */
  4880. }
  4881. /**
  4882. * @brief Rx FIFO 0 callback.
  4883. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  4884. * the configuration information for the specified FDCAN.
  4885. * @param RxFifo0ITs indicates which Rx FIFO 0 interrupts are signaled.
  4886. * This parameter can be any combination of @arg FDCAN_Rx_Fifo0_Interrupts.
  4887. * @retval None
  4888. */
  4889. __weak void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs)
  4890. {
  4891. /* Prevent unused argument(s) compilation warning */
  4892. UNUSED(hfdcan);
  4893. UNUSED(RxFifo0ITs);
  4894. /* NOTE : This function Should not be modified, when the callback is needed,
  4895. the HAL_FDCAN_RxFifo0Callback could be implemented in the user file
  4896. */
  4897. }
  4898. /**
  4899. * @brief Rx FIFO 1 callback.
  4900. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  4901. * the configuration information for the specified FDCAN.
  4902. * @param RxFifo1ITs indicates which Rx FIFO 1 interrupts are signaled.
  4903. * This parameter can be any combination of @arg FDCAN_Rx_Fifo1_Interrupts.
  4904. * @retval None
  4905. */
  4906. __weak void HAL_FDCAN_RxFifo1Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo1ITs)
  4907. {
  4908. /* Prevent unused argument(s) compilation warning */
  4909. UNUSED(hfdcan);
  4910. UNUSED(RxFifo1ITs);
  4911. /* NOTE : This function Should not be modified, when the callback is needed,
  4912. the HAL_FDCAN_RxFifo1Callback could be implemented in the user file
  4913. */
  4914. }
  4915. /**
  4916. * @brief Tx FIFO Empty callback.
  4917. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  4918. * the configuration information for the specified FDCAN.
  4919. * @retval None
  4920. */
  4921. __weak void HAL_FDCAN_TxFifoEmptyCallback(FDCAN_HandleTypeDef *hfdcan)
  4922. {
  4923. /* Prevent unused argument(s) compilation warning */
  4924. UNUSED(hfdcan);
  4925. /* NOTE : This function Should not be modified, when the callback is needed,
  4926. the HAL_FDCAN_TxFifoEmptyCallback could be implemented in the user file
  4927. */
  4928. }
  4929. /**
  4930. * @brief Transmission Complete callback.
  4931. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  4932. * the configuration information for the specified FDCAN.
  4933. * @param BufferIndexes Indexes of the transmitted buffers.
  4934. * This parameter can be any combination of @arg FDCAN_Tx_location.
  4935. * @retval None
  4936. */
  4937. __weak void HAL_FDCAN_TxBufferCompleteCallback(FDCAN_HandleTypeDef *hfdcan, uint32_t BufferIndexes)
  4938. {
  4939. /* Prevent unused argument(s) compilation warning */
  4940. UNUSED(hfdcan);
  4941. UNUSED(BufferIndexes);
  4942. /* NOTE : This function Should not be modified, when the callback is needed,
  4943. the HAL_FDCAN_TxBufferCompleteCallback could be implemented in the user file
  4944. */
  4945. }
  4946. /**
  4947. * @brief Transmission Cancellation callback.
  4948. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  4949. * the configuration information for the specified FDCAN.
  4950. * @param BufferIndexes Indexes of the aborted buffers.
  4951. * This parameter can be any combination of @arg FDCAN_Tx_location.
  4952. * @retval None
  4953. */
  4954. __weak void HAL_FDCAN_TxBufferAbortCallback(FDCAN_HandleTypeDef *hfdcan, uint32_t BufferIndexes)
  4955. {
  4956. /* Prevent unused argument(s) compilation warning */
  4957. UNUSED(hfdcan);
  4958. UNUSED(BufferIndexes);
  4959. /* NOTE : This function Should not be modified, when the callback is needed,
  4960. the HAL_FDCAN_TxBufferAbortCallback could be implemented in the user file
  4961. */
  4962. }
  4963. /**
  4964. * @brief Rx Buffer New Message callback.
  4965. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  4966. * the configuration information for the specified FDCAN.
  4967. * @retval None
  4968. */
  4969. __weak void HAL_FDCAN_RxBufferNewMessageCallback(FDCAN_HandleTypeDef *hfdcan)
  4970. {
  4971. /* Prevent unused argument(s) compilation warning */
  4972. UNUSED(hfdcan);
  4973. /* NOTE : This function Should not be modified, when the callback is needed,
  4974. the HAL_FDCAN_RxBufferNewMessageCallback could be implemented in the user file
  4975. */
  4976. }
  4977. /**
  4978. * @brief Timestamp Wraparound callback.
  4979. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  4980. * the configuration information for the specified FDCAN.
  4981. * @retval None
  4982. */
  4983. __weak void HAL_FDCAN_TimestampWraparoundCallback(FDCAN_HandleTypeDef *hfdcan)
  4984. {
  4985. /* Prevent unused argument(s) compilation warning */
  4986. UNUSED(hfdcan);
  4987. /* NOTE : This function Should not be modified, when the callback is needed,
  4988. the HAL_FDCAN_TimestampWraparoundCallback could be implemented in the user file
  4989. */
  4990. }
  4991. /**
  4992. * @brief Timeout Occurred callback.
  4993. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  4994. * the configuration information for the specified FDCAN.
  4995. * @retval None
  4996. */
  4997. __weak void HAL_FDCAN_TimeoutOccurredCallback(FDCAN_HandleTypeDef *hfdcan)
  4998. {
  4999. /* Prevent unused argument(s) compilation warning */
  5000. UNUSED(hfdcan);
  5001. /* NOTE : This function Should not be modified, when the callback is needed,
  5002. the HAL_FDCAN_TimeoutOccurredCallback could be implemented in the user file
  5003. */
  5004. }
  5005. /**
  5006. * @brief High Priority Message callback.
  5007. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  5008. * the configuration information for the specified FDCAN.
  5009. * @retval None
  5010. */
  5011. __weak void HAL_FDCAN_HighPriorityMessageCallback(FDCAN_HandleTypeDef *hfdcan)
  5012. {
  5013. /* Prevent unused argument(s) compilation warning */
  5014. UNUSED(hfdcan);
  5015. /* NOTE : This function Should not be modified, when the callback is needed,
  5016. the HAL_FDCAN_HighPriorityMessageCallback could be implemented in the user file
  5017. */
  5018. }
  5019. /**
  5020. * @brief Error callback.
  5021. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  5022. * the configuration information for the specified FDCAN.
  5023. * @retval None
  5024. */
  5025. __weak void HAL_FDCAN_ErrorCallback(FDCAN_HandleTypeDef *hfdcan)
  5026. {
  5027. /* Prevent unused argument(s) compilation warning */
  5028. UNUSED(hfdcan);
  5029. /* NOTE : This function Should not be modified, when the callback is needed,
  5030. the HAL_FDCAN_ErrorCallback could be implemented in the user file
  5031. */
  5032. }
  5033. /**
  5034. * @brief Error status callback.
  5035. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  5036. * the configuration information for the specified FDCAN.
  5037. * @param ErrorStatusITs indicates which Error Status interrupts are signaled.
  5038. * This parameter can be any combination of @arg FDCAN_Error_Status_Interrupts.
  5039. * @retval None
  5040. */
  5041. __weak void HAL_FDCAN_ErrorStatusCallback(FDCAN_HandleTypeDef *hfdcan, uint32_t ErrorStatusITs)
  5042. {
  5043. /* Prevent unused argument(s) compilation warning */
  5044. UNUSED(hfdcan);
  5045. UNUSED(ErrorStatusITs);
  5046. /* NOTE : This function Should not be modified, when the callback is needed,
  5047. the HAL_FDCAN_ErrorStatusCallback could be implemented in the user file
  5048. */
  5049. }
  5050. /**
  5051. * @brief TT Schedule Synchronization callback.
  5052. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  5053. * the configuration information for the specified FDCAN.
  5054. * @param TTSchedSyncITs indicates which TT Schedule Synchronization interrupts are signaled.
  5055. * This parameter can be any combination of @arg FDCAN_TTScheduleSynchronization_Interrupts.
  5056. * @retval None
  5057. */
  5058. __weak void HAL_FDCAN_TT_ScheduleSyncCallback(FDCAN_HandleTypeDef *hfdcan, uint32_t TTSchedSyncITs)
  5059. {
  5060. /* Prevent unused argument(s) compilation warning */
  5061. UNUSED(hfdcan);
  5062. UNUSED(TTSchedSyncITs);
  5063. /* NOTE : This function Should not be modified, when the callback is needed,
  5064. the HAL_FDCAN_TT_ScheduleSyncCallback could be implemented in the user file
  5065. */
  5066. }
  5067. /**
  5068. * @brief TT Time Mark callback.
  5069. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  5070. * the configuration information for the specified FDCAN.
  5071. * @param TTTimeMarkITs indicates which TT Schedule Synchronization interrupts are signaled.
  5072. * This parameter can be any combination of @arg FDCAN_TTTimeMark_Interrupts.
  5073. * @retval None
  5074. */
  5075. __weak void HAL_FDCAN_TT_TimeMarkCallback(FDCAN_HandleTypeDef *hfdcan, uint32_t TTTimeMarkITs)
  5076. {
  5077. /* Prevent unused argument(s) compilation warning */
  5078. UNUSED(hfdcan);
  5079. UNUSED(TTTimeMarkITs);
  5080. /* NOTE : This function Should not be modified, when the callback is needed,
  5081. the HAL_FDCAN_TT_TimeMarkCallback could be implemented in the user file
  5082. */
  5083. }
  5084. /**
  5085. * @brief TT Stop Watch callback.
  5086. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  5087. * the configuration information for the specified FDCAN.
  5088. * @param SWTime Time Value captured at the Stop Watch Trigger pin (fdcan1_swt) falling/rising
  5089. * edge (as configured via HAL_FDCAN_TTConfigStopWatch).
  5090. * This parameter is a number between 0 and 0xFFFF.
  5091. * @param SWCycleCount Cycle count value captured together with SWTime.
  5092. * This parameter is a number between 0 and 0x3F.
  5093. * @retval None
  5094. */
  5095. __weak void HAL_FDCAN_TT_StopWatchCallback(FDCAN_HandleTypeDef *hfdcan, uint32_t SWTime, uint32_t SWCycleCount)
  5096. {
  5097. /* Prevent unused argument(s) compilation warning */
  5098. UNUSED(hfdcan);
  5099. UNUSED(SWTime);
  5100. UNUSED(SWCycleCount);
  5101. /* NOTE : This function Should not be modified, when the callback is needed,
  5102. the HAL_FDCAN_TT_StopWatchCallback could be implemented in the user file
  5103. */
  5104. }
  5105. /**
  5106. * @brief TT Global Time callback.
  5107. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  5108. * the configuration information for the specified FDCAN.
  5109. * @param TTGlobTimeITs indicates which TT Global Time interrupts are signaled.
  5110. * This parameter can be any combination of @arg FDCAN_TTGlobalTime_Interrupts.
  5111. * @retval None
  5112. */
  5113. __weak void HAL_FDCAN_TT_GlobalTimeCallback(FDCAN_HandleTypeDef *hfdcan, uint32_t TTGlobTimeITs)
  5114. {
  5115. /* Prevent unused argument(s) compilation warning */
  5116. UNUSED(hfdcan);
  5117. UNUSED(TTGlobTimeITs);
  5118. /* NOTE : This function Should not be modified, when the callback is needed,
  5119. the HAL_FDCAN_TT_GlobalTimeCallback could be implemented in the user file
  5120. */
  5121. }
  5122. /**
  5123. * @}
  5124. */
  5125. /** @defgroup FDCAN_Exported_Functions_Group7 Peripheral State functions
  5126. * @brief FDCAN Peripheral State functions
  5127. *
  5128. @verbatim
  5129. ==============================================================================
  5130. ##### Peripheral State functions #####
  5131. ==============================================================================
  5132. [..]
  5133. This subsection provides functions allowing to :
  5134. (+) HAL_FDCAN_GetState() : Return the FDCAN state.
  5135. (+) HAL_FDCAN_GetError() : Return the FDCAN error code if any.
  5136. @endverbatim
  5137. * @{
  5138. */
  5139. /**
  5140. * @brief Return the FDCAN state
  5141. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  5142. * the configuration information for the specified FDCAN.
  5143. * @retval HAL state
  5144. */
  5145. HAL_FDCAN_StateTypeDef HAL_FDCAN_GetState(FDCAN_HandleTypeDef *hfdcan)
  5146. {
  5147. /* Return FDCAN state */
  5148. return hfdcan->State;
  5149. }
  5150. /**
  5151. * @brief Return the FDCAN error code
  5152. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  5153. * the configuration information for the specified FDCAN.
  5154. * @retval FDCAN Error Code
  5155. */
  5156. uint32_t HAL_FDCAN_GetError(FDCAN_HandleTypeDef *hfdcan)
  5157. {
  5158. /* Return FDCAN error code */
  5159. return hfdcan->ErrorCode;
  5160. }
  5161. /**
  5162. * @}
  5163. */
  5164. /**
  5165. * @}
  5166. */
  5167. /** @addtogroup FDCAN_Private_Functions
  5168. * @{
  5169. */
  5170. /**
  5171. * @brief Calculate each RAM block start address and size
  5172. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  5173. * the configuration information for the specified FDCAN.
  5174. * @retval HAL status
  5175. */
  5176. static HAL_StatusTypeDef FDCAN_CalcultateRamBlockAddresses(FDCAN_HandleTypeDef *hfdcan)
  5177. {
  5178. uint32_t RAMcounter;
  5179. uint32_t StartAddress;
  5180. StartAddress = hfdcan->Init.MessageRAMOffset;
  5181. /* Standard filter list start address */
  5182. MODIFY_REG(hfdcan->Instance->SIDFC, FDCAN_SIDFC_FLSSA, (StartAddress << FDCAN_SIDFC_FLSSA_Pos));
  5183. /* Standard filter elements number */
  5184. MODIFY_REG(hfdcan->Instance->SIDFC, FDCAN_SIDFC_LSS, (hfdcan->Init.StdFiltersNbr << FDCAN_SIDFC_LSS_Pos));
  5185. /* Extended filter list start address */
  5186. StartAddress += hfdcan->Init.StdFiltersNbr;
  5187. MODIFY_REG(hfdcan->Instance->XIDFC, FDCAN_XIDFC_FLESA, (StartAddress << FDCAN_XIDFC_FLESA_Pos));
  5188. /* Extended filter elements number */
  5189. MODIFY_REG(hfdcan->Instance->XIDFC, FDCAN_XIDFC_LSE, (hfdcan->Init.ExtFiltersNbr << FDCAN_XIDFC_LSE_Pos));
  5190. /* Rx FIFO 0 start address */
  5191. StartAddress += (hfdcan->Init.ExtFiltersNbr * 2U);
  5192. MODIFY_REG(hfdcan->Instance->RXF0C, FDCAN_RXF0C_F0SA, (StartAddress << FDCAN_RXF0C_F0SA_Pos));
  5193. /* Rx FIFO 0 elements number */
  5194. MODIFY_REG(hfdcan->Instance->RXF0C, FDCAN_RXF0C_F0S, (hfdcan->Init.RxFifo0ElmtsNbr << FDCAN_RXF0C_F0S_Pos));
  5195. /* Rx FIFO 1 start address */
  5196. StartAddress += (hfdcan->Init.RxFifo0ElmtsNbr * hfdcan->Init.RxFifo0ElmtSize);
  5197. MODIFY_REG(hfdcan->Instance->RXF1C, FDCAN_RXF1C_F1SA, (StartAddress << FDCAN_RXF1C_F1SA_Pos));
  5198. /* Rx FIFO 1 elements number */
  5199. MODIFY_REG(hfdcan->Instance->RXF1C, FDCAN_RXF1C_F1S, (hfdcan->Init.RxFifo1ElmtsNbr << FDCAN_RXF1C_F1S_Pos));
  5200. /* Rx buffer list start address */
  5201. StartAddress += (hfdcan->Init.RxFifo1ElmtsNbr * hfdcan->Init.RxFifo1ElmtSize);
  5202. MODIFY_REG(hfdcan->Instance->RXBC, FDCAN_RXBC_RBSA, (StartAddress << FDCAN_RXBC_RBSA_Pos));
  5203. /* Tx event FIFO start address */
  5204. StartAddress += (hfdcan->Init.RxBuffersNbr * hfdcan->Init.RxBufferSize);
  5205. MODIFY_REG(hfdcan->Instance->TXEFC, FDCAN_TXEFC_EFSA, (StartAddress << FDCAN_TXEFC_EFSA_Pos));
  5206. /* Tx event FIFO elements number */
  5207. MODIFY_REG(hfdcan->Instance->TXEFC, FDCAN_TXEFC_EFS, (hfdcan->Init.TxEventsNbr << FDCAN_TXEFC_EFS_Pos));
  5208. /* Tx buffer list start address */
  5209. StartAddress += (hfdcan->Init.TxEventsNbr * 2U);
  5210. MODIFY_REG(hfdcan->Instance->TXBC, FDCAN_TXBC_TBSA, (StartAddress << FDCAN_TXBC_TBSA_Pos));
  5211. /* Dedicated Tx buffers number */
  5212. MODIFY_REG(hfdcan->Instance->TXBC, FDCAN_TXBC_NDTB, (hfdcan->Init.TxBuffersNbr << FDCAN_TXBC_NDTB_Pos));
  5213. /* Tx FIFO/queue elements number */
  5214. MODIFY_REG(hfdcan->Instance->TXBC, FDCAN_TXBC_TFQS, (hfdcan->Init.TxFifoQueueElmtsNbr << FDCAN_TXBC_TFQS_Pos));
  5215. hfdcan->msgRam.StandardFilterSA = SRAMCAN_BASE + (hfdcan->Init.MessageRAMOffset * 4U);
  5216. hfdcan->msgRam.ExtendedFilterSA = hfdcan->msgRam.StandardFilterSA + (hfdcan->Init.StdFiltersNbr * 4U);
  5217. hfdcan->msgRam.RxFIFO0SA = hfdcan->msgRam.ExtendedFilterSA + (hfdcan->Init.ExtFiltersNbr * 2U * 4U);
  5218. hfdcan->msgRam.RxFIFO1SA = hfdcan->msgRam.RxFIFO0SA + (hfdcan->Init.RxFifo0ElmtsNbr * hfdcan->Init.RxFifo0ElmtSize * 4U);
  5219. hfdcan->msgRam.RxBufferSA = hfdcan->msgRam.RxFIFO1SA + (hfdcan->Init.RxFifo1ElmtsNbr * hfdcan->Init.RxFifo1ElmtSize * 4U);
  5220. hfdcan->msgRam.TxEventFIFOSA = hfdcan->msgRam.RxBufferSA + (hfdcan->Init.RxBuffersNbr * hfdcan->Init.RxBufferSize * 4U);
  5221. hfdcan->msgRam.TxBufferSA = hfdcan->msgRam.TxEventFIFOSA + (hfdcan->Init.TxEventsNbr * 2U * 4U);
  5222. hfdcan->msgRam.TxFIFOQSA = hfdcan->msgRam.TxBufferSA + (hfdcan->Init.TxBuffersNbr * hfdcan->Init.TxElmtSize * 4U);
  5223. hfdcan->msgRam.EndAddress = hfdcan->msgRam.TxFIFOQSA + (hfdcan->Init.TxFifoQueueElmtsNbr * hfdcan->Init.TxElmtSize * 4U);
  5224. if (hfdcan->msgRam.EndAddress > FDCAN_MESSAGE_RAM_END_ADDRESS) /* Last address of the Message RAM */
  5225. {
  5226. /* Update error code.
  5227. Message RAM overflow */
  5228. hfdcan->ErrorCode |= HAL_FDCAN_ERROR_PARAM;
  5229. /* Change FDCAN state */
  5230. hfdcan->State = HAL_FDCAN_STATE_ERROR;
  5231. return HAL_ERROR;
  5232. }
  5233. else
  5234. {
  5235. /* Flush the allocated Message RAM area */
  5236. for (RAMcounter = hfdcan->msgRam.StandardFilterSA; RAMcounter < hfdcan->msgRam.EndAddress; RAMcounter += 4U)
  5237. {
  5238. *(uint32_t *)(RAMcounter) = 0x00000000;
  5239. }
  5240. }
  5241. /* Return function status */
  5242. return HAL_OK;
  5243. }
  5244. /**
  5245. * @brief Copy Tx message to the message RAM.
  5246. * @param hfdcan pointer to an FDCAN_HandleTypeDef structure that contains
  5247. * the configuration information for the specified FDCAN.
  5248. * @param pTxHeader pointer to a FDCAN_TxHeaderTypeDef structure.
  5249. * @param pTxData pointer to a buffer containing the payload of the Tx frame.
  5250. * @param BufferIndex index of the buffer to be configured.
  5251. * @retval HAL status
  5252. */
  5253. static void FDCAN_CopyMessageToRAM(FDCAN_HandleTypeDef *hfdcan, FDCAN_TxHeaderTypeDef *pTxHeader, uint8_t *pTxData, uint32_t BufferIndex)
  5254. {
  5255. uint32_t TxElementW1;
  5256. uint32_t TxElementW2;
  5257. uint32_t *TxAddress;
  5258. uint32_t ByteCounter;
  5259. /* Build first word of Tx header element */
  5260. if (pTxHeader->IdType == FDCAN_STANDARD_ID)
  5261. {
  5262. TxElementW1 = (pTxHeader->ErrorStateIndicator |
  5263. FDCAN_STANDARD_ID |
  5264. pTxHeader->TxFrameType |
  5265. (pTxHeader->Identifier << 18));
  5266. }
  5267. else /* pTxHeader->IdType == FDCAN_EXTENDED_ID */
  5268. {
  5269. TxElementW1 = (pTxHeader->ErrorStateIndicator |
  5270. FDCAN_EXTENDED_ID |
  5271. pTxHeader->TxFrameType |
  5272. pTxHeader->Identifier);
  5273. }
  5274. /* Build second word of Tx header element */
  5275. TxElementW2 = ((pTxHeader->MessageMarker << 24) |
  5276. pTxHeader->TxEventFifoControl |
  5277. pTxHeader->FDFormat |
  5278. pTxHeader->BitRateSwitch |
  5279. pTxHeader->DataLength);
  5280. /* Calculate Tx element address */
  5281. TxAddress = (uint32_t *)(hfdcan->msgRam.TxBufferSA + (BufferIndex * hfdcan->Init.TxElmtSize * 4U));
  5282. /* Write Tx element header to the message RAM */
  5283. *TxAddress = TxElementW1;
  5284. TxAddress++;
  5285. *TxAddress = TxElementW2;
  5286. TxAddress++;
  5287. /* Write Tx payload to the message RAM */
  5288. for (ByteCounter = 0; ByteCounter < DLCtoBytes[pTxHeader->DataLength >> 16]; ByteCounter += 4U)
  5289. {
  5290. *TxAddress = (((uint32_t)pTxData[ByteCounter + 3U] << 24) |
  5291. ((uint32_t)pTxData[ByteCounter + 2U] << 16) |
  5292. ((uint32_t)pTxData[ByteCounter + 1U] << 8) |
  5293. (uint32_t)pTxData[ByteCounter]);
  5294. TxAddress++;
  5295. }
  5296. }
  5297. /**
  5298. * @}
  5299. */
  5300. #endif /* HAL_FDCAN_MODULE_ENABLED */
  5301. /**
  5302. * @}
  5303. */
  5304. /**
  5305. * @}
  5306. */
  5307. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/