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

2674 lines
92 KiB

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