训练营PLSR题目
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

2750 líneas
99 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_fmpsmbus.c
  4. * @author MCD Application Team
  5. * @brief FMPSMBUS HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the System Management Bus (SMBus) peripheral,
  8. * based on I2C principles of operation :
  9. * + Initialization and de-initialization functions
  10. * + IO operation functions
  11. * + Peripheral State and Errors functions
  12. *
  13. ******************************************************************************
  14. * @attention
  15. *
  16. * Copyright (c) 2016 STMicroelectronics.
  17. * All rights reserved.
  18. *
  19. * This software is licensed under terms that can be found in the LICENSE file
  20. * in the root directory of this software component.
  21. * If no LICENSE file comes with this software, it is provided AS-IS.
  22. *
  23. ******************************************************************************
  24. @verbatim
  25. ==============================================================================
  26. ##### How to use this driver #####
  27. ==============================================================================
  28. [..]
  29. The FMPSMBUS HAL driver can be used as follows:
  30. (#) Declare a FMPSMBUS_HandleTypeDef handle structure, for example:
  31. FMPSMBUS_HandleTypeDef hfmpsmbus;
  32. (#)Initialize the FMPSMBUS low level resources by implementing the HAL_FMPSMBUS_MspInit() API:
  33. (##) Enable the FMPSMBUSx interface clock
  34. (##) FMPSMBUS pins configuration
  35. (+++) Enable the clock for the FMPSMBUS GPIOs
  36. (+++) Configure FMPSMBUS pins as alternate function open-drain
  37. (##) NVIC configuration if you need to use interrupt process
  38. (+++) Configure the FMPSMBUSx interrupt priority
  39. (+++) Enable the NVIC FMPSMBUS IRQ Channel
  40. (#) Configure the Communication Clock Timing, Bus Timeout, Own Address1, Master Addressing mode,
  41. Dual Addressing mode, Own Address2, Own Address2 Mask, General call, Nostretch mode,
  42. Peripheral mode and Packet Error Check mode in the hfmpsmbus Init structure.
  43. (#) Initialize the FMPSMBUS registers by calling the HAL_FMPSMBUS_Init() API:
  44. (++) These API's configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
  45. by calling the customized HAL_FMPSMBUS_MspInit(&hfmpsmbus) API.
  46. (#) To check if target device is ready for communication, use the function HAL_FMPSMBUS_IsDeviceReady()
  47. (#) For FMPSMBUS IO operations, only one mode of operations is available within this driver
  48. *** Interrupt mode IO operation ***
  49. ===================================
  50. [..]
  51. (+) Transmit in master/host FMPSMBUS mode an amount of data in non-blocking mode
  52. using HAL_FMPSMBUS_Master_Transmit_IT()
  53. (++) At transmission end of transfer HAL_FMPSMBUS_MasterTxCpltCallback() is executed and users can
  54. add their own code by customization of function pointer HAL_FMPSMBUS_MasterTxCpltCallback()
  55. (+) Receive in master/host FMPSMBUS mode an amount of data in non-blocking mode
  56. using HAL_FMPSMBUS_Master_Receive_IT()
  57. (++) At reception end of transfer HAL_FMPSMBUS_MasterRxCpltCallback() is executed and users can
  58. add their own code by customization of function pointer HAL_FMPSMBUS_MasterRxCpltCallback()
  59. (+) Abort a master/host FMPSMBUS process communication with Interrupt using HAL_FMPSMBUS_Master_Abort_IT()
  60. (++) The associated previous transfer callback is called at the end of abort process
  61. (++) mean HAL_FMPSMBUS_MasterTxCpltCallback() in case of previous state was master transmit
  62. (++) mean HAL_FMPSMBUS_MasterRxCpltCallback() in case of previous state was master receive
  63. (+) Enable/disable the Address listen mode in slave/device or host/slave FMPSMBUS mode
  64. using HAL_FMPSMBUS_EnableListen_IT() HAL_FMPSMBUS_DisableListen_IT()
  65. (++) When address slave/device FMPSMBUS match, HAL_FMPSMBUS_AddrCallback() is executed and users can
  66. add their own code to check the Address Match Code and the transmission direction
  67. request by master/host (Write/Read).
  68. (++) At Listen mode end HAL_FMPSMBUS_ListenCpltCallback() is executed and users can
  69. add their own code by customization of function pointer HAL_FMPSMBUS_ListenCpltCallback()
  70. (+) Transmit in slave/device FMPSMBUS mode an amount of data in non-blocking mode
  71. using HAL_FMPSMBUS_Slave_Transmit_IT()
  72. (++) At transmission end of transfer HAL_FMPSMBUS_SlaveTxCpltCallback() is executed and users can
  73. add their own code by customization of function pointer HAL_FMPSMBUS_SlaveTxCpltCallback()
  74. (+) Receive in slave/device FMPSMBUS mode an amount of data in non-blocking mode
  75. using HAL_FMPSMBUS_Slave_Receive_IT()
  76. (++) At reception end of transfer HAL_FMPSMBUS_SlaveRxCpltCallback() is executed and users can
  77. add their own code by customization of function pointer HAL_FMPSMBUS_SlaveRxCpltCallback()
  78. (+) Enable/Disable the FMPSMBUS alert mode using
  79. HAL_FMPSMBUS_EnableAlert_IT() or HAL_FMPSMBUS_DisableAlert_IT()
  80. (++) When FMPSMBUS Alert is generated HAL_FMPSMBUS_ErrorCallback() is executed and users can
  81. add their own code by customization of function pointer HAL_FMPSMBUS_ErrorCallback()
  82. to check the Alert Error Code using function HAL_FMPSMBUS_GetError()
  83. (+) Get HAL state machine or error values using HAL_FMPSMBUS_GetState() or HAL_FMPSMBUS_GetError()
  84. (+) In case of transfer Error, HAL_FMPSMBUS_ErrorCallback() function is executed and users can
  85. add their own code by customization of function pointer HAL_FMPSMBUS_ErrorCallback()
  86. to check the Error Code using function HAL_FMPSMBUS_GetError()
  87. *** FMPSMBUS HAL driver macros list ***
  88. ==================================
  89. [..]
  90. Below the list of most used macros in FMPSMBUS HAL driver.
  91. (+) __HAL_FMPSMBUS_ENABLE: Enable the FMPSMBUS peripheral
  92. (+) __HAL_FMPSMBUS_DISABLE: Disable the FMPSMBUS peripheral
  93. (+) __HAL_FMPSMBUS_GET_FLAG: Check whether the specified FMPSMBUS flag is set or not
  94. (+) __HAL_FMPSMBUS_CLEAR_FLAG: Clear the specified FMPSMBUS pending flag
  95. (+) __HAL_FMPSMBUS_ENABLE_IT: Enable the specified FMPSMBUS interrupt
  96. (+) __HAL_FMPSMBUS_DISABLE_IT: Disable the specified FMPSMBUS interrupt
  97. *** Callback registration ***
  98. =============================================
  99. [..]
  100. The compilation flag USE_HAL_FMPSMBUS_REGISTER_CALLBACKS when set to 1
  101. allows the user to configure dynamically the driver callbacks.
  102. Use Functions HAL_FMPSMBUS_RegisterCallback() or HAL_FMPSMBUS_RegisterAddrCallback()
  103. to register an interrupt callback.
  104. [..]
  105. Function HAL_FMPSMBUS_RegisterCallback() allows to register following callbacks:
  106. (+) MasterTxCpltCallback : callback for Master transmission end of transfer.
  107. (+) MasterRxCpltCallback : callback for Master reception end of transfer.
  108. (+) SlaveTxCpltCallback : callback for Slave transmission end of transfer.
  109. (+) SlaveRxCpltCallback : callback for Slave reception end of transfer.
  110. (+) ListenCpltCallback : callback for end of listen mode.
  111. (+) ErrorCallback : callback for error detection.
  112. (+) MspInitCallback : callback for Msp Init.
  113. (+) MspDeInitCallback : callback for Msp DeInit.
  114. This function takes as parameters the HAL peripheral handle, the Callback ID
  115. and a pointer to the user callback function.
  116. [..]
  117. For specific callback AddrCallback use dedicated register callbacks : HAL_FMPSMBUS_RegisterAddrCallback.
  118. [..]
  119. Use function HAL_FMPSMBUS_UnRegisterCallback to reset a callback to the default
  120. weak function.
  121. HAL_FMPSMBUS_UnRegisterCallback takes as parameters the HAL peripheral handle,
  122. and the Callback ID.
  123. This function allows to reset following callbacks:
  124. (+) MasterTxCpltCallback : callback for Master transmission end of transfer.
  125. (+) MasterRxCpltCallback : callback for Master reception end of transfer.
  126. (+) SlaveTxCpltCallback : callback for Slave transmission end of transfer.
  127. (+) SlaveRxCpltCallback : callback for Slave reception end of transfer.
  128. (+) ListenCpltCallback : callback for end of listen mode.
  129. (+) ErrorCallback : callback for error detection.
  130. (+) MspInitCallback : callback for Msp Init.
  131. (+) MspDeInitCallback : callback for Msp DeInit.
  132. [..]
  133. For callback AddrCallback use dedicated register callbacks : HAL_FMPSMBUS_UnRegisterAddrCallback.
  134. [..]
  135. By default, after the HAL_FMPSMBUS_Init() and when the state is HAL_FMPI2C_STATE_RESET
  136. all callbacks are set to the corresponding weak functions:
  137. examples HAL_FMPSMBUS_MasterTxCpltCallback(), HAL_FMPSMBUS_MasterRxCpltCallback().
  138. Exception done for MspInit and MspDeInit functions that are
  139. reset to the legacy weak functions in the HAL_FMPSMBUS_Init()/ HAL_FMPSMBUS_DeInit() only when
  140. these callbacks are null (not registered beforehand).
  141. If MspInit or MspDeInit are not null, the HAL_FMPSMBUS_Init()/ HAL_FMPSMBUS_DeInit()
  142. keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
  143. [..]
  144. Callbacks can be registered/unregistered in HAL_FMPI2C_STATE_READY state only.
  145. Exception done MspInit/MspDeInit functions that can be registered/unregistered
  146. in HAL_FMPI2C_STATE_READY or HAL_FMPI2C_STATE_RESET state,
  147. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  148. Then, the user first registers the MspInit/MspDeInit user callbacks
  149. using HAL_FMPSMBUS_RegisterCallback() before calling HAL_FMPSMBUS_DeInit()
  150. or HAL_FMPSMBUS_Init() function.
  151. [..]
  152. When the compilation flag USE_HAL_FMPSMBUS_REGISTER_CALLBACKS is set to 0 or
  153. not defined, the callback registration feature is not available and all callbacks
  154. are set to the corresponding weak functions.
  155. [..]
  156. (@) You can refer to the FMPSMBUS HAL driver header file for more useful macros
  157. @endverbatim
  158. */
  159. /* Includes ------------------------------------------------------------------*/
  160. #include "stm32f4xx_hal.h"
  161. /** @addtogroup STM32F4xx_HAL_Driver
  162. * @{
  163. */
  164. /** @defgroup FMPSMBUS FMPSMBUS
  165. * @brief FMPSMBUS HAL module driver
  166. * @{
  167. */
  168. #ifdef HAL_FMPSMBUS_MODULE_ENABLED
  169. #if defined(FMPI2C_CR1_PE)
  170. /* Private typedef -----------------------------------------------------------*/
  171. /* Private constants ---------------------------------------------------------*/
  172. /** @defgroup FMPSMBUS_Private_Define FMPSMBUS Private Constants
  173. * @{
  174. */
  175. #define TIMING_CLEAR_MASK (0xF0FFFFFFUL) /*!< FMPSMBUS TIMING clear register Mask */
  176. #define HAL_TIMEOUT_ADDR (10000U) /*!< 10 s */
  177. #define HAL_TIMEOUT_BUSY (25U) /*!< 25 ms */
  178. #define HAL_TIMEOUT_DIR (25U) /*!< 25 ms */
  179. #define HAL_TIMEOUT_RXNE (25U) /*!< 25 ms */
  180. #define HAL_TIMEOUT_STOPF (25U) /*!< 25 ms */
  181. #define HAL_TIMEOUT_TC (25U) /*!< 25 ms */
  182. #define HAL_TIMEOUT_TCR (25U) /*!< 25 ms */
  183. #define HAL_TIMEOUT_TXIS (25U) /*!< 25 ms */
  184. #define MAX_NBYTE_SIZE 255U
  185. /**
  186. * @}
  187. */
  188. /* Private macro -------------------------------------------------------------*/
  189. /* Private variables ---------------------------------------------------------*/
  190. /* Private function prototypes -----------------------------------------------*/
  191. /** @addtogroup FMPSMBUS_Private_Functions FMPSMBUS Private Functions
  192. * @{
  193. */
  194. static HAL_StatusTypeDef FMPSMBUS_WaitOnFlagUntilTimeout(FMPSMBUS_HandleTypeDef *hfmpsmbus, uint32_t Flag,
  195. FlagStatus Status, uint32_t Timeout);
  196. static void FMPSMBUS_Enable_IRQ(FMPSMBUS_HandleTypeDef *hfmpsmbus, uint32_t InterruptRequest);
  197. static void FMPSMBUS_Disable_IRQ(FMPSMBUS_HandleTypeDef *hfmpsmbus, uint32_t InterruptRequest);
  198. static HAL_StatusTypeDef FMPSMBUS_Master_ISR(FMPSMBUS_HandleTypeDef *hfmpsmbus, uint32_t StatusFlags);
  199. static HAL_StatusTypeDef FMPSMBUS_Slave_ISR(FMPSMBUS_HandleTypeDef *hfmpsmbus, uint32_t StatusFlags);
  200. static void FMPSMBUS_ConvertOtherXferOptions(FMPSMBUS_HandleTypeDef *hfmpsmbus);
  201. static void FMPSMBUS_ITErrorHandler(FMPSMBUS_HandleTypeDef *hfmpsmbus);
  202. static void FMPSMBUS_TransferConfig(FMPSMBUS_HandleTypeDef *hfmpsmbus, uint16_t DevAddress, uint8_t Size,
  203. uint32_t Mode, uint32_t Request);
  204. /**
  205. * @}
  206. */
  207. /* Exported functions --------------------------------------------------------*/
  208. /** @defgroup FMPSMBUS_Exported_Functions FMPSMBUS Exported Functions
  209. * @{
  210. */
  211. /** @defgroup FMPSMBUS_Exported_Functions_Group1 Initialization and de-initialization functions
  212. * @brief Initialization and Configuration functions
  213. *
  214. @verbatim
  215. ===============================================================================
  216. ##### Initialization and de-initialization functions #####
  217. ===============================================================================
  218. [..] This subsection provides a set of functions allowing to initialize and
  219. deinitialize the FMPSMBUSx peripheral:
  220. (+) User must Implement HAL_FMPSMBUS_MspInit() function in which he configures
  221. all related peripherals resources (CLOCK, GPIO, IT and NVIC ).
  222. (+) Call the function HAL_FMPSMBUS_Init() to configure the selected device with
  223. the selected configuration:
  224. (++) Clock Timing
  225. (++) Bus Timeout
  226. (++) Analog Filer mode
  227. (++) Own Address 1
  228. (++) Addressing mode (Master, Slave)
  229. (++) Dual Addressing mode
  230. (++) Own Address 2
  231. (++) Own Address 2 Mask
  232. (++) General call mode
  233. (++) Nostretch mode
  234. (++) Packet Error Check mode
  235. (++) Peripheral mode
  236. (+) Call the function HAL_FMPSMBUS_DeInit() to restore the default configuration
  237. of the selected FMPSMBUSx peripheral.
  238. (+) Enable/Disable Analog/Digital filters with HAL_FMPSMBUS_ConfigAnalogFilter() and
  239. HAL_FMPSMBUS_ConfigDigitalFilter().
  240. @endverbatim
  241. * @{
  242. */
  243. /**
  244. * @brief Initialize the FMPSMBUS according to the specified parameters
  245. * in the FMPSMBUS_InitTypeDef and initialize the associated handle.
  246. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  247. * the configuration information for the specified FMPSMBUS.
  248. * @retval HAL status
  249. */
  250. HAL_StatusTypeDef HAL_FMPSMBUS_Init(FMPSMBUS_HandleTypeDef *hfmpsmbus)
  251. {
  252. /* Check the FMPSMBUS handle allocation */
  253. if (hfmpsmbus == NULL)
  254. {
  255. return HAL_ERROR;
  256. }
  257. /* Check the parameters */
  258. assert_param(IS_FMPSMBUS_ALL_INSTANCE(hfmpsmbus->Instance));
  259. assert_param(IS_FMPSMBUS_ANALOG_FILTER(hfmpsmbus->Init.AnalogFilter));
  260. assert_param(IS_FMPSMBUS_OWN_ADDRESS1(hfmpsmbus->Init.OwnAddress1));
  261. assert_param(IS_FMPSMBUS_ADDRESSING_MODE(hfmpsmbus->Init.AddressingMode));
  262. assert_param(IS_FMPSMBUS_DUAL_ADDRESS(hfmpsmbus->Init.DualAddressMode));
  263. assert_param(IS_FMPSMBUS_OWN_ADDRESS2(hfmpsmbus->Init.OwnAddress2));
  264. assert_param(IS_FMPSMBUS_OWN_ADDRESS2_MASK(hfmpsmbus->Init.OwnAddress2Masks));
  265. assert_param(IS_FMPSMBUS_GENERAL_CALL(hfmpsmbus->Init.GeneralCallMode));
  266. assert_param(IS_FMPSMBUS_NO_STRETCH(hfmpsmbus->Init.NoStretchMode));
  267. assert_param(IS_FMPSMBUS_PEC(hfmpsmbus->Init.PacketErrorCheckMode));
  268. assert_param(IS_FMPSMBUS_PERIPHERAL_MODE(hfmpsmbus->Init.PeripheralMode));
  269. if (hfmpsmbus->State == HAL_FMPSMBUS_STATE_RESET)
  270. {
  271. /* Allocate lock resource and initialize it */
  272. hfmpsmbus->Lock = HAL_UNLOCKED;
  273. #if (USE_HAL_FMPSMBUS_REGISTER_CALLBACKS == 1)
  274. hfmpsmbus->MasterTxCpltCallback = HAL_FMPSMBUS_MasterTxCpltCallback; /* Legacy weak MasterTxCpltCallback */
  275. hfmpsmbus->MasterRxCpltCallback = HAL_FMPSMBUS_MasterRxCpltCallback; /* Legacy weak MasterRxCpltCallback */
  276. hfmpsmbus->SlaveTxCpltCallback = HAL_FMPSMBUS_SlaveTxCpltCallback; /* Legacy weak SlaveTxCpltCallback */
  277. hfmpsmbus->SlaveRxCpltCallback = HAL_FMPSMBUS_SlaveRxCpltCallback; /* Legacy weak SlaveRxCpltCallback */
  278. hfmpsmbus->ListenCpltCallback = HAL_FMPSMBUS_ListenCpltCallback; /* Legacy weak ListenCpltCallback */
  279. hfmpsmbus->ErrorCallback = HAL_FMPSMBUS_ErrorCallback; /* Legacy weak ErrorCallback */
  280. hfmpsmbus->AddrCallback = HAL_FMPSMBUS_AddrCallback; /* Legacy weak AddrCallback */
  281. if (hfmpsmbus->MspInitCallback == NULL)
  282. {
  283. hfmpsmbus->MspInitCallback = HAL_FMPSMBUS_MspInit; /* Legacy weak MspInit */
  284. }
  285. /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  286. hfmpsmbus->MspInitCallback(hfmpsmbus);
  287. #else
  288. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  289. HAL_FMPSMBUS_MspInit(hfmpsmbus);
  290. #endif /* USE_HAL_FMPSMBUS_REGISTER_CALLBACKS */
  291. }
  292. hfmpsmbus->State = HAL_FMPSMBUS_STATE_BUSY;
  293. /* Disable the selected FMPSMBUS peripheral */
  294. __HAL_FMPSMBUS_DISABLE(hfmpsmbus);
  295. /*---------------------------- FMPSMBUSx TIMINGR Configuration ------------------------*/
  296. /* Configure FMPSMBUSx: Frequency range */
  297. hfmpsmbus->Instance->TIMINGR = hfmpsmbus->Init.Timing & TIMING_CLEAR_MASK;
  298. /*---------------------------- FMPSMBUSx TIMEOUTR Configuration ------------------------*/
  299. /* Configure FMPSMBUSx: Bus Timeout */
  300. hfmpsmbus->Instance->TIMEOUTR &= ~FMPI2C_TIMEOUTR_TIMOUTEN;
  301. hfmpsmbus->Instance->TIMEOUTR &= ~FMPI2C_TIMEOUTR_TEXTEN;
  302. hfmpsmbus->Instance->TIMEOUTR = hfmpsmbus->Init.SMBusTimeout;
  303. /*---------------------------- FMPSMBUSx OAR1 Configuration -----------------------*/
  304. /* Configure FMPSMBUSx: Own Address1 and ack own address1 mode */
  305. hfmpsmbus->Instance->OAR1 &= ~FMPI2C_OAR1_OA1EN;
  306. if (hfmpsmbus->Init.OwnAddress1 != 0UL)
  307. {
  308. if (hfmpsmbus->Init.AddressingMode == FMPSMBUS_ADDRESSINGMODE_7BIT)
  309. {
  310. hfmpsmbus->Instance->OAR1 = (FMPI2C_OAR1_OA1EN | hfmpsmbus->Init.OwnAddress1);
  311. }
  312. else /* FMPSMBUS_ADDRESSINGMODE_10BIT */
  313. {
  314. hfmpsmbus->Instance->OAR1 = (FMPI2C_OAR1_OA1EN | FMPI2C_OAR1_OA1MODE | hfmpsmbus->Init.OwnAddress1);
  315. }
  316. }
  317. /*---------------------------- FMPSMBUSx CR2 Configuration ------------------------*/
  318. /* Configure FMPSMBUSx: Addressing Master mode */
  319. if (hfmpsmbus->Init.AddressingMode == FMPSMBUS_ADDRESSINGMODE_10BIT)
  320. {
  321. hfmpsmbus->Instance->CR2 = (FMPI2C_CR2_ADD10);
  322. }
  323. /* Enable the AUTOEND by default, and enable NACK (should be disable only during Slave process) */
  324. /* AUTOEND and NACK bit will be manage during Transfer process */
  325. hfmpsmbus->Instance->CR2 |= (FMPI2C_CR2_AUTOEND | FMPI2C_CR2_NACK);
  326. /*---------------------------- FMPSMBUSx OAR2 Configuration -----------------------*/
  327. /* Configure FMPSMBUSx: Dual mode and Own Address2 */
  328. hfmpsmbus->Instance->OAR2 = (hfmpsmbus->Init.DualAddressMode | hfmpsmbus->Init.OwnAddress2 | \
  329. (hfmpsmbus->Init.OwnAddress2Masks << 8U));
  330. /*---------------------------- FMPSMBUSx CR1 Configuration ------------------------*/
  331. /* Configure FMPSMBUSx: Generalcall and NoStretch mode */
  332. hfmpsmbus->Instance->CR1 = (hfmpsmbus->Init.GeneralCallMode | hfmpsmbus->Init.NoStretchMode | \
  333. hfmpsmbus->Init.PacketErrorCheckMode | hfmpsmbus->Init.PeripheralMode | \
  334. hfmpsmbus->Init.AnalogFilter);
  335. /* Enable Slave Byte Control only in case of Packet Error Check is enabled
  336. and FMPSMBUS Peripheral is set in Slave mode */
  337. if ((hfmpsmbus->Init.PacketErrorCheckMode == FMPSMBUS_PEC_ENABLE) && \
  338. ((hfmpsmbus->Init.PeripheralMode == FMPSMBUS_PERIPHERAL_MODE_FMPSMBUS_SLAVE) || \
  339. (hfmpsmbus->Init.PeripheralMode == FMPSMBUS_PERIPHERAL_MODE_FMPSMBUS_SLAVE_ARP)))
  340. {
  341. hfmpsmbus->Instance->CR1 |= FMPI2C_CR1_SBC;
  342. }
  343. /* Enable the selected FMPSMBUS peripheral */
  344. __HAL_FMPSMBUS_ENABLE(hfmpsmbus);
  345. hfmpsmbus->ErrorCode = HAL_FMPSMBUS_ERROR_NONE;
  346. hfmpsmbus->PreviousState = HAL_FMPSMBUS_STATE_READY;
  347. hfmpsmbus->State = HAL_FMPSMBUS_STATE_READY;
  348. return HAL_OK;
  349. }
  350. /**
  351. * @brief DeInitialize the FMPSMBUS peripheral.
  352. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  353. * the configuration information for the specified FMPSMBUS.
  354. * @retval HAL status
  355. */
  356. HAL_StatusTypeDef HAL_FMPSMBUS_DeInit(FMPSMBUS_HandleTypeDef *hfmpsmbus)
  357. {
  358. /* Check the FMPSMBUS handle allocation */
  359. if (hfmpsmbus == NULL)
  360. {
  361. return HAL_ERROR;
  362. }
  363. /* Check the parameters */
  364. assert_param(IS_FMPSMBUS_ALL_INSTANCE(hfmpsmbus->Instance));
  365. hfmpsmbus->State = HAL_FMPSMBUS_STATE_BUSY;
  366. /* Disable the FMPSMBUS Peripheral Clock */
  367. __HAL_FMPSMBUS_DISABLE(hfmpsmbus);
  368. #if (USE_HAL_FMPSMBUS_REGISTER_CALLBACKS == 1)
  369. if (hfmpsmbus->MspDeInitCallback == NULL)
  370. {
  371. hfmpsmbus->MspDeInitCallback = HAL_FMPSMBUS_MspDeInit; /* Legacy weak MspDeInit */
  372. }
  373. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  374. hfmpsmbus->MspDeInitCallback(hfmpsmbus);
  375. #else
  376. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  377. HAL_FMPSMBUS_MspDeInit(hfmpsmbus);
  378. #endif /* USE_HAL_FMPSMBUS_REGISTER_CALLBACKS */
  379. hfmpsmbus->ErrorCode = HAL_FMPSMBUS_ERROR_NONE;
  380. hfmpsmbus->PreviousState = HAL_FMPSMBUS_STATE_RESET;
  381. hfmpsmbus->State = HAL_FMPSMBUS_STATE_RESET;
  382. /* Release Lock */
  383. __HAL_UNLOCK(hfmpsmbus);
  384. return HAL_OK;
  385. }
  386. /**
  387. * @brief Initialize the FMPSMBUS MSP.
  388. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  389. * the configuration information for the specified FMPSMBUS.
  390. * @retval None
  391. */
  392. __weak void HAL_FMPSMBUS_MspInit(FMPSMBUS_HandleTypeDef *hfmpsmbus)
  393. {
  394. /* Prevent unused argument(s) compilation warning */
  395. UNUSED(hfmpsmbus);
  396. /* NOTE : This function should not be modified, when the callback is needed,
  397. the HAL_FMPSMBUS_MspInit could be implemented in the user file
  398. */
  399. }
  400. /**
  401. * @brief DeInitialize the FMPSMBUS MSP.
  402. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  403. * the configuration information for the specified FMPSMBUS.
  404. * @retval None
  405. */
  406. __weak void HAL_FMPSMBUS_MspDeInit(FMPSMBUS_HandleTypeDef *hfmpsmbus)
  407. {
  408. /* Prevent unused argument(s) compilation warning */
  409. UNUSED(hfmpsmbus);
  410. /* NOTE : This function should not be modified, when the callback is needed,
  411. the HAL_FMPSMBUS_MspDeInit could be implemented in the user file
  412. */
  413. }
  414. /**
  415. * @brief Configure Analog noise filter.
  416. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  417. * the configuration information for the specified FMPSMBUS.
  418. * @param AnalogFilter This parameter can be one of the following values:
  419. * @arg @ref FMPSMBUS_ANALOGFILTER_ENABLE
  420. * @arg @ref FMPSMBUS_ANALOGFILTER_DISABLE
  421. * @retval HAL status
  422. */
  423. HAL_StatusTypeDef HAL_FMPSMBUS_ConfigAnalogFilter(FMPSMBUS_HandleTypeDef *hfmpsmbus, uint32_t AnalogFilter)
  424. {
  425. /* Check the parameters */
  426. assert_param(IS_FMPSMBUS_ALL_INSTANCE(hfmpsmbus->Instance));
  427. assert_param(IS_FMPSMBUS_ANALOG_FILTER(AnalogFilter));
  428. if (hfmpsmbus->State == HAL_FMPSMBUS_STATE_READY)
  429. {
  430. /* Process Locked */
  431. __HAL_LOCK(hfmpsmbus);
  432. hfmpsmbus->State = HAL_FMPSMBUS_STATE_BUSY;
  433. /* Disable the selected FMPSMBUS peripheral */
  434. __HAL_FMPSMBUS_DISABLE(hfmpsmbus);
  435. /* Reset ANOFF bit */
  436. hfmpsmbus->Instance->CR1 &= ~(FMPI2C_CR1_ANFOFF);
  437. /* Set analog filter bit*/
  438. hfmpsmbus->Instance->CR1 |= AnalogFilter;
  439. __HAL_FMPSMBUS_ENABLE(hfmpsmbus);
  440. hfmpsmbus->State = HAL_FMPSMBUS_STATE_READY;
  441. /* Process Unlocked */
  442. __HAL_UNLOCK(hfmpsmbus);
  443. return HAL_OK;
  444. }
  445. else
  446. {
  447. return HAL_BUSY;
  448. }
  449. }
  450. /**
  451. * @brief Configure Digital noise filter.
  452. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  453. * the configuration information for the specified FMPSMBUS.
  454. * @param DigitalFilter Coefficient of digital noise filter between Min_Data=0x00 and Max_Data=0x0F.
  455. * @retval HAL status
  456. */
  457. HAL_StatusTypeDef HAL_FMPSMBUS_ConfigDigitalFilter(FMPSMBUS_HandleTypeDef *hfmpsmbus, uint32_t DigitalFilter)
  458. {
  459. uint32_t tmpreg;
  460. /* Check the parameters */
  461. assert_param(IS_FMPSMBUS_ALL_INSTANCE(hfmpsmbus->Instance));
  462. assert_param(IS_FMPSMBUS_DIGITAL_FILTER(DigitalFilter));
  463. if (hfmpsmbus->State == HAL_FMPSMBUS_STATE_READY)
  464. {
  465. /* Process Locked */
  466. __HAL_LOCK(hfmpsmbus);
  467. hfmpsmbus->State = HAL_FMPSMBUS_STATE_BUSY;
  468. /* Disable the selected FMPSMBUS peripheral */
  469. __HAL_FMPSMBUS_DISABLE(hfmpsmbus);
  470. /* Get the old register value */
  471. tmpreg = hfmpsmbus->Instance->CR1;
  472. /* Reset FMPI2C DNF bits [11:8] */
  473. tmpreg &= ~(FMPI2C_CR1_DNF);
  474. /* Set FMPI2Cx DNF coefficient */
  475. tmpreg |= DigitalFilter << FMPI2C_CR1_DNF_Pos;
  476. /* Store the new register value */
  477. hfmpsmbus->Instance->CR1 = tmpreg;
  478. __HAL_FMPSMBUS_ENABLE(hfmpsmbus);
  479. hfmpsmbus->State = HAL_FMPSMBUS_STATE_READY;
  480. /* Process Unlocked */
  481. __HAL_UNLOCK(hfmpsmbus);
  482. return HAL_OK;
  483. }
  484. else
  485. {
  486. return HAL_BUSY;
  487. }
  488. }
  489. #if (USE_HAL_FMPSMBUS_REGISTER_CALLBACKS == 1)
  490. /**
  491. * @brief Register a User FMPSMBUS Callback
  492. * To be used instead of the weak predefined callback
  493. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  494. * the configuration information for the specified FMPSMBUS.
  495. * @param CallbackID ID of the callback to be registered
  496. * This parameter can be one of the following values:
  497. * @arg @ref HAL_FMPSMBUS_MASTER_TX_COMPLETE_CB_ID Master Tx Transfer completed callback ID
  498. * @arg @ref HAL_FMPSMBUS_MASTER_RX_COMPLETE_CB_ID Master Rx Transfer completed callback ID
  499. * @arg @ref HAL_FMPSMBUS_SLAVE_TX_COMPLETE_CB_ID Slave Tx Transfer completed callback ID
  500. * @arg @ref HAL_FMPSMBUS_SLAVE_RX_COMPLETE_CB_ID Slave Rx Transfer completed callback ID
  501. * @arg @ref HAL_FMPSMBUS_LISTEN_COMPLETE_CB_ID Listen Complete callback ID
  502. * @arg @ref HAL_FMPSMBUS_ERROR_CB_ID Error callback ID
  503. * @arg @ref HAL_FMPSMBUS_MSPINIT_CB_ID MspInit callback ID
  504. * @arg @ref HAL_FMPSMBUS_MSPDEINIT_CB_ID MspDeInit callback ID
  505. * @param pCallback pointer to the Callback function
  506. * @retval HAL status
  507. */
  508. HAL_StatusTypeDef HAL_FMPSMBUS_RegisterCallback(FMPSMBUS_HandleTypeDef *hfmpsmbus,
  509. HAL_FMPSMBUS_CallbackIDTypeDef CallbackID,
  510. pFMPSMBUS_CallbackTypeDef pCallback)
  511. {
  512. HAL_StatusTypeDef status = HAL_OK;
  513. if (pCallback == NULL)
  514. {
  515. /* Update the error code */
  516. hfmpsmbus->ErrorCode |= HAL_FMPSMBUS_ERROR_INVALID_CALLBACK;
  517. return HAL_ERROR;
  518. }
  519. /* Process locked */
  520. __HAL_LOCK(hfmpsmbus);
  521. if (HAL_FMPSMBUS_STATE_READY == hfmpsmbus->State)
  522. {
  523. switch (CallbackID)
  524. {
  525. case HAL_FMPSMBUS_MASTER_TX_COMPLETE_CB_ID :
  526. hfmpsmbus->MasterTxCpltCallback = pCallback;
  527. break;
  528. case HAL_FMPSMBUS_MASTER_RX_COMPLETE_CB_ID :
  529. hfmpsmbus->MasterRxCpltCallback = pCallback;
  530. break;
  531. case HAL_FMPSMBUS_SLAVE_TX_COMPLETE_CB_ID :
  532. hfmpsmbus->SlaveTxCpltCallback = pCallback;
  533. break;
  534. case HAL_FMPSMBUS_SLAVE_RX_COMPLETE_CB_ID :
  535. hfmpsmbus->SlaveRxCpltCallback = pCallback;
  536. break;
  537. case HAL_FMPSMBUS_LISTEN_COMPLETE_CB_ID :
  538. hfmpsmbus->ListenCpltCallback = pCallback;
  539. break;
  540. case HAL_FMPSMBUS_ERROR_CB_ID :
  541. hfmpsmbus->ErrorCallback = pCallback;
  542. break;
  543. case HAL_FMPSMBUS_MSPINIT_CB_ID :
  544. hfmpsmbus->MspInitCallback = pCallback;
  545. break;
  546. case HAL_FMPSMBUS_MSPDEINIT_CB_ID :
  547. hfmpsmbus->MspDeInitCallback = pCallback;
  548. break;
  549. default :
  550. /* Update the error code */
  551. hfmpsmbus->ErrorCode |= HAL_FMPSMBUS_ERROR_INVALID_CALLBACK;
  552. /* Return error status */
  553. status = HAL_ERROR;
  554. break;
  555. }
  556. }
  557. else if (HAL_FMPSMBUS_STATE_RESET == hfmpsmbus->State)
  558. {
  559. switch (CallbackID)
  560. {
  561. case HAL_FMPSMBUS_MSPINIT_CB_ID :
  562. hfmpsmbus->MspInitCallback = pCallback;
  563. break;
  564. case HAL_FMPSMBUS_MSPDEINIT_CB_ID :
  565. hfmpsmbus->MspDeInitCallback = pCallback;
  566. break;
  567. default :
  568. /* Update the error code */
  569. hfmpsmbus->ErrorCode |= HAL_FMPSMBUS_ERROR_INVALID_CALLBACK;
  570. /* Return error status */
  571. status = HAL_ERROR;
  572. break;
  573. }
  574. }
  575. else
  576. {
  577. /* Update the error code */
  578. hfmpsmbus->ErrorCode |= HAL_FMPSMBUS_ERROR_INVALID_CALLBACK;
  579. /* Return error status */
  580. status = HAL_ERROR;
  581. }
  582. /* Release Lock */
  583. __HAL_UNLOCK(hfmpsmbus);
  584. return status;
  585. }
  586. /**
  587. * @brief Unregister an FMPSMBUS Callback
  588. * FMPSMBUS callback is redirected to the weak predefined callback
  589. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  590. * the configuration information for the specified FMPSMBUS.
  591. * @param CallbackID ID of the callback to be unregistered
  592. * This parameter can be one of the following values:
  593. * This parameter can be one of the following values:
  594. * @arg @ref HAL_FMPSMBUS_MASTER_TX_COMPLETE_CB_ID Master Tx Transfer completed callback ID
  595. * @arg @ref HAL_FMPSMBUS_MASTER_RX_COMPLETE_CB_ID Master Rx Transfer completed callback ID
  596. * @arg @ref HAL_FMPSMBUS_SLAVE_TX_COMPLETE_CB_ID Slave Tx Transfer completed callback ID
  597. * @arg @ref HAL_FMPSMBUS_SLAVE_RX_COMPLETE_CB_ID Slave Rx Transfer completed callback ID
  598. * @arg @ref HAL_FMPSMBUS_LISTEN_COMPLETE_CB_ID Listen Complete callback ID
  599. * @arg @ref HAL_FMPSMBUS_ERROR_CB_ID Error callback ID
  600. * @arg @ref HAL_FMPSMBUS_MSPINIT_CB_ID MspInit callback ID
  601. * @arg @ref HAL_FMPSMBUS_MSPDEINIT_CB_ID MspDeInit callback ID
  602. * @retval HAL status
  603. */
  604. HAL_StatusTypeDef HAL_FMPSMBUS_UnRegisterCallback(FMPSMBUS_HandleTypeDef *hfmpsmbus,
  605. HAL_FMPSMBUS_CallbackIDTypeDef CallbackID)
  606. {
  607. HAL_StatusTypeDef status = HAL_OK;
  608. /* Process locked */
  609. __HAL_LOCK(hfmpsmbus);
  610. if (HAL_FMPSMBUS_STATE_READY == hfmpsmbus->State)
  611. {
  612. switch (CallbackID)
  613. {
  614. case HAL_FMPSMBUS_MASTER_TX_COMPLETE_CB_ID :
  615. hfmpsmbus->MasterTxCpltCallback = HAL_FMPSMBUS_MasterTxCpltCallback; /* Legacy weak MasterTxCpltCallback */
  616. break;
  617. case HAL_FMPSMBUS_MASTER_RX_COMPLETE_CB_ID :
  618. hfmpsmbus->MasterRxCpltCallback = HAL_FMPSMBUS_MasterRxCpltCallback; /* Legacy weak MasterRxCpltCallback */
  619. break;
  620. case HAL_FMPSMBUS_SLAVE_TX_COMPLETE_CB_ID :
  621. hfmpsmbus->SlaveTxCpltCallback = HAL_FMPSMBUS_SlaveTxCpltCallback; /* Legacy weak SlaveTxCpltCallback */
  622. break;
  623. case HAL_FMPSMBUS_SLAVE_RX_COMPLETE_CB_ID :
  624. hfmpsmbus->SlaveRxCpltCallback = HAL_FMPSMBUS_SlaveRxCpltCallback; /* Legacy weak SlaveRxCpltCallback */
  625. break;
  626. case HAL_FMPSMBUS_LISTEN_COMPLETE_CB_ID :
  627. hfmpsmbus->ListenCpltCallback = HAL_FMPSMBUS_ListenCpltCallback; /* Legacy weak ListenCpltCallback */
  628. break;
  629. case HAL_FMPSMBUS_ERROR_CB_ID :
  630. hfmpsmbus->ErrorCallback = HAL_FMPSMBUS_ErrorCallback; /* Legacy weak ErrorCallback */
  631. break;
  632. case HAL_FMPSMBUS_MSPINIT_CB_ID :
  633. hfmpsmbus->MspInitCallback = HAL_FMPSMBUS_MspInit; /* Legacy weak MspInit */
  634. break;
  635. case HAL_FMPSMBUS_MSPDEINIT_CB_ID :
  636. hfmpsmbus->MspDeInitCallback = HAL_FMPSMBUS_MspDeInit; /* Legacy weak MspDeInit */
  637. break;
  638. default :
  639. /* Update the error code */
  640. hfmpsmbus->ErrorCode |= HAL_FMPSMBUS_ERROR_INVALID_CALLBACK;
  641. /* Return error status */
  642. status = HAL_ERROR;
  643. break;
  644. }
  645. }
  646. else if (HAL_FMPSMBUS_STATE_RESET == hfmpsmbus->State)
  647. {
  648. switch (CallbackID)
  649. {
  650. case HAL_FMPSMBUS_MSPINIT_CB_ID :
  651. hfmpsmbus->MspInitCallback = HAL_FMPSMBUS_MspInit; /* Legacy weak MspInit */
  652. break;
  653. case HAL_FMPSMBUS_MSPDEINIT_CB_ID :
  654. hfmpsmbus->MspDeInitCallback = HAL_FMPSMBUS_MspDeInit; /* Legacy weak MspDeInit */
  655. break;
  656. default :
  657. /* Update the error code */
  658. hfmpsmbus->ErrorCode |= HAL_FMPSMBUS_ERROR_INVALID_CALLBACK;
  659. /* Return error status */
  660. status = HAL_ERROR;
  661. break;
  662. }
  663. }
  664. else
  665. {
  666. /* Update the error code */
  667. hfmpsmbus->ErrorCode |= HAL_FMPSMBUS_ERROR_INVALID_CALLBACK;
  668. /* Return error status */
  669. status = HAL_ERROR;
  670. }
  671. /* Release Lock */
  672. __HAL_UNLOCK(hfmpsmbus);
  673. return status;
  674. }
  675. /**
  676. * @brief Register the Slave Address Match FMPSMBUS Callback
  677. * To be used instead of the weak HAL_FMPSMBUS_AddrCallback() predefined callback
  678. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  679. * the configuration information for the specified FMPSMBUS.
  680. * @param pCallback pointer to the Address Match Callback function
  681. * @retval HAL status
  682. */
  683. HAL_StatusTypeDef HAL_FMPSMBUS_RegisterAddrCallback(FMPSMBUS_HandleTypeDef *hfmpsmbus,
  684. pFMPSMBUS_AddrCallbackTypeDef pCallback)
  685. {
  686. HAL_StatusTypeDef status = HAL_OK;
  687. if (pCallback == NULL)
  688. {
  689. /* Update the error code */
  690. hfmpsmbus->ErrorCode |= HAL_FMPSMBUS_ERROR_INVALID_CALLBACK;
  691. return HAL_ERROR;
  692. }
  693. /* Process locked */
  694. __HAL_LOCK(hfmpsmbus);
  695. if (HAL_FMPSMBUS_STATE_READY == hfmpsmbus->State)
  696. {
  697. hfmpsmbus->AddrCallback = pCallback;
  698. }
  699. else
  700. {
  701. /* Update the error code */
  702. hfmpsmbus->ErrorCode |= HAL_FMPSMBUS_ERROR_INVALID_CALLBACK;
  703. /* Return error status */
  704. status = HAL_ERROR;
  705. }
  706. /* Release Lock */
  707. __HAL_UNLOCK(hfmpsmbus);
  708. return status;
  709. }
  710. /**
  711. * @brief UnRegister the Slave Address Match FMPSMBUS Callback
  712. * Info Ready FMPSMBUS Callback is redirected to the weak HAL_FMPSMBUS_AddrCallback() predefined callback
  713. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  714. * the configuration information for the specified FMPSMBUS.
  715. * @retval HAL status
  716. */
  717. HAL_StatusTypeDef HAL_FMPSMBUS_UnRegisterAddrCallback(FMPSMBUS_HandleTypeDef *hfmpsmbus)
  718. {
  719. HAL_StatusTypeDef status = HAL_OK;
  720. /* Process locked */
  721. __HAL_LOCK(hfmpsmbus);
  722. if (HAL_FMPSMBUS_STATE_READY == hfmpsmbus->State)
  723. {
  724. hfmpsmbus->AddrCallback = HAL_FMPSMBUS_AddrCallback; /* Legacy weak AddrCallback */
  725. }
  726. else
  727. {
  728. /* Update the error code */
  729. hfmpsmbus->ErrorCode |= HAL_FMPSMBUS_ERROR_INVALID_CALLBACK;
  730. /* Return error status */
  731. status = HAL_ERROR;
  732. }
  733. /* Release Lock */
  734. __HAL_UNLOCK(hfmpsmbus);
  735. return status;
  736. }
  737. #endif /* USE_HAL_FMPSMBUS_REGISTER_CALLBACKS */
  738. /**
  739. * @}
  740. */
  741. /** @defgroup FMPSMBUS_Exported_Functions_Group2 Input and Output operation functions
  742. * @brief Data transfers functions
  743. *
  744. @verbatim
  745. ===============================================================================
  746. ##### IO operation functions #####
  747. ===============================================================================
  748. [..]
  749. This subsection provides a set of functions allowing to manage the FMPSMBUS data
  750. transfers.
  751. (#) Blocking mode function to check if device is ready for usage is :
  752. (++) HAL_FMPSMBUS_IsDeviceReady()
  753. (#) There is only one mode of transfer:
  754. (++) Non-Blocking mode : The communication is performed using Interrupts.
  755. These functions return the status of the transfer startup.
  756. The end of the data processing will be indicated through the
  757. dedicated FMPSMBUS IRQ when using Interrupt mode.
  758. (#) Non-Blocking mode functions with Interrupt are :
  759. (++) HAL_FMPSMBUS_Master_Transmit_IT()
  760. (++) HAL_FMPSMBUS_Master_Receive_IT()
  761. (++) HAL_FMPSMBUS_Slave_Transmit_IT()
  762. (++) HAL_FMPSMBUS_Slave_Receive_IT()
  763. (++) HAL_FMPSMBUS_EnableListen_IT() or alias HAL_FMPSMBUS_EnableListen_IT()
  764. (++) HAL_FMPSMBUS_DisableListen_IT()
  765. (++) HAL_FMPSMBUS_EnableAlert_IT()
  766. (++) HAL_FMPSMBUS_DisableAlert_IT()
  767. (#) A set of Transfer Complete Callbacks are provided in non-Blocking mode:
  768. (++) HAL_FMPSMBUS_MasterTxCpltCallback()
  769. (++) HAL_FMPSMBUS_MasterRxCpltCallback()
  770. (++) HAL_FMPSMBUS_SlaveTxCpltCallback()
  771. (++) HAL_FMPSMBUS_SlaveRxCpltCallback()
  772. (++) HAL_FMPSMBUS_AddrCallback()
  773. (++) HAL_FMPSMBUS_ListenCpltCallback()
  774. (++) HAL_FMPSMBUS_ErrorCallback()
  775. @endverbatim
  776. * @{
  777. */
  778. /**
  779. * @brief Transmit in master/host FMPSMBUS mode an amount of data in non-blocking mode with Interrupt.
  780. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  781. * the configuration information for the specified FMPSMBUS.
  782. * @param DevAddress Target device address: The device 7 bits address value
  783. * in datasheet must be shifted to the left before calling the interface
  784. * @param pData Pointer to data buffer
  785. * @param Size Amount of data to be sent
  786. * @param XferOptions Options of Transfer, value of @ref FMPSMBUS_XferOptions_definition
  787. * @retval HAL status
  788. */
  789. HAL_StatusTypeDef HAL_FMPSMBUS_Master_Transmit_IT(FMPSMBUS_HandleTypeDef *hfmpsmbus, uint16_t DevAddress,
  790. uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  791. {
  792. uint32_t tmp;
  793. /* Check the parameters */
  794. assert_param(IS_FMPSMBUS_TRANSFER_OPTIONS_REQUEST(XferOptions));
  795. if (hfmpsmbus->State == HAL_FMPSMBUS_STATE_READY)
  796. {
  797. /* Process Locked */
  798. __HAL_LOCK(hfmpsmbus);
  799. hfmpsmbus->State = HAL_FMPSMBUS_STATE_MASTER_BUSY_TX;
  800. hfmpsmbus->ErrorCode = HAL_FMPSMBUS_ERROR_NONE;
  801. /* Prepare transfer parameters */
  802. hfmpsmbus->pBuffPtr = pData;
  803. hfmpsmbus->XferCount = Size;
  804. hfmpsmbus->XferOptions = XferOptions;
  805. /* In case of Quick command, remove autoend mode */
  806. /* Manage the stop generation by software */
  807. if (hfmpsmbus->pBuffPtr == NULL)
  808. {
  809. hfmpsmbus->XferOptions &= ~FMPSMBUS_AUTOEND_MODE;
  810. }
  811. if (Size > MAX_NBYTE_SIZE)
  812. {
  813. hfmpsmbus->XferSize = MAX_NBYTE_SIZE;
  814. }
  815. else
  816. {
  817. hfmpsmbus->XferSize = Size;
  818. }
  819. /* Send Slave Address */
  820. /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE and generate RESTART */
  821. if ((hfmpsmbus->XferSize < hfmpsmbus->XferCount) && (hfmpsmbus->XferSize == MAX_NBYTE_SIZE))
  822. {
  823. FMPSMBUS_TransferConfig(hfmpsmbus, DevAddress, (uint8_t)hfmpsmbus->XferSize,
  824. FMPSMBUS_RELOAD_MODE | (hfmpsmbus->XferOptions & FMPSMBUS_SENDPEC_MODE),
  825. FMPSMBUS_GENERATE_START_WRITE);
  826. }
  827. else
  828. {
  829. /* If transfer direction not change, do not generate Restart Condition */
  830. /* Mean Previous state is same as current state */
  831. /* Store current volatile XferOptions, misra rule */
  832. tmp = hfmpsmbus->XferOptions;
  833. if ((hfmpsmbus->PreviousState == HAL_FMPSMBUS_STATE_MASTER_BUSY_TX) && \
  834. (IS_FMPSMBUS_TRANSFER_OTHER_OPTIONS_REQUEST(tmp) == 0))
  835. {
  836. FMPSMBUS_TransferConfig(hfmpsmbus, DevAddress, (uint8_t)hfmpsmbus->XferSize, hfmpsmbus->XferOptions,
  837. FMPSMBUS_NO_STARTSTOP);
  838. }
  839. /* Else transfer direction change, so generate Restart with new transfer direction */
  840. else
  841. {
  842. /* Convert OTHER_xxx XferOptions if any */
  843. FMPSMBUS_ConvertOtherXferOptions(hfmpsmbus);
  844. /* Handle Transfer */
  845. FMPSMBUS_TransferConfig(hfmpsmbus, DevAddress, (uint8_t)hfmpsmbus->XferSize,
  846. hfmpsmbus->XferOptions,
  847. FMPSMBUS_GENERATE_START_WRITE);
  848. }
  849. /* If PEC mode is enable, size to transmit manage by SW part should be Size-1 byte, corresponding to PEC byte */
  850. /* PEC byte is automatically sent by HW block, no need to manage it in Transmit process */
  851. if (FMPSMBUS_GET_PEC_MODE(hfmpsmbus) != 0UL)
  852. {
  853. hfmpsmbus->XferSize--;
  854. hfmpsmbus->XferCount--;
  855. }
  856. }
  857. /* Process Unlocked */
  858. __HAL_UNLOCK(hfmpsmbus);
  859. /* Note : The FMPSMBUS interrupts must be enabled after unlocking current process
  860. to avoid the risk of FMPSMBUS interrupt handle execution before current
  861. process unlock */
  862. FMPSMBUS_Enable_IRQ(hfmpsmbus, FMPSMBUS_IT_TX);
  863. return HAL_OK;
  864. }
  865. else
  866. {
  867. return HAL_BUSY;
  868. }
  869. }
  870. /**
  871. * @brief Receive in master/host FMPSMBUS mode an amount of data in non-blocking mode with Interrupt.
  872. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  873. * the configuration information for the specified FMPSMBUS.
  874. * @param DevAddress Target device address: The device 7 bits address value
  875. * in datasheet must be shifted to the left before calling the interface
  876. * @param pData Pointer to data buffer
  877. * @param Size Amount of data to be sent
  878. * @param XferOptions Options of Transfer, value of @ref FMPSMBUS_XferOptions_definition
  879. * @retval HAL status
  880. */
  881. HAL_StatusTypeDef HAL_FMPSMBUS_Master_Receive_IT(FMPSMBUS_HandleTypeDef *hfmpsmbus, uint16_t DevAddress, uint8_t *pData,
  882. uint16_t Size, uint32_t XferOptions)
  883. {
  884. uint32_t tmp;
  885. /* Check the parameters */
  886. assert_param(IS_FMPSMBUS_TRANSFER_OPTIONS_REQUEST(XferOptions));
  887. if (hfmpsmbus->State == HAL_FMPSMBUS_STATE_READY)
  888. {
  889. /* Process Locked */
  890. __HAL_LOCK(hfmpsmbus);
  891. hfmpsmbus->State = HAL_FMPSMBUS_STATE_MASTER_BUSY_RX;
  892. hfmpsmbus->ErrorCode = HAL_FMPSMBUS_ERROR_NONE;
  893. /* Prepare transfer parameters */
  894. hfmpsmbus->pBuffPtr = pData;
  895. hfmpsmbus->XferCount = Size;
  896. hfmpsmbus->XferOptions = XferOptions;
  897. /* In case of Quick command, remove autoend mode */
  898. /* Manage the stop generation by software */
  899. if (hfmpsmbus->pBuffPtr == NULL)
  900. {
  901. hfmpsmbus->XferOptions &= ~FMPSMBUS_AUTOEND_MODE;
  902. }
  903. if (Size > MAX_NBYTE_SIZE)
  904. {
  905. hfmpsmbus->XferSize = MAX_NBYTE_SIZE;
  906. }
  907. else
  908. {
  909. hfmpsmbus->XferSize = Size;
  910. }
  911. /* Send Slave Address */
  912. /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE and generate RESTART */
  913. if ((hfmpsmbus->XferSize < hfmpsmbus->XferCount) && (hfmpsmbus->XferSize == MAX_NBYTE_SIZE))
  914. {
  915. FMPSMBUS_TransferConfig(hfmpsmbus, DevAddress, (uint8_t)hfmpsmbus->XferSize,
  916. FMPSMBUS_RELOAD_MODE | (hfmpsmbus->XferOptions & FMPSMBUS_SENDPEC_MODE),
  917. FMPSMBUS_GENERATE_START_READ);
  918. }
  919. else
  920. {
  921. /* If transfer direction not change, do not generate Restart Condition */
  922. /* Mean Previous state is same as current state */
  923. /* Store current volatile XferOptions, Misra rule */
  924. tmp = hfmpsmbus->XferOptions;
  925. if ((hfmpsmbus->PreviousState == HAL_FMPSMBUS_STATE_MASTER_BUSY_RX) && \
  926. (IS_FMPSMBUS_TRANSFER_OTHER_OPTIONS_REQUEST(tmp) == 0))
  927. {
  928. FMPSMBUS_TransferConfig(hfmpsmbus, DevAddress, (uint8_t)hfmpsmbus->XferSize, hfmpsmbus->XferOptions,
  929. FMPSMBUS_NO_STARTSTOP);
  930. }
  931. /* Else transfer direction change, so generate Restart with new transfer direction */
  932. else
  933. {
  934. /* Convert OTHER_xxx XferOptions if any */
  935. FMPSMBUS_ConvertOtherXferOptions(hfmpsmbus);
  936. /* Handle Transfer */
  937. FMPSMBUS_TransferConfig(hfmpsmbus, DevAddress, (uint8_t)hfmpsmbus->XferSize,
  938. hfmpsmbus->XferOptions,
  939. FMPSMBUS_GENERATE_START_READ);
  940. }
  941. }
  942. /* Process Unlocked */
  943. __HAL_UNLOCK(hfmpsmbus);
  944. /* Note : The FMPSMBUS interrupts must be enabled after unlocking current process
  945. to avoid the risk of FMPSMBUS interrupt handle execution before current
  946. process unlock */
  947. FMPSMBUS_Enable_IRQ(hfmpsmbus, FMPSMBUS_IT_RX);
  948. return HAL_OK;
  949. }
  950. else
  951. {
  952. return HAL_BUSY;
  953. }
  954. }
  955. /**
  956. * @brief Abort a master/host FMPSMBUS process communication with Interrupt.
  957. * @note This abort can be called only if state is ready
  958. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  959. * the configuration information for the specified FMPSMBUS.
  960. * @param DevAddress Target device address: The device 7 bits address value
  961. * in datasheet must be shifted to the left before calling the interface
  962. * @retval HAL status
  963. */
  964. HAL_StatusTypeDef HAL_FMPSMBUS_Master_Abort_IT(FMPSMBUS_HandleTypeDef *hfmpsmbus, uint16_t DevAddress)
  965. {
  966. if (hfmpsmbus->State == HAL_FMPSMBUS_STATE_READY)
  967. {
  968. /* Process Locked */
  969. __HAL_LOCK(hfmpsmbus);
  970. /* Keep the same state as previous */
  971. /* to perform as well the call of the corresponding end of transfer callback */
  972. if (hfmpsmbus->PreviousState == HAL_FMPSMBUS_STATE_MASTER_BUSY_TX)
  973. {
  974. hfmpsmbus->State = HAL_FMPSMBUS_STATE_MASTER_BUSY_TX;
  975. }
  976. else if (hfmpsmbus->PreviousState == HAL_FMPSMBUS_STATE_MASTER_BUSY_RX)
  977. {
  978. hfmpsmbus->State = HAL_FMPSMBUS_STATE_MASTER_BUSY_RX;
  979. }
  980. else
  981. {
  982. /* Wrong usage of abort function */
  983. /* This function should be used only in case of abort monitored by master device */
  984. return HAL_ERROR;
  985. }
  986. hfmpsmbus->ErrorCode = HAL_FMPSMBUS_ERROR_NONE;
  987. /* Set NBYTES to 1 to generate a dummy read on FMPSMBUS peripheral */
  988. /* Set AUTOEND mode, this will generate a NACK then STOP condition to abort the current transfer */
  989. FMPSMBUS_TransferConfig(hfmpsmbus, DevAddress, 1, FMPSMBUS_AUTOEND_MODE, FMPSMBUS_NO_STARTSTOP);
  990. /* Process Unlocked */
  991. __HAL_UNLOCK(hfmpsmbus);
  992. /* Note : The FMPSMBUS interrupts must be enabled after unlocking current process
  993. to avoid the risk of FMPSMBUS interrupt handle execution before current
  994. process unlock */
  995. if (hfmpsmbus->State == HAL_FMPSMBUS_STATE_MASTER_BUSY_TX)
  996. {
  997. FMPSMBUS_Enable_IRQ(hfmpsmbus, FMPSMBUS_IT_TX);
  998. }
  999. else if (hfmpsmbus->State == HAL_FMPSMBUS_STATE_MASTER_BUSY_RX)
  1000. {
  1001. FMPSMBUS_Enable_IRQ(hfmpsmbus, FMPSMBUS_IT_RX);
  1002. }
  1003. else
  1004. {
  1005. /* Nothing to do */
  1006. }
  1007. return HAL_OK;
  1008. }
  1009. else
  1010. {
  1011. return HAL_BUSY;
  1012. }
  1013. }
  1014. /**
  1015. * @brief Transmit in slave/device FMPSMBUS mode an amount of data in non-blocking mode with Interrupt.
  1016. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  1017. * the configuration information for the specified FMPSMBUS.
  1018. * @param pData Pointer to data buffer
  1019. * @param Size Amount of data to be sent
  1020. * @param XferOptions Options of Transfer, value of @ref FMPSMBUS_XferOptions_definition
  1021. * @retval HAL status
  1022. */
  1023. HAL_StatusTypeDef HAL_FMPSMBUS_Slave_Transmit_IT(FMPSMBUS_HandleTypeDef *hfmpsmbus, uint8_t *pData, uint16_t Size,
  1024. uint32_t XferOptions)
  1025. {
  1026. /* Check the parameters */
  1027. assert_param(IS_FMPSMBUS_TRANSFER_OPTIONS_REQUEST(XferOptions));
  1028. if ((hfmpsmbus->State & HAL_FMPSMBUS_STATE_LISTEN) == HAL_FMPSMBUS_STATE_LISTEN)
  1029. {
  1030. if ((pData == NULL) || (Size == 0UL))
  1031. {
  1032. hfmpsmbus->ErrorCode = HAL_FMPSMBUS_ERROR_INVALID_PARAM;
  1033. return HAL_ERROR;
  1034. }
  1035. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  1036. FMPSMBUS_Disable_IRQ(hfmpsmbus, FMPSMBUS_IT_ADDR | FMPSMBUS_IT_TX);
  1037. /* Process Locked */
  1038. __HAL_LOCK(hfmpsmbus);
  1039. hfmpsmbus->State = (HAL_FMPSMBUS_STATE_SLAVE_BUSY_TX | HAL_FMPSMBUS_STATE_LISTEN);
  1040. hfmpsmbus->ErrorCode = HAL_FMPSMBUS_ERROR_NONE;
  1041. /* Set SBC bit to manage Acknowledge at each bit */
  1042. hfmpsmbus->Instance->CR1 |= FMPI2C_CR1_SBC;
  1043. /* Enable Address Acknowledge */
  1044. hfmpsmbus->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  1045. /* Prepare transfer parameters */
  1046. hfmpsmbus->pBuffPtr = pData;
  1047. hfmpsmbus->XferCount = Size;
  1048. hfmpsmbus->XferOptions = XferOptions;
  1049. /* Convert OTHER_xxx XferOptions if any */
  1050. FMPSMBUS_ConvertOtherXferOptions(hfmpsmbus);
  1051. if (Size > MAX_NBYTE_SIZE)
  1052. {
  1053. hfmpsmbus->XferSize = MAX_NBYTE_SIZE;
  1054. }
  1055. else
  1056. {
  1057. hfmpsmbus->XferSize = Size;
  1058. }
  1059. /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE and generate RESTART */
  1060. if ((hfmpsmbus->XferSize < hfmpsmbus->XferCount) && (hfmpsmbus->XferSize == MAX_NBYTE_SIZE))
  1061. {
  1062. FMPSMBUS_TransferConfig(hfmpsmbus, 0, (uint8_t)hfmpsmbus->XferSize,
  1063. FMPSMBUS_RELOAD_MODE | (hfmpsmbus->XferOptions & FMPSMBUS_SENDPEC_MODE),
  1064. FMPSMBUS_NO_STARTSTOP);
  1065. }
  1066. else
  1067. {
  1068. /* Set NBYTE to transmit */
  1069. FMPSMBUS_TransferConfig(hfmpsmbus, 0, (uint8_t)hfmpsmbus->XferSize, hfmpsmbus->XferOptions,
  1070. FMPSMBUS_NO_STARTSTOP);
  1071. /* If PEC mode is enable, size to transmit should be Size-1 byte, corresponding to PEC byte */
  1072. /* PEC byte is automatically sent by HW block, no need to manage it in Transmit process */
  1073. if (FMPSMBUS_GET_PEC_MODE(hfmpsmbus) != 0UL)
  1074. {
  1075. hfmpsmbus->XferSize--;
  1076. hfmpsmbus->XferCount--;
  1077. }
  1078. }
  1079. /* Clear ADDR flag after prepare the transfer parameters */
  1080. /* This action will generate an acknowledge to the HOST */
  1081. __HAL_FMPSMBUS_CLEAR_FLAG(hfmpsmbus, FMPSMBUS_FLAG_ADDR);
  1082. /* Process Unlocked */
  1083. __HAL_UNLOCK(hfmpsmbus);
  1084. /* Note : The FMPSMBUS interrupts must be enabled after unlocking current process
  1085. to avoid the risk of FMPSMBUS interrupt handle execution before current
  1086. process unlock */
  1087. /* REnable ADDR interrupt */
  1088. FMPSMBUS_Enable_IRQ(hfmpsmbus, FMPSMBUS_IT_TX | FMPSMBUS_IT_ADDR);
  1089. return HAL_OK;
  1090. }
  1091. else
  1092. {
  1093. return HAL_BUSY;
  1094. }
  1095. }
  1096. /**
  1097. * @brief Receive in slave/device FMPSMBUS mode an amount of data in non-blocking mode with Interrupt.
  1098. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  1099. * the configuration information for the specified FMPSMBUS.
  1100. * @param pData Pointer to data buffer
  1101. * @param Size Amount of data to be sent
  1102. * @param XferOptions Options of Transfer, value of @ref FMPSMBUS_XferOptions_definition
  1103. * @retval HAL status
  1104. */
  1105. HAL_StatusTypeDef HAL_FMPSMBUS_Slave_Receive_IT(FMPSMBUS_HandleTypeDef *hfmpsmbus, uint8_t *pData, uint16_t Size,
  1106. uint32_t XferOptions)
  1107. {
  1108. /* Check the parameters */
  1109. assert_param(IS_FMPSMBUS_TRANSFER_OPTIONS_REQUEST(XferOptions));
  1110. if ((hfmpsmbus->State & HAL_FMPSMBUS_STATE_LISTEN) == HAL_FMPSMBUS_STATE_LISTEN)
  1111. {
  1112. if ((pData == NULL) || (Size == 0UL))
  1113. {
  1114. hfmpsmbus->ErrorCode = HAL_FMPSMBUS_ERROR_INVALID_PARAM;
  1115. return HAL_ERROR;
  1116. }
  1117. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  1118. FMPSMBUS_Disable_IRQ(hfmpsmbus, FMPSMBUS_IT_ADDR | FMPSMBUS_IT_RX);
  1119. /* Process Locked */
  1120. __HAL_LOCK(hfmpsmbus);
  1121. hfmpsmbus->State = (HAL_FMPSMBUS_STATE_SLAVE_BUSY_RX | HAL_FMPSMBUS_STATE_LISTEN);
  1122. hfmpsmbus->ErrorCode = HAL_FMPSMBUS_ERROR_NONE;
  1123. /* Set SBC bit to manage Acknowledge at each bit */
  1124. hfmpsmbus->Instance->CR1 |= FMPI2C_CR1_SBC;
  1125. /* Enable Address Acknowledge */
  1126. hfmpsmbus->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  1127. /* Prepare transfer parameters */
  1128. hfmpsmbus->pBuffPtr = pData;
  1129. hfmpsmbus->XferSize = Size;
  1130. hfmpsmbus->XferCount = Size;
  1131. hfmpsmbus->XferOptions = XferOptions;
  1132. /* Convert OTHER_xxx XferOptions if any */
  1133. FMPSMBUS_ConvertOtherXferOptions(hfmpsmbus);
  1134. /* Set NBYTE to receive */
  1135. /* If XferSize equal "1", or XferSize equal "2" with PEC requested (mean 1 data byte + 1 PEC byte */
  1136. /* no need to set RELOAD bit mode, a ACK will be automatically generated in that case */
  1137. /* else need to set RELOAD bit mode to generate an automatic ACK at each byte Received */
  1138. /* This RELOAD bit will be reset for last BYTE to be receive in FMPSMBUS_Slave_ISR */
  1139. if (((FMPSMBUS_GET_PEC_MODE(hfmpsmbus) != 0UL) && (hfmpsmbus->XferSize == 2U)) || (hfmpsmbus->XferSize == 1U))
  1140. {
  1141. FMPSMBUS_TransferConfig(hfmpsmbus, 0, (uint8_t)hfmpsmbus->XferSize, hfmpsmbus->XferOptions,
  1142. FMPSMBUS_NO_STARTSTOP);
  1143. }
  1144. else
  1145. {
  1146. FMPSMBUS_TransferConfig(hfmpsmbus, 0, 1, hfmpsmbus->XferOptions | FMPSMBUS_RELOAD_MODE, FMPSMBUS_NO_STARTSTOP);
  1147. }
  1148. /* Clear ADDR flag after prepare the transfer parameters */
  1149. /* This action will generate an acknowledge to the HOST */
  1150. __HAL_FMPSMBUS_CLEAR_FLAG(hfmpsmbus, FMPSMBUS_FLAG_ADDR);
  1151. /* Process Unlocked */
  1152. __HAL_UNLOCK(hfmpsmbus);
  1153. /* Note : The FMPSMBUS interrupts must be enabled after unlocking current process
  1154. to avoid the risk of FMPSMBUS interrupt handle execution before current
  1155. process unlock */
  1156. /* REnable ADDR interrupt */
  1157. FMPSMBUS_Enable_IRQ(hfmpsmbus, FMPSMBUS_IT_RX | FMPSMBUS_IT_ADDR);
  1158. return HAL_OK;
  1159. }
  1160. else
  1161. {
  1162. return HAL_BUSY;
  1163. }
  1164. }
  1165. /**
  1166. * @brief Enable the Address listen mode with Interrupt.
  1167. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  1168. * the configuration information for the specified FMPSMBUS.
  1169. * @retval HAL status
  1170. */
  1171. HAL_StatusTypeDef HAL_FMPSMBUS_EnableListen_IT(FMPSMBUS_HandleTypeDef *hfmpsmbus)
  1172. {
  1173. hfmpsmbus->State = HAL_FMPSMBUS_STATE_LISTEN;
  1174. /* Enable the Address Match interrupt */
  1175. FMPSMBUS_Enable_IRQ(hfmpsmbus, FMPSMBUS_IT_ADDR);
  1176. return HAL_OK;
  1177. }
  1178. /**
  1179. * @brief Disable the Address listen mode with Interrupt.
  1180. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  1181. * the configuration information for the specified FMPSMBUS.
  1182. * @retval HAL status
  1183. */
  1184. HAL_StatusTypeDef HAL_FMPSMBUS_DisableListen_IT(FMPSMBUS_HandleTypeDef *hfmpsmbus)
  1185. {
  1186. /* Disable Address listen mode only if a transfer is not ongoing */
  1187. if (hfmpsmbus->State == HAL_FMPSMBUS_STATE_LISTEN)
  1188. {
  1189. hfmpsmbus->State = HAL_FMPSMBUS_STATE_READY;
  1190. /* Disable the Address Match interrupt */
  1191. FMPSMBUS_Disable_IRQ(hfmpsmbus, FMPSMBUS_IT_ADDR);
  1192. return HAL_OK;
  1193. }
  1194. else
  1195. {
  1196. return HAL_BUSY;
  1197. }
  1198. }
  1199. /**
  1200. * @brief Enable the FMPSMBUS alert mode with Interrupt.
  1201. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  1202. * the configuration information for the specified FMPSMBUSx peripheral.
  1203. * @retval HAL status
  1204. */
  1205. HAL_StatusTypeDef HAL_FMPSMBUS_EnableAlert_IT(FMPSMBUS_HandleTypeDef *hfmpsmbus)
  1206. {
  1207. /* Enable SMBus alert */
  1208. hfmpsmbus->Instance->CR1 |= FMPI2C_CR1_ALERTEN;
  1209. /* Clear ALERT flag */
  1210. __HAL_FMPSMBUS_CLEAR_FLAG(hfmpsmbus, FMPSMBUS_FLAG_ALERT);
  1211. /* Enable Alert Interrupt */
  1212. FMPSMBUS_Enable_IRQ(hfmpsmbus, FMPSMBUS_IT_ALERT);
  1213. return HAL_OK;
  1214. }
  1215. /**
  1216. * @brief Disable the FMPSMBUS alert mode with Interrupt.
  1217. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  1218. * the configuration information for the specified FMPSMBUSx peripheral.
  1219. * @retval HAL status
  1220. */
  1221. HAL_StatusTypeDef HAL_FMPSMBUS_DisableAlert_IT(FMPSMBUS_HandleTypeDef *hfmpsmbus)
  1222. {
  1223. /* Enable SMBus alert */
  1224. hfmpsmbus->Instance->CR1 &= ~FMPI2C_CR1_ALERTEN;
  1225. /* Disable Alert Interrupt */
  1226. FMPSMBUS_Disable_IRQ(hfmpsmbus, FMPSMBUS_IT_ALERT);
  1227. return HAL_OK;
  1228. }
  1229. /**
  1230. * @brief Check if target device is ready for communication.
  1231. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  1232. * the configuration information for the specified FMPSMBUS.
  1233. * @param DevAddress Target device address: The device 7 bits address value
  1234. * in datasheet must be shifted to the left before calling the interface
  1235. * @param Trials Number of trials
  1236. * @param Timeout Timeout duration
  1237. * @retval HAL status
  1238. */
  1239. HAL_StatusTypeDef HAL_FMPSMBUS_IsDeviceReady(FMPSMBUS_HandleTypeDef *hfmpsmbus, uint16_t DevAddress, uint32_t Trials,
  1240. uint32_t Timeout)
  1241. {
  1242. uint32_t tickstart;
  1243. __IO uint32_t FMPSMBUS_Trials = 0UL;
  1244. FlagStatus tmp1;
  1245. FlagStatus tmp2;
  1246. if (hfmpsmbus->State == HAL_FMPSMBUS_STATE_READY)
  1247. {
  1248. if (__HAL_FMPSMBUS_GET_FLAG(hfmpsmbus, FMPSMBUS_FLAG_BUSY) != RESET)
  1249. {
  1250. return HAL_BUSY;
  1251. }
  1252. /* Process Locked */
  1253. __HAL_LOCK(hfmpsmbus);
  1254. hfmpsmbus->State = HAL_FMPSMBUS_STATE_BUSY;
  1255. hfmpsmbus->ErrorCode = HAL_FMPSMBUS_ERROR_NONE;
  1256. do
  1257. {
  1258. /* Generate Start */
  1259. hfmpsmbus->Instance->CR2 = FMPSMBUS_GENERATE_START(hfmpsmbus->Init.AddressingMode, DevAddress);
  1260. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  1261. /* Wait until STOPF flag is set or a NACK flag is set*/
  1262. tickstart = HAL_GetTick();
  1263. tmp1 = __HAL_FMPSMBUS_GET_FLAG(hfmpsmbus, FMPSMBUS_FLAG_STOPF);
  1264. tmp2 = __HAL_FMPSMBUS_GET_FLAG(hfmpsmbus, FMPSMBUS_FLAG_AF);
  1265. while ((tmp1 == RESET) && (tmp2 == RESET))
  1266. {
  1267. if (Timeout != HAL_MAX_DELAY)
  1268. {
  1269. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
  1270. {
  1271. /* Device is ready */
  1272. hfmpsmbus->State = HAL_FMPSMBUS_STATE_READY;
  1273. /* Update FMPSMBUS error code */
  1274. hfmpsmbus->ErrorCode |= HAL_FMPSMBUS_ERROR_HALTIMEOUT;
  1275. /* Process Unlocked */
  1276. __HAL_UNLOCK(hfmpsmbus);
  1277. return HAL_ERROR;
  1278. }
  1279. }
  1280. tmp1 = __HAL_FMPSMBUS_GET_FLAG(hfmpsmbus, FMPSMBUS_FLAG_STOPF);
  1281. tmp2 = __HAL_FMPSMBUS_GET_FLAG(hfmpsmbus, FMPSMBUS_FLAG_AF);
  1282. }
  1283. /* Check if the NACKF flag has not been set */
  1284. if (__HAL_FMPSMBUS_GET_FLAG(hfmpsmbus, FMPSMBUS_FLAG_AF) == RESET)
  1285. {
  1286. /* Wait until STOPF flag is reset */
  1287. if (FMPSMBUS_WaitOnFlagUntilTimeout(hfmpsmbus, FMPSMBUS_FLAG_STOPF, RESET, Timeout) != HAL_OK)
  1288. {
  1289. return HAL_ERROR;
  1290. }
  1291. /* Clear STOP Flag */
  1292. __HAL_FMPSMBUS_CLEAR_FLAG(hfmpsmbus, FMPSMBUS_FLAG_STOPF);
  1293. /* Device is ready */
  1294. hfmpsmbus->State = HAL_FMPSMBUS_STATE_READY;
  1295. /* Process Unlocked */
  1296. __HAL_UNLOCK(hfmpsmbus);
  1297. return HAL_OK;
  1298. }
  1299. else
  1300. {
  1301. /* Wait until STOPF flag is reset */
  1302. if (FMPSMBUS_WaitOnFlagUntilTimeout(hfmpsmbus, FMPSMBUS_FLAG_STOPF, RESET, Timeout) != HAL_OK)
  1303. {
  1304. return HAL_ERROR;
  1305. }
  1306. /* Clear NACK Flag */
  1307. __HAL_FMPSMBUS_CLEAR_FLAG(hfmpsmbus, FMPSMBUS_FLAG_AF);
  1308. /* Clear STOP Flag, auto generated with autoend*/
  1309. __HAL_FMPSMBUS_CLEAR_FLAG(hfmpsmbus, FMPSMBUS_FLAG_STOPF);
  1310. }
  1311. /* Check if the maximum allowed number of trials has been reached */
  1312. if (FMPSMBUS_Trials == Trials)
  1313. {
  1314. /* Generate Stop */
  1315. hfmpsmbus->Instance->CR2 |= FMPI2C_CR2_STOP;
  1316. /* Wait until STOPF flag is reset */
  1317. if (FMPSMBUS_WaitOnFlagUntilTimeout(hfmpsmbus, FMPSMBUS_FLAG_STOPF, RESET, Timeout) != HAL_OK)
  1318. {
  1319. return HAL_ERROR;
  1320. }
  1321. /* Clear STOP Flag */
  1322. __HAL_FMPSMBUS_CLEAR_FLAG(hfmpsmbus, FMPSMBUS_FLAG_STOPF);
  1323. }
  1324. /* Increment Trials */
  1325. FMPSMBUS_Trials++;
  1326. } while (FMPSMBUS_Trials < Trials);
  1327. hfmpsmbus->State = HAL_FMPSMBUS_STATE_READY;
  1328. /* Update FMPSMBUS error code */
  1329. hfmpsmbus->ErrorCode |= HAL_FMPSMBUS_ERROR_HALTIMEOUT;
  1330. /* Process Unlocked */
  1331. __HAL_UNLOCK(hfmpsmbus);
  1332. return HAL_ERROR;
  1333. }
  1334. else
  1335. {
  1336. return HAL_BUSY;
  1337. }
  1338. }
  1339. /**
  1340. * @}
  1341. */
  1342. /** @defgroup FMPSMBUS_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
  1343. * @{
  1344. */
  1345. /**
  1346. * @brief Handle FMPSMBUS event interrupt request.
  1347. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  1348. * the configuration information for the specified FMPSMBUS.
  1349. * @retval None
  1350. */
  1351. void HAL_FMPSMBUS_EV_IRQHandler(FMPSMBUS_HandleTypeDef *hfmpsmbus)
  1352. {
  1353. /* Use a local variable to store the current ISR flags */
  1354. /* This action will avoid a wrong treatment due to ISR flags change during interrupt handler */
  1355. uint32_t tmpisrvalue = READ_REG(hfmpsmbus->Instance->ISR);
  1356. uint32_t tmpcr1value = READ_REG(hfmpsmbus->Instance->CR1);
  1357. /* FMPSMBUS in mode Transmitter ---------------------------------------------------*/
  1358. if ((FMPSMBUS_CHECK_IT_SOURCE(tmpcr1value, (FMPSMBUS_IT_TCI | FMPSMBUS_IT_STOPI |
  1359. FMPSMBUS_IT_NACKI | FMPSMBUS_IT_TXI)) != RESET) &&
  1360. ((FMPSMBUS_CHECK_FLAG(tmpisrvalue, FMPSMBUS_FLAG_TXIS) != RESET) ||
  1361. (FMPSMBUS_CHECK_FLAG(tmpisrvalue, FMPSMBUS_FLAG_TCR) != RESET) ||
  1362. (FMPSMBUS_CHECK_FLAG(tmpisrvalue, FMPSMBUS_FLAG_TC) != RESET) ||
  1363. (FMPSMBUS_CHECK_FLAG(tmpisrvalue, FMPSMBUS_FLAG_STOPF) != RESET) ||
  1364. (FMPSMBUS_CHECK_FLAG(tmpisrvalue, FMPSMBUS_FLAG_AF) != RESET)))
  1365. {
  1366. /* Slave mode selected */
  1367. if ((hfmpsmbus->State & HAL_FMPSMBUS_STATE_SLAVE_BUSY_TX) == HAL_FMPSMBUS_STATE_SLAVE_BUSY_TX)
  1368. {
  1369. (void)FMPSMBUS_Slave_ISR(hfmpsmbus, tmpisrvalue);
  1370. }
  1371. /* Master mode selected */
  1372. else if ((hfmpsmbus->State & HAL_FMPSMBUS_STATE_MASTER_BUSY_TX) == HAL_FMPSMBUS_STATE_MASTER_BUSY_TX)
  1373. {
  1374. (void)FMPSMBUS_Master_ISR(hfmpsmbus, tmpisrvalue);
  1375. }
  1376. else
  1377. {
  1378. /* Nothing to do */
  1379. }
  1380. }
  1381. /* FMPSMBUS in mode Receiver ----------------------------------------------------*/
  1382. if ((FMPSMBUS_CHECK_IT_SOURCE(tmpcr1value, (FMPSMBUS_IT_TCI | FMPSMBUS_IT_STOPI |
  1383. FMPSMBUS_IT_NACKI | FMPSMBUS_IT_RXI)) != RESET) &&
  1384. ((FMPSMBUS_CHECK_FLAG(tmpisrvalue, FMPSMBUS_FLAG_RXNE) != RESET) ||
  1385. (FMPSMBUS_CHECK_FLAG(tmpisrvalue, FMPSMBUS_FLAG_TCR) != RESET) ||
  1386. (FMPSMBUS_CHECK_FLAG(tmpisrvalue, FMPSMBUS_FLAG_TC) != RESET) ||
  1387. (FMPSMBUS_CHECK_FLAG(tmpisrvalue, FMPSMBUS_FLAG_STOPF) != RESET) ||
  1388. (FMPSMBUS_CHECK_FLAG(tmpisrvalue, FMPSMBUS_FLAG_AF) != RESET)))
  1389. {
  1390. /* Slave mode selected */
  1391. if ((hfmpsmbus->State & HAL_FMPSMBUS_STATE_SLAVE_BUSY_RX) == HAL_FMPSMBUS_STATE_SLAVE_BUSY_RX)
  1392. {
  1393. (void)FMPSMBUS_Slave_ISR(hfmpsmbus, tmpisrvalue);
  1394. }
  1395. /* Master mode selected */
  1396. else if ((hfmpsmbus->State & HAL_FMPSMBUS_STATE_MASTER_BUSY_RX) == HAL_FMPSMBUS_STATE_MASTER_BUSY_RX)
  1397. {
  1398. (void)FMPSMBUS_Master_ISR(hfmpsmbus, tmpisrvalue);
  1399. }
  1400. else
  1401. {
  1402. /* Nothing to do */
  1403. }
  1404. }
  1405. /* FMPSMBUS in mode Listener Only --------------------------------------------------*/
  1406. if (((FMPSMBUS_CHECK_IT_SOURCE(tmpcr1value, FMPSMBUS_IT_ADDRI) != RESET) ||
  1407. (FMPSMBUS_CHECK_IT_SOURCE(tmpcr1value, FMPSMBUS_IT_STOPI) != RESET) ||
  1408. (FMPSMBUS_CHECK_IT_SOURCE(tmpcr1value, FMPSMBUS_IT_NACKI) != RESET)) &&
  1409. ((FMPSMBUS_CHECK_FLAG(tmpisrvalue, FMPSMBUS_FLAG_ADDR) != RESET) ||
  1410. (FMPSMBUS_CHECK_FLAG(tmpisrvalue, FMPSMBUS_FLAG_STOPF) != RESET) ||
  1411. (FMPSMBUS_CHECK_FLAG(tmpisrvalue, FMPSMBUS_FLAG_AF) != RESET)))
  1412. {
  1413. if ((hfmpsmbus->State & HAL_FMPSMBUS_STATE_LISTEN) == HAL_FMPSMBUS_STATE_LISTEN)
  1414. {
  1415. (void)FMPSMBUS_Slave_ISR(hfmpsmbus, tmpisrvalue);
  1416. }
  1417. }
  1418. }
  1419. /**
  1420. * @brief Handle FMPSMBUS error interrupt request.
  1421. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  1422. * the configuration information for the specified FMPSMBUS.
  1423. * @retval None
  1424. */
  1425. void HAL_FMPSMBUS_ER_IRQHandler(FMPSMBUS_HandleTypeDef *hfmpsmbus)
  1426. {
  1427. FMPSMBUS_ITErrorHandler(hfmpsmbus);
  1428. }
  1429. /**
  1430. * @brief Master Tx Transfer completed callback.
  1431. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  1432. * the configuration information for the specified FMPSMBUS.
  1433. * @retval None
  1434. */
  1435. __weak void HAL_FMPSMBUS_MasterTxCpltCallback(FMPSMBUS_HandleTypeDef *hfmpsmbus)
  1436. {
  1437. /* Prevent unused argument(s) compilation warning */
  1438. UNUSED(hfmpsmbus);
  1439. /* NOTE : This function should not be modified, when the callback is needed,
  1440. the HAL_FMPSMBUS_MasterTxCpltCallback() could be implemented in the user file
  1441. */
  1442. }
  1443. /**
  1444. * @brief Master Rx Transfer completed callback.
  1445. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  1446. * the configuration information for the specified FMPSMBUS.
  1447. * @retval None
  1448. */
  1449. __weak void HAL_FMPSMBUS_MasterRxCpltCallback(FMPSMBUS_HandleTypeDef *hfmpsmbus)
  1450. {
  1451. /* Prevent unused argument(s) compilation warning */
  1452. UNUSED(hfmpsmbus);
  1453. /* NOTE : This function should not be modified, when the callback is needed,
  1454. the HAL_FMPSMBUS_MasterRxCpltCallback() could be implemented in the user file
  1455. */
  1456. }
  1457. /** @brief Slave Tx Transfer completed callback.
  1458. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  1459. * the configuration information for the specified FMPSMBUS.
  1460. * @retval None
  1461. */
  1462. __weak void HAL_FMPSMBUS_SlaveTxCpltCallback(FMPSMBUS_HandleTypeDef *hfmpsmbus)
  1463. {
  1464. /* Prevent unused argument(s) compilation warning */
  1465. UNUSED(hfmpsmbus);
  1466. /* NOTE : This function should not be modified, when the callback is needed,
  1467. the HAL_FMPSMBUS_SlaveTxCpltCallback() could be implemented in the user file
  1468. */
  1469. }
  1470. /**
  1471. * @brief Slave Rx Transfer completed callback.
  1472. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  1473. * the configuration information for the specified FMPSMBUS.
  1474. * @retval None
  1475. */
  1476. __weak void HAL_FMPSMBUS_SlaveRxCpltCallback(FMPSMBUS_HandleTypeDef *hfmpsmbus)
  1477. {
  1478. /* Prevent unused argument(s) compilation warning */
  1479. UNUSED(hfmpsmbus);
  1480. /* NOTE : This function should not be modified, when the callback is needed,
  1481. the HAL_FMPSMBUS_SlaveRxCpltCallback() could be implemented in the user file
  1482. */
  1483. }
  1484. /**
  1485. * @brief Slave Address Match callback.
  1486. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  1487. * the configuration information for the specified FMPSMBUS.
  1488. * @param TransferDirection Master request Transfer Direction (Write/Read)
  1489. * @param AddrMatchCode Address Match Code
  1490. * @retval None
  1491. */
  1492. __weak void HAL_FMPSMBUS_AddrCallback(FMPSMBUS_HandleTypeDef *hfmpsmbus, uint8_t TransferDirection,
  1493. uint16_t AddrMatchCode)
  1494. {
  1495. /* Prevent unused argument(s) compilation warning */
  1496. UNUSED(hfmpsmbus);
  1497. UNUSED(TransferDirection);
  1498. UNUSED(AddrMatchCode);
  1499. /* NOTE : This function should not be modified, when the callback is needed,
  1500. the HAL_FMPSMBUS_AddrCallback() could be implemented in the user file
  1501. */
  1502. }
  1503. /**
  1504. * @brief Listen Complete callback.
  1505. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  1506. * the configuration information for the specified FMPSMBUS.
  1507. * @retval None
  1508. */
  1509. __weak void HAL_FMPSMBUS_ListenCpltCallback(FMPSMBUS_HandleTypeDef *hfmpsmbus)
  1510. {
  1511. /* Prevent unused argument(s) compilation warning */
  1512. UNUSED(hfmpsmbus);
  1513. /* NOTE : This function should not be modified, when the callback is needed,
  1514. the HAL_FMPSMBUS_ListenCpltCallback() could be implemented in the user file
  1515. */
  1516. }
  1517. /**
  1518. * @brief FMPSMBUS error callback.
  1519. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  1520. * the configuration information for the specified FMPSMBUS.
  1521. * @retval None
  1522. */
  1523. __weak void HAL_FMPSMBUS_ErrorCallback(FMPSMBUS_HandleTypeDef *hfmpsmbus)
  1524. {
  1525. /* Prevent unused argument(s) compilation warning */
  1526. UNUSED(hfmpsmbus);
  1527. /* NOTE : This function should not be modified, when the callback is needed,
  1528. the HAL_FMPSMBUS_ErrorCallback() could be implemented in the user file
  1529. */
  1530. }
  1531. /**
  1532. * @}
  1533. */
  1534. /** @defgroup FMPSMBUS_Exported_Functions_Group3 Peripheral State and Errors functions
  1535. * @brief Peripheral State and Errors functions
  1536. *
  1537. @verbatim
  1538. ===============================================================================
  1539. ##### Peripheral State and Errors functions #####
  1540. ===============================================================================
  1541. [..]
  1542. This subsection permits to get in run-time the status of the peripheral
  1543. and the data flow.
  1544. @endverbatim
  1545. * @{
  1546. */
  1547. /**
  1548. * @brief Return the FMPSMBUS handle state.
  1549. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  1550. * the configuration information for the specified FMPSMBUS.
  1551. * @retval HAL state
  1552. */
  1553. uint32_t HAL_FMPSMBUS_GetState(FMPSMBUS_HandleTypeDef *hfmpsmbus)
  1554. {
  1555. /* Return FMPSMBUS handle state */
  1556. return hfmpsmbus->State;
  1557. }
  1558. /**
  1559. * @brief Return the FMPSMBUS error code.
  1560. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  1561. * the configuration information for the specified FMPSMBUS.
  1562. * @retval FMPSMBUS Error Code
  1563. */
  1564. uint32_t HAL_FMPSMBUS_GetError(FMPSMBUS_HandleTypeDef *hfmpsmbus)
  1565. {
  1566. return hfmpsmbus->ErrorCode;
  1567. }
  1568. /**
  1569. * @}
  1570. */
  1571. /**
  1572. * @}
  1573. */
  1574. /** @addtogroup FMPSMBUS_Private_Functions FMPSMBUS Private Functions
  1575. * @brief Data transfers Private functions
  1576. * @{
  1577. */
  1578. /**
  1579. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Master Mode.
  1580. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  1581. * the configuration information for the specified FMPSMBUS.
  1582. * @param StatusFlags Value of Interrupt Flags.
  1583. * @retval HAL status
  1584. */
  1585. static HAL_StatusTypeDef FMPSMBUS_Master_ISR(FMPSMBUS_HandleTypeDef *hfmpsmbus, uint32_t StatusFlags)
  1586. {
  1587. uint16_t DevAddress;
  1588. /* Process Locked */
  1589. __HAL_LOCK(hfmpsmbus);
  1590. if (FMPSMBUS_CHECK_FLAG(StatusFlags, FMPSMBUS_FLAG_AF) != RESET)
  1591. {
  1592. /* Clear NACK Flag */
  1593. __HAL_FMPSMBUS_CLEAR_FLAG(hfmpsmbus, FMPSMBUS_FLAG_AF);
  1594. /* Set corresponding Error Code */
  1595. /* No need to generate STOP, it is automatically done */
  1596. hfmpsmbus->ErrorCode |= HAL_FMPSMBUS_ERROR_ACKF;
  1597. /* Process Unlocked */
  1598. __HAL_UNLOCK(hfmpsmbus);
  1599. /* Call the Error callback to inform upper layer */
  1600. #if (USE_HAL_FMPSMBUS_REGISTER_CALLBACKS == 1)
  1601. hfmpsmbus->ErrorCallback(hfmpsmbus);
  1602. #else
  1603. HAL_FMPSMBUS_ErrorCallback(hfmpsmbus);
  1604. #endif /* USE_HAL_FMPSMBUS_REGISTER_CALLBACKS */
  1605. }
  1606. else if (FMPSMBUS_CHECK_FLAG(StatusFlags, FMPSMBUS_FLAG_STOPF) != RESET)
  1607. {
  1608. /* Check and treat errors if errors occurs during STOP process */
  1609. FMPSMBUS_ITErrorHandler(hfmpsmbus);
  1610. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1611. if (hfmpsmbus->State == HAL_FMPSMBUS_STATE_MASTER_BUSY_TX)
  1612. {
  1613. /* Disable Interrupt */
  1614. FMPSMBUS_Disable_IRQ(hfmpsmbus, FMPSMBUS_IT_TX);
  1615. /* Clear STOP Flag */
  1616. __HAL_FMPSMBUS_CLEAR_FLAG(hfmpsmbus, FMPSMBUS_FLAG_STOPF);
  1617. /* Clear Configuration Register 2 */
  1618. FMPSMBUS_RESET_CR2(hfmpsmbus);
  1619. /* Flush remaining data in Fifo register in case of error occurs before TXEmpty */
  1620. /* Disable the selected FMPSMBUS peripheral */
  1621. __HAL_FMPSMBUS_DISABLE(hfmpsmbus);
  1622. hfmpsmbus->PreviousState = HAL_FMPSMBUS_STATE_READY;
  1623. hfmpsmbus->State = HAL_FMPSMBUS_STATE_READY;
  1624. /* Process Unlocked */
  1625. __HAL_UNLOCK(hfmpsmbus);
  1626. /* Re-enable the selected FMPSMBUS peripheral */
  1627. __HAL_FMPSMBUS_ENABLE(hfmpsmbus);
  1628. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1629. #if (USE_HAL_FMPSMBUS_REGISTER_CALLBACKS == 1)
  1630. hfmpsmbus->MasterTxCpltCallback(hfmpsmbus);
  1631. #else
  1632. HAL_FMPSMBUS_MasterTxCpltCallback(hfmpsmbus);
  1633. #endif /* USE_HAL_FMPSMBUS_REGISTER_CALLBACKS */
  1634. }
  1635. else if (hfmpsmbus->State == HAL_FMPSMBUS_STATE_MASTER_BUSY_RX)
  1636. {
  1637. /* Store Last receive data if any */
  1638. if (FMPSMBUS_CHECK_FLAG(StatusFlags, FMPSMBUS_FLAG_RXNE) != RESET)
  1639. {
  1640. /* Read data from RXDR */
  1641. *hfmpsmbus->pBuffPtr = (uint8_t)(hfmpsmbus->Instance->RXDR);
  1642. /* Increment Buffer pointer */
  1643. hfmpsmbus->pBuffPtr++;
  1644. if ((hfmpsmbus->XferSize > 0U))
  1645. {
  1646. hfmpsmbus->XferSize--;
  1647. hfmpsmbus->XferCount--;
  1648. }
  1649. }
  1650. /* Disable Interrupt */
  1651. FMPSMBUS_Disable_IRQ(hfmpsmbus, FMPSMBUS_IT_RX);
  1652. /* Clear STOP Flag */
  1653. __HAL_FMPSMBUS_CLEAR_FLAG(hfmpsmbus, FMPSMBUS_FLAG_STOPF);
  1654. /* Clear Configuration Register 2 */
  1655. FMPSMBUS_RESET_CR2(hfmpsmbus);
  1656. hfmpsmbus->PreviousState = HAL_FMPSMBUS_STATE_READY;
  1657. hfmpsmbus->State = HAL_FMPSMBUS_STATE_READY;
  1658. /* Process Unlocked */
  1659. __HAL_UNLOCK(hfmpsmbus);
  1660. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1661. #if (USE_HAL_FMPSMBUS_REGISTER_CALLBACKS == 1)
  1662. hfmpsmbus->MasterRxCpltCallback(hfmpsmbus);
  1663. #else
  1664. HAL_FMPSMBUS_MasterRxCpltCallback(hfmpsmbus);
  1665. #endif /* USE_HAL_FMPSMBUS_REGISTER_CALLBACKS */
  1666. }
  1667. else
  1668. {
  1669. /* Nothing to do */
  1670. }
  1671. }
  1672. else if (FMPSMBUS_CHECK_FLAG(StatusFlags, FMPSMBUS_FLAG_RXNE) != RESET)
  1673. {
  1674. /* Read data from RXDR */
  1675. *hfmpsmbus->pBuffPtr = (uint8_t)(hfmpsmbus->Instance->RXDR);
  1676. /* Increment Buffer pointer */
  1677. hfmpsmbus->pBuffPtr++;
  1678. /* Increment Size counter */
  1679. hfmpsmbus->XferSize--;
  1680. hfmpsmbus->XferCount--;
  1681. }
  1682. else if (FMPSMBUS_CHECK_FLAG(StatusFlags, FMPSMBUS_FLAG_TXIS) != RESET)
  1683. {
  1684. /* Write data to TXDR */
  1685. hfmpsmbus->Instance->TXDR = *hfmpsmbus->pBuffPtr;
  1686. /* Increment Buffer pointer */
  1687. hfmpsmbus->pBuffPtr++;
  1688. /* Increment Size counter */
  1689. hfmpsmbus->XferSize--;
  1690. hfmpsmbus->XferCount--;
  1691. }
  1692. else if (FMPSMBUS_CHECK_FLAG(StatusFlags, FMPSMBUS_FLAG_TCR) != RESET)
  1693. {
  1694. if ((hfmpsmbus->XferCount != 0U) && (hfmpsmbus->XferSize == 0U))
  1695. {
  1696. DevAddress = (uint16_t)(hfmpsmbus->Instance->CR2 & FMPI2C_CR2_SADD);
  1697. if (hfmpsmbus->XferCount > MAX_NBYTE_SIZE)
  1698. {
  1699. FMPSMBUS_TransferConfig(hfmpsmbus, DevAddress, MAX_NBYTE_SIZE,
  1700. (FMPSMBUS_RELOAD_MODE | (hfmpsmbus->XferOptions & FMPSMBUS_SENDPEC_MODE)),
  1701. FMPSMBUS_NO_STARTSTOP);
  1702. hfmpsmbus->XferSize = MAX_NBYTE_SIZE;
  1703. }
  1704. else
  1705. {
  1706. hfmpsmbus->XferSize = hfmpsmbus->XferCount;
  1707. FMPSMBUS_TransferConfig(hfmpsmbus, DevAddress, (uint8_t)hfmpsmbus->XferSize, hfmpsmbus->XferOptions,
  1708. FMPSMBUS_NO_STARTSTOP);
  1709. /* If PEC mode is enable, size to transmit should be Size-1 byte, corresponding to PEC byte */
  1710. /* PEC byte is automatically sent by HW block, no need to manage it in Transmit process */
  1711. if (FMPSMBUS_GET_PEC_MODE(hfmpsmbus) != 0UL)
  1712. {
  1713. hfmpsmbus->XferSize--;
  1714. hfmpsmbus->XferCount--;
  1715. }
  1716. }
  1717. }
  1718. else if ((hfmpsmbus->XferCount == 0U) && (hfmpsmbus->XferSize == 0U))
  1719. {
  1720. /* Call TxCpltCallback() if no stop mode is set */
  1721. if (FMPSMBUS_GET_STOP_MODE(hfmpsmbus) != FMPSMBUS_AUTOEND_MODE)
  1722. {
  1723. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1724. if (hfmpsmbus->State == HAL_FMPSMBUS_STATE_MASTER_BUSY_TX)
  1725. {
  1726. /* Disable Interrupt */
  1727. FMPSMBUS_Disable_IRQ(hfmpsmbus, FMPSMBUS_IT_TX);
  1728. hfmpsmbus->PreviousState = hfmpsmbus->State;
  1729. hfmpsmbus->State = HAL_FMPSMBUS_STATE_READY;
  1730. /* Process Unlocked */
  1731. __HAL_UNLOCK(hfmpsmbus);
  1732. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1733. #if (USE_HAL_FMPSMBUS_REGISTER_CALLBACKS == 1)
  1734. hfmpsmbus->MasterTxCpltCallback(hfmpsmbus);
  1735. #else
  1736. HAL_FMPSMBUS_MasterTxCpltCallback(hfmpsmbus);
  1737. #endif /* USE_HAL_FMPSMBUS_REGISTER_CALLBACKS */
  1738. }
  1739. else if (hfmpsmbus->State == HAL_FMPSMBUS_STATE_MASTER_BUSY_RX)
  1740. {
  1741. FMPSMBUS_Disable_IRQ(hfmpsmbus, FMPSMBUS_IT_RX);
  1742. hfmpsmbus->PreviousState = hfmpsmbus->State;
  1743. hfmpsmbus->State = HAL_FMPSMBUS_STATE_READY;
  1744. /* Process Unlocked */
  1745. __HAL_UNLOCK(hfmpsmbus);
  1746. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1747. #if (USE_HAL_FMPSMBUS_REGISTER_CALLBACKS == 1)
  1748. hfmpsmbus->MasterRxCpltCallback(hfmpsmbus);
  1749. #else
  1750. HAL_FMPSMBUS_MasterRxCpltCallback(hfmpsmbus);
  1751. #endif /* USE_HAL_FMPSMBUS_REGISTER_CALLBACKS */
  1752. }
  1753. else
  1754. {
  1755. /* Nothing to do */
  1756. }
  1757. }
  1758. }
  1759. else
  1760. {
  1761. /* Nothing to do */
  1762. }
  1763. }
  1764. else if (FMPSMBUS_CHECK_FLAG(StatusFlags, FMPSMBUS_FLAG_TC) != RESET)
  1765. {
  1766. if (hfmpsmbus->XferCount == 0U)
  1767. {
  1768. /* Specific use case for Quick command */
  1769. if (hfmpsmbus->pBuffPtr == NULL)
  1770. {
  1771. /* Generate a Stop command */
  1772. hfmpsmbus->Instance->CR2 |= FMPI2C_CR2_STOP;
  1773. }
  1774. /* Call TxCpltCallback() if no stop mode is set */
  1775. else if (FMPSMBUS_GET_STOP_MODE(hfmpsmbus) != FMPSMBUS_AUTOEND_MODE)
  1776. {
  1777. /* No Generate Stop, to permit restart mode */
  1778. /* The stop will be done at the end of transfer, when FMPSMBUS_AUTOEND_MODE enable */
  1779. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1780. if (hfmpsmbus->State == HAL_FMPSMBUS_STATE_MASTER_BUSY_TX)
  1781. {
  1782. /* Disable Interrupt */
  1783. FMPSMBUS_Disable_IRQ(hfmpsmbus, FMPSMBUS_IT_TX);
  1784. hfmpsmbus->PreviousState = hfmpsmbus->State;
  1785. hfmpsmbus->State = HAL_FMPSMBUS_STATE_READY;
  1786. /* Process Unlocked */
  1787. __HAL_UNLOCK(hfmpsmbus);
  1788. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1789. #if (USE_HAL_FMPSMBUS_REGISTER_CALLBACKS == 1)
  1790. hfmpsmbus->MasterTxCpltCallback(hfmpsmbus);
  1791. #else
  1792. HAL_FMPSMBUS_MasterTxCpltCallback(hfmpsmbus);
  1793. #endif /* USE_HAL_FMPSMBUS_REGISTER_CALLBACKS */
  1794. }
  1795. else if (hfmpsmbus->State == HAL_FMPSMBUS_STATE_MASTER_BUSY_RX)
  1796. {
  1797. FMPSMBUS_Disable_IRQ(hfmpsmbus, FMPSMBUS_IT_RX);
  1798. hfmpsmbus->PreviousState = hfmpsmbus->State;
  1799. hfmpsmbus->State = HAL_FMPSMBUS_STATE_READY;
  1800. /* Process Unlocked */
  1801. __HAL_UNLOCK(hfmpsmbus);
  1802. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1803. #if (USE_HAL_FMPSMBUS_REGISTER_CALLBACKS == 1)
  1804. hfmpsmbus->MasterRxCpltCallback(hfmpsmbus);
  1805. #else
  1806. HAL_FMPSMBUS_MasterRxCpltCallback(hfmpsmbus);
  1807. #endif /* USE_HAL_FMPSMBUS_REGISTER_CALLBACKS */
  1808. }
  1809. else
  1810. {
  1811. /* Nothing to do */
  1812. }
  1813. }
  1814. else
  1815. {
  1816. /* Nothing to do */
  1817. }
  1818. }
  1819. }
  1820. else
  1821. {
  1822. /* Nothing to do */
  1823. }
  1824. /* Process Unlocked */
  1825. __HAL_UNLOCK(hfmpsmbus);
  1826. return HAL_OK;
  1827. }
  1828. /**
  1829. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Slave Mode.
  1830. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  1831. * the configuration information for the specified FMPSMBUS.
  1832. * @param StatusFlags Value of Interrupt Flags.
  1833. * @retval HAL status
  1834. */
  1835. static HAL_StatusTypeDef FMPSMBUS_Slave_ISR(FMPSMBUS_HandleTypeDef *hfmpsmbus, uint32_t StatusFlags)
  1836. {
  1837. uint8_t TransferDirection;
  1838. uint16_t SlaveAddrCode;
  1839. /* Process Locked */
  1840. __HAL_LOCK(hfmpsmbus);
  1841. if (FMPSMBUS_CHECK_FLAG(StatusFlags, FMPSMBUS_FLAG_AF) != RESET)
  1842. {
  1843. /* Check that FMPSMBUS transfer finished */
  1844. /* if yes, normal usecase, a NACK is sent by the HOST when Transfer is finished */
  1845. /* Mean XferCount == 0*/
  1846. /* So clear Flag NACKF only */
  1847. if (hfmpsmbus->XferCount == 0U)
  1848. {
  1849. /* Clear NACK Flag */
  1850. __HAL_FMPSMBUS_CLEAR_FLAG(hfmpsmbus, FMPSMBUS_FLAG_AF);
  1851. /* Process Unlocked */
  1852. __HAL_UNLOCK(hfmpsmbus);
  1853. }
  1854. else
  1855. {
  1856. /* if no, error usecase, a Non-Acknowledge of last Data is generated by the HOST*/
  1857. /* Clear NACK Flag */
  1858. __HAL_FMPSMBUS_CLEAR_FLAG(hfmpsmbus, FMPSMBUS_FLAG_AF);
  1859. /* Set HAL State to "Idle" State, mean to LISTEN state */
  1860. /* So reset Slave Busy state */
  1861. hfmpsmbus->PreviousState = hfmpsmbus->State;
  1862. hfmpsmbus->State &= ~((uint32_t)HAL_FMPSMBUS_STATE_SLAVE_BUSY_TX);
  1863. hfmpsmbus->State &= ~((uint32_t)HAL_FMPSMBUS_STATE_SLAVE_BUSY_RX);
  1864. /* Disable RX/TX Interrupts, keep only ADDR Interrupt */
  1865. FMPSMBUS_Disable_IRQ(hfmpsmbus, FMPSMBUS_IT_RX | FMPSMBUS_IT_TX);
  1866. /* Set ErrorCode corresponding to a Non-Acknowledge */
  1867. hfmpsmbus->ErrorCode |= HAL_FMPSMBUS_ERROR_ACKF;
  1868. /* Process Unlocked */
  1869. __HAL_UNLOCK(hfmpsmbus);
  1870. /* Call the Error callback to inform upper layer */
  1871. #if (USE_HAL_FMPSMBUS_REGISTER_CALLBACKS == 1)
  1872. hfmpsmbus->ErrorCallback(hfmpsmbus);
  1873. #else
  1874. HAL_FMPSMBUS_ErrorCallback(hfmpsmbus);
  1875. #endif /* USE_HAL_FMPSMBUS_REGISTER_CALLBACKS */
  1876. }
  1877. }
  1878. else if (FMPSMBUS_CHECK_FLAG(StatusFlags, FMPSMBUS_FLAG_ADDR) != RESET)
  1879. {
  1880. TransferDirection = (uint8_t)(FMPSMBUS_GET_DIR(hfmpsmbus));
  1881. SlaveAddrCode = (uint16_t)(FMPSMBUS_GET_ADDR_MATCH(hfmpsmbus));
  1882. /* Disable ADDR interrupt to prevent multiple ADDRInterrupt*/
  1883. /* Other ADDRInterrupt will be treat in next Listen usecase */
  1884. __HAL_FMPSMBUS_DISABLE_IT(hfmpsmbus, FMPSMBUS_IT_ADDRI);
  1885. /* Process Unlocked */
  1886. __HAL_UNLOCK(hfmpsmbus);
  1887. /* Call Slave Addr callback */
  1888. #if (USE_HAL_FMPSMBUS_REGISTER_CALLBACKS == 1)
  1889. hfmpsmbus->AddrCallback(hfmpsmbus, TransferDirection, SlaveAddrCode);
  1890. #else
  1891. HAL_FMPSMBUS_AddrCallback(hfmpsmbus, TransferDirection, SlaveAddrCode);
  1892. #endif /* USE_HAL_FMPSMBUS_REGISTER_CALLBACKS */
  1893. }
  1894. else if ((FMPSMBUS_CHECK_FLAG(StatusFlags, FMPSMBUS_FLAG_RXNE) != RESET) ||
  1895. (FMPSMBUS_CHECK_FLAG(StatusFlags, FMPSMBUS_FLAG_TCR) != RESET))
  1896. {
  1897. if ((hfmpsmbus->State & HAL_FMPSMBUS_STATE_SLAVE_BUSY_RX) == HAL_FMPSMBUS_STATE_SLAVE_BUSY_RX)
  1898. {
  1899. /* Read data from RXDR */
  1900. *hfmpsmbus->pBuffPtr = (uint8_t)(hfmpsmbus->Instance->RXDR);
  1901. /* Increment Buffer pointer */
  1902. hfmpsmbus->pBuffPtr++;
  1903. hfmpsmbus->XferSize--;
  1904. hfmpsmbus->XferCount--;
  1905. if (hfmpsmbus->XferCount == 1U)
  1906. {
  1907. /* Receive last Byte, can be PEC byte in case of PEC BYTE enabled */
  1908. /* or only the last Byte of Transfer */
  1909. /* So reset the RELOAD bit mode */
  1910. hfmpsmbus->XferOptions &= ~FMPSMBUS_RELOAD_MODE;
  1911. FMPSMBUS_TransferConfig(hfmpsmbus, 0, 1, hfmpsmbus->XferOptions, FMPSMBUS_NO_STARTSTOP);
  1912. }
  1913. else if (hfmpsmbus->XferCount == 0U)
  1914. {
  1915. /* Last Byte is received, disable Interrupt */
  1916. FMPSMBUS_Disable_IRQ(hfmpsmbus, FMPSMBUS_IT_RX);
  1917. /* Remove HAL_FMPSMBUS_STATE_SLAVE_BUSY_RX, keep only HAL_FMPSMBUS_STATE_LISTEN */
  1918. hfmpsmbus->PreviousState = hfmpsmbus->State;
  1919. hfmpsmbus->State &= ~((uint32_t)HAL_FMPSMBUS_STATE_SLAVE_BUSY_RX);
  1920. /* Process Unlocked */
  1921. __HAL_UNLOCK(hfmpsmbus);
  1922. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1923. #if (USE_HAL_FMPSMBUS_REGISTER_CALLBACKS == 1)
  1924. hfmpsmbus->SlaveRxCpltCallback(hfmpsmbus);
  1925. #else
  1926. HAL_FMPSMBUS_SlaveRxCpltCallback(hfmpsmbus);
  1927. #endif /* USE_HAL_FMPSMBUS_REGISTER_CALLBACKS */
  1928. }
  1929. else
  1930. {
  1931. /* Set Reload for next Bytes */
  1932. FMPSMBUS_TransferConfig(hfmpsmbus, 0, 1,
  1933. FMPSMBUS_RELOAD_MODE | (hfmpsmbus->XferOptions & FMPSMBUS_SENDPEC_MODE),
  1934. FMPSMBUS_NO_STARTSTOP);
  1935. /* Ack last Byte Read */
  1936. hfmpsmbus->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  1937. }
  1938. }
  1939. else if ((hfmpsmbus->State & HAL_FMPSMBUS_STATE_SLAVE_BUSY_TX) == HAL_FMPSMBUS_STATE_SLAVE_BUSY_TX)
  1940. {
  1941. if ((hfmpsmbus->XferCount != 0U) && (hfmpsmbus->XferSize == 0U))
  1942. {
  1943. if (hfmpsmbus->XferCount > MAX_NBYTE_SIZE)
  1944. {
  1945. FMPSMBUS_TransferConfig(hfmpsmbus, 0, MAX_NBYTE_SIZE,
  1946. (FMPSMBUS_RELOAD_MODE | (hfmpsmbus->XferOptions & FMPSMBUS_SENDPEC_MODE)),
  1947. FMPSMBUS_NO_STARTSTOP);
  1948. hfmpsmbus->XferSize = MAX_NBYTE_SIZE;
  1949. }
  1950. else
  1951. {
  1952. hfmpsmbus->XferSize = hfmpsmbus->XferCount;
  1953. FMPSMBUS_TransferConfig(hfmpsmbus, 0, (uint8_t)hfmpsmbus->XferSize, hfmpsmbus->XferOptions,
  1954. FMPSMBUS_NO_STARTSTOP);
  1955. /* If PEC mode is enable, size to transmit should be Size-1 byte, corresponding to PEC byte */
  1956. /* PEC byte is automatically sent by HW block, no need to manage it in Transmit process */
  1957. if (FMPSMBUS_GET_PEC_MODE(hfmpsmbus) != 0UL)
  1958. {
  1959. hfmpsmbus->XferSize--;
  1960. hfmpsmbus->XferCount--;
  1961. }
  1962. }
  1963. }
  1964. }
  1965. else
  1966. {
  1967. /* Nothing to do */
  1968. }
  1969. }
  1970. else if (FMPSMBUS_CHECK_FLAG(StatusFlags, FMPSMBUS_FLAG_TXIS) != RESET)
  1971. {
  1972. /* Write data to TXDR only if XferCount not reach "0" */
  1973. /* A TXIS flag can be set, during STOP treatment */
  1974. /* Check if all Data have already been sent */
  1975. /* If it is the case, this last write in TXDR is not sent, correspond to a dummy TXIS event */
  1976. if (hfmpsmbus->XferCount > 0U)
  1977. {
  1978. /* Write data to TXDR */
  1979. hfmpsmbus->Instance->TXDR = *hfmpsmbus->pBuffPtr;
  1980. /* Increment Buffer pointer */
  1981. hfmpsmbus->pBuffPtr++;
  1982. hfmpsmbus->XferCount--;
  1983. hfmpsmbus->XferSize--;
  1984. }
  1985. if (hfmpsmbus->XferCount == 0U)
  1986. {
  1987. /* Last Byte is Transmitted */
  1988. /* Remove HAL_FMPSMBUS_STATE_SLAVE_BUSY_TX, keep only HAL_FMPSMBUS_STATE_LISTEN */
  1989. FMPSMBUS_Disable_IRQ(hfmpsmbus, FMPSMBUS_IT_TX);
  1990. hfmpsmbus->PreviousState = hfmpsmbus->State;
  1991. hfmpsmbus->State &= ~((uint32_t)HAL_FMPSMBUS_STATE_SLAVE_BUSY_TX);
  1992. /* Process Unlocked */
  1993. __HAL_UNLOCK(hfmpsmbus);
  1994. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1995. #if (USE_HAL_FMPSMBUS_REGISTER_CALLBACKS == 1)
  1996. hfmpsmbus->SlaveTxCpltCallback(hfmpsmbus);
  1997. #else
  1998. HAL_FMPSMBUS_SlaveTxCpltCallback(hfmpsmbus);
  1999. #endif /* USE_HAL_FMPSMBUS_REGISTER_CALLBACKS */
  2000. }
  2001. }
  2002. else
  2003. {
  2004. /* Nothing to do */
  2005. }
  2006. /* Check if STOPF is set */
  2007. if (FMPSMBUS_CHECK_FLAG(StatusFlags, FMPSMBUS_FLAG_STOPF) != RESET)
  2008. {
  2009. if ((hfmpsmbus->State & HAL_FMPSMBUS_STATE_LISTEN) == HAL_FMPSMBUS_STATE_LISTEN)
  2010. {
  2011. /* Store Last receive data if any */
  2012. if (__HAL_FMPSMBUS_GET_FLAG(hfmpsmbus, FMPSMBUS_FLAG_RXNE) != RESET)
  2013. {
  2014. /* Read data from RXDR */
  2015. *hfmpsmbus->pBuffPtr = (uint8_t)(hfmpsmbus->Instance->RXDR);
  2016. /* Increment Buffer pointer */
  2017. hfmpsmbus->pBuffPtr++;
  2018. if ((hfmpsmbus->XferSize > 0U))
  2019. {
  2020. hfmpsmbus->XferSize--;
  2021. hfmpsmbus->XferCount--;
  2022. }
  2023. }
  2024. /* Disable RX and TX Interrupts */
  2025. FMPSMBUS_Disable_IRQ(hfmpsmbus, FMPSMBUS_IT_RX | FMPSMBUS_IT_TX);
  2026. /* Disable ADDR Interrupt */
  2027. FMPSMBUS_Disable_IRQ(hfmpsmbus, FMPSMBUS_IT_ADDR);
  2028. /* Disable Address Acknowledge */
  2029. hfmpsmbus->Instance->CR2 |= FMPI2C_CR2_NACK;
  2030. /* Clear Configuration Register 2 */
  2031. FMPSMBUS_RESET_CR2(hfmpsmbus);
  2032. /* Clear STOP Flag */
  2033. __HAL_FMPSMBUS_CLEAR_FLAG(hfmpsmbus, FMPSMBUS_FLAG_STOPF);
  2034. /* Clear ADDR flag */
  2035. __HAL_FMPSMBUS_CLEAR_FLAG(hfmpsmbus, FMPSMBUS_FLAG_ADDR);
  2036. hfmpsmbus->XferOptions = 0;
  2037. hfmpsmbus->PreviousState = hfmpsmbus->State;
  2038. hfmpsmbus->State = HAL_FMPSMBUS_STATE_READY;
  2039. /* Process Unlocked */
  2040. __HAL_UNLOCK(hfmpsmbus);
  2041. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  2042. #if (USE_HAL_FMPSMBUS_REGISTER_CALLBACKS == 1)
  2043. hfmpsmbus->ListenCpltCallback(hfmpsmbus);
  2044. #else
  2045. HAL_FMPSMBUS_ListenCpltCallback(hfmpsmbus);
  2046. #endif /* USE_HAL_FMPSMBUS_REGISTER_CALLBACKS */
  2047. }
  2048. }
  2049. /* Process Unlocked */
  2050. __HAL_UNLOCK(hfmpsmbus);
  2051. return HAL_OK;
  2052. }
  2053. /**
  2054. * @brief Manage the enabling of Interrupts.
  2055. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  2056. * the configuration information for the specified FMPSMBUS.
  2057. * @param InterruptRequest Value of @ref FMPSMBUS_Interrupt_configuration_definition.
  2058. * @retval HAL status
  2059. */
  2060. static void FMPSMBUS_Enable_IRQ(FMPSMBUS_HandleTypeDef *hfmpsmbus, uint32_t InterruptRequest)
  2061. {
  2062. uint32_t tmpisr = 0UL;
  2063. if ((InterruptRequest & FMPSMBUS_IT_ALERT) == FMPSMBUS_IT_ALERT)
  2064. {
  2065. /* Enable ERR interrupt */
  2066. tmpisr |= FMPSMBUS_IT_ERRI;
  2067. }
  2068. if ((InterruptRequest & FMPSMBUS_IT_ADDR) == FMPSMBUS_IT_ADDR)
  2069. {
  2070. /* Enable ADDR, STOP interrupt */
  2071. tmpisr |= FMPSMBUS_IT_ADDRI | FMPSMBUS_IT_STOPI | FMPSMBUS_IT_NACKI | FMPSMBUS_IT_ERRI;
  2072. }
  2073. if ((InterruptRequest & FMPSMBUS_IT_TX) == FMPSMBUS_IT_TX)
  2074. {
  2075. /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  2076. tmpisr |= FMPSMBUS_IT_ERRI | FMPSMBUS_IT_TCI | FMPSMBUS_IT_STOPI | FMPSMBUS_IT_NACKI | FMPSMBUS_IT_TXI;
  2077. }
  2078. if ((InterruptRequest & FMPSMBUS_IT_RX) == FMPSMBUS_IT_RX)
  2079. {
  2080. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  2081. tmpisr |= FMPSMBUS_IT_ERRI | FMPSMBUS_IT_TCI | FMPSMBUS_IT_STOPI | FMPSMBUS_IT_NACKI | FMPSMBUS_IT_RXI;
  2082. }
  2083. /* Enable interrupts only at the end */
  2084. /* to avoid the risk of FMPSMBUS interrupt handle execution before */
  2085. /* all interrupts requested done */
  2086. __HAL_FMPSMBUS_ENABLE_IT(hfmpsmbus, tmpisr);
  2087. }
  2088. /**
  2089. * @brief Manage the disabling of Interrupts.
  2090. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  2091. * the configuration information for the specified FMPSMBUS.
  2092. * @param InterruptRequest Value of @ref FMPSMBUS_Interrupt_configuration_definition.
  2093. * @retval HAL status
  2094. */
  2095. static void FMPSMBUS_Disable_IRQ(FMPSMBUS_HandleTypeDef *hfmpsmbus, uint32_t InterruptRequest)
  2096. {
  2097. uint32_t tmpisr = 0UL;
  2098. uint32_t tmpstate = hfmpsmbus->State;
  2099. if ((tmpstate == HAL_FMPSMBUS_STATE_READY) && ((InterruptRequest & FMPSMBUS_IT_ALERT) == FMPSMBUS_IT_ALERT))
  2100. {
  2101. /* Disable ERR interrupt */
  2102. tmpisr |= FMPSMBUS_IT_ERRI;
  2103. }
  2104. if ((InterruptRequest & FMPSMBUS_IT_TX) == FMPSMBUS_IT_TX)
  2105. {
  2106. /* Disable TC, STOP, NACK and TXI interrupt */
  2107. tmpisr |= FMPSMBUS_IT_TCI | FMPSMBUS_IT_TXI;
  2108. if ((FMPSMBUS_GET_ALERT_ENABLED(hfmpsmbus) == 0UL)
  2109. && ((tmpstate & HAL_FMPSMBUS_STATE_LISTEN) != HAL_FMPSMBUS_STATE_LISTEN))
  2110. {
  2111. /* Disable ERR interrupt */
  2112. tmpisr |= FMPSMBUS_IT_ERRI;
  2113. }
  2114. if ((tmpstate & HAL_FMPSMBUS_STATE_LISTEN) != HAL_FMPSMBUS_STATE_LISTEN)
  2115. {
  2116. /* Disable STOP and NACK interrupt */
  2117. tmpisr |= FMPSMBUS_IT_STOPI | FMPSMBUS_IT_NACKI;
  2118. }
  2119. }
  2120. if ((InterruptRequest & FMPSMBUS_IT_RX) == FMPSMBUS_IT_RX)
  2121. {
  2122. /* Disable TC, STOP, NACK and RXI interrupt */
  2123. tmpisr |= FMPSMBUS_IT_TCI | FMPSMBUS_IT_RXI;
  2124. if ((FMPSMBUS_GET_ALERT_ENABLED(hfmpsmbus) == 0UL)
  2125. && ((tmpstate & HAL_FMPSMBUS_STATE_LISTEN) != HAL_FMPSMBUS_STATE_LISTEN))
  2126. {
  2127. /* Disable ERR interrupt */
  2128. tmpisr |= FMPSMBUS_IT_ERRI;
  2129. }
  2130. if ((tmpstate & HAL_FMPSMBUS_STATE_LISTEN) != HAL_FMPSMBUS_STATE_LISTEN)
  2131. {
  2132. /* Disable STOP and NACK interrupt */
  2133. tmpisr |= FMPSMBUS_IT_STOPI | FMPSMBUS_IT_NACKI;
  2134. }
  2135. }
  2136. if ((InterruptRequest & FMPSMBUS_IT_ADDR) == FMPSMBUS_IT_ADDR)
  2137. {
  2138. /* Disable ADDR, STOP and NACK interrupt */
  2139. tmpisr |= FMPSMBUS_IT_ADDRI | FMPSMBUS_IT_STOPI | FMPSMBUS_IT_NACKI;
  2140. if (FMPSMBUS_GET_ALERT_ENABLED(hfmpsmbus) == 0UL)
  2141. {
  2142. /* Disable ERR interrupt */
  2143. tmpisr |= FMPSMBUS_IT_ERRI;
  2144. }
  2145. }
  2146. /* Disable interrupts only at the end */
  2147. /* to avoid a breaking situation like at "t" time */
  2148. /* all disable interrupts request are not done */
  2149. __HAL_FMPSMBUS_DISABLE_IT(hfmpsmbus, tmpisr);
  2150. }
  2151. /**
  2152. * @brief FMPSMBUS interrupts error handler.
  2153. * @param hfmpsmbus FMPSMBUS handle.
  2154. * @retval None
  2155. */
  2156. static void FMPSMBUS_ITErrorHandler(FMPSMBUS_HandleTypeDef *hfmpsmbus)
  2157. {
  2158. uint32_t itflags = READ_REG(hfmpsmbus->Instance->ISR);
  2159. uint32_t itsources = READ_REG(hfmpsmbus->Instance->CR1);
  2160. uint32_t tmpstate;
  2161. uint32_t tmperror;
  2162. /* FMPSMBUS Bus error interrupt occurred ------------------------------------*/
  2163. if (((itflags & FMPSMBUS_FLAG_BERR) == FMPSMBUS_FLAG_BERR) && \
  2164. ((itsources & FMPSMBUS_IT_ERRI) == FMPSMBUS_IT_ERRI))
  2165. {
  2166. hfmpsmbus->ErrorCode |= HAL_FMPSMBUS_ERROR_BERR;
  2167. /* Clear BERR flag */
  2168. __HAL_FMPSMBUS_CLEAR_FLAG(hfmpsmbus, FMPSMBUS_FLAG_BERR);
  2169. }
  2170. /* FMPSMBUS Over-Run/Under-Run interrupt occurred ----------------------------------------*/
  2171. if (((itflags & FMPSMBUS_FLAG_OVR) == FMPSMBUS_FLAG_OVR) && \
  2172. ((itsources & FMPSMBUS_IT_ERRI) == FMPSMBUS_IT_ERRI))
  2173. {
  2174. hfmpsmbus->ErrorCode |= HAL_FMPSMBUS_ERROR_OVR;
  2175. /* Clear OVR flag */
  2176. __HAL_FMPSMBUS_CLEAR_FLAG(hfmpsmbus, FMPSMBUS_FLAG_OVR);
  2177. }
  2178. /* FMPSMBUS Arbitration Loss error interrupt occurred ------------------------------------*/
  2179. if (((itflags & FMPSMBUS_FLAG_ARLO) == FMPSMBUS_FLAG_ARLO) && \
  2180. ((itsources & FMPSMBUS_IT_ERRI) == FMPSMBUS_IT_ERRI))
  2181. {
  2182. hfmpsmbus->ErrorCode |= HAL_FMPSMBUS_ERROR_ARLO;
  2183. /* Clear ARLO flag */
  2184. __HAL_FMPSMBUS_CLEAR_FLAG(hfmpsmbus, FMPSMBUS_FLAG_ARLO);
  2185. }
  2186. /* FMPSMBUS Timeout error interrupt occurred ---------------------------------------------*/
  2187. if (((itflags & FMPSMBUS_FLAG_TIMEOUT) == FMPSMBUS_FLAG_TIMEOUT) && \
  2188. ((itsources & FMPSMBUS_IT_ERRI) == FMPSMBUS_IT_ERRI))
  2189. {
  2190. hfmpsmbus->ErrorCode |= HAL_FMPSMBUS_ERROR_BUSTIMEOUT;
  2191. /* Clear TIMEOUT flag */
  2192. __HAL_FMPSMBUS_CLEAR_FLAG(hfmpsmbus, FMPSMBUS_FLAG_TIMEOUT);
  2193. }
  2194. /* FMPSMBUS Alert error interrupt occurred -----------------------------------------------*/
  2195. if (((itflags & FMPSMBUS_FLAG_ALERT) == FMPSMBUS_FLAG_ALERT) && \
  2196. ((itsources & FMPSMBUS_IT_ERRI) == FMPSMBUS_IT_ERRI))
  2197. {
  2198. hfmpsmbus->ErrorCode |= HAL_FMPSMBUS_ERROR_ALERT;
  2199. /* Clear ALERT flag */
  2200. __HAL_FMPSMBUS_CLEAR_FLAG(hfmpsmbus, FMPSMBUS_FLAG_ALERT);
  2201. }
  2202. /* FMPSMBUS Packet Error Check error interrupt occurred ----------------------------------*/
  2203. if (((itflags & FMPSMBUS_FLAG_PECERR) == FMPSMBUS_FLAG_PECERR) && \
  2204. ((itsources & FMPSMBUS_IT_ERRI) == FMPSMBUS_IT_ERRI))
  2205. {
  2206. hfmpsmbus->ErrorCode |= HAL_FMPSMBUS_ERROR_PECERR;
  2207. /* Clear PEC error flag */
  2208. __HAL_FMPSMBUS_CLEAR_FLAG(hfmpsmbus, FMPSMBUS_FLAG_PECERR);
  2209. }
  2210. /* Store current volatile hfmpsmbus->State, misra rule */
  2211. tmperror = hfmpsmbus->ErrorCode;
  2212. /* Call the Error Callback in case of Error detected */
  2213. if ((tmperror != HAL_FMPSMBUS_ERROR_NONE) && (tmperror != HAL_FMPSMBUS_ERROR_ACKF))
  2214. {
  2215. /* Do not Reset the HAL state in case of ALERT error */
  2216. if ((tmperror & HAL_FMPSMBUS_ERROR_ALERT) != HAL_FMPSMBUS_ERROR_ALERT)
  2217. {
  2218. /* Store current volatile hfmpsmbus->State, misra rule */
  2219. tmpstate = hfmpsmbus->State;
  2220. if (((tmpstate & HAL_FMPSMBUS_STATE_SLAVE_BUSY_TX) == HAL_FMPSMBUS_STATE_SLAVE_BUSY_TX)
  2221. || ((tmpstate & HAL_FMPSMBUS_STATE_SLAVE_BUSY_RX) == HAL_FMPSMBUS_STATE_SLAVE_BUSY_RX))
  2222. {
  2223. /* Reset only HAL_FMPSMBUS_STATE_SLAVE_BUSY_XX */
  2224. /* keep HAL_FMPSMBUS_STATE_LISTEN if set */
  2225. hfmpsmbus->PreviousState = HAL_FMPSMBUS_STATE_READY;
  2226. hfmpsmbus->State = HAL_FMPSMBUS_STATE_LISTEN;
  2227. }
  2228. }
  2229. /* Call the Error callback to inform upper layer */
  2230. #if (USE_HAL_FMPSMBUS_REGISTER_CALLBACKS == 1)
  2231. hfmpsmbus->ErrorCallback(hfmpsmbus);
  2232. #else
  2233. HAL_FMPSMBUS_ErrorCallback(hfmpsmbus);
  2234. #endif /* USE_HAL_FMPSMBUS_REGISTER_CALLBACKS */
  2235. }
  2236. }
  2237. /**
  2238. * @brief Handle FMPSMBUS Communication Timeout.
  2239. * @param hfmpsmbus Pointer to a FMPSMBUS_HandleTypeDef structure that contains
  2240. * the configuration information for the specified FMPSMBUS.
  2241. * @param Flag Specifies the FMPSMBUS flag to check.
  2242. * @param Status The new Flag status (SET or RESET).
  2243. * @param Timeout Timeout duration
  2244. * @retval HAL status
  2245. */
  2246. static HAL_StatusTypeDef FMPSMBUS_WaitOnFlagUntilTimeout(FMPSMBUS_HandleTypeDef *hfmpsmbus, uint32_t Flag,
  2247. FlagStatus Status, uint32_t Timeout)
  2248. {
  2249. uint32_t tickstart = HAL_GetTick();
  2250. /* Wait until flag is set */
  2251. while ((FlagStatus)(__HAL_FMPSMBUS_GET_FLAG(hfmpsmbus, Flag)) == Status)
  2252. {
  2253. /* Check for the Timeout */
  2254. if (Timeout != HAL_MAX_DELAY)
  2255. {
  2256. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
  2257. {
  2258. hfmpsmbus->PreviousState = hfmpsmbus->State;
  2259. hfmpsmbus->State = HAL_FMPSMBUS_STATE_READY;
  2260. /* Update FMPSMBUS error code */
  2261. hfmpsmbus->ErrorCode |= HAL_FMPSMBUS_ERROR_HALTIMEOUT;
  2262. /* Process Unlocked */
  2263. __HAL_UNLOCK(hfmpsmbus);
  2264. return HAL_ERROR;
  2265. }
  2266. }
  2267. }
  2268. return HAL_OK;
  2269. }
  2270. /**
  2271. * @brief Handle FMPSMBUSx communication when starting transfer or during transfer (TC or TCR flag are set).
  2272. * @param hfmpsmbus FMPSMBUS handle.
  2273. * @param DevAddress specifies the slave address to be programmed.
  2274. * @param Size specifies the number of bytes to be programmed.
  2275. * This parameter must be a value between 0 and 255.
  2276. * @param Mode New state of the FMPSMBUS START condition generation.
  2277. * This parameter can be one or a combination of the following values:
  2278. * @arg @ref FMPSMBUS_RELOAD_MODE Enable Reload mode.
  2279. * @arg @ref FMPSMBUS_AUTOEND_MODE Enable Automatic end mode.
  2280. * @arg @ref FMPSMBUS_SOFTEND_MODE Enable Software end mode and Reload mode.
  2281. * @arg @ref FMPSMBUS_SENDPEC_MODE Enable Packet Error Calculation mode.
  2282. * @param Request New state of the FMPSMBUS START condition generation.
  2283. * This parameter can be one of the following values:
  2284. * @arg @ref FMPSMBUS_NO_STARTSTOP Don't Generate stop and start condition.
  2285. * @arg @ref FMPSMBUS_GENERATE_STOP Generate stop condition (Size should be set to 0).
  2286. * @arg @ref FMPSMBUS_GENERATE_START_READ Generate Restart for read request.
  2287. * @arg @ref FMPSMBUS_GENERATE_START_WRITE Generate Restart for write request.
  2288. * @retval None
  2289. */
  2290. static void FMPSMBUS_TransferConfig(FMPSMBUS_HandleTypeDef *hfmpsmbus, uint16_t DevAddress, uint8_t Size,
  2291. uint32_t Mode, uint32_t Request)
  2292. {
  2293. /* Check the parameters */
  2294. assert_param(IS_FMPSMBUS_ALL_INSTANCE(hfmpsmbus->Instance));
  2295. assert_param(IS_FMPSMBUS_TRANSFER_MODE(Mode));
  2296. assert_param(IS_FMPSMBUS_TRANSFER_REQUEST(Request));
  2297. /* update CR2 register */
  2298. MODIFY_REG(hfmpsmbus->Instance->CR2,
  2299. ((FMPI2C_CR2_SADD | FMPI2C_CR2_NBYTES | FMPI2C_CR2_RELOAD | FMPI2C_CR2_AUTOEND | \
  2300. (FMPI2C_CR2_RD_WRN & (uint32_t)(Request >> (31UL - FMPI2C_CR2_RD_WRN_Pos))) | \
  2301. FMPI2C_CR2_START | FMPI2C_CR2_STOP | FMPI2C_CR2_PECBYTE)), \
  2302. (uint32_t)(((uint32_t)DevAddress & FMPI2C_CR2_SADD) | \
  2303. (((uint32_t)Size << FMPI2C_CR2_NBYTES_Pos) & FMPI2C_CR2_NBYTES) | \
  2304. (uint32_t)Mode | (uint32_t)Request));
  2305. }
  2306. /**
  2307. * @brief Convert FMPSMBUSx OTHER_xxx XferOptions to functional XferOptions.
  2308. * @param hfmpsmbus FMPSMBUS handle.
  2309. * @retval None
  2310. */
  2311. static void FMPSMBUS_ConvertOtherXferOptions(FMPSMBUS_HandleTypeDef *hfmpsmbus)
  2312. {
  2313. /* if user set XferOptions to FMPSMBUS_OTHER_FRAME_NO_PEC */
  2314. /* it request implicitly to generate a restart condition */
  2315. /* set XferOptions to FMPSMBUS_FIRST_FRAME */
  2316. if (hfmpsmbus->XferOptions == FMPSMBUS_OTHER_FRAME_NO_PEC)
  2317. {
  2318. hfmpsmbus->XferOptions = FMPSMBUS_FIRST_FRAME;
  2319. }
  2320. /* else if user set XferOptions to FMPSMBUS_OTHER_FRAME_WITH_PEC */
  2321. /* it request implicitly to generate a restart condition */
  2322. /* set XferOptions to FMPSMBUS_FIRST_FRAME | FMPSMBUS_SENDPEC_MODE */
  2323. else if (hfmpsmbus->XferOptions == FMPSMBUS_OTHER_FRAME_WITH_PEC)
  2324. {
  2325. hfmpsmbus->XferOptions = FMPSMBUS_FIRST_FRAME | FMPSMBUS_SENDPEC_MODE;
  2326. }
  2327. /* else if user set XferOptions to FMPSMBUS_OTHER_AND_LAST_FRAME_NO_PEC */
  2328. /* it request implicitly to generate a restart condition */
  2329. /* then generate a stop condition at the end of transfer */
  2330. /* set XferOptions to FMPSMBUS_FIRST_AND_LAST_FRAME_NO_PEC */
  2331. else if (hfmpsmbus->XferOptions == FMPSMBUS_OTHER_AND_LAST_FRAME_NO_PEC)
  2332. {
  2333. hfmpsmbus->XferOptions = FMPSMBUS_FIRST_AND_LAST_FRAME_NO_PEC;
  2334. }
  2335. /* else if user set XferOptions to FMPSMBUS_OTHER_AND_LAST_FRAME_WITH_PEC */
  2336. /* it request implicitly to generate a restart condition */
  2337. /* then generate a stop condition at the end of transfer */
  2338. /* set XferOptions to FMPSMBUS_FIRST_AND_LAST_FRAME_WITH_PEC */
  2339. else if (hfmpsmbus->XferOptions == FMPSMBUS_OTHER_AND_LAST_FRAME_WITH_PEC)
  2340. {
  2341. hfmpsmbus->XferOptions = FMPSMBUS_FIRST_AND_LAST_FRAME_WITH_PEC;
  2342. }
  2343. else
  2344. {
  2345. /* Nothing to do */
  2346. }
  2347. }
  2348. /**
  2349. * @}
  2350. */
  2351. #endif /* FMPI2C_CR1_PE */
  2352. #endif /* HAL_FMPSMBUS_MODULE_ENABLED */
  2353. /**
  2354. * @}
  2355. */
  2356. /**
  2357. * @}
  2358. */