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.
 
 
 

4111 lines
119 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_i2c.c
  4. * @author MCD Application Team
  5. * @version V1.0.1
  6. * @date 25-June-2015
  7. * @brief I2C HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Inter Integrated Circuit (I2C) peripheral:
  10. * + Initialization and de-initialization functions
  11. * + IO operation functions
  12. * + Peripheral State and Errors functions
  13. *
  14. @verbatim
  15. ==============================================================================
  16. ##### How to use this driver #####
  17. ==============================================================================
  18. [..]
  19. The I2C HAL driver can be used as follows:
  20. (#) Declare a I2C_HandleTypeDef handle structure, for example:
  21. I2C_HandleTypeDef hi2c;
  22. (#)Initialize the I2C low level resources by implement the HAL_I2C_MspInit ()API:
  23. (##) Enable the I2Cx interface clock
  24. (##) I2C pins configuration
  25. (+++) Enable the clock for the I2C GPIOs
  26. (+++) Configure I2C pins as alternate function open-drain
  27. (##) NVIC configuration if you need to use interrupt process
  28. (+++) Configure the I2Cx interrupt priority
  29. (+++) Enable the NVIC I2C IRQ Channel
  30. (##) DMA Configuration if you need to use DMA process
  31. (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive stream
  32. (+++) Enable the DMAx interface clock using
  33. (+++) Configure the DMA handle parameters
  34. (+++) Configure the DMA Tx or Rx Stream
  35. (+++) Associate the initialized DMA handle to the hi2c DMA Tx or Rx handle
  36. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx or Rx Stream
  37. (#) Configure the Communication Clock Timing, Own Address1, Master Addressing Mode, Dual Addressing mode,
  38. Own Address2, Own Address2 Mask, General call and Nostretch mode in the hi2c Init structure.
  39. (#) Initialize the I2C registers by calling the HAL_I2C_Init(), configures also the low level Hardware
  40. (GPIO, CLOCK, NVIC...etc) by calling the customed HAL_I2C_MspInit(&hi2c) API.
  41. (#) To check if target device is ready for communication, use the function HAL_I2C_IsDeviceReady()
  42. (#) For I2C IO and IO MEM operations, three operation modes are available within this driver :
  43. *** Polling mode IO operation ***
  44. =================================
  45. [..]
  46. (+) Transmit in master mode an amount of data in blocking mode using HAL_I2C_Master_Transmit()
  47. (+) Receive in master mode an amount of data in blocking mode using HAL_I2C_Master_Receive()
  48. (+) Transmit in slave mode an amount of data in blocking mode using HAL_I2C_Slave_Transmit()
  49. (+) Receive in slave mode an amount of data in blocking mode using HAL_I2C_Slave_Receive()
  50. *** Polling mode IO MEM operation ***
  51. =====================================
  52. [..]
  53. (+) Write an amount of data in blocking mode to a specific memory address using HAL_I2C_Mem_Write()
  54. (+) Read an amount of data in blocking mode from a specific memory address using HAL_I2C_Mem_Read()
  55. *** Interrupt mode IO operation ***
  56. ===================================
  57. [..]
  58. (+) Transmit in master mode an amount of data in non blocking mode using HAL_I2C_Master_Transmit_IT()
  59. (+) At transmission end of transfer HAL_I2C_MasterTxCpltCallback is executed and user can
  60. add his own code by customization of function pointer HAL_I2C_MasterTxCpltCallback
  61. (+) Receive in master mode an amount of data in non blocking mode using HAL_I2C_Master_Receive_IT()
  62. (+) At reception end of transfer HAL_I2C_MasterRxCpltCallback is executed and user can
  63. add his own code by customization of function pointer HAL_I2C_MasterRxCpltCallback
  64. (+) Transmit in slave mode an amount of data in non blocking mode using HAL_I2C_Slave_Transmit_IT()
  65. (+) At transmission end of transfer HAL_I2C_SlaveTxCpltCallback is executed and user can
  66. add his own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback
  67. (+) Receive in slave mode an amount of data in non blocking mode using HAL_I2C_Slave_Receive_IT()
  68. (+) At reception end of transfer HAL_I2C_SlaveRxCpltCallback is executed and user can
  69. add his own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback
  70. (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
  71. add his own code by customization of function pointer HAL_I2C_ErrorCallback
  72. *** Interrupt mode IO MEM operation ***
  73. =======================================
  74. [..]
  75. (+) Write an amount of data in no-blocking mode with Interrupt to a specific memory address using
  76. HAL_I2C_Mem_Write_IT()
  77. (+) At MEM end of write transfer HAL_I2C_MemTxCpltCallback is executed and user can
  78. add his own code by customization of function pointer HAL_I2C_MemTxCpltCallback
  79. (+) Read an amount of data in no-blocking mode with Interrupt from a specific memory address using
  80. HAL_I2C_Mem_Read_IT()
  81. (+) At MEM end of read transfer HAL_I2C_MemRxCpltCallback is executed and user can
  82. add his own code by customization of function pointer HAL_I2C_MemRxCpltCallback
  83. (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
  84. add his own code by customization of function pointer HAL_I2C_ErrorCallback
  85. *** DMA mode IO operation ***
  86. ==============================
  87. [..]
  88. (+) Transmit in master mode an amount of data in non blocking mode (DMA) using
  89. HAL_I2C_Master_Transmit_DMA()
  90. (+) At transmission end of transfer HAL_I2C_MasterTxCpltCallback is executed and user can
  91. add his own code by customization of function pointer HAL_I2C_MasterTxCpltCallback
  92. (+) Receive in master mode an amount of data in non blocking mode (DMA) using
  93. HAL_I2C_Master_Receive_DMA()
  94. (+) At reception end of transfer HAL_I2C_MasterRxCpltCallback is executed and user can
  95. add his own code by customization of function pointer HAL_I2C_MasterRxCpltCallback
  96. (+) Transmit in slave mode an amount of data in non blocking mode (DMA) using
  97. HAL_I2C_Slave_Transmit_DMA()
  98. (+) At transmission end of transfer HAL_I2C_SlaveTxCpltCallback is executed and user can
  99. add his own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback
  100. (+) Receive in slave mode an amount of data in non blocking mode (DMA) using
  101. HAL_I2C_Slave_Receive_DMA()
  102. (+) At reception end of transfer HAL_I2C_SlaveRxCpltCallback is executed and user can
  103. add his own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback
  104. (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
  105. add his own code by customization of function pointer HAL_I2C_ErrorCallback
  106. *** DMA mode IO MEM operation ***
  107. =================================
  108. [..]
  109. (+) Write an amount of data in no-blocking mode with DMA to a specific memory address using
  110. HAL_I2C_Mem_Write_DMA()
  111. (+) At MEM end of write transfer HAL_I2C_MemTxCpltCallback is executed and user can
  112. add his own code by customization of function pointer HAL_I2C_MemTxCpltCallback
  113. (+) Read an amount of data in no-blocking mode with DMA from a specific memory address using
  114. HAL_I2C_Mem_Read_DMA()
  115. (+) At MEM end of read transfer HAL_I2C_MemRxCpltCallback is executed and user can
  116. add his own code by customization of function pointer HAL_I2C_MemRxCpltCallback
  117. (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
  118. add his own code by customization of function pointer HAL_I2C_ErrorCallback
  119. *** I2C HAL driver macros list ***
  120. ==================================
  121. [..]
  122. Below the list of most used macros in I2C HAL driver.
  123. (+) __HAL_I2C_ENABLE: Enable the I2C peripheral
  124. (+) __HAL_I2C_DISABLE: Disable the I2C peripheral
  125. (+) __HAL_I2C_GET_FLAG : Checks whether the specified I2C flag is set or not
  126. (+) __HAL_I2C_CLEAR_FLAG : Clear the specified I2C pending flag
  127. (+) __HAL_I2C_ENABLE_IT: Enable the specified I2C interrupt
  128. (+) __HAL_I2C_DISABLE_IT: Disable the specified I2C interrupt
  129. [..]
  130. (@) You can refer to the I2C HAL driver header file for more useful macros
  131. @endverbatim
  132. ******************************************************************************
  133. * @attention
  134. *
  135. * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  136. *
  137. * Redistribution and use in source and binary forms, with or without modification,
  138. * are permitted provided that the following conditions are met:
  139. * 1. Redistributions of source code must retain the above copyright notice,
  140. * this list of conditions and the following disclaimer.
  141. * 2. Redistributions in binary form must reproduce the above copyright notice,
  142. * this list of conditions and the following disclaimer in the documentation
  143. * and/or other materials provided with the distribution.
  144. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  145. * may be used to endorse or promote products derived from this software
  146. * without specific prior written permission.
  147. *
  148. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  149. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  150. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  151. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  152. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  153. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  154. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  155. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  156. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  157. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  158. *
  159. ******************************************************************************
  160. */
  161. /* Includes ------------------------------------------------------------------*/
  162. #include "stm32f7xx_hal.h"
  163. /** @addtogroup STM32F7xx_HAL_Driver
  164. * @{
  165. */
  166. /** @defgroup I2C I2C
  167. * @brief I2C HAL module driver
  168. * @{
  169. */
  170. #ifdef HAL_I2C_MODULE_ENABLED
  171. /* Private typedef -----------------------------------------------------------*/
  172. /* Private constants ---------------------------------------------------------*/
  173. /** @addtogroup I2C_Private_Constants I2C Private Constants
  174. * @{
  175. */
  176. #define TIMING_CLEAR_MASK ((uint32_t)0xF0FFFFFF) /*<! I2C TIMING clear register Mask */
  177. #define I2C_TIMEOUT_ADDR ((uint32_t)10000) /* 10 s */
  178. #define I2C_TIMEOUT_BUSY ((uint32_t)25) /* 25 ms */
  179. #define I2C_TIMEOUT_DIR ((uint32_t)25) /* 25 ms */
  180. #define I2C_TIMEOUT_RXNE ((uint32_t)25) /* 25 ms */
  181. #define I2C_TIMEOUT_STOPF ((uint32_t)25) /* 25 ms */
  182. #define I2C_TIMEOUT_TC ((uint32_t)25) /* 25 ms */
  183. #define I2C_TIMEOUT_TCR ((uint32_t)25) /* 25 ms */
  184. #define I2C_TIMEOUT_TXIS ((uint32_t)25) /* 25 ms */
  185. #define I2C_TIMEOUT_FLAG ((uint32_t)25) /* 25 ms */
  186. /**
  187. * @}
  188. */
  189. /* Private macro -------------------------------------------------------------*/
  190. /* Private variables ---------------------------------------------------------*/
  191. /* Private function prototypes -----------------------------------------------*/
  192. /** @addtogroup I2C_Private_Functions I2C Private Functions
  193. * @{
  194. */
  195. static void I2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma);
  196. static void I2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma);
  197. static void I2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma);
  198. static void I2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma);
  199. static void I2C_DMAMemTransmitCplt(DMA_HandleTypeDef *hdma);
  200. static void I2C_DMAMemReceiveCplt(DMA_HandleTypeDef *hdma);
  201. static void I2C_DMAError(DMA_HandleTypeDef *hdma);
  202. static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout);
  203. static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout);
  204. static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
  205. static HAL_StatusTypeDef I2C_WaitOnTXISFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
  206. static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
  207. static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
  208. static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
  209. static HAL_StatusTypeDef I2C_MasterTransmit_ISR(I2C_HandleTypeDef *hi2c);
  210. static HAL_StatusTypeDef I2C_MasterReceive_ISR(I2C_HandleTypeDef *hi2c);
  211. static HAL_StatusTypeDef I2C_SlaveTransmit_ISR(I2C_HandleTypeDef *hi2c);
  212. static HAL_StatusTypeDef I2C_SlaveReceive_ISR(I2C_HandleTypeDef *hi2c);
  213. static void I2C_TransferConfig(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t Size, uint32_t Mode, uint32_t Request);
  214. /**
  215. * @}
  216. */
  217. /* Exported functions --------------------------------------------------------*/
  218. /** @defgroup I2C_Exported_Functions I2C Exported Functions
  219. * @{
  220. */
  221. /** @defgroup I2C_Exported_Functions_Group1 Initialization and de-initialization functions
  222. * @brief Initialization and Configuration functions
  223. *
  224. @verbatim
  225. ===============================================================================
  226. ##### Initialization and de-initialization functions #####
  227. ===============================================================================
  228. [..] This subsection provides a set of functions allowing to initialize and
  229. de-initialize the I2Cx peripheral:
  230. (+) User must Implement HAL_I2C_MspInit() function in which he configures
  231. all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
  232. (+) Call the function HAL_I2C_Init() to configure the selected device with
  233. the selected configuration:
  234. (++) Clock Timing
  235. (++) Own Address 1
  236. (++) Addressing mode (Master, Slave)
  237. (++) Dual Addressing mode
  238. (++) Own Address 2
  239. (++) Own Address 2 Mask
  240. (++) General call mode
  241. (++) Nostretch mode
  242. (+) Call the function HAL_I2C_DeInit() to restore the default configuration
  243. of the selected I2Cx peripheral.
  244. @endverbatim
  245. * @{
  246. */
  247. /**
  248. * @brief Initializes the I2C according to the specified parameters
  249. * in the I2C_InitTypeDef and create the associated handle.
  250. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  251. * the configuration information for the specified I2C.
  252. * @retval HAL status
  253. */
  254. HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c)
  255. {
  256. /* Check the I2C handle allocation */
  257. if(hi2c == NULL)
  258. {
  259. return HAL_ERROR;
  260. }
  261. /* Check the parameters */
  262. assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  263. assert_param(IS_I2C_OWN_ADDRESS1(hi2c->Init.OwnAddress1));
  264. assert_param(IS_I2C_ADDRESSING_MODE(hi2c->Init.AddressingMode));
  265. assert_param(IS_I2C_DUAL_ADDRESS(hi2c->Init.DualAddressMode));
  266. assert_param(IS_I2C_OWN_ADDRESS2(hi2c->Init.OwnAddress2));
  267. assert_param(IS_I2C_OWN_ADDRESS2_MASK(hi2c->Init.OwnAddress2Masks));
  268. assert_param(IS_I2C_GENERAL_CALL(hi2c->Init.GeneralCallMode));
  269. assert_param(IS_I2C_NO_STRETCH(hi2c->Init.NoStretchMode));
  270. if(hi2c->State == HAL_I2C_STATE_RESET)
  271. {
  272. /* Allocate lock resource and initialize it */
  273. hi2c->Lock = HAL_UNLOCKED;
  274. /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  275. HAL_I2C_MspInit(hi2c);
  276. }
  277. hi2c->State = HAL_I2C_STATE_BUSY;
  278. /* Disable the selected I2C peripheral */
  279. __HAL_I2C_DISABLE(hi2c);
  280. /*---------------------------- I2Cx TIMINGR Configuration ------------------*/
  281. /* Configure I2Cx: Frequency range */
  282. hi2c->Instance->TIMINGR = hi2c->Init.Timing & TIMING_CLEAR_MASK;
  283. /*---------------------------- I2Cx OAR1 Configuration ---------------------*/
  284. /* Configure I2Cx: Own Address1 and ack own address1 mode */
  285. hi2c->Instance->OAR1 &= ~I2C_OAR1_OA1EN;
  286. if(hi2c->Init.OwnAddress1 != 0)
  287. {
  288. if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
  289. {
  290. hi2c->Instance->OAR1 = (I2C_OAR1_OA1EN | hi2c->Init.OwnAddress1);
  291. }
  292. else /* I2C_ADDRESSINGMODE_10BIT */
  293. {
  294. hi2c->Instance->OAR1 = (I2C_OAR1_OA1EN | I2C_OAR1_OA1MODE | hi2c->Init.OwnAddress1);
  295. }
  296. }
  297. /*---------------------------- I2Cx CR2 Configuration ----------------------*/
  298. /* Configure I2Cx: Addressing Master mode */
  299. if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
  300. {
  301. hi2c->Instance->CR2 = (I2C_CR2_ADD10);
  302. }
  303. /* Enable the AUTOEND by default, and enable NACK (should be disable only during Slave process */
  304. hi2c->Instance->CR2 |= (I2C_CR2_AUTOEND | I2C_CR2_NACK);
  305. /*---------------------------- I2Cx OAR2 Configuration ---------------------*/
  306. /* Configure I2Cx: Dual mode and Own Address2 */
  307. hi2c->Instance->OAR2 = (hi2c->Init.DualAddressMode | hi2c->Init.OwnAddress2 | (hi2c->Init.OwnAddress2Masks << 8));
  308. /*---------------------------- I2Cx CR1 Configuration ----------------------*/
  309. /* Configure I2Cx: Generalcall and NoStretch mode */
  310. hi2c->Instance->CR1 = (hi2c->Init.GeneralCallMode | hi2c->Init.NoStretchMode);
  311. /* Enable the selected I2C peripheral */
  312. __HAL_I2C_ENABLE(hi2c);
  313. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  314. hi2c->State = HAL_I2C_STATE_READY;
  315. return HAL_OK;
  316. }
  317. /**
  318. * @brief DeInitializes the I2C peripheral.
  319. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  320. * the configuration information for the specified I2C.
  321. * @retval HAL status
  322. */
  323. HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *hi2c)
  324. {
  325. /* Check the I2C handle allocation */
  326. if(hi2c == NULL)
  327. {
  328. return HAL_ERROR;
  329. }
  330. /* Check the parameters */
  331. assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  332. hi2c->State = HAL_I2C_STATE_BUSY;
  333. /* Disable the I2C Peripheral Clock */
  334. __HAL_I2C_DISABLE(hi2c);
  335. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  336. HAL_I2C_MspDeInit(hi2c);
  337. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  338. hi2c->State = HAL_I2C_STATE_RESET;
  339. /* Release Lock */
  340. __HAL_UNLOCK(hi2c);
  341. return HAL_OK;
  342. }
  343. /**
  344. * @brief I2C MSP Init.
  345. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  346. * the configuration information for the specified I2C.
  347. * @retval None
  348. */
  349. __weak void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
  350. {
  351. /* NOTE : This function Should not be modified, when the callback is needed,
  352. the HAL_I2C_MspInit could be implemented in the user file
  353. */
  354. }
  355. /**
  356. * @brief I2C MSP DeInit
  357. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  358. * the configuration information for the specified I2C.
  359. * @retval None
  360. */
  361. __weak void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c)
  362. {
  363. /* NOTE : This function Should not be modified, when the callback is needed,
  364. the HAL_I2C_MspDeInit could be implemented in the user file
  365. */
  366. }
  367. /**
  368. * @}
  369. */
  370. /** @defgroup I2C_Exported_Functions_Group2 Input and Output operation functions
  371. * @brief Data transfers functions
  372. *
  373. @verbatim
  374. ===============================================================================
  375. ##### IO operation functions #####
  376. ===============================================================================
  377. [..]
  378. This subsection provides a set of functions allowing to manage the I2C data
  379. transfers.
  380. (#) There are two modes of transfer:
  381. (++) Blocking mode : The communication is performed in the polling mode.
  382. The status of all data processing is returned by the same function
  383. after finishing transfer.
  384. (++) No-Blocking mode : The communication is performed using Interrupts
  385. or DMA. These functions return the status of the transfer startup.
  386. The end of the data processing will be indicated through the
  387. dedicated I2C IRQ when using Interrupt mode or the DMA IRQ when
  388. using DMA mode.
  389. (#) Blocking mode functions are :
  390. (++) HAL_I2C_Master_Transmit()
  391. (++) HAL_I2C_Master_Receive()
  392. (++) HAL_I2C_Slave_Transmit()
  393. (++) HAL_I2C_Slave_Receive()
  394. (++) HAL_I2C_Mem_Write()
  395. (++) HAL_I2C_Mem_Read()
  396. (++) HAL_I2C_IsDeviceReady()
  397. (#) No-Blocking mode functions with Interrupt are :
  398. (++) HAL_I2C_Master_Transmit_IT()
  399. (++) HAL_I2C_Master_Receive_IT()
  400. (++) HAL_I2C_Slave_Transmit_IT()
  401. (++) HAL_I2C_Slave_Receive_IT()
  402. (++) HAL_I2C_Mem_Write_IT()
  403. (++) HAL_I2C_Mem_Read_IT()
  404. (#) No-Blocking mode functions with DMA are :
  405. (++) HAL_I2C_Master_Transmit_DMA()
  406. (++) HAL_I2C_Master_Receive_DMA()
  407. (++) HAL_I2C_Slave_Transmit_DMA()
  408. (++) HAL_I2C_Slave_Receive_DMA()
  409. (++) HAL_I2C_Mem_Write_DMA()
  410. (++) HAL_I2C_Mem_Read_DMA()
  411. (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  412. (++) HAL_I2C_MemTxCpltCallback()
  413. (++) HAL_I2C_MemRxCpltCallback()
  414. (++) HAL_I2C_MasterTxCpltCallback()
  415. (++) HAL_I2C_MasterRxCpltCallback()
  416. (++) HAL_I2C_SlaveTxCpltCallback()
  417. (++) HAL_I2C_SlaveRxCpltCallback()
  418. (++) HAL_I2C_ErrorCallback()
  419. @endverbatim
  420. * @{
  421. */
  422. /**
  423. * @brief Transmits in master mode an amount of data in blocking mode.
  424. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  425. * the configuration information for the specified I2C.
  426. * @param DevAddress: Target device address
  427. * @param pData: Pointer to data buffer
  428. * @param Size: Amount of data to be sent
  429. * @param Timeout: Timeout duration
  430. * @retval HAL status
  431. */
  432. HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  433. {
  434. uint32_t sizetmp = 0;
  435. if(hi2c->State == HAL_I2C_STATE_READY)
  436. {
  437. if((pData == NULL ) || (Size == 0))
  438. {
  439. return HAL_ERROR;
  440. }
  441. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  442. {
  443. return HAL_BUSY;
  444. }
  445. /* Process Locked */
  446. __HAL_LOCK(hi2c);
  447. hi2c->State = HAL_I2C_STATE_MASTER_BUSY_TX;
  448. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  449. /* Send Slave Address */
  450. /* Set NBYTES to write and reload if size > 255 and generate RESTART */
  451. /* Size > 255, need to set RELOAD bit */
  452. if(Size > 255)
  453. {
  454. I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
  455. sizetmp = 255;
  456. }
  457. else
  458. {
  459. I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_GENERATE_START_WRITE);
  460. sizetmp = Size;
  461. }
  462. do
  463. {
  464. /* Wait until TXIS flag is set */
  465. if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
  466. {
  467. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  468. {
  469. return HAL_ERROR;
  470. }
  471. else
  472. {
  473. return HAL_TIMEOUT;
  474. }
  475. }
  476. /* Write data to TXDR */
  477. hi2c->Instance->TXDR = (*pData++);
  478. sizetmp--;
  479. Size--;
  480. if((sizetmp == 0)&&(Size!=0))
  481. {
  482. /* Wait until TXE flag is set */
  483. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
  484. {
  485. return HAL_TIMEOUT;
  486. }
  487. if(Size > 255)
  488. {
  489. I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  490. sizetmp = 255;
  491. }
  492. else
  493. {
  494. I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  495. sizetmp = Size;
  496. }
  497. }
  498. }while(Size > 0);
  499. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  500. /* Wait until STOPF flag is set */
  501. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
  502. {
  503. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  504. {
  505. return HAL_ERROR;
  506. }
  507. else
  508. {
  509. return HAL_TIMEOUT;
  510. }
  511. }
  512. /* Clear STOP Flag */
  513. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  514. /* Clear Configuration Register 2 */
  515. I2C_RESET_CR2(hi2c);
  516. hi2c->State = HAL_I2C_STATE_READY;
  517. /* Process Unlocked */
  518. __HAL_UNLOCK(hi2c);
  519. return HAL_OK;
  520. }
  521. else
  522. {
  523. return HAL_BUSY;
  524. }
  525. }
  526. /**
  527. * @brief Receives in master mode an amount of data in blocking mode.
  528. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  529. * the configuration information for the specified I2C.
  530. * @param DevAddress: Target device address
  531. * @param pData: Pointer to data buffer
  532. * @param Size: Amount of data to be sent
  533. * @param Timeout: Timeout duration
  534. * @retval HAL status
  535. */
  536. HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  537. {
  538. uint32_t sizetmp = 0;
  539. if(hi2c->State == HAL_I2C_STATE_READY)
  540. {
  541. if((pData == NULL ) || (Size == 0))
  542. {
  543. return HAL_ERROR;
  544. }
  545. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  546. {
  547. return HAL_BUSY;
  548. }
  549. /* Process Locked */
  550. __HAL_LOCK(hi2c);
  551. hi2c->State = HAL_I2C_STATE_MASTER_BUSY_RX;
  552. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  553. /* Send Slave Address */
  554. /* Set NBYTES to write and reload if size > 255 and generate RESTART */
  555. /* Size > 255, need to set RELOAD bit */
  556. if(Size > 255)
  557. {
  558. I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
  559. sizetmp = 255;
  560. }
  561. else
  562. {
  563. I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
  564. sizetmp = Size;
  565. }
  566. do
  567. {
  568. /* Wait until RXNE flag is set */
  569. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  570. {
  571. return HAL_TIMEOUT;
  572. }
  573. /* Write data to RXDR */
  574. (*pData++) =hi2c->Instance->RXDR;
  575. sizetmp--;
  576. Size--;
  577. if((sizetmp == 0)&&(Size!=0))
  578. {
  579. /* Wait until TCR flag is set */
  580. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
  581. {
  582. return HAL_TIMEOUT;
  583. }
  584. if(Size > 255)
  585. {
  586. I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  587. sizetmp = 255;
  588. }
  589. else
  590. {
  591. I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  592. sizetmp = Size;
  593. }
  594. }
  595. }while(Size > 0);
  596. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  597. /* Wait until STOPF flag is set */
  598. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  599. {
  600. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  601. {
  602. return HAL_ERROR;
  603. }
  604. else
  605. {
  606. return HAL_TIMEOUT;
  607. }
  608. }
  609. /* Clear STOP Flag */
  610. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  611. /* Clear Configuration Register 2 */
  612. I2C_RESET_CR2(hi2c);
  613. hi2c->State = HAL_I2C_STATE_READY;
  614. /* Process Unlocked */
  615. __HAL_UNLOCK(hi2c);
  616. return HAL_OK;
  617. }
  618. else
  619. {
  620. return HAL_BUSY;
  621. }
  622. }
  623. /**
  624. * @brief Transmits in slave mode an amount of data in blocking mode.
  625. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  626. * the configuration information for the specified I2C.
  627. * @param pData: Pointer to data buffer
  628. * @param Size: Amount of data to be sent
  629. * @param Timeout: Timeout duration
  630. * @retval HAL status
  631. */
  632. HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  633. {
  634. if(hi2c->State == HAL_I2C_STATE_READY)
  635. {
  636. if((pData == NULL ) || (Size == 0))
  637. {
  638. return HAL_ERROR;
  639. }
  640. /* Process Locked */
  641. __HAL_LOCK(hi2c);
  642. hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_RX;
  643. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  644. /* Enable Address Acknowledge */
  645. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  646. /* Wait until ADDR flag is set */
  647. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)
  648. {
  649. /* Disable Address Acknowledge */
  650. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  651. return HAL_TIMEOUT;
  652. }
  653. /* Clear ADDR flag */
  654. __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
  655. /* If 10bit addressing mode is selected */
  656. if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
  657. {
  658. /* Wait until ADDR flag is set */
  659. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)
  660. {
  661. /* Disable Address Acknowledge */
  662. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  663. return HAL_TIMEOUT;
  664. }
  665. /* Clear ADDR flag */
  666. __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
  667. }
  668. /* Wait until DIR flag is set Transmitter mode */
  669. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, RESET, Timeout) != HAL_OK)
  670. {
  671. /* Disable Address Acknowledge */
  672. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  673. return HAL_TIMEOUT;
  674. }
  675. do
  676. {
  677. /* Wait until TXIS flag is set */
  678. if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
  679. {
  680. /* Disable Address Acknowledge */
  681. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  682. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  683. {
  684. return HAL_ERROR;
  685. }
  686. else
  687. {
  688. return HAL_TIMEOUT;
  689. }
  690. }
  691. /* Read data from TXDR */
  692. hi2c->Instance->TXDR = (*pData++);
  693. Size--;
  694. }while(Size > 0);
  695. /* Wait until STOP flag is set */
  696. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  697. {
  698. /* Disable Address Acknowledge */
  699. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  700. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  701. {
  702. /* Normal use case for Transmitter mode */
  703. /* A NACK is generated to confirm the end of transfer */
  704. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  705. }
  706. else
  707. {
  708. return HAL_TIMEOUT;
  709. }
  710. }
  711. /* Clear STOP flag */
  712. __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_STOPF);
  713. /* Wait until BUSY flag is reset */
  714. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)
  715. {
  716. /* Disable Address Acknowledge */
  717. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  718. return HAL_TIMEOUT;
  719. }
  720. /* Disable Address Acknowledge */
  721. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  722. hi2c->State = HAL_I2C_STATE_READY;
  723. /* Process Unlocked */
  724. __HAL_UNLOCK(hi2c);
  725. return HAL_OK;
  726. }
  727. else
  728. {
  729. return HAL_BUSY;
  730. }
  731. }
  732. /**
  733. * @brief Receive in slave mode an amount of data in blocking mode
  734. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  735. * the configuration information for the specified I2C.
  736. * @param pData: Pointer to data buffer
  737. * @param Size: Amount of data to be sent
  738. * @param Timeout: Timeout duration
  739. * @retval HAL status
  740. */
  741. HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  742. {
  743. if(hi2c->State == HAL_I2C_STATE_READY)
  744. {
  745. if((pData == NULL ) || (Size == 0))
  746. {
  747. return HAL_ERROR;
  748. }
  749. /* Process Locked */
  750. __HAL_LOCK(hi2c);
  751. hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_RX;
  752. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  753. /* Enable Address Acknowledge */
  754. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  755. /* Wait until ADDR flag is set */
  756. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout) != HAL_OK)
  757. {
  758. /* Disable Address Acknowledge */
  759. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  760. return HAL_TIMEOUT;
  761. }
  762. /* Clear ADDR flag */
  763. __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
  764. /* Wait until DIR flag is reset Receiver mode */
  765. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, SET, Timeout) != HAL_OK)
  766. {
  767. /* Disable Address Acknowledge */
  768. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  769. return HAL_TIMEOUT;
  770. }
  771. while(Size > 0)
  772. {
  773. /* Wait until RXNE flag is set */
  774. if(I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
  775. {
  776. /* Disable Address Acknowledge */
  777. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  778. if(hi2c->ErrorCode == HAL_I2C_ERROR_TIMEOUT)
  779. {
  780. return HAL_TIMEOUT;
  781. }
  782. else
  783. {
  784. return HAL_ERROR;
  785. }
  786. }
  787. /* Read data from RXDR */
  788. (*pData++) = hi2c->Instance->RXDR;
  789. Size--;
  790. }
  791. /* Wait until STOP flag is set */
  792. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  793. {
  794. /* Disable Address Acknowledge */
  795. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  796. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  797. {
  798. return HAL_ERROR;
  799. }
  800. else
  801. {
  802. return HAL_TIMEOUT;
  803. }
  804. }
  805. /* Clear STOP flag */
  806. __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_STOPF);
  807. /* Wait until BUSY flag is reset */
  808. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)
  809. {
  810. /* Disable Address Acknowledge */
  811. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  812. return HAL_TIMEOUT;
  813. }
  814. /* Disable Address Acknowledge */
  815. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  816. hi2c->State = HAL_I2C_STATE_READY;
  817. /* Process Unlocked */
  818. __HAL_UNLOCK(hi2c);
  819. return HAL_OK;
  820. }
  821. else
  822. {
  823. return HAL_BUSY;
  824. }
  825. }
  826. /**
  827. * @brief Transmit in master mode an amount of data in no-blocking mode with Interrupt
  828. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  829. * the configuration information for the specified I2C.
  830. * @param DevAddress: Target device address
  831. * @param pData: Pointer to data buffer
  832. * @param Size: Amount of data to be sent
  833. * @retval HAL status
  834. */
  835. HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  836. {
  837. if(hi2c->State == HAL_I2C_STATE_READY)
  838. {
  839. if((pData == NULL) || (Size == 0))
  840. {
  841. return HAL_ERROR;
  842. }
  843. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  844. {
  845. return HAL_BUSY;
  846. }
  847. /* Process Locked */
  848. __HAL_LOCK(hi2c);
  849. hi2c->State = HAL_I2C_STATE_MASTER_BUSY_TX;
  850. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  851. hi2c->pBuffPtr = pData;
  852. hi2c->XferCount = Size;
  853. if(Size > 255)
  854. {
  855. hi2c->XferSize = 255;
  856. }
  857. else
  858. {
  859. hi2c->XferSize = Size;
  860. }
  861. /* Send Slave Address */
  862. /* Set NBYTES to write and reload if size > 255 and generate RESTART */
  863. if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
  864. {
  865. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
  866. }
  867. else
  868. {
  869. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_WRITE);
  870. }
  871. /* Process Unlocked */
  872. __HAL_UNLOCK(hi2c);
  873. /* Note : The I2C interrupts must be enabled after unlocking current process
  874. to avoid the risk of I2C interrupt handle execution before current
  875. process unlock */
  876. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  877. /* possible to enable all of these */
  878. /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  879. __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_TXI );
  880. return HAL_OK;
  881. }
  882. else
  883. {
  884. return HAL_BUSY;
  885. }
  886. }
  887. /**
  888. * @brief Receive in master mode an amount of data in no-blocking mode with Interrupt
  889. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  890. * the configuration information for the specified I2C.
  891. * @param DevAddress: Target device address
  892. * @param pData: Pointer to data buffer
  893. * @param Size: Amount of data to be sent
  894. * @retval HAL status
  895. */
  896. HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  897. {
  898. if(hi2c->State == HAL_I2C_STATE_READY)
  899. {
  900. if((pData == NULL) || (Size == 0))
  901. {
  902. return HAL_ERROR;
  903. }
  904. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  905. {
  906. return HAL_BUSY;
  907. }
  908. /* Process Locked */
  909. __HAL_LOCK(hi2c);
  910. hi2c->State = HAL_I2C_STATE_MASTER_BUSY_RX;
  911. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  912. hi2c->pBuffPtr = pData;
  913. hi2c->XferCount = Size;
  914. if(Size > 255)
  915. {
  916. hi2c->XferSize = 255;
  917. }
  918. else
  919. {
  920. hi2c->XferSize = Size;
  921. }
  922. /* Send Slave Address */
  923. /* Set NBYTES to write and reload if size > 255 and generate RESTART */
  924. if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
  925. {
  926. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
  927. }
  928. else
  929. {
  930. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
  931. }
  932. /* Process Unlocked */
  933. __HAL_UNLOCK(hi2c);
  934. /* Note : The I2C interrupts must be enabled after unlocking current process
  935. to avoid the risk of I2C interrupt handle execution before current
  936. process unlock */
  937. /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  938. /* possible to enable all of these */
  939. /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  940. __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_RXI );
  941. return HAL_OK;
  942. }
  943. else
  944. {
  945. return HAL_BUSY;
  946. }
  947. }
  948. /**
  949. * @brief Transmit in slave mode an amount of data in no-blocking mode with Interrupt
  950. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  951. * the configuration information for the specified I2C.
  952. * @param pData: Pointer to data buffer
  953. * @param Size: Amount of data to be sent
  954. * @retval HAL status
  955. */
  956. HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  957. {
  958. if(hi2c->State == HAL_I2C_STATE_READY)
  959. {
  960. if((pData == NULL) || (Size == 0))
  961. {
  962. return HAL_ERROR;
  963. }
  964. /* Process Locked */
  965. __HAL_LOCK(hi2c);
  966. hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_TX;
  967. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  968. /* Enable Address Acknowledge */
  969. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  970. hi2c->pBuffPtr = pData;
  971. hi2c->XferSize = Size;
  972. hi2c->XferCount = Size;
  973. /* Process Unlocked */
  974. __HAL_UNLOCK(hi2c);
  975. /* Note : The I2C interrupts must be enabled after unlocking current process
  976. to avoid the risk of I2C interrupt handle execution before current
  977. process unlock */
  978. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  979. /* possible to enable all of these */
  980. /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  981. __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_TXI );
  982. return HAL_OK;
  983. }
  984. else
  985. {
  986. return HAL_BUSY;
  987. }
  988. }
  989. /**
  990. * @brief Receive in slave mode an amount of data in no-blocking mode with Interrupt
  991. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  992. * the configuration information for the specified I2C.
  993. * @param pData: Pointer to data buffer
  994. * @param Size: Amount of data to be sent
  995. * @retval HAL status
  996. */
  997. HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  998. {
  999. if(hi2c->State == HAL_I2C_STATE_READY)
  1000. {
  1001. if((pData == NULL) || (Size == 0))
  1002. {
  1003. return HAL_ERROR;
  1004. }
  1005. /* Process Locked */
  1006. __HAL_LOCK(hi2c);
  1007. hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_RX;
  1008. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1009. /* Enable Address Acknowledge */
  1010. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  1011. hi2c->pBuffPtr = pData;
  1012. hi2c->XferSize = Size;
  1013. hi2c->XferCount = Size;
  1014. /* Process Unlocked */
  1015. __HAL_UNLOCK(hi2c);
  1016. /* Note : The I2C interrupts must be enabled after unlocking current process
  1017. to avoid the risk of I2C interrupt handle execution before current
  1018. process unlock */
  1019. /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  1020. /* possible to enable all of these */
  1021. /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  1022. __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI);
  1023. return HAL_OK;
  1024. }
  1025. else
  1026. {
  1027. return HAL_BUSY;
  1028. }
  1029. }
  1030. /**
  1031. * @brief Transmit in master mode an amount of data in no-blocking mode with DMA
  1032. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  1033. * the configuration information for the specified I2C.
  1034. * @param DevAddress: Target device address
  1035. * @param pData: Pointer to data buffer
  1036. * @param Size: Amount of data to be sent
  1037. * @retval HAL status
  1038. */
  1039. HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  1040. {
  1041. if(hi2c->State == HAL_I2C_STATE_READY)
  1042. {
  1043. if((pData == NULL) || (Size == 0))
  1044. {
  1045. return HAL_ERROR;
  1046. }
  1047. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1048. {
  1049. return HAL_BUSY;
  1050. }
  1051. /* Process Locked */
  1052. __HAL_LOCK(hi2c);
  1053. hi2c->State = HAL_I2C_STATE_MASTER_BUSY_TX;
  1054. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1055. hi2c->pBuffPtr = pData;
  1056. hi2c->XferCount = Size;
  1057. if(Size > 255)
  1058. {
  1059. hi2c->XferSize = 255;
  1060. }
  1061. else
  1062. {
  1063. hi2c->XferSize = Size;
  1064. }
  1065. /* Set the I2C DMA transfer complete callback */
  1066. hi2c->hdmatx->XferCpltCallback = I2C_DMAMasterTransmitCplt;
  1067. /* Set the DMA error callback */
  1068. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  1069. /* Enable the DMA channel */
  1070. HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
  1071. /* Send Slave Address */
  1072. /* Set NBYTES to write and reload if size > 255 and generate RESTART */
  1073. if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
  1074. {
  1075. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
  1076. }
  1077. else
  1078. {
  1079. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_WRITE);
  1080. }
  1081. /* Wait until TXIS flag is set */
  1082. if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, I2C_TIMEOUT_TXIS) != HAL_OK)
  1083. {
  1084. /* Disable Address Acknowledge */
  1085. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1086. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1087. {
  1088. return HAL_ERROR;
  1089. }
  1090. else
  1091. {
  1092. return HAL_TIMEOUT;
  1093. }
  1094. }
  1095. /* Enable DMA Request */
  1096. hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  1097. /* Process Unlocked */
  1098. __HAL_UNLOCK(hi2c);
  1099. return HAL_OK;
  1100. }
  1101. else
  1102. {
  1103. return HAL_BUSY;
  1104. }
  1105. }
  1106. /**
  1107. * @brief Receive in master mode an amount of data in no-blocking mode with DMA
  1108. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  1109. * the configuration information for the specified I2C.
  1110. * @param DevAddress: Target device address
  1111. * @param pData: Pointer to data buffer
  1112. * @param Size: Amount of data to be sent
  1113. * @retval HAL status
  1114. */
  1115. HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  1116. {
  1117. if(hi2c->State == HAL_I2C_STATE_READY)
  1118. {
  1119. if((pData == NULL) || (Size == 0))
  1120. {
  1121. return HAL_ERROR;
  1122. }
  1123. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1124. {
  1125. return HAL_BUSY;
  1126. }
  1127. /* Process Locked */
  1128. __HAL_LOCK(hi2c);
  1129. hi2c->State = HAL_I2C_STATE_MASTER_BUSY_RX;
  1130. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1131. hi2c->pBuffPtr = pData;
  1132. hi2c->XferCount = Size;
  1133. if(Size > 255)
  1134. {
  1135. hi2c->XferSize = 255;
  1136. }
  1137. else
  1138. {
  1139. hi2c->XferSize = Size;
  1140. }
  1141. /* Set the I2C DMA transfer complete callback */
  1142. hi2c->hdmarx->XferCpltCallback = I2C_DMAMasterReceiveCplt;
  1143. /* Set the DMA error callback */
  1144. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  1145. /* Enable the DMA channel */
  1146. HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, hi2c->XferSize);
  1147. /* Send Slave Address */
  1148. /* Set NBYTES to write and reload if size > 255 and generate RESTART */
  1149. if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
  1150. {
  1151. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
  1152. }
  1153. else
  1154. {
  1155. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
  1156. }
  1157. /* Wait until RXNE flag is set */
  1158. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, I2C_TIMEOUT_RXNE) != HAL_OK)
  1159. {
  1160. return HAL_TIMEOUT;
  1161. }
  1162. /* Enable DMA Request */
  1163. hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  1164. /* Process Unlocked */
  1165. __HAL_UNLOCK(hi2c);
  1166. return HAL_OK;
  1167. }
  1168. else
  1169. {
  1170. return HAL_BUSY;
  1171. }
  1172. }
  1173. /**
  1174. * @brief Transmit in slave mode an amount of data in no-blocking mode with DMA
  1175. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  1176. * the configuration information for the specified I2C.
  1177. * @param pData: Pointer to data buffer
  1178. * @param Size: Amount of data to be sent
  1179. * @retval HAL status
  1180. */
  1181. HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  1182. {
  1183. if(hi2c->State == HAL_I2C_STATE_READY)
  1184. {
  1185. if((pData == NULL) || (Size == 0))
  1186. {
  1187. return HAL_ERROR;
  1188. }
  1189. /* Process Locked */
  1190. __HAL_LOCK(hi2c);
  1191. hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_TX;
  1192. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1193. hi2c->pBuffPtr = pData;
  1194. hi2c->XferCount = Size;
  1195. hi2c->XferSize = Size;
  1196. /* Set the I2C DMA transfer complete callback */
  1197. hi2c->hdmatx->XferCpltCallback = I2C_DMASlaveTransmitCplt;
  1198. /* Set the DMA error callback */
  1199. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  1200. /* Enable the DMA channel */
  1201. HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
  1202. /* Enable Address Acknowledge */
  1203. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  1204. /* Wait until ADDR flag is set */
  1205. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, I2C_TIMEOUT_ADDR) != HAL_OK)
  1206. {
  1207. /* Disable Address Acknowledge */
  1208. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1209. return HAL_TIMEOUT;
  1210. }
  1211. /* Clear ADDR flag */
  1212. __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
  1213. /* If 10bits addressing mode is selected */
  1214. if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
  1215. {
  1216. /* Wait until ADDR flag is set */
  1217. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, I2C_TIMEOUT_ADDR) != HAL_OK)
  1218. {
  1219. /* Disable Address Acknowledge */
  1220. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1221. return HAL_TIMEOUT;
  1222. }
  1223. /* Clear ADDR flag */
  1224. __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
  1225. }
  1226. /* Wait until DIR flag is set Transmitter mode */
  1227. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, RESET, I2C_TIMEOUT_BUSY) != HAL_OK)
  1228. {
  1229. /* Disable Address Acknowledge */
  1230. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1231. return HAL_TIMEOUT;
  1232. }
  1233. /* Enable DMA Request */
  1234. hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  1235. /* Process Unlocked */
  1236. __HAL_UNLOCK(hi2c);
  1237. return HAL_OK;
  1238. }
  1239. else
  1240. {
  1241. return HAL_BUSY;
  1242. }
  1243. }
  1244. /**
  1245. * @brief Receive in slave mode an amount of data in no-blocking mode with DMA
  1246. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  1247. * the configuration information for the specified I2C.
  1248. * @param pData: Pointer to data buffer
  1249. * @param Size: Amount of data to be sent
  1250. * @retval HAL status
  1251. */
  1252. HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  1253. {
  1254. if(hi2c->State == HAL_I2C_STATE_READY)
  1255. {
  1256. if((pData == NULL) || (Size == 0))
  1257. {
  1258. return HAL_ERROR;
  1259. }
  1260. /* Process Locked */
  1261. __HAL_LOCK(hi2c);
  1262. hi2c->State = HAL_I2C_STATE_SLAVE_BUSY_RX;
  1263. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1264. hi2c->pBuffPtr = pData;
  1265. hi2c->XferSize = Size;
  1266. hi2c->XferCount = Size;
  1267. /* Set the I2C DMA transfer complete callback */
  1268. hi2c->hdmarx->XferCpltCallback = I2C_DMASlaveReceiveCplt;
  1269. /* Set the DMA error callback */
  1270. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  1271. /* Enable the DMA channel */
  1272. HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, Size);
  1273. /* Enable Address Acknowledge */
  1274. hi2c->Instance->CR2 &= ~I2C_CR2_NACK;
  1275. /* Wait until ADDR flag is set */
  1276. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, I2C_TIMEOUT_ADDR) != HAL_OK)
  1277. {
  1278. /* Disable Address Acknowledge */
  1279. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1280. return HAL_TIMEOUT;
  1281. }
  1282. /* Clear ADDR flag */
  1283. __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_ADDR);
  1284. /* Wait until DIR flag is set Receiver mode */
  1285. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, SET, I2C_TIMEOUT_DIR) != HAL_OK)
  1286. {
  1287. /* Disable Address Acknowledge */
  1288. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  1289. return HAL_TIMEOUT;
  1290. }
  1291. /* Enable DMA Request */
  1292. hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  1293. /* Process Unlocked */
  1294. __HAL_UNLOCK(hi2c);
  1295. return HAL_OK;
  1296. }
  1297. else
  1298. {
  1299. return HAL_BUSY;
  1300. }
  1301. }
  1302. /**
  1303. * @brief Write an amount of data in blocking mode to a specific memory address
  1304. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  1305. * the configuration information for the specified I2C.
  1306. * @param DevAddress: Target device address
  1307. * @param MemAddress: Internal memory address
  1308. * @param MemAddSize: Size of internal memory address
  1309. * @param pData: Pointer to data buffer
  1310. * @param Size: Amount of data to be sent
  1311. * @param Timeout: Timeout duration
  1312. * @retval HAL status
  1313. */
  1314. 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)
  1315. {
  1316. uint32_t Sizetmp = 0;
  1317. /* Check the parameters */
  1318. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  1319. if(hi2c->State == HAL_I2C_STATE_READY)
  1320. {
  1321. if((pData == NULL) || (Size == 0))
  1322. {
  1323. return HAL_ERROR;
  1324. }
  1325. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1326. {
  1327. return HAL_BUSY;
  1328. }
  1329. /* Process Locked */
  1330. __HAL_LOCK(hi2c);
  1331. hi2c->State = HAL_I2C_STATE_MEM_BUSY_TX;
  1332. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1333. /* Send Slave Address and Memory Address */
  1334. if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, Timeout) != HAL_OK)
  1335. {
  1336. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1337. {
  1338. /* Process Unlocked */
  1339. __HAL_UNLOCK(hi2c);
  1340. return HAL_ERROR;
  1341. }
  1342. else
  1343. {
  1344. /* Process Unlocked */
  1345. __HAL_UNLOCK(hi2c);
  1346. return HAL_TIMEOUT;
  1347. }
  1348. }
  1349. /* Set NBYTES to write and reload if size > 255 */
  1350. /* Size > 255, need to set RELOAD bit */
  1351. if(Size > 255)
  1352. {
  1353. I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  1354. Sizetmp = 255;
  1355. }
  1356. else
  1357. {
  1358. I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  1359. Sizetmp = Size;
  1360. }
  1361. do
  1362. {
  1363. /* Wait until TXIS flag is set */
  1364. if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
  1365. {
  1366. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1367. {
  1368. return HAL_ERROR;
  1369. }
  1370. else
  1371. {
  1372. return HAL_TIMEOUT;
  1373. }
  1374. }
  1375. /* Write data to DR */
  1376. hi2c->Instance->TXDR = (*pData++);
  1377. Sizetmp--;
  1378. Size--;
  1379. if((Sizetmp == 0)&&(Size!=0))
  1380. {
  1381. /* Wait until TCR flag is set */
  1382. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
  1383. {
  1384. return HAL_TIMEOUT;
  1385. }
  1386. if(Size > 255)
  1387. {
  1388. I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  1389. Sizetmp = 255;
  1390. }
  1391. else
  1392. {
  1393. I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  1394. Sizetmp = Size;
  1395. }
  1396. }
  1397. }while(Size > 0);
  1398. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  1399. /* Wait until STOPF flag is reset */
  1400. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  1401. {
  1402. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1403. {
  1404. return HAL_ERROR;
  1405. }
  1406. else
  1407. {
  1408. return HAL_TIMEOUT;
  1409. }
  1410. }
  1411. /* Clear STOP Flag */
  1412. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  1413. /* Clear Configuration Register 2 */
  1414. I2C_RESET_CR2(hi2c);
  1415. hi2c->State = HAL_I2C_STATE_READY;
  1416. /* Process Unlocked */
  1417. __HAL_UNLOCK(hi2c);
  1418. return HAL_OK;
  1419. }
  1420. else
  1421. {
  1422. return HAL_BUSY;
  1423. }
  1424. }
  1425. /**
  1426. * @brief Read an amount of data in blocking mode from a specific memory address
  1427. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  1428. * the configuration information for the specified I2C.
  1429. * @param DevAddress: Target device address
  1430. * @param MemAddress: Internal memory address
  1431. * @param MemAddSize: Size of internal memory address
  1432. * @param pData: Pointer to data buffer
  1433. * @param Size: Amount of data to be sent
  1434. * @param Timeout: Timeout duration
  1435. * @retval HAL status
  1436. */
  1437. 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)
  1438. {
  1439. uint32_t Sizetmp = 0;
  1440. /* Check the parameters */
  1441. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  1442. if(hi2c->State == HAL_I2C_STATE_READY)
  1443. {
  1444. if((pData == NULL) || (Size == 0))
  1445. {
  1446. return HAL_ERROR;
  1447. }
  1448. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1449. {
  1450. return HAL_BUSY;
  1451. }
  1452. /* Process Locked */
  1453. __HAL_LOCK(hi2c);
  1454. hi2c->State = HAL_I2C_STATE_MEM_BUSY_RX;
  1455. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1456. /* Send Slave Address and Memory Address */
  1457. if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, Timeout) != HAL_OK)
  1458. {
  1459. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1460. {
  1461. /* Process Unlocked */
  1462. __HAL_UNLOCK(hi2c);
  1463. return HAL_ERROR;
  1464. }
  1465. else
  1466. {
  1467. /* Process Unlocked */
  1468. __HAL_UNLOCK(hi2c);
  1469. return HAL_TIMEOUT;
  1470. }
  1471. }
  1472. /* Send Slave Address */
  1473. /* Set NBYTES to write and reload if size > 255 and generate RESTART */
  1474. /* Size > 255, need to set RELOAD bit */
  1475. if(Size > 255)
  1476. {
  1477. I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
  1478. Sizetmp = 255;
  1479. }
  1480. else
  1481. {
  1482. I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
  1483. Sizetmp = Size;
  1484. }
  1485. do
  1486. {
  1487. /* Wait until RXNE flag is set */
  1488. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  1489. {
  1490. return HAL_TIMEOUT;
  1491. }
  1492. /* Read data from RXDR */
  1493. (*pData++) = hi2c->Instance->RXDR;
  1494. /* Decrement the Size counter */
  1495. Sizetmp--;
  1496. Size--;
  1497. if((Sizetmp == 0)&&(Size!=0))
  1498. {
  1499. /* Wait until TCR flag is set */
  1500. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
  1501. {
  1502. return HAL_TIMEOUT;
  1503. }
  1504. if(Size > 255)
  1505. {
  1506. I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  1507. Sizetmp = 255;
  1508. }
  1509. else
  1510. {
  1511. I2C_TransferConfig(hi2c,DevAddress,Size, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  1512. Sizetmp = Size;
  1513. }
  1514. }
  1515. }while(Size > 0);
  1516. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  1517. /* Wait until STOPF flag is reset */
  1518. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  1519. {
  1520. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1521. {
  1522. return HAL_ERROR;
  1523. }
  1524. else
  1525. {
  1526. return HAL_TIMEOUT;
  1527. }
  1528. }
  1529. /* Clear STOP Flag */
  1530. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  1531. /* Clear Configuration Register 2 */
  1532. I2C_RESET_CR2(hi2c);
  1533. hi2c->State = HAL_I2C_STATE_READY;
  1534. /* Process Unlocked */
  1535. __HAL_UNLOCK(hi2c);
  1536. return HAL_OK;
  1537. }
  1538. else
  1539. {
  1540. return HAL_BUSY;
  1541. }
  1542. }
  1543. /**
  1544. * @brief Write an amount of data in no-blocking mode with Interrupt to a specific memory address
  1545. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  1546. * the configuration information for the specified I2C.
  1547. * @param DevAddress: Target device address
  1548. * @param MemAddress: Internal memory address
  1549. * @param MemAddSize: Size of internal memory address
  1550. * @param pData: Pointer to data buffer
  1551. * @param Size: Amount of data to be sent
  1552. * @retval HAL status
  1553. */
  1554. 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)
  1555. {
  1556. /* Check the parameters */
  1557. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  1558. if(hi2c->State == HAL_I2C_STATE_READY)
  1559. {
  1560. if((pData == NULL) || (Size == 0))
  1561. {
  1562. return HAL_ERROR;
  1563. }
  1564. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1565. {
  1566. return HAL_BUSY;
  1567. }
  1568. /* Process Locked */
  1569. __HAL_LOCK(hi2c);
  1570. hi2c->State = HAL_I2C_STATE_MEM_BUSY_TX;
  1571. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1572. hi2c->pBuffPtr = pData;
  1573. hi2c->XferCount = Size;
  1574. if(Size > 255)
  1575. {
  1576. hi2c->XferSize = 255;
  1577. }
  1578. else
  1579. {
  1580. hi2c->XferSize = Size;
  1581. }
  1582. /* Send Slave Address and Memory Address */
  1583. if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
  1584. {
  1585. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1586. {
  1587. /* Process Unlocked */
  1588. __HAL_UNLOCK(hi2c);
  1589. return HAL_ERROR;
  1590. }
  1591. else
  1592. {
  1593. /* Process Unlocked */
  1594. __HAL_UNLOCK(hi2c);
  1595. return HAL_TIMEOUT;
  1596. }
  1597. }
  1598. /* Set NBYTES to write and reload if size > 255 */
  1599. /* Size > 255, need to set RELOAD bit */
  1600. if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
  1601. {
  1602. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  1603. }
  1604. else
  1605. {
  1606. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  1607. }
  1608. /* Process Unlocked */
  1609. __HAL_UNLOCK(hi2c);
  1610. /* Note : The I2C interrupts must be enabled after unlocking current process
  1611. to avoid the risk of I2C interrupt handle execution before current
  1612. process unlock */
  1613. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  1614. /* possible to enable all of these */
  1615. /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  1616. __HAL_I2C_ENABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_TXI );
  1617. return HAL_OK;
  1618. }
  1619. else
  1620. {
  1621. return HAL_BUSY;
  1622. }
  1623. }
  1624. /**
  1625. * @brief Read an amount of data in no-blocking mode with Interrupt from a specific memory address
  1626. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  1627. * the configuration information for the specified I2C.
  1628. * @param DevAddress: Target device address
  1629. * @param MemAddress: Internal memory address
  1630. * @param MemAddSize: Size of internal memory address
  1631. * @param pData: Pointer to data buffer
  1632. * @param Size: Amount of data to be sent
  1633. * @retval HAL status
  1634. */
  1635. 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)
  1636. {
  1637. /* Check the parameters */
  1638. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  1639. if(hi2c->State == HAL_I2C_STATE_READY)
  1640. {
  1641. if((pData == NULL) || (Size == 0))
  1642. {
  1643. return HAL_ERROR;
  1644. }
  1645. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1646. {
  1647. return HAL_BUSY;
  1648. }
  1649. /* Process Locked */
  1650. __HAL_LOCK(hi2c);
  1651. hi2c->State = HAL_I2C_STATE_MEM_BUSY_RX;
  1652. hi2c->pBuffPtr = pData;
  1653. hi2c->XferCount = Size;
  1654. if(Size > 255)
  1655. {
  1656. hi2c->XferSize = 255;
  1657. }
  1658. else
  1659. {
  1660. hi2c->XferSize = Size;
  1661. }
  1662. /* Send Slave Address and Memory Address */
  1663. if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
  1664. {
  1665. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1666. {
  1667. /* Process Unlocked */
  1668. __HAL_UNLOCK(hi2c);
  1669. return HAL_ERROR;
  1670. }
  1671. else
  1672. {
  1673. /* Process Unlocked */
  1674. __HAL_UNLOCK(hi2c);
  1675. return HAL_TIMEOUT;
  1676. }
  1677. }
  1678. /* Set NBYTES to write and reload if size > 255 and generate RESTART */
  1679. /* Size > 255, need to set RELOAD bit */
  1680. if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
  1681. {
  1682. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
  1683. }
  1684. else
  1685. {
  1686. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
  1687. }
  1688. /* Process Unlocked */
  1689. __HAL_UNLOCK(hi2c);
  1690. /* Note : The I2C interrupts must be enabled after unlocking current process
  1691. to avoid the risk of I2C interrupt handle execution before current
  1692. process unlock */
  1693. /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  1694. /* possible to enable all of these */
  1695. /* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
  1696. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_RXI );
  1697. return HAL_OK;
  1698. }
  1699. else
  1700. {
  1701. return HAL_BUSY;
  1702. }
  1703. }
  1704. /**
  1705. * @brief Write an amount of data in no-blocking mode with DMA to a specific memory address
  1706. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  1707. * the configuration information for the specified I2C.
  1708. * @param DevAddress: Target device address
  1709. * @param MemAddress: Internal memory address
  1710. * @param MemAddSize: Size of internal memory address
  1711. * @param pData: Pointer to data buffer
  1712. * @param Size: Amount of data to be sent
  1713. * @retval HAL status
  1714. */
  1715. 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)
  1716. {
  1717. /* Check the parameters */
  1718. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  1719. if(hi2c->State == HAL_I2C_STATE_READY)
  1720. {
  1721. if((pData == NULL) || (Size == 0))
  1722. {
  1723. return HAL_ERROR;
  1724. }
  1725. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1726. {
  1727. return HAL_BUSY;
  1728. }
  1729. /* Process Locked */
  1730. __HAL_LOCK(hi2c);
  1731. hi2c->State = HAL_I2C_STATE_MEM_BUSY_TX;
  1732. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1733. hi2c->pBuffPtr = pData;
  1734. hi2c->XferCount = Size;
  1735. if(Size > 255)
  1736. {
  1737. hi2c->XferSize = 255;
  1738. }
  1739. else
  1740. {
  1741. hi2c->XferSize = Size;
  1742. }
  1743. /* Set the I2C DMA transfer complete callback */
  1744. hi2c->hdmatx->XferCpltCallback = I2C_DMAMemTransmitCplt;
  1745. /* Set the DMA error callback */
  1746. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  1747. /* Enable the DMA channel */
  1748. HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
  1749. /* Send Slave Address and Memory Address */
  1750. if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
  1751. {
  1752. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1753. {
  1754. /* Process Unlocked */
  1755. __HAL_UNLOCK(hi2c);
  1756. return HAL_ERROR;
  1757. }
  1758. else
  1759. {
  1760. /* Process Unlocked */
  1761. __HAL_UNLOCK(hi2c);
  1762. return HAL_TIMEOUT;
  1763. }
  1764. }
  1765. /* Send Slave Address */
  1766. /* Set NBYTES to write and reload if size > 255 */
  1767. if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
  1768. {
  1769. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  1770. }
  1771. else
  1772. {
  1773. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  1774. }
  1775. /* Wait until TXIS flag is set */
  1776. if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, I2C_TIMEOUT_TXIS) != HAL_OK)
  1777. {
  1778. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1779. {
  1780. return HAL_ERROR;
  1781. }
  1782. else
  1783. {
  1784. return HAL_TIMEOUT;
  1785. }
  1786. }
  1787. /* Enable DMA Request */
  1788. hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  1789. /* Process Unlocked */
  1790. __HAL_UNLOCK(hi2c);
  1791. return HAL_OK;
  1792. }
  1793. else
  1794. {
  1795. return HAL_BUSY;
  1796. }
  1797. }
  1798. /**
  1799. * @brief Reads an amount of data in no-blocking mode with DMA from a specific memory address.
  1800. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  1801. * the configuration information for the specified I2C.
  1802. * @param DevAddress: Target device address
  1803. * @param MemAddress: Internal memory address
  1804. * @param MemAddSize: Size of internal memory address
  1805. * @param pData: Pointer to data buffer
  1806. * @param Size: Amount of data to be read
  1807. * @retval HAL status
  1808. */
  1809. 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)
  1810. {
  1811. /* Check the parameters */
  1812. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  1813. if(hi2c->State == HAL_I2C_STATE_READY)
  1814. {
  1815. if((pData == NULL) || (Size == 0))
  1816. {
  1817. return HAL_ERROR;
  1818. }
  1819. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1820. {
  1821. return HAL_BUSY;
  1822. }
  1823. /* Process Locked */
  1824. __HAL_LOCK(hi2c);
  1825. hi2c->State = HAL_I2C_STATE_MEM_BUSY_RX;
  1826. hi2c->pBuffPtr = pData;
  1827. hi2c->XferCount = Size;
  1828. if(Size > 255)
  1829. {
  1830. hi2c->XferSize = 255;
  1831. }
  1832. else
  1833. {
  1834. hi2c->XferSize = Size;
  1835. }
  1836. /* Set the I2C DMA transfer complete callback */
  1837. hi2c->hdmarx->XferCpltCallback = I2C_DMAMemReceiveCplt;
  1838. /* Set the DMA error callback */
  1839. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  1840. /* Enable the DMA channel */
  1841. HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, hi2c->XferSize);
  1842. /* Send Slave Address and Memory Address */
  1843. if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
  1844. {
  1845. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  1846. {
  1847. /* Process Unlocked */
  1848. __HAL_UNLOCK(hi2c);
  1849. return HAL_ERROR;
  1850. }
  1851. else
  1852. {
  1853. /* Process Unlocked */
  1854. __HAL_UNLOCK(hi2c);
  1855. return HAL_TIMEOUT;
  1856. }
  1857. }
  1858. /* Set NBYTES to write and reload if size > 255 and generate RESTART */
  1859. if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
  1860. {
  1861. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
  1862. }
  1863. else
  1864. {
  1865. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
  1866. }
  1867. /* Wait until RXNE flag is set */
  1868. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, I2C_TIMEOUT_RXNE) != HAL_OK)
  1869. {
  1870. return HAL_TIMEOUT;
  1871. }
  1872. /* Enable DMA Request */
  1873. hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  1874. /* Process Unlocked */
  1875. __HAL_UNLOCK(hi2c);
  1876. return HAL_OK;
  1877. }
  1878. else
  1879. {
  1880. return HAL_BUSY;
  1881. }
  1882. }
  1883. /**
  1884. * @brief Checks if target device is ready for communication.
  1885. * @note This function is used with Memory devices
  1886. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  1887. * the configuration information for the specified I2C.
  1888. * @param DevAddress: Target device address
  1889. * @param Trials: Number of trials
  1890. * @param Timeout: Timeout duration
  1891. * @retval HAL status
  1892. */
  1893. HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
  1894. {
  1895. uint32_t tickstart = 0;
  1896. __IO uint32_t I2C_Trials = 0;
  1897. if(hi2c->State == HAL_I2C_STATE_READY)
  1898. {
  1899. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
  1900. {
  1901. return HAL_BUSY;
  1902. }
  1903. /* Process Locked */
  1904. __HAL_LOCK(hi2c);
  1905. hi2c->State = HAL_I2C_STATE_BUSY;
  1906. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1907. do
  1908. {
  1909. /* Generate Start */
  1910. hi2c->Instance->CR2 = I2C_GENERATE_START(hi2c->Init.AddressingMode,DevAddress);
  1911. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  1912. /* Wait until STOPF flag is set or a NACK flag is set*/
  1913. tickstart = HAL_GetTick();
  1914. while((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET) && (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == RESET) && (hi2c->State != HAL_I2C_STATE_TIMEOUT))
  1915. {
  1916. if(Timeout != HAL_MAX_DELAY)
  1917. {
  1918. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1919. {
  1920. /* Device is ready */
  1921. hi2c->State = HAL_I2C_STATE_READY;
  1922. /* Process Unlocked */
  1923. __HAL_UNLOCK(hi2c);
  1924. return HAL_TIMEOUT;
  1925. }
  1926. }
  1927. }
  1928. /* Check if the NACKF flag has not been set */
  1929. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == RESET)
  1930. {
  1931. /* Wait until STOPF flag is reset */
  1932. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout) != HAL_OK)
  1933. {
  1934. return HAL_TIMEOUT;
  1935. }
  1936. /* Clear STOP Flag */
  1937. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  1938. /* Device is ready */
  1939. hi2c->State = HAL_I2C_STATE_READY;
  1940. /* Process Unlocked */
  1941. __HAL_UNLOCK(hi2c);
  1942. return HAL_OK;
  1943. }
  1944. else
  1945. {
  1946. /* Wait until STOPF flag is reset */
  1947. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout) != HAL_OK)
  1948. {
  1949. return HAL_TIMEOUT;
  1950. }
  1951. /* Clear NACK Flag */
  1952. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  1953. /* Clear STOP Flag, auto generated with autoend*/
  1954. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  1955. }
  1956. /* Check if the maximum allowed number of trials has been reached */
  1957. if (I2C_Trials++ == Trials)
  1958. {
  1959. /* Generate Stop */
  1960. hi2c->Instance->CR2 |= I2C_CR2_STOP;
  1961. /* Wait until STOPF flag is reset */
  1962. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout) != HAL_OK)
  1963. {
  1964. return HAL_TIMEOUT;
  1965. }
  1966. /* Clear STOP Flag */
  1967. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  1968. }
  1969. }while(I2C_Trials < Trials);
  1970. hi2c->State = HAL_I2C_STATE_READY;
  1971. /* Process Unlocked */
  1972. __HAL_UNLOCK(hi2c);
  1973. return HAL_TIMEOUT;
  1974. }
  1975. else
  1976. {
  1977. return HAL_BUSY;
  1978. }
  1979. }
  1980. /**
  1981. * @}
  1982. */
  1983. /** @defgroup IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
  1984. * @{
  1985. */
  1986. /**
  1987. * @brief This function handles I2C event interrupt request.
  1988. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  1989. * the configuration information for the specified I2C.
  1990. * @retval None
  1991. */
  1992. void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c)
  1993. {
  1994. /* I2C in mode Transmitter ---------------------------------------------------*/
  1995. if (((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)) && (__HAL_I2C_GET_IT_SOURCE(hi2c, (I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_TXI | I2C_IT_ADDRI)) == SET))
  1996. {
  1997. /* Slave mode selected */
  1998. if (hi2c->State == HAL_I2C_STATE_SLAVE_BUSY_TX)
  1999. {
  2000. I2C_SlaveTransmit_ISR(hi2c);
  2001. }
  2002. }
  2003. if (((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)) && (__HAL_I2C_GET_IT_SOURCE(hi2c, (I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_TXI)) == SET))
  2004. {
  2005. /* Master mode selected */
  2006. if ((hi2c->State == HAL_I2C_STATE_MASTER_BUSY_TX) || (hi2c->State == HAL_I2C_STATE_MEM_BUSY_TX))
  2007. {
  2008. I2C_MasterTransmit_ISR(hi2c);
  2009. }
  2010. }
  2011. /* I2C in mode Receiver ----------------------------------------------------*/
  2012. if (((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)) && (__HAL_I2C_GET_IT_SOURCE(hi2c, (I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_RXI | I2C_IT_ADDRI)) == SET))
  2013. {
  2014. /* Slave mode selected */
  2015. if (hi2c->State == HAL_I2C_STATE_SLAVE_BUSY_RX)
  2016. {
  2017. I2C_SlaveReceive_ISR(hi2c);
  2018. }
  2019. }
  2020. if (((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) || (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)) && (__HAL_I2C_GET_IT_SOURCE(hi2c, (I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_RXI)) == SET))
  2021. {
  2022. /* Master mode selected */
  2023. if ((hi2c->State == HAL_I2C_STATE_MASTER_BUSY_RX) || (hi2c->State == HAL_I2C_STATE_MEM_BUSY_RX))
  2024. {
  2025. I2C_MasterReceive_ISR(hi2c);
  2026. }
  2027. }
  2028. }
  2029. /**
  2030. * @brief This function handles I2C error interrupt request.
  2031. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2032. * the configuration information for the specified I2C.
  2033. * @retval None
  2034. */
  2035. void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef *hi2c)
  2036. {
  2037. /* I2C Bus error interrupt occurred ------------------------------------*/
  2038. if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BERR) == SET) && (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_ERRI) == SET))
  2039. {
  2040. hi2c->ErrorCode |= HAL_I2C_ERROR_BERR;
  2041. /* Clear BERR flag */
  2042. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_BERR);
  2043. }
  2044. /* I2C Over-Run/Under-Run interrupt occurred ----------------------------------------*/
  2045. if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_OVR) == SET) && (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_ERRI) == SET))
  2046. {
  2047. hi2c->ErrorCode |= HAL_I2C_ERROR_OVR;
  2048. /* Clear OVR flag */
  2049. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_OVR);
  2050. }
  2051. /* I2C Arbitration Loss error interrupt occurred -------------------------------------*/
  2052. if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ARLO) == SET) && (__HAL_I2C_GET_IT_SOURCE(hi2c, I2C_IT_ERRI) == SET))
  2053. {
  2054. hi2c->ErrorCode |= HAL_I2C_ERROR_ARLO;
  2055. /* Clear ARLO flag */
  2056. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ARLO);
  2057. }
  2058. /* Call the Error Callback in case of Error detected */
  2059. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  2060. {
  2061. hi2c->State = HAL_I2C_STATE_READY;
  2062. HAL_I2C_ErrorCallback(hi2c);
  2063. }
  2064. }
  2065. /**
  2066. * @brief Master Tx Transfer completed callbacks.
  2067. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2068. * the configuration information for the specified I2C.
  2069. * @retval None
  2070. */
  2071. __weak void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
  2072. {
  2073. /* NOTE : This function Should not be modified, when the callback is needed,
  2074. the HAL_I2C_TxCpltCallback could be implemented in the user file
  2075. */
  2076. }
  2077. /**
  2078. * @brief Master Rx Transfer completed callbacks.
  2079. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2080. * the configuration information for the specified I2C.
  2081. * @retval None
  2082. */
  2083. __weak void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
  2084. {
  2085. /* NOTE : This function Should not be modified, when the callback is needed,
  2086. the HAL_I2C_TxCpltCallback could be implemented in the user file
  2087. */
  2088. }
  2089. /** @brief Slave Tx Transfer completed callbacks.
  2090. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2091. * the configuration information for the specified I2C.
  2092. * @retval None
  2093. */
  2094. __weak void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c)
  2095. {
  2096. /* NOTE : This function Should not be modified, when the callback is needed,
  2097. the HAL_I2C_TxCpltCallback could be implemented in the user file
  2098. */
  2099. }
  2100. /**
  2101. * @brief Slave Rx Transfer completed callbacks.
  2102. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2103. * the configuration information for the specified I2C.
  2104. * @retval None
  2105. */
  2106. __weak void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
  2107. {
  2108. /* NOTE : This function Should not be modified, when the callback is needed,
  2109. the HAL_I2C_TxCpltCallback could be implemented in the user file
  2110. */
  2111. }
  2112. /**
  2113. * @brief Memory Tx Transfer completed callbacks.
  2114. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2115. * the configuration information for the specified I2C.
  2116. * @retval None
  2117. */
  2118. __weak void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c)
  2119. {
  2120. /* NOTE : This function Should not be modified, when the callback is needed,
  2121. the HAL_I2C_TxCpltCallback could be implemented in the user file
  2122. */
  2123. }
  2124. /**
  2125. * @brief Memory Rx Transfer completed callbacks.
  2126. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2127. * the configuration information for the specified I2C.
  2128. * @retval None
  2129. */
  2130. __weak void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c)
  2131. {
  2132. /* NOTE : This function Should not be modified, when the callback is needed,
  2133. the HAL_I2C_TxCpltCallback could be implemented in the user file
  2134. */
  2135. }
  2136. /**
  2137. * @brief I2C error callbacks.
  2138. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2139. * the configuration information for the specified I2C.
  2140. * @retval None
  2141. */
  2142. __weak void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
  2143. {
  2144. /* NOTE : This function Should not be modified, when the callback is needed,
  2145. the HAL_I2C_ErrorCallback could be implemented in the user file
  2146. */
  2147. }
  2148. /**
  2149. * @}
  2150. */
  2151. /** @defgroup I2C_Exported_Functions_Group3 Peripheral State and Errors functions
  2152. * @brief Peripheral State and Errors functions
  2153. *
  2154. @verbatim
  2155. ===============================================================================
  2156. ##### Peripheral State and Errors functions #####
  2157. ===============================================================================
  2158. [..]
  2159. This subsection permit to get in run-time the status of the peripheral
  2160. and the data flow.
  2161. @endverbatim
  2162. * @{
  2163. */
  2164. /**
  2165. * @brief Returns the I2C state.
  2166. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2167. * the configuration information for the specified I2C.
  2168. * @retval HAL state
  2169. */
  2170. HAL_I2C_StateTypeDef HAL_I2C_GetState(I2C_HandleTypeDef *hi2c)
  2171. {
  2172. return hi2c->State;
  2173. }
  2174. /**
  2175. * @brief Return the I2C error code
  2176. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2177. * the configuration information for the specified I2C.
  2178. * @retval I2C Error Code
  2179. */
  2180. uint32_t HAL_I2C_GetError(I2C_HandleTypeDef *hi2c)
  2181. {
  2182. return hi2c->ErrorCode;
  2183. }
  2184. /**
  2185. * @}
  2186. */
  2187. /**
  2188. * @}
  2189. */
  2190. /** @addtogroup I2C_Private_Functions
  2191. * @{
  2192. */
  2193. /**
  2194. * @brief Handle Interrupt Flags Master Transmit Mode
  2195. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2196. * the configuration information for the specified I2C.
  2197. * @retval HAL status
  2198. */
  2199. static HAL_StatusTypeDef I2C_MasterTransmit_ISR(I2C_HandleTypeDef *hi2c)
  2200. {
  2201. uint16_t DevAddress;
  2202. /* Process Locked */
  2203. __HAL_LOCK(hi2c);
  2204. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == SET)
  2205. {
  2206. /* Write data to TXDR */
  2207. hi2c->Instance->TXDR = (*hi2c->pBuffPtr++);
  2208. hi2c->XferSize--;
  2209. hi2c->XferCount--;
  2210. }
  2211. else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET)
  2212. {
  2213. if((hi2c->XferSize == 0)&&(hi2c->XferCount!=0))
  2214. {
  2215. DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
  2216. if(hi2c->XferCount > 255)
  2217. {
  2218. I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  2219. hi2c->XferSize = 255;
  2220. }
  2221. else
  2222. {
  2223. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferCount, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  2224. hi2c->XferSize = hi2c->XferCount;
  2225. }
  2226. }
  2227. else
  2228. {
  2229. /* Process Unlocked */
  2230. __HAL_UNLOCK(hi2c);
  2231. /* Wrong size Status regarding TCR flag event */
  2232. hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
  2233. HAL_I2C_ErrorCallback(hi2c);
  2234. }
  2235. }
  2236. else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET)
  2237. {
  2238. if(hi2c->XferCount == 0)
  2239. {
  2240. /* Generate Stop */
  2241. hi2c->Instance->CR2 |= I2C_CR2_STOP;
  2242. }
  2243. else
  2244. {
  2245. /* Process Unlocked */
  2246. __HAL_UNLOCK(hi2c);
  2247. /* Wrong size Status regarding TCR flag event */
  2248. hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
  2249. HAL_I2C_ErrorCallback(hi2c);
  2250. }
  2251. }
  2252. else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
  2253. {
  2254. /* Disable ERR, TC, STOP, NACK, TXI interrupt */
  2255. __HAL_I2C_DISABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_TXI );
  2256. /* Clear STOP Flag */
  2257. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2258. /* Clear Configuration Register 2 */
  2259. I2C_RESET_CR2(hi2c);
  2260. hi2c->State = HAL_I2C_STATE_READY;
  2261. /* Process Unlocked */
  2262. __HAL_UNLOCK(hi2c);
  2263. if(hi2c->State == HAL_I2C_STATE_MEM_BUSY_TX)
  2264. {
  2265. HAL_I2C_MemTxCpltCallback(hi2c);
  2266. }
  2267. else
  2268. {
  2269. HAL_I2C_MasterTxCpltCallback(hi2c);
  2270. }
  2271. }
  2272. else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
  2273. {
  2274. /* Clear NACK Flag */
  2275. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  2276. /* Process Unlocked */
  2277. __HAL_UNLOCK(hi2c);
  2278. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  2279. HAL_I2C_ErrorCallback(hi2c);
  2280. }
  2281. /* Process Unlocked */
  2282. __HAL_UNLOCK(hi2c);
  2283. return HAL_OK;
  2284. }
  2285. /**
  2286. * @brief Handle Interrupt Flags Master Receive Mode
  2287. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2288. * the configuration information for the specified I2C.
  2289. * @retval HAL status
  2290. */
  2291. static HAL_StatusTypeDef I2C_MasterReceive_ISR(I2C_HandleTypeDef *hi2c)
  2292. {
  2293. uint16_t DevAddress;
  2294. /* Process Locked */
  2295. __HAL_LOCK(hi2c);
  2296. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
  2297. {
  2298. /* Read data from RXDR */
  2299. (*hi2c->pBuffPtr++) = hi2c->Instance->RXDR;
  2300. hi2c->XferSize--;
  2301. hi2c->XferCount--;
  2302. }
  2303. else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TCR) == SET)
  2304. {
  2305. if((hi2c->XferSize == 0)&&(hi2c->XferCount!=0))
  2306. {
  2307. DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
  2308. if(hi2c->XferCount > 255)
  2309. {
  2310. I2C_TransferConfig(hi2c,DevAddress,255, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  2311. hi2c->XferSize = 255;
  2312. }
  2313. else
  2314. {
  2315. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferCount, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  2316. hi2c->XferSize = hi2c->XferCount;
  2317. }
  2318. }
  2319. else
  2320. {
  2321. /* Process Unlocked */
  2322. __HAL_UNLOCK(hi2c);
  2323. /* Wrong size Status regarding TCR flag event */
  2324. hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
  2325. HAL_I2C_ErrorCallback(hi2c);
  2326. }
  2327. }
  2328. else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TC) == SET)
  2329. {
  2330. if(hi2c->XferCount == 0)
  2331. {
  2332. /* Generate Stop */
  2333. hi2c->Instance->CR2 |= I2C_CR2_STOP;
  2334. }
  2335. else
  2336. {
  2337. /* Process Unlocked */
  2338. __HAL_UNLOCK(hi2c);
  2339. /* Wrong size Status regarding TCR flag event */
  2340. hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
  2341. HAL_I2C_ErrorCallback(hi2c);
  2342. }
  2343. }
  2344. else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
  2345. {
  2346. /* Disable ERR, TC, STOP, NACK, TXI interrupt */
  2347. __HAL_I2C_DISABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_RXI );
  2348. /* Clear STOP Flag */
  2349. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2350. /* Clear Configuration Register 2 */
  2351. I2C_RESET_CR2(hi2c);
  2352. hi2c->State = HAL_I2C_STATE_READY;
  2353. /* Process Unlocked */
  2354. __HAL_UNLOCK(hi2c);
  2355. if(hi2c->State == HAL_I2C_STATE_MEM_BUSY_RX)
  2356. {
  2357. HAL_I2C_MemRxCpltCallback(hi2c);
  2358. }
  2359. else
  2360. {
  2361. HAL_I2C_MasterRxCpltCallback(hi2c);
  2362. }
  2363. }
  2364. else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
  2365. {
  2366. /* Clear NACK Flag */
  2367. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  2368. /* Process Unlocked */
  2369. __HAL_UNLOCK(hi2c);
  2370. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  2371. HAL_I2C_ErrorCallback(hi2c);
  2372. }
  2373. /* Process Unlocked */
  2374. __HAL_UNLOCK(hi2c);
  2375. return HAL_OK;
  2376. }
  2377. /**
  2378. * @brief Handle Interrupt Flags Slave Transmit Mode
  2379. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2380. * the configuration information for the specified I2C.
  2381. * @retval HAL status
  2382. */
  2383. static HAL_StatusTypeDef I2C_SlaveTransmit_ISR(I2C_HandleTypeDef *hi2c)
  2384. {
  2385. /* Process locked */
  2386. __HAL_LOCK(hi2c);
  2387. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) != RESET)
  2388. {
  2389. /* Check that I2C transfer finished */
  2390. /* if yes, normal usecase, a NACK is sent by the MASTER when Transfer is finished */
  2391. /* Mean XferCount == 0*/
  2392. /* So clear Flag NACKF only */
  2393. if(hi2c->XferCount == 0)
  2394. {
  2395. /* Clear NACK Flag */
  2396. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  2397. /* Process Unlocked */
  2398. __HAL_UNLOCK(hi2c);
  2399. }
  2400. else
  2401. {
  2402. /* if no, error usecase, a Non-Acknowledge of last Data is generated by the MASTER*/
  2403. /* Clear NACK Flag */
  2404. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  2405. /* Set ErrorCode corresponding to a Non-Acknowledge */
  2406. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  2407. /* Process Unlocked */
  2408. __HAL_UNLOCK(hi2c);
  2409. /* Call the Error callback to prevent upper layer */
  2410. HAL_I2C_ErrorCallback(hi2c);
  2411. }
  2412. }
  2413. else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)
  2414. {
  2415. /* Clear ADDR flag */
  2416. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  2417. }
  2418. /* Check first if STOPF is set */
  2419. /* to prevent a Write Data in TX buffer */
  2420. /* which is stuck in TXDR until next */
  2421. /* communication with Master */
  2422. else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
  2423. {
  2424. /* Disable ERRI, TCI, STOPI, NACKI, ADDRI, RXI, TXI interrupt */
  2425. __HAL_I2C_DISABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI );
  2426. /* Disable Address Acknowledge */
  2427. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  2428. /* Clear STOP Flag */
  2429. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2430. hi2c->State = HAL_I2C_STATE_READY;
  2431. /* Process Unlocked */
  2432. __HAL_UNLOCK(hi2c);
  2433. HAL_I2C_SlaveTxCpltCallback(hi2c);
  2434. }
  2435. else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == SET)
  2436. {
  2437. /* Write data to TXDR only if XferCount not reach "0" */
  2438. /* A TXIS flag can be set, during STOP treatment */
  2439. if(hi2c->XferCount > 0)
  2440. {
  2441. /* Write data to TXDR */
  2442. hi2c->Instance->TXDR = (*hi2c->pBuffPtr++);
  2443. hi2c->XferCount--;
  2444. }
  2445. }
  2446. /* Process Unlocked */
  2447. __HAL_UNLOCK(hi2c);
  2448. return HAL_OK;
  2449. }
  2450. /**
  2451. * @brief Handle Interrupt Flags Slave Receive Mode
  2452. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2453. * the configuration information for the specified I2C.
  2454. * @retval HAL status
  2455. */
  2456. static HAL_StatusTypeDef I2C_SlaveReceive_ISR(I2C_HandleTypeDef *hi2c)
  2457. {
  2458. /* Process Locked */
  2459. __HAL_LOCK(hi2c);
  2460. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) != RESET)
  2461. {
  2462. /* Clear NACK Flag */
  2463. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  2464. /* Process Unlocked */
  2465. __HAL_UNLOCK(hi2c);
  2466. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  2467. HAL_I2C_ErrorCallback(hi2c);
  2468. }
  2469. else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)
  2470. {
  2471. /* Clear ADDR flag */
  2472. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  2473. }
  2474. else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
  2475. {
  2476. /* Read data from RXDR */
  2477. (*hi2c->pBuffPtr++) = hi2c->Instance->RXDR;
  2478. hi2c->XferSize--;
  2479. hi2c->XferCount--;
  2480. }
  2481. else if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
  2482. {
  2483. /* Disable ERRI, TCI, STOPI, NACKI, ADDRI, RXI, TXI interrupt */
  2484. __HAL_I2C_DISABLE_IT(hi2c,I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_RXI );
  2485. /* Disable Address Acknowledge */
  2486. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  2487. /* Clear STOP Flag */
  2488. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2489. hi2c->State = HAL_I2C_STATE_READY;
  2490. /* Process Unlocked */
  2491. __HAL_UNLOCK(hi2c);
  2492. HAL_I2C_SlaveRxCpltCallback(hi2c);
  2493. }
  2494. /* Process Unlocked */
  2495. __HAL_UNLOCK(hi2c);
  2496. return HAL_OK;
  2497. }
  2498. /**
  2499. * @brief Master sends target device address followed by internal memory address for write request.
  2500. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2501. * the configuration information for the specified I2C.
  2502. * @param DevAddress: Target device address
  2503. * @param MemAddress: Internal memory address
  2504. * @param MemAddSize: Size of internal memory address
  2505. * @param Timeout: Timeout duration
  2506. * @retval HAL status
  2507. */
  2508. static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout)
  2509. {
  2510. I2C_TransferConfig(hi2c,DevAddress,MemAddSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE);
  2511. /* Wait until TXIS flag is set */
  2512. if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
  2513. {
  2514. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2515. {
  2516. return HAL_ERROR;
  2517. }
  2518. else
  2519. {
  2520. return HAL_TIMEOUT;
  2521. }
  2522. }
  2523. /* If Memory address size is 8Bit */
  2524. if(MemAddSize == I2C_MEMADD_SIZE_8BIT)
  2525. {
  2526. /* Send Memory Address */
  2527. hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
  2528. }
  2529. /* If Memory address size is 16Bit */
  2530. else
  2531. {
  2532. /* Send MSB of Memory Address */
  2533. hi2c->Instance->TXDR = I2C_MEM_ADD_MSB(MemAddress);
  2534. /* Wait until TXIS flag is set */
  2535. if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
  2536. {
  2537. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2538. {
  2539. return HAL_ERROR;
  2540. }
  2541. else
  2542. {
  2543. return HAL_TIMEOUT;
  2544. }
  2545. }
  2546. /* Send LSB of Memory Address */
  2547. hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
  2548. }
  2549. /* Wait until TCR flag is set */
  2550. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout) != HAL_OK)
  2551. {
  2552. return HAL_TIMEOUT;
  2553. }
  2554. return HAL_OK;
  2555. }
  2556. /**
  2557. * @brief Master sends target device address followed by internal memory address for read request.
  2558. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  2559. * the configuration information for the specified I2C.
  2560. * @param DevAddress: Target device address
  2561. * @param MemAddress: Internal memory address
  2562. * @param MemAddSize: Size of internal memory address
  2563. * @param Timeout: Timeout duration
  2564. * @retval HAL status
  2565. */
  2566. static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout)
  2567. {
  2568. I2C_TransferConfig(hi2c,DevAddress,MemAddSize, I2C_SOFTEND_MODE, I2C_GENERATE_START_WRITE);
  2569. /* Wait until TXIS flag is set */
  2570. if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
  2571. {
  2572. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2573. {
  2574. return HAL_ERROR;
  2575. }
  2576. else
  2577. {
  2578. return HAL_TIMEOUT;
  2579. }
  2580. }
  2581. /* If Memory address size is 8Bit */
  2582. if(MemAddSize == I2C_MEMADD_SIZE_8BIT)
  2583. {
  2584. /* Send Memory Address */
  2585. hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
  2586. }
  2587. /* If Memory address size is 16Bit */
  2588. else
  2589. {
  2590. /* Send MSB of Memory Address */
  2591. hi2c->Instance->TXDR = I2C_MEM_ADD_MSB(MemAddress);
  2592. /* Wait until TXIS flag is set */
  2593. if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout) != HAL_OK)
  2594. {
  2595. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2596. {
  2597. return HAL_ERROR;
  2598. }
  2599. else
  2600. {
  2601. return HAL_TIMEOUT;
  2602. }
  2603. }
  2604. /* Send LSB of Memory Address */
  2605. hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress);
  2606. }
  2607. /* Wait until TC flag is set */
  2608. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TC, RESET, Timeout) != HAL_OK)
  2609. {
  2610. return HAL_TIMEOUT;
  2611. }
  2612. return HAL_OK;
  2613. }
  2614. /**
  2615. * @brief DMA I2C master transmit process complete callback.
  2616. * @param hdma: DMA handle
  2617. * @retval None
  2618. */
  2619. static void I2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma)
  2620. {
  2621. uint16_t DevAddress;
  2622. I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  2623. /* Check if last DMA request was done with RELOAD */
  2624. /* Set NBYTES to write and reload if size > 255 */
  2625. if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
  2626. {
  2627. /* Wait until TCR flag is set */
  2628. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, I2C_TIMEOUT_TCR) != HAL_OK)
  2629. {
  2630. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2631. }
  2632. /* Disable DMA Request */
  2633. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  2634. /* Check if Errors has been detected during transfer */
  2635. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  2636. {
  2637. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2638. /* Wait until STOPF flag is reset */
  2639. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  2640. {
  2641. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2642. {
  2643. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  2644. }
  2645. else
  2646. {
  2647. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2648. }
  2649. }
  2650. /* Clear STOP Flag */
  2651. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2652. /* Clear Configuration Register 2 */
  2653. I2C_RESET_CR2(hi2c);
  2654. hi2c->XferCount = 0;
  2655. hi2c->State = HAL_I2C_STATE_READY;
  2656. HAL_I2C_ErrorCallback(hi2c);
  2657. }
  2658. else
  2659. {
  2660. hi2c->pBuffPtr += hi2c->XferSize;
  2661. hi2c->XferCount -= hi2c->XferSize;
  2662. if(hi2c->XferCount > 255)
  2663. {
  2664. hi2c->XferSize = 255;
  2665. }
  2666. else
  2667. {
  2668. hi2c->XferSize = hi2c->XferCount;
  2669. }
  2670. DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
  2671. /* Enable the DMA channel */
  2672. HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
  2673. /* Send Slave Address */
  2674. /* Set NBYTES to write and reload if size > 255 */
  2675. if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
  2676. {
  2677. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  2678. }
  2679. else
  2680. {
  2681. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  2682. }
  2683. /* Wait until TXIS flag is set */
  2684. if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, I2C_TIMEOUT_TXIS) != HAL_OK)
  2685. {
  2686. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2687. /* Wait until STOPF flag is reset */
  2688. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  2689. {
  2690. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2691. {
  2692. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  2693. }
  2694. else
  2695. {
  2696. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2697. }
  2698. }
  2699. /* Clear STOP Flag */
  2700. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2701. /* Clear Configuration Register 2 */
  2702. I2C_RESET_CR2(hi2c);
  2703. hi2c->XferCount = 0;
  2704. hi2c->State = HAL_I2C_STATE_READY;
  2705. HAL_I2C_ErrorCallback(hi2c);
  2706. }
  2707. else
  2708. {
  2709. /* Enable DMA Request */
  2710. hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  2711. }
  2712. }
  2713. }
  2714. else
  2715. {
  2716. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2717. /* Wait until STOPF flag is reset */
  2718. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  2719. {
  2720. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2721. {
  2722. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  2723. }
  2724. else
  2725. {
  2726. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2727. }
  2728. }
  2729. /* Clear STOP Flag */
  2730. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2731. /* Clear Configuration Register 2 */
  2732. I2C_RESET_CR2(hi2c);
  2733. /* Disable DMA Request */
  2734. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  2735. hi2c->XferCount = 0;
  2736. hi2c->State = HAL_I2C_STATE_READY;
  2737. /* Check if Errors has been detected during transfer */
  2738. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  2739. {
  2740. HAL_I2C_ErrorCallback(hi2c);
  2741. }
  2742. else
  2743. {
  2744. HAL_I2C_MasterTxCpltCallback(hi2c);
  2745. }
  2746. }
  2747. }
  2748. /**
  2749. * @brief DMA I2C slave transmit process complete callback.
  2750. * @param hdma: DMA handle
  2751. * @retval None
  2752. */
  2753. static void I2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma)
  2754. {
  2755. I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  2756. /* Wait until STOP flag is set */
  2757. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  2758. {
  2759. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2760. {
  2761. /* Normal Use case, a AF is generated by master */
  2762. /* to inform slave the end of transfer */
  2763. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2764. }
  2765. else
  2766. {
  2767. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2768. }
  2769. }
  2770. /* Clear STOP flag */
  2771. __HAL_I2C_CLEAR_FLAG(hi2c,I2C_FLAG_STOPF);
  2772. /* Wait until BUSY flag is reset */
  2773. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY) != HAL_OK)
  2774. {
  2775. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2776. }
  2777. /* Disable DMA Request */
  2778. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  2779. hi2c->XferCount = 0;
  2780. hi2c->State = HAL_I2C_STATE_READY;
  2781. /* Check if Errors has been detected during transfer */
  2782. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  2783. {
  2784. HAL_I2C_ErrorCallback(hi2c);
  2785. }
  2786. else
  2787. {
  2788. HAL_I2C_SlaveTxCpltCallback(hi2c);
  2789. }
  2790. }
  2791. /**
  2792. * @brief DMA I2C master receive process complete callback
  2793. * @param hdma: DMA handle
  2794. * @retval None
  2795. */
  2796. static void I2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma)
  2797. {
  2798. I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  2799. uint16_t DevAddress;
  2800. /* Check if last DMA request was done with RELOAD */
  2801. /* Set NBYTES to write and reload if size > 255 */
  2802. if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
  2803. {
  2804. /* Wait until TCR flag is set */
  2805. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, I2C_TIMEOUT_TCR) != HAL_OK)
  2806. {
  2807. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2808. }
  2809. /* Disable DMA Request */
  2810. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  2811. /* Check if Errors has been detected during transfer */
  2812. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  2813. {
  2814. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2815. /* Wait until STOPF flag is reset */
  2816. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  2817. {
  2818. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2819. {
  2820. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  2821. }
  2822. else
  2823. {
  2824. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2825. }
  2826. }
  2827. /* Clear STOP Flag */
  2828. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2829. /* Clear Configuration Register 2 */
  2830. I2C_RESET_CR2(hi2c);
  2831. hi2c->XferCount = 0;
  2832. hi2c->State = HAL_I2C_STATE_READY;
  2833. HAL_I2C_ErrorCallback(hi2c);
  2834. }
  2835. else
  2836. {
  2837. hi2c->pBuffPtr += hi2c->XferSize;
  2838. hi2c->XferCount -= hi2c->XferSize;
  2839. if(hi2c->XferCount > 255)
  2840. {
  2841. hi2c->XferSize = 255;
  2842. }
  2843. else
  2844. {
  2845. hi2c->XferSize = hi2c->XferCount;
  2846. }
  2847. DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
  2848. /* Enable the DMA channel */
  2849. HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
  2850. /* Send Slave Address */
  2851. /* Set NBYTES to write and reload if size > 255 */
  2852. if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
  2853. {
  2854. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  2855. }
  2856. else
  2857. {
  2858. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  2859. }
  2860. /* Wait until RXNE flag is set */
  2861. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, I2C_TIMEOUT_RXNE) != HAL_OK)
  2862. {
  2863. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2864. }
  2865. /* Check if Errors has been detected during transfer */
  2866. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  2867. {
  2868. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2869. /* Wait until STOPF flag is reset */
  2870. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  2871. {
  2872. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2873. {
  2874. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  2875. }
  2876. else
  2877. {
  2878. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2879. }
  2880. }
  2881. /* Clear STOP Flag */
  2882. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2883. /* Clear Configuration Register 2 */
  2884. I2C_RESET_CR2(hi2c);
  2885. hi2c->XferCount = 0;
  2886. hi2c->State = HAL_I2C_STATE_READY;
  2887. HAL_I2C_ErrorCallback(hi2c);
  2888. }
  2889. else
  2890. {
  2891. /* Enable DMA Request */
  2892. hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  2893. }
  2894. }
  2895. }
  2896. else
  2897. {
  2898. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2899. /* Wait until STOPF flag is reset */
  2900. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  2901. {
  2902. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2903. {
  2904. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  2905. }
  2906. else
  2907. {
  2908. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2909. }
  2910. }
  2911. /* Clear STOP Flag */
  2912. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2913. /* Clear Configuration Register 2 */
  2914. I2C_RESET_CR2(hi2c);
  2915. /* Disable DMA Request */
  2916. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  2917. hi2c->XferCount = 0;
  2918. hi2c->State = HAL_I2C_STATE_READY;
  2919. /* Check if Errors has been detected during transfer */
  2920. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  2921. {
  2922. HAL_I2C_ErrorCallback(hi2c);
  2923. }
  2924. else
  2925. {
  2926. HAL_I2C_MasterRxCpltCallback(hi2c);
  2927. }
  2928. }
  2929. }
  2930. /**
  2931. * @brief DMA I2C slave receive process complete callback.
  2932. * @param hdma: DMA handle
  2933. * @retval None
  2934. */
  2935. static void I2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma)
  2936. {
  2937. I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  2938. /* Wait until STOPF flag is reset */
  2939. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  2940. {
  2941. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2942. {
  2943. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  2944. }
  2945. else
  2946. {
  2947. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2948. }
  2949. }
  2950. /* Clear STOPF flag */
  2951. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  2952. /* Wait until BUSY flag is reset */
  2953. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY) != HAL_OK)
  2954. {
  2955. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2956. }
  2957. /* Disable DMA Request */
  2958. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  2959. /* Disable Address Acknowledge */
  2960. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  2961. hi2c->XferCount = 0;
  2962. hi2c->State = HAL_I2C_STATE_READY;
  2963. /* Check if Errors has been detected during transfer */
  2964. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  2965. {
  2966. HAL_I2C_ErrorCallback(hi2c);
  2967. }
  2968. else
  2969. {
  2970. HAL_I2C_SlaveRxCpltCallback(hi2c);
  2971. }
  2972. }
  2973. /**
  2974. * @brief DMA I2C Memory Write process complete callback
  2975. * @param hdma : DMA handle
  2976. * @retval None
  2977. */
  2978. static void I2C_DMAMemTransmitCplt(DMA_HandleTypeDef *hdma)
  2979. {
  2980. uint16_t DevAddress;
  2981. I2C_HandleTypeDef* hi2c = ( I2C_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2982. /* Check if last DMA request was done with RELOAD */
  2983. /* Set NBYTES to write and reload if size > 255 */
  2984. if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
  2985. {
  2986. /* Wait until TCR flag is set */
  2987. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, I2C_TIMEOUT_TCR) != HAL_OK)
  2988. {
  2989. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2990. }
  2991. /* Disable DMA Request */
  2992. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  2993. /* Check if Errors has been detected during transfer */
  2994. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  2995. {
  2996. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2997. /* Wait until STOPF flag is reset */
  2998. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  2999. {
  3000. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3001. {
  3002. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  3003. }
  3004. else
  3005. {
  3006. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3007. }
  3008. }
  3009. /* Clear STOP Flag */
  3010. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3011. /* Clear Configuration Register 2 */
  3012. I2C_RESET_CR2(hi2c);
  3013. hi2c->XferCount = 0;
  3014. hi2c->State = HAL_I2C_STATE_READY;
  3015. HAL_I2C_ErrorCallback(hi2c);
  3016. }
  3017. else
  3018. {
  3019. hi2c->pBuffPtr += hi2c->XferSize;
  3020. hi2c->XferCount -= hi2c->XferSize;
  3021. if(hi2c->XferCount > 255)
  3022. {
  3023. hi2c->XferSize = 255;
  3024. }
  3025. else
  3026. {
  3027. hi2c->XferSize = hi2c->XferCount;
  3028. }
  3029. DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
  3030. /* Enable the DMA channel */
  3031. HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->TXDR, hi2c->XferSize);
  3032. /* Send Slave Address */
  3033. /* Set NBYTES to write and reload if size > 255 */
  3034. if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
  3035. {
  3036. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  3037. }
  3038. else
  3039. {
  3040. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  3041. }
  3042. /* Wait until TXIS flag is set */
  3043. if(I2C_WaitOnTXISFlagUntilTimeout(hi2c, I2C_TIMEOUT_TXIS) != HAL_OK)
  3044. {
  3045. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  3046. /* Wait until STOPF flag is reset */
  3047. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  3048. {
  3049. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3050. {
  3051. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  3052. }
  3053. else
  3054. {
  3055. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3056. }
  3057. }
  3058. /* Clear STOP Flag */
  3059. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3060. /* Clear Configuration Register 2 */
  3061. I2C_RESET_CR2(hi2c);
  3062. hi2c->XferCount = 0;
  3063. hi2c->State = HAL_I2C_STATE_READY;
  3064. HAL_I2C_ErrorCallback(hi2c);
  3065. }
  3066. else
  3067. {
  3068. /* Enable DMA Request */
  3069. hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN;
  3070. }
  3071. }
  3072. }
  3073. else
  3074. {
  3075. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  3076. /* Wait until STOPF flag is reset */
  3077. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  3078. {
  3079. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3080. {
  3081. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  3082. }
  3083. else
  3084. {
  3085. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3086. }
  3087. }
  3088. /* Clear STOP Flag */
  3089. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3090. /* Clear Configuration Register 2 */
  3091. I2C_RESET_CR2(hi2c);
  3092. /* Disable DMA Request */
  3093. hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN;
  3094. hi2c->XferCount = 0;
  3095. hi2c->State = HAL_I2C_STATE_READY;
  3096. /* Check if Errors has been detected during transfer */
  3097. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  3098. {
  3099. HAL_I2C_ErrorCallback(hi2c);
  3100. }
  3101. else
  3102. {
  3103. HAL_I2C_MemTxCpltCallback(hi2c);
  3104. }
  3105. }
  3106. }
  3107. /**
  3108. * @brief DMA I2C Memory Read process complete callback
  3109. * @param hdma: DMA handle
  3110. * @retval None
  3111. */
  3112. static void I2C_DMAMemReceiveCplt(DMA_HandleTypeDef *hdma)
  3113. {
  3114. I2C_HandleTypeDef* hi2c = ( I2C_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  3115. uint16_t DevAddress;
  3116. /* Check if last DMA request was done with RELOAD */
  3117. /* Set NBYTES to write and reload if size > 255 */
  3118. if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
  3119. {
  3120. /* Wait until TCR flag is set */
  3121. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, I2C_TIMEOUT_TCR) != HAL_OK)
  3122. {
  3123. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3124. }
  3125. /* Disable DMA Request */
  3126. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  3127. /* Check if Errors has been detected during transfer */
  3128. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  3129. {
  3130. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  3131. /* Wait until STOPF flag is reset */
  3132. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  3133. {
  3134. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3135. {
  3136. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  3137. }
  3138. else
  3139. {
  3140. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3141. }
  3142. }
  3143. /* Clear STOP Flag */
  3144. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3145. /* Clear Configuration Register 2 */
  3146. I2C_RESET_CR2(hi2c);
  3147. hi2c->XferCount = 0;
  3148. hi2c->State = HAL_I2C_STATE_READY;
  3149. HAL_I2C_ErrorCallback(hi2c);
  3150. }
  3151. else
  3152. {
  3153. hi2c->pBuffPtr += hi2c->XferSize;
  3154. hi2c->XferCount -= hi2c->XferSize;
  3155. if(hi2c->XferCount > 255)
  3156. {
  3157. hi2c->XferSize = 255;
  3158. }
  3159. else
  3160. {
  3161. hi2c->XferSize = hi2c->XferCount;
  3162. }
  3163. DevAddress = (hi2c->Instance->CR2 & I2C_CR2_SADD);
  3164. /* Enable the DMA channel */
  3165. HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
  3166. /* Send Slave Address */
  3167. /* Set NBYTES to write and reload if size > 255 */
  3168. if( (hi2c->XferSize == 255) && (hi2c->XferSize < hi2c->XferCount) )
  3169. {
  3170. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
  3171. }
  3172. else
  3173. {
  3174. I2C_TransferConfig(hi2c,DevAddress,hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
  3175. }
  3176. /* Wait until RXNE flag is set */
  3177. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, I2C_TIMEOUT_RXNE) != HAL_OK)
  3178. {
  3179. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3180. }
  3181. /* Check if Errors has been detected during transfer */
  3182. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  3183. {
  3184. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  3185. /* Wait until STOPF flag is reset */
  3186. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  3187. {
  3188. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3189. {
  3190. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  3191. }
  3192. else
  3193. {
  3194. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3195. }
  3196. }
  3197. /* Clear STOP Flag */
  3198. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3199. /* Clear Configuration Register 2 */
  3200. I2C_RESET_CR2(hi2c);
  3201. hi2c->XferCount = 0;
  3202. hi2c->State = HAL_I2C_STATE_READY;
  3203. HAL_I2C_ErrorCallback(hi2c);
  3204. }
  3205. else
  3206. {
  3207. /* Enable DMA Request */
  3208. hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN;
  3209. }
  3210. }
  3211. }
  3212. else
  3213. {
  3214. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  3215. /* Wait until STOPF flag is reset */
  3216. if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, I2C_TIMEOUT_STOPF) != HAL_OK)
  3217. {
  3218. if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  3219. {
  3220. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  3221. }
  3222. else
  3223. {
  3224. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3225. }
  3226. }
  3227. /* Clear STOP Flag */
  3228. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3229. /* Clear Configuration Register 2 */
  3230. I2C_RESET_CR2(hi2c);
  3231. /* Disable DMA Request */
  3232. hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN;
  3233. hi2c->XferCount = 0;
  3234. hi2c->State = HAL_I2C_STATE_READY;
  3235. /* Check if Errors has been detected during transfer */
  3236. if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  3237. {
  3238. HAL_I2C_ErrorCallback(hi2c);
  3239. }
  3240. else
  3241. {
  3242. HAL_I2C_MemRxCpltCallback(hi2c);
  3243. }
  3244. }
  3245. }
  3246. /**
  3247. * @brief DMA I2C communication error callback.
  3248. * @param hdma : DMA handle
  3249. * @retval None
  3250. */
  3251. static void I2C_DMAError(DMA_HandleTypeDef *hdma)
  3252. {
  3253. I2C_HandleTypeDef* hi2c = ( I2C_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  3254. /* Disable Acknowledge */
  3255. hi2c->Instance->CR2 |= I2C_CR2_NACK;
  3256. hi2c->XferCount = 0;
  3257. hi2c->State = HAL_I2C_STATE_READY;
  3258. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  3259. HAL_I2C_ErrorCallback(hi2c);
  3260. }
  3261. /**
  3262. * @brief This function handles I2C Communication Timeout.
  3263. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  3264. * the configuration information for the specified I2C.
  3265. * @param Flag: specifies the I2C flag to check.
  3266. * @param Status: The new Flag status (SET or RESET).
  3267. * @param Timeout: Timeout duration
  3268. * @retval HAL status
  3269. */
  3270. static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
  3271. {
  3272. uint32_t tickstart = HAL_GetTick();
  3273. /* Wait until flag is set */
  3274. if(Status == RESET)
  3275. {
  3276. while(__HAL_I2C_GET_FLAG(hi2c, Flag) == RESET)
  3277. {
  3278. /* Check for the Timeout */
  3279. if(Timeout != HAL_MAX_DELAY)
  3280. {
  3281. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  3282. {
  3283. hi2c->State= HAL_I2C_STATE_READY;
  3284. /* Process Unlocked */
  3285. __HAL_UNLOCK(hi2c);
  3286. return HAL_TIMEOUT;
  3287. }
  3288. }
  3289. }
  3290. }
  3291. else
  3292. {
  3293. while(__HAL_I2C_GET_FLAG(hi2c, Flag) != RESET)
  3294. {
  3295. /* Check for the Timeout */
  3296. if(Timeout != HAL_MAX_DELAY)
  3297. {
  3298. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  3299. {
  3300. hi2c->State= HAL_I2C_STATE_READY;
  3301. /* Process Unlocked */
  3302. __HAL_UNLOCK(hi2c);
  3303. return HAL_TIMEOUT;
  3304. }
  3305. }
  3306. }
  3307. }
  3308. return HAL_OK;
  3309. }
  3310. /**
  3311. * @brief This function handles I2C Communication Timeout for specific usage of TXIS flag.
  3312. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  3313. * the configuration information for the specified I2C.
  3314. * @param Timeout: Timeout duration
  3315. * @retval HAL status
  3316. */
  3317. static HAL_StatusTypeDef I2C_WaitOnTXISFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout)
  3318. {
  3319. uint32_t tickstart = HAL_GetTick();
  3320. while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == RESET)
  3321. {
  3322. /* Check if a NACK is detected */
  3323. if(I2C_IsAcknowledgeFailed(hi2c, Timeout) != HAL_OK)
  3324. {
  3325. return HAL_ERROR;
  3326. }
  3327. /* Check for the Timeout */
  3328. if(Timeout != HAL_MAX_DELAY)
  3329. {
  3330. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  3331. {
  3332. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3333. hi2c->State= HAL_I2C_STATE_READY;
  3334. /* Process Unlocked */
  3335. __HAL_UNLOCK(hi2c);
  3336. return HAL_TIMEOUT;
  3337. }
  3338. }
  3339. }
  3340. return HAL_OK;
  3341. }
  3342. /**
  3343. * @brief This function handles I2C Communication Timeout for specific usage of STOP flag.
  3344. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  3345. * the configuration information for the specified I2C.
  3346. * @param Timeout: Timeout duration
  3347. * @retval HAL status
  3348. */
  3349. static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout)
  3350. {
  3351. uint32_t tickstart = 0x00;
  3352. tickstart = HAL_GetTick();
  3353. while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
  3354. {
  3355. /* Check if a NACK is detected */
  3356. if(I2C_IsAcknowledgeFailed(hi2c, Timeout) != HAL_OK)
  3357. {
  3358. return HAL_ERROR;
  3359. }
  3360. /* Check for the Timeout */
  3361. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  3362. {
  3363. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3364. hi2c->State= HAL_I2C_STATE_READY;
  3365. /* Process Unlocked */
  3366. __HAL_UNLOCK(hi2c);
  3367. return HAL_TIMEOUT;
  3368. }
  3369. }
  3370. return HAL_OK;
  3371. }
  3372. /**
  3373. * @brief This function handles I2C Communication Timeout for specific usage of RXNE flag.
  3374. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  3375. * the configuration information for the specified I2C.
  3376. * @param Timeout: Timeout duration
  3377. * @retval HAL status
  3378. */
  3379. static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout)
  3380. {
  3381. uint32_t tickstart = 0x00;
  3382. tickstart = HAL_GetTick();
  3383. while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == RESET)
  3384. {
  3385. /* Check if a STOPF is detected */
  3386. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
  3387. {
  3388. /* Clear STOP Flag */
  3389. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3390. /* Clear Configuration Register 2 */
  3391. I2C_RESET_CR2(hi2c);
  3392. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3393. hi2c->State= HAL_I2C_STATE_READY;
  3394. /* Process Unlocked */
  3395. __HAL_UNLOCK(hi2c);
  3396. return HAL_ERROR;
  3397. }
  3398. /* Check for the Timeout */
  3399. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  3400. {
  3401. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3402. hi2c->State= HAL_I2C_STATE_READY;
  3403. /* Process Unlocked */
  3404. __HAL_UNLOCK(hi2c);
  3405. return HAL_TIMEOUT;
  3406. }
  3407. }
  3408. return HAL_OK;
  3409. }
  3410. /**
  3411. * @brief This function handles Acknowledge failed detection during an I2C Communication.
  3412. * @param hi2c : Pointer to a I2C_HandleTypeDef structure that contains
  3413. * the configuration information for the specified I2C.
  3414. * @param Timeout: Timeout duration
  3415. * @retval HAL status
  3416. */
  3417. static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c, uint32_t Timeout)
  3418. {
  3419. uint32_t tickstart = 0x00;
  3420. tickstart = HAL_GetTick();
  3421. if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
  3422. {
  3423. /* Generate stop if necessary only in case of I2C peripheral in MASTER mode */
  3424. if((hi2c->State == HAL_I2C_STATE_MASTER_BUSY_TX) || (hi2c->State == HAL_I2C_STATE_MEM_BUSY_TX)
  3425. || (hi2c->State == HAL_I2C_STATE_MEM_BUSY_RX))
  3426. {
  3427. /* No need to generate the STOP condition if AUTOEND mode is enabled */
  3428. /* Generate the STOP condition only in case of SOFTEND mode is enabled */
  3429. if((hi2c->Instance->CR2 & I2C_AUTOEND_MODE) != I2C_AUTOEND_MODE)
  3430. {
  3431. /* Generate Stop */
  3432. hi2c->Instance->CR2 |= I2C_CR2_STOP;
  3433. }
  3434. }
  3435. /* Wait until STOP Flag is reset */
  3436. /* AutoEnd should be initiate after AF */
  3437. while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
  3438. {
  3439. /* Check for the Timeout */
  3440. if(Timeout != HAL_MAX_DELAY)
  3441. {
  3442. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  3443. {
  3444. hi2c->State= HAL_I2C_STATE_READY;
  3445. /* Process Unlocked */
  3446. __HAL_UNLOCK(hi2c);
  3447. return HAL_TIMEOUT;
  3448. }
  3449. }
  3450. }
  3451. /* Clear NACKF Flag */
  3452. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  3453. /* Clear STOP Flag */
  3454. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  3455. /* Clear Configuration Register 2 */
  3456. I2C_RESET_CR2(hi2c);
  3457. hi2c->ErrorCode = HAL_I2C_ERROR_AF;
  3458. hi2c->State= HAL_I2C_STATE_READY;
  3459. /* Process Unlocked */
  3460. __HAL_UNLOCK(hi2c);
  3461. return HAL_ERROR;
  3462. }
  3463. return HAL_OK;
  3464. }
  3465. /**
  3466. * @brief Handles I2Cx communication when starting transfer or during transfer (TC or TCR flag are set).
  3467. * @param hi2c: I2C handle.
  3468. * @param DevAddress: specifies the slave address to be programmed.
  3469. * @param Size: specifies the number of bytes to be programmed.
  3470. * This parameter must be a value between 0 and 255.
  3471. * @param Mode: new state of the I2C START condition generation.
  3472. * This parameter can be one of the following values:
  3473. * @arg I2C_RELOAD_MODE: Enable Reload mode .
  3474. * @arg I2C_AUTOEND_MODE: Enable Automatic end mode.
  3475. * @arg I2C_SOFTEND_MODE: Enable Software end mode.
  3476. * @param Request: new state of the I2C START condition generation.
  3477. * This parameter can be one of the following values:
  3478. * @arg I2C_NO_STARTSTOP: Don't Generate stop and start condition.
  3479. * @arg I2C_GENERATE_STOP: Generate stop condition (Size should be set to 0).
  3480. * @arg I2C_GENERATE_START_READ: Generate Restart for read request.
  3481. * @arg I2C_GENERATE_START_WRITE: Generate Restart for write request.
  3482. * @retval None
  3483. */
  3484. static void I2C_TransferConfig(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t Size, uint32_t Mode, uint32_t Request)
  3485. {
  3486. uint32_t tmpreg = 0;
  3487. /* Check the parameters */
  3488. assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  3489. assert_param(IS_TRANSFER_MODE(Mode));
  3490. assert_param(IS_TRANSFER_REQUEST(Request));
  3491. /* Get the CR2 register value */
  3492. tmpreg = hi2c->Instance->CR2;
  3493. /* clear tmpreg specific bits */
  3494. tmpreg &= (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP));
  3495. /* update tmpreg */
  3496. tmpreg |= (uint32_t)(((uint32_t)DevAddress & I2C_CR2_SADD) | (((uint32_t)Size << 16 ) & I2C_CR2_NBYTES) | \
  3497. (uint32_t)Mode | (uint32_t)Request);
  3498. /* update CR2 register */
  3499. hi2c->Instance->CR2 = tmpreg;
  3500. }
  3501. /**
  3502. * @}
  3503. */
  3504. /**
  3505. * @}
  3506. */
  3507. #endif /* HAL_I2C_MODULE_ENABLED */
  3508. /**
  3509. * @}
  3510. */
  3511. /**
  3512. * @}
  3513. */
  3514. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/