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.
 
 
 

6614 lines
213 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32wbxx_hal_i2c.c
  4. * @author MCD Application Team
  5. * @brief I2C HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Inter Integrated Circuit (I2C) peripheral:
  8. * + Initialization and de-initialization functions
  9. * + IO operation functions
  10. * + Peripheral State and Errors functions
  11. *
  12. @verbatim
  13. ==============================================================================
  14. ##### How to use this driver #####
  15. ==============================================================================
  16. [..]
  17. The I2C HAL driver can be used as follows:
  18. (#) Declare a I2C_HandleTypeDef handle structure, for example:
  19. I2C_HandleTypeDef hi2c;
  20. (#)Initialize the I2C low level resources by implementing the @ref HAL_I2C_MspInit() API:
  21. (##) Enable the I2Cx interface clock
  22. (##) I2C pins configuration
  23. (+++) Enable the clock for the I2C GPIOs
  24. (+++) Configure I2C pins as alternate function open-drain
  25. (##) NVIC configuration if you need to use interrupt process
  26. (+++) Configure the I2Cx interrupt priority
  27. (+++) Enable the NVIC I2C IRQ Channel
  28. (##) DMA Configuration if you need to use DMA process
  29. (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive channel
  30. (+++) Enable the DMAx interface clock using
  31. (+++) Configure the DMA handle parameters
  32. (+++) Configure the DMA Tx or Rx channel
  33. (+++) Associate the initialized DMA handle to the hi2c DMA Tx or Rx handle
  34. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on
  35. the DMA Tx or Rx channel
  36. (#) Configure the Communication Clock Timing, Own Address1, Master Addressing mode, Dual Addressing mode,
  37. Own Address2, Own Address2 Mask, General call and Nostretch mode in the hi2c Init structure.
  38. (#) Initialize the I2C registers by calling the @ref HAL_I2C_Init(), configures also the low level Hardware
  39. (GPIO, CLOCK, NVIC...etc) by calling the customized @ref HAL_I2C_MspInit(&hi2c) API.
  40. (#) To check if target device is ready for communication, use the function @ref HAL_I2C_IsDeviceReady()
  41. (#) For I2C IO and IO MEM operations, three operation modes are available within this driver :
  42. *** Polling mode IO operation ***
  43. =================================
  44. [..]
  45. (+) Transmit in master mode an amount of data in blocking mode using @ref HAL_I2C_Master_Transmit()
  46. (+) Receive in master mode an amount of data in blocking mode using @ref HAL_I2C_Master_Receive()
  47. (+) Transmit in slave mode an amount of data in blocking mode using @ref HAL_I2C_Slave_Transmit()
  48. (+) Receive in slave mode an amount of data in blocking mode using @ref HAL_I2C_Slave_Receive()
  49. *** Polling mode IO MEM operation ***
  50. =====================================
  51. [..]
  52. (+) Write an amount of data in blocking mode to a specific memory address using @ref HAL_I2C_Mem_Write()
  53. (+) Read an amount of data in blocking mode from a specific memory address using @ref HAL_I2C_Mem_Read()
  54. *** Interrupt mode IO operation ***
  55. ===================================
  56. [..]
  57. (+) Transmit in master mode an amount of data in non-blocking mode using @ref HAL_I2C_Master_Transmit_IT()
  58. (+) At transmission end of transfer, @ref HAL_I2C_MasterTxCpltCallback() is executed and user can
  59. add his own code by customization of function pointer @ref HAL_I2C_MasterTxCpltCallback()
  60. (+) Receive in master mode an amount of data in non-blocking mode using @ref HAL_I2C_Master_Receive_IT()
  61. (+) At reception end of transfer, @ref HAL_I2C_MasterRxCpltCallback() is executed and user can
  62. add his own code by customization of function pointer @ref HAL_I2C_MasterRxCpltCallback()
  63. (+) Transmit in slave mode an amount of data in non-blocking mode using @ref HAL_I2C_Slave_Transmit_IT()
  64. (+) At transmission end of transfer, @ref HAL_I2C_SlaveTxCpltCallback() is executed and user can
  65. add his own code by customization of function pointer @ref HAL_I2C_SlaveTxCpltCallback()
  66. (+) Receive in slave mode an amount of data in non-blocking mode using @ref HAL_I2C_Slave_Receive_IT()
  67. (+) At reception end of transfer, @ref HAL_I2C_SlaveRxCpltCallback() is executed and user can
  68. add his own code by customization of function pointer @ref HAL_I2C_SlaveRxCpltCallback()
  69. (+) In case of transfer Error, @ref HAL_I2C_ErrorCallback() function is executed and user can
  70. add his own code by customization of function pointer @ref HAL_I2C_ErrorCallback()
  71. (+) Abort a master I2C process communication with Interrupt using @ref HAL_I2C_Master_Abort_IT()
  72. (+) End of abort process, @ref HAL_I2C_AbortCpltCallback() is executed and user can
  73. add his own code by customization of function pointer @ref HAL_I2C_AbortCpltCallback()
  74. (+) Discard a slave I2C process communication using @ref __HAL_I2C_GENERATE_NACK() macro.
  75. This action will inform Master to generate a Stop condition to discard the communication.
  76. *** Interrupt mode or DMA mode IO sequential operation ***
  77. ==========================================================
  78. [..]
  79. (@) These interfaces allow to manage a sequential transfer with a repeated start condition
  80. when a direction change during transfer
  81. [..]
  82. (+) A specific option field manage the different steps of a sequential transfer
  83. (+) Option field values are defined through @ref I2C_XFEROPTIONS and are listed below:
  84. (++) I2C_FIRST_AND_LAST_FRAME: No sequential usage, functional is same as associated interfaces in no sequential mode
  85. (++) I2C_FIRST_FRAME: Sequential usage, this option allow to manage a sequence with start condition, address
  86. and data to transfer without a final stop condition
  87. (++) I2C_FIRST_AND_NEXT_FRAME: Sequential usage (Master only), this option allow to manage a sequence with start condition, address
  88. and data to transfer without a final stop condition, an then permit a call the same master sequential interface
  89. several times (like @ref HAL_I2C_Master_Seq_Transmit_IT() then @ref HAL_I2C_Master_Seq_Transmit_IT()
  90. or @ref HAL_I2C_Master_Seq_Transmit_DMA() then @ref HAL_I2C_Master_Seq_Transmit_DMA())
  91. (++) I2C_NEXT_FRAME: Sequential usage, this option allow to manage a sequence with a restart condition, address
  92. and with new data to transfer if the direction change or manage only the new data to transfer
  93. if no direction change and without a final stop condition in both cases
  94. (++) I2C_LAST_FRAME: Sequential usage, this option allow to manage a sequance with a restart condition, address
  95. and with new data to transfer if the direction change or manage only the new data to transfer
  96. if no direction change and with a final stop condition in both cases
  97. (++) I2C_LAST_FRAME_NO_STOP: Sequential usage (Master only), this option allow to manage a restart condition after several call of the same master sequential
  98. interface several times (link with option I2C_FIRST_AND_NEXT_FRAME).
  99. Usage can, transfer several bytes one by one using HAL_I2C_Master_Seq_Transmit_IT(option I2C_FIRST_AND_NEXT_FRAME then I2C_NEXT_FRAME)
  100. or HAL_I2C_Master_Seq_Receive_IT(option I2C_FIRST_AND_NEXT_FRAME then I2C_NEXT_FRAME)
  101. or HAL_I2C_Master_Seq_Transmit_DMA(option I2C_FIRST_AND_NEXT_FRAME then I2C_NEXT_FRAME)
  102. or HAL_I2C_Master_Seq_Receive_DMA(option I2C_FIRST_AND_NEXT_FRAME then I2C_NEXT_FRAME).
  103. Then usage of this option I2C_LAST_FRAME_NO_STOP at the last Transmit or Receive sequence permit to call the opposite interface Receive or Transmit
  104. without stopping the communication and so generate a restart condition.
  105. (++) I2C_OTHER_FRAME: Sequential usage (Master only), this option allow to manage a restart condition after each call of the same master sequential
  106. interface.
  107. Usage can, transfer several bytes one by one with a restart with slave address between each bytes using HAL_I2C_Master_Seq_Transmit_IT(option I2C_FIRST_FRAME then I2C_OTHER_FRAME)
  108. or HAL_I2C_Master_Seq_Receive_IT(option I2C_FIRST_FRAME then I2C_OTHER_FRAME)
  109. or HAL_I2C_Master_Seq_Transmit_DMA(option I2C_FIRST_FRAME then I2C_OTHER_FRAME)
  110. or HAL_I2C_Master_Seq_Receive_DMA(option I2C_FIRST_FRAME then I2C_OTHER_FRAME).
  111. Then usage of this option I2C_OTHER_AND_LAST_FRAME at the last frame to help automatic generation of STOP condition.
  112. (+) Different sequential I2C interfaces are listed below:
  113. (++) Sequential transmit in master I2C mode an amount of data in non-blocking mode using @ref HAL_I2C_Master_Seq_Transmit_IT()
  114. or using @ref HAL_I2C_Master_Seq_Transmit_DMA()
  115. (+++) At transmission end of current frame transfer, @ref HAL_I2C_MasterTxCpltCallback() is executed and user can
  116. add his own code by customization of function pointer @ref HAL_I2C_MasterTxCpltCallback()
  117. (++) Sequential receive in master I2C mode an amount of data in non-blocking mode using @ref HAL_I2C_Master_Seq_Receive_IT()
  118. or using @ref HAL_I2C_Master_Seq_Receive_DMA()
  119. (+++) At reception end of current frame transfer, @ref HAL_I2C_MasterRxCpltCallback() is executed and user can
  120. add his own code by customization of function pointer @ref HAL_I2C_MasterRxCpltCallback()
  121. (++) Abort a master IT or DMA I2C process communication with Interrupt using @ref HAL_I2C_Master_Abort_IT()
  122. (+++) End of abort process, @ref HAL_I2C_AbortCpltCallback() is executed and user can
  123. add his own code by customization of function pointer @ref HAL_I2C_AbortCpltCallback()
  124. (++) Enable/disable the Address listen mode in slave I2C mode using @ref HAL_I2C_EnableListen_IT() @ref HAL_I2C_DisableListen_IT()
  125. (+++) When address slave I2C match, @ref HAL_I2C_AddrCallback() is executed and user can
  126. add his own code to check the Address Match Code and the transmission direction request by master (Write/Read).
  127. (+++) At Listen mode end @ref HAL_I2C_ListenCpltCallback() is executed and user can
  128. add his own code by customization of function pointer @ref HAL_I2C_ListenCpltCallback()
  129. (++) Sequential transmit in slave I2C mode an amount of data in non-blocking mode using @ref HAL_I2C_Slave_Seq_Transmit_IT()
  130. or using @ref HAL_I2C_Slave_Seq_Transmit_DMA()
  131. (+++) At transmission end of current frame transfer, @ref HAL_I2C_SlaveTxCpltCallback() is executed and user can
  132. add his own code by customization of function pointer @ref HAL_I2C_SlaveTxCpltCallback()
  133. (++) Sequential receive in slave I2C mode an amount of data in non-blocking mode using @ref HAL_I2C_Slave_Seq_Receive_IT()
  134. or using @ref HAL_I2C_Slave_Seq_Receive_DMA()
  135. (+++) At reception end of current frame transfer, @ref HAL_I2C_SlaveRxCpltCallback() is executed and user can
  136. add his own code by customization of function pointer @ref HAL_I2C_SlaveRxCpltCallback()
  137. (++) In case of transfer Error, @ref HAL_I2C_ErrorCallback() function is executed and user can
  138. add his own code by customization of function pointer @ref HAL_I2C_ErrorCallback()
  139. (++) Discard a slave I2C process communication using @ref __HAL_I2C_GENERATE_NACK() macro.
  140. This action will inform Master to generate a Stop condition to discard the communication.
  141. *** Interrupt mode IO MEM operation ***
  142. =======================================
  143. [..]
  144. (+) Write an amount of data in non-blocking mode with Interrupt to a specific memory address using
  145. @ref HAL_I2C_Mem_Write_IT()
  146. (+) At Memory end of write transfer, @ref HAL_I2C_MemTxCpltCallback() is executed and user can
  147. add his own code by customization of function pointer @ref HAL_I2C_MemTxCpltCallback()
  148. (+) Read an amount of data in non-blocking mode with Interrupt from a specific memory address using
  149. @ref HAL_I2C_Mem_Read_IT()
  150. (+) At Memory end of read transfer, @ref HAL_I2C_MemRxCpltCallback() is executed and user can
  151. add his own code by customization of function pointer @ref HAL_I2C_MemRxCpltCallback()
  152. (+) In case of transfer Error, @ref HAL_I2C_ErrorCallback() function is executed and user can
  153. add his own code by customization of function pointer @ref HAL_I2C_ErrorCallback()
  154. *** DMA mode IO operation ***
  155. ==============================
  156. [..]
  157. (+) Transmit in master mode an amount of data in non-blocking mode (DMA) using
  158. @ref HAL_I2C_Master_Transmit_DMA()
  159. (+) At transmission end of transfer, @ref HAL_I2C_MasterTxCpltCallback() is executed and user can
  160. add his own code by customization of function pointer @ref HAL_I2C_MasterTxCpltCallback()
  161. (+) Receive in master mode an amount of data in non-blocking mode (DMA) using
  162. @ref HAL_I2C_Master_Receive_DMA()
  163. (+) At reception end of transfer, @ref HAL_I2C_MasterRxCpltCallback() is executed and user can
  164. add his own code by customization of function pointer @ref HAL_I2C_MasterRxCpltCallback()
  165. (+) Transmit in slave mode an amount of data in non-blocking mode (DMA) using
  166. @ref HAL_I2C_Slave_Transmit_DMA()
  167. (+) At transmission end of transfer, @ref HAL_I2C_SlaveTxCpltCallback() is executed and user can
  168. add his own code by customization of function pointer @ref HAL_I2C_SlaveTxCpltCallback()
  169. (+) Receive in slave mode an amount of data in non-blocking mode (DMA) using
  170. @ref HAL_I2C_Slave_Receive_DMA()
  171. (+) At reception end of transfer, @ref HAL_I2C_SlaveRxCpltCallback() is executed and user can
  172. add his own code by customization of function pointer @ref HAL_I2C_SlaveRxCpltCallback()
  173. (+) In case of transfer Error, @ref HAL_I2C_ErrorCallback() function is executed and user can
  174. add his own code by customization of function pointer @ref HAL_I2C_ErrorCallback()
  175. (+) Abort a master I2C process communication with Interrupt using @ref HAL_I2C_Master_Abort_IT()
  176. (+) End of abort process, @ref HAL_I2C_AbortCpltCallback() is executed and user can
  177. add his own code by customization of function pointer @ref HAL_I2C_AbortCpltCallback()
  178. (+) Discard a slave I2C process communication using @ref __HAL_I2C_GENERATE_NACK() macro.
  179. This action will inform Master to generate a Stop condition to discard the communication.
  180. *** DMA mode IO MEM operation ***
  181. =================================
  182. [..]
  183. (+) Write an amount of data in non-blocking mode with DMA to a specific memory address using
  184. @ref HAL_I2C_Mem_Write_DMA()
  185. (+) At Memory end of write transfer, @ref HAL_I2C_MemTxCpltCallback() is executed and user can
  186. add his own code by customization of function pointer @ref HAL_I2C_MemTxCpltCallback()
  187. (+) Read an amount of data in non-blocking mode with DMA from a specific memory address using
  188. @ref HAL_I2C_Mem_Read_DMA()
  189. (+) At Memory end of read transfer, @ref HAL_I2C_MemRxCpltCallback() is executed and user can
  190. add his own code by customization of function pointer @ref HAL_I2C_MemRxCpltCallback()
  191. (+) In case of transfer Error, @ref HAL_I2C_ErrorCallback() function is executed and user can
  192. add his own code by customization of function pointer @ref HAL_I2C_ErrorCallback()
  193. *** I2C HAL driver macros list ***
  194. ==================================
  195. [..]
  196. Below the list of most used macros in I2C HAL driver.
  197. (+) @ref __HAL_I2C_ENABLE: Enable the I2C peripheral
  198. (+) @ref __HAL_I2C_DISABLE: Disable the I2C peripheral
  199. (+) @ref __HAL_I2C_GENERATE_NACK: Generate a Non-Acknowledge I2C peripheral in Slave mode
  200. (+) @ref __HAL_I2C_GET_FLAG: Check whether the specified I2C flag is set or not
  201. (+) @ref __HAL_I2C_CLEAR_FLAG: Clear the specified I2C pending flag
  202. (+) @ref __HAL_I2C_ENABLE_IT: Enable the specified I2C interrupt
  203. (+) @ref __HAL_I2C_DISABLE_IT: Disable the specified I2C interrupt
  204. *** Callback registration ***
  205. =============================================
  206. [..]
  207. The compilation flag USE_HAL_I2C_REGISTER_CALLBACKS when set to 1
  208. allows the user to configure dynamically the driver callbacks.
  209. Use Functions @ref HAL_I2C_RegisterCallback() or @ref HAL_I2C_RegisterAddrCallback()
  210. to register an interrupt callback.
  211. [..]
  212. Function @ref HAL_I2C_RegisterCallback() allows to register following callbacks:
  213. (+) MasterTxCpltCallback : callback for Master transmission end of transfer.
  214. (+) MasterRxCpltCallback : callback for Master reception end of transfer.
  215. (+) SlaveTxCpltCallback : callback for Slave transmission end of transfer.
  216. (+) SlaveRxCpltCallback : callback for Slave reception end of transfer.
  217. (+) ListenCpltCallback : callback for end of listen mode.
  218. (+) MemTxCpltCallback : callback for Memory transmission end of transfer.
  219. (+) MemRxCpltCallback : callback for Memory reception end of transfer.
  220. (+) ErrorCallback : callback for error detection.
  221. (+) AbortCpltCallback : callback for abort completion process.
  222. (+) MspInitCallback : callback for Msp Init.
  223. (+) MspDeInitCallback : callback for Msp DeInit.
  224. This function takes as parameters the HAL peripheral handle, the Callback ID
  225. and a pointer to the user callback function.
  226. [..]
  227. For specific callback AddrCallback use dedicated register callbacks : @ref HAL_I2C_RegisterAddrCallback().
  228. [..]
  229. Use function @ref HAL_I2C_UnRegisterCallback to reset a callback to the default
  230. weak function.
  231. @ref HAL_I2C_UnRegisterCallback takes as parameters the HAL peripheral handle,
  232. and the Callback ID.
  233. This function allows to reset following callbacks:
  234. (+) MasterTxCpltCallback : callback for Master transmission end of transfer.
  235. (+) MasterRxCpltCallback : callback for Master reception end of transfer.
  236. (+) SlaveTxCpltCallback : callback for Slave transmission end of transfer.
  237. (+) SlaveRxCpltCallback : callback for Slave reception end of transfer.
  238. (+) ListenCpltCallback : callback for end of listen mode.
  239. (+) MemTxCpltCallback : callback for Memory transmission end of transfer.
  240. (+) MemRxCpltCallback : callback for Memory reception end of transfer.
  241. (+) ErrorCallback : callback for error detection.
  242. (+) AbortCpltCallback : callback for abort completion process.
  243. (+) MspInitCallback : callback for Msp Init.
  244. (+) MspDeInitCallback : callback for Msp DeInit.
  245. [..]
  246. For callback AddrCallback use dedicated register callbacks : @ref HAL_I2C_UnRegisterAddrCallback().
  247. [..]
  248. By default, after the @ref HAL_I2C_Init() and when the state is @ref HAL_I2C_STATE_RESET
  249. all callbacks are set to the corresponding weak functions:
  250. examples @ref HAL_I2C_MasterTxCpltCallback(), @ref HAL_I2C_MasterRxCpltCallback().
  251. Exception done for MspInit and MspDeInit functions that are
  252. reset to the legacy weak functions in the @ref HAL_I2C_Init()/ @ref HAL_I2C_DeInit() only when
  253. these callbacks are null (not registered beforehand).
  254. If MspInit or MspDeInit are not null, the @ref HAL_I2C_Init()/ @ref HAL_I2C_DeInit()
  255. keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
  256. [..]
  257. Callbacks can be registered/unregistered in @ref HAL_I2C_STATE_READY state only.
  258. Exception done MspInit/MspDeInit functions that can be registered/unregistered
  259. in @ref HAL_I2C_STATE_READY or @ref HAL_I2C_STATE_RESET state,
  260. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  261. Then, the user first registers the MspInit/MspDeInit user callbacks
  262. using @ref HAL_I2C_RegisterCallback() before calling @ref HAL_I2C_DeInit()
  263. or @ref HAL_I2C_Init() function.
  264. [..]
  265. When the compilation flag USE_HAL_I2C_REGISTER_CALLBACKS is set to 0 or
  266. not defined, the callback registration feature is not available and all callbacks
  267. are set to the corresponding weak functions.
  268. [..]
  269. (@) You can refer to the I2C HAL driver header file for more useful macros
  270. @endverbatim
  271. ******************************************************************************
  272. * @attention
  273. *
  274. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  275. * All rights reserved.</center></h2>
  276. *
  277. * This software component is licensed by ST under BSD 3-Clause license,
  278. * the "License"; You may not use this file except in compliance with the
  279. * License. You may obtain a copy of the License at:
  280. * opensource.org/licenses/BSD-3-Clause
  281. *
  282. ******************************************************************************
  283. */
  284. /* Includes ------------------------------------------------------------------*/
  285. #include "stm32wbxx_hal.h"
  286. /** @addtogroup STM32WBxx_HAL_Driver
  287. * @{
  288. */
  289. /** @defgroup I2C I2C
  290. * @brief I2C HAL module driver
  291. * @{
  292. */
  293. #ifdef HAL_I2C_MODULE_ENABLED
  294. /* Private typedef -----------------------------------------------------------*/
  295. /* Private define ------------------------------------------------------------*/
  296. /** @defgroup I2C_Private_Define I2C Private Define
  297. * @{
  298. */
  299. #define TIMING_CLEAR_MASK (0xF0FFFFFFU) /*!< I2C TIMING clear register Mask */
  300. #define I2C_TIMEOUT_ADDR (10000U) /*!< 10 s */
  301. #define I2C_TIMEOUT_BUSY (25U) /*!< 25 ms */
  302. #define I2C_TIMEOUT_DIR (25U) /*!< 25 ms */
  303. #define I2C_TIMEOUT_RXNE (25U) /*!< 25 ms */
  304. #define I2C_TIMEOUT_STOPF (25U) /*!< 25 ms */
  305. #define I2C_TIMEOUT_TC (25U) /*!< 25 ms */
  306. #define I2C_TIMEOUT_TCR (25U) /*!< 25 ms */
  307. #define I2C_TIMEOUT_TXIS (25U) /*!< 25 ms */
  308. #define I2C_TIMEOUT_FLAG (25U) /*!< 25 ms */
  309. #define MAX_NBYTE_SIZE 255U
  310. #define SlaveAddr_SHIFT 7U
  311. #define SlaveAddr_MSK 0x06U
  312. /* Private define for @ref PreviousState usage */
  313. #define I2C_STATE_MSK ((uint32_t)((uint32_t)((uint32_t)HAL_I2C_STATE_BUSY_TX | (uint32_t)HAL_I2C_STATE_BUSY_RX) & (uint32_t)(~((uint32_t)HAL_I2C_STATE_READY)))) /*!< Mask State define, keep only RX and TX bits */
  314. #define I2C_STATE_NONE ((uint32_t)(HAL_I2C_MODE_NONE)) /*!< Default Value */
  315. #define I2C_STATE_MASTER_BUSY_TX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_TX & I2C_STATE_MSK) | (uint32_t)HAL_I2C_MODE_MASTER)) /*!< Master Busy TX, combinaison of State LSB and Mode enum */
  316. #define I2C_STATE_MASTER_BUSY_RX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_RX & I2C_STATE_MSK) | (uint32_t)HAL_I2C_MODE_MASTER)) /*!< Master Busy RX, combinaison of State LSB and Mode enum */
  317. #define I2C_STATE_SLAVE_BUSY_TX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_TX & I2C_STATE_MSK) | (uint32_t)HAL_I2C_MODE_SLAVE)) /*!< Slave Busy TX, combinaison of State LSB and Mode enum */
  318. #define I2C_STATE_SLAVE_BUSY_RX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_RX & I2C_STATE_MSK) | (uint32_t)HAL_I2C_MODE_SLAVE)) /*!< Slave Busy RX, combinaison of State LSB and Mode enum */
  319. #define I2C_STATE_MEM_BUSY_TX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_TX & I2C_STATE_MSK) | (uint32_t)HAL_I2C_MODE_MEM)) /*!< Memory Busy TX, combinaison of State LSB and Mode enum */
  320. #define I2C_STATE_MEM_BUSY_RX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_RX & I2C_STATE_MSK) | (uint32_t)HAL_I2C_MODE_MEM)) /*!< Memory Busy RX, combinaison of State LSB and Mode enum */
  321. /* Private define to centralize the enable/disable of Interrupts */
  322. #define I2C_XFER_TX_IT (uint16_t)(0x0001U) /* Bit field can be combinated with @ref I2C_XFER_LISTEN_IT */
  323. #define I2C_XFER_RX_IT (uint16_t)(0x0002U) /* Bit field can be combinated with @ref I2C_XFER_LISTEN_IT */
  324. #define I2C_XFER_LISTEN_IT (uint16_t)(0x8000U) /* Bit field can be combinated with @ref I2C_XFER_TX_IT and @ref I2C_XFER_RX_IT */
  325. #define I2C_XFER_ERROR_IT (uint16_t)(0x0010U) /* Bit definition to manage addition of global Error and NACK treatment */
  326. #define I2C_XFER_CPLT_IT (uint16_t)(0x0020U) /* Bit definition to manage only STOP evenement */
  327. #define I2C_XFER_RELOAD_IT (uint16_t)(0x0040U) /* Bit definition to manage only Reload of NBYTE */
  328. /* Private define Sequential Transfer Options default/reset value */
  329. #define I2C_NO_OPTION_FRAME (0xFFFF0000U)
  330. /**
  331. * @}
  332. */
  333. /* Private macro -------------------------------------------------------------*/
  334. /* Private variables ---------------------------------------------------------*/
  335. /* Private function prototypes -----------------------------------------------*/
  336. /** @defgroup I2C_Private_Functions I2C Private Functions
  337. * @{
  338. */
  339. /* Private functions to handle DMA transfer */
  340. static void I2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma);
  341. static void I2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma);
  342. static void I2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma);
  343. static void I2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma);
  344. static void I2C_DMAError(DMA_HandleTypeDef *hdma);
  345. static void I2C_DMAAbort(DMA_HandleTypeDef *hdma);
  346. /* Private functions to handle IT transfer */
  347. static void I2C_ITAddrCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags);
  348. static void I2C_ITMasterSeqCplt(I2C_HandleTypeDef *hi2c);
  349. static void I2C_ITSlaveSeqCplt(I2C_HandleTypeDef *hi2c);
  350. static void I2C_ITMasterCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags);
  351. static void I2C_ITSlaveCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags);
  352. static void I2C_ITListenCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags);
  353. static void I2C_ITError(I2C_HandleTypeDef *hi2c, uint32_t ErrorCode);
  354. /* Private functions to handle IT transfer */
  355. static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart);
  356. static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart);
  357. /* Private functions for I2C transfer IRQ handler */
  358. static HAL_StatusTypeDef I2C_Master_ISR_IT(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags, uint32_t ITSources);
  359. static HAL_StatusTypeDef I2C_Slave_ISR_IT(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags, uint32_t ITSources);
  360. static HAL_StatusTypeDef I2C_Master_ISR_DMA(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags, uint32_t ITSources);
  361. static HAL_StatusTypeDef I2C_Slave_ISR_DMA(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags, uint32_t ITSources);
  362. /* Private functions to handle flags during polling transfer */
  363. static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t Tickstart);
  364. static HAL_StatusTypeDef I2C_WaitOnTXISFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
  365. static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
  366. static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
  367. static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
  368. /* Private functions to centralize the enable/disable of Interrupts */
  369. static void I2C_Enable_IRQ(I2C_HandleTypeDef *hi2c, uint16_t InterruptRequest);
  370. static void I2C_Disable_IRQ(I2C_HandleTypeDef *hi2c, uint16_t InterruptRequest);
  371. /* Private function to treat different error callback */
  372. static void I2C_TreatErrorCallback(I2C_HandleTypeDef *hi2c);
  373. /* Private function to flush TXDR register */
  374. static void I2C_Flush_TXDR(I2C_HandleTypeDef *hi2c);
  375. /* Private function to handle start, restart or stop a transfer */
  376. static void I2C_TransferConfig(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t Size, uint32_t Mode, uint32_t Request);
  377. /* Private function to Convert Specific options */
  378. static void I2C_ConvertOtherXferOptions(I2C_HandleTypeDef *hi2c);
  379. /**
  380. * @}
  381. */
  382. /* Exported functions --------------------------------------------------------*/
  383. /** @defgroup I2C_Exported_Functions I2C Exported Functions
  384. * @{
  385. */
  386. /** @defgroup I2C_Exported_Functions_Group1 Initialization and de-initialization functions
  387. * @brief Initialization and Configuration functions
  388. *
  389. @verbatim
  390. ===============================================================================
  391. ##### Initialization and de-initialization functions #####
  392. ===============================================================================
  393. [..] This subsection provides a set of functions allowing to initialize and
  394. deinitialize the I2Cx peripheral:
  395. (+) User must Implement HAL_I2C_MspInit() function in which he configures
  396. all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
  397. (+) Call the function HAL_I2C_Init() to configure the selected device with
  398. the selected configuration:
  399. (++) Clock Timing
  400. (++) Own Address 1
  401. (++) Addressing mode (Master, Slave)
  402. (++) Dual Addressing mode
  403. (++) Own Address 2
  404. (++) Own Address 2 Mask
  405. (++) General call mode
  406. (++) Nostretch mode
  407. (+) Call the function HAL_I2C_DeInit() to restore the default configuration
  408. of the selected I2Cx peripheral.
  409. @endverbatim
  410. * @{
  411. */
  412. /**
  413. * @brief Initializes the I2C according to the specified parameters
  414. * in the I2C_InitTypeDef and initialize the associated handle.
  415. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  416. * the configuration information for the specified I2C.
  417. * @retval HAL status
  418. */
  419. HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c)
  420. {
  421. /* Check the I2C handle allocation */
  422. if (hi2c == NULL)
  423. {
  424. return HAL_ERROR;
  425. }
  426. /* Check the parameters */
  427. assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  428. assert_param(IS_I2C_OWN_ADDRESS1(hi2c->Init.OwnAddress1));
  429. assert_param(IS_I2C_ADDRESSING_MODE(hi2c->Init.AddressingMode));
  430. assert_param(IS_I2C_DUAL_ADDRESS(hi2c->Init.DualAddressMode));
  431. assert_param(IS_I2C_OWN_ADDRESS2(hi2c->Init.OwnAddress2));
  432. assert_param(IS_I2C_OWN_ADDRESS2_MASK(hi2c->Init.OwnAddress2Masks));
  433. assert_param(IS_I2C_GENERAL_CALL(hi2c->Init.GeneralCallMode));
  434. assert_param(IS_I2C_NO_STRETCH(hi2c->Init.NoStretchMode));
  435. if (hi2c->State == HAL_I2C_STATE_RESET)
  436. {
  437. /* Allocate lock resource and initialize it */
  438. hi2c->Lock = HAL_UNLOCKED;
  439. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  440. /* Init the I2C Callback settings */
  441. hi2c->MasterTxCpltCallback = HAL_I2C_MasterTxCpltCallback; /* Legacy weak MasterTxCpltCallback */
  442. hi2c->MasterRxCpltCallback = HAL_I2C_MasterRxCpltCallback; /* Legacy weak MasterRxCpltCallback */
  443. hi2c->SlaveTxCpltCallback = HAL_I2C_SlaveTxCpltCallback; /* Legacy weak SlaveTxCpltCallback */
  444. hi2c->SlaveRxCpltCallback = HAL_I2C_SlaveRxCpltCallback; /* Legacy weak SlaveRxCpltCallback */
  445. hi2c->ListenCpltCallback = HAL_I2C_ListenCpltCallback; /* Legacy weak ListenCpltCallback */
  446. hi2c->MemTxCpltCallback = HAL_I2C_MemTxCpltCallback; /* Legacy weak MemTxCpltCallback */
  447. hi2c->MemRxCpltCallback = HAL_I2C_MemRxCpltCallback; /* Legacy weak MemRxCpltCallback */
  448. hi2c->ErrorCallback = HAL_I2C_ErrorCallback; /* Legacy weak ErrorCallback */
  449. hi2c->AbortCpltCallback = HAL_I2C_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
  450. hi2c->AddrCallback = HAL_I2C_AddrCallback; /* Legacy weak AddrCallback */
  451. if (hi2c->MspInitCallback == NULL)
  452. {
  453. hi2c->MspInitCallback = HAL_I2C_MspInit; /* Legacy weak MspInit */
  454. }
  455. /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  456. hi2c->MspInitCallback(hi2c);
  457. #else
  458. /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  459. HAL_I2C_MspInit(hi2c);
  460. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  461. }
  462. hi2c->State = HAL_I2C_STATE_BUSY;
  463. /* Disable the selected I2C peripheral */
  464. __HAL_I2C_DISABLE(hi2c);
  465. /*---------------------------- I2Cx TIMINGR Configuration ------------------*/
  466. /* Configure I2Cx: Frequency range */
  467. hi2c->Instance->TIMINGR = hi2c->Init.Timing & TIMING_CLEAR_MASK;
  468. /*---------------------------- I2Cx OAR1 Configuration ---------------------*/
  469. /* Disable Own Address1 before set the Own Address1 configuration */
  470. hi2c->Instance->OAR1 &= ~I2C_OAR1_OA1EN;
  471. /* Configure I2Cx: Own Address1 and ack own address1 mode */
  472. if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
  473. {
  474. hi2c->Instance->OAR1 = (I2C_OAR1_OA1EN | hi2c->Init.OwnAddress1);
  475. }
  476. else /* I2C_ADDRESSINGMODE_10BIT */
  477. {
  478. hi2c->Instance->OAR1 = (I2C_OAR1_OA1EN | I2C_OAR1_OA1MODE | hi2c->Init.OwnAddress1);
  479. }
  480. /*---------------------------- I2Cx CR2 Configuration ----------------------*/
  481. /* Configure I2Cx: Addressing Master mode */
  482. if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
  483. {
  484. hi2c->Instance->CR2 = (I2C_CR2_ADD10);
  485. }
  486. /* Enable the AUTOEND by default, and enable NACK (should be disable only during Slave process */
  487. hi2c->Instance->CR2 |= (I2C_CR2_AUTOEND | I2C_CR2_NACK);
  488. /*---------------------------- I2Cx OAR2 Configuration ---------------------*/
  489. /* Disable Own Address2 before set the Own Address2 configuration */
  490. hi2c->Instance->OAR2 &= ~I2C_DUALADDRESS_ENABLE;
  491. /* Configure I2Cx: Dual mode and Own Address2 */
  492. hi2c->Instance->OAR2 = (hi2c->Init.DualAddressMode | hi2c->Init.OwnAddress2 | (hi2c->Init.OwnAddress2Masks << 8));
  493. /*---------------------------- I2Cx CR1 Configuration ----------------------*/
  494. /* Configure I2Cx: Generalcall and NoStretch mode */
  495. hi2c->Instance->CR1 = (hi2c->Init.GeneralCallMode | hi2c->Init.NoStretchMode);
  496. /* Enable the selected I2C peripheral */
  497. __HAL_I2C_ENABLE(hi2c);
  498. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  499. hi2c->State = HAL_I2C_STATE_READY;
  500. hi2c->PreviousState = I2C_STATE_NONE;
  501. hi2c->Mode = HAL_I2C_MODE_NONE;
  502. return HAL_OK;
  503. }
  504. /**
  505. * @brief DeInitialize the I2C peripheral.
  506. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  507. * the configuration information for the specified I2C.
  508. * @retval HAL status
  509. */
  510. HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *hi2c)
  511. {
  512. /* Check the I2C handle allocation */
  513. if (hi2c == NULL)
  514. {
  515. return HAL_ERROR;
  516. }
  517. /* Check the parameters */
  518. assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  519. hi2c->State = HAL_I2C_STATE_BUSY;
  520. /* Disable the I2C Peripheral Clock */
  521. __HAL_I2C_DISABLE(hi2c);
  522. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  523. if (hi2c->MspDeInitCallback == NULL)
  524. {
  525. hi2c->MspDeInitCallback = HAL_I2C_MspDeInit; /* Legacy weak MspDeInit */
  526. }
  527. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  528. hi2c->MspDeInitCallback(hi2c);
  529. #else
  530. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  531. HAL_I2C_MspDeInit(hi2c);
  532. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  533. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  534. hi2c->State = HAL_I2C_STATE_RESET;
  535. hi2c->PreviousState = I2C_STATE_NONE;
  536. hi2c->Mode = HAL_I2C_MODE_NONE;
  537. /* Release Lock */
  538. __HAL_UNLOCK(hi2c);
  539. return HAL_OK;
  540. }
  541. /**
  542. * @brief Initialize the I2C MSP.
  543. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  544. * the configuration information for the specified I2C.
  545. * @retval None
  546. */
  547. __weak void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
  548. {
  549. /* Prevent unused argument(s) compilation warning */
  550. UNUSED(hi2c);
  551. /* NOTE : This function should not be modified, when the callback is needed,
  552. the HAL_I2C_MspInit could be implemented in the user file
  553. */
  554. }
  555. /**
  556. * @brief DeInitialize the I2C MSP.
  557. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  558. * the configuration information for the specified I2C.
  559. * @retval None
  560. */
  561. __weak void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c)
  562. {
  563. /* Prevent unused argument(s) compilation warning */
  564. UNUSED(hi2c);
  565. /* NOTE : This function should not be modified, when the callback is needed,
  566. the HAL_I2C_MspDeInit could be implemented in the user file
  567. */
  568. }
  569. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  570. /**
  571. * @brief Register a User I2C Callback
  572. * To be used instead of the weak predefined callback
  573. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  574. * the configuration information for the specified I2C.
  575. * @param CallbackID ID of the callback to be registered
  576. * This parameter can be one of the following values:
  577. * @arg @ref HAL_I2C_MASTER_TX_COMPLETE_CB_ID Master Tx Transfer completed callback ID
  578. * @arg @ref HAL_I2C_MASTER_RX_COMPLETE_CB_ID Master Rx Transfer completed callback ID
  579. * @arg @ref HAL_I2C_SLAVE_TX_COMPLETE_CB_ID Slave Tx Transfer completed callback ID
  580. * @arg @ref HAL_I2C_SLAVE_RX_COMPLETE_CB_ID Slave Rx Transfer completed callback ID
  581. * @arg @ref HAL_I2C_LISTEN_COMPLETE_CB_ID Listen Complete callback ID
  582. * @arg @ref HAL_I2C_MEM_TX_COMPLETE_CB_ID Memory Tx Transfer callback ID
  583. * @arg @ref HAL_I2C_MEM_RX_COMPLETE_CB_ID Memory Rx Transfer completed callback ID
  584. * @arg @ref HAL_I2C_ERROR_CB_ID Error callback ID
  585. * @arg @ref HAL_I2C_ABORT_CB_ID Abort callback ID
  586. * @arg @ref HAL_I2C_MSPINIT_CB_ID MspInit callback ID
  587. * @arg @ref HAL_I2C_MSPDEINIT_CB_ID MspDeInit callback ID
  588. * @param pCallback pointer to the Callback function
  589. * @retval HAL status
  590. */
  591. HAL_StatusTypeDef HAL_I2C_RegisterCallback(I2C_HandleTypeDef *hi2c, HAL_I2C_CallbackIDTypeDef CallbackID, pI2C_CallbackTypeDef pCallback)
  592. {
  593. HAL_StatusTypeDef status = HAL_OK;
  594. if (pCallback == NULL)
  595. {
  596. /* Update the error code */
  597. hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
  598. return HAL_ERROR;
  599. }
  600. /* Process locked */
  601. __HAL_LOCK(hi2c);
  602. if (HAL_I2C_STATE_READY == hi2c->State)
  603. {
  604. switch (CallbackID)
  605. {
  606. case HAL_I2C_MASTER_TX_COMPLETE_CB_ID :
  607. hi2c->MasterTxCpltCallback = pCallback;
  608. break;
  609. case HAL_I2C_MASTER_RX_COMPLETE_CB_ID :
  610. hi2c->MasterRxCpltCallback = pCallback;
  611. break;
  612. case HAL_I2C_SLAVE_TX_COMPLETE_CB_ID :
  613. hi2c->SlaveTxCpltCallback = pCallback;
  614. break;
  615. case HAL_I2C_SLAVE_RX_COMPLETE_CB_ID :
  616. hi2c->SlaveRxCpltCallback = pCallback;
  617. break;
  618. case HAL_I2C_LISTEN_COMPLETE_CB_ID :
  619. hi2c->ListenCpltCallback = pCallback;
  620. break;
  621. case HAL_I2C_MEM_TX_COMPLETE_CB_ID :
  622. hi2c->MemTxCpltCallback = pCallback;
  623. break;
  624. case HAL_I2C_MEM_RX_COMPLETE_CB_ID :
  625. hi2c->MemRxCpltCallback = pCallback;
  626. break;
  627. case HAL_I2C_ERROR_CB_ID :
  628. hi2c->ErrorCallback = pCallback;
  629. break;
  630. case HAL_I2C_ABORT_CB_ID :
  631. hi2c->AbortCpltCallback = pCallback;
  632. break;
  633. case HAL_I2C_MSPINIT_CB_ID :
  634. hi2c->MspInitCallback = pCallback;
  635. break;
  636. case HAL_I2C_MSPDEINIT_CB_ID :
  637. hi2c->MspDeInitCallback = pCallback;
  638. break;
  639. default :
  640. /* Update the error code */
  641. hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
  642. /* Return error status */
  643. status = HAL_ERROR;
  644. break;
  645. }
  646. }
  647. else if (HAL_I2C_STATE_RESET == hi2c->State)
  648. {
  649. switch (CallbackID)
  650. {
  651. case HAL_I2C_MSPINIT_CB_ID :
  652. hi2c->MspInitCallback = pCallback;
  653. break;
  654. case HAL_I2C_MSPDEINIT_CB_ID :
  655. hi2c->MspDeInitCallback = pCallback;
  656. break;
  657. default :
  658. /* Update the error code */
  659. hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
  660. /* Return error status */
  661. status = HAL_ERROR;
  662. break;
  663. }
  664. }
  665. else
  666. {
  667. /* Update the error code */
  668. hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
  669. /* Return error status */
  670. status = HAL_ERROR;
  671. }
  672. /* Release Lock */
  673. __HAL_UNLOCK(hi2c);
  674. return status;
  675. }
  676. /**
  677. * @brief Unregister an I2C Callback
  678. * I2C callback is redirected to the weak predefined callback
  679. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  680. * the configuration information for the specified I2C.
  681. * @param CallbackID ID of the callback to be unregistered
  682. * This parameter can be one of the following values:
  683. * This parameter can be one of the following values:
  684. * @arg @ref HAL_I2C_MASTER_TX_COMPLETE_CB_ID Master Tx Transfer completed callback ID
  685. * @arg @ref HAL_I2C_MASTER_RX_COMPLETE_CB_ID Master Rx Transfer completed callback ID
  686. * @arg @ref HAL_I2C_SLAVE_TX_COMPLETE_CB_ID Slave Tx Transfer completed callback ID
  687. * @arg @ref HAL_I2C_SLAVE_RX_COMPLETE_CB_ID Slave Rx Transfer completed callback ID
  688. * @arg @ref HAL_I2C_LISTEN_COMPLETE_CB_ID Listen Complete callback ID
  689. * @arg @ref HAL_I2C_MEM_TX_COMPLETE_CB_ID Memory Tx Transfer callback ID
  690. * @arg @ref HAL_I2C_MEM_RX_COMPLETE_CB_ID Memory Rx Transfer completed callback ID
  691. * @arg @ref HAL_I2C_ERROR_CB_ID Error callback ID
  692. * @arg @ref HAL_I2C_ABORT_CB_ID Abort callback ID
  693. * @arg @ref HAL_I2C_MSPINIT_CB_ID MspInit callback ID
  694. * @arg @ref HAL_I2C_MSPDEINIT_CB_ID MspDeInit callback ID
  695. * @retval HAL status
  696. */
  697. HAL_StatusTypeDef HAL_I2C_UnRegisterCallback(I2C_HandleTypeDef *hi2c, HAL_I2C_CallbackIDTypeDef CallbackID)
  698. {
  699. HAL_StatusTypeDef status = HAL_OK;
  700. /* Process locked */
  701. __HAL_LOCK(hi2c);
  702. if (HAL_I2C_STATE_READY == hi2c->State)
  703. {
  704. switch (CallbackID)
  705. {
  706. case HAL_I2C_MASTER_TX_COMPLETE_CB_ID :
  707. hi2c->MasterTxCpltCallback = HAL_I2C_MasterTxCpltCallback; /* Legacy weak MasterTxCpltCallback */
  708. break;
  709. case HAL_I2C_MASTER_RX_COMPLETE_CB_ID :
  710. hi2c->MasterRxCpltCallback = HAL_I2C_MasterRxCpltCallback; /* Legacy weak MasterRxCpltCallback */
  711. break;
  712. case HAL_I2C_SLAVE_TX_COMPLETE_CB_ID :
  713. hi2c->SlaveTxCpltCallback = HAL_I2C_SlaveTxCpltCallback; /* Legacy weak SlaveTxCpltCallback */
  714. break;
  715. case HAL_I2C_SLAVE_RX_COMPLETE_CB_ID :
  716. hi2c->SlaveRxCpltCallback = HAL_I2C_SlaveRxCpltCallback; /* Legacy weak SlaveRxCpltCallback */
  717. break;
  718. case HAL_I2C_LISTEN_COMPLETE_CB_ID :
  719. hi2c->ListenCpltCallback = HAL_I2C_ListenCpltCallback; /* Legacy weak ListenCpltCallback */
  720. break;
  721. case HAL_I2C_MEM_TX_COMPLETE_CB_ID :
  722. hi2c->MemTxCpltCallback = HAL_I2C_MemTxCpltCallback; /* Legacy weak MemTxCpltCallback */
  723. break;
  724. case HAL_I2C_MEM_RX_COMPLETE_CB_ID :
  725. hi2c->MemRxCpltCallback = HAL_I2C_MemRxCpltCallback; /* Legacy weak MemRxCpltCallback */
  726. break;
  727. case HAL_I2C_ERROR_CB_ID :
  728. hi2c->ErrorCallback = HAL_I2C_ErrorCallback; /* Legacy weak ErrorCallback */
  729. break;
  730. case HAL_I2C_ABORT_CB_ID :
  731. hi2c->AbortCpltCallback = HAL_I2C_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
  732. break;
  733. case HAL_I2C_MSPINIT_CB_ID :
  734. hi2c->MspInitCallback = HAL_I2C_MspInit; /* Legacy weak MspInit */
  735. break;
  736. case HAL_I2C_MSPDEINIT_CB_ID :
  737. hi2c->MspDeInitCallback = HAL_I2C_MspDeInit; /* Legacy weak MspDeInit */
  738. break;
  739. default :
  740. /* Update the error code */
  741. hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
  742. /* Return error status */
  743. status = HAL_ERROR;
  744. break;
  745. }
  746. }
  747. else if (HAL_I2C_STATE_RESET == hi2c->State)
  748. {
  749. switch (CallbackID)
  750. {
  751. case HAL_I2C_MSPINIT_CB_ID :
  752. hi2c->MspInitCallback = HAL_I2C_MspInit; /* Legacy weak MspInit */
  753. break;
  754. case HAL_I2C_MSPDEINIT_CB_ID :
  755. hi2c->MspDeInitCallback = HAL_I2C_MspDeInit; /* Legacy weak MspDeInit */
  756. break;
  757. default :
  758. /* Update the error code */
  759. hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
  760. /* Return error status */
  761. status = HAL_ERROR;
  762. break;
  763. }
  764. }
  765. else
  766. {
  767. /* Update the error code */
  768. hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
  769. /* Return error status */
  770. status = HAL_ERROR;
  771. }
  772. /* Release Lock */
  773. __HAL_UNLOCK(hi2c);
  774. return status;
  775. }
  776. /**
  777. * @brief Register the Slave Address Match I2C Callback
  778. * To be used instead of the weak HAL_I2C_AddrCallback() predefined callback
  779. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  780. * the configuration information for the specified I2C.
  781. * @param pCallback pointer to the Address Match Callback function
  782. * @retval HAL status
  783. */
  784. HAL_StatusTypeDef HAL_I2C_RegisterAddrCallback(I2C_HandleTypeDef *hi2c, pI2C_AddrCallbackTypeDef pCallback)
  785. {
  786. HAL_StatusTypeDef status = HAL_OK;
  787. if (pCallback == NULL)
  788. {
  789. /* Update the error code */
  790. hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
  791. return HAL_ERROR;
  792. }
  793. /* Process locked */
  794. __HAL_LOCK(hi2c);
  795. if (HAL_I2C_STATE_READY == hi2c->State)
  796. {
  797. hi2c->AddrCallback = pCallback;
  798. }
  799. else
  800. {
  801. /* Update the error code */
  802. hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
  803. /* Return error status */
  804. status = HAL_ERROR;
  805. }
  806. /* Release Lock */
  807. __HAL_UNLOCK(hi2c);
  808. return status;
  809. }
  810. /**
  811. * @brief UnRegister the Slave Address Match I2C Callback
  812. * Info Ready I2C Callback is redirected to the weak HAL_I2C_AddrCallback() predefined callback
  813. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  814. * the configuration information for the specified I2C.
  815. * @retval HAL status
  816. */
  817. HAL_StatusTypeDef HAL_I2C_UnRegisterAddrCallback(I2C_HandleTypeDef *hi2c)
  818. {
  819. HAL_StatusTypeDef status = HAL_OK;
  820. /* Process locked */
  821. __HAL_LOCK(hi2c);
  822. if (HAL_I2C_STATE_READY == hi2c->State)
  823. {
  824. hi2c->AddrCallback = HAL_I2C_AddrCallback; /* Legacy weak AddrCallback */
  825. }
  826. else
  827. {
  828. /* Update the error code */
  829. hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
  830. /* Return error status */
  831. status = HAL_ERROR;
  832. }
  833. /* Release Lock */
  834. __HAL_UNLOCK(hi2c);
  835. return status;
  836. }
  837. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  838. /**
  839. * @}
  840. */
  841. /** @defgroup I2C_Exported_Functions_Group2 Input and Output operation functions
  842. * @brief Data transfers functions
  843. *
  844. @verbatim
  845. ===============================================================================
  846. ##### IO operation functions #####
  847. ===============================================================================
  848. [..]
  849. This subsection provides a set of functions allowing to manage the I2C data
  850. transfers.
  851. (#) There are two modes of transfer:
  852. (++) Blocking mode : The communication is performed in the polling mode.
  853. The status of all data processing is returned by the same function
  854. after finishing transfer.
  855. (++) No-Blocking mode : The communication is performed using Interrupts
  856. or DMA. These functions return the status of the transfer startup.
  857. The end of the data processing will be indicated through the
  858. dedicated I2C IRQ when using Interrupt mode or the DMA IRQ when
  859. using DMA mode.
  860. (#) Blocking mode functions are :
  861. (++) HAL_I2C_Master_Transmit()
  862. (++) HAL_I2C_Master_Receive()
  863. (++) HAL_I2C_Slave_Transmit()
  864. (++) HAL_I2C_Slave_Receive()
  865. (++) HAL_I2C_Mem_Write()
  866. (++) HAL_I2C_Mem_Read()
  867. (++) HAL_I2C_IsDeviceReady()
  868. (#) No-Blocking mode functions with Interrupt are :
  869. (++) HAL_I2C_Master_Transmit_IT()
  870. (++) HAL_I2C_Master_Receive_IT()
  871. (++) HAL_I2C_Slave_Transmit_IT()
  872. (++) HAL_I2C_Slave_Receive_IT()
  873. (++) HAL_I2C_Mem_Write_IT()
  874. (++) HAL_I2C_Mem_Read_IT()
  875. (++) HAL_I2C_Master_Seq_Transmit_IT()
  876. (++) HAL_I2C_Master_Seq_Receive_IT()
  877. (++) HAL_I2C_Slave_Seq_Transmit_IT()
  878. (++) HAL_I2C_Slave_Seq_Receive_IT()
  879. (++) HAL_I2C_EnableListen_IT()
  880. (++) HAL_I2C_DisableListen_IT()
  881. (++) HAL_I2C_Master_Abort_IT()
  882. (#) No-Blocking mode functions with DMA are :
  883. (++) HAL_I2C_Master_Transmit_DMA()
  884. (++) HAL_I2C_Master_Receive_DMA()
  885. (++) HAL_I2C_Slave_Transmit_DMA()
  886. (++) HAL_I2C_Slave_Receive_DMA()
  887. (++) HAL_I2C_Mem_Write_DMA()
  888. (++) HAL_I2C_Mem_Read_DMA()
  889. (++) HAL_I2C_Master_Seq_Transmit_DMA()
  890. (++) HAL_I2C_Master_Seq_Receive_DMA()
  891. (++) HAL_I2C_Slave_Seq_Transmit_DMA()
  892. (++) HAL_I2C_Slave_Seq_Receive_DMA()
  893. (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  894. (++) HAL_I2C_MasterTxCpltCallback()
  895. (++) HAL_I2C_MasterRxCpltCallback()
  896. (++) HAL_I2C_SlaveTxCpltCallback()
  897. (++) HAL_I2C_SlaveRxCpltCallback()
  898. (++) HAL_I2C_MemTxCpltCallback()
  899. (++) HAL_I2C_MemRxCpltCallback()
  900. (++) HAL_I2C_AddrCallback()
  901. (++) HAL_I2C_ListenCpltCallback()
  902. (++) HAL_I2C_ErrorCallback()
  903. (++) HAL_I2C_AbortCpltCallback()
  904. @endverbatim
  905. * @{
  906. */
  907. /**
  908. * @brief Transmits in master mode an amount of data in blocking mode.
  909. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  910. * the configuration information for the specified I2C.
  911. * @param DevAddress Target device address: The device 7 bits address value
  912. * in datasheet must be shifted to the left before calling the interface
  913. * @param pData Pointer to data buffer
  914. * @param Size Amount of data to be sent
  915. * @param Timeout Timeout duration
  916. * @retval HAL status
  917. */
  918. HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  919. {
  920. uint32_t tickstart;
  921. if (hi2c->State == HAL_I2C_STATE_READY)
  922. {
  923. /* Process Locked */
  924. __HAL_LOCK(hi2c);
  925. /* Init tickstart for timeout management*/
  926. tickstart = HAL_GetTick();
  927. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY, tickstart) != HAL_OK)
  928. {
  929. return HAL_ERROR;
  930. }
  931. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  932. hi2c->Mode = HAL_I2C_MODE_MASTER;
  933. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  934. /* Prepare transfer parameters */
  935. hi2c->pBuffPtr = pData;
  936. hi2c->XferCount = Size;
  937. hi2c->XferISR = NULL;
  938. /* Send Slave Address */
  939. /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  940. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  941. {
  942. hi2c->XferSize = MAX_NBYTE_SIZE;
  943. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
  944. }
  945. else
  946. {
  947. hi2c->XferSize = hi2c->XferCount;
  948. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_WRITE);
  949. }
  950. while (hi2c->XferCount > 0U)
  951. {
  952. /* Wait until TXIS flag is set */
  953. if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  954. {
  955. return HAL_ERROR;
  956. }
  957. /* Write data to TXDR */
  958. hi2c->Instance->TXDR = *hi2c->pBuffPtr;
  959. /* Increment Buffer pointer */
  960. hi2c->pBuffPtr++;
  961. hi2c->XferCount--;
  962. hi2c->XferSize--;
  963. if ((hi2c->XferCount != 0U) && (hi2c->XferSize == 0U))
  964. {
  965. /* Wait until TCR flag is set */
  966. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK)
  967. {
  968. return HAL_ERROR;
  969. }
  970. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  971. {
  972. hi2c->XferSize = MAX_NBYTE_SIZE;
  973. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  974. }
  975. else
  976. {
  977. hi2c->XferSize = hi2c->XferCount;
  978. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  979. }
  980. }
  981. }
  982. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  983. /* Wait until STOPF flag is set */
  984. if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  985. {
  986. return HAL_ERROR;
  987. }
  988. /* Clear STOP Flag */
  989. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  990. /* Clear Configuration Register 2 */
  991. I2C_RESET_CR2(hi2c);
  992. hi2c->State = HAL_I2C_STATE_READY;
  993. hi2c->Mode = HAL_I2C_MODE_NONE;
  994. /* Process Unlocked */
  995. __HAL_UNLOCK(hi2c);
  996. return HAL_OK;
  997. }
  998. else
  999. {
  1000. return HAL_BUSY;
  1001. }
  1002. }
  1003. /**
  1004. * @brief Receives in master mode an amount of data in blocking mode.
  1005. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1006. * the configuration information for the specified I2C.
  1007. * @param DevAddress Target device address: The device 7 bits address value
  1008. * in datasheet must be shifted to the left before calling the interface
  1009. * @param pData Pointer to data buffer
  1010. * @param Size Amount of data to be sent
  1011. * @param Timeout Timeout duration
  1012. * @retval HAL status
  1013. */
  1014. HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  1015. {
  1016. uint32_t tickstart;
  1017. if (hi2c->State == HAL_I2C_STATE_READY)
  1018. {
  1019. /* Process Locked */
  1020. __HAL_LOCK(hi2c);
  1021. /* Init tickstart for timeout management*/
  1022. tickstart = HAL_GetTick();
  1023. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY, tickstart) != HAL_OK)
  1024. {
  1025. return HAL_ERROR;
  1026. }
  1027. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  1028. hi2c->Mode = HAL_I2C_MODE_MASTER;
  1029. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1030. /* Prepare transfer parameters */
  1031. hi2c->pBuffPtr = pData;
  1032. hi2c->XferCount = Size;
  1033. hi2c->XferISR = NULL;
  1034. /* Send Slave Address */
  1035. /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  1036. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  1037. {
  1038. hi2c->XferSize = MAX_NBYTE_SIZE;
  1039. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
  1040. }
  1041. else
  1042. {
  1043. hi2c->XferSize = hi2c->XferCount;
  1044. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
  1045. }
  1046. while (hi2c->XferCount > 0U)
  1047. {
  1048. /* Wait until RXNE flag is set */
  1049. if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  1050. {
  1051. return HAL_ERROR;
  1052. }
  1053. /* Read data from RXDR */
  1054. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
  1055. /* Increment Buffer pointer */
  1056. hi2c->pBuffPtr++;
  1057. hi2c->XferSize--;
  1058. hi2c->XferCount--;
  1059. if ((hi2c->XferCount != 0U) && (hi2c->XferSize == 0U))
  1060. {
  1061. /* Wait until TCR flag is set */
  1062. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK)
  1063. {
  1064. return HAL_ERROR;
  1065. }
  1066. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  1067. {
  1068. hi2c->XferSize = MAX_NBYTE_SIZE;
  1069. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  1070. }
  1071. else
  1072. {
  1073. hi2c->XferSize = hi2c->XferCount;
  1074. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  1075. }
  1076. }
  1077. }
  1078. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  1079. /* Wait until STOPF flag is set */
  1080. if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  1081. {
  1082. return HAL_ERROR;
  1083. }
  1084. /* Clear STOP Flag */
  1085. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  1086. /* Clear Configuration Register 2 */
  1087. I2C_RESET_CR2(hi2c);
  1088. hi2c->State = HAL_I2C_STATE_READY;
  1089. hi2c->Mode = HAL_I2C_MODE_NONE;
  1090. /* Process Unlocked */
  1091. __HAL_UNLOCK(hi2c);
  1092. return HAL_OK;
  1093. }
  1094. else
  1095. {
  1096. return HAL_BUSY;
  1097. }
  1098. }
  1099. /**
  1100. * @brief Transmits in slave mode an amount of data in blocking mode.
  1101. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1102. * the configuration information for the specified I2C.
  1103. * @param pData Pointer to data buffer
  1104. * @param Size Amount of data to be sent
  1105. * @param Timeout Timeout duration
  1106. * @retval HAL status
  1107. */
  1108. HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  1109. {
  1110. uint32_t tickstart;
  1111. if (hi2c->State == HAL_I2C_STATE_READY)
  1112. {
  1113. if ((pData == NULL) || (Size == 0U))
  1114. {
  1115. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  1116. return HAL_ERROR;
  1117. }
  1118. /* Process Locked */
  1119. __HAL_LOCK(hi2c);
  1120. /* Init tickstart for timeout management*/
  1121. tickstart = HAL_GetTick();
  1122. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  1123. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  1124. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1125. /* Prepare transfer parameters */
  1126. hi2c->pBuffPtr = pData;
  1127. hi2c->XferCount = Size;
  1128. hi2c->XferISR = NULL;
  1129. /* Enable Address Acknowledge */
  1130. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  1131. /* Wait until ADDR flag is set */
  1132. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
  1133. {
  1134. /* Disable Address Acknowledge */
  1135. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1136. return HAL_ERROR;
  1137. }
  1138. /* Clear ADDR flag */
  1139. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  1140. /* If 10bit addressing mode is selected */
  1141. if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
  1142. {
  1143. /* Wait until ADDR flag is set */
  1144. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
  1145. {
  1146. /* Disable Address Acknowledge */
  1147. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1148. return HAL_ERROR;
  1149. }
  1150. /* Clear ADDR flag */
  1151. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  1152. }
  1153. /* Wait until DIR flag is set Transmitter mode */
  1154. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, RESET, Timeout, tickstart) != HAL_OK)
  1155. {
  1156. /* Disable Address Acknowledge */
  1157. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1158. return HAL_ERROR;
  1159. }
  1160. while (hi2c->XferCount > 0U)
  1161. {
  1162. /* Wait until TXIS flag is set */
  1163. if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  1164. {
  1165. /* Disable Address Acknowledge */
  1166. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1167. return HAL_ERROR;
  1168. }
  1169. /* Write data to TXDR */
  1170. hi2c->Instance->TXDR = *hi2c->pBuffPtr;
  1171. /* Increment Buffer pointer */
  1172. hi2c->pBuffPtr++;
  1173. hi2c->XferCount--;
  1174. }
  1175. /* Wait until STOP flag is set */
  1176. if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  1177. {
  1178. /* Disable Address Acknowledge */
  1179. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1180. if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1181. {
  1182. /* Normal use case for Transmitter mode */
  1183. /* A NACK is generated to confirm the end of transfer */
  1184. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1185. }
  1186. else
  1187. {
  1188. return HAL_ERROR;
  1189. }
  1190. }
  1191. /* Clear STOP flag */
  1192. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  1193. /* Wait until BUSY flag is reset */
  1194. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout, tickstart) != HAL_OK)
  1195. {
  1196. /* Disable Address Acknowledge */
  1197. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1198. return HAL_ERROR;
  1199. }
  1200. /* Disable Address Acknowledge */
  1201. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1202. hi2c->State = HAL_I2C_STATE_READY;
  1203. hi2c->Mode = HAL_I2C_MODE_NONE;
  1204. /* Process Unlocked */
  1205. __HAL_UNLOCK(hi2c);
  1206. return HAL_OK;
  1207. }
  1208. else
  1209. {
  1210. return HAL_BUSY;
  1211. }
  1212. }
  1213. /**
  1214. * @brief Receive in slave mode an amount of data in blocking mode
  1215. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1216. * the configuration information for the specified I2C.
  1217. * @param pData Pointer to data buffer
  1218. * @param Size Amount of data to be sent
  1219. * @param Timeout Timeout duration
  1220. * @retval HAL status
  1221. */
  1222. HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  1223. {
  1224. uint32_t tickstart;
  1225. if (hi2c->State == HAL_I2C_STATE_READY)
  1226. {
  1227. if ((pData == NULL) || (Size == 0U))
  1228. {
  1229. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  1230. return HAL_ERROR;
  1231. }
  1232. /* Process Locked */
  1233. __HAL_LOCK(hi2c);
  1234. /* Init tickstart for timeout management*/
  1235. tickstart = HAL_GetTick();
  1236. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  1237. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  1238. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1239. /* Prepare transfer parameters */
  1240. hi2c->pBuffPtr = pData;
  1241. hi2c->XferCount = Size;
  1242. hi2c->XferISR = NULL;
  1243. /* Enable Address Acknowledge */
  1244. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  1245. /* Wait until ADDR flag is set */
  1246. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
  1247. {
  1248. /* Disable Address Acknowledge */
  1249. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1250. return HAL_ERROR;
  1251. }
  1252. /* Clear ADDR flag */
  1253. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  1254. /* Wait until DIR flag is reset Receiver mode */
  1255. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, SET, Timeout, tickstart) != HAL_OK)
  1256. {
  1257. /* Disable Address Acknowledge */
  1258. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1259. return HAL_ERROR;
  1260. }
  1261. while (hi2c->XferCount > 0U)
  1262. {
  1263. /* Wait until RXNE flag is set */
  1264. if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  1265. {
  1266. /* Disable Address Acknowledge */
  1267. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1268. /* Store Last receive data if any */
  1269. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
  1270. {
  1271. /* Read data from RXDR */
  1272. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
  1273. /* Increment Buffer pointer */
  1274. hi2c->pBuffPtr++;
  1275. hi2c->XferCount--;
  1276. }
  1277. return HAL_ERROR;
  1278. }
  1279. /* Read data from RXDR */
  1280. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
  1281. /* Increment Buffer pointer */
  1282. hi2c->pBuffPtr++;
  1283. hi2c->XferCount--;
  1284. }
  1285. /* Wait until STOP flag is set */
  1286. if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  1287. {
  1288. /* Disable Address Acknowledge */
  1289. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1290. return HAL_ERROR;
  1291. }
  1292. /* Clear STOP flag */
  1293. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  1294. /* Wait until BUSY flag is reset */
  1295. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout, tickstart) != HAL_OK)
  1296. {
  1297. /* Disable Address Acknowledge */
  1298. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1299. return HAL_ERROR;
  1300. }
  1301. /* Disable Address Acknowledge */
  1302. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1303. hi2c->State = HAL_I2C_STATE_READY;
  1304. hi2c->Mode = HAL_I2C_MODE_NONE;
  1305. /* Process Unlocked */
  1306. __HAL_UNLOCK(hi2c);
  1307. return HAL_OK;
  1308. }
  1309. else
  1310. {
  1311. return HAL_BUSY;
  1312. }
  1313. }
  1314. /**
  1315. * @brief Transmit in master mode an amount of data in non-blocking mode with Interrupt
  1316. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1317. * the configuration information for the specified I2C.
  1318. * @param DevAddress Target device address: The device 7 bits address value
  1319. * in datasheet must be shifted to the left before calling the interface
  1320. * @param pData Pointer to data buffer
  1321. * @param Size Amount of data to be sent
  1322. * @retval HAL status
  1323. */
  1324. HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  1325. {
  1326. uint32_t xfermode;
  1327. if (hi2c->State == HAL_I2C_STATE_READY)
  1328. {
  1329. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1330. {
  1331. return HAL_BUSY;
  1332. }
  1333. /* Process Locked */
  1334. __HAL_LOCK(hi2c);
  1335. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  1336. hi2c->Mode = HAL_I2C_MODE_MASTER;
  1337. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1338. /* Prepare transfer parameters */
  1339. hi2c->pBuffPtr = pData;
  1340. hi2c->XferCount = Size;
  1341. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1342. hi2c->XferISR = I2C_Master_ISR_IT;
  1343. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  1344. {
  1345. hi2c->XferSize = MAX_NBYTE_SIZE;
  1346. xfermode = I2C_RELOAD_MODE;
  1347. }
  1348. else
  1349. {
  1350. hi2c->XferSize = hi2c->XferCount;
  1351. xfermode = I2C_AUTOEND_MODE;
  1352. }
  1353. /* Send Slave Address */
  1354. /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE */
  1355. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, I2C_GENERATE_START_WRITE);
  1356. /* Process Unlocked */
  1357. __HAL_UNLOCK(hi2c);
  1358. /* Note : The I2C interrupts must be enabled after unlocking current process
  1359. to avoid the risk of I2C interrupt handle execution before current
  1360. process unlock */
  1361. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  1362. /* possible to enable all of these */
  1363. /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  1364. I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
  1365. return HAL_OK;
  1366. }
  1367. else
  1368. {
  1369. return HAL_BUSY;
  1370. }
  1371. }
  1372. /**
  1373. * @brief Receive in master mode an amount of data in non-blocking mode with Interrupt
  1374. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1375. * the configuration information for the specified I2C.
  1376. * @param DevAddress Target device address: The device 7 bits address value
  1377. * in datasheet must be shifted to the left before calling the interface
  1378. * @param pData Pointer to data buffer
  1379. * @param Size Amount of data to be sent
  1380. * @retval HAL status
  1381. */
  1382. HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  1383. {
  1384. uint32_t xfermode;
  1385. if (hi2c->State == HAL_I2C_STATE_READY)
  1386. {
  1387. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1388. {
  1389. return HAL_BUSY;
  1390. }
  1391. /* Process Locked */
  1392. __HAL_LOCK(hi2c);
  1393. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  1394. hi2c->Mode = HAL_I2C_MODE_MASTER;
  1395. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1396. /* Prepare transfer parameters */
  1397. hi2c->pBuffPtr = pData;
  1398. hi2c->XferCount = Size;
  1399. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1400. hi2c->XferISR = I2C_Master_ISR_IT;
  1401. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  1402. {
  1403. hi2c->XferSize = MAX_NBYTE_SIZE;
  1404. xfermode = I2C_RELOAD_MODE;
  1405. }
  1406. else
  1407. {
  1408. hi2c->XferSize = hi2c->XferCount;
  1409. xfermode = I2C_AUTOEND_MODE;
  1410. }
  1411. /* Send Slave Address */
  1412. /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE */
  1413. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, I2C_GENERATE_START_READ);
  1414. /* Process Unlocked */
  1415. __HAL_UNLOCK(hi2c);
  1416. /* Note : The I2C interrupts must be enabled after unlocking current process
  1417. to avoid the risk of I2C interrupt handle execution before current
  1418. process unlock */
  1419. /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  1420. /* possible to enable all of these */
  1421. /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  1422. I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT);
  1423. return HAL_OK;
  1424. }
  1425. else
  1426. {
  1427. return HAL_BUSY;
  1428. }
  1429. }
  1430. /**
  1431. * @brief Transmit in slave mode an amount of data in non-blocking mode with Interrupt
  1432. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1433. * the configuration information for the specified I2C.
  1434. * @param pData Pointer to data buffer
  1435. * @param Size Amount of data to be sent
  1436. * @retval HAL status
  1437. */
  1438. HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  1439. {
  1440. if (hi2c->State == HAL_I2C_STATE_READY)
  1441. {
  1442. /* Process Locked */
  1443. __HAL_LOCK(hi2c);
  1444. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  1445. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  1446. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1447. /* Enable Address Acknowledge */
  1448. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  1449. /* Prepare transfer parameters */
  1450. hi2c->pBuffPtr = pData;
  1451. hi2c->XferCount = Size;
  1452. hi2c->XferSize = hi2c->XferCount;
  1453. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1454. hi2c->XferISR = I2C_Slave_ISR_IT;
  1455. /* Process Unlocked */
  1456. __HAL_UNLOCK(hi2c);
  1457. /* Note : The I2C interrupts must be enabled after unlocking current process
  1458. to avoid the risk of I2C interrupt handle execution before current
  1459. process unlock */
  1460. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  1461. /* possible to enable all of these */
  1462. /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  1463. I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT | I2C_XFER_LISTEN_IT);
  1464. return HAL_OK;
  1465. }
  1466. else
  1467. {
  1468. return HAL_BUSY;
  1469. }
  1470. }
  1471. /**
  1472. * @brief Receive in slave mode an amount of data in non-blocking mode with Interrupt
  1473. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1474. * the configuration information for the specified I2C.
  1475. * @param pData Pointer to data buffer
  1476. * @param Size Amount of data to be sent
  1477. * @retval HAL status
  1478. */
  1479. HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  1480. {
  1481. if (hi2c->State == HAL_I2C_STATE_READY)
  1482. {
  1483. /* Process Locked */
  1484. __HAL_LOCK(hi2c);
  1485. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  1486. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  1487. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1488. /* Enable Address Acknowledge */
  1489. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  1490. /* Prepare transfer parameters */
  1491. hi2c->pBuffPtr = pData;
  1492. hi2c->XferCount = Size;
  1493. hi2c->XferSize = hi2c->XferCount;
  1494. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1495. hi2c->XferISR = I2C_Slave_ISR_IT;
  1496. /* Process Unlocked */
  1497. __HAL_UNLOCK(hi2c);
  1498. /* Note : The I2C interrupts must be enabled after unlocking current process
  1499. to avoid the risk of I2C interrupt handle execution before current
  1500. process unlock */
  1501. /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  1502. /* possible to enable all of these */
  1503. /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  1504. I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT | I2C_XFER_LISTEN_IT);
  1505. return HAL_OK;
  1506. }
  1507. else
  1508. {
  1509. return HAL_BUSY;
  1510. }
  1511. }
  1512. /**
  1513. * @brief Transmit in master mode an amount of data in non-blocking mode with DMA
  1514. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1515. * the configuration information for the specified I2C.
  1516. * @param DevAddress Target device address: The device 7 bits address value
  1517. * in datasheet must be shifted to the left before calling the interface
  1518. * @param pData Pointer to data buffer
  1519. * @param Size Amount of data to be sent
  1520. * @retval HAL status
  1521. */
  1522. HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  1523. {
  1524. uint32_t xfermode;
  1525. HAL_StatusTypeDef dmaxferstatus;
  1526. if (hi2c->State == HAL_I2C_STATE_READY)
  1527. {
  1528. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1529. {
  1530. return HAL_BUSY;
  1531. }
  1532. /* Process Locked */
  1533. __HAL_LOCK(hi2c);
  1534. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  1535. hi2c->Mode = HAL_I2C_MODE_MASTER;
  1536. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1537. /* Prepare transfer parameters */
  1538. hi2c->pBuffPtr = pData;
  1539. hi2c->XferCount = Size;
  1540. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1541. hi2c->XferISR = I2C_Master_ISR_DMA;
  1542. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  1543. {
  1544. hi2c->XferSize = MAX_NBYTE_SIZE;
  1545. xfermode = I2C_RELOAD_MODE;
  1546. }
  1547. else
  1548. {
  1549. hi2c->XferSize = hi2c->XferCount;
  1550. xfermode = I2C_AUTOEND_MODE;
  1551. }
  1552. if (hi2c->XferSize > 0U)
  1553. {
  1554. if (hi2c->hdmatx != NULL)
  1555. {
  1556. /* Set the I2C DMA transfer complete callback */
  1557. hi2c->hdmatx->XferCpltCallback = I2C_DMAMasterTransmitCplt;
  1558. /* Set the DMA error callback */
  1559. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  1560. /* Set the unused DMA callbacks to NULL */
  1561. hi2c->hdmatx->XferHalfCpltCallback = NULL;
  1562. hi2c->hdmatx->XferAbortCallback = NULL;
  1563. /* Enable the DMA channel */
  1564. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
  1565. }
  1566. else
  1567. {
  1568. /* Update I2C state */
  1569. hi2c->State = HAL_I2C_STATE_READY;
  1570. hi2c->Mode = HAL_I2C_MODE_NONE;
  1571. /* Update I2C error code */
  1572. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  1573. /* Process Unlocked */
  1574. __HAL_UNLOCK(hi2c);
  1575. return HAL_ERROR;
  1576. }
  1577. if (dmaxferstatus == HAL_OK)
  1578. {
  1579. /* Send Slave Address */
  1580. /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  1581. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, I2C_GENERATE_START_WRITE);
  1582. /* Update XferCount value */
  1583. hi2c->XferCount -= hi2c->XferSize;
  1584. /* Process Unlocked */
  1585. __HAL_UNLOCK(hi2c);
  1586. /* Note : The I2C interrupts must be enabled after unlocking current process
  1587. to avoid the risk of I2C interrupt handle execution before current
  1588. process unlock */
  1589. /* Enable ERR and NACK interrupts */
  1590. I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT);
  1591. /* Enable DMA Request */
  1592. hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  1593. }
  1594. else
  1595. {
  1596. /* Update I2C state */
  1597. hi2c->State = HAL_I2C_STATE_READY;
  1598. hi2c->Mode = HAL_I2C_MODE_NONE;
  1599. /* Update I2C error code */
  1600. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  1601. /* Process Unlocked */
  1602. __HAL_UNLOCK(hi2c);
  1603. return HAL_ERROR;
  1604. }
  1605. }
  1606. else
  1607. {
  1608. /* Update Transfer ISR function pointer */
  1609. hi2c->XferISR = I2C_Master_ISR_IT;
  1610. /* Send Slave Address */
  1611. /* Set NBYTES to write and generate START condition */
  1612. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_WRITE);
  1613. /* Process Unlocked */
  1614. __HAL_UNLOCK(hi2c);
  1615. /* Note : The I2C interrupts must be enabled after unlocking current process
  1616. to avoid the risk of I2C interrupt handle execution before current
  1617. process unlock */
  1618. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  1619. /* possible to enable all of these */
  1620. /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  1621. I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
  1622. }
  1623. return HAL_OK;
  1624. }
  1625. else
  1626. {
  1627. return HAL_BUSY;
  1628. }
  1629. }
  1630. /**
  1631. * @brief Receive in master mode an amount of data in non-blocking mode with DMA
  1632. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1633. * the configuration information for the specified I2C.
  1634. * @param DevAddress Target device address: The device 7 bits address value
  1635. * in datasheet must be shifted to the left before calling the interface
  1636. * @param pData Pointer to data buffer
  1637. * @param Size Amount of data to be sent
  1638. * @retval HAL status
  1639. */
  1640. HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  1641. {
  1642. uint32_t xfermode;
  1643. HAL_StatusTypeDef dmaxferstatus;
  1644. if (hi2c->State == HAL_I2C_STATE_READY)
  1645. {
  1646. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1647. {
  1648. return HAL_BUSY;
  1649. }
  1650. /* Process Locked */
  1651. __HAL_LOCK(hi2c);
  1652. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  1653. hi2c->Mode = HAL_I2C_MODE_MASTER;
  1654. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1655. /* Prepare transfer parameters */
  1656. hi2c->pBuffPtr = pData;
  1657. hi2c->XferCount = Size;
  1658. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1659. hi2c->XferISR = I2C_Master_ISR_DMA;
  1660. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  1661. {
  1662. hi2c->XferSize = MAX_NBYTE_SIZE;
  1663. xfermode = I2C_RELOAD_MODE;
  1664. }
  1665. else
  1666. {
  1667. hi2c->XferSize = hi2c->XferCount;
  1668. xfermode = I2C_AUTOEND_MODE;
  1669. }
  1670. if (hi2c->XferSize > 0U)
  1671. {
  1672. if (hi2c->hdmarx != NULL)
  1673. {
  1674. /* Set the I2C DMA transfer complete callback */
  1675. hi2c->hdmarx->XferCpltCallback = I2C_DMAMasterReceiveCplt;
  1676. /* Set the DMA error callback */
  1677. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  1678. /* Set the unused DMA callbacks to NULL */
  1679. hi2c->hdmarx->XferHalfCpltCallback = NULL;
  1680. hi2c->hdmarx->XferAbortCallback = NULL;
  1681. /* Enable the DMA channel */
  1682. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, hi2c->XferSize);
  1683. }
  1684. else
  1685. {
  1686. /* Update I2C state */
  1687. hi2c->State = HAL_I2C_STATE_READY;
  1688. hi2c->Mode = HAL_I2C_MODE_NONE;
  1689. /* Update I2C error code */
  1690. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  1691. /* Process Unlocked */
  1692. __HAL_UNLOCK(hi2c);
  1693. return HAL_ERROR;
  1694. }
  1695. if (dmaxferstatus == HAL_OK)
  1696. {
  1697. /* Send Slave Address */
  1698. /* Set NBYTES to read and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  1699. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, I2C_GENERATE_START_READ);
  1700. /* Update XferCount value */
  1701. hi2c->XferCount -= hi2c->XferSize;
  1702. /* Process Unlocked */
  1703. __HAL_UNLOCK(hi2c);
  1704. /* Note : The I2C interrupts must be enabled after unlocking current process
  1705. to avoid the risk of I2C interrupt handle execution before current
  1706. process unlock */
  1707. /* Enable ERR and NACK interrupts */
  1708. I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT);
  1709. /* Enable DMA Request */
  1710. hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  1711. }
  1712. else
  1713. {
  1714. /* Update I2C state */
  1715. hi2c->State = HAL_I2C_STATE_READY;
  1716. hi2c->Mode = HAL_I2C_MODE_NONE;
  1717. /* Update I2C error code */
  1718. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  1719. /* Process Unlocked */
  1720. __HAL_UNLOCK(hi2c);
  1721. return HAL_ERROR;
  1722. }
  1723. }
  1724. else
  1725. {
  1726. /* Update Transfer ISR function pointer */
  1727. hi2c->XferISR = I2C_Master_ISR_IT;
  1728. /* Send Slave Address */
  1729. /* Set NBYTES to read and generate START condition */
  1730. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
  1731. /* Process Unlocked */
  1732. __HAL_UNLOCK(hi2c);
  1733. /* Note : The I2C interrupts must be enabled after unlocking current process
  1734. to avoid the risk of I2C interrupt handle execution before current
  1735. process unlock */
  1736. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  1737. /* possible to enable all of these */
  1738. /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  1739. I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
  1740. }
  1741. return HAL_OK;
  1742. }
  1743. else
  1744. {
  1745. return HAL_BUSY;
  1746. }
  1747. }
  1748. /**
  1749. * @brief Transmit in slave mode an amount of data in non-blocking mode with DMA
  1750. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1751. * the configuration information for the specified I2C.
  1752. * @param pData Pointer to data buffer
  1753. * @param Size Amount of data to be sent
  1754. * @retval HAL status
  1755. */
  1756. HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  1757. {
  1758. HAL_StatusTypeDef dmaxferstatus;
  1759. if (hi2c->State == HAL_I2C_STATE_READY)
  1760. {
  1761. if ((pData == NULL) || (Size == 0U))
  1762. {
  1763. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  1764. return HAL_ERROR;
  1765. }
  1766. /* Process Locked */
  1767. __HAL_LOCK(hi2c);
  1768. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  1769. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  1770. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1771. /* Prepare transfer parameters */
  1772. hi2c->pBuffPtr = pData;
  1773. hi2c->XferCount = Size;
  1774. hi2c->XferSize = hi2c->XferCount;
  1775. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1776. hi2c->XferISR = I2C_Slave_ISR_DMA;
  1777. if (hi2c->hdmatx != NULL)
  1778. {
  1779. /* Set the I2C DMA transfer complete callback */
  1780. hi2c->hdmatx->XferCpltCallback = I2C_DMASlaveTransmitCplt;
  1781. /* Set the DMA error callback */
  1782. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  1783. /* Set the unused DMA callbacks to NULL */
  1784. hi2c->hdmatx->XferHalfCpltCallback = NULL;
  1785. hi2c->hdmatx->XferAbortCallback = NULL;
  1786. /* Enable the DMA channel */
  1787. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
  1788. }
  1789. else
  1790. {
  1791. /* Update I2C state */
  1792. hi2c->State = HAL_I2C_STATE_LISTEN;
  1793. hi2c->Mode = HAL_I2C_MODE_NONE;
  1794. /* Update I2C error code */
  1795. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  1796. /* Process Unlocked */
  1797. __HAL_UNLOCK(hi2c);
  1798. return HAL_ERROR;
  1799. }
  1800. if (dmaxferstatus == HAL_OK)
  1801. {
  1802. /* Enable Address Acknowledge */
  1803. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  1804. /* Process Unlocked */
  1805. __HAL_UNLOCK(hi2c);
  1806. /* Note : The I2C interrupts must be enabled after unlocking current process
  1807. to avoid the risk of I2C interrupt handle execution before current
  1808. process unlock */
  1809. /* Enable ERR, STOP, NACK, ADDR interrupts */
  1810. I2C_Enable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
  1811. /* Enable DMA Request */
  1812. hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  1813. }
  1814. else
  1815. {
  1816. /* Update I2C state */
  1817. hi2c->State = HAL_I2C_STATE_LISTEN;
  1818. hi2c->Mode = HAL_I2C_MODE_NONE;
  1819. /* Update I2C error code */
  1820. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  1821. /* Process Unlocked */
  1822. __HAL_UNLOCK(hi2c);
  1823. return HAL_ERROR;
  1824. }
  1825. return HAL_OK;
  1826. }
  1827. else
  1828. {
  1829. return HAL_BUSY;
  1830. }
  1831. }
  1832. /**
  1833. * @brief Receive in slave mode an amount of data in non-blocking mode with DMA
  1834. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1835. * the configuration information for the specified I2C.
  1836. * @param pData Pointer to data buffer
  1837. * @param Size Amount of data to be sent
  1838. * @retval HAL status
  1839. */
  1840. HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  1841. {
  1842. HAL_StatusTypeDef dmaxferstatus;
  1843. if (hi2c->State == HAL_I2C_STATE_READY)
  1844. {
  1845. if ((pData == NULL) || (Size == 0U))
  1846. {
  1847. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  1848. return HAL_ERROR;
  1849. }
  1850. /* Process Locked */
  1851. __HAL_LOCK(hi2c);
  1852. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  1853. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  1854. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1855. /* Prepare transfer parameters */
  1856. hi2c->pBuffPtr = pData;
  1857. hi2c->XferCount = Size;
  1858. hi2c->XferSize = hi2c->XferCount;
  1859. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1860. hi2c->XferISR = I2C_Slave_ISR_DMA;
  1861. if (hi2c->hdmarx != NULL)
  1862. {
  1863. /* Set the I2C DMA transfer complete callback */
  1864. hi2c->hdmarx->XferCpltCallback = I2C_DMASlaveReceiveCplt;
  1865. /* Set the DMA error callback */
  1866. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  1867. /* Set the unused DMA callbacks to NULL */
  1868. hi2c->hdmarx->XferHalfCpltCallback = NULL;
  1869. hi2c->hdmarx->XferAbortCallback = NULL;
  1870. /* Enable the DMA channel */
  1871. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, hi2c->XferSize);
  1872. }
  1873. else
  1874. {
  1875. /* Update I2C state */
  1876. hi2c->State = HAL_I2C_STATE_LISTEN;
  1877. hi2c->Mode = HAL_I2C_MODE_NONE;
  1878. /* Update I2C error code */
  1879. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  1880. /* Process Unlocked */
  1881. __HAL_UNLOCK(hi2c);
  1882. return HAL_ERROR;
  1883. }
  1884. if (dmaxferstatus == HAL_OK)
  1885. {
  1886. /* Enable Address Acknowledge */
  1887. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  1888. /* Process Unlocked */
  1889. __HAL_UNLOCK(hi2c);
  1890. /* Note : The I2C interrupts must be enabled after unlocking current process
  1891. to avoid the risk of I2C interrupt handle execution before current
  1892. process unlock */
  1893. /* Enable ERR, STOP, NACK, ADDR interrupts */
  1894. I2C_Enable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
  1895. /* Enable DMA Request */
  1896. hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  1897. }
  1898. else
  1899. {
  1900. /* Update I2C state */
  1901. hi2c->State = HAL_I2C_STATE_LISTEN;
  1902. hi2c->Mode = HAL_I2C_MODE_NONE;
  1903. /* Update I2C error code */
  1904. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  1905. /* Process Unlocked */
  1906. __HAL_UNLOCK(hi2c);
  1907. return HAL_ERROR;
  1908. }
  1909. return HAL_OK;
  1910. }
  1911. else
  1912. {
  1913. return HAL_BUSY;
  1914. }
  1915. }
  1916. /**
  1917. * @brief Write an amount of data in blocking mode to a specific memory address
  1918. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1919. * the configuration information for the specified I2C.
  1920. * @param DevAddress Target device address: The device 7 bits address value
  1921. * in datasheet must be shifted to the left before calling the interface
  1922. * @param MemAddress Internal memory address
  1923. * @param MemAddSize Size of internal memory address
  1924. * @param pData Pointer to data buffer
  1925. * @param Size Amount of data to be sent
  1926. * @param Timeout Timeout duration
  1927. * @retval HAL status
  1928. */
  1929. HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  1930. {
  1931. uint32_t tickstart;
  1932. /* Check the parameters */
  1933. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  1934. if (hi2c->State == HAL_I2C_STATE_READY)
  1935. {
  1936. if ((pData == NULL) || (Size == 0U))
  1937. {
  1938. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  1939. return HAL_ERROR;
  1940. }
  1941. /* Process Locked */
  1942. __HAL_LOCK(hi2c);
  1943. /* Init tickstart for timeout management*/
  1944. tickstart = HAL_GetTick();
  1945. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY, tickstart) != HAL_OK)
  1946. {
  1947. return HAL_ERROR;
  1948. }
  1949. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  1950. hi2c->Mode = HAL_I2C_MODE_MEM;
  1951. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1952. /* Prepare transfer parameters */
  1953. hi2c->pBuffPtr = pData;
  1954. hi2c->XferCount = Size;
  1955. hi2c->XferISR = NULL;
  1956. /* Send Slave Address and Memory Address */
  1957. if (I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)
  1958. {
  1959. /* Process Unlocked */
  1960. __HAL_UNLOCK(hi2c);
  1961. return HAL_ERROR;
  1962. }
  1963. /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE */
  1964. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  1965. {
  1966. hi2c->XferSize = MAX_NBYTE_SIZE;
  1967. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  1968. }
  1969. else
  1970. {
  1971. hi2c->XferSize = hi2c->XferCount;
  1972. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  1973. }
  1974. do
  1975. {
  1976. /* Wait until TXIS flag is set */
  1977. if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  1978. {
  1979. return HAL_ERROR;
  1980. }
  1981. /* Write data to TXDR */
  1982. hi2c->Instance->TXDR = *hi2c->pBuffPtr;
  1983. /* Increment Buffer pointer */
  1984. hi2c->pBuffPtr++;
  1985. hi2c->XferCount--;
  1986. hi2c->XferSize--;
  1987. if ((hi2c->XferCount != 0U) && (hi2c->XferSize == 0U))
  1988. {
  1989. /* Wait until TCR flag is set */
  1990. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK)
  1991. {
  1992. return HAL_ERROR;
  1993. }
  1994. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  1995. {
  1996. hi2c->XferSize = MAX_NBYTE_SIZE;
  1997. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  1998. }
  1999. else
  2000. {
  2001. hi2c->XferSize = hi2c->XferCount;
  2002. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  2003. }
  2004. }
  2005. }
  2006. while (hi2c->XferCount > 0U);
  2007. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2008. /* Wait until STOPF flag is reset */
  2009. if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  2010. {
  2011. return HAL_ERROR;
  2012. }
  2013. /* Clear STOP Flag */
  2014. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2015. /* Clear Configuration Register 2 */
  2016. I2C_RESET_CR2(hi2c);
  2017. hi2c->State = HAL_I2C_STATE_READY;
  2018. hi2c->Mode = HAL_I2C_MODE_NONE;
  2019. /* Process Unlocked */
  2020. __HAL_UNLOCK(hi2c);
  2021. return HAL_OK;
  2022. }
  2023. else
  2024. {
  2025. return HAL_BUSY;
  2026. }
  2027. }
  2028. /**
  2029. * @brief Read an amount of data in blocking mode from a specific memory address
  2030. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2031. * the configuration information for the specified I2C.
  2032. * @param DevAddress Target device address: The device 7 bits address value
  2033. * in datasheet must be shifted to the left before calling the interface
  2034. * @param MemAddress Internal memory address
  2035. * @param MemAddSize Size of internal memory address
  2036. * @param pData Pointer to data buffer
  2037. * @param Size Amount of data to be sent
  2038. * @param Timeout Timeout duration
  2039. * @retval HAL status
  2040. */
  2041. HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  2042. {
  2043. uint32_t tickstart;
  2044. /* Check the parameters */
  2045. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  2046. if (hi2c->State == HAL_I2C_STATE_READY)
  2047. {
  2048. if ((pData == NULL) || (Size == 0U))
  2049. {
  2050. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  2051. return HAL_ERROR;
  2052. }
  2053. /* Process Locked */
  2054. __HAL_LOCK(hi2c);
  2055. /* Init tickstart for timeout management*/
  2056. tickstart = HAL_GetTick();
  2057. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY, tickstart) != HAL_OK)
  2058. {
  2059. return HAL_ERROR;
  2060. }
  2061. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  2062. hi2c->Mode = HAL_I2C_MODE_MEM;
  2063. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2064. /* Prepare transfer parameters */
  2065. hi2c->pBuffPtr = pData;
  2066. hi2c->XferCount = Size;
  2067. hi2c->XferISR = NULL;
  2068. /* Send Slave Address and Memory Address */
  2069. if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)
  2070. {
  2071. /* Process Unlocked */
  2072. __HAL_UNLOCK(hi2c);
  2073. return HAL_ERROR;
  2074. }
  2075. /* Send Slave Address */
  2076. /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  2077. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  2078. {
  2079. hi2c->XferSize = MAX_NBYTE_SIZE;
  2080. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
  2081. }
  2082. else
  2083. {
  2084. hi2c->XferSize = hi2c->XferCount;
  2085. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
  2086. }
  2087. do
  2088. {
  2089. /* Wait until RXNE flag is set */
  2090. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout, tickstart) != HAL_OK)
  2091. {
  2092. return HAL_ERROR;
  2093. }
  2094. /* Read data from RXDR */
  2095. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
  2096. /* Increment Buffer pointer */
  2097. hi2c->pBuffPtr++;
  2098. hi2c->XferSize--;
  2099. hi2c->XferCount--;
  2100. if ((hi2c->XferCount != 0U) && (hi2c->XferSize == 0U))
  2101. {
  2102. /* Wait until TCR flag is set */
  2103. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK)
  2104. {
  2105. return HAL_ERROR;
  2106. }
  2107. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  2108. {
  2109. hi2c->XferSize = MAX_NBYTE_SIZE;
  2110. I2C_TransferConfig(hi2c, DevAddress, (uint8_t) hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  2111. }
  2112. else
  2113. {
  2114. hi2c->XferSize = hi2c->XferCount;
  2115. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  2116. }
  2117. }
  2118. }
  2119. while (hi2c->XferCount > 0U);
  2120. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2121. /* Wait until STOPF flag is reset */
  2122. if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  2123. {
  2124. return HAL_ERROR;
  2125. }
  2126. /* Clear STOP Flag */
  2127. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2128. /* Clear Configuration Register 2 */
  2129. I2C_RESET_CR2(hi2c);
  2130. hi2c->State = HAL_I2C_STATE_READY;
  2131. hi2c->Mode = HAL_I2C_MODE_NONE;
  2132. /* Process Unlocked */
  2133. __HAL_UNLOCK(hi2c);
  2134. return HAL_OK;
  2135. }
  2136. else
  2137. {
  2138. return HAL_BUSY;
  2139. }
  2140. }
  2141. /**
  2142. * @brief Write an amount of data in non-blocking mode with Interrupt to a specific memory address
  2143. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2144. * the configuration information for the specified I2C.
  2145. * @param DevAddress Target device address: The device 7 bits address value
  2146. * in datasheet must be shifted to the left before calling the interface
  2147. * @param MemAddress Internal memory address
  2148. * @param MemAddSize Size of internal memory address
  2149. * @param pData Pointer to data buffer
  2150. * @param Size Amount of data to be sent
  2151. * @retval HAL status
  2152. */
  2153. HAL_StatusTypeDef HAL_I2C_Mem_Write_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
  2154. {
  2155. uint32_t tickstart;
  2156. uint32_t xfermode;
  2157. /* Check the parameters */
  2158. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  2159. if (hi2c->State == HAL_I2C_STATE_READY)
  2160. {
  2161. if ((pData == NULL) || (Size == 0U))
  2162. {
  2163. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  2164. return HAL_ERROR;
  2165. }
  2166. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  2167. {
  2168. return HAL_BUSY;
  2169. }
  2170. /* Process Locked */
  2171. __HAL_LOCK(hi2c);
  2172. /* Init tickstart for timeout management*/
  2173. tickstart = HAL_GetTick();
  2174. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  2175. hi2c->Mode = HAL_I2C_MODE_MEM;
  2176. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2177. /* Prepare transfer parameters */
  2178. hi2c->pBuffPtr = pData;
  2179. hi2c->XferCount = Size;
  2180. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2181. hi2c->XferISR = I2C_Master_ISR_IT;
  2182. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  2183. {
  2184. hi2c->XferSize = MAX_NBYTE_SIZE;
  2185. xfermode = I2C_RELOAD_MODE;
  2186. }
  2187. else
  2188. {
  2189. hi2c->XferSize = hi2c->XferCount;
  2190. xfermode = I2C_AUTOEND_MODE;
  2191. }
  2192. /* Send Slave Address and Memory Address */
  2193. if (I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
  2194. {
  2195. /* Process Unlocked */
  2196. __HAL_UNLOCK(hi2c);
  2197. return HAL_ERROR;
  2198. }
  2199. /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  2200. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, I2C_NO_STARTSTOP);
  2201. /* Process Unlocked */
  2202. __HAL_UNLOCK(hi2c);
  2203. /* Note : The I2C interrupts must be enabled after unlocking current process
  2204. to avoid the risk of I2C interrupt handle execution before current
  2205. process unlock */
  2206. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  2207. /* possible to enable all of these */
  2208. /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  2209. I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
  2210. return HAL_OK;
  2211. }
  2212. else
  2213. {
  2214. return HAL_BUSY;
  2215. }
  2216. }
  2217. /**
  2218. * @brief Read an amount of data in non-blocking mode with Interrupt from a specific memory address
  2219. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2220. * the configuration information for the specified I2C.
  2221. * @param DevAddress Target device address: The device 7 bits address value
  2222. * in datasheet must be shifted to the left before calling the interface
  2223. * @param MemAddress Internal memory address
  2224. * @param MemAddSize Size of internal memory address
  2225. * @param pData Pointer to data buffer
  2226. * @param Size Amount of data to be sent
  2227. * @retval HAL status
  2228. */
  2229. HAL_StatusTypeDef HAL_I2C_Mem_Read_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
  2230. {
  2231. uint32_t tickstart;
  2232. uint32_t xfermode;
  2233. /* Check the parameters */
  2234. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  2235. if (hi2c->State == HAL_I2C_STATE_READY)
  2236. {
  2237. if ((pData == NULL) || (Size == 0U))
  2238. {
  2239. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  2240. return HAL_ERROR;
  2241. }
  2242. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  2243. {
  2244. return HAL_BUSY;
  2245. }
  2246. /* Process Locked */
  2247. __HAL_LOCK(hi2c);
  2248. /* Init tickstart for timeout management*/
  2249. tickstart = HAL_GetTick();
  2250. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  2251. hi2c->Mode = HAL_I2C_MODE_MEM;
  2252. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2253. /* Prepare transfer parameters */
  2254. hi2c->pBuffPtr = pData;
  2255. hi2c->XferCount = Size;
  2256. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2257. hi2c->XferISR = I2C_Master_ISR_IT;
  2258. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  2259. {
  2260. hi2c->XferSize = MAX_NBYTE_SIZE;
  2261. xfermode = I2C_RELOAD_MODE;
  2262. }
  2263. else
  2264. {
  2265. hi2c->XferSize = hi2c->XferCount;
  2266. xfermode = I2C_AUTOEND_MODE;
  2267. }
  2268. /* Send Slave Address and Memory Address */
  2269. if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
  2270. {
  2271. /* Process Unlocked */
  2272. __HAL_UNLOCK(hi2c);
  2273. return HAL_ERROR;
  2274. }
  2275. /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  2276. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, I2C_GENERATE_START_READ);
  2277. /* Process Unlocked */
  2278. __HAL_UNLOCK(hi2c);
  2279. /* Note : The I2C interrupts must be enabled after unlocking current process
  2280. to avoid the risk of I2C interrupt handle execution before current
  2281. process unlock */
  2282. /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  2283. /* possible to enable all of these */
  2284. /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  2285. I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT);
  2286. return HAL_OK;
  2287. }
  2288. else
  2289. {
  2290. return HAL_BUSY;
  2291. }
  2292. }
  2293. /**
  2294. * @brief Write an amount of data in non-blocking mode with DMA to a specific memory address
  2295. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2296. * the configuration information for the specified I2C.
  2297. * @param DevAddress Target device address: The device 7 bits address value
  2298. * in datasheet must be shifted to the left before calling the interface
  2299. * @param MemAddress Internal memory address
  2300. * @param MemAddSize Size of internal memory address
  2301. * @param pData Pointer to data buffer
  2302. * @param Size Amount of data to be sent
  2303. * @retval HAL status
  2304. */
  2305. HAL_StatusTypeDef HAL_I2C_Mem_Write_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
  2306. {
  2307. uint32_t tickstart;
  2308. uint32_t xfermode;
  2309. HAL_StatusTypeDef dmaxferstatus;
  2310. /* Check the parameters */
  2311. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  2312. if (hi2c->State == HAL_I2C_STATE_READY)
  2313. {
  2314. if ((pData == NULL) || (Size == 0U))
  2315. {
  2316. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  2317. return HAL_ERROR;
  2318. }
  2319. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  2320. {
  2321. return HAL_BUSY;
  2322. }
  2323. /* Process Locked */
  2324. __HAL_LOCK(hi2c);
  2325. /* Init tickstart for timeout management*/
  2326. tickstart = HAL_GetTick();
  2327. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  2328. hi2c->Mode = HAL_I2C_MODE_MEM;
  2329. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2330. /* Prepare transfer parameters */
  2331. hi2c->pBuffPtr = pData;
  2332. hi2c->XferCount = Size;
  2333. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2334. hi2c->XferISR = I2C_Master_ISR_DMA;
  2335. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  2336. {
  2337. hi2c->XferSize = MAX_NBYTE_SIZE;
  2338. xfermode = I2C_RELOAD_MODE;
  2339. }
  2340. else
  2341. {
  2342. hi2c->XferSize = hi2c->XferCount;
  2343. xfermode = I2C_AUTOEND_MODE;
  2344. }
  2345. /* Send Slave Address and Memory Address */
  2346. if (I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
  2347. {
  2348. /* Process Unlocked */
  2349. __HAL_UNLOCK(hi2c);
  2350. return HAL_ERROR;
  2351. }
  2352. if (hi2c->hdmatx != NULL)
  2353. {
  2354. /* Set the I2C DMA transfer complete callback */
  2355. hi2c->hdmatx->XferCpltCallback = I2C_DMAMasterTransmitCplt;
  2356. /* Set the DMA error callback */
  2357. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  2358. /* Set the unused DMA callbacks to NULL */
  2359. hi2c->hdmatx->XferHalfCpltCallback = NULL;
  2360. hi2c->hdmatx->XferAbortCallback = NULL;
  2361. /* Enable the DMA channel */
  2362. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
  2363. }
  2364. else
  2365. {
  2366. /* Update I2C state */
  2367. hi2c->State = HAL_I2C_STATE_READY;
  2368. hi2c->Mode = HAL_I2C_MODE_NONE;
  2369. /* Update I2C error code */
  2370. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  2371. /* Process Unlocked */
  2372. __HAL_UNLOCK(hi2c);
  2373. return HAL_ERROR;
  2374. }
  2375. if (dmaxferstatus == HAL_OK)
  2376. {
  2377. /* Send Slave Address */
  2378. /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  2379. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, I2C_NO_STARTSTOP);
  2380. /* Update XferCount value */
  2381. hi2c->XferCount -= hi2c->XferSize;
  2382. /* Process Unlocked */
  2383. __HAL_UNLOCK(hi2c);
  2384. /* Note : The I2C interrupts must be enabled after unlocking current process
  2385. to avoid the risk of I2C interrupt handle execution before current
  2386. process unlock */
  2387. /* Enable ERR and NACK interrupts */
  2388. I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT);
  2389. /* Enable DMA Request */
  2390. hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  2391. }
  2392. else
  2393. {
  2394. /* Update I2C state */
  2395. hi2c->State = HAL_I2C_STATE_READY;
  2396. hi2c->Mode = HAL_I2C_MODE_NONE;
  2397. /* Update I2C error code */
  2398. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  2399. /* Process Unlocked */
  2400. __HAL_UNLOCK(hi2c);
  2401. return HAL_ERROR;
  2402. }
  2403. return HAL_OK;
  2404. }
  2405. else
  2406. {
  2407. return HAL_BUSY;
  2408. }
  2409. }
  2410. /**
  2411. * @brief Reads an amount of data in non-blocking mode with DMA from a specific memory address.
  2412. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2413. * the configuration information for the specified I2C.
  2414. * @param DevAddress Target device address: The device 7 bits address value
  2415. * in datasheet must be shifted to the left before calling the interface
  2416. * @param MemAddress Internal memory address
  2417. * @param MemAddSize Size of internal memory address
  2418. * @param pData Pointer to data buffer
  2419. * @param Size Amount of data to be read
  2420. * @retval HAL status
  2421. */
  2422. HAL_StatusTypeDef HAL_I2C_Mem_Read_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
  2423. {
  2424. uint32_t tickstart;
  2425. uint32_t xfermode;
  2426. HAL_StatusTypeDef dmaxferstatus;
  2427. /* Check the parameters */
  2428. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  2429. if (hi2c->State == HAL_I2C_STATE_READY)
  2430. {
  2431. if ((pData == NULL) || (Size == 0U))
  2432. {
  2433. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  2434. return HAL_ERROR;
  2435. }
  2436. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  2437. {
  2438. return HAL_BUSY;
  2439. }
  2440. /* Process Locked */
  2441. __HAL_LOCK(hi2c);
  2442. /* Init tickstart for timeout management*/
  2443. tickstart = HAL_GetTick();
  2444. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  2445. hi2c->Mode = HAL_I2C_MODE_MEM;
  2446. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2447. /* Prepare transfer parameters */
  2448. hi2c->pBuffPtr = pData;
  2449. hi2c->XferCount = Size;
  2450. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2451. hi2c->XferISR = I2C_Master_ISR_DMA;
  2452. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  2453. {
  2454. hi2c->XferSize = MAX_NBYTE_SIZE;
  2455. xfermode = I2C_RELOAD_MODE;
  2456. }
  2457. else
  2458. {
  2459. hi2c->XferSize = hi2c->XferCount;
  2460. xfermode = I2C_AUTOEND_MODE;
  2461. }
  2462. /* Send Slave Address and Memory Address */
  2463. if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
  2464. {
  2465. /* Process Unlocked */
  2466. __HAL_UNLOCK(hi2c);
  2467. return HAL_ERROR;
  2468. }
  2469. if (hi2c->hdmarx != NULL)
  2470. {
  2471. /* Set the I2C DMA transfer complete callback */
  2472. hi2c->hdmarx->XferCpltCallback = I2C_DMAMasterReceiveCplt;
  2473. /* Set the DMA error callback */
  2474. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  2475. /* Set the unused DMA callbacks to NULL */
  2476. hi2c->hdmarx->XferHalfCpltCallback = NULL;
  2477. hi2c->hdmarx->XferAbortCallback = NULL;
  2478. /* Enable the DMA channel */
  2479. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, hi2c->XferSize);
  2480. }
  2481. else
  2482. {
  2483. /* Update I2C state */
  2484. hi2c->State = HAL_I2C_STATE_READY;
  2485. hi2c->Mode = HAL_I2C_MODE_NONE;
  2486. /* Update I2C error code */
  2487. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  2488. /* Process Unlocked */
  2489. __HAL_UNLOCK(hi2c);
  2490. return HAL_ERROR;
  2491. }
  2492. if (dmaxferstatus == HAL_OK)
  2493. {
  2494. /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  2495. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, I2C_GENERATE_START_READ);
  2496. /* Update XferCount value */
  2497. hi2c->XferCount -= hi2c->XferSize;
  2498. /* Process Unlocked */
  2499. __HAL_UNLOCK(hi2c);
  2500. /* Note : The I2C interrupts must be enabled after unlocking current process
  2501. to avoid the risk of I2C interrupt handle execution before current
  2502. process unlock */
  2503. /* Enable ERR and NACK interrupts */
  2504. I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT);
  2505. /* Enable DMA Request */
  2506. hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  2507. }
  2508. else
  2509. {
  2510. /* Update I2C state */
  2511. hi2c->State = HAL_I2C_STATE_READY;
  2512. hi2c->Mode = HAL_I2C_MODE_NONE;
  2513. /* Update I2C error code */
  2514. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  2515. /* Process Unlocked */
  2516. __HAL_UNLOCK(hi2c);
  2517. return HAL_ERROR;
  2518. }
  2519. return HAL_OK;
  2520. }
  2521. else
  2522. {
  2523. return HAL_BUSY;
  2524. }
  2525. }
  2526. /**
  2527. * @brief Checks if target device is ready for communication.
  2528. * @note This function is used with Memory devices
  2529. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2530. * the configuration information for the specified I2C.
  2531. * @param DevAddress Target device address: The device 7 bits address value
  2532. * in datasheet must be shifted to the left before calling the interface
  2533. * @param Trials Number of trials
  2534. * @param Timeout Timeout duration
  2535. * @retval HAL status
  2536. */
  2537. HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
  2538. {
  2539. uint32_t tickstart;
  2540. __IO uint32_t I2C_Trials = 0UL;
  2541. FlagStatus tmp1;
  2542. FlagStatus tmp2;
  2543. if (hi2c->State == HAL_I2C_STATE_READY)
  2544. {
  2545. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  2546. {
  2547. return HAL_BUSY;
  2548. }
  2549. /* Process Locked */
  2550. __HAL_LOCK(hi2c);
  2551. hi2c->State = HAL_I2C_STATE_BUSY;
  2552. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2553. do
  2554. {
  2555. /* Generate Start */
  2556. hi2c->Instance->CR2 = I2C_GENERATE_START(hi2c->Init.AddressingMode, DevAddress);
  2557. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2558. /* Wait until STOPF flag is set or a NACK flag is set*/
  2559. tickstart = HAL_GetTick();
  2560. tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF);
  2561. tmp2 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF);
  2562. while ((tmp1 == RESET) && (tmp2 == RESET))
  2563. {
  2564. if (Timeout != HAL_MAX_DELAY)
  2565. {
  2566. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  2567. {
  2568. /* Update I2C state */
  2569. hi2c->State = HAL_I2C_STATE_READY;
  2570. /* Update I2C error code */
  2571. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2572. /* Process Unlocked */
  2573. __HAL_UNLOCK(hi2c);
  2574. return HAL_ERROR;
  2575. }
  2576. }
  2577. tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF);
  2578. tmp2 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF);
  2579. }
  2580. /* Check if the NACKF flag has not been set */
  2581. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == RESET)
  2582. {
  2583. /* Wait until STOPF flag is reset */
  2584. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout, tickstart) != HAL_OK)
  2585. {
  2586. return HAL_ERROR;
  2587. }
  2588. /* Clear STOP Flag */
  2589. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2590. /* Device is ready */
  2591. hi2c->State = HAL_I2C_STATE_READY;
  2592. /* Process Unlocked */
  2593. __HAL_UNLOCK(hi2c);
  2594. return HAL_OK;
  2595. }
  2596. else
  2597. {
  2598. /* Wait until STOPF flag is reset */
  2599. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout, tickstart) != HAL_OK)
  2600. {
  2601. return HAL_ERROR;
  2602. }
  2603. /* Clear NACK Flag */
  2604. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  2605. /* Clear STOP Flag, auto generated with autoend*/
  2606. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2607. }
  2608. /* Check if the maximum allowed number of trials has been reached */
  2609. if (I2C_Trials == Trials)
  2610. {
  2611. /* Generate Stop */
  2612. hi2c->Instance->CR2 |= I2C_CR2_STOP;
  2613. /* Wait until STOPF flag is reset */
  2614. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout, tickstart) != HAL_OK)
  2615. {
  2616. return HAL_ERROR;
  2617. }
  2618. /* Clear STOP Flag */
  2619. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2620. }
  2621. /* Increment Trials */
  2622. I2C_Trials++;
  2623. }
  2624. while (I2C_Trials < Trials);
  2625. /* Update I2C state */
  2626. hi2c->State = HAL_I2C_STATE_READY;
  2627. /* Update I2C error code */
  2628. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2629. /* Process Unlocked */
  2630. __HAL_UNLOCK(hi2c);
  2631. return HAL_ERROR;
  2632. }
  2633. else
  2634. {
  2635. return HAL_BUSY;
  2636. }
  2637. }
  2638. /**
  2639. * @brief Sequential transmit in master I2C mode an amount of data in non-blocking mode with Interrupt.
  2640. * @note This interface allow to manage repeated start condition when a direction change during transfer
  2641. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2642. * the configuration information for the specified I2C.
  2643. * @param DevAddress Target device address: The device 7 bits address value
  2644. * in datasheet must be shifted to the left before calling the interface
  2645. * @param pData Pointer to data buffer
  2646. * @param Size Amount of data to be sent
  2647. * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
  2648. * @retval HAL status
  2649. */
  2650. HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  2651. {
  2652. uint32_t xfermode;
  2653. uint32_t xferrequest = I2C_GENERATE_START_WRITE;
  2654. /* Check the parameters */
  2655. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  2656. if (hi2c->State == HAL_I2C_STATE_READY)
  2657. {
  2658. /* Process Locked */
  2659. __HAL_LOCK(hi2c);
  2660. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  2661. hi2c->Mode = HAL_I2C_MODE_MASTER;
  2662. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2663. /* Prepare transfer parameters */
  2664. hi2c->pBuffPtr = pData;
  2665. hi2c->XferCount = Size;
  2666. hi2c->XferOptions = XferOptions;
  2667. hi2c->XferISR = I2C_Master_ISR_IT;
  2668. /* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */
  2669. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  2670. {
  2671. hi2c->XferSize = MAX_NBYTE_SIZE;
  2672. xfermode = I2C_RELOAD_MODE;
  2673. }
  2674. else
  2675. {
  2676. hi2c->XferSize = hi2c->XferCount;
  2677. xfermode = hi2c->XferOptions;
  2678. }
  2679. /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
  2680. /* Mean Previous state is same as current state */
  2681. if ((hi2c->PreviousState == I2C_STATE_MASTER_BUSY_TX) && (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 0))
  2682. {
  2683. xferrequest = I2C_NO_STARTSTOP;
  2684. }
  2685. else
  2686. {
  2687. /* Convert OTHER_xxx XferOptions if any */
  2688. I2C_ConvertOtherXferOptions(hi2c);
  2689. /* Update xfermode accordingly if no reload is necessary */
  2690. if (hi2c->XferCount <= MAX_NBYTE_SIZE)
  2691. {
  2692. xfermode = hi2c->XferOptions;
  2693. }
  2694. }
  2695. /* Send Slave Address and set NBYTES to write */
  2696. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, xferrequest);
  2697. /* Process Unlocked */
  2698. __HAL_UNLOCK(hi2c);
  2699. /* Note : The I2C interrupts must be enabled after unlocking current process
  2700. to avoid the risk of I2C interrupt handle execution before current
  2701. process unlock */
  2702. I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
  2703. return HAL_OK;
  2704. }
  2705. else
  2706. {
  2707. return HAL_BUSY;
  2708. }
  2709. }
  2710. /**
  2711. * @brief Sequential transmit in master I2C mode an amount of data in non-blocking mode with DMA.
  2712. * @note This interface allow to manage repeated start condition when a direction change during transfer
  2713. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2714. * the configuration information for the specified I2C.
  2715. * @param DevAddress Target device address: The device 7 bits address value
  2716. * in datasheet must be shifted to the left before calling the interface
  2717. * @param pData Pointer to data buffer
  2718. * @param Size Amount of data to be sent
  2719. * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
  2720. * @retval HAL status
  2721. */
  2722. HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  2723. {
  2724. uint32_t xfermode;
  2725. uint32_t xferrequest = I2C_GENERATE_START_WRITE;
  2726. HAL_StatusTypeDef dmaxferstatus;
  2727. /* Check the parameters */
  2728. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  2729. if (hi2c->State == HAL_I2C_STATE_READY)
  2730. {
  2731. /* Process Locked */
  2732. __HAL_LOCK(hi2c);
  2733. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  2734. hi2c->Mode = HAL_I2C_MODE_MASTER;
  2735. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2736. /* Prepare transfer parameters */
  2737. hi2c->pBuffPtr = pData;
  2738. hi2c->XferCount = Size;
  2739. hi2c->XferOptions = XferOptions;
  2740. hi2c->XferISR = I2C_Master_ISR_DMA;
  2741. /* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */
  2742. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  2743. {
  2744. hi2c->XferSize = MAX_NBYTE_SIZE;
  2745. xfermode = I2C_RELOAD_MODE;
  2746. }
  2747. else
  2748. {
  2749. hi2c->XferSize = hi2c->XferCount;
  2750. xfermode = hi2c->XferOptions;
  2751. }
  2752. /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
  2753. /* Mean Previous state is same as current state */
  2754. if ((hi2c->PreviousState == I2C_STATE_MASTER_BUSY_TX) && (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 0))
  2755. {
  2756. xferrequest = I2C_NO_STARTSTOP;
  2757. }
  2758. else
  2759. {
  2760. /* Convert OTHER_xxx XferOptions if any */
  2761. I2C_ConvertOtherXferOptions(hi2c);
  2762. /* Update xfermode accordingly if no reload is necessary */
  2763. if (hi2c->XferCount <= MAX_NBYTE_SIZE)
  2764. {
  2765. xfermode = hi2c->XferOptions;
  2766. }
  2767. }
  2768. if (hi2c->XferSize > 0U)
  2769. {
  2770. if (hi2c->hdmatx != NULL)
  2771. {
  2772. /* Set the I2C DMA transfer complete callback */
  2773. hi2c->hdmatx->XferCpltCallback = I2C_DMAMasterTransmitCplt;
  2774. /* Set the DMA error callback */
  2775. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  2776. /* Set the unused DMA callbacks to NULL */
  2777. hi2c->hdmatx->XferHalfCpltCallback = NULL;
  2778. hi2c->hdmatx->XferAbortCallback = NULL;
  2779. /* Enable the DMA channel */
  2780. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
  2781. }
  2782. else
  2783. {
  2784. /* Update I2C state */
  2785. hi2c->State = HAL_I2C_STATE_READY;
  2786. hi2c->Mode = HAL_I2C_MODE_NONE;
  2787. /* Update I2C error code */
  2788. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  2789. /* Process Unlocked */
  2790. __HAL_UNLOCK(hi2c);
  2791. return HAL_ERROR;
  2792. }
  2793. if (dmaxferstatus == HAL_OK)
  2794. {
  2795. /* Send Slave Address and set NBYTES to write */
  2796. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, xferrequest);
  2797. /* Update XferCount value */
  2798. hi2c->XferCount -= hi2c->XferSize;
  2799. /* Process Unlocked */
  2800. __HAL_UNLOCK(hi2c);
  2801. /* Note : The I2C interrupts must be enabled after unlocking current process
  2802. to avoid the risk of I2C interrupt handle execution before current
  2803. process unlock */
  2804. /* Enable ERR and NACK interrupts */
  2805. I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT);
  2806. /* Enable DMA Request */
  2807. hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  2808. }
  2809. else
  2810. {
  2811. /* Update I2C state */
  2812. hi2c->State = HAL_I2C_STATE_READY;
  2813. hi2c->Mode = HAL_I2C_MODE_NONE;
  2814. /* Update I2C error code */
  2815. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  2816. /* Process Unlocked */
  2817. __HAL_UNLOCK(hi2c);
  2818. return HAL_ERROR;
  2819. }
  2820. }
  2821. else
  2822. {
  2823. /* Update Transfer ISR function pointer */
  2824. hi2c->XferISR = I2C_Master_ISR_IT;
  2825. /* Send Slave Address */
  2826. /* Set NBYTES to write and generate START condition */
  2827. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_WRITE);
  2828. /* Process Unlocked */
  2829. __HAL_UNLOCK(hi2c);
  2830. /* Note : The I2C interrupts must be enabled after unlocking current process
  2831. to avoid the risk of I2C interrupt handle execution before current
  2832. process unlock */
  2833. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  2834. /* possible to enable all of these */
  2835. /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  2836. I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
  2837. }
  2838. return HAL_OK;
  2839. }
  2840. else
  2841. {
  2842. return HAL_BUSY;
  2843. }
  2844. }
  2845. /**
  2846. * @brief Sequential receive in master I2C mode an amount of data in non-blocking mode with Interrupt
  2847. * @note This interface allow to manage repeated start condition when a direction change during transfer
  2848. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2849. * the configuration information for the specified I2C.
  2850. * @param DevAddress Target device address: The device 7 bits address value
  2851. * in datasheet must be shifted to the left before calling the interface
  2852. * @param pData Pointer to data buffer
  2853. * @param Size Amount of data to be sent
  2854. * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
  2855. * @retval HAL status
  2856. */
  2857. HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  2858. {
  2859. uint32_t xfermode;
  2860. uint32_t xferrequest = I2C_GENERATE_START_READ;
  2861. /* Check the parameters */
  2862. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  2863. if (hi2c->State == HAL_I2C_STATE_READY)
  2864. {
  2865. /* Process Locked */
  2866. __HAL_LOCK(hi2c);
  2867. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  2868. hi2c->Mode = HAL_I2C_MODE_MASTER;
  2869. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2870. /* Prepare transfer parameters */
  2871. hi2c->pBuffPtr = pData;
  2872. hi2c->XferCount = Size;
  2873. hi2c->XferOptions = XferOptions;
  2874. hi2c->XferISR = I2C_Master_ISR_IT;
  2875. /* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */
  2876. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  2877. {
  2878. hi2c->XferSize = MAX_NBYTE_SIZE;
  2879. xfermode = I2C_RELOAD_MODE;
  2880. }
  2881. else
  2882. {
  2883. hi2c->XferSize = hi2c->XferCount;
  2884. xfermode = hi2c->XferOptions;
  2885. }
  2886. /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
  2887. /* Mean Previous state is same as current state */
  2888. if ((hi2c->PreviousState == I2C_STATE_MASTER_BUSY_RX) && (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 0))
  2889. {
  2890. xferrequest = I2C_NO_STARTSTOP;
  2891. }
  2892. else
  2893. {
  2894. /* Convert OTHER_xxx XferOptions if any */
  2895. I2C_ConvertOtherXferOptions(hi2c);
  2896. /* Update xfermode accordingly if no reload is necessary */
  2897. if (hi2c->XferCount <= MAX_NBYTE_SIZE)
  2898. {
  2899. xfermode = hi2c->XferOptions;
  2900. }
  2901. }
  2902. /* Send Slave Address and set NBYTES to read */
  2903. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, xferrequest);
  2904. /* Process Unlocked */
  2905. __HAL_UNLOCK(hi2c);
  2906. /* Note : The I2C interrupts must be enabled after unlocking current process
  2907. to avoid the risk of I2C interrupt handle execution before current
  2908. process unlock */
  2909. I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT);
  2910. return HAL_OK;
  2911. }
  2912. else
  2913. {
  2914. return HAL_BUSY;
  2915. }
  2916. }
  2917. /**
  2918. * @brief Sequential receive in master I2C mode an amount of data in non-blocking mode with DMA
  2919. * @note This interface allow to manage repeated start condition when a direction change during transfer
  2920. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2921. * the configuration information for the specified I2C.
  2922. * @param DevAddress Target device address: The device 7 bits address value
  2923. * in datasheet must be shifted to the left before calling the interface
  2924. * @param pData Pointer to data buffer
  2925. * @param Size Amount of data to be sent
  2926. * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
  2927. * @retval HAL status
  2928. */
  2929. HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  2930. {
  2931. uint32_t xfermode;
  2932. uint32_t xferrequest = I2C_GENERATE_START_READ;
  2933. HAL_StatusTypeDef dmaxferstatus;
  2934. /* Check the parameters */
  2935. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  2936. if (hi2c->State == HAL_I2C_STATE_READY)
  2937. {
  2938. /* Process Locked */
  2939. __HAL_LOCK(hi2c);
  2940. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  2941. hi2c->Mode = HAL_I2C_MODE_MASTER;
  2942. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2943. /* Prepare transfer parameters */
  2944. hi2c->pBuffPtr = pData;
  2945. hi2c->XferCount = Size;
  2946. hi2c->XferOptions = XferOptions;
  2947. hi2c->XferISR = I2C_Master_ISR_DMA;
  2948. /* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */
  2949. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  2950. {
  2951. hi2c->XferSize = MAX_NBYTE_SIZE;
  2952. xfermode = I2C_RELOAD_MODE;
  2953. }
  2954. else
  2955. {
  2956. hi2c->XferSize = hi2c->XferCount;
  2957. xfermode = hi2c->XferOptions;
  2958. }
  2959. /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
  2960. /* Mean Previous state is same as current state */
  2961. if ((hi2c->PreviousState == I2C_STATE_MASTER_BUSY_RX) && (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 0))
  2962. {
  2963. xferrequest = I2C_NO_STARTSTOP;
  2964. }
  2965. else
  2966. {
  2967. /* Convert OTHER_xxx XferOptions if any */
  2968. I2C_ConvertOtherXferOptions(hi2c);
  2969. /* Update xfermode accordingly if no reload is necessary */
  2970. if (hi2c->XferCount <= MAX_NBYTE_SIZE)
  2971. {
  2972. xfermode = hi2c->XferOptions;
  2973. }
  2974. }
  2975. if (hi2c->XferSize > 0U)
  2976. {
  2977. if (hi2c->hdmarx != NULL)
  2978. {
  2979. /* Set the I2C DMA transfer complete callback */
  2980. hi2c->hdmarx->XferCpltCallback = I2C_DMAMasterReceiveCplt;
  2981. /* Set the DMA error callback */
  2982. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  2983. /* Set the unused DMA callbacks to NULL */
  2984. hi2c->hdmarx->XferHalfCpltCallback = NULL;
  2985. hi2c->hdmarx->XferAbortCallback = NULL;
  2986. /* Enable the DMA channel */
  2987. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, hi2c->XferSize);
  2988. }
  2989. else
  2990. {
  2991. /* Update I2C state */
  2992. hi2c->State = HAL_I2C_STATE_READY;
  2993. hi2c->Mode = HAL_I2C_MODE_NONE;
  2994. /* Update I2C error code */
  2995. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  2996. /* Process Unlocked */
  2997. __HAL_UNLOCK(hi2c);
  2998. return HAL_ERROR;
  2999. }
  3000. if (dmaxferstatus == HAL_OK)
  3001. {
  3002. /* Send Slave Address and set NBYTES to read */
  3003. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, xferrequest);
  3004. /* Update XferCount value */
  3005. hi2c->XferCount -= hi2c->XferSize;
  3006. /* Process Unlocked */
  3007. __HAL_UNLOCK(hi2c);
  3008. /* Note : The I2C interrupts must be enabled after unlocking current process
  3009. to avoid the risk of I2C interrupt handle execution before current
  3010. process unlock */
  3011. /* Enable ERR and NACK interrupts */
  3012. I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT);
  3013. /* Enable DMA Request */
  3014. hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  3015. }
  3016. else
  3017. {
  3018. /* Update I2C state */
  3019. hi2c->State = HAL_I2C_STATE_READY;
  3020. hi2c->Mode = HAL_I2C_MODE_NONE;
  3021. /* Update I2C error code */
  3022. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  3023. /* Process Unlocked */
  3024. __HAL_UNLOCK(hi2c);
  3025. return HAL_ERROR;
  3026. }
  3027. }
  3028. else
  3029. {
  3030. /* Update Transfer ISR function pointer */
  3031. hi2c->XferISR = I2C_Master_ISR_IT;
  3032. /* Send Slave Address */
  3033. /* Set NBYTES to read and generate START condition */
  3034. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
  3035. /* Process Unlocked */
  3036. __HAL_UNLOCK(hi2c);
  3037. /* Note : The I2C interrupts must be enabled after unlocking current process
  3038. to avoid the risk of I2C interrupt handle execution before current
  3039. process unlock */
  3040. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  3041. /* possible to enable all of these */
  3042. /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  3043. I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT);
  3044. }
  3045. return HAL_OK;
  3046. }
  3047. else
  3048. {
  3049. return HAL_BUSY;
  3050. }
  3051. }
  3052. /**
  3053. * @brief Sequential transmit in slave/device I2C mode an amount of data in non-blocking mode with Interrupt
  3054. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3055. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3056. * the configuration information for the specified I2C.
  3057. * @param pData Pointer to data buffer
  3058. * @param Size Amount of data to be sent
  3059. * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
  3060. * @retval HAL status
  3061. */
  3062. HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  3063. {
  3064. /* Check the parameters */
  3065. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3066. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
  3067. {
  3068. if ((pData == NULL) || (Size == 0U))
  3069. {
  3070. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  3071. return HAL_ERROR;
  3072. }
  3073. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  3074. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_TX_IT);
  3075. /* Process Locked */
  3076. __HAL_LOCK(hi2c);
  3077. /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */
  3078. /* and then toggle the HAL slave RX state to TX state */
  3079. if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN)
  3080. {
  3081. /* Disable associated Interrupts */
  3082. I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT);
  3083. /* Abort DMA Xfer if any */
  3084. if ((hi2c->Instance->CR1 & I2C_CR1_RXDMAEN) == I2C_CR1_RXDMAEN)
  3085. {
  3086. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  3087. if (hi2c->hdmarx != NULL)
  3088. {
  3089. /* Set the I2C DMA Abort callback :
  3090. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  3091. hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
  3092. /* Abort DMA RX */
  3093. if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
  3094. {
  3095. /* Call Directly XferAbortCallback function in case of error */
  3096. hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
  3097. }
  3098. }
  3099. }
  3100. }
  3101. hi2c->State = HAL_I2C_STATE_BUSY_TX_LISTEN;
  3102. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  3103. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3104. /* Enable Address Acknowledge */
  3105. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  3106. /* Prepare transfer parameters */
  3107. hi2c->pBuffPtr = pData;
  3108. hi2c->XferCount = Size;
  3109. hi2c->XferSize = hi2c->XferCount;
  3110. hi2c->XferOptions = XferOptions;
  3111. hi2c->XferISR = I2C_Slave_ISR_IT;
  3112. if (I2C_GET_DIR(hi2c) == I2C_DIRECTION_RECEIVE)
  3113. {
  3114. /* Clear ADDR flag after prepare the transfer parameters */
  3115. /* This action will generate an acknowledge to the Master */
  3116. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  3117. }
  3118. /* Process Unlocked */
  3119. __HAL_UNLOCK(hi2c);
  3120. /* Note : The I2C interrupts must be enabled after unlocking current process
  3121. to avoid the risk of I2C interrupt handle execution before current
  3122. process unlock */
  3123. /* REnable ADDR interrupt */
  3124. I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT | I2C_XFER_LISTEN_IT);
  3125. return HAL_OK;
  3126. }
  3127. else
  3128. {
  3129. return HAL_ERROR;
  3130. }
  3131. }
  3132. /**
  3133. * @brief Sequential transmit in slave/device I2C mode an amount of data in non-blocking mode with DMA
  3134. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3135. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3136. * the configuration information for the specified I2C.
  3137. * @param pData Pointer to data buffer
  3138. * @param Size Amount of data to be sent
  3139. * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
  3140. * @retval HAL status
  3141. */
  3142. HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  3143. {
  3144. HAL_StatusTypeDef dmaxferstatus;
  3145. /* Check the parameters */
  3146. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3147. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
  3148. {
  3149. if ((pData == NULL) || (Size == 0U))
  3150. {
  3151. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  3152. return HAL_ERROR;
  3153. }
  3154. /* Process Locked */
  3155. __HAL_LOCK(hi2c);
  3156. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  3157. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_TX_IT);
  3158. /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */
  3159. /* and then toggle the HAL slave RX state to TX state */
  3160. if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN)
  3161. {
  3162. /* Disable associated Interrupts */
  3163. I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT);
  3164. if ((hi2c->Instance->CR1 & I2C_CR1_RXDMAEN) == I2C_CR1_RXDMAEN)
  3165. {
  3166. /* Abort DMA Xfer if any */
  3167. if (hi2c->hdmarx != NULL)
  3168. {
  3169. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  3170. /* Set the I2C DMA Abort callback :
  3171. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  3172. hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
  3173. /* Abort DMA RX */
  3174. if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
  3175. {
  3176. /* Call Directly XferAbortCallback function in case of error */
  3177. hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
  3178. }
  3179. }
  3180. }
  3181. }
  3182. else if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN)
  3183. {
  3184. if ((hi2c->Instance->CR1 & I2C_CR1_TXDMAEN) == I2C_CR1_TXDMAEN)
  3185. {
  3186. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  3187. /* Abort DMA Xfer if any */
  3188. if (hi2c->hdmatx != NULL)
  3189. {
  3190. /* Set the I2C DMA Abort callback :
  3191. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  3192. hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
  3193. /* Abort DMA TX */
  3194. if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
  3195. {
  3196. /* Call Directly XferAbortCallback function in case of error */
  3197. hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
  3198. }
  3199. }
  3200. }
  3201. }
  3202. else
  3203. {
  3204. /* Nothing to do */
  3205. }
  3206. hi2c->State = HAL_I2C_STATE_BUSY_TX_LISTEN;
  3207. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  3208. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3209. /* Enable Address Acknowledge */
  3210. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  3211. /* Prepare transfer parameters */
  3212. hi2c->pBuffPtr = pData;
  3213. hi2c->XferCount = Size;
  3214. hi2c->XferSize = hi2c->XferCount;
  3215. hi2c->XferOptions = XferOptions;
  3216. hi2c->XferISR = I2C_Slave_ISR_DMA;
  3217. if (hi2c->hdmatx != NULL)
  3218. {
  3219. /* Set the I2C DMA transfer complete callback */
  3220. hi2c->hdmatx->XferCpltCallback = I2C_DMASlaveTransmitCplt;
  3221. /* Set the DMA error callback */
  3222. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  3223. /* Set the unused DMA callbacks to NULL */
  3224. hi2c->hdmatx->XferHalfCpltCallback = NULL;
  3225. hi2c->hdmatx->XferAbortCallback = NULL;
  3226. /* Enable the DMA channel */
  3227. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
  3228. }
  3229. else
  3230. {
  3231. /* Update I2C state */
  3232. hi2c->State = HAL_I2C_STATE_LISTEN;
  3233. hi2c->Mode = HAL_I2C_MODE_NONE;
  3234. /* Update I2C error code */
  3235. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  3236. /* Process Unlocked */
  3237. __HAL_UNLOCK(hi2c);
  3238. return HAL_ERROR;
  3239. }
  3240. if (dmaxferstatus == HAL_OK)
  3241. {
  3242. /* Update XferCount value */
  3243. hi2c->XferCount -= hi2c->XferSize;
  3244. /* Reset XferSize */
  3245. hi2c->XferSize = 0;
  3246. }
  3247. else
  3248. {
  3249. /* Update I2C state */
  3250. hi2c->State = HAL_I2C_STATE_LISTEN;
  3251. hi2c->Mode = HAL_I2C_MODE_NONE;
  3252. /* Update I2C error code */
  3253. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  3254. /* Process Unlocked */
  3255. __HAL_UNLOCK(hi2c);
  3256. return HAL_ERROR;
  3257. }
  3258. if (I2C_GET_DIR(hi2c) == I2C_DIRECTION_RECEIVE)
  3259. {
  3260. /* Clear ADDR flag after prepare the transfer parameters */
  3261. /* This action will generate an acknowledge to the Master */
  3262. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  3263. }
  3264. /* Process Unlocked */
  3265. __HAL_UNLOCK(hi2c);
  3266. /* Note : The I2C interrupts must be enabled after unlocking current process
  3267. to avoid the risk of I2C interrupt handle execution before current
  3268. process unlock */
  3269. /* Enable ERR, STOP, NACK, ADDR interrupts */
  3270. I2C_Enable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
  3271. /* Enable DMA Request */
  3272. hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  3273. return HAL_OK;
  3274. }
  3275. else
  3276. {
  3277. return HAL_ERROR;
  3278. }
  3279. }
  3280. /**
  3281. * @brief Sequential receive in slave/device I2C mode an amount of data in non-blocking mode with Interrupt
  3282. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3283. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3284. * the configuration information for the specified I2C.
  3285. * @param pData Pointer to data buffer
  3286. * @param Size Amount of data to be sent
  3287. * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
  3288. * @retval HAL status
  3289. */
  3290. HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  3291. {
  3292. /* Check the parameters */
  3293. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3294. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
  3295. {
  3296. if ((pData == NULL) || (Size == 0U))
  3297. {
  3298. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  3299. return HAL_ERROR;
  3300. }
  3301. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  3302. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_RX_IT);
  3303. /* Process Locked */
  3304. __HAL_LOCK(hi2c);
  3305. /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */
  3306. /* and then toggle the HAL slave TX state to RX state */
  3307. if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN)
  3308. {
  3309. /* Disable associated Interrupts */
  3310. I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
  3311. if ((hi2c->Instance->CR1 & I2C_CR1_TXDMAEN) == I2C_CR1_TXDMAEN)
  3312. {
  3313. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  3314. /* Abort DMA Xfer if any */
  3315. if (hi2c->hdmatx != NULL)
  3316. {
  3317. /* Set the I2C DMA Abort callback :
  3318. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  3319. hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
  3320. /* Abort DMA TX */
  3321. if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
  3322. {
  3323. /* Call Directly XferAbortCallback function in case of error */
  3324. hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
  3325. }
  3326. }
  3327. }
  3328. }
  3329. hi2c->State = HAL_I2C_STATE_BUSY_RX_LISTEN;
  3330. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  3331. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3332. /* Enable Address Acknowledge */
  3333. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  3334. /* Prepare transfer parameters */
  3335. hi2c->pBuffPtr = pData;
  3336. hi2c->XferCount = Size;
  3337. hi2c->XferSize = hi2c->XferCount;
  3338. hi2c->XferOptions = XferOptions;
  3339. hi2c->XferISR = I2C_Slave_ISR_IT;
  3340. if (I2C_GET_DIR(hi2c) == I2C_DIRECTION_TRANSMIT)
  3341. {
  3342. /* Clear ADDR flag after prepare the transfer parameters */
  3343. /* This action will generate an acknowledge to the Master */
  3344. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  3345. }
  3346. /* Process Unlocked */
  3347. __HAL_UNLOCK(hi2c);
  3348. /* Note : The I2C interrupts must be enabled after unlocking current process
  3349. to avoid the risk of I2C interrupt handle execution before current
  3350. process unlock */
  3351. /* REnable ADDR interrupt */
  3352. I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT | I2C_XFER_LISTEN_IT);
  3353. return HAL_OK;
  3354. }
  3355. else
  3356. {
  3357. return HAL_ERROR;
  3358. }
  3359. }
  3360. /**
  3361. * @brief Sequential receive in slave/device I2C mode an amount of data in non-blocking mode with DMA
  3362. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3363. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3364. * the configuration information for the specified I2C.
  3365. * @param pData Pointer to data buffer
  3366. * @param Size Amount of data to be sent
  3367. * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS
  3368. * @retval HAL status
  3369. */
  3370. HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  3371. {
  3372. HAL_StatusTypeDef dmaxferstatus;
  3373. /* Check the parameters */
  3374. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3375. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
  3376. {
  3377. if ((pData == NULL) || (Size == 0U))
  3378. {
  3379. hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM;
  3380. return HAL_ERROR;
  3381. }
  3382. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  3383. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_RX_IT);
  3384. /* Process Locked */
  3385. __HAL_LOCK(hi2c);
  3386. /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */
  3387. /* and then toggle the HAL slave TX state to RX state */
  3388. if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN)
  3389. {
  3390. /* Disable associated Interrupts */
  3391. I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
  3392. if ((hi2c->Instance->CR1 & I2C_CR1_TXDMAEN) == I2C_CR1_TXDMAEN)
  3393. {
  3394. /* Abort DMA Xfer if any */
  3395. if (hi2c->hdmatx != NULL)
  3396. {
  3397. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  3398. /* Set the I2C DMA Abort callback :
  3399. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  3400. hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
  3401. /* Abort DMA TX */
  3402. if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
  3403. {
  3404. /* Call Directly XferAbortCallback function in case of error */
  3405. hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
  3406. }
  3407. }
  3408. }
  3409. }
  3410. else if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN)
  3411. {
  3412. if ((hi2c->Instance->CR1 & I2C_CR1_RXDMAEN) == I2C_CR1_RXDMAEN)
  3413. {
  3414. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  3415. /* Abort DMA Xfer if any */
  3416. if (hi2c->hdmarx != NULL)
  3417. {
  3418. /* Set the I2C DMA Abort callback :
  3419. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  3420. hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
  3421. /* Abort DMA RX */
  3422. if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
  3423. {
  3424. /* Call Directly XferAbortCallback function in case of error */
  3425. hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
  3426. }
  3427. }
  3428. }
  3429. }
  3430. else
  3431. {
  3432. /* Nothing to do */
  3433. }
  3434. hi2c->State = HAL_I2C_STATE_BUSY_RX_LISTEN;
  3435. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  3436. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3437. /* Enable Address Acknowledge */
  3438. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  3439. /* Prepare transfer parameters */
  3440. hi2c->pBuffPtr = pData;
  3441. hi2c->XferCount = Size;
  3442. hi2c->XferSize = hi2c->XferCount;
  3443. hi2c->XferOptions = XferOptions;
  3444. hi2c->XferISR = I2C_Slave_ISR_DMA;
  3445. if (hi2c->hdmarx != NULL)
  3446. {
  3447. /* Set the I2C DMA transfer complete callback */
  3448. hi2c->hdmarx->XferCpltCallback = I2C_DMASlaveReceiveCplt;
  3449. /* Set the DMA error callback */
  3450. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  3451. /* Set the unused DMA callbacks to NULL */
  3452. hi2c->hdmarx->XferHalfCpltCallback = NULL;
  3453. hi2c->hdmarx->XferAbortCallback = NULL;
  3454. /* Enable the DMA channel */
  3455. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, hi2c->XferSize);
  3456. }
  3457. else
  3458. {
  3459. /* Update I2C state */
  3460. hi2c->State = HAL_I2C_STATE_LISTEN;
  3461. hi2c->Mode = HAL_I2C_MODE_NONE;
  3462. /* Update I2C error code */
  3463. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  3464. /* Process Unlocked */
  3465. __HAL_UNLOCK(hi2c);
  3466. return HAL_ERROR;
  3467. }
  3468. if (dmaxferstatus == HAL_OK)
  3469. {
  3470. /* Update XferCount value */
  3471. hi2c->XferCount -= hi2c->XferSize;
  3472. /* Reset XferSize */
  3473. hi2c->XferSize = 0;
  3474. }
  3475. else
  3476. {
  3477. /* Update I2C state */
  3478. hi2c->State = HAL_I2C_STATE_LISTEN;
  3479. hi2c->Mode = HAL_I2C_MODE_NONE;
  3480. /* Update I2C error code */
  3481. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  3482. /* Process Unlocked */
  3483. __HAL_UNLOCK(hi2c);
  3484. return HAL_ERROR;
  3485. }
  3486. if (I2C_GET_DIR(hi2c) == I2C_DIRECTION_TRANSMIT)
  3487. {
  3488. /* Clear ADDR flag after prepare the transfer parameters */
  3489. /* This action will generate an acknowledge to the Master */
  3490. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  3491. }
  3492. /* Process Unlocked */
  3493. __HAL_UNLOCK(hi2c);
  3494. /* Note : The I2C interrupts must be enabled after unlocking current process
  3495. to avoid the risk of I2C interrupt handle execution before current
  3496. process unlock */
  3497. /* REnable ADDR interrupt */
  3498. I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT | I2C_XFER_LISTEN_IT);
  3499. /* Enable DMA Request */
  3500. hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  3501. return HAL_OK;
  3502. }
  3503. else
  3504. {
  3505. return HAL_ERROR;
  3506. }
  3507. }
  3508. /**
  3509. * @brief Enable the Address listen mode with Interrupt.
  3510. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3511. * the configuration information for the specified I2C.
  3512. * @retval HAL status
  3513. */
  3514. HAL_StatusTypeDef HAL_I2C_EnableListen_IT(I2C_HandleTypeDef *hi2c)
  3515. {
  3516. if (hi2c->State == HAL_I2C_STATE_READY)
  3517. {
  3518. hi2c->State = HAL_I2C_STATE_LISTEN;
  3519. hi2c->XferISR = I2C_Slave_ISR_IT;
  3520. /* Enable the Address Match interrupt */
  3521. I2C_Enable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
  3522. return HAL_OK;
  3523. }
  3524. else
  3525. {
  3526. return HAL_BUSY;
  3527. }
  3528. }
  3529. /**
  3530. * @brief Disable the Address listen mode with Interrupt.
  3531. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3532. * the configuration information for the specified I2C
  3533. * @retval HAL status
  3534. */
  3535. HAL_StatusTypeDef HAL_I2C_DisableListen_IT(I2C_HandleTypeDef *hi2c)
  3536. {
  3537. /* Declaration of tmp to prevent undefined behavior of volatile usage */
  3538. uint32_t tmp;
  3539. /* Disable Address listen mode only if a transfer is not ongoing */
  3540. if (hi2c->State == HAL_I2C_STATE_LISTEN)
  3541. {
  3542. tmp = (uint32_t)(hi2c->State) & I2C_STATE_MSK;
  3543. hi2c->PreviousState = tmp | (uint32_t)(hi2c->Mode);
  3544. hi2c->State = HAL_I2C_STATE_READY;
  3545. hi2c->Mode = HAL_I2C_MODE_NONE;
  3546. hi2c->XferISR = NULL;
  3547. /* Disable the Address Match interrupt */
  3548. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
  3549. return HAL_OK;
  3550. }
  3551. else
  3552. {
  3553. return HAL_BUSY;
  3554. }
  3555. }
  3556. /**
  3557. * @brief Abort a master I2C IT or DMA process communication with Interrupt.
  3558. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3559. * the configuration information for the specified I2C.
  3560. * @param DevAddress Target device address: The device 7 bits address value
  3561. * in datasheet must be shifted to the left before calling the interface
  3562. * @retval HAL status
  3563. */
  3564. HAL_StatusTypeDef HAL_I2C_Master_Abort_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress)
  3565. {
  3566. if (hi2c->Mode == HAL_I2C_MODE_MASTER)
  3567. {
  3568. /* Process Locked */
  3569. __HAL_LOCK(hi2c);
  3570. /* Disable Interrupts and Store Previous state */
  3571. if (hi2c->State == HAL_I2C_STATE_BUSY_TX)
  3572. {
  3573. I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
  3574. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX;
  3575. }
  3576. else if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
  3577. {
  3578. I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT);
  3579. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
  3580. }
  3581. else
  3582. {
  3583. /* Do nothing */
  3584. }
  3585. /* Set State at HAL_I2C_STATE_ABORT */
  3586. hi2c->State = HAL_I2C_STATE_ABORT;
  3587. /* Set NBYTES to 1 to generate a dummy read on I2C peripheral */
  3588. /* Set AUTOEND mode, this will generate a NACK then STOP condition to abort the current transfer */
  3589. I2C_TransferConfig(hi2c, DevAddress, 1, I2C_AUTOEND_MODE, I2C_GENERATE_STOP);
  3590. /* Process Unlocked */
  3591. __HAL_UNLOCK(hi2c);
  3592. /* Note : The I2C interrupts must be enabled after unlocking current process
  3593. to avoid the risk of I2C interrupt handle execution before current
  3594. process unlock */
  3595. I2C_Enable_IRQ(hi2c, I2C_XFER_CPLT_IT);
  3596. return HAL_OK;
  3597. }
  3598. else
  3599. {
  3600. /* Wrong usage of abort function */
  3601. /* This function should be used only in case of abort monitored by master device */
  3602. return HAL_ERROR;
  3603. }
  3604. }
  3605. /**
  3606. * @}
  3607. */
  3608. /** @defgroup I2C_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
  3609. * @{
  3610. */
  3611. /**
  3612. * @brief This function handles I2C event interrupt request.
  3613. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3614. * the configuration information for the specified I2C.
  3615. * @retval None
  3616. */
  3617. void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c)
  3618. {
  3619. /* Get current IT Flags and IT sources value */
  3620. uint32_t itflags = READ_REG(hi2c->Instance->ISR);
  3621. uint32_t itsources = READ_REG(hi2c->Instance->CR1);
  3622. /* I2C events treatment -------------------------------------*/
  3623. if (hi2c->XferISR != NULL)
  3624. {
  3625. hi2c->XferISR(hi2c, itflags, itsources);
  3626. }
  3627. }
  3628. /**
  3629. * @brief This function handles I2C error interrupt request.
  3630. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3631. * the configuration information for the specified I2C.
  3632. * @retval None
  3633. */
  3634. void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef *hi2c)
  3635. {
  3636. uint32_t itflags = READ_REG(hi2c->Instance->ISR);
  3637. uint32_t itsources = READ_REG(hi2c->Instance->CR1);
  3638. uint32_t tmperror;
  3639. /* I2C Bus error interrupt occurred ------------------------------------*/
  3640. if ((I2C_CHECK_FLAG(itflags, I2C_FLAG_BERR) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERRI) != RESET))
  3641. {
  3642. hi2c->ErrorCode |= HAL_I2C_ERROR_BERR;
  3643. /* Clear BERR flag */
  3644. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_BERR);
  3645. }
  3646. /* I2C Over-Run/Under-Run interrupt occurred ----------------------------------------*/
  3647. if ((I2C_CHECK_FLAG(itflags, I2C_FLAG_OVR) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERRI) != RESET))
  3648. {
  3649. hi2c->ErrorCode |= HAL_I2C_ERROR_OVR;
  3650. /* Clear OVR flag */
  3651. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_OVR);
  3652. }
  3653. /* I2C Arbitration Loss error interrupt occurred -------------------------------------*/
  3654. if ((I2C_CHECK_FLAG(itflags, I2C_FLAG_ARLO) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERRI) != RESET))
  3655. {
  3656. hi2c->ErrorCode |= HAL_I2C_ERROR_ARLO;
  3657. /* Clear ARLO flag */
  3658. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ARLO);
  3659. }
  3660. /* Store current volatile hi2c->ErrorCode, misra rule */
  3661. tmperror = hi2c->ErrorCode;
  3662. /* Call the Error Callback in case of Error detected */
  3663. if ((tmperror & (HAL_I2C_ERROR_BERR | HAL_I2C_ERROR_OVR | HAL_I2C_ERROR_ARLO)) != HAL_I2C_ERROR_NONE)
  3664. {
  3665. I2C_ITError(hi2c, tmperror);
  3666. }
  3667. }
  3668. /**
  3669. * @brief Master Tx Transfer completed callback.
  3670. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3671. * the configuration information for the specified I2C.
  3672. * @retval None
  3673. */
  3674. __weak void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
  3675. {
  3676. /* Prevent unused argument(s) compilation warning */
  3677. UNUSED(hi2c);
  3678. /* NOTE : This function should not be modified, when the callback is needed,
  3679. the HAL_I2C_MasterTxCpltCallback could be implemented in the user file
  3680. */
  3681. }
  3682. /**
  3683. * @brief Master Rx Transfer completed callback.
  3684. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3685. * the configuration information for the specified I2C.
  3686. * @retval None
  3687. */
  3688. __weak void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
  3689. {
  3690. /* Prevent unused argument(s) compilation warning */
  3691. UNUSED(hi2c);
  3692. /* NOTE : This function should not be modified, when the callback is needed,
  3693. the HAL_I2C_MasterRxCpltCallback could be implemented in the user file
  3694. */
  3695. }
  3696. /** @brief Slave Tx Transfer completed callback.
  3697. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3698. * the configuration information for the specified I2C.
  3699. * @retval None
  3700. */
  3701. __weak void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c)
  3702. {
  3703. /* Prevent unused argument(s) compilation warning */
  3704. UNUSED(hi2c);
  3705. /* NOTE : This function should not be modified, when the callback is needed,
  3706. the HAL_I2C_SlaveTxCpltCallback could be implemented in the user file
  3707. */
  3708. }
  3709. /**
  3710. * @brief Slave Rx Transfer completed callback.
  3711. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3712. * the configuration information for the specified I2C.
  3713. * @retval None
  3714. */
  3715. __weak void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
  3716. {
  3717. /* Prevent unused argument(s) compilation warning */
  3718. UNUSED(hi2c);
  3719. /* NOTE : This function should not be modified, when the callback is needed,
  3720. the HAL_I2C_SlaveRxCpltCallback could be implemented in the user file
  3721. */
  3722. }
  3723. /**
  3724. * @brief Slave Address Match callback.
  3725. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3726. * the configuration information for the specified I2C.
  3727. * @param TransferDirection Master request Transfer Direction (Write/Read), value of @ref I2C_XFERDIRECTION
  3728. * @param AddrMatchCode Address Match Code
  3729. * @retval None
  3730. */
  3731. __weak void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
  3732. {
  3733. /* Prevent unused argument(s) compilation warning */
  3734. UNUSED(hi2c);
  3735. UNUSED(TransferDirection);
  3736. UNUSED(AddrMatchCode);
  3737. /* NOTE : This function should not be modified, when the callback is needed,
  3738. the HAL_I2C_AddrCallback() could be implemented in the user file
  3739. */
  3740. }
  3741. /**
  3742. * @brief Listen Complete callback.
  3743. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3744. * the configuration information for the specified I2C.
  3745. * @retval None
  3746. */
  3747. __weak void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c)
  3748. {
  3749. /* Prevent unused argument(s) compilation warning */
  3750. UNUSED(hi2c);
  3751. /* NOTE : This function should not be modified, when the callback is needed,
  3752. the HAL_I2C_ListenCpltCallback() could be implemented in the user file
  3753. */
  3754. }
  3755. /**
  3756. * @brief Memory Tx Transfer completed callback.
  3757. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3758. * the configuration information for the specified I2C.
  3759. * @retval None
  3760. */
  3761. __weak void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c)
  3762. {
  3763. /* Prevent unused argument(s) compilation warning */
  3764. UNUSED(hi2c);
  3765. /* NOTE : This function should not be modified, when the callback is needed,
  3766. the HAL_I2C_MemTxCpltCallback could be implemented in the user file
  3767. */
  3768. }
  3769. /**
  3770. * @brief Memory Rx Transfer completed callback.
  3771. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3772. * the configuration information for the specified I2C.
  3773. * @retval None
  3774. */
  3775. __weak void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c)
  3776. {
  3777. /* Prevent unused argument(s) compilation warning */
  3778. UNUSED(hi2c);
  3779. /* NOTE : This function should not be modified, when the callback is needed,
  3780. the HAL_I2C_MemRxCpltCallback could be implemented in the user file
  3781. */
  3782. }
  3783. /**
  3784. * @brief I2C error callback.
  3785. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3786. * the configuration information for the specified I2C.
  3787. * @retval None
  3788. */
  3789. __weak void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
  3790. {
  3791. /* Prevent unused argument(s) compilation warning */
  3792. UNUSED(hi2c);
  3793. /* NOTE : This function should not be modified, when the callback is needed,
  3794. the HAL_I2C_ErrorCallback could be implemented in the user file
  3795. */
  3796. }
  3797. /**
  3798. * @brief I2C abort callback.
  3799. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3800. * the configuration information for the specified I2C.
  3801. * @retval None
  3802. */
  3803. __weak void HAL_I2C_AbortCpltCallback(I2C_HandleTypeDef *hi2c)
  3804. {
  3805. /* Prevent unused argument(s) compilation warning */
  3806. UNUSED(hi2c);
  3807. /* NOTE : This function should not be modified, when the callback is needed,
  3808. the HAL_I2C_AbortCpltCallback could be implemented in the user file
  3809. */
  3810. }
  3811. /**
  3812. * @}
  3813. */
  3814. /** @defgroup I2C_Exported_Functions_Group3 Peripheral State, Mode and Error functions
  3815. * @brief Peripheral State, Mode and Error functions
  3816. *
  3817. @verbatim
  3818. ===============================================================================
  3819. ##### Peripheral State, Mode and Error functions #####
  3820. ===============================================================================
  3821. [..]
  3822. This subsection permit to get in run-time the status of the peripheral
  3823. and the data flow.
  3824. @endverbatim
  3825. * @{
  3826. */
  3827. /**
  3828. * @brief Return the I2C handle state.
  3829. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3830. * the configuration information for the specified I2C.
  3831. * @retval HAL state
  3832. */
  3833. HAL_I2C_StateTypeDef HAL_I2C_GetState(I2C_HandleTypeDef *hi2c)
  3834. {
  3835. /* Return I2C handle state */
  3836. return hi2c->State;
  3837. }
  3838. /**
  3839. * @brief Returns the I2C Master, Slave, Memory or no mode.
  3840. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3841. * the configuration information for I2C module
  3842. * @retval HAL mode
  3843. */
  3844. HAL_I2C_ModeTypeDef HAL_I2C_GetMode(I2C_HandleTypeDef *hi2c)
  3845. {
  3846. return hi2c->Mode;
  3847. }
  3848. /**
  3849. * @brief Return the I2C error code.
  3850. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3851. * the configuration information for the specified I2C.
  3852. * @retval I2C Error Code
  3853. */
  3854. uint32_t HAL_I2C_GetError(I2C_HandleTypeDef *hi2c)
  3855. {
  3856. return hi2c->ErrorCode;
  3857. }
  3858. /**
  3859. * @}
  3860. */
  3861. /**
  3862. * @}
  3863. */
  3864. /** @addtogroup I2C_Private_Functions
  3865. * @{
  3866. */
  3867. /**
  3868. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Master Mode with Interrupt.
  3869. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3870. * the configuration information for the specified I2C.
  3871. * @param ITFlags Interrupt flags to handle.
  3872. * @param ITSources Interrupt sources enabled.
  3873. * @retval HAL status
  3874. */
  3875. static HAL_StatusTypeDef I2C_Master_ISR_IT(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags, uint32_t ITSources)
  3876. {
  3877. uint16_t devaddress;
  3878. uint32_t tmpITFlags = ITFlags;
  3879. /* Process Locked */
  3880. __HAL_LOCK(hi2c);
  3881. if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_AF) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET))
  3882. {
  3883. /* Clear NACK Flag */
  3884. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  3885. /* Set corresponding Error Code */
  3886. /* No need to generate STOP, it is automatically done */
  3887. /* Error callback will be send during stop flag treatment */
  3888. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  3889. /* Flush TX register */
  3890. I2C_Flush_TXDR(hi2c);
  3891. }
  3892. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_RXNE) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_RXI) != RESET))
  3893. {
  3894. /* Remove RXNE flag on temporary variable as read done */
  3895. tmpITFlags &= ~I2C_FLAG_RXNE;
  3896. /* Read data from RXDR */
  3897. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
  3898. /* Increment Buffer pointer */
  3899. hi2c->pBuffPtr++;
  3900. hi2c->XferSize--;
  3901. hi2c->XferCount--;
  3902. }
  3903. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TXIS) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TXI) != RESET))
  3904. {
  3905. /* Write data to TXDR */
  3906. hi2c->Instance->TXDR = *hi2c->pBuffPtr;
  3907. /* Increment Buffer pointer */
  3908. hi2c->pBuffPtr++;
  3909. hi2c->XferSize--;
  3910. hi2c->XferCount--;
  3911. }
  3912. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TCR) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
  3913. {
  3914. if ((hi2c->XferCount != 0U) && (hi2c->XferSize == 0U))
  3915. {
  3916. devaddress = (uint16_t)(hi2c->Instance->CR2 & I2C_CR2_SADD);
  3917. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  3918. {
  3919. hi2c->XferSize = MAX_NBYTE_SIZE;
  3920. I2C_TransferConfig(hi2c, devaddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  3921. }
  3922. else
  3923. {
  3924. hi2c->XferSize = hi2c->XferCount;
  3925. if (hi2c->XferOptions != I2C_NO_OPTION_FRAME)
  3926. {
  3927. I2C_TransferConfig(hi2c, devaddress, (uint8_t)hi2c->XferSize, hi2c->XferOptions, I2C_NO_STARTSTOP);
  3928. }
  3929. else
  3930. {
  3931. I2C_TransferConfig(hi2c, devaddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  3932. }
  3933. }
  3934. }
  3935. else
  3936. {
  3937. /* Call TxCpltCallback() if no stop mode is set */
  3938. if (I2C_GET_STOP_MODE(hi2c) != I2C_AUTOEND_MODE)
  3939. {
  3940. /* Call I2C Master Sequential complete process */
  3941. I2C_ITMasterSeqCplt(hi2c);
  3942. }
  3943. else
  3944. {
  3945. /* Wrong size Status regarding TCR flag event */
  3946. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3947. I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE);
  3948. }
  3949. }
  3950. }
  3951. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TC) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
  3952. {
  3953. if (hi2c->XferCount == 0U)
  3954. {
  3955. if (I2C_GET_STOP_MODE(hi2c) != I2C_AUTOEND_MODE)
  3956. {
  3957. /* Generate a stop condition in case of no transfer option */
  3958. if (hi2c->XferOptions == I2C_NO_OPTION_FRAME)
  3959. {
  3960. /* Generate Stop */
  3961. hi2c->Instance->CR2 |= I2C_CR2_STOP;
  3962. }
  3963. else
  3964. {
  3965. /* Call I2C Master Sequential complete process */
  3966. I2C_ITMasterSeqCplt(hi2c);
  3967. }
  3968. }
  3969. }
  3970. else
  3971. {
  3972. /* Wrong size Status regarding TC flag event */
  3973. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3974. I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE);
  3975. }
  3976. }
  3977. else
  3978. {
  3979. /* Nothing to do */
  3980. }
  3981. if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_STOPF) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET))
  3982. {
  3983. /* Call I2C Master complete process */
  3984. I2C_ITMasterCplt(hi2c, tmpITFlags);
  3985. }
  3986. /* Process Unlocked */
  3987. __HAL_UNLOCK(hi2c);
  3988. return HAL_OK;
  3989. }
  3990. /**
  3991. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Slave Mode with Interrupt.
  3992. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3993. * the configuration information for the specified I2C.
  3994. * @param ITFlags Interrupt flags to handle.
  3995. * @param ITSources Interrupt sources enabled.
  3996. * @retval HAL status
  3997. */
  3998. static HAL_StatusTypeDef I2C_Slave_ISR_IT(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags, uint32_t ITSources)
  3999. {
  4000. uint32_t tmpoptions = hi2c->XferOptions;
  4001. uint32_t tmpITFlags = ITFlags;
  4002. /* Process locked */
  4003. __HAL_LOCK(hi2c);
  4004. /* Check if STOPF is set */
  4005. if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_STOPF) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET))
  4006. {
  4007. /* Call I2C Slave complete process */
  4008. I2C_ITSlaveCplt(hi2c, tmpITFlags);
  4009. }
  4010. if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_AF) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET))
  4011. {
  4012. /* Check that I2C transfer finished */
  4013. /* if yes, normal use case, a NACK is sent by the MASTER when Transfer is finished */
  4014. /* Mean XferCount == 0*/
  4015. /* So clear Flag NACKF only */
  4016. if (hi2c->XferCount == 0U)
  4017. {
  4018. if ((hi2c->State == HAL_I2C_STATE_LISTEN) && (tmpoptions == I2C_FIRST_AND_LAST_FRAME)) /* Same action must be done for (tmpoptions == I2C_LAST_FRAME) which removed for Warning[Pa134]: left and right operands are identical */
  4019. {
  4020. /* Call I2C Listen complete process */
  4021. I2C_ITListenCplt(hi2c, tmpITFlags);
  4022. }
  4023. else if ((hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN) && (tmpoptions != I2C_NO_OPTION_FRAME))
  4024. {
  4025. /* Clear NACK Flag */
  4026. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4027. /* Flush TX register */
  4028. I2C_Flush_TXDR(hi2c);
  4029. /* Last Byte is Transmitted */
  4030. /* Call I2C Slave Sequential complete process */
  4031. I2C_ITSlaveSeqCplt(hi2c);
  4032. }
  4033. else
  4034. {
  4035. /* Clear NACK Flag */
  4036. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4037. }
  4038. }
  4039. else
  4040. {
  4041. /* if no, error use case, a Non-Acknowledge of last Data is generated by the MASTER*/
  4042. /* Clear NACK Flag */
  4043. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4044. /* Set ErrorCode corresponding to a Non-Acknowledge */
  4045. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  4046. if ((tmpoptions == I2C_FIRST_FRAME) || (tmpoptions == I2C_NEXT_FRAME))
  4047. {
  4048. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4049. I2C_ITError(hi2c, hi2c->ErrorCode);
  4050. }
  4051. }
  4052. }
  4053. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_RXNE) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_RXI) != RESET))
  4054. {
  4055. if (hi2c->XferCount > 0U)
  4056. {
  4057. /* Read data from RXDR */
  4058. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
  4059. /* Increment Buffer pointer */
  4060. hi2c->pBuffPtr++;
  4061. hi2c->XferSize--;
  4062. hi2c->XferCount--;
  4063. }
  4064. if ((hi2c->XferCount == 0U) && \
  4065. (tmpoptions != I2C_NO_OPTION_FRAME))
  4066. {
  4067. /* Call I2C Slave Sequential complete process */
  4068. I2C_ITSlaveSeqCplt(hi2c);
  4069. }
  4070. }
  4071. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_ADDR) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_ADDRI) != RESET))
  4072. {
  4073. I2C_ITAddrCplt(hi2c, tmpITFlags);
  4074. }
  4075. else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TXIS) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TXI) != RESET))
  4076. {
  4077. /* Write data to TXDR only if XferCount not reach "0" */
  4078. /* A TXIS flag can be set, during STOP treatment */
  4079. /* Check if all data have already been sent */
  4080. /* If it is the case, this last write in TXDR is not sent, correspond to a dummy TXIS event */
  4081. if (hi2c->XferCount > 0U)
  4082. {
  4083. /* Write data to TXDR */
  4084. hi2c->Instance->TXDR = *hi2c->pBuffPtr;
  4085. /* Increment Buffer pointer */
  4086. hi2c->pBuffPtr++;
  4087. hi2c->XferCount--;
  4088. hi2c->XferSize--;
  4089. }
  4090. else
  4091. {
  4092. if ((tmpoptions == I2C_NEXT_FRAME) || (tmpoptions == I2C_FIRST_FRAME))
  4093. {
  4094. /* Last Byte is Transmitted */
  4095. /* Call I2C Slave Sequential complete process */
  4096. I2C_ITSlaveSeqCplt(hi2c);
  4097. }
  4098. }
  4099. }
  4100. else
  4101. {
  4102. /* Nothing to do */
  4103. }
  4104. /* Process Unlocked */
  4105. __HAL_UNLOCK(hi2c);
  4106. return HAL_OK;
  4107. }
  4108. /**
  4109. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Master Mode with DMA.
  4110. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4111. * the configuration information for the specified I2C.
  4112. * @param ITFlags Interrupt flags to handle.
  4113. * @param ITSources Interrupt sources enabled.
  4114. * @retval HAL status
  4115. */
  4116. static HAL_StatusTypeDef I2C_Master_ISR_DMA(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags, uint32_t ITSources)
  4117. {
  4118. uint16_t devaddress;
  4119. uint32_t xfermode;
  4120. /* Process Locked */
  4121. __HAL_LOCK(hi2c);
  4122. if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_AF) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET))
  4123. {
  4124. /* Clear NACK Flag */
  4125. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4126. /* Set corresponding Error Code */
  4127. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  4128. /* No need to generate STOP, it is automatically done */
  4129. /* But enable STOP interrupt, to treat it */
  4130. /* Error callback will be send during stop flag treatment */
  4131. I2C_Enable_IRQ(hi2c, I2C_XFER_CPLT_IT);
  4132. /* Flush TX register */
  4133. I2C_Flush_TXDR(hi2c);
  4134. }
  4135. else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_TCR) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
  4136. {
  4137. /* Disable TC interrupt */
  4138. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_TCI);
  4139. if (hi2c->XferCount != 0U)
  4140. {
  4141. /* Recover Slave address */
  4142. devaddress = (uint16_t)(hi2c->Instance->CR2 & I2C_CR2_SADD);
  4143. /* Prepare the new XferSize to transfer */
  4144. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  4145. {
  4146. hi2c->XferSize = MAX_NBYTE_SIZE;
  4147. xfermode = I2C_RELOAD_MODE;
  4148. }
  4149. else
  4150. {
  4151. hi2c->XferSize = hi2c->XferCount;
  4152. if (hi2c->XferOptions != I2C_NO_OPTION_FRAME)
  4153. {
  4154. xfermode = hi2c->XferOptions;
  4155. }
  4156. else
  4157. {
  4158. xfermode = I2C_AUTOEND_MODE;
  4159. }
  4160. }
  4161. /* Set the new XferSize in Nbytes register */
  4162. I2C_TransferConfig(hi2c, devaddress, (uint8_t)hi2c->XferSize, xfermode, I2C_NO_STARTSTOP);
  4163. /* Update XferCount value */
  4164. hi2c->XferCount -= hi2c->XferSize;
  4165. /* Enable DMA Request */
  4166. if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
  4167. {
  4168. hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  4169. }
  4170. else
  4171. {
  4172. hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  4173. }
  4174. }
  4175. else
  4176. {
  4177. /* Call TxCpltCallback() if no stop mode is set */
  4178. if (I2C_GET_STOP_MODE(hi2c) != I2C_AUTOEND_MODE)
  4179. {
  4180. /* Call I2C Master Sequential complete process */
  4181. I2C_ITMasterSeqCplt(hi2c);
  4182. }
  4183. else
  4184. {
  4185. /* Wrong size Status regarding TCR flag event */
  4186. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4187. I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE);
  4188. }
  4189. }
  4190. }
  4191. else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_TC) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET))
  4192. {
  4193. if (hi2c->XferCount == 0U)
  4194. {
  4195. if (I2C_GET_STOP_MODE(hi2c) != I2C_AUTOEND_MODE)
  4196. {
  4197. /* Generate a stop condition in case of no transfer option */
  4198. if (hi2c->XferOptions == I2C_NO_OPTION_FRAME)
  4199. {
  4200. /* Generate Stop */
  4201. hi2c->Instance->CR2 |= I2C_CR2_STOP;
  4202. }
  4203. else
  4204. {
  4205. /* Call I2C Master Sequential complete process */
  4206. I2C_ITMasterSeqCplt(hi2c);
  4207. }
  4208. }
  4209. }
  4210. else
  4211. {
  4212. /* Wrong size Status regarding TC flag event */
  4213. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4214. I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE);
  4215. }
  4216. }
  4217. else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_STOPF) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET))
  4218. {
  4219. /* Call I2C Master complete process */
  4220. I2C_ITMasterCplt(hi2c, ITFlags);
  4221. }
  4222. else
  4223. {
  4224. /* Nothing to do */
  4225. }
  4226. /* Process Unlocked */
  4227. __HAL_UNLOCK(hi2c);
  4228. return HAL_OK;
  4229. }
  4230. /**
  4231. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Slave Mode with DMA.
  4232. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4233. * the configuration information for the specified I2C.
  4234. * @param ITFlags Interrupt flags to handle.
  4235. * @param ITSources Interrupt sources enabled.
  4236. * @retval HAL status
  4237. */
  4238. static HAL_StatusTypeDef I2C_Slave_ISR_DMA(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags, uint32_t ITSources)
  4239. {
  4240. uint32_t tmpoptions = hi2c->XferOptions;
  4241. uint32_t treatdmanack = 0U;
  4242. HAL_I2C_StateTypeDef tmpstate;
  4243. /* Process locked */
  4244. __HAL_LOCK(hi2c);
  4245. /* Check if STOPF is set */
  4246. if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_STOPF) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET))
  4247. {
  4248. /* Call I2C Slave complete process */
  4249. I2C_ITSlaveCplt(hi2c, ITFlags);
  4250. }
  4251. if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_AF) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET))
  4252. {
  4253. /* Check that I2C transfer finished */
  4254. /* if yes, normal use case, a NACK is sent by the MASTER when Transfer is finished */
  4255. /* Mean XferCount == 0 */
  4256. /* So clear Flag NACKF only */
  4257. if ((I2C_CHECK_IT_SOURCE(ITSources, I2C_CR1_TXDMAEN) != RESET) ||
  4258. (I2C_CHECK_IT_SOURCE(ITSources, I2C_CR1_RXDMAEN) != RESET))
  4259. {
  4260. /* Split check of hdmarx, for MISRA compliance */
  4261. if (hi2c->hdmarx != NULL)
  4262. {
  4263. if (I2C_CHECK_IT_SOURCE(ITSources, I2C_CR1_RXDMAEN) != RESET)
  4264. {
  4265. if (__HAL_DMA_GET_COUNTER(hi2c->hdmarx) == 0U)
  4266. {
  4267. treatdmanack = 1U;
  4268. }
  4269. }
  4270. }
  4271. /* Split check of hdmatx, for MISRA compliance */
  4272. if (hi2c->hdmatx != NULL)
  4273. {
  4274. if (I2C_CHECK_IT_SOURCE(ITSources, I2C_CR1_TXDMAEN) != RESET)
  4275. {
  4276. if (__HAL_DMA_GET_COUNTER(hi2c->hdmatx) == 0U)
  4277. {
  4278. treatdmanack = 1U;
  4279. }
  4280. }
  4281. }
  4282. if (treatdmanack == 1U)
  4283. {
  4284. if ((hi2c->State == HAL_I2C_STATE_LISTEN) && (tmpoptions == I2C_FIRST_AND_LAST_FRAME)) /* Same action must be done for (tmpoptions == I2C_LAST_FRAME) which removed for Warning[Pa134]: left and right operands are identical */
  4285. {
  4286. /* Call I2C Listen complete process */
  4287. I2C_ITListenCplt(hi2c, ITFlags);
  4288. }
  4289. else if ((hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN) && (tmpoptions != I2C_NO_OPTION_FRAME))
  4290. {
  4291. /* Clear NACK Flag */
  4292. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4293. /* Flush TX register */
  4294. I2C_Flush_TXDR(hi2c);
  4295. /* Last Byte is Transmitted */
  4296. /* Call I2C Slave Sequential complete process */
  4297. I2C_ITSlaveSeqCplt(hi2c);
  4298. }
  4299. else
  4300. {
  4301. /* Clear NACK Flag */
  4302. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4303. }
  4304. }
  4305. else
  4306. {
  4307. /* if no, error use case, a Non-Acknowledge of last Data is generated by the MASTER*/
  4308. /* Clear NACK Flag */
  4309. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4310. /* Set ErrorCode corresponding to a Non-Acknowledge */
  4311. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  4312. /* Store current hi2c->State, solve MISRA2012-Rule-13.5 */
  4313. tmpstate = hi2c->State;
  4314. if ((tmpoptions == I2C_FIRST_FRAME) || (tmpoptions == I2C_NEXT_FRAME))
  4315. {
  4316. if ((tmpstate == HAL_I2C_STATE_BUSY_TX) || (tmpstate == HAL_I2C_STATE_BUSY_TX_LISTEN))
  4317. {
  4318. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX;
  4319. }
  4320. else if ((tmpstate == HAL_I2C_STATE_BUSY_RX) || (tmpstate == HAL_I2C_STATE_BUSY_RX_LISTEN))
  4321. {
  4322. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_RX;
  4323. }
  4324. else
  4325. {
  4326. /* Do nothing */
  4327. }
  4328. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4329. I2C_ITError(hi2c, hi2c->ErrorCode);
  4330. }
  4331. }
  4332. }
  4333. else
  4334. {
  4335. /* Only Clear NACK Flag, no DMA treatment is pending */
  4336. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4337. }
  4338. }
  4339. else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_ADDR) != RESET) && (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_ADDRI) != RESET))
  4340. {
  4341. I2C_ITAddrCplt(hi2c, ITFlags);
  4342. }
  4343. else
  4344. {
  4345. /* Nothing to do */
  4346. }
  4347. /* Process Unlocked */
  4348. __HAL_UNLOCK(hi2c);
  4349. return HAL_OK;
  4350. }
  4351. /**
  4352. * @brief Master sends target device address followed by internal memory address for write request.
  4353. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4354. * the configuration information for the specified I2C.
  4355. * @param DevAddress Target device address: The device 7 bits address value
  4356. * in datasheet must be shifted to the left before calling the interface
  4357. * @param MemAddress Internal memory address
  4358. * @param MemAddSize Size of internal memory address
  4359. * @param Timeout Timeout duration
  4360. * @param Tickstart Tick start value
  4361. * @retval HAL status
  4362. */
  4363. static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart)
  4364. {
  4365. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)MemAddSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
  4366. /* Wait until TXIS flag is set */
  4367. if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
  4368. {
  4369. return HAL_ERROR;
  4370. }
  4371. /* If Memory address size is 8Bit */
  4372. if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
  4373. {
  4374. /* Send Memory Address */
  4375. hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
  4376. }
  4377. /* If Memory address size is 16Bit */
  4378. else
  4379. {
  4380. /* Send MSB of Memory Address */
  4381. hi2c->Instance->TXDR = I2C_MEM_ADD_MSB(MemAddress);
  4382. /* Wait until TXIS flag is set */
  4383. if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
  4384. {
  4385. return HAL_ERROR;
  4386. }
  4387. /* Send LSB of Memory Address */
  4388. hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
  4389. }
  4390. /* Wait until TCR flag is set */
  4391. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout, Tickstart) != HAL_OK)
  4392. {
  4393. return HAL_ERROR;
  4394. }
  4395. return HAL_OK;
  4396. }
  4397. /**
  4398. * @brief Master sends target device address followed by internal memory address for read request.
  4399. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4400. * the configuration information for the specified I2C.
  4401. * @param DevAddress Target device address: The device 7 bits address value
  4402. * in datasheet must be shifted to the left before calling the interface
  4403. * @param MemAddress Internal memory address
  4404. * @param MemAddSize Size of internal memory address
  4405. * @param Timeout Timeout duration
  4406. * @param Tickstart Tick start value
  4407. * @retval HAL status
  4408. */
  4409. static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart)
  4410. {
  4411. I2C_TransferConfig(hi2c, DevAddress, (uint8_t)MemAddSize, I2C_SOFTEND_MODE, I2C_GENERATE_START_WRITE);
  4412. /* Wait until TXIS flag is set */
  4413. if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
  4414. {
  4415. return HAL_ERROR;
  4416. }
  4417. /* If Memory address size is 8Bit */
  4418. if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
  4419. {
  4420. /* Send Memory Address */
  4421. hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
  4422. }
  4423. /* If Memory address size is 16Bit */
  4424. else
  4425. {
  4426. /* Send MSB of Memory Address */
  4427. hi2c->Instance->TXDR = I2C_MEM_ADD_MSB(MemAddress);
  4428. /* Wait until TXIS flag is set */
  4429. if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
  4430. {
  4431. return HAL_ERROR;
  4432. }
  4433. /* Send LSB of Memory Address */
  4434. hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
  4435. }
  4436. /* Wait until TC flag is set */
  4437. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TC, RESET, Timeout, Tickstart) != HAL_OK)
  4438. {
  4439. return HAL_ERROR;
  4440. }
  4441. return HAL_OK;
  4442. }
  4443. /**
  4444. * @brief I2C Address complete process callback.
  4445. * @param hi2c I2C handle.
  4446. * @param ITFlags Interrupt flags to handle.
  4447. * @retval None
  4448. */
  4449. static void I2C_ITAddrCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags)
  4450. {
  4451. uint8_t transferdirection;
  4452. uint16_t slaveaddrcode;
  4453. uint16_t ownadd1code;
  4454. uint16_t ownadd2code;
  4455. /* Prevent unused argument(s) compilation warning */
  4456. UNUSED(ITFlags);
  4457. /* In case of Listen state, need to inform upper layer of address match code event */
  4458. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
  4459. {
  4460. transferdirection = I2C_GET_DIR(hi2c);
  4461. slaveaddrcode = I2C_GET_ADDR_MATCH(hi2c);
  4462. ownadd1code = I2C_GET_OWN_ADDRESS1(hi2c);
  4463. ownadd2code = I2C_GET_OWN_ADDRESS2(hi2c);
  4464. /* If 10bits addressing mode is selected */
  4465. if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
  4466. {
  4467. if ((slaveaddrcode & SlaveAddr_MSK) == ((ownadd1code >> SlaveAddr_SHIFT) & SlaveAddr_MSK))
  4468. {
  4469. slaveaddrcode = ownadd1code;
  4470. hi2c->AddrEventCount++;
  4471. if (hi2c->AddrEventCount == 2U)
  4472. {
  4473. /* Reset Address Event counter */
  4474. hi2c->AddrEventCount = 0U;
  4475. /* Clear ADDR flag */
  4476. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  4477. /* Process Unlocked */
  4478. __HAL_UNLOCK(hi2c);
  4479. /* Call Slave Addr callback */
  4480. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4481. hi2c->AddrCallback(hi2c, transferdirection, slaveaddrcode);
  4482. #else
  4483. HAL_I2C_AddrCallback(hi2c, transferdirection, slaveaddrcode);
  4484. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4485. }
  4486. }
  4487. else
  4488. {
  4489. slaveaddrcode = ownadd2code;
  4490. /* Disable ADDR Interrupts */
  4491. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
  4492. /* Process Unlocked */
  4493. __HAL_UNLOCK(hi2c);
  4494. /* Call Slave Addr callback */
  4495. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4496. hi2c->AddrCallback(hi2c, transferdirection, slaveaddrcode);
  4497. #else
  4498. HAL_I2C_AddrCallback(hi2c, transferdirection, slaveaddrcode);
  4499. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4500. }
  4501. }
  4502. /* else 7 bits addressing mode is selected */
  4503. else
  4504. {
  4505. /* Disable ADDR Interrupts */
  4506. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT);
  4507. /* Process Unlocked */
  4508. __HAL_UNLOCK(hi2c);
  4509. /* Call Slave Addr callback */
  4510. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4511. hi2c->AddrCallback(hi2c, transferdirection, slaveaddrcode);
  4512. #else
  4513. HAL_I2C_AddrCallback(hi2c, transferdirection, slaveaddrcode);
  4514. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4515. }
  4516. }
  4517. /* Else clear address flag only */
  4518. else
  4519. {
  4520. /* Clear ADDR flag */
  4521. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  4522. /* Process Unlocked */
  4523. __HAL_UNLOCK(hi2c);
  4524. }
  4525. }
  4526. /**
  4527. * @brief I2C Master sequential complete process.
  4528. * @param hi2c I2C handle.
  4529. * @retval None
  4530. */
  4531. static void I2C_ITMasterSeqCplt(I2C_HandleTypeDef *hi2c)
  4532. {
  4533. /* Reset I2C handle mode */
  4534. hi2c->Mode = HAL_I2C_MODE_NONE;
  4535. /* No Generate Stop, to permit restart mode */
  4536. /* The stop will be done at the end of transfer, when I2C_AUTOEND_MODE enable */
  4537. if (hi2c->State == HAL_I2C_STATE_BUSY_TX)
  4538. {
  4539. hi2c->State = HAL_I2C_STATE_READY;
  4540. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX;
  4541. hi2c->XferISR = NULL;
  4542. /* Disable Interrupts */
  4543. I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
  4544. /* Process Unlocked */
  4545. __HAL_UNLOCK(hi2c);
  4546. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4547. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4548. hi2c->MasterTxCpltCallback(hi2c);
  4549. #else
  4550. HAL_I2C_MasterTxCpltCallback(hi2c);
  4551. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4552. }
  4553. /* hi2c->State == HAL_I2C_STATE_BUSY_RX */
  4554. else
  4555. {
  4556. hi2c->State = HAL_I2C_STATE_READY;
  4557. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
  4558. hi2c->XferISR = NULL;
  4559. /* Disable Interrupts */
  4560. I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT);
  4561. /* Process Unlocked */
  4562. __HAL_UNLOCK(hi2c);
  4563. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4564. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4565. hi2c->MasterRxCpltCallback(hi2c);
  4566. #else
  4567. HAL_I2C_MasterRxCpltCallback(hi2c);
  4568. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4569. }
  4570. }
  4571. /**
  4572. * @brief I2C Slave sequential complete process.
  4573. * @param hi2c I2C handle.
  4574. * @retval None
  4575. */
  4576. static void I2C_ITSlaveSeqCplt(I2C_HandleTypeDef *hi2c)
  4577. {
  4578. uint32_t tmpcr1value = READ_REG(hi2c->Instance->CR1);
  4579. /* Reset I2C handle mode */
  4580. hi2c->Mode = HAL_I2C_MODE_NONE;
  4581. /* If a DMA is ongoing, Update handle size context */
  4582. if (I2C_CHECK_IT_SOURCE(tmpcr1value, I2C_CR1_TXDMAEN) != RESET)
  4583. {
  4584. /* Disable DMA Request */
  4585. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  4586. }
  4587. else if (I2C_CHECK_IT_SOURCE(tmpcr1value, I2C_CR1_RXDMAEN) != RESET)
  4588. {
  4589. /* Disable DMA Request */
  4590. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  4591. }
  4592. else
  4593. {
  4594. /* Do nothing */
  4595. }
  4596. if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN)
  4597. {
  4598. /* Remove HAL_I2C_STATE_SLAVE_BUSY_TX, keep only HAL_I2C_STATE_LISTEN */
  4599. hi2c->State = HAL_I2C_STATE_LISTEN;
  4600. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX;
  4601. /* Disable Interrupts */
  4602. I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
  4603. /* Process Unlocked */
  4604. __HAL_UNLOCK(hi2c);
  4605. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4606. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4607. hi2c->SlaveTxCpltCallback(hi2c);
  4608. #else
  4609. HAL_I2C_SlaveTxCpltCallback(hi2c);
  4610. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4611. }
  4612. else if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN)
  4613. {
  4614. /* Remove HAL_I2C_STATE_SLAVE_BUSY_RX, keep only HAL_I2C_STATE_LISTEN */
  4615. hi2c->State = HAL_I2C_STATE_LISTEN;
  4616. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_RX;
  4617. /* Disable Interrupts */
  4618. I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT);
  4619. /* Process Unlocked */
  4620. __HAL_UNLOCK(hi2c);
  4621. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4622. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4623. hi2c->SlaveRxCpltCallback(hi2c);
  4624. #else
  4625. HAL_I2C_SlaveRxCpltCallback(hi2c);
  4626. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4627. }
  4628. else
  4629. {
  4630. /* Nothing to do */
  4631. }
  4632. }
  4633. /**
  4634. * @brief I2C Master complete process.
  4635. * @param hi2c I2C handle.
  4636. * @param ITFlags Interrupt flags to handle.
  4637. * @retval None
  4638. */
  4639. static void I2C_ITMasterCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags)
  4640. {
  4641. uint32_t tmperror;
  4642. uint32_t tmpITFlags = ITFlags;
  4643. __IO uint32_t tmpreg;
  4644. /* Clear STOP Flag */
  4645. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  4646. /* Disable Interrupts and Store Previous state */
  4647. if (hi2c->State == HAL_I2C_STATE_BUSY_TX)
  4648. {
  4649. I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT);
  4650. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX;
  4651. }
  4652. else if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
  4653. {
  4654. I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT);
  4655. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
  4656. }
  4657. else
  4658. {
  4659. /* Do nothing */
  4660. }
  4661. /* Clear Configuration Register 2 */
  4662. I2C_RESET_CR2(hi2c);
  4663. /* Reset handle parameters */
  4664. hi2c->XferISR = NULL;
  4665. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  4666. if (I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_AF) != RESET)
  4667. {
  4668. /* Clear NACK Flag */
  4669. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4670. /* Set acknowledge error code */
  4671. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  4672. }
  4673. /* Fetch Last receive data if any */
  4674. if ((hi2c->State == HAL_I2C_STATE_ABORT) && (I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_RXNE) != RESET))
  4675. {
  4676. /* Read data from RXDR */
  4677. tmpreg = (uint8_t)hi2c->Instance->RXDR;
  4678. UNUSED(tmpreg);
  4679. }
  4680. /* Flush TX register */
  4681. I2C_Flush_TXDR(hi2c);
  4682. /* Store current volatile hi2c->ErrorCode, misra rule */
  4683. tmperror = hi2c->ErrorCode;
  4684. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4685. if ((hi2c->State == HAL_I2C_STATE_ABORT) || (tmperror != HAL_I2C_ERROR_NONE))
  4686. {
  4687. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4688. I2C_ITError(hi2c, hi2c->ErrorCode);
  4689. }
  4690. /* hi2c->State == HAL_I2C_STATE_BUSY_TX */
  4691. else if (hi2c->State == HAL_I2C_STATE_BUSY_TX)
  4692. {
  4693. hi2c->State = HAL_I2C_STATE_READY;
  4694. hi2c->PreviousState = I2C_STATE_NONE;
  4695. if (hi2c->Mode == HAL_I2C_MODE_MEM)
  4696. {
  4697. hi2c->Mode = HAL_I2C_MODE_NONE;
  4698. /* Process Unlocked */
  4699. __HAL_UNLOCK(hi2c);
  4700. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4701. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4702. hi2c->MemTxCpltCallback(hi2c);
  4703. #else
  4704. HAL_I2C_MemTxCpltCallback(hi2c);
  4705. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4706. }
  4707. else
  4708. {
  4709. hi2c->Mode = HAL_I2C_MODE_NONE;
  4710. /* Process Unlocked */
  4711. __HAL_UNLOCK(hi2c);
  4712. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4713. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4714. hi2c->MasterTxCpltCallback(hi2c);
  4715. #else
  4716. HAL_I2C_MasterTxCpltCallback(hi2c);
  4717. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4718. }
  4719. }
  4720. /* hi2c->State == HAL_I2C_STATE_BUSY_RX */
  4721. else if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
  4722. {
  4723. hi2c->State = HAL_I2C_STATE_READY;
  4724. hi2c->PreviousState = I2C_STATE_NONE;
  4725. if (hi2c->Mode == HAL_I2C_MODE_MEM)
  4726. {
  4727. hi2c->Mode = HAL_I2C_MODE_NONE;
  4728. /* Process Unlocked */
  4729. __HAL_UNLOCK(hi2c);
  4730. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4731. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4732. hi2c->MemRxCpltCallback(hi2c);
  4733. #else
  4734. HAL_I2C_MemRxCpltCallback(hi2c);
  4735. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4736. }
  4737. else
  4738. {
  4739. hi2c->Mode = HAL_I2C_MODE_NONE;
  4740. /* Process Unlocked */
  4741. __HAL_UNLOCK(hi2c);
  4742. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4743. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4744. hi2c->MasterRxCpltCallback(hi2c);
  4745. #else
  4746. HAL_I2C_MasterRxCpltCallback(hi2c);
  4747. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4748. }
  4749. }
  4750. else
  4751. {
  4752. /* Nothing to do */
  4753. }
  4754. }
  4755. /**
  4756. * @brief I2C Slave complete process.
  4757. * @param hi2c I2C handle.
  4758. * @param ITFlags Interrupt flags to handle.
  4759. * @retval None
  4760. */
  4761. static void I2C_ITSlaveCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags)
  4762. {
  4763. uint32_t tmpcr1value = READ_REG(hi2c->Instance->CR1);
  4764. uint32_t tmpITFlags = ITFlags;
  4765. HAL_I2C_StateTypeDef tmpstate = hi2c->State;
  4766. /* Clear STOP Flag */
  4767. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  4768. /* Disable Interrupts and Store Previous state */
  4769. if ((tmpstate == HAL_I2C_STATE_BUSY_TX) || (tmpstate == HAL_I2C_STATE_BUSY_TX_LISTEN))
  4770. {
  4771. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_TX_IT);
  4772. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX;
  4773. }
  4774. else if ((tmpstate == HAL_I2C_STATE_BUSY_RX) || (tmpstate == HAL_I2C_STATE_BUSY_RX_LISTEN))
  4775. {
  4776. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_RX_IT);
  4777. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_RX;
  4778. }
  4779. else
  4780. {
  4781. /* Do nothing */
  4782. }
  4783. /* Disable Address Acknowledge */
  4784. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  4785. /* Clear Configuration Register 2 */
  4786. I2C_RESET_CR2(hi2c);
  4787. /* Flush TX register */
  4788. I2C_Flush_TXDR(hi2c);
  4789. /* If a DMA is ongoing, Update handle size context */
  4790. if (I2C_CHECK_IT_SOURCE(tmpcr1value, I2C_CR1_TXDMAEN) != RESET)
  4791. {
  4792. /* Disable DMA Request */
  4793. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  4794. if (hi2c->hdmatx != NULL)
  4795. {
  4796. hi2c->XferCount = (uint16_t)__HAL_DMA_GET_COUNTER(hi2c->hdmatx);
  4797. }
  4798. }
  4799. else if (I2C_CHECK_IT_SOURCE(tmpcr1value, I2C_CR1_RXDMAEN) != RESET)
  4800. {
  4801. /* Disable DMA Request */
  4802. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  4803. if (hi2c->hdmarx != NULL)
  4804. {
  4805. hi2c->XferCount = (uint16_t)__HAL_DMA_GET_COUNTER(hi2c->hdmarx);
  4806. }
  4807. }
  4808. else
  4809. {
  4810. /* Do nothing */
  4811. }
  4812. /* Store Last receive data if any */
  4813. if (I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_RXNE) != RESET)
  4814. {
  4815. /* Remove RXNE flag on temporary variable as read done */
  4816. tmpITFlags &= ~I2C_FLAG_RXNE;
  4817. /* Read data from RXDR */
  4818. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
  4819. /* Increment Buffer pointer */
  4820. hi2c->pBuffPtr++;
  4821. if ((hi2c->XferSize > 0U))
  4822. {
  4823. hi2c->XferSize--;
  4824. hi2c->XferCount--;
  4825. }
  4826. }
  4827. /* All data are not transferred, so set error code accordingly */
  4828. if (hi2c->XferCount != 0U)
  4829. {
  4830. /* Set ErrorCode corresponding to a Non-Acknowledge */
  4831. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  4832. }
  4833. hi2c->Mode = HAL_I2C_MODE_NONE;
  4834. hi2c->XferISR = NULL;
  4835. if (hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  4836. {
  4837. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4838. I2C_ITError(hi2c, hi2c->ErrorCode);
  4839. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  4840. if (hi2c->State == HAL_I2C_STATE_LISTEN)
  4841. {
  4842. /* Call I2C Listen complete process */
  4843. I2C_ITListenCplt(hi2c, tmpITFlags);
  4844. }
  4845. }
  4846. else if (hi2c->XferOptions != I2C_NO_OPTION_FRAME)
  4847. {
  4848. /* Call the Sequential Complete callback, to inform upper layer of the end of Transfer */
  4849. I2C_ITSlaveSeqCplt(hi2c);
  4850. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  4851. hi2c->State = HAL_I2C_STATE_READY;
  4852. hi2c->PreviousState = I2C_STATE_NONE;
  4853. /* Process Unlocked */
  4854. __HAL_UNLOCK(hi2c);
  4855. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  4856. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4857. hi2c->ListenCpltCallback(hi2c);
  4858. #else
  4859. HAL_I2C_ListenCpltCallback(hi2c);
  4860. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4861. }
  4862. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4863. else if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
  4864. {
  4865. hi2c->State = HAL_I2C_STATE_READY;
  4866. hi2c->PreviousState = I2C_STATE_NONE;
  4867. /* Process Unlocked */
  4868. __HAL_UNLOCK(hi2c);
  4869. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4870. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4871. hi2c->SlaveRxCpltCallback(hi2c);
  4872. #else
  4873. HAL_I2C_SlaveRxCpltCallback(hi2c);
  4874. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4875. }
  4876. else
  4877. {
  4878. hi2c->State = HAL_I2C_STATE_READY;
  4879. hi2c->PreviousState = I2C_STATE_NONE;
  4880. /* Process Unlocked */
  4881. __HAL_UNLOCK(hi2c);
  4882. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4883. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4884. hi2c->SlaveTxCpltCallback(hi2c);
  4885. #else
  4886. HAL_I2C_SlaveTxCpltCallback(hi2c);
  4887. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4888. }
  4889. }
  4890. /**
  4891. * @brief I2C Listen complete process.
  4892. * @param hi2c I2C handle.
  4893. * @param ITFlags Interrupt flags to handle.
  4894. * @retval None
  4895. */
  4896. static void I2C_ITListenCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags)
  4897. {
  4898. /* Reset handle parameters */
  4899. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  4900. hi2c->PreviousState = I2C_STATE_NONE;
  4901. hi2c->State = HAL_I2C_STATE_READY;
  4902. hi2c->Mode = HAL_I2C_MODE_NONE;
  4903. hi2c->XferISR = NULL;
  4904. /* Store Last receive data if any */
  4905. if (I2C_CHECK_FLAG(ITFlags, I2C_FLAG_RXNE) != RESET)
  4906. {
  4907. /* Read data from RXDR */
  4908. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR;
  4909. /* Increment Buffer pointer */
  4910. hi2c->pBuffPtr++;
  4911. if ((hi2c->XferSize > 0U))
  4912. {
  4913. hi2c->XferSize--;
  4914. hi2c->XferCount--;
  4915. /* Set ErrorCode corresponding to a Non-Acknowledge */
  4916. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  4917. }
  4918. }
  4919. /* Disable all Interrupts*/
  4920. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_RX_IT | I2C_XFER_TX_IT);
  4921. /* Clear NACK Flag */
  4922. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4923. /* Process Unlocked */
  4924. __HAL_UNLOCK(hi2c);
  4925. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  4926. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4927. hi2c->ListenCpltCallback(hi2c);
  4928. #else
  4929. HAL_I2C_ListenCpltCallback(hi2c);
  4930. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4931. }
  4932. /**
  4933. * @brief I2C interrupts error process.
  4934. * @param hi2c I2C handle.
  4935. * @param ErrorCode Error code to handle.
  4936. * @retval None
  4937. */
  4938. static void I2C_ITError(I2C_HandleTypeDef *hi2c, uint32_t ErrorCode)
  4939. {
  4940. HAL_I2C_StateTypeDef tmpstate = hi2c->State;
  4941. uint32_t tmppreviousstate;
  4942. /* Reset handle parameters */
  4943. hi2c->Mode = HAL_I2C_MODE_NONE;
  4944. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  4945. hi2c->XferCount = 0U;
  4946. /* Set new error code */
  4947. hi2c->ErrorCode |= ErrorCode;
  4948. /* Disable Interrupts */
  4949. if ((tmpstate == HAL_I2C_STATE_LISTEN) ||
  4950. (tmpstate == HAL_I2C_STATE_BUSY_TX_LISTEN) ||
  4951. (tmpstate == HAL_I2C_STATE_BUSY_RX_LISTEN))
  4952. {
  4953. /* Disable all interrupts, except interrupts related to LISTEN state */
  4954. I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT | I2C_XFER_TX_IT);
  4955. /* keep HAL_I2C_STATE_LISTEN if set */
  4956. hi2c->State = HAL_I2C_STATE_LISTEN;
  4957. hi2c->XferISR = I2C_Slave_ISR_IT;
  4958. }
  4959. else
  4960. {
  4961. /* Disable all interrupts */
  4962. I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_RX_IT | I2C_XFER_TX_IT);
  4963. /* If state is an abort treatment on going, don't change state */
  4964. /* This change will be do later */
  4965. if (hi2c->State != HAL_I2C_STATE_ABORT)
  4966. {
  4967. /* Set HAL_I2C_STATE_READY */
  4968. hi2c->State = HAL_I2C_STATE_READY;
  4969. }
  4970. hi2c->XferISR = NULL;
  4971. }
  4972. /* Abort DMA TX transfer if any */
  4973. tmppreviousstate = hi2c->PreviousState;
  4974. if ((hi2c->hdmatx != NULL) && ((tmppreviousstate == I2C_STATE_MASTER_BUSY_TX) || (tmppreviousstate == I2C_STATE_SLAVE_BUSY_TX)))
  4975. {
  4976. if ((hi2c->Instance->CR1 & I2C_CR1_TXDMAEN) == I2C_CR1_TXDMAEN)
  4977. {
  4978. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  4979. }
  4980. if (HAL_DMA_GetState(hi2c->hdmatx) != HAL_DMA_STATE_READY)
  4981. {
  4982. /* Set the I2C DMA Abort callback :
  4983. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  4984. hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
  4985. /* Process Unlocked */
  4986. __HAL_UNLOCK(hi2c);
  4987. /* Abort DMA TX */
  4988. if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
  4989. {
  4990. /* Call Directly XferAbortCallback function in case of error */
  4991. hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
  4992. }
  4993. }
  4994. else
  4995. {
  4996. I2C_TreatErrorCallback(hi2c);
  4997. }
  4998. }
  4999. /* Abort DMA RX transfer if any */
  5000. else if ((hi2c->hdmarx != NULL) && ((tmppreviousstate == I2C_STATE_MASTER_BUSY_RX) || (tmppreviousstate == I2C_STATE_SLAVE_BUSY_RX)))
  5001. {
  5002. if ((hi2c->Instance->CR1 & I2C_CR1_RXDMAEN) == I2C_CR1_RXDMAEN)
  5003. {
  5004. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  5005. }
  5006. if (HAL_DMA_GetState(hi2c->hdmarx) != HAL_DMA_STATE_READY)
  5007. {
  5008. /* Set the I2C DMA Abort callback :
  5009. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  5010. hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
  5011. /* Process Unlocked */
  5012. __HAL_UNLOCK(hi2c);
  5013. /* Abort DMA RX */
  5014. if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
  5015. {
  5016. /* Call Directly hi2c->hdmarx->XferAbortCallback function in case of error */
  5017. hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
  5018. }
  5019. }
  5020. else
  5021. {
  5022. I2C_TreatErrorCallback(hi2c);
  5023. }
  5024. }
  5025. else
  5026. {
  5027. I2C_TreatErrorCallback(hi2c);
  5028. }
  5029. }
  5030. /**
  5031. * @brief I2C Error callback treatment.
  5032. * @param hi2c I2C handle.
  5033. * @retval None
  5034. */
  5035. static void I2C_TreatErrorCallback(I2C_HandleTypeDef *hi2c)
  5036. {
  5037. if (hi2c->State == HAL_I2C_STATE_ABORT)
  5038. {
  5039. hi2c->State = HAL_I2C_STATE_READY;
  5040. hi2c->PreviousState = I2C_STATE_NONE;
  5041. /* Process Unlocked */
  5042. __HAL_UNLOCK(hi2c);
  5043. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5044. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5045. hi2c->AbortCpltCallback(hi2c);
  5046. #else
  5047. HAL_I2C_AbortCpltCallback(hi2c);
  5048. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5049. }
  5050. else
  5051. {
  5052. hi2c->PreviousState = I2C_STATE_NONE;
  5053. /* Process Unlocked */
  5054. __HAL_UNLOCK(hi2c);
  5055. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5056. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5057. hi2c->ErrorCallback(hi2c);
  5058. #else
  5059. HAL_I2C_ErrorCallback(hi2c);
  5060. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5061. }
  5062. }
  5063. /**
  5064. * @brief I2C Tx data register flush process.
  5065. * @param hi2c I2C handle.
  5066. * @retval None
  5067. */
  5068. static void I2C_Flush_TXDR(I2C_HandleTypeDef *hi2c)
  5069. {
  5070. /* If a pending TXIS flag is set */
  5071. /* Write a dummy data in TXDR to clear it */
  5072. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) != RESET)
  5073. {
  5074. hi2c->Instance->TXDR = 0x00U;
  5075. }
  5076. /* Flush TX register if not empty */
  5077. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXE) == RESET)
  5078. {
  5079. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_TXE);
  5080. }
  5081. }
  5082. /**
  5083. * @brief DMA I2C master transmit process complete callback.
  5084. * @param hdma DMA handle
  5085. * @retval None
  5086. */
  5087. static void I2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma)
  5088. {
  5089. I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
  5090. /* Disable DMA Request */
  5091. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  5092. /* If last transfer, enable STOP interrupt */
  5093. if (hi2c->XferCount == 0U)
  5094. {
  5095. /* Enable STOP interrupt */
  5096. I2C_Enable_IRQ(hi2c, I2C_XFER_CPLT_IT);
  5097. }
  5098. /* else prepare a new DMA transfer and enable TCReload interrupt */
  5099. else
  5100. {
  5101. /* Update Buffer pointer */
  5102. hi2c->pBuffPtr += hi2c->XferSize;
  5103. /* Set the XferSize to transfer */
  5104. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  5105. {
  5106. hi2c->XferSize = MAX_NBYTE_SIZE;
  5107. }
  5108. else
  5109. {
  5110. hi2c->XferSize = hi2c->XferCount;
  5111. }
  5112. /* Enable the DMA channel */
  5113. if (HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize) != HAL_OK)
  5114. {
  5115. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5116. I2C_ITError(hi2c, HAL_I2C_ERROR_DMA);
  5117. }
  5118. else
  5119. {
  5120. /* Enable TC interrupts */
  5121. I2C_Enable_IRQ(hi2c, I2C_XFER_RELOAD_IT);
  5122. }
  5123. }
  5124. }
  5125. /**
  5126. * @brief DMA I2C slave transmit process complete callback.
  5127. * @param hdma DMA handle
  5128. * @retval None
  5129. */
  5130. static void I2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma)
  5131. {
  5132. I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
  5133. uint32_t tmpoptions = hi2c->XferOptions;
  5134. if ((tmpoptions == I2C_NEXT_FRAME) || (tmpoptions == I2C_FIRST_FRAME))
  5135. {
  5136. /* Disable DMA Request */
  5137. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  5138. /* Last Byte is Transmitted */
  5139. /* Call I2C Slave Sequential complete process */
  5140. I2C_ITSlaveSeqCplt(hi2c);
  5141. }
  5142. else
  5143. {
  5144. /* No specific action, Master fully manage the generation of STOP condition */
  5145. /* Mean that this generation can arrive at any time, at the end or during DMA process */
  5146. /* So STOP condition should be manage through Interrupt treatment */
  5147. }
  5148. }
  5149. /**
  5150. * @brief DMA I2C master receive process complete callback.
  5151. * @param hdma DMA handle
  5152. * @retval None
  5153. */
  5154. static void I2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma)
  5155. {
  5156. I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
  5157. /* Disable DMA Request */
  5158. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  5159. /* If last transfer, enable STOP interrupt */
  5160. if (hi2c->XferCount == 0U)
  5161. {
  5162. /* Enable STOP interrupt */
  5163. I2C_Enable_IRQ(hi2c, I2C_XFER_CPLT_IT);
  5164. }
  5165. /* else prepare a new DMA transfer and enable TCReload interrupt */
  5166. else
  5167. {
  5168. /* Update Buffer pointer */
  5169. hi2c->pBuffPtr += hi2c->XferSize;
  5170. /* Set the XferSize to transfer */
  5171. if (hi2c->XferCount > MAX_NBYTE_SIZE)
  5172. {
  5173. hi2c->XferSize = MAX_NBYTE_SIZE;
  5174. }
  5175. else
  5176. {
  5177. hi2c->XferSize = hi2c->XferCount;
  5178. }
  5179. /* Enable the DMA channel */
  5180. if (HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize) != HAL_OK)
  5181. {
  5182. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5183. I2C_ITError(hi2c, HAL_I2C_ERROR_DMA);
  5184. }
  5185. else
  5186. {
  5187. /* Enable TC interrupts */
  5188. I2C_Enable_IRQ(hi2c, I2C_XFER_RELOAD_IT);
  5189. }
  5190. }
  5191. }
  5192. /**
  5193. * @brief DMA I2C slave receive process complete callback.
  5194. * @param hdma DMA handle
  5195. * @retval None
  5196. */
  5197. static void I2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma)
  5198. {
  5199. I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
  5200. uint32_t tmpoptions = hi2c->XferOptions;
  5201. if ((__HAL_DMA_GET_COUNTER(hi2c->hdmarx) == 0U) && \
  5202. (tmpoptions != I2C_NO_OPTION_FRAME))
  5203. {
  5204. /* Disable DMA Request */
  5205. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  5206. /* Call I2C Slave Sequential complete process */
  5207. I2C_ITSlaveSeqCplt(hi2c);
  5208. }
  5209. else
  5210. {
  5211. /* No specific action, Master fully manage the generation of STOP condition */
  5212. /* Mean that this generation can arrive at any time, at the end or during DMA process */
  5213. /* So STOP condition should be manage through Interrupt treatment */
  5214. }
  5215. }
  5216. /**
  5217. * @brief DMA I2C communication error callback.
  5218. * @param hdma DMA handle
  5219. * @retval None
  5220. */
  5221. static void I2C_DMAError(DMA_HandleTypeDef *hdma)
  5222. {
  5223. I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
  5224. /* Disable Acknowledge */
  5225. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  5226. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5227. I2C_ITError(hi2c, HAL_I2C_ERROR_DMA);
  5228. }
  5229. /**
  5230. * @brief DMA I2C communication abort callback
  5231. * (To be called at end of DMA Abort procedure).
  5232. * @param hdma DMA handle.
  5233. * @retval None
  5234. */
  5235. static void I2C_DMAAbort(DMA_HandleTypeDef *hdma)
  5236. {
  5237. I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
  5238. /* Reset AbortCpltCallback */
  5239. if (hi2c->hdmatx != NULL)
  5240. {
  5241. hi2c->hdmatx->XferAbortCallback = NULL;
  5242. }
  5243. if (hi2c->hdmarx != NULL)
  5244. {
  5245. hi2c->hdmarx->XferAbortCallback = NULL;
  5246. }
  5247. I2C_TreatErrorCallback(hi2c);
  5248. }
  5249. /**
  5250. * @brief This function handles I2C Communication Timeout.
  5251. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5252. * the configuration information for the specified I2C.
  5253. * @param Flag Specifies the I2C flag to check.
  5254. * @param Status The new Flag status (SET or RESET).
  5255. * @param Timeout Timeout duration
  5256. * @param Tickstart Tick start value
  5257. * @retval HAL status
  5258. */
  5259. static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t Tickstart)
  5260. {
  5261. while (__HAL_I2C_GET_FLAG(hi2c, Flag) == Status)
  5262. {
  5263. /* Check for the Timeout */
  5264. if (Timeout != HAL_MAX_DELAY)
  5265. {
  5266. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  5267. {
  5268. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  5269. hi2c->State = HAL_I2C_STATE_READY;
  5270. hi2c->Mode = HAL_I2C_MODE_NONE;
  5271. /* Process Unlocked */
  5272. __HAL_UNLOCK(hi2c);
  5273. return HAL_ERROR;
  5274. }
  5275. }
  5276. }
  5277. return HAL_OK;
  5278. }
  5279. /**
  5280. * @brief This function handles I2C Communication Timeout for specific usage of TXIS flag.
  5281. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5282. * the configuration information for the specified I2C.
  5283. * @param Timeout Timeout duration
  5284. * @param Tickstart Tick start value
  5285. * @retval HAL status
  5286. */
  5287. static HAL_StatusTypeDef I2C_WaitOnTXISFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
  5288. {
  5289. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == RESET)
  5290. {
  5291. /* Check if a NACK is detected */
  5292. if (I2C_IsAcknowledgeFailed(hi2c, Timeout, Tickstart) != HAL_OK)
  5293. {
  5294. return HAL_ERROR;
  5295. }
  5296. /* Check for the Timeout */
  5297. if (Timeout != HAL_MAX_DELAY)
  5298. {
  5299. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  5300. {
  5301. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  5302. hi2c->State = HAL_I2C_STATE_READY;
  5303. hi2c->Mode = HAL_I2C_MODE_NONE;
  5304. /* Process Unlocked */
  5305. __HAL_UNLOCK(hi2c);
  5306. return HAL_ERROR;
  5307. }
  5308. }
  5309. }
  5310. return HAL_OK;
  5311. }
  5312. /**
  5313. * @brief This function handles I2C Communication Timeout for specific usage of STOP flag.
  5314. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5315. * the configuration information for the specified I2C.
  5316. * @param Timeout Timeout duration
  5317. * @param Tickstart Tick start value
  5318. * @retval HAL status
  5319. */
  5320. static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
  5321. {
  5322. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
  5323. {
  5324. /* Check if a NACK is detected */
  5325. if (I2C_IsAcknowledgeFailed(hi2c, Timeout, Tickstart) != HAL_OK)
  5326. {
  5327. return HAL_ERROR;
  5328. }
  5329. /* Check for the Timeout */
  5330. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  5331. {
  5332. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  5333. hi2c->State = HAL_I2C_STATE_READY;
  5334. hi2c->Mode = HAL_I2C_MODE_NONE;
  5335. /* Process Unlocked */
  5336. __HAL_UNLOCK(hi2c);
  5337. return HAL_ERROR;
  5338. }
  5339. }
  5340. return HAL_OK;
  5341. }
  5342. /**
  5343. * @brief This function handles I2C Communication Timeout for specific usage of RXNE flag.
  5344. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5345. * the configuration information for the specified I2C.
  5346. * @param Timeout Timeout duration
  5347. * @param Tickstart Tick start value
  5348. * @retval HAL status
  5349. */
  5350. static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
  5351. {
  5352. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == RESET)
  5353. {
  5354. /* Check if a NACK is detected */
  5355. if (I2C_IsAcknowledgeFailed(hi2c, Timeout, Tickstart) != HAL_OK)
  5356. {
  5357. return HAL_ERROR;
  5358. }
  5359. /* Check if a STOPF is detected */
  5360. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
  5361. {
  5362. /* Check if an RXNE is pending */
  5363. /* Store Last receive data if any */
  5364. if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET) && (hi2c->XferSize > 0U))
  5365. {
  5366. /* Return HAL_OK */
  5367. /* The Reading of data from RXDR will be done in caller function */
  5368. return HAL_OK;
  5369. }
  5370. else
  5371. {
  5372. /* Clear STOP Flag */
  5373. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  5374. /* Clear Configuration Register 2 */
  5375. I2C_RESET_CR2(hi2c);
  5376. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  5377. hi2c->State = HAL_I2C_STATE_READY;
  5378. hi2c->Mode = HAL_I2C_MODE_NONE;
  5379. /* Process Unlocked */
  5380. __HAL_UNLOCK(hi2c);
  5381. return HAL_ERROR;
  5382. }
  5383. }
  5384. /* Check for the Timeout */
  5385. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  5386. {
  5387. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  5388. hi2c->State = HAL_I2C_STATE_READY;
  5389. /* Process Unlocked */
  5390. __HAL_UNLOCK(hi2c);
  5391. return HAL_ERROR;
  5392. }
  5393. }
  5394. return HAL_OK;
  5395. }
  5396. /**
  5397. * @brief This function handles Acknowledge failed detection during an I2C Communication.
  5398. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5399. * the configuration information for the specified I2C.
  5400. * @param Timeout Timeout duration
  5401. * @param Tickstart Tick start value
  5402. * @retval HAL status
  5403. */
  5404. static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
  5405. {
  5406. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
  5407. {
  5408. /* Wait until STOP Flag is reset */
  5409. /* AutoEnd should be initiate after AF */
  5410. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
  5411. {
  5412. /* Check for the Timeout */
  5413. if (Timeout != HAL_MAX_DELAY)
  5414. {
  5415. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  5416. {
  5417. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  5418. hi2c->State = HAL_I2C_STATE_READY;
  5419. hi2c->Mode = HAL_I2C_MODE_NONE;
  5420. /* Process Unlocked */
  5421. __HAL_UNLOCK(hi2c);
  5422. return HAL_ERROR;
  5423. }
  5424. }
  5425. }
  5426. /* Clear NACKF Flag */
  5427. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  5428. /* Clear STOP Flag */
  5429. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  5430. /* Flush TX register */
  5431. I2C_Flush_TXDR(hi2c);
  5432. /* Clear Configuration Register 2 */
  5433. I2C_RESET_CR2(hi2c);
  5434. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  5435. hi2c->State = HAL_I2C_STATE_READY;
  5436. hi2c->Mode = HAL_I2C_MODE_NONE;
  5437. /* Process Unlocked */
  5438. __HAL_UNLOCK(hi2c);
  5439. return HAL_ERROR;
  5440. }
  5441. return HAL_OK;
  5442. }
  5443. /**
  5444. * @brief Handles I2Cx communication when starting transfer or during transfer (TC or TCR flag are set).
  5445. * @param hi2c I2C handle.
  5446. * @param DevAddress Specifies the slave address to be programmed.
  5447. * @param Size Specifies the number of bytes to be programmed.
  5448. * This parameter must be a value between 0 and 255.
  5449. * @param Mode New state of the I2C START condition generation.
  5450. * This parameter can be one of the following values:
  5451. * @arg @ref I2C_RELOAD_MODE Enable Reload mode .
  5452. * @arg @ref I2C_AUTOEND_MODE Enable Automatic end mode.
  5453. * @arg @ref I2C_SOFTEND_MODE Enable Software end mode.
  5454. * @param Request New state of the I2C START condition generation.
  5455. * This parameter can be one of the following values:
  5456. * @arg @ref I2C_NO_STARTSTOP Don't Generate stop and start condition.
  5457. * @arg @ref I2C_GENERATE_STOP Generate stop condition (Size should be set to 0).
  5458. * @arg @ref I2C_GENERATE_START_READ Generate Restart for read request.
  5459. * @arg @ref I2C_GENERATE_START_WRITE Generate Restart for write request.
  5460. * @retval None
  5461. */
  5462. static void I2C_TransferConfig(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t Size, uint32_t Mode, uint32_t Request)
  5463. {
  5464. /* Check the parameters */
  5465. assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  5466. assert_param(IS_TRANSFER_MODE(Mode));
  5467. assert_param(IS_TRANSFER_REQUEST(Request));
  5468. /* update CR2 register */
  5469. MODIFY_REG(hi2c->Instance->CR2, ((I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | (I2C_CR2_RD_WRN & (uint32_t)(Request >> (31U - I2C_CR2_RD_WRN_Pos))) | I2C_CR2_START | I2C_CR2_STOP)), \
  5470. (uint32_t)(((uint32_t)DevAddress & I2C_CR2_SADD) | (((uint32_t)Size << I2C_CR2_NBYTES_Pos) & I2C_CR2_NBYTES) | (uint32_t)Mode | (uint32_t)Request));
  5471. }
  5472. /**
  5473. * @brief Manage the enabling of Interrupts.
  5474. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5475. * the configuration information for the specified I2C.
  5476. * @param InterruptRequest Value of @ref I2C_Interrupt_configuration_definition.
  5477. * @retval None
  5478. */
  5479. static void I2C_Enable_IRQ(I2C_HandleTypeDef *hi2c, uint16_t InterruptRequest)
  5480. {
  5481. uint32_t tmpisr = 0U;
  5482. if ((hi2c->XferISR == I2C_Master_ISR_DMA) || \
  5483. (hi2c->XferISR == I2C_Slave_ISR_DMA))
  5484. {
  5485. if ((InterruptRequest & I2C_XFER_LISTEN_IT) == I2C_XFER_LISTEN_IT)
  5486. {
  5487. /* Enable ERR, STOP, NACK and ADDR interrupts */
  5488. tmpisr |= I2C_IT_ADDRI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ERRI;
  5489. }
  5490. if (InterruptRequest == I2C_XFER_ERROR_IT)
  5491. {
  5492. /* Enable ERR and NACK interrupts */
  5493. tmpisr |= I2C_IT_ERRI | I2C_IT_NACKI;
  5494. }
  5495. if (InterruptRequest == I2C_XFER_CPLT_IT)
  5496. {
  5497. /* Enable STOP interrupts */
  5498. tmpisr |= (I2C_IT_STOPI | I2C_IT_TCI);
  5499. }
  5500. if (InterruptRequest == I2C_XFER_RELOAD_IT)
  5501. {
  5502. /* Enable TC interrupts */
  5503. tmpisr |= I2C_IT_TCI;
  5504. }
  5505. }
  5506. else
  5507. {
  5508. if ((InterruptRequest & I2C_XFER_LISTEN_IT) == I2C_XFER_LISTEN_IT)
  5509. {
  5510. /* Enable ERR, STOP, NACK, and ADDR interrupts */
  5511. tmpisr |= I2C_IT_ADDRI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ERRI;
  5512. }
  5513. if ((InterruptRequest & I2C_XFER_TX_IT) == I2C_XFER_TX_IT)
  5514. {
  5515. /* Enable ERR, TC, STOP, NACK and RXI interrupts */
  5516. tmpisr |= I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_TXI;
  5517. }
  5518. if ((InterruptRequest & I2C_XFER_RX_IT) == I2C_XFER_RX_IT)
  5519. {
  5520. /* Enable ERR, TC, STOP, NACK and TXI interrupts */
  5521. tmpisr |= I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_RXI;
  5522. }
  5523. if (InterruptRequest == I2C_XFER_CPLT_IT)
  5524. {
  5525. /* Enable STOP interrupts */
  5526. tmpisr |= I2C_IT_STOPI;
  5527. }
  5528. }
  5529. /* Enable interrupts only at the end */
  5530. /* to avoid the risk of I2C interrupt handle execution before */
  5531. /* all interrupts requested done */
  5532. __HAL_I2C_ENABLE_IT(hi2c, tmpisr);
  5533. }
  5534. /**
  5535. * @brief Manage the disabling of Interrupts.
  5536. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5537. * the configuration information for the specified I2C.
  5538. * @param InterruptRequest Value of @ref I2C_Interrupt_configuration_definition.
  5539. * @retval None
  5540. */
  5541. static void I2C_Disable_IRQ(I2C_HandleTypeDef *hi2c, uint16_t InterruptRequest)
  5542. {
  5543. uint32_t tmpisr = 0U;
  5544. if ((InterruptRequest & I2C_XFER_TX_IT) == I2C_XFER_TX_IT)
  5545. {
  5546. /* Disable TC and TXI interrupts */
  5547. tmpisr |= I2C_IT_TCI | I2C_IT_TXI;
  5548. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) != (uint32_t)HAL_I2C_STATE_LISTEN)
  5549. {
  5550. /* Disable NACK and STOP interrupts */
  5551. tmpisr |= I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ERRI;
  5552. }
  5553. }
  5554. if ((InterruptRequest & I2C_XFER_RX_IT) == I2C_XFER_RX_IT)
  5555. {
  5556. /* Disable TC and RXI interrupts */
  5557. tmpisr |= I2C_IT_TCI | I2C_IT_RXI;
  5558. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) != (uint32_t)HAL_I2C_STATE_LISTEN)
  5559. {
  5560. /* Disable NACK and STOP interrupts */
  5561. tmpisr |= I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ERRI;
  5562. }
  5563. }
  5564. if ((InterruptRequest & I2C_XFER_LISTEN_IT) == I2C_XFER_LISTEN_IT)
  5565. {
  5566. /* Disable ADDR, NACK and STOP interrupts */
  5567. tmpisr |= I2C_IT_ADDRI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ERRI;
  5568. }
  5569. if (InterruptRequest == I2C_XFER_ERROR_IT)
  5570. {
  5571. /* Enable ERR and NACK interrupts */
  5572. tmpisr |= I2C_IT_ERRI | I2C_IT_NACKI;
  5573. }
  5574. if (InterruptRequest == I2C_XFER_CPLT_IT)
  5575. {
  5576. /* Enable STOP interrupts */
  5577. tmpisr |= I2C_IT_STOPI;
  5578. }
  5579. if (InterruptRequest == I2C_XFER_RELOAD_IT)
  5580. {
  5581. /* Enable TC interrupts */
  5582. tmpisr |= I2C_IT_TCI;
  5583. }
  5584. /* Disable interrupts only at the end */
  5585. /* to avoid a breaking situation like at "t" time */
  5586. /* all disable interrupts request are not done */
  5587. __HAL_I2C_DISABLE_IT(hi2c, tmpisr);
  5588. }
  5589. /**
  5590. * @brief Convert I2Cx OTHER_xxx XferOptions to functional XferOptions.
  5591. * @param hi2c I2C handle.
  5592. * @retval None
  5593. */
  5594. static void I2C_ConvertOtherXferOptions(I2C_HandleTypeDef *hi2c)
  5595. {
  5596. /* if user set XferOptions to I2C_OTHER_FRAME */
  5597. /* it request implicitly to generate a restart condition */
  5598. /* set XferOptions to I2C_FIRST_FRAME */
  5599. if (hi2c->XferOptions == I2C_OTHER_FRAME)
  5600. {
  5601. hi2c->XferOptions = I2C_FIRST_FRAME;
  5602. }
  5603. /* else if user set XferOptions to I2C_OTHER_AND_LAST_FRAME */
  5604. /* it request implicitly to generate a restart condition */
  5605. /* then generate a stop condition at the end of transfer */
  5606. /* set XferOptions to I2C_FIRST_AND_LAST_FRAME */
  5607. else if (hi2c->XferOptions == I2C_OTHER_AND_LAST_FRAME)
  5608. {
  5609. hi2c->XferOptions = I2C_FIRST_AND_LAST_FRAME;
  5610. }
  5611. else
  5612. {
  5613. /* Nothing to do */
  5614. }
  5615. }
  5616. /**
  5617. * @}
  5618. */
  5619. #endif /* HAL_I2C_MODULE_ENABLED */
  5620. /**
  5621. * @}
  5622. */
  5623. /**
  5624. * @}
  5625. */
  5626. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/