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.
 
 
 

4817 lines
162 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_fmpi2c.c
  4. * @author MCD Application Team
  5. * @version V1.7.1
  6. * @date 14-April-2017
  7. * @brief FMPI2C HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Inter Integrated Circuit (FMPI2C) 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 FMPI2C HAL driver can be used as follows:
  20. (#) Declare a FMPI2C_HandleTypeDef handle structure, for example:
  21. FMPI2C_HandleTypeDef hfmpi2c;
  22. (#)Initialize the FMPI2C low level resources by implementing the HAL_FMPI2C_MspInit() API:
  23. (##) Enable the FMPI2Cx interface clock
  24. (##) FMPI2C pins configuration
  25. (+++) Enable the clock for the FMPI2C GPIOs
  26. (+++) Configure FMPI2C pins as alternate function open-drain
  27. (##) NVIC configuration if you need to use interrupt process
  28. (+++) Configure the FMPI2Cx interrupt priority
  29. (+++) Enable the NVIC FMPI2C IRQ Channel
  30. (##) DMA Configuration if you need to use DMA process
  31. (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive channel
  32. (+++) Enable the DMAx interface clock using
  33. (+++) Configure the DMA handle parameters
  34. (+++) Configure the DMA Tx or Rx channel
  35. (+++) Associate the initialized DMA handle to the hfmpi2c DMA Tx or Rx handle
  36. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on
  37. the DMA Tx or Rx channel
  38. (#) Configure the Communication Clock Timing, Own Address1, Master Addressing mode, Dual Addressing mode,
  39. Own Address2, Own Address2 Mask, General call and Nostretch mode in the hfmpi2c Init structure.
  40. (#) Initialize the FMPI2C registers by calling the HAL_FMPI2C_Init(), configures also the low level Hardware
  41. (GPIO, CLOCK, NVIC...etc) by calling the customized HAL_FMPI2C_MspInit(&hfmpi2c) API.
  42. (#) To check if target device is ready for communication, use the function HAL_FMPI2C_IsDeviceReady()
  43. (#) For FMPI2C IO and IO MEM operations, three operation modes are available within this driver :
  44. *** Polling mode IO operation ***
  45. =================================
  46. [..]
  47. (+) Transmit in master mode an amount of data in blocking mode using HAL_FMPI2C_Master_Transmit()
  48. (+) Receive in master mode an amount of data in blocking mode using HAL_FMPI2C_Master_Receive()
  49. (+) Transmit in slave mode an amount of data in blocking mode using HAL_FMPI2C_Slave_Transmit()
  50. (+) Receive in slave mode an amount of data in blocking mode using HAL_FMPI2C_Slave_Receive()
  51. *** Polling mode IO MEM operation ***
  52. =====================================
  53. [..]
  54. (+) Write an amount of data in blocking mode to a specific memory address using HAL_FMPI2C_Mem_Write()
  55. (+) Read an amount of data in blocking mode from a specific memory address using HAL_FMPI2C_Mem_Read()
  56. *** Interrupt mode IO operation ***
  57. ===================================
  58. [..]
  59. (+) Transmit in master mode an amount of data in non-blocking mode using HAL_FMPI2C_Master_Transmit_IT()
  60. (+) At transmission end of transfer, HAL_FMPI2C_MasterTxCpltCallback() is executed and user can
  61. add his own code by customization of function pointer HAL_FMPI2C_MasterTxCpltCallback()
  62. (+) Receive in master mode an amount of data in non-blocking mode using HAL_FMPI2C_Master_Receive_IT()
  63. (+) At reception end of transfer, HAL_FMPI2C_MasterRxCpltCallback() is executed and user can
  64. add his own code by customization of function pointer HAL_FMPI2C_MasterRxCpltCallback()
  65. (+) Transmit in slave mode an amount of data in non-blocking mode using HAL_FMPI2C_Slave_Transmit_IT()
  66. (+) At transmission end of transfer, HAL_FMPI2C_SlaveTxCpltCallback() is executed and user can
  67. add his own code by customization of function pointer HAL_FMPI2C_SlaveTxCpltCallback()
  68. (+) Receive in slave mode an amount of data in non-blocking mode using HAL_FMPI2C_Slave_Receive_IT()
  69. (+) At reception end of transfer, HAL_FMPI2C_SlaveRxCpltCallback() is executed and user can
  70. add his own code by customization of function pointer HAL_FMPI2C_SlaveRxCpltCallback()
  71. (+) In case of transfer Error, HAL_FMPI2C_ErrorCallback() function is executed and user can
  72. add his own code by customization of function pointer HAL_FMPI2C_ErrorCallback()
  73. (+) Abort a master FMPI2C process communication with Interrupt using HAL_FMPI2C_Master_Abort_IT()
  74. (+) End of abort process, HAL_FMPI2C_MasterRxCpltCallback() or HAL_FMPI2C_MasterTxCpltCallback() is executed and user can
  75. add his own code by customization of function pointer HAL_FMPI2C_MasterRxCpltCallback() or HAL_FMPI2C_MasterTxCpltCallback()
  76. (+) Discard a slave FMPI2C process communication using __HAL_FMPI2C_GENERATE_NACK() macro.
  77. This action will inform Master to generate a Stop condition to discard the communication.
  78. *** Interrupt mode IO sequential operation ***
  79. ===================================
  80. [..]
  81. (@) These interfaces allow to manage a sequential transfer with a repeated start condition
  82. when a direction change during transfer
  83. [..]
  84. (+) A specific option field manage the different steps of a sequential transfer
  85. (+) Option field values are defined through FMPI2C_XFEROPTIONS and are listed below:
  86. (++) FMPI2C_FIRST_AND_LAST_FRAME: No sequential usage, functionnal is same as associated interfaces in no sequential mode
  87. (++) FMPI2C_FIRST_FRAME: Sequential usage, this option allow to manage a sequence with start condition, address
  88. and data to transfer without a final stop condition
  89. (++) FMPI2C_FIRST_AND_NEXT_FRAME: Sequential usage (Master only), this option allow to manage a sequence with start condition, address
  90. and data to transfer without a final stop condition, an then permit a call the same master sequential interface
  91. several times (like HAL_FMPI2C_Master_Sequential_Transmit_IT() then HAL_FMPI2C_Master_Sequential_Transmit_IT())
  92. (++) FMPI2C_NEXT_FRAME: Sequential usage, this option allow to manage a sequence with a restart condition, address
  93. and with new data to transfer if the direction change or manage only the new data to transfer
  94. if no direction change and without a final stop condition in both cases
  95. (++) FMPI2C_LAST_FRAME: Sequential usage, this option allow to manage a sequance with a restart condition, address
  96. and with new data to transfer if the direction change or manage only the new data to transfer
  97. if no direction change and with a final stop condition in both cases
  98. (+) Differents sequential FMPI2C interfaces are listed below:
  99. (++) Sequential transmit in master FMPI2C mode an amount of data in non-blocking mode using HAL_FMPI2C_Master_Sequential_Transmit_IT()
  100. (+++) At transmission end of current frame transfer, HAL_FMPI2C_MasterTxCpltCallback() is executed and user can
  101. add his own code by customization of function pointer HAL_FMPI2C_MasterTxCpltCallback()
  102. (++) Sequential receive in master FMPI2C mode an amount of data in non-blocking mode using HAL_FMPI2C_Master_Sequential_Receive_IT()
  103. (+++) At reception end of current frame transfer, HAL_FMPI2C_MasterRxCpltCallback() is executed and user can
  104. add his own code by customization of function pointer HAL_FMPI2C_MasterRxCpltCallback()
  105. (++) Abort a master FMPI2C process communication with Interrupt using HAL_FMPI2C_Master_Abort_IT()
  106. (+++) End of abort process, HAL_FMPI2C_AbortCpltCallback() is executed and user can
  107. add his own code by customization of function pointer HAL_FMPI2C_AbortCpltCallback()
  108. (+++) mean HAL_FMPI2C_MasterTxCpltCallback() in case of previous state was master transmit
  109. (+++) mean HAL_FMPI2C_MasterRxCpltCallback() in case of previous state was master receive
  110. (++) Enable/disable the Address listen mode in slave FMPI2C mode using HAL_FMPI2C_EnableListen_IT() HAL_FMPI2C_DisableListen_IT()
  111. (+++) When address slave FMPI2C match, HAL_FMPI2C_AddrCallback() is executed and user can
  112. add his own code to check the Address Match Code and the transmission direction request by master (Write/Read).
  113. (+++) At Listen mode end HAL_FMPI2C_ListenCpltCallback() is executed and user can
  114. add his own code by customization of function pointer HAL_FMPI2C_ListenCpltCallback()
  115. (++) Sequential transmit in slave FMPI2C mode an amount of data in non-blocking mode using HAL_FMPI2C_Slave_Sequential_Transmit_IT()
  116. (+++) At transmission end of current frame transfer, HAL_FMPI2C_SlaveTxCpltCallback() is executed and user can
  117. add his own code by customization of function pointer HAL_FMPI2C_SlaveTxCpltCallback()
  118. (++) Sequential receive in slave FMPI2C mode an amount of data in non-blocking mode using HAL_FMPI2C_Slave_Sequential_Receive_IT()
  119. (+++) At reception end of current frame transfer, HAL_FMPI2C_SlaveRxCpltCallback() is executed and user can
  120. add his own code by customization of function pointer HAL_FMPI2C_SlaveRxCpltCallback()
  121. (++) In case of transfer Error, HAL_FMPI2C_ErrorCallback() function is executed and user can
  122. add his own code by customization of function pointer HAL_FMPI2C_ErrorCallback()
  123. (++) Abort a master FMPI2C process communication with Interrupt using HAL_FMPI2C_Master_Abort_IT()
  124. (++) End of abort process, HAL_FMPI2C_AbortCpltCallback() is executed and user can
  125. add his own code by customization of function pointer HAL_FMPI2C_AbortCpltCallback()
  126. (++) Discard a slave FMPI2C process communication using __HAL_FMPI2C_GENERATE_NACK() macro.
  127. This action will inform Master to generate a Stop condition to discard the communication.
  128. *** Interrupt mode IO MEM operation ***
  129. =======================================
  130. [..]
  131. (+) Write an amount of data in non-blocking mode with Interrupt to a specific memory address using
  132. HAL_FMPI2C_Mem_Write_IT()
  133. (+) At Memory end of write transfer, HAL_FMPI2C_MemTxCpltCallback() is executed and user can
  134. add his own code by customization of function pointer HAL_FMPI2C_MemTxCpltCallback()
  135. (+) Read an amount of data in non-blocking mode with Interrupt from a specific memory address using
  136. HAL_FMPI2C_Mem_Read_IT()
  137. (+) At Memory end of read transfer, HAL_FMPI2C_MemRxCpltCallback() is executed and user can
  138. add his own code by customization of function pointer HAL_FMPI2C_MemRxCpltCallback()
  139. (+) In case of transfer Error, HAL_FMPI2C_ErrorCallback() function is executed and user can
  140. add his own code by customization of function pointer HAL_FMPI2C_ErrorCallback()
  141. *** DMA mode IO operation ***
  142. ==============================
  143. [..]
  144. (+) Transmit in master mode an amount of data in non-blocking mode (DMA) using
  145. HAL_FMPI2C_Master_Transmit_DMA()
  146. (+) At transmission end of transfer, HAL_FMPI2C_MasterTxCpltCallback() is executed and user can
  147. add his own code by customization of function pointer HAL_FMPI2C_MasterTxCpltCallback()
  148. (+) Receive in master mode an amount of data in non-blocking mode (DMA) using
  149. HAL_FMPI2C_Master_Receive_DMA()
  150. (+) At reception end of transfer, HAL_FMPI2C_MasterRxCpltCallback() is executed and user can
  151. add his own code by customization of function pointer HAL_FMPI2C_MasterRxCpltCallback()
  152. (+) Transmit in slave mode an amount of data in non-blocking mode (DMA) using
  153. HAL_FMPI2C_Slave_Transmit_DMA()
  154. (+) At transmission end of transfer, HAL_FMPI2C_SlaveTxCpltCallback() is executed and user can
  155. add his own code by customization of function pointer HAL_FMPI2C_SlaveTxCpltCallback()
  156. (+) Receive in slave mode an amount of data in non-blocking mode (DMA) using
  157. HAL_FMPI2C_Slave_Receive_DMA()
  158. (+) At reception end of transfer, HAL_FMPI2C_SlaveRxCpltCallback() is executed and user can
  159. add his own code by customization of function pointer HAL_FMPI2C_SlaveRxCpltCallback()
  160. (+) In case of transfer Error, HAL_FMPI2C_ErrorCallback() function is executed and user can
  161. add his own code by customization of function pointer HAL_FMPI2C_ErrorCallback()
  162. (+) Abort a master FMPI2C process communication with Interrupt using HAL_FMPI2C_Master_Abort_IT()
  163. (+) End of abort process, HAL_FMPI2C_MasterRxCpltCallback() or HAL_FMPI2C_MasterTxCpltCallback() is executed and user can
  164. add his own code by customization of function pointer HAL_FMPI2C_MasterRxCpltCallback() or HAL_FMPI2C_MasterTxCpltCallback()
  165. (+) Discard a slave FMPI2C process communication using __HAL_FMPI2C_GENERATE_NACK() macro.
  166. This action will inform Master to generate a Stop condition to discard the communication.
  167. *** DMA mode IO MEM operation ***
  168. =================================
  169. [..]
  170. (+) Write an amount of data in non-blocking mode with DMA to a specific memory address using
  171. HAL_FMPI2C_Mem_Write_DMA()
  172. (+) At Memory end of write transfer, HAL_FMPI2C_MemTxCpltCallback() is executed and user can
  173. add his own code by customization of function pointer HAL_FMPI2C_MemTxCpltCallback()
  174. (+) Read an amount of data in non-blocking mode with DMA from a specific memory address using
  175. HAL_FMPI2C_Mem_Read_DMA()
  176. (+) At Memory end of read transfer, HAL_FMPI2C_MemRxCpltCallback() is executed and user can
  177. add his own code by customization of function pointer HAL_FMPI2C_MemRxCpltCallback()
  178. (+) In case of transfer Error, HAL_FMPI2C_ErrorCallback() function is executed and user can
  179. add his own code by customization of function pointer HAL_FMPI2C_ErrorCallback()
  180. *** FMPI2C HAL driver macros list ***
  181. ==================================
  182. [..]
  183. Below the list of most used macros in FMPI2C HAL driver.
  184. (+) __HAL_FMPI2C_ENABLE: Enable the FMPI2C peripheral
  185. (+) __HAL_FMPI2C_DISABLE: Disable the FMPI2C peripheral
  186. (+) __HAL_FMPI2C_GENERATE_NACK: Generate a Non-Acknowledge FMPI2C peripheral in Slave mode
  187. (+) __HAL_FMPI2C_GET_FLAG: Check whether the specified FMPI2C flag is set or not
  188. (+) __HAL_FMPI2C_CLEAR_FLAG: Clear the specified FMPI2C pending flag
  189. (+) __HAL_FMPI2C_ENABLE_IT: Enable the specified FMPI2C interrupt
  190. (+) __HAL_FMPI2C_DISABLE_IT: Disable the specified FMPI2C interrupt
  191. [..]
  192. (@) You can refer to the FMPI2C HAL driver header file for more useful macros
  193. @endverbatim
  194. ******************************************************************************
  195. * @attention
  196. *
  197. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  198. *
  199. * Redistribution and use in source and binary forms, with or without modification,
  200. * are permitted provided that the following conditions are met:
  201. * 1. Redistributions of source code must retain the above copyright notice,
  202. * this list of conditions and the following disclaimer.
  203. * 2. Redistributions in binary form must reproduce the above copyright notice,
  204. * this list of conditions and the following disclaimer in the documentation
  205. * and/or other materials provided with the distribution.
  206. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  207. * may be used to endorse or promote products derived from this software
  208. * without specific prior written permission.
  209. *
  210. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  211. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  212. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  213. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  214. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  215. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  216. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  217. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  218. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  219. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  220. *
  221. ******************************************************************************
  222. */
  223. /* Includes ------------------------------------------------------------------*/
  224. #include "stm32f4xx_hal.h"
  225. /** @addtogroup STM32F4xx_HAL_Driver
  226. * @{
  227. */
  228. /** @defgroup FMPI2C FMPI2C
  229. * @brief FMPI2C HAL module driver
  230. * @{
  231. */
  232. #ifdef HAL_FMPI2C_MODULE_ENABLED
  233. #if defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) || defined(STM32F446xx) || defined(STM32F412Zx) ||\
  234. defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
  235. /* Private typedef -----------------------------------------------------------*/
  236. /* Private define ------------------------------------------------------------*/
  237. /** @defgroup FMPI2C_Private_Define FMPI2C Private Define
  238. * @{
  239. */
  240. #define TIMING_CLEAR_MASK 0xF0FFFFFFU /*!< FMPI2C TIMING clear register Mask */
  241. #define FMPI2C_TIMEOUT_ADDR 10000U /*!< 10 s */
  242. #define FMPI2C_TIMEOUT_BUSY 25U /*!< 25 ms */
  243. #define FMPI2C_TIMEOUT_DIR 25U /*!< 25 ms */
  244. #define FMPI2C_TIMEOUT_RXNE 25U /*!< 25 ms */
  245. #define FMPI2C_TIMEOUT_STOPF 25U /*!< 25 ms */
  246. #define FMPI2C_TIMEOUT_TC 25U /*!< 25 ms */
  247. #define FMPI2C_TIMEOUT_TCR 25U /*!< 25 ms */
  248. #define FMPI2C_TIMEOUT_TXIS 25U /*!< 25 ms */
  249. #define FMPI2C_TIMEOUT_FLAG 25U /*!< 25 ms */
  250. #define MAX_NBYTE_SIZE 255U
  251. #define SlaveAddr_SHIFT 7U
  252. #define SlaveAddr_MSK 0x06U
  253. /* Private define for @ref PreviousState usage */
  254. #define FMPI2C_STATE_MSK ((uint32_t)((HAL_FMPI2C_STATE_BUSY_TX | HAL_FMPI2C_STATE_BUSY_RX) & (~((uint32_t)HAL_FMPI2C_STATE_READY)))) /*!< Mask State define, keep only RX and TX bits */
  255. #define FMPI2C_STATE_NONE ((uint32_t)(HAL_FMPI2C_MODE_NONE)) /*!< Default Value */
  256. #define FMPI2C_STATE_MASTER_BUSY_TX ((uint32_t)((HAL_FMPI2C_STATE_BUSY_TX & FMPI2C_STATE_MSK) | HAL_FMPI2C_MODE_MASTER)) /*!< Master Busy TX, combinaison of State LSB and Mode enum */
  257. #define FMPI2C_STATE_MASTER_BUSY_RX ((uint32_t)((HAL_FMPI2C_STATE_BUSY_RX & FMPI2C_STATE_MSK) | HAL_FMPI2C_MODE_MASTER)) /*!< Master Busy RX, combinaison of State LSB and Mode enum */
  258. #define FMPI2C_STATE_SLAVE_BUSY_TX ((uint32_t)((HAL_FMPI2C_STATE_BUSY_TX & FMPI2C_STATE_MSK) | HAL_FMPI2C_MODE_SLAVE)) /*!< Slave Busy TX, combinaison of State LSB and Mode enum */
  259. #define FMPI2C_STATE_SLAVE_BUSY_RX ((uint32_t)((HAL_FMPI2C_STATE_BUSY_RX & FMPI2C_STATE_MSK) | HAL_FMPI2C_MODE_SLAVE)) /*!< Slave Busy RX, combinaison of State LSB and Mode enum */
  260. #define FMPI2C_STATE_MEM_BUSY_TX ((uint32_t)((HAL_FMPI2C_STATE_BUSY_TX & FMPI2C_STATE_MSK) | HAL_FMPI2C_MODE_MEM)) /*!< Memory Busy TX, combinaison of State LSB and Mode enum */
  261. #define FMPI2C_STATE_MEM_BUSY_RX ((uint32_t)((HAL_FMPI2C_STATE_BUSY_RX & FMPI2C_STATE_MSK) | HAL_FMPI2C_MODE_MEM)) /*!< Memory Busy RX, combinaison of State LSB and Mode enum */
  262. /* Private define to centralize the enable/disable of Interrupts */
  263. #define FMPI2C_XFER_TX_IT 0x00000001U
  264. #define FMPI2C_XFER_RX_IT 0x00000002U
  265. #define FMPI2C_XFER_LISTEN_IT 0x00000004U
  266. #define FMPI2C_XFER_ERROR_IT 0x00000011U
  267. #define FMPI2C_XFER_CPLT_IT 0x00000012U
  268. #define FMPI2C_XFER_RELOAD_IT 0x00000012U
  269. /* Private define Sequential Transfer Options default/reset value */
  270. #define FMPI2C_NO_OPTION_FRAME 0xFFFF0000U
  271. /**
  272. * @}
  273. */
  274. /* Private macro -------------------------------------------------------------*/
  275. #define FMPI2C_GET_DMA_REMAIN_DATA(__HANDLE__) ((((__HANDLE__)->State) == HAL_FMPI2C_STATE_BUSY_TX) ? \
  276. ((uint32_t)((__HANDLE__)->hdmatx->Instance->NDTR)) : \
  277. ((uint32_t)((__HANDLE__)->hdmarx->Instance->NDTR)))
  278. /* Private variables ---------------------------------------------------------*/
  279. /* Private function prototypes -----------------------------------------------*/
  280. /** @defgroup FMPI2C_Private_Functions FMPI2C Private Functions
  281. * @{
  282. */
  283. /* Private functions to handle DMA transfer */
  284. static void FMPI2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma);
  285. static void FMPI2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma);
  286. static void FMPI2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma);
  287. static void FMPI2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma);
  288. static void FMPI2C_DMAError(DMA_HandleTypeDef *hdma);
  289. static void FMPI2C_DMAAbort(DMA_HandleTypeDef *hdma);
  290. /* Private functions to handle IT transfer */
  291. static void FMPI2C_ITAddrCplt (FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags);
  292. static void FMPI2C_ITMasterSequentialCplt (FMPI2C_HandleTypeDef *hfmpi2c);
  293. static void FMPI2C_ITSlaveSequentialCplt (FMPI2C_HandleTypeDef *hfmpi2c);
  294. static void FMPI2C_ITMasterCplt (FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags);
  295. static void FMPI2C_ITSlaveCplt (FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags);
  296. static void FMPI2C_ITListenCplt (FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags);
  297. static void FMPI2C_ITError (FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags);
  298. /* Private functions to handle IT transfer */
  299. static HAL_StatusTypeDef FMPI2C_RequestMemoryWrite (FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart);
  300. static HAL_StatusTypeDef FMPI2C_RequestMemoryRead (FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart);
  301. /* Private functions for FMPI2C transfer IRQ handler */
  302. static HAL_StatusTypeDef FMPI2C_Master_ISR_IT(struct __FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags, uint32_t ITSources);
  303. static HAL_StatusTypeDef FMPI2C_Slave_ISR_IT(struct __FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags, uint32_t ITSources);
  304. static HAL_StatusTypeDef FMPI2C_Master_ISR_DMA(struct __FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags, uint32_t ITSources);
  305. static HAL_StatusTypeDef FMPI2C_Slave_ISR_DMA(struct __FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags, uint32_t ITSources);
  306. /* Private functions to handle flags during polling transfer */
  307. static HAL_StatusTypeDef FMPI2C_WaitOnFlagUntilTimeout (FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t Tickstart);
  308. static HAL_StatusTypeDef FMPI2C_WaitOnTXISFlagUntilTimeout (FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout, uint32_t Tickstart);
  309. static HAL_StatusTypeDef FMPI2C_WaitOnRXNEFlagUntilTimeout (FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout, uint32_t Tickstart);
  310. static HAL_StatusTypeDef FMPI2C_WaitOnSTOPFlagUntilTimeout (FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout, uint32_t Tickstart);
  311. static HAL_StatusTypeDef FMPI2C_IsAcknowledgeFailed (FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout, uint32_t Tickstart);
  312. /* Private functions to centralize the enable/disable of Interrupts */
  313. static HAL_StatusTypeDef FMPI2C_Enable_IRQ (FMPI2C_HandleTypeDef *hfmpi2c, uint16_t InterruptRequest);
  314. static HAL_StatusTypeDef FMPI2C_Disable_IRQ (FMPI2C_HandleTypeDef *hfmpi2c, uint16_t InterruptRequest);
  315. /* Private functions to flush TXDR register */
  316. static void FMPI2C_Flush_TXDR (FMPI2C_HandleTypeDef *hfmpi2c);
  317. /* Private functions to handle start, restart or stop a transfer */
  318. static void FMPI2C_TransferConfig (FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t Size, uint32_t Mode, uint32_t Request);
  319. /**
  320. * @}
  321. */
  322. /* Exported functions --------------------------------------------------------*/
  323. /** @defgroup FMPI2C_Exported_Functions FMPI2C Exported Functions
  324. * @{
  325. */
  326. /** @defgroup FMPI2C_Exported_Functions_Group1 Initialization and de-initialization functions
  327. * @brief Initialization and Configuration functions
  328. *
  329. @verbatim
  330. ===============================================================================
  331. ##### Initialization and de-initialization functions #####
  332. ===============================================================================
  333. [..] This subsection provides a set of functions allowing to initialize and
  334. deinitialize the FMPI2Cx peripheral:
  335. (+) User must Implement HAL_FMPI2C_MspInit() function in which he configures
  336. all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
  337. (+) Call the function HAL_FMPI2C_Init() to configure the selected device with
  338. the selected configuration:
  339. (++) Clock Timing
  340. (++) Own Address 1
  341. (++) Addressing mode (Master, Slave)
  342. (++) Dual Addressing mode
  343. (++) Own Address 2
  344. (++) Own Address 2 Mask
  345. (++) General call mode
  346. (++) Nostretch mode
  347. (+) Call the function HAL_FMPI2C_DeInit() to restore the default configuration
  348. of the selected FMPI2Cx peripheral.
  349. @endverbatim
  350. * @{
  351. */
  352. /**
  353. * @brief Initializes the FMPI2C according to the specified parameters
  354. * in the FMPI2C_InitTypeDef and initialize the associated handle.
  355. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  356. * the configuration information for the specified FMPI2C.
  357. * @retval HAL status
  358. */
  359. HAL_StatusTypeDef HAL_FMPI2C_Init(FMPI2C_HandleTypeDef *hfmpi2c)
  360. {
  361. /* Check the FMPI2C handle allocation */
  362. if(hfmpi2c == NULL)
  363. {
  364. return HAL_ERROR;
  365. }
  366. /* Check the parameters */
  367. assert_param(IS_FMPI2C_ALL_INSTANCE(hfmpi2c->Instance));
  368. assert_param(IS_FMPI2C_OWN_ADDRESS1(hfmpi2c->Init.OwnAddress1));
  369. assert_param(IS_FMPI2C_ADDRESSING_MODE(hfmpi2c->Init.AddressingMode));
  370. assert_param(IS_FMPI2C_DUAL_ADDRESS(hfmpi2c->Init.DualAddressMode));
  371. assert_param(IS_FMPI2C_OWN_ADDRESS2(hfmpi2c->Init.OwnAddress2));
  372. assert_param(IS_FMPI2C_OWN_ADDRESS2_MASK(hfmpi2c->Init.OwnAddress2Masks));
  373. assert_param(IS_FMPI2C_GENERAL_CALL(hfmpi2c->Init.GeneralCallMode));
  374. assert_param(IS_FMPI2C_NO_STRETCH(hfmpi2c->Init.NoStretchMode));
  375. if(hfmpi2c->State == HAL_FMPI2C_STATE_RESET)
  376. {
  377. /* Allocate lock resource and initialize it */
  378. hfmpi2c->Lock = HAL_UNLOCKED;
  379. /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  380. HAL_FMPI2C_MspInit(hfmpi2c);
  381. }
  382. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY;
  383. /* Disable the selected FMPI2C peripheral */
  384. __HAL_FMPI2C_DISABLE(hfmpi2c);
  385. /*---------------------------- FMPI2Cx TIMINGR Configuration ------------------*/
  386. /* Configure FMPI2Cx: Frequency range */
  387. hfmpi2c->Instance->TIMINGR = hfmpi2c->Init.Timing & TIMING_CLEAR_MASK;
  388. /*---------------------------- FMPI2Cx OAR1 Configuration ---------------------*/
  389. /* Disable Own Address1 before set the Own Address1 configuration */
  390. hfmpi2c->Instance->OAR1 &= ~FMPI2C_OAR1_OA1EN;
  391. /* Configure FMPI2Cx: Own Address1 and ack own address1 mode */
  392. if(hfmpi2c->Init.OwnAddress1 != 0U)
  393. {
  394. if(hfmpi2c->Init.AddressingMode == FMPI2C_ADDRESSINGMODE_7BIT)
  395. {
  396. hfmpi2c->Instance->OAR1 = (FMPI2C_OAR1_OA1EN | hfmpi2c->Init.OwnAddress1);
  397. }
  398. else /* FMPI2C_ADDRESSINGMODE_10BIT */
  399. {
  400. hfmpi2c->Instance->OAR1 = (FMPI2C_OAR1_OA1EN | FMPI2C_OAR1_OA1MODE | hfmpi2c->Init.OwnAddress1);
  401. }
  402. }
  403. /*---------------------------- FMPI2Cx CR2 Configuration ----------------------*/
  404. /* Configure FMPI2Cx: Addressing Master mode */
  405. if(hfmpi2c->Init.AddressingMode == FMPI2C_ADDRESSINGMODE_10BIT)
  406. {
  407. hfmpi2c->Instance->CR2 = (FMPI2C_CR2_ADD10);
  408. }
  409. /* Enable the AUTOEND by default, and enable NACK (should be disable only during Slave process */
  410. hfmpi2c->Instance->CR2 |= (FMPI2C_CR2_AUTOEND | FMPI2C_CR2_NACK);
  411. /*---------------------------- FMPI2Cx OAR2 Configuration ---------------------*/
  412. /* Disable Own Address2 before set the Own Address2 configuration */
  413. hfmpi2c->Instance->OAR2 &= ~FMPI2C_DUALADDRESS_ENABLE;
  414. /* Configure FMPI2Cx: Dual mode and Own Address2 */
  415. hfmpi2c->Instance->OAR2 = (hfmpi2c->Init.DualAddressMode | hfmpi2c->Init.OwnAddress2 | (hfmpi2c->Init.OwnAddress2Masks << 8));
  416. /*---------------------------- FMPI2Cx CR1 Configuration ----------------------*/
  417. /* Configure FMPI2Cx: Generalcall and NoStretch mode */
  418. hfmpi2c->Instance->CR1 = (hfmpi2c->Init.GeneralCallMode | hfmpi2c->Init.NoStretchMode);
  419. /* Enable the selected FMPI2C peripheral */
  420. __HAL_FMPI2C_ENABLE(hfmpi2c);
  421. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  422. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  423. hfmpi2c->PreviousState = FMPI2C_STATE_NONE;
  424. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  425. return HAL_OK;
  426. }
  427. /**
  428. * @brief DeInitialize the FMPI2C peripheral.
  429. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  430. * the configuration information for the specified FMPI2C.
  431. * @retval HAL status
  432. */
  433. HAL_StatusTypeDef HAL_FMPI2C_DeInit(FMPI2C_HandleTypeDef *hfmpi2c)
  434. {
  435. /* Check the FMPI2C handle allocation */
  436. if(hfmpi2c == NULL)
  437. {
  438. return HAL_ERROR;
  439. }
  440. /* Check the parameters */
  441. assert_param(IS_FMPI2C_ALL_INSTANCE(hfmpi2c->Instance));
  442. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY;
  443. /* Disable the FMPI2C Peripheral Clock */
  444. __HAL_FMPI2C_DISABLE(hfmpi2c);
  445. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  446. HAL_FMPI2C_MspDeInit(hfmpi2c);
  447. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  448. hfmpi2c->State = HAL_FMPI2C_STATE_RESET;
  449. hfmpi2c->PreviousState = FMPI2C_STATE_NONE;
  450. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  451. /* Release Lock */
  452. __HAL_UNLOCK(hfmpi2c);
  453. return HAL_OK;
  454. }
  455. /**
  456. * @brief Initialize the FMPI2C MSP.
  457. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  458. * the configuration information for the specified FMPI2C.
  459. * @retval None
  460. */
  461. __weak void HAL_FMPI2C_MspInit(FMPI2C_HandleTypeDef *hfmpi2c)
  462. {
  463. /* Prevent unused argument(s) compilation warning */
  464. UNUSED(hfmpi2c);
  465. /* NOTE : This function should not be modified, when the callback is needed,
  466. the HAL_FMPI2C_MspInit could be implemented in the user file
  467. */
  468. }
  469. /**
  470. * @brief DeInitialize the FMPI2C MSP.
  471. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  472. * the configuration information for the specified FMPI2C.
  473. * @retval None
  474. */
  475. __weak void HAL_FMPI2C_MspDeInit(FMPI2C_HandleTypeDef *hfmpi2c)
  476. {
  477. /* Prevent unused argument(s) compilation warning */
  478. UNUSED(hfmpi2c);
  479. /* NOTE : This function should not be modified, when the callback is needed,
  480. the HAL_FMPI2C_MspDeInit could be implemented in the user file
  481. */
  482. }
  483. /**
  484. * @}
  485. */
  486. /** @defgroup FMPI2C_Exported_Functions_Group2 Input and Output operation functions
  487. * @brief Data transfers functions
  488. *
  489. @verbatim
  490. ===============================================================================
  491. ##### IO operation functions #####
  492. ===============================================================================
  493. [..]
  494. This subsection provides a set of functions allowing to manage the FMPI2C data
  495. transfers.
  496. (#) There are two modes of transfer:
  497. (++) Blocking mode : The communication is performed in the polling mode.
  498. The status of all data processing is returned by the same function
  499. after finishing transfer.
  500. (++) No-Blocking mode : The communication is performed using Interrupts
  501. or DMA. These functions return the status of the transfer startup.
  502. The end of the data processing will be indicated through the
  503. dedicated FMPI2C IRQ when using Interrupt mode or the DMA IRQ when
  504. using DMA mode.
  505. (#) Blocking mode functions are :
  506. (++) HAL_FMPI2C_Master_Transmit()
  507. (++) HAL_FMPI2C_Master_Receive()
  508. (++) HAL_FMPI2C_Slave_Transmit()
  509. (++) HAL_FMPI2C_Slave_Receive()
  510. (++) HAL_FMPI2C_Mem_Write()
  511. (++) HAL_FMPI2C_Mem_Read()
  512. (++) HAL_FMPI2C_IsDeviceReady()
  513. (#) No-Blocking mode functions with Interrupt are :
  514. (++) HAL_FMPI2C_Master_Transmit_IT()
  515. (++) HAL_FMPI2C_Master_Receive_IT()
  516. (++) HAL_FMPI2C_Slave_Transmit_IT()
  517. (++) HAL_FMPI2C_Slave_Receive_IT()
  518. (++) HAL_FMPI2C_Master_Sequential_Transmit_IT()
  519. (++) HAL_FMPI2C_Master_Sequential_Receive_IT()
  520. (++) HAL_FMPI2C_Slave_Sequential_Transmit_IT()
  521. (++) HAL_FMPI2C_Slave_Sequential_Receive_IT()
  522. (++) HAL_FMPI2C_Mem_Write_IT()
  523. (++) HAL_FMPI2C_Mem_Read_IT()
  524. (#) No-Blocking mode functions with DMA are :
  525. (++) HAL_FMPI2C_Master_Transmit_DMA()
  526. (++) HAL_FMPI2C_Master_Receive_DMA()
  527. (++) HAL_FMPI2C_Slave_Transmit_DMA()
  528. (++) HAL_FMPI2C_Slave_Receive_DMA()
  529. (++) HAL_FMPI2C_Mem_Write_DMA()
  530. (++) HAL_FMPI2C_Mem_Read_DMA()
  531. (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  532. (++) HAL_FMPI2C_MemTxCpltCallback()
  533. (++) HAL_FMPI2C_MemRxCpltCallback()
  534. (++) HAL_FMPI2C_MasterTxCpltCallback()
  535. (++) HAL_FMPI2C_MasterRxCpltCallback()
  536. (++) HAL_FMPI2C_SlaveTxCpltCallback()
  537. (++) HAL_FMPI2C_SlaveRxCpltCallback()
  538. (++) HAL_FMPI2C_ErrorCallback()
  539. (++) HAL_FMPI2C_AbortCpltCallback()
  540. @endverbatim
  541. * @{
  542. */
  543. /**
  544. * @brief Transmits in master mode an amount of data in blocking mode.
  545. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  546. * the configuration information for the specified FMPI2C.
  547. * @param DevAddress Target device address: The device 7 bits address value
  548. * in datasheet must be shift at right before call interface
  549. * @param pData Pointer to data buffer
  550. * @param Size Amount of data to be sent
  551. * @param Timeout Timeout duration
  552. * @retval HAL status
  553. */
  554. HAL_StatusTypeDef HAL_FMPI2C_Master_Transmit(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  555. {
  556. uint32_t tickstart = 0U;
  557. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  558. {
  559. /* Process Locked */
  560. __HAL_LOCK(hfmpi2c);
  561. /* Init tickstart for timeout management*/
  562. tickstart = HAL_GetTick();
  563. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_BUSY, SET, FMPI2C_TIMEOUT_BUSY, tickstart) != HAL_OK)
  564. {
  565. return HAL_TIMEOUT;
  566. }
  567. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  568. hfmpi2c->Mode = HAL_FMPI2C_MODE_MASTER;
  569. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  570. /* Prepare transfer parameters */
  571. hfmpi2c->pBuffPtr = pData;
  572. hfmpi2c->XferCount = Size;
  573. hfmpi2c->XferISR = NULL;
  574. /* Send Slave Address */
  575. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  576. if(hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  577. {
  578. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  579. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_GENERATE_START_WRITE);
  580. }
  581. else
  582. {
  583. hfmpi2c->XferSize = hfmpi2c->XferCount;
  584. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_GENERATE_START_WRITE);
  585. }
  586. while(hfmpi2c->XferSize > 0U)
  587. {
  588. /* Wait until TXIS flag is set */
  589. if(FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  590. {
  591. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  592. {
  593. return HAL_ERROR;
  594. }
  595. else
  596. {
  597. return HAL_TIMEOUT;
  598. }
  599. }
  600. /* Write data to TXDR */
  601. hfmpi2c->Instance->TXDR = (*hfmpi2c->pBuffPtr++);
  602. hfmpi2c->XferCount--;
  603. hfmpi2c->XferSize--;
  604. if((hfmpi2c->XferSize == 0U) && (hfmpi2c->XferCount!=0U))
  605. {
  606. /* Wait until TCR flag is set */
  607. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK)
  608. {
  609. return HAL_TIMEOUT;
  610. }
  611. if(hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  612. {
  613. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  614. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  615. }
  616. else
  617. {
  618. hfmpi2c->XferSize = hfmpi2c->XferCount;
  619. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  620. }
  621. }
  622. }
  623. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  624. /* Wait until STOPF flag is set */
  625. if(FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  626. {
  627. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  628. {
  629. return HAL_ERROR;
  630. }
  631. else
  632. {
  633. return HAL_TIMEOUT;
  634. }
  635. }
  636. /* Clear STOP Flag */
  637. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  638. /* Clear Configuration Register 2 */
  639. FMPI2C_RESET_CR2(hfmpi2c);
  640. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  641. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  642. /* Process Unlocked */
  643. __HAL_UNLOCK(hfmpi2c);
  644. return HAL_OK;
  645. }
  646. else
  647. {
  648. return HAL_BUSY;
  649. }
  650. }
  651. /**
  652. * @brief Receives in master mode an amount of data in blocking mode.
  653. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  654. * the configuration information for the specified FMPI2C.
  655. * @param DevAddress Target device address: The device 7 bits address value
  656. * in datasheet must be shift at right before call interface
  657. * @param pData Pointer to data buffer
  658. * @param Size Amount of data to be sent
  659. * @param Timeout Timeout duration
  660. * @retval HAL status
  661. */
  662. HAL_StatusTypeDef HAL_FMPI2C_Master_Receive(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  663. {
  664. uint32_t tickstart = 0U;
  665. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  666. {
  667. /* Process Locked */
  668. __HAL_LOCK(hfmpi2c);
  669. /* Init tickstart for timeout management*/
  670. tickstart = HAL_GetTick();
  671. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_BUSY, SET, FMPI2C_TIMEOUT_BUSY, tickstart) != HAL_OK)
  672. {
  673. return HAL_TIMEOUT;
  674. }
  675. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  676. hfmpi2c->Mode = HAL_FMPI2C_MODE_MASTER;
  677. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  678. /* Prepare transfer parameters */
  679. hfmpi2c->pBuffPtr = pData;
  680. hfmpi2c->XferCount = Size;
  681. hfmpi2c->XferISR = NULL;
  682. /* Send Slave Address */
  683. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  684. if(hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  685. {
  686. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  687. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_GENERATE_START_READ);
  688. }
  689. else
  690. {
  691. hfmpi2c->XferSize = hfmpi2c->XferCount;
  692. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_GENERATE_START_READ);
  693. }
  694. while(hfmpi2c->XferSize > 0U)
  695. {
  696. /* Wait until RXNE flag is set */
  697. if(FMPI2C_WaitOnRXNEFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  698. {
  699. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  700. {
  701. return HAL_ERROR;
  702. }
  703. else
  704. {
  705. return HAL_TIMEOUT;
  706. }
  707. }
  708. /* Read data from RXDR */
  709. (*hfmpi2c->pBuffPtr++) = hfmpi2c->Instance->RXDR;
  710. hfmpi2c->XferSize--;
  711. hfmpi2c->XferCount--;
  712. if((hfmpi2c->XferSize == 0U) && (hfmpi2c->XferCount != 0U))
  713. {
  714. /* Wait until TCR flag is set */
  715. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK)
  716. {
  717. return HAL_TIMEOUT;
  718. }
  719. if(hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  720. {
  721. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  722. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  723. }
  724. else
  725. {
  726. hfmpi2c->XferSize = hfmpi2c->XferCount;
  727. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  728. }
  729. }
  730. }
  731. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  732. /* Wait until STOPF flag is set */
  733. if(FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  734. {
  735. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  736. {
  737. return HAL_ERROR;
  738. }
  739. else
  740. {
  741. return HAL_TIMEOUT;
  742. }
  743. }
  744. /* Clear STOP Flag */
  745. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  746. /* Clear Configuration Register 2 */
  747. FMPI2C_RESET_CR2(hfmpi2c);
  748. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  749. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  750. /* Process Unlocked */
  751. __HAL_UNLOCK(hfmpi2c);
  752. return HAL_OK;
  753. }
  754. else
  755. {
  756. return HAL_BUSY;
  757. }
  758. }
  759. /**
  760. * @brief Transmits in slave mode an amount of data in blocking mode.
  761. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  762. * the configuration information for the specified FMPI2C.
  763. * @param pData Pointer to data buffer
  764. * @param Size Amount of data to be sent
  765. * @param Timeout Timeout duration
  766. * @retval HAL status
  767. */
  768. HAL_StatusTypeDef HAL_FMPI2C_Slave_Transmit(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  769. {
  770. uint32_t tickstart = 0U;
  771. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  772. {
  773. if((pData == NULL ) || (Size == 0U))
  774. {
  775. return HAL_ERROR;
  776. }
  777. /* Process Locked */
  778. __HAL_LOCK(hfmpi2c);
  779. /* Init tickstart for timeout management*/
  780. tickstart = HAL_GetTick();
  781. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  782. hfmpi2c->Mode = HAL_FMPI2C_MODE_SLAVE;
  783. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  784. /* Prepare transfer parameters */
  785. hfmpi2c->pBuffPtr = pData;
  786. hfmpi2c->XferCount = Size;
  787. hfmpi2c->XferISR = NULL;
  788. /* Enable Address Acknowledge */
  789. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  790. /* Wait until ADDR flag is set */
  791. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
  792. {
  793. /* Disable Address Acknowledge */
  794. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  795. return HAL_TIMEOUT;
  796. }
  797. /* Clear ADDR flag */
  798. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c,FMPI2C_FLAG_ADDR);
  799. /* If 10bit addressing mode is selected */
  800. if(hfmpi2c->Init.AddressingMode == FMPI2C_ADDRESSINGMODE_10BIT)
  801. {
  802. /* Wait until ADDR flag is set */
  803. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
  804. {
  805. /* Disable Address Acknowledge */
  806. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  807. return HAL_TIMEOUT;
  808. }
  809. /* Clear ADDR flag */
  810. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c,FMPI2C_FLAG_ADDR);
  811. }
  812. /* Wait until DIR flag is set Transmitter mode */
  813. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_DIR, RESET, Timeout, tickstart) != HAL_OK)
  814. {
  815. /* Disable Address Acknowledge */
  816. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  817. return HAL_TIMEOUT;
  818. }
  819. while(hfmpi2c->XferCount > 0U)
  820. {
  821. /* Wait until TXIS flag is set */
  822. if(FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  823. {
  824. /* Disable Address Acknowledge */
  825. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  826. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  827. {
  828. return HAL_ERROR;
  829. }
  830. else
  831. {
  832. return HAL_TIMEOUT;
  833. }
  834. }
  835. /* Write data to TXDR */
  836. hfmpi2c->Instance->TXDR = (*hfmpi2c->pBuffPtr++);
  837. hfmpi2c->XferCount--;
  838. }
  839. /* Wait until STOP flag is set */
  840. if(FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  841. {
  842. /* Disable Address Acknowledge */
  843. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  844. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  845. {
  846. /* Normal use case for Transmitter mode */
  847. /* A NACK is generated to confirm the end of transfer */
  848. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  849. }
  850. else
  851. {
  852. return HAL_TIMEOUT;
  853. }
  854. }
  855. /* Clear STOP flag */
  856. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c,FMPI2C_FLAG_STOPF);
  857. /* Wait until BUSY flag is reset */
  858. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_BUSY, SET, Timeout, tickstart) != HAL_OK)
  859. {
  860. /* Disable Address Acknowledge */
  861. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  862. return HAL_TIMEOUT;
  863. }
  864. /* Disable Address Acknowledge */
  865. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  866. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  867. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  868. /* Process Unlocked */
  869. __HAL_UNLOCK(hfmpi2c);
  870. return HAL_OK;
  871. }
  872. else
  873. {
  874. return HAL_BUSY;
  875. }
  876. }
  877. /**
  878. * @brief Receive in slave mode an amount of data in blocking mode
  879. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  880. * the configuration information for the specified FMPI2C.
  881. * @param pData Pointer to data buffer
  882. * @param Size Amount of data to be sent
  883. * @param Timeout Timeout duration
  884. * @retval HAL status
  885. */
  886. HAL_StatusTypeDef HAL_FMPI2C_Slave_Receive(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  887. {
  888. uint32_t tickstart = 0U;
  889. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  890. {
  891. if((pData == NULL ) || (Size == 0U))
  892. {
  893. return HAL_ERROR;
  894. }
  895. /* Process Locked */
  896. __HAL_LOCK(hfmpi2c);
  897. /* Init tickstart for timeout management*/
  898. tickstart = HAL_GetTick();
  899. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  900. hfmpi2c->Mode = HAL_FMPI2C_MODE_SLAVE;
  901. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  902. /* Prepare transfer parameters */
  903. hfmpi2c->pBuffPtr = pData;
  904. hfmpi2c->XferCount = Size;
  905. hfmpi2c->XferISR = NULL;
  906. /* Enable Address Acknowledge */
  907. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  908. /* Wait until ADDR flag is set */
  909. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
  910. {
  911. /* Disable Address Acknowledge */
  912. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  913. return HAL_TIMEOUT;
  914. }
  915. /* Clear ADDR flag */
  916. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c,FMPI2C_FLAG_ADDR);
  917. /* Wait until DIR flag is reset Receiver mode */
  918. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_DIR, SET, Timeout, tickstart) != HAL_OK)
  919. {
  920. /* Disable Address Acknowledge */
  921. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  922. return HAL_TIMEOUT;
  923. }
  924. while(hfmpi2c->XferCount > 0U)
  925. {
  926. /* Wait until RXNE flag is set */
  927. if(FMPI2C_WaitOnRXNEFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  928. {
  929. /* Disable Address Acknowledge */
  930. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  931. /* Store Last receive data if any */
  932. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_RXNE) == SET)
  933. {
  934. /* Read data from RXDR */
  935. (*hfmpi2c->pBuffPtr++) = hfmpi2c->Instance->RXDR;
  936. hfmpi2c->XferCount--;
  937. }
  938. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_TIMEOUT)
  939. {
  940. return HAL_TIMEOUT;
  941. }
  942. else
  943. {
  944. return HAL_ERROR;
  945. }
  946. }
  947. /* Read data from RXDR */
  948. (*hfmpi2c->pBuffPtr++) = hfmpi2c->Instance->RXDR;
  949. hfmpi2c->XferCount--;
  950. }
  951. /* Wait until STOP flag is set */
  952. if(FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  953. {
  954. /* Disable Address Acknowledge */
  955. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  956. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  957. {
  958. return HAL_ERROR;
  959. }
  960. else
  961. {
  962. return HAL_TIMEOUT;
  963. }
  964. }
  965. /* Clear STOP flag */
  966. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c,FMPI2C_FLAG_STOPF);
  967. /* Wait until BUSY flag is reset */
  968. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_BUSY, SET, Timeout, tickstart) != HAL_OK)
  969. {
  970. /* Disable Address Acknowledge */
  971. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  972. return HAL_TIMEOUT;
  973. }
  974. /* Disable Address Acknowledge */
  975. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  976. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  977. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  978. /* Process Unlocked */
  979. __HAL_UNLOCK(hfmpi2c);
  980. return HAL_OK;
  981. }
  982. else
  983. {
  984. return HAL_BUSY;
  985. }
  986. }
  987. /**
  988. * @brief Transmit in master mode an amount of data in non-blocking mode with Interrupt
  989. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  990. * the configuration information for the specified FMPI2C.
  991. * @param DevAddress Target device address: The device 7 bits address value
  992. * in datasheet must be shift at right before call interface
  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_FMPI2C_Master_Transmit_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  998. {
  999. uint32_t xfermode = 0U;
  1000. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1001. {
  1002. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  1003. {
  1004. return HAL_BUSY;
  1005. }
  1006. /* Process Locked */
  1007. __HAL_LOCK(hfmpi2c);
  1008. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  1009. hfmpi2c->Mode = HAL_FMPI2C_MODE_MASTER;
  1010. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1011. /* Prepare transfer parameters */
  1012. hfmpi2c->pBuffPtr = pData;
  1013. hfmpi2c->XferCount = Size;
  1014. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1015. hfmpi2c->XferISR = FMPI2C_Master_ISR_IT;
  1016. if(hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1017. {
  1018. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1019. xfermode = FMPI2C_RELOAD_MODE;
  1020. }
  1021. else
  1022. {
  1023. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1024. xfermode = FMPI2C_AUTOEND_MODE;
  1025. }
  1026. /* Send Slave Address */
  1027. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE */
  1028. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, xfermode, FMPI2C_GENERATE_START_WRITE);
  1029. /* Process Unlocked */
  1030. __HAL_UNLOCK(hfmpi2c);
  1031. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1032. to avoid the risk of FMPI2C interrupt handle execution before current
  1033. process unlock */
  1034. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  1035. /* possible to enable all of these */
  1036. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  1037. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT);
  1038. return HAL_OK;
  1039. }
  1040. else
  1041. {
  1042. return HAL_BUSY;
  1043. }
  1044. }
  1045. /**
  1046. * @brief Receive in master mode an amount of data in non-blocking mode with Interrupt
  1047. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1048. * the configuration information for the specified FMPI2C.
  1049. * @param DevAddress Target device address: The device 7 bits address value
  1050. * in datasheet must be shift at right before call interface
  1051. * @param pData Pointer to data buffer
  1052. * @param Size Amount of data to be sent
  1053. * @retval HAL status
  1054. */
  1055. HAL_StatusTypeDef HAL_FMPI2C_Master_Receive_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  1056. {
  1057. uint32_t xfermode = 0U;
  1058. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1059. {
  1060. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  1061. {
  1062. return HAL_BUSY;
  1063. }
  1064. /* Process Locked */
  1065. __HAL_LOCK(hfmpi2c);
  1066. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  1067. hfmpi2c->Mode = HAL_FMPI2C_MODE_MASTER;
  1068. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1069. /* Prepare transfer parameters */
  1070. hfmpi2c->pBuffPtr = pData;
  1071. hfmpi2c->XferCount = Size;
  1072. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1073. hfmpi2c->XferISR = FMPI2C_Master_ISR_IT;
  1074. if(hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1075. {
  1076. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1077. xfermode = FMPI2C_RELOAD_MODE;
  1078. }
  1079. else
  1080. {
  1081. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1082. xfermode = FMPI2C_AUTOEND_MODE;
  1083. }
  1084. /* Send Slave Address */
  1085. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE */
  1086. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, xfermode, FMPI2C_GENERATE_START_READ);
  1087. /* Process Unlocked */
  1088. __HAL_UNLOCK(hfmpi2c);
  1089. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1090. to avoid the risk of FMPI2C interrupt handle execution before current
  1091. process unlock */
  1092. /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  1093. /* possible to enable all of these */
  1094. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  1095. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT);
  1096. return HAL_OK;
  1097. }
  1098. else
  1099. {
  1100. return HAL_BUSY;
  1101. }
  1102. }
  1103. /**
  1104. * @brief Transmit in slave mode an amount of data in non-blocking mode with Interrupt
  1105. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1106. * the configuration information for the specified FMPI2C.
  1107. * @param pData Pointer to data buffer
  1108. * @param Size Amount of data to be sent
  1109. * @retval HAL status
  1110. */
  1111. HAL_StatusTypeDef HAL_FMPI2C_Slave_Transmit_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size)
  1112. {
  1113. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1114. {
  1115. /* Process Locked */
  1116. __HAL_LOCK(hfmpi2c);
  1117. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  1118. hfmpi2c->Mode = HAL_FMPI2C_MODE_SLAVE;
  1119. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1120. /* Enable Address Acknowledge */
  1121. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  1122. /* Prepare transfer parameters */
  1123. hfmpi2c->pBuffPtr = pData;
  1124. hfmpi2c->XferCount = Size;
  1125. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1126. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1127. hfmpi2c->XferISR = FMPI2C_Slave_ISR_IT;
  1128. /* Process Unlocked */
  1129. __HAL_UNLOCK(hfmpi2c);
  1130. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1131. to avoid the risk of FMPI2C interrupt handle execution before current
  1132. process unlock */
  1133. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  1134. /* possible to enable all of these */
  1135. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  1136. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT | FMPI2C_XFER_LISTEN_IT);
  1137. return HAL_OK;
  1138. }
  1139. else
  1140. {
  1141. return HAL_BUSY;
  1142. }
  1143. }
  1144. /**
  1145. * @brief Receive in slave mode an amount of data in non-blocking mode with Interrupt
  1146. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1147. * the configuration information for the specified FMPI2C.
  1148. * @param pData Pointer to data buffer
  1149. * @param Size Amount of data to be sent
  1150. * @retval HAL status
  1151. */
  1152. HAL_StatusTypeDef HAL_FMPI2C_Slave_Receive_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size)
  1153. {
  1154. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1155. {
  1156. /* Process Locked */
  1157. __HAL_LOCK(hfmpi2c);
  1158. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  1159. hfmpi2c->Mode = HAL_FMPI2C_MODE_SLAVE;
  1160. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1161. /* Enable Address Acknowledge */
  1162. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  1163. /* Prepare transfer parameters */
  1164. hfmpi2c->pBuffPtr = pData;
  1165. hfmpi2c->XferCount = Size;
  1166. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1167. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1168. hfmpi2c->XferISR = FMPI2C_Slave_ISR_IT;
  1169. /* Process Unlocked */
  1170. __HAL_UNLOCK(hfmpi2c);
  1171. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1172. to avoid the risk of FMPI2C interrupt handle execution before current
  1173. process unlock */
  1174. /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  1175. /* possible to enable all of these */
  1176. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  1177. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT | FMPI2C_XFER_LISTEN_IT);
  1178. return HAL_OK;
  1179. }
  1180. else
  1181. {
  1182. return HAL_BUSY;
  1183. }
  1184. }
  1185. /**
  1186. * @brief Transmit in master mode an amount of data in non-blocking mode with DMA
  1187. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1188. * the configuration information for the specified FMPI2C.
  1189. * @param DevAddress Target device address: The device 7 bits address value
  1190. * in datasheet must be shift at right before call interface
  1191. * @param pData Pointer to data buffer
  1192. * @param Size Amount of data to be sent
  1193. * @retval HAL status
  1194. */
  1195. HAL_StatusTypeDef HAL_FMPI2C_Master_Transmit_DMA(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  1196. {
  1197. uint32_t xfermode = 0U;
  1198. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1199. {
  1200. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  1201. {
  1202. return HAL_BUSY;
  1203. }
  1204. /* Process Locked */
  1205. __HAL_LOCK(hfmpi2c);
  1206. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  1207. hfmpi2c->Mode = HAL_FMPI2C_MODE_MASTER;
  1208. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1209. /* Prepare transfer parameters */
  1210. hfmpi2c->pBuffPtr = pData;
  1211. hfmpi2c->XferCount = Size;
  1212. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1213. hfmpi2c->XferISR = FMPI2C_Master_ISR_DMA;
  1214. if(hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1215. {
  1216. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1217. xfermode = FMPI2C_RELOAD_MODE;
  1218. }
  1219. else
  1220. {
  1221. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1222. xfermode = FMPI2C_AUTOEND_MODE;
  1223. }
  1224. /* Set the FMPI2C DMA transfer complete callback */
  1225. hfmpi2c->hdmatx->XferCpltCallback = FMPI2C_DMAMasterTransmitCplt;
  1226. /* Set the DMA error callback */
  1227. hfmpi2c->hdmatx->XferErrorCallback = FMPI2C_DMAError;
  1228. /* Set the unused DMA callbacks to NULL */
  1229. hfmpi2c->hdmatx->XferHalfCpltCallback = NULL;
  1230. hfmpi2c->hdmatx->XferAbortCallback = NULL;
  1231. /* Enable the DMA channel */
  1232. HAL_DMA_Start_IT(hfmpi2c->hdmatx, (uint32_t)pData, (uint32_t)&hfmpi2c->Instance->TXDR, hfmpi2c->XferSize);
  1233. /* Send Slave Address */
  1234. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  1235. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, xfermode, FMPI2C_GENERATE_START_WRITE);
  1236. /* Update XferCount value */
  1237. hfmpi2c->XferCount -= hfmpi2c->XferSize;
  1238. /* Process Unlocked */
  1239. __HAL_UNLOCK(hfmpi2c);
  1240. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1241. to avoid the risk of FMPI2C interrupt handle execution before current
  1242. process unlock */
  1243. /* Enable ERR and NACK interrupts */
  1244. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_ERROR_IT);
  1245. /* Enable DMA Request */
  1246. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_TXDMAEN;
  1247. return HAL_OK;
  1248. }
  1249. else
  1250. {
  1251. return HAL_BUSY;
  1252. }
  1253. }
  1254. /**
  1255. * @brief Receive in master mode an amount of data in non-blocking mode with DMA
  1256. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1257. * the configuration information for the specified FMPI2C.
  1258. * @param DevAddress Target device address: The device 7 bits address value
  1259. * in datasheet must be shift at right before call interface
  1260. * @param pData Pointer to data buffer
  1261. * @param Size Amount of data to be sent
  1262. * @retval HAL status
  1263. */
  1264. HAL_StatusTypeDef HAL_FMPI2C_Master_Receive_DMA(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  1265. {
  1266. uint32_t xfermode = 0U;
  1267. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1268. {
  1269. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  1270. {
  1271. return HAL_BUSY;
  1272. }
  1273. /* Process Locked */
  1274. __HAL_LOCK(hfmpi2c);
  1275. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  1276. hfmpi2c->Mode = HAL_FMPI2C_MODE_MASTER;
  1277. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1278. /* Prepare transfer parameters */
  1279. hfmpi2c->pBuffPtr = pData;
  1280. hfmpi2c->XferCount = Size;
  1281. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1282. hfmpi2c->XferISR = FMPI2C_Master_ISR_DMA;
  1283. if(hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1284. {
  1285. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1286. xfermode = FMPI2C_RELOAD_MODE;
  1287. }
  1288. else
  1289. {
  1290. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1291. xfermode = FMPI2C_AUTOEND_MODE;
  1292. }
  1293. if(hfmpi2c->XferSize > 0U)
  1294. {
  1295. /* Set the FMPI2C DMA transfer complete callback */
  1296. hfmpi2c->hdmarx->XferCpltCallback = FMPI2C_DMAMasterReceiveCplt;
  1297. /* Set the DMA error callback */
  1298. hfmpi2c->hdmarx->XferErrorCallback = FMPI2C_DMAError;
  1299. /* Set the unused DMA callbacks to NULL */
  1300. hfmpi2c->hdmarx->XferHalfCpltCallback = NULL;
  1301. hfmpi2c->hdmarx->XferAbortCallback = NULL;
  1302. /* Enable the DMA channel */
  1303. HAL_DMA_Start_IT(hfmpi2c->hdmarx, (uint32_t)&hfmpi2c->Instance->RXDR, (uint32_t)pData, hfmpi2c->XferSize);
  1304. /* Send Slave Address */
  1305. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  1306. FMPI2C_TransferConfig(hfmpi2c,DevAddress,hfmpi2c->XferSize, xfermode, FMPI2C_GENERATE_START_READ);
  1307. /* Update XferCount value */
  1308. hfmpi2c->XferCount -= hfmpi2c->XferSize;
  1309. /* Process Unlocked */
  1310. __HAL_UNLOCK(hfmpi2c);
  1311. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1312. to avoid the risk of FMPI2C interrupt handle execution before current
  1313. process unlock */
  1314. /* Enable ERR and NACK interrupts */
  1315. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_ERROR_IT);
  1316. /* Enable DMA Request */
  1317. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_RXDMAEN;
  1318. }
  1319. else
  1320. {
  1321. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  1322. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  1323. /* Process Unlocked */
  1324. __HAL_UNLOCK(hfmpi2c);
  1325. }
  1326. return HAL_OK;
  1327. }
  1328. else
  1329. {
  1330. return HAL_BUSY;
  1331. }
  1332. }
  1333. /**
  1334. * @brief Transmit in slave mode an amount of data in non-blocking mode with DMA
  1335. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1336. * the configuration information for the specified FMPI2C.
  1337. * @param pData Pointer to data buffer
  1338. * @param Size Amount of data to be sent
  1339. * @retval HAL status
  1340. */
  1341. HAL_StatusTypeDef HAL_FMPI2C_Slave_Transmit_DMA(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size)
  1342. {
  1343. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1344. {
  1345. if((pData == NULL) || (Size == 0))
  1346. {
  1347. return HAL_ERROR;
  1348. }
  1349. /* Process Locked */
  1350. __HAL_LOCK(hfmpi2c);
  1351. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  1352. hfmpi2c->Mode = HAL_FMPI2C_MODE_SLAVE;
  1353. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1354. /* Prepare transfer parameters */
  1355. hfmpi2c->pBuffPtr = pData;
  1356. hfmpi2c->XferCount = Size;
  1357. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1358. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1359. hfmpi2c->XferISR = FMPI2C_Slave_ISR_DMA;
  1360. /* Set the FMPI2C DMA transfer complete callback */
  1361. hfmpi2c->hdmatx->XferCpltCallback = FMPI2C_DMASlaveTransmitCplt;
  1362. /* Set the DMA error callback */
  1363. hfmpi2c->hdmatx->XferErrorCallback = FMPI2C_DMAError;
  1364. /* Set the unused DMA callbacks to NULL */
  1365. hfmpi2c->hdmatx->XferHalfCpltCallback = NULL;
  1366. hfmpi2c->hdmatx->XferAbortCallback = NULL;
  1367. /* Enable the DMA channel */
  1368. HAL_DMA_Start_IT(hfmpi2c->hdmatx, (uint32_t)pData, (uint32_t)&hfmpi2c->Instance->TXDR, hfmpi2c->XferSize);
  1369. /* Enable Address Acknowledge */
  1370. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  1371. /* Process Unlocked */
  1372. __HAL_UNLOCK(hfmpi2c);
  1373. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1374. to avoid the risk of FMPI2C interrupt handle execution before current
  1375. process unlock */
  1376. /* Enable ERR, STOP, NACK, ADDR interrupts */
  1377. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT);
  1378. /* Enable DMA Request */
  1379. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_TXDMAEN;
  1380. return HAL_OK;
  1381. }
  1382. else
  1383. {
  1384. return HAL_BUSY;
  1385. }
  1386. }
  1387. /**
  1388. * @brief Receive in slave mode an amount of data in non-blocking mode with DMA
  1389. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1390. * the configuration information for the specified FMPI2C.
  1391. * @param pData Pointer to data buffer
  1392. * @param Size Amount of data to be sent
  1393. * @retval HAL status
  1394. */
  1395. HAL_StatusTypeDef HAL_FMPI2C_Slave_Receive_DMA(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size)
  1396. {
  1397. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1398. {
  1399. if((pData == NULL) || (Size == 0))
  1400. {
  1401. return HAL_ERROR;
  1402. }
  1403. /* Process Locked */
  1404. __HAL_LOCK(hfmpi2c);
  1405. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  1406. hfmpi2c->Mode = HAL_FMPI2C_MODE_SLAVE;
  1407. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1408. /* Prepare transfer parameters */
  1409. hfmpi2c->pBuffPtr = pData;
  1410. hfmpi2c->XferCount = Size;
  1411. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1412. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1413. hfmpi2c->XferISR = FMPI2C_Slave_ISR_DMA;
  1414. /* Set the FMPI2C DMA transfer complete callback */
  1415. hfmpi2c->hdmarx->XferCpltCallback = FMPI2C_DMASlaveReceiveCplt;
  1416. /* Set the DMA error callback */
  1417. hfmpi2c->hdmarx->XferErrorCallback = FMPI2C_DMAError;
  1418. /* Set the unused DMA callbacks to NULL */
  1419. hfmpi2c->hdmarx->XferHalfCpltCallback = NULL;
  1420. hfmpi2c->hdmarx->XferAbortCallback = NULL;
  1421. /* Enable the DMA channel */
  1422. HAL_DMA_Start_IT(hfmpi2c->hdmarx, (uint32_t)&hfmpi2c->Instance->RXDR, (uint32_t)pData, hfmpi2c->XferSize);
  1423. /* Enable Address Acknowledge */
  1424. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  1425. /* Process Unlocked */
  1426. __HAL_UNLOCK(hfmpi2c);
  1427. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1428. to avoid the risk of FMPI2C interrupt handle execution before current
  1429. process unlock */
  1430. /* Enable ERR, STOP, NACK, ADDR interrupts */
  1431. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT);
  1432. /* Enable DMA Request */
  1433. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_RXDMAEN;
  1434. return HAL_OK;
  1435. }
  1436. else
  1437. {
  1438. return HAL_BUSY;
  1439. }
  1440. }
  1441. /**
  1442. * @brief Write an amount of data in blocking mode to a specific memory address
  1443. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1444. * the configuration information for the specified FMPI2C.
  1445. * @param DevAddress Target device address
  1446. * @param MemAddress Internal memory address
  1447. * @param MemAddSize Size of internal memory address
  1448. * @param pData Pointer to data buffer
  1449. * @param Size Amount of data to be sent
  1450. * @param Timeout Timeout duration
  1451. * @retval HAL status
  1452. */
  1453. HAL_StatusTypeDef HAL_FMPI2C_Mem_Write(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  1454. {
  1455. uint32_t tickstart = 0U;
  1456. /* Check the parameters */
  1457. assert_param(IS_FMPI2C_MEMADD_SIZE(MemAddSize));
  1458. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1459. {
  1460. if((pData == NULL) || (Size == 0))
  1461. {
  1462. return HAL_ERROR;
  1463. }
  1464. /* Process Locked */
  1465. __HAL_LOCK(hfmpi2c);
  1466. /* Init tickstart for timeout management*/
  1467. tickstart = HAL_GetTick();
  1468. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_BUSY, SET, FMPI2C_TIMEOUT_BUSY, tickstart) != HAL_OK)
  1469. {
  1470. return HAL_TIMEOUT;
  1471. }
  1472. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  1473. hfmpi2c->Mode = HAL_FMPI2C_MODE_MEM;
  1474. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1475. /* Prepare transfer parameters */
  1476. hfmpi2c->pBuffPtr = pData;
  1477. hfmpi2c->XferCount = Size;
  1478. hfmpi2c->XferISR = NULL;
  1479. /* Send Slave Address and Memory Address */
  1480. if(FMPI2C_RequestMemoryWrite(hfmpi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)
  1481. {
  1482. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  1483. {
  1484. /* Process Unlocked */
  1485. __HAL_UNLOCK(hfmpi2c);
  1486. return HAL_ERROR;
  1487. }
  1488. else
  1489. {
  1490. /* Process Unlocked */
  1491. __HAL_UNLOCK(hfmpi2c);
  1492. return HAL_TIMEOUT;
  1493. }
  1494. }
  1495. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE */
  1496. if(hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1497. {
  1498. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1499. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  1500. }
  1501. else
  1502. {
  1503. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1504. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  1505. }
  1506. do
  1507. {
  1508. /* Wait until TXIS flag is set */
  1509. if(FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  1510. {
  1511. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  1512. {
  1513. return HAL_ERROR;
  1514. }
  1515. else
  1516. {
  1517. return HAL_TIMEOUT;
  1518. }
  1519. }
  1520. /* Write data to TXDR */
  1521. hfmpi2c->Instance->TXDR = (*hfmpi2c->pBuffPtr++);
  1522. hfmpi2c->XferCount--;
  1523. hfmpi2c->XferSize--;
  1524. if((hfmpi2c->XferSize == 0U) && (hfmpi2c->XferCount!=0U))
  1525. {
  1526. /* Wait until TCR flag is set */
  1527. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK)
  1528. {
  1529. return HAL_TIMEOUT;
  1530. }
  1531. if(hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1532. {
  1533. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1534. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  1535. }
  1536. else
  1537. {
  1538. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1539. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  1540. }
  1541. }
  1542. }while(hfmpi2c->XferCount > 0U);
  1543. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  1544. /* Wait until STOPF flag is reset */
  1545. if(FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  1546. {
  1547. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  1548. {
  1549. return HAL_ERROR;
  1550. }
  1551. else
  1552. {
  1553. return HAL_TIMEOUT;
  1554. }
  1555. }
  1556. /* Clear STOP Flag */
  1557. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  1558. /* Clear Configuration Register 2 */
  1559. FMPI2C_RESET_CR2(hfmpi2c);
  1560. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  1561. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  1562. /* Process Unlocked */
  1563. __HAL_UNLOCK(hfmpi2c);
  1564. return HAL_OK;
  1565. }
  1566. else
  1567. {
  1568. return HAL_BUSY;
  1569. }
  1570. }
  1571. /**
  1572. * @brief Read an amount of data in blocking mode from a specific memory address
  1573. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1574. * the configuration information for the specified FMPI2C.
  1575. * @param DevAddress Target device address
  1576. * @param MemAddress Internal memory address
  1577. * @param MemAddSize Size of internal memory address
  1578. * @param pData Pointer to data buffer
  1579. * @param Size Amount of data to be sent
  1580. * @param Timeout Timeout duration
  1581. * @retval HAL status
  1582. */
  1583. HAL_StatusTypeDef HAL_FMPI2C_Mem_Read(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  1584. {
  1585. uint32_t tickstart = 0U;
  1586. /* Check the parameters */
  1587. assert_param(IS_FMPI2C_MEMADD_SIZE(MemAddSize));
  1588. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1589. {
  1590. if((pData == NULL) || (Size == 0))
  1591. {
  1592. return HAL_ERROR;
  1593. }
  1594. /* Process Locked */
  1595. __HAL_LOCK(hfmpi2c);
  1596. /* Init tickstart for timeout management*/
  1597. tickstart = HAL_GetTick();
  1598. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_BUSY, SET, FMPI2C_TIMEOUT_BUSY, tickstart) != HAL_OK)
  1599. {
  1600. return HAL_TIMEOUT;
  1601. }
  1602. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  1603. hfmpi2c->Mode = HAL_FMPI2C_MODE_MEM;
  1604. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1605. /* Prepare transfer parameters */
  1606. hfmpi2c->pBuffPtr = pData;
  1607. hfmpi2c->XferCount = Size;
  1608. hfmpi2c->XferISR = NULL;
  1609. /* Send Slave Address and Memory Address */
  1610. if(FMPI2C_RequestMemoryRead(hfmpi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)
  1611. {
  1612. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  1613. {
  1614. /* Process Unlocked */
  1615. __HAL_UNLOCK(hfmpi2c);
  1616. return HAL_ERROR;
  1617. }
  1618. else
  1619. {
  1620. /* Process Unlocked */
  1621. __HAL_UNLOCK(hfmpi2c);
  1622. return HAL_TIMEOUT;
  1623. }
  1624. }
  1625. /* Send Slave Address */
  1626. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  1627. if(hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1628. {
  1629. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1630. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_GENERATE_START_READ);
  1631. }
  1632. else
  1633. {
  1634. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1635. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_GENERATE_START_READ);
  1636. }
  1637. do
  1638. {
  1639. /* Wait until RXNE flag is set */
  1640. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_RXNE, RESET, Timeout, tickstart) != HAL_OK)
  1641. {
  1642. return HAL_TIMEOUT;
  1643. }
  1644. /* Read data from RXDR */
  1645. (*hfmpi2c->pBuffPtr++) = hfmpi2c->Instance->RXDR;
  1646. hfmpi2c->XferSize--;
  1647. hfmpi2c->XferCount--;
  1648. if((hfmpi2c->XferSize == 0U) && (hfmpi2c->XferCount != 0U))
  1649. {
  1650. /* Wait until TCR flag is set */
  1651. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK)
  1652. {
  1653. return HAL_TIMEOUT;
  1654. }
  1655. if(hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1656. {
  1657. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1658. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  1659. }
  1660. else
  1661. {
  1662. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1663. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  1664. }
  1665. }
  1666. }while(hfmpi2c->XferCount > 0U);
  1667. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  1668. /* Wait until STOPF flag is reset */
  1669. if(FMPI2C_WaitOnSTOPFlagUntilTimeout(hfmpi2c, Timeout, tickstart) != HAL_OK)
  1670. {
  1671. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  1672. {
  1673. return HAL_ERROR;
  1674. }
  1675. else
  1676. {
  1677. return HAL_TIMEOUT;
  1678. }
  1679. }
  1680. /* Clear STOP Flag */
  1681. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  1682. /* Clear Configuration Register 2 */
  1683. FMPI2C_RESET_CR2(hfmpi2c);
  1684. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  1685. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  1686. /* Process Unlocked */
  1687. __HAL_UNLOCK(hfmpi2c);
  1688. return HAL_OK;
  1689. }
  1690. else
  1691. {
  1692. return HAL_BUSY;
  1693. }
  1694. }
  1695. /**
  1696. * @brief Write an amount of data in non-blocking mode with Interrupt to a specific memory address
  1697. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1698. * the configuration information for the specified FMPI2C.
  1699. * @param DevAddress Target device address
  1700. * @param MemAddress Internal memory address
  1701. * @param MemAddSize Size of internal memory address
  1702. * @param pData Pointer to data buffer
  1703. * @param Size Amount of data to be sent
  1704. * @retval HAL status
  1705. */
  1706. HAL_StatusTypeDef HAL_FMPI2C_Mem_Write_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
  1707. {
  1708. uint32_t tickstart = 0U;
  1709. uint32_t xfermode = 0U;
  1710. /* Check the parameters */
  1711. assert_param(IS_FMPI2C_MEMADD_SIZE(MemAddSize));
  1712. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1713. {
  1714. if((pData == NULL) || (Size == 0))
  1715. {
  1716. return HAL_ERROR;
  1717. }
  1718. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  1719. {
  1720. return HAL_BUSY;
  1721. }
  1722. /* Process Locked */
  1723. __HAL_LOCK(hfmpi2c);
  1724. /* Init tickstart for timeout management*/
  1725. tickstart = HAL_GetTick();
  1726. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  1727. hfmpi2c->Mode = HAL_FMPI2C_MODE_MEM;
  1728. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1729. /* Prepare transfer parameters */
  1730. hfmpi2c->pBuffPtr = pData;
  1731. hfmpi2c->XferCount = Size;
  1732. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1733. hfmpi2c->XferISR = FMPI2C_Master_ISR_IT;
  1734. if(hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1735. {
  1736. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1737. xfermode = FMPI2C_RELOAD_MODE;
  1738. }
  1739. else
  1740. {
  1741. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1742. xfermode = FMPI2C_AUTOEND_MODE;
  1743. }
  1744. /* Send Slave Address and Memory Address */
  1745. if(FMPI2C_RequestMemoryWrite(hfmpi2c, DevAddress, MemAddress, MemAddSize, FMPI2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
  1746. {
  1747. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  1748. {
  1749. /* Process Unlocked */
  1750. __HAL_UNLOCK(hfmpi2c);
  1751. return HAL_ERROR;
  1752. }
  1753. else
  1754. {
  1755. /* Process Unlocked */
  1756. __HAL_UNLOCK(hfmpi2c);
  1757. return HAL_TIMEOUT;
  1758. }
  1759. }
  1760. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  1761. FMPI2C_TransferConfig(hfmpi2c,DevAddress, hfmpi2c->XferSize, xfermode, FMPI2C_NO_STARTSTOP);
  1762. /* Process Unlocked */
  1763. __HAL_UNLOCK(hfmpi2c);
  1764. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1765. to avoid the risk of FMPI2C interrupt handle execution before current
  1766. process unlock */
  1767. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  1768. /* possible to enable all of these */
  1769. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  1770. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT);
  1771. return HAL_OK;
  1772. }
  1773. else
  1774. {
  1775. return HAL_BUSY;
  1776. }
  1777. }
  1778. /**
  1779. * @brief Read an amount of data in non-blocking mode with Interrupt from a specific memory address
  1780. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1781. * the configuration information for the specified FMPI2C.
  1782. * @param DevAddress Target device address
  1783. * @param MemAddress Internal memory address
  1784. * @param MemAddSize Size of internal memory address
  1785. * @param pData Pointer to data buffer
  1786. * @param Size Amount of data to be sent
  1787. * @retval HAL status
  1788. */
  1789. HAL_StatusTypeDef HAL_FMPI2C_Mem_Read_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
  1790. {
  1791. uint32_t tickstart = 0U;
  1792. uint32_t xfermode = 0U;
  1793. /* Check the parameters */
  1794. assert_param(IS_FMPI2C_MEMADD_SIZE(MemAddSize));
  1795. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1796. {
  1797. if((pData == NULL) || (Size == 0))
  1798. {
  1799. return HAL_ERROR;
  1800. }
  1801. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  1802. {
  1803. return HAL_BUSY;
  1804. }
  1805. /* Process Locked */
  1806. __HAL_LOCK(hfmpi2c);
  1807. /* Init tickstart for timeout management*/
  1808. tickstart = HAL_GetTick();
  1809. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  1810. hfmpi2c->Mode = HAL_FMPI2C_MODE_MEM;
  1811. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1812. /* Prepare transfer parameters */
  1813. hfmpi2c->pBuffPtr = pData;
  1814. hfmpi2c->XferCount = Size;
  1815. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1816. hfmpi2c->XferISR = FMPI2C_Master_ISR_IT;
  1817. if(hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1818. {
  1819. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1820. xfermode = FMPI2C_RELOAD_MODE;
  1821. }
  1822. else
  1823. {
  1824. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1825. xfermode = FMPI2C_AUTOEND_MODE;
  1826. }
  1827. /* Send Slave Address and Memory Address */
  1828. if(FMPI2C_RequestMemoryRead(hfmpi2c, DevAddress, MemAddress, MemAddSize, FMPI2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
  1829. {
  1830. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  1831. {
  1832. /* Process Unlocked */
  1833. __HAL_UNLOCK(hfmpi2c);
  1834. return HAL_ERROR;
  1835. }
  1836. else
  1837. {
  1838. /* Process Unlocked */
  1839. __HAL_UNLOCK(hfmpi2c);
  1840. return HAL_TIMEOUT;
  1841. }
  1842. }
  1843. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  1844. FMPI2C_TransferConfig(hfmpi2c,DevAddress,hfmpi2c->XferSize, xfermode, FMPI2C_GENERATE_START_READ);
  1845. /* Process Unlocked */
  1846. __HAL_UNLOCK(hfmpi2c);
  1847. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1848. to avoid the risk of FMPI2C interrupt handle execution before current
  1849. process unlock */
  1850. /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  1851. /* possible to enable all of these */
  1852. /* FMPI2C_IT_ERRI | FMPI2C_IT_TCI| FMPI2C_IT_STOPI| FMPI2C_IT_NACKI | FMPI2C_IT_ADDRI | FMPI2C_IT_RXI | FMPI2C_IT_TXI */
  1853. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT);
  1854. return HAL_OK;
  1855. }
  1856. else
  1857. {
  1858. return HAL_BUSY;
  1859. }
  1860. }
  1861. /**
  1862. * @brief Write an amount of data in non-blocking mode with DMA to a specific memory address
  1863. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1864. * the configuration information for the specified FMPI2C.
  1865. * @param DevAddress Target device address
  1866. * @param MemAddress Internal memory address
  1867. * @param MemAddSize Size of internal memory address
  1868. * @param pData Pointer to data buffer
  1869. * @param Size Amount of data to be sent
  1870. * @retval HAL status
  1871. */
  1872. HAL_StatusTypeDef HAL_FMPI2C_Mem_Write_DMA(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
  1873. {
  1874. uint32_t tickstart = 0U;
  1875. uint32_t xfermode = 0U;
  1876. /* Check the parameters */
  1877. assert_param(IS_FMPI2C_MEMADD_SIZE(MemAddSize));
  1878. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1879. {
  1880. if((pData == NULL) || (Size == 0))
  1881. {
  1882. return HAL_ERROR;
  1883. }
  1884. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  1885. {
  1886. return HAL_BUSY;
  1887. }
  1888. /* Process Locked */
  1889. __HAL_LOCK(hfmpi2c);
  1890. /* Init tickstart for timeout management*/
  1891. tickstart = HAL_GetTick();
  1892. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  1893. hfmpi2c->Mode = HAL_FMPI2C_MODE_MEM;
  1894. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1895. /* Prepare transfer parameters */
  1896. hfmpi2c->pBuffPtr = pData;
  1897. hfmpi2c->XferCount = Size;
  1898. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1899. hfmpi2c->XferISR = FMPI2C_Master_ISR_DMA;
  1900. if(hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1901. {
  1902. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1903. xfermode = FMPI2C_RELOAD_MODE;
  1904. }
  1905. else
  1906. {
  1907. hfmpi2c->XferSize = hfmpi2c->XferCount;
  1908. xfermode = FMPI2C_AUTOEND_MODE;
  1909. }
  1910. /* Send Slave Address and Memory Address */
  1911. if(FMPI2C_RequestMemoryWrite(hfmpi2c, DevAddress, MemAddress, MemAddSize, FMPI2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
  1912. {
  1913. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  1914. {
  1915. /* Process Unlocked */
  1916. __HAL_UNLOCK(hfmpi2c);
  1917. return HAL_ERROR;
  1918. }
  1919. else
  1920. {
  1921. /* Process Unlocked */
  1922. __HAL_UNLOCK(hfmpi2c);
  1923. return HAL_TIMEOUT;
  1924. }
  1925. }
  1926. /* Set the FMPI2C DMA transfer complete callback */
  1927. hfmpi2c->hdmatx->XferCpltCallback = FMPI2C_DMAMasterTransmitCplt;
  1928. /* Set the DMA error callback */
  1929. hfmpi2c->hdmatx->XferErrorCallback = FMPI2C_DMAError;
  1930. /* Set the unused DMA callbacks to NULL */
  1931. hfmpi2c->hdmatx->XferHalfCpltCallback = NULL;
  1932. hfmpi2c->hdmatx->XferAbortCallback = NULL;
  1933. /* Enable the DMA channel */
  1934. HAL_DMA_Start_IT(hfmpi2c->hdmatx, (uint32_t)pData, (uint32_t)&hfmpi2c->Instance->TXDR, hfmpi2c->XferSize);
  1935. /* Send Slave Address */
  1936. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  1937. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, xfermode, FMPI2C_NO_STARTSTOP);
  1938. /* Update XferCount value */
  1939. hfmpi2c->XferCount -= hfmpi2c->XferSize;
  1940. /* Process Unlocked */
  1941. __HAL_UNLOCK(hfmpi2c);
  1942. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  1943. to avoid the risk of FMPI2C interrupt handle execution before current
  1944. process unlock */
  1945. /* Enable ERR and NACK interrupts */
  1946. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_ERROR_IT);
  1947. /* Enable DMA Request */
  1948. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_TXDMAEN;
  1949. return HAL_OK;
  1950. }
  1951. else
  1952. {
  1953. return HAL_BUSY;
  1954. }
  1955. }
  1956. /**
  1957. * @brief Reads an amount of data in non-blocking mode with DMA from a specific memory address.
  1958. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  1959. * the configuration information for the specified FMPI2C.
  1960. * @param DevAddress Target device address
  1961. * @param MemAddress Internal memory address
  1962. * @param MemAddSize Size of internal memory address
  1963. * @param pData Pointer to data buffer
  1964. * @param Size Amount of data to be read
  1965. * @retval HAL status
  1966. */
  1967. HAL_StatusTypeDef HAL_FMPI2C_Mem_Read_DMA(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
  1968. {
  1969. uint32_t tickstart = 0U;
  1970. uint32_t xfermode = 0U;
  1971. /* Check the parameters */
  1972. assert_param(IS_FMPI2C_MEMADD_SIZE(MemAddSize));
  1973. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  1974. {
  1975. if((pData == NULL) || (Size == 0))
  1976. {
  1977. return HAL_ERROR;
  1978. }
  1979. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  1980. {
  1981. return HAL_BUSY;
  1982. }
  1983. /* Process Locked */
  1984. __HAL_LOCK(hfmpi2c);
  1985. /* Init tickstart for timeout management*/
  1986. tickstart = HAL_GetTick();
  1987. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  1988. hfmpi2c->Mode = HAL_FMPI2C_MODE_MEM;
  1989. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  1990. /* Prepare transfer parameters */
  1991. hfmpi2c->pBuffPtr = pData;
  1992. hfmpi2c->XferCount = Size;
  1993. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  1994. hfmpi2c->XferISR = FMPI2C_Master_ISR_DMA;
  1995. if(hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  1996. {
  1997. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  1998. xfermode = FMPI2C_RELOAD_MODE;
  1999. }
  2000. else
  2001. {
  2002. hfmpi2c->XferSize = hfmpi2c->XferCount;
  2003. xfermode = FMPI2C_AUTOEND_MODE;
  2004. }
  2005. /* Send Slave Address and Memory Address */
  2006. if(FMPI2C_RequestMemoryRead(hfmpi2c, DevAddress, MemAddress, MemAddSize, FMPI2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
  2007. {
  2008. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  2009. {
  2010. /* Process Unlocked */
  2011. __HAL_UNLOCK(hfmpi2c);
  2012. return HAL_ERROR;
  2013. }
  2014. else
  2015. {
  2016. /* Process Unlocked */
  2017. __HAL_UNLOCK(hfmpi2c);
  2018. return HAL_TIMEOUT;
  2019. }
  2020. }
  2021. /* Set the FMPI2C DMA transfer complete callback */
  2022. hfmpi2c->hdmarx->XferCpltCallback = FMPI2C_DMAMasterReceiveCplt;
  2023. /* Set the DMA error callback */
  2024. hfmpi2c->hdmarx->XferErrorCallback = FMPI2C_DMAError;
  2025. /* Set the unused DMA callbacks to NULL */
  2026. hfmpi2c->hdmarx->XferHalfCpltCallback = NULL;
  2027. hfmpi2c->hdmarx->XferAbortCallback = NULL;
  2028. /* Enable the DMA channel */
  2029. HAL_DMA_Start_IT(hfmpi2c->hdmarx, (uint32_t)&hfmpi2c->Instance->RXDR, (uint32_t)pData, hfmpi2c->XferSize);
  2030. /* Set NBYTES to write and reload if hfmpi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */
  2031. FMPI2C_TransferConfig(hfmpi2c,DevAddress, hfmpi2c->XferSize, xfermode, FMPI2C_GENERATE_START_READ);
  2032. /* Update XferCount value */
  2033. hfmpi2c->XferCount -= hfmpi2c->XferSize;
  2034. /* Process Unlocked */
  2035. __HAL_UNLOCK(hfmpi2c);
  2036. /* Enable DMA Request */
  2037. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_RXDMAEN;
  2038. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  2039. to avoid the risk of FMPI2C interrupt handle execution before current
  2040. process unlock */
  2041. /* Enable ERR and NACK interrupts */
  2042. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_ERROR_IT);
  2043. return HAL_OK;
  2044. }
  2045. else
  2046. {
  2047. return HAL_BUSY;
  2048. }
  2049. }
  2050. /**
  2051. * @brief Checks if target device is ready for communication.
  2052. * @note This function is used with Memory devices
  2053. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2054. * the configuration information for the specified FMPI2C.
  2055. * @param DevAddress Target device address
  2056. * @param Trials Number of trials
  2057. * @param Timeout Timeout duration
  2058. * @retval HAL status
  2059. */
  2060. HAL_StatusTypeDef HAL_FMPI2C_IsDeviceReady(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
  2061. {
  2062. uint32_t tickstart = 0U;
  2063. __IO uint32_t FMPI2C_Trials = 0U;
  2064. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  2065. {
  2066. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_BUSY) == SET)
  2067. {
  2068. return HAL_BUSY;
  2069. }
  2070. /* Process Locked */
  2071. __HAL_LOCK(hfmpi2c);
  2072. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY;
  2073. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  2074. do
  2075. {
  2076. /* Generate Start */
  2077. hfmpi2c->Instance->CR2 = FMPI2C_GENERATE_START(hfmpi2c->Init.AddressingMode,DevAddress);
  2078. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  2079. /* Wait until STOPF flag is set or a NACK flag is set*/
  2080. tickstart = HAL_GetTick();
  2081. while((__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF) == RESET) && (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_AF) == RESET) && (hfmpi2c->State != HAL_FMPI2C_STATE_TIMEOUT))
  2082. {
  2083. if(Timeout != HAL_MAX_DELAY)
  2084. {
  2085. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  2086. {
  2087. /* Device is ready */
  2088. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2089. /* Process Unlocked */
  2090. __HAL_UNLOCK(hfmpi2c);
  2091. return HAL_TIMEOUT;
  2092. }
  2093. }
  2094. }
  2095. /* Check if the NACKF flag has not been set */
  2096. if (__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_AF) == RESET)
  2097. {
  2098. /* Wait until STOPF flag is reset */
  2099. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_STOPF, RESET, Timeout, tickstart) != HAL_OK)
  2100. {
  2101. return HAL_TIMEOUT;
  2102. }
  2103. /* Clear STOP Flag */
  2104. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  2105. /* Device is ready */
  2106. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2107. /* Process Unlocked */
  2108. __HAL_UNLOCK(hfmpi2c);
  2109. return HAL_OK;
  2110. }
  2111. else
  2112. {
  2113. /* Wait until STOPF flag is reset */
  2114. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_STOPF, RESET, Timeout, tickstart) != HAL_OK)
  2115. {
  2116. return HAL_TIMEOUT;
  2117. }
  2118. /* Clear NACK Flag */
  2119. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  2120. /* Clear STOP Flag, auto generated with autoend*/
  2121. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  2122. }
  2123. /* Check if the maximum allowed number of trials has been reached */
  2124. if (FMPI2C_Trials++ == Trials)
  2125. {
  2126. /* Generate Stop */
  2127. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_STOP;
  2128. /* Wait until STOPF flag is reset */
  2129. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_STOPF, RESET, Timeout, tickstart) != HAL_OK)
  2130. {
  2131. return HAL_TIMEOUT;
  2132. }
  2133. /* Clear STOP Flag */
  2134. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  2135. }
  2136. }while(FMPI2C_Trials < Trials);
  2137. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2138. /* Process Unlocked */
  2139. __HAL_UNLOCK(hfmpi2c);
  2140. return HAL_TIMEOUT;
  2141. }
  2142. else
  2143. {
  2144. return HAL_BUSY;
  2145. }
  2146. }
  2147. /**
  2148. * @brief Sequential transmit in master FMPI2C mode an amount of data in non-blocking mode with Interrupt.
  2149. * @note This interface allow to manage repeated start condition when a direction change during transfer
  2150. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2151. * the configuration information for the specified FMPI2C.
  2152. * @param DevAddress Target device address: The device 7 bits address value
  2153. * in datasheet must be shift at right before call interface
  2154. * @param pData Pointer to data buffer
  2155. * @param Size Amount of data to be sent
  2156. * @param XferOptions Options of Transfer, value of @ref FMPI2C_XFEROPTIONS
  2157. * @retval HAL status
  2158. */
  2159. HAL_StatusTypeDef HAL_FMPI2C_Master_Sequential_Transmit_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  2160. {
  2161. uint32_t xfermode = 0U;
  2162. uint32_t xferrequest = FMPI2C_GENERATE_START_WRITE;
  2163. /* Check the parameters */
  2164. assert_param(IS_FMPI2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  2165. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  2166. {
  2167. /* Process Locked */
  2168. __HAL_LOCK(hfmpi2c);
  2169. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX;
  2170. hfmpi2c->Mode = HAL_FMPI2C_MODE_MASTER;
  2171. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  2172. /* Prepare transfer parameters */
  2173. hfmpi2c->pBuffPtr = pData;
  2174. hfmpi2c->XferCount = Size;
  2175. hfmpi2c->XferOptions = XferOptions;
  2176. hfmpi2c->XferISR = FMPI2C_Master_ISR_IT;
  2177. /* If size > MAX_NBYTE_SIZE, use reload mode */
  2178. if(hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  2179. {
  2180. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  2181. xfermode = FMPI2C_RELOAD_MODE;
  2182. }
  2183. else
  2184. {
  2185. hfmpi2c->XferSize = hfmpi2c->XferCount;
  2186. xfermode = hfmpi2c->XferOptions;
  2187. }
  2188. /* If transfer direction not change, do not generate Restart Condition */
  2189. /* Mean Previous state is same as current state */
  2190. if(hfmpi2c->PreviousState == FMPI2C_STATE_SLAVE_BUSY_TX)
  2191. {
  2192. xferrequest = FMPI2C_NO_STARTSTOP;
  2193. }
  2194. /* Send Slave Address and set NBYTES to write */
  2195. FMPI2C_TransferConfig(hfmpi2c, DevAddress, hfmpi2c->XferSize, xfermode, xferrequest);
  2196. /* Process Unlocked */
  2197. __HAL_UNLOCK(hfmpi2c);
  2198. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  2199. to avoid the risk of FMPI2C interrupt handle execution before current
  2200. process unlock */
  2201. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT);
  2202. return HAL_OK;
  2203. }
  2204. else
  2205. {
  2206. return HAL_BUSY;
  2207. }
  2208. }
  2209. /**
  2210. * @brief Sequential receive in master FMPI2C mode an amount of data in non-blocking mode with Interrupt
  2211. * @note This interface allow to manage repeated start condition when a direction change during transfer
  2212. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2213. * the configuration information for the specified FMPI2C.
  2214. * @param DevAddress Target device address: The device 7 bits address value
  2215. * in datasheet must be shift at right before call interface
  2216. * @param pData Pointer to data buffer
  2217. * @param Size Amount of data to be sent
  2218. * @param XferOptions Options of Transfer, value of @ref FMPI2C_XFEROPTIONS
  2219. * @retval HAL status
  2220. */
  2221. HAL_StatusTypeDef HAL_FMPI2C_Master_Sequential_Receive_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  2222. {
  2223. uint32_t xfermode = 0U;
  2224. uint32_t xferrequest = FMPI2C_GENERATE_START_READ;
  2225. /* Check the parameters */
  2226. assert_param(IS_FMPI2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  2227. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  2228. {
  2229. /* Process Locked */
  2230. __HAL_LOCK(hfmpi2c);
  2231. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX;
  2232. hfmpi2c->Mode = HAL_FMPI2C_MODE_MASTER;
  2233. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  2234. /* Prepare transfer parameters */
  2235. hfmpi2c->pBuffPtr = pData;
  2236. hfmpi2c->XferCount = Size;
  2237. hfmpi2c->XferOptions = XferOptions;
  2238. hfmpi2c->XferISR = FMPI2C_Master_ISR_IT;
  2239. /* If hfmpi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */
  2240. if(hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  2241. {
  2242. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  2243. xfermode = FMPI2C_RELOAD_MODE;
  2244. }
  2245. else
  2246. {
  2247. hfmpi2c->XferSize = hfmpi2c->XferCount;
  2248. xfermode = hfmpi2c->XferOptions;
  2249. }
  2250. /* If transfer direction not change, do not generate Restart Condition */
  2251. /* Mean Previous state is same as current state */
  2252. if(hfmpi2c->PreviousState == FMPI2C_STATE_MASTER_BUSY_RX)
  2253. {
  2254. xferrequest = FMPI2C_NO_STARTSTOP;
  2255. }
  2256. /* Send Slave Address and set NBYTES to read */
  2257. FMPI2C_TransferConfig(hfmpi2c,DevAddress, hfmpi2c->XferSize, xfermode, xferrequest);
  2258. /* Process Unlocked */
  2259. __HAL_UNLOCK(hfmpi2c);
  2260. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  2261. to avoid the risk of FMPI2C interrupt handle execution before current
  2262. process unlock */
  2263. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT);
  2264. return HAL_OK;
  2265. }
  2266. else
  2267. {
  2268. return HAL_BUSY;
  2269. }
  2270. }
  2271. /**
  2272. * @brief Sequential transmit in slave/device FMPI2C mode an amount of data in non-blocking mode with Interrupt
  2273. * @note This interface allow to manage repeated start condition when a direction change during transfer
  2274. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2275. * the configuration information for the specified FMPI2C.
  2276. * @param pData Pointer to data buffer
  2277. * @param Size Amount of data to be sent
  2278. * @param XferOptions Options of Transfer, value of @ref FMPI2C_XFEROPTIONS
  2279. * @retval HAL status
  2280. */
  2281. HAL_StatusTypeDef HAL_FMPI2C_Slave_Sequential_Transmit_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  2282. {
  2283. /* Check the parameters */
  2284. assert_param(IS_FMPI2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  2285. if((hfmpi2c->State & HAL_FMPI2C_STATE_LISTEN) == HAL_FMPI2C_STATE_LISTEN)
  2286. {
  2287. if((pData == NULL) || (Size == 0))
  2288. {
  2289. return HAL_ERROR;
  2290. }
  2291. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  2292. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT | FMPI2C_XFER_TX_IT);
  2293. /* Process Locked */
  2294. __HAL_LOCK(hfmpi2c);
  2295. /* FMPI2C cannot manage full duplex exchange so disable previous IT enabled if any */
  2296. /* and then toggle the HAL slave RX state to TX state */
  2297. if(hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_RX_LISTEN)
  2298. {
  2299. /* Disable associated Interrupts */
  2300. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT);
  2301. }
  2302. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_TX_LISTEN;
  2303. hfmpi2c->Mode = HAL_FMPI2C_MODE_SLAVE;
  2304. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  2305. /* Enable Address Acknowledge */
  2306. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  2307. /* Prepare transfer parameters */
  2308. hfmpi2c->pBuffPtr = pData;
  2309. hfmpi2c->XferCount = Size;
  2310. hfmpi2c->XferSize = hfmpi2c->XferCount;
  2311. hfmpi2c->XferOptions = XferOptions;
  2312. hfmpi2c->XferISR = FMPI2C_Slave_ISR_IT;
  2313. if(FMPI2C_GET_DIR(hfmpi2c) == FMPI2C_DIRECTION_RECEIVE)
  2314. {
  2315. /* Clear ADDR flag after prepare the transfer parameters */
  2316. /* This action will generate an acknowledge to the Master */
  2317. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c,FMPI2C_FLAG_ADDR);
  2318. }
  2319. /* Process Unlocked */
  2320. __HAL_UNLOCK(hfmpi2c);
  2321. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  2322. to avoid the risk of FMPI2C interrupt handle execution before current
  2323. process unlock */
  2324. /* REnable ADDR interrupt */
  2325. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT | FMPI2C_XFER_LISTEN_IT);
  2326. return HAL_OK;
  2327. }
  2328. else
  2329. {
  2330. return HAL_ERROR;
  2331. }
  2332. }
  2333. /**
  2334. * @brief Sequential receive in slave/device FMPI2C mode an amount of data in non-blocking mode with Interrupt
  2335. * @note This interface allow to manage repeated start condition when a direction change during transfer
  2336. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2337. * the configuration information for the specified FMPI2C.
  2338. * @param pData Pointer to data buffer
  2339. * @param Size Amount of data to be sent
  2340. * @param XferOptions Options of Transfer, value of @ref FMPI2C_XFEROPTIONS
  2341. * @retval HAL status
  2342. */
  2343. HAL_StatusTypeDef HAL_FMPI2C_Slave_Sequential_Receive_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  2344. {
  2345. /* Check the parameters */
  2346. assert_param(IS_FMPI2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  2347. if((hfmpi2c->State & HAL_FMPI2C_STATE_LISTEN) == HAL_FMPI2C_STATE_LISTEN)
  2348. {
  2349. if((pData == NULL) || (Size == 0))
  2350. {
  2351. return HAL_ERROR;
  2352. }
  2353. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  2354. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT | FMPI2C_XFER_RX_IT);
  2355. /* Process Locked */
  2356. __HAL_LOCK(hfmpi2c);
  2357. /* FMPI2C cannot manage full duplex exchange so disable previous IT enabled if any */
  2358. /* and then toggle the HAL slave TX state to RX state */
  2359. if(hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_TX_LISTEN)
  2360. {
  2361. /* Disable associated Interrupts */
  2362. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT);
  2363. }
  2364. hfmpi2c->State = HAL_FMPI2C_STATE_BUSY_RX_LISTEN;
  2365. hfmpi2c->Mode = HAL_FMPI2C_MODE_SLAVE;
  2366. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  2367. /* Enable Address Acknowledge */
  2368. hfmpi2c->Instance->CR2 &= ~FMPI2C_CR2_NACK;
  2369. /* Prepare transfer parameters */
  2370. hfmpi2c->pBuffPtr = pData;
  2371. hfmpi2c->XferCount = Size;
  2372. hfmpi2c->XferSize = hfmpi2c->XferCount;
  2373. hfmpi2c->XferOptions = XferOptions;
  2374. hfmpi2c->XferISR = FMPI2C_Slave_ISR_IT;
  2375. if(FMPI2C_GET_DIR(hfmpi2c) == FMPI2C_DIRECTION_TRANSMIT)
  2376. {
  2377. /* Clear ADDR flag after prepare the transfer parameters */
  2378. /* This action will generate an acknowledge to the Master */
  2379. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c,FMPI2C_FLAG_ADDR);
  2380. }
  2381. /* Process Unlocked */
  2382. __HAL_UNLOCK(hfmpi2c);
  2383. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  2384. to avoid the risk of FMPI2C interrupt handle execution before current
  2385. process unlock */
  2386. /* REnable ADDR interrupt */
  2387. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT | FMPI2C_XFER_LISTEN_IT);
  2388. return HAL_OK;
  2389. }
  2390. else
  2391. {
  2392. return HAL_ERROR;
  2393. }
  2394. }
  2395. /**
  2396. * @brief Enable the Address listen mode with Interrupt.
  2397. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2398. * the configuration information for the specified FMPI2C.
  2399. * @retval HAL status
  2400. */
  2401. HAL_StatusTypeDef HAL_FMPI2C_EnableListen_IT(FMPI2C_HandleTypeDef *hfmpi2c)
  2402. {
  2403. if(hfmpi2c->State == HAL_FMPI2C_STATE_READY)
  2404. {
  2405. hfmpi2c->State = HAL_FMPI2C_STATE_LISTEN;
  2406. hfmpi2c->XferISR = FMPI2C_Slave_ISR_IT;
  2407. /* Enable the Address Match interrupt */
  2408. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT);
  2409. return HAL_OK;
  2410. }
  2411. else
  2412. {
  2413. return HAL_BUSY;
  2414. }
  2415. }
  2416. /**
  2417. * @brief Disable the Address listen mode with Interrupt.
  2418. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2419. * the configuration information for the specified FMPI2C
  2420. * @retval HAL status
  2421. */
  2422. HAL_StatusTypeDef HAL_FMPI2C_DisableListen_IT(FMPI2C_HandleTypeDef *hfmpi2c)
  2423. {
  2424. /* Declaration of tmp to prevent undefined behavior of volatile usage */
  2425. uint32_t tmp;
  2426. /* Disable Address listen mode only if a transfer is not ongoing */
  2427. if(hfmpi2c->State == HAL_FMPI2C_STATE_LISTEN)
  2428. {
  2429. tmp = (uint32_t)(hfmpi2c->State) & FMPI2C_STATE_MSK;
  2430. hfmpi2c->PreviousState = tmp | (uint32_t)(hfmpi2c->Mode);
  2431. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  2432. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  2433. hfmpi2c->XferISR = NULL;
  2434. /* Disable the Address Match interrupt */
  2435. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT);
  2436. return HAL_OK;
  2437. }
  2438. else
  2439. {
  2440. return HAL_BUSY;
  2441. }
  2442. }
  2443. /**
  2444. * @brief Abort a master FMPI2C IT or DMA process communication with Interrupt.
  2445. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2446. * the configuration information for the specified FMPI2C.
  2447. * @param DevAddress Target device address: The device 7 bits address value
  2448. * in datasheet must be shift at right before call interface
  2449. * @retval HAL status
  2450. */
  2451. HAL_StatusTypeDef HAL_FMPI2C_Master_Abort_IT(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress)
  2452. {
  2453. if(hfmpi2c->Mode == HAL_FMPI2C_MODE_MASTER)
  2454. {
  2455. /* Process Locked */
  2456. __HAL_LOCK(hfmpi2c);
  2457. /* Disable Interrupts */
  2458. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT);
  2459. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT);
  2460. /* Set State at HAL_FMPI2C_STATE_ABORT */
  2461. hfmpi2c->State = HAL_FMPI2C_STATE_ABORT;
  2462. /* Set NBYTES to 1 to generate a dummy read on FMPI2C peripheral */
  2463. /* Set AUTOEND mode, this will generate a NACK then STOP condition to abort the current transfer */
  2464. FMPI2C_TransferConfig(hfmpi2c, DevAddress, 1, FMPI2C_AUTOEND_MODE, FMPI2C_GENERATE_STOP);
  2465. /* Process Unlocked */
  2466. __HAL_UNLOCK(hfmpi2c);
  2467. /* Note : The FMPI2C interrupts must be enabled after unlocking current process
  2468. to avoid the risk of FMPI2C interrupt handle execution before current
  2469. process unlock */
  2470. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_CPLT_IT);
  2471. return HAL_OK;
  2472. }
  2473. else
  2474. {
  2475. /* Wrong usage of abort function */
  2476. /* This function should be used only in case of abort monitored by master device */
  2477. return HAL_ERROR;
  2478. }
  2479. }
  2480. /**
  2481. * @}
  2482. */
  2483. /** @defgroup FMPI2C_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
  2484. * @{
  2485. */
  2486. /**
  2487. * @brief This function handles FMPI2C event interrupt request.
  2488. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2489. * the configuration information for the specified FMPI2C.
  2490. * @retval None
  2491. */
  2492. void HAL_FMPI2C_EV_IRQHandler(FMPI2C_HandleTypeDef *hfmpi2c)
  2493. {
  2494. /* Get current IT Flags and IT sources value */
  2495. uint32_t itflags = READ_REG(hfmpi2c->Instance->ISR);
  2496. uint32_t itsources = READ_REG(hfmpi2c->Instance->CR1);
  2497. /* FMPI2C events treatment -------------------------------------*/
  2498. if(hfmpi2c->XferISR != NULL)
  2499. {
  2500. hfmpi2c->XferISR(hfmpi2c, itflags, itsources);
  2501. }
  2502. }
  2503. /**
  2504. * @brief This function handles FMPI2C error interrupt request.
  2505. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2506. * the configuration information for the specified FMPI2C.
  2507. * @retval None
  2508. */
  2509. void HAL_FMPI2C_ER_IRQHandler(FMPI2C_HandleTypeDef *hfmpi2c)
  2510. {
  2511. uint32_t itflags = READ_REG(hfmpi2c->Instance->ISR);
  2512. uint32_t itsources = READ_REG(hfmpi2c->Instance->CR1);
  2513. /* FMPI2C Bus error interrupt occurred ------------------------------------*/
  2514. if(((itflags & FMPI2C_FLAG_BERR) != RESET) && ((itsources & FMPI2C_IT_ERRI) != RESET))
  2515. {
  2516. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_BERR;
  2517. /* Clear BERR flag */
  2518. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_BERR);
  2519. }
  2520. /* FMPI2C Over-Run/Under-Run interrupt occurred ----------------------------------------*/
  2521. if(((itflags & FMPI2C_FLAG_OVR) != RESET) && ((itsources & FMPI2C_IT_ERRI) != RESET))
  2522. {
  2523. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_OVR;
  2524. /* Clear OVR flag */
  2525. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_OVR);
  2526. }
  2527. /* FMPI2C Arbitration Loss error interrupt occurred -------------------------------------*/
  2528. if(((itflags & FMPI2C_FLAG_ARLO) != RESET) && ((itsources & FMPI2C_IT_ERRI) != RESET))
  2529. {
  2530. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_ARLO;
  2531. /* Clear ARLO flag */
  2532. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_ARLO);
  2533. }
  2534. /* Call the Error Callback in case of Error detected */
  2535. if((hfmpi2c->ErrorCode & (HAL_FMPI2C_ERROR_BERR | HAL_FMPI2C_ERROR_OVR | HAL_FMPI2C_ERROR_ARLO)) != HAL_FMPI2C_ERROR_NONE)
  2536. {
  2537. FMPI2C_ITError(hfmpi2c, hfmpi2c->ErrorCode);
  2538. }
  2539. }
  2540. /**
  2541. * @brief Master Tx Transfer completed callback.
  2542. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2543. * the configuration information for the specified FMPI2C.
  2544. * @retval None
  2545. */
  2546. __weak void HAL_FMPI2C_MasterTxCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  2547. {
  2548. /* Prevent unused argument(s) compilation warning */
  2549. UNUSED(hfmpi2c);
  2550. /* NOTE : This function should not be modified, when the callback is needed,
  2551. the HAL_FMPI2C_MasterTxCpltCallback could be implemented in the user file
  2552. */
  2553. }
  2554. /**
  2555. * @brief Master Rx Transfer completed callback.
  2556. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2557. * the configuration information for the specified FMPI2C.
  2558. * @retval None
  2559. */
  2560. __weak void HAL_FMPI2C_MasterRxCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  2561. {
  2562. /* Prevent unused argument(s) compilation warning */
  2563. UNUSED(hfmpi2c);
  2564. /* NOTE : This function should not be modified, when the callback is needed,
  2565. the HAL_FMPI2C_MasterRxCpltCallback could be implemented in the user file
  2566. */
  2567. }
  2568. /** @brief Slave Tx Transfer completed callback.
  2569. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2570. * the configuration information for the specified FMPI2C.
  2571. * @retval None
  2572. */
  2573. __weak void HAL_FMPI2C_SlaveTxCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  2574. {
  2575. /* Prevent unused argument(s) compilation warning */
  2576. UNUSED(hfmpi2c);
  2577. /* NOTE : This function should not be modified, when the callback is needed,
  2578. the HAL_FMPI2C_SlaveTxCpltCallback could be implemented in the user file
  2579. */
  2580. }
  2581. /**
  2582. * @brief Slave Rx Transfer completed callback.
  2583. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2584. * the configuration information for the specified FMPI2C.
  2585. * @retval None
  2586. */
  2587. __weak void HAL_FMPI2C_SlaveRxCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  2588. {
  2589. /* Prevent unused argument(s) compilation warning */
  2590. UNUSED(hfmpi2c);
  2591. /* NOTE : This function should not be modified, when the callback is needed,
  2592. the HAL_FMPI2C_SlaveRxCpltCallback could be implemented in the user file
  2593. */
  2594. }
  2595. /**
  2596. * @brief Slave Address Match callback.
  2597. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2598. * the configuration information for the specified FMPI2C.
  2599. * @param TransferDirection: Master request Transfer Direction (Write/Read), value of @ref FMPI2C_XFEROPTIONS
  2600. * @param AddrMatchCode: Address Match Code
  2601. * @retval None
  2602. */
  2603. __weak void HAL_FMPI2C_AddrCallback(FMPI2C_HandleTypeDef *hfmpi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
  2604. {
  2605. /* Prevent unused argument(s) compilation warning */
  2606. UNUSED(hfmpi2c);
  2607. UNUSED(TransferDirection);
  2608. UNUSED(AddrMatchCode);
  2609. /* NOTE : This function should not be modified, when the callback is needed,
  2610. the HAL_FMPI2C_AddrCallback() could be implemented in the user file
  2611. */
  2612. }
  2613. /**
  2614. * @brief Listen Complete callback.
  2615. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2616. * the configuration information for the specified FMPI2C.
  2617. * @retval None
  2618. */
  2619. __weak void HAL_FMPI2C_ListenCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  2620. {
  2621. /* Prevent unused argument(s) compilation warning */
  2622. UNUSED(hfmpi2c);
  2623. /* NOTE : This function should not be modified, when the callback is needed,
  2624. the HAL_FMPI2C_ListenCpltCallback() could be implemented in the user file
  2625. */
  2626. }
  2627. /**
  2628. * @brief Memory Tx Transfer completed callback.
  2629. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2630. * the configuration information for the specified FMPI2C.
  2631. * @retval None
  2632. */
  2633. __weak void HAL_FMPI2C_MemTxCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  2634. {
  2635. /* Prevent unused argument(s) compilation warning */
  2636. UNUSED(hfmpi2c);
  2637. /* NOTE : This function should not be modified, when the callback is needed,
  2638. the HAL_FMPI2C_MemTxCpltCallback could be implemented in the user file
  2639. */
  2640. }
  2641. /**
  2642. * @brief Memory Rx Transfer completed callback.
  2643. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2644. * the configuration information for the specified FMPI2C.
  2645. * @retval None
  2646. */
  2647. __weak void HAL_FMPI2C_MemRxCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  2648. {
  2649. /* Prevent unused argument(s) compilation warning */
  2650. UNUSED(hfmpi2c);
  2651. /* NOTE : This function should not be modified, when the callback is needed,
  2652. the HAL_FMPI2C_MemRxCpltCallback could be implemented in the user file
  2653. */
  2654. }
  2655. /**
  2656. * @brief FMPI2C error callback.
  2657. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2658. * the configuration information for the specified FMPI2C.
  2659. * @retval None
  2660. */
  2661. __weak void HAL_FMPI2C_ErrorCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  2662. {
  2663. /* Prevent unused argument(s) compilation warning */
  2664. UNUSED(hfmpi2c);
  2665. /* NOTE : This function should not be modified, when the callback is needed,
  2666. the HAL_FMPI2C_ErrorCallback could be implemented in the user file
  2667. */
  2668. }
  2669. /**
  2670. * @brief FMPI2C abort callback.
  2671. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2672. * the configuration information for the specified FMPI2C.
  2673. * @retval None
  2674. */
  2675. __weak void HAL_FMPI2C_AbortCpltCallback(FMPI2C_HandleTypeDef *hfmpi2c)
  2676. {
  2677. /* Prevent unused argument(s) compilation warning */
  2678. UNUSED(hfmpi2c);
  2679. /* NOTE : This function should not be modified, when the callback is needed,
  2680. the HAL_FMPI2C_AbortCpltCallback could be implemented in the user file
  2681. */
  2682. }
  2683. /**
  2684. * @}
  2685. */
  2686. /** @defgroup FMPI2C_Exported_Functions_Group3 Peripheral State, Mode and Error functions
  2687. * @brief Peripheral State, Mode and Error functions
  2688. *
  2689. @verbatim
  2690. ===============================================================================
  2691. ##### Peripheral State, Mode and Error functions #####
  2692. ===============================================================================
  2693. [..]
  2694. This subsection permit to get in run-time the status of the peripheral
  2695. and the data flow.
  2696. @endverbatim
  2697. * @{
  2698. */
  2699. /**
  2700. * @brief Return the FMPI2C handle state.
  2701. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2702. * the configuration information for the specified FMPI2C.
  2703. * @retval HAL state
  2704. */
  2705. HAL_FMPI2C_StateTypeDef HAL_FMPI2C_GetState(FMPI2C_HandleTypeDef *hfmpi2c)
  2706. {
  2707. /* Return FMPI2C handle state */
  2708. return hfmpi2c->State;
  2709. }
  2710. /**
  2711. * @brief Returns the FMPI2C Master, Slave, Memory or no mode.
  2712. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2713. * the configuration information for FMPI2C module
  2714. * @retval HAL mode
  2715. */
  2716. HAL_FMPI2C_ModeTypeDef HAL_FMPI2C_GetMode(FMPI2C_HandleTypeDef *hfmpi2c)
  2717. {
  2718. return hfmpi2c->Mode;
  2719. }
  2720. /**
  2721. * @brief Return the FMPI2C error code.
  2722. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2723. * the configuration information for the specified FMPI2C.
  2724. * @retval FMPI2C Error Code
  2725. */
  2726. uint32_t HAL_FMPI2C_GetError(FMPI2C_HandleTypeDef *hfmpi2c)
  2727. {
  2728. return hfmpi2c->ErrorCode;
  2729. }
  2730. /**
  2731. * @}
  2732. */
  2733. /**
  2734. * @}
  2735. */
  2736. /** @addtogroup FMPI2C_Private_Functions
  2737. * @{
  2738. */
  2739. /**
  2740. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Master Mode with Interrupt.
  2741. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2742. * the configuration information for the specified FMPI2C.
  2743. * @param ITFlags Interrupt flags to handle.
  2744. * @param ITSources Interrupt sources enabled.
  2745. * @retval HAL status
  2746. */
  2747. static HAL_StatusTypeDef FMPI2C_Master_ISR_IT(struct __FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags, uint32_t ITSources)
  2748. {
  2749. uint16_t devaddress = 0;
  2750. /* Process Locked */
  2751. __HAL_LOCK(hfmpi2c);
  2752. if(((ITFlags & FMPI2C_FLAG_AF) != RESET) && ((ITSources & FMPI2C_IT_NACKI) != RESET))
  2753. {
  2754. /* Clear NACK Flag */
  2755. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  2756. /* Set corresponding Error Code */
  2757. /* No need to generate STOP, it is automatically done */
  2758. /* Error callback will be send during stop flag treatment */
  2759. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  2760. /* Flush TX register */
  2761. FMPI2C_Flush_TXDR(hfmpi2c);
  2762. }
  2763. else if(((ITFlags & FMPI2C_FLAG_RXNE) != RESET) && ((ITSources & FMPI2C_IT_RXI) != RESET))
  2764. {
  2765. /* Read data from RXDR */
  2766. (*hfmpi2c->pBuffPtr++) = hfmpi2c->Instance->RXDR;
  2767. hfmpi2c->XferSize--;
  2768. hfmpi2c->XferCount--;
  2769. }
  2770. else if(((ITFlags & FMPI2C_FLAG_TXIS) != RESET) && ((ITSources & FMPI2C_IT_TXI) != RESET))
  2771. {
  2772. /* Write data to TXDR */
  2773. hfmpi2c->Instance->TXDR = (*hfmpi2c->pBuffPtr++);
  2774. hfmpi2c->XferSize--;
  2775. hfmpi2c->XferCount--;
  2776. }
  2777. else if(((ITFlags & FMPI2C_FLAG_TCR) != RESET) && ((ITSources & FMPI2C_IT_TCI) != RESET))
  2778. {
  2779. if((hfmpi2c->XferSize == 0U) && (hfmpi2c->XferCount != 0U))
  2780. {
  2781. devaddress = (hfmpi2c->Instance->CR2 & FMPI2C_CR2_SADD);
  2782. if(hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  2783. {
  2784. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  2785. FMPI2C_TransferConfig(hfmpi2c, devaddress, hfmpi2c->XferSize, FMPI2C_RELOAD_MODE, FMPI2C_NO_STARTSTOP);
  2786. }
  2787. else
  2788. {
  2789. hfmpi2c->XferSize = hfmpi2c->XferCount;
  2790. if(hfmpi2c->XferOptions != FMPI2C_NO_OPTION_FRAME)
  2791. {
  2792. FMPI2C_TransferConfig(hfmpi2c, devaddress, hfmpi2c->XferSize, hfmpi2c->XferOptions, FMPI2C_NO_STARTSTOP);
  2793. }
  2794. else
  2795. {
  2796. FMPI2C_TransferConfig(hfmpi2c, devaddress, hfmpi2c->XferSize, FMPI2C_AUTOEND_MODE, FMPI2C_NO_STARTSTOP);
  2797. }
  2798. }
  2799. }
  2800. else
  2801. {
  2802. /* Call TxCpltCallback() if no stop mode is set */
  2803. if((FMPI2C_GET_STOP_MODE(hfmpi2c) != FMPI2C_AUTOEND_MODE)&&(hfmpi2c->Mode == HAL_FMPI2C_MODE_MASTER))
  2804. {
  2805. /* Call FMPI2C Master Sequential complete process */
  2806. FMPI2C_ITMasterSequentialCplt(hfmpi2c);
  2807. }
  2808. else
  2809. {
  2810. /* Wrong size Status regarding TCR flag event */
  2811. /* Call the corresponding callback to inform upper layer of End of Transfer */
  2812. FMPI2C_ITError(hfmpi2c, HAL_FMPI2C_ERROR_SIZE);
  2813. }
  2814. }
  2815. }
  2816. else if(((ITFlags & FMPI2C_FLAG_TC) != RESET) && ((ITSources & FMPI2C_IT_TCI) != RESET))
  2817. {
  2818. if(hfmpi2c->XferCount == 0U)
  2819. {
  2820. if((FMPI2C_GET_STOP_MODE(hfmpi2c) != FMPI2C_AUTOEND_MODE)&&(hfmpi2c->Mode == HAL_FMPI2C_MODE_MASTER))
  2821. {
  2822. /* Call FMPI2C Master Sequential complete process */
  2823. FMPI2C_ITMasterSequentialCplt(hfmpi2c);
  2824. }
  2825. }
  2826. else
  2827. {
  2828. /* Wrong size Status regarding TC flag event */
  2829. /* Call the corresponding callback to inform upper layer of End of Transfer */
  2830. FMPI2C_ITError(hfmpi2c, HAL_FMPI2C_ERROR_SIZE);
  2831. }
  2832. }
  2833. if(((ITFlags & FMPI2C_FLAG_STOPF) != RESET) && ((ITSources & FMPI2C_IT_STOPI) != RESET))
  2834. {
  2835. /* Call FMPI2C Master complete process */
  2836. FMPI2C_ITMasterCplt(hfmpi2c, ITFlags);
  2837. }
  2838. /* Process Unlocked */
  2839. __HAL_UNLOCK(hfmpi2c);
  2840. return HAL_OK;
  2841. }
  2842. /**
  2843. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Slave Mode with Interrupt.
  2844. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2845. * the configuration information for the specified FMPI2C.
  2846. * @param ITFlags Interrupt flags to handle.
  2847. * @param ITSources Interrupt sources enabled.
  2848. * @retval HAL status
  2849. */
  2850. static HAL_StatusTypeDef FMPI2C_Slave_ISR_IT(struct __FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags, uint32_t ITSources)
  2851. {
  2852. /* Process locked */
  2853. __HAL_LOCK(hfmpi2c);
  2854. if(((ITFlags & FMPI2C_FLAG_AF) != RESET) && ((ITSources & FMPI2C_IT_NACKI) != RESET))
  2855. {
  2856. /* Check that FMPI2C transfer finished */
  2857. /* if yes, normal use case, a NACK is sent by the MASTER when Transfer is finished */
  2858. /* Mean XferCount == 0*/
  2859. /* So clear Flag NACKF only */
  2860. if(hfmpi2c->XferCount == 0U)
  2861. {
  2862. if(((hfmpi2c->XferOptions == FMPI2C_FIRST_AND_LAST_FRAME) || (hfmpi2c->XferOptions == FMPI2C_LAST_FRAME)) && \
  2863. (hfmpi2c->State == HAL_FMPI2C_STATE_LISTEN))
  2864. {
  2865. /* Call FMPI2C Listen complete process */
  2866. FMPI2C_ITListenCplt(hfmpi2c, ITFlags);
  2867. }
  2868. else if((hfmpi2c->XferOptions != FMPI2C_NO_OPTION_FRAME) && (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_TX_LISTEN))
  2869. {
  2870. /* Clear NACK Flag */
  2871. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  2872. /* Flush TX register */
  2873. FMPI2C_Flush_TXDR(hfmpi2c);
  2874. /* Last Byte is Transmitted */
  2875. /* Call FMPI2C Slave Sequential complete process */
  2876. FMPI2C_ITSlaveSequentialCplt(hfmpi2c);
  2877. }
  2878. else
  2879. {
  2880. /* Clear NACK Flag */
  2881. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  2882. }
  2883. }
  2884. else
  2885. {
  2886. /* if no, error use case, a Non-Acknowledge of last Data is generated by the MASTER*/
  2887. /* Clear NACK Flag */
  2888. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  2889. /* Set ErrorCode corresponding to a Non-Acknowledge */
  2890. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  2891. }
  2892. }
  2893. else if(((ITFlags & FMPI2C_FLAG_RXNE) != RESET) && ((ITSources & FMPI2C_IT_RXI) != RESET))
  2894. {
  2895. if(hfmpi2c->XferCount > 0U)
  2896. {
  2897. /* Read data from RXDR */
  2898. (*hfmpi2c->pBuffPtr++) = hfmpi2c->Instance->RXDR;
  2899. hfmpi2c->XferSize--;
  2900. hfmpi2c->XferCount--;
  2901. }
  2902. if((hfmpi2c->XferCount == 0U) && \
  2903. (hfmpi2c->XferOptions != FMPI2C_NO_OPTION_FRAME))
  2904. {
  2905. /* Call FMPI2C Slave Sequential complete process */
  2906. FMPI2C_ITSlaveSequentialCplt(hfmpi2c);
  2907. }
  2908. }
  2909. else if(((ITFlags & FMPI2C_FLAG_ADDR) != RESET) && ((ITSources & FMPI2C_IT_ADDRI) != RESET))
  2910. {
  2911. FMPI2C_ITAddrCplt(hfmpi2c, ITFlags);
  2912. }
  2913. else if(((ITFlags & FMPI2C_FLAG_TXIS) != RESET) && ((ITSources & FMPI2C_IT_TXI) != RESET))
  2914. {
  2915. /* Write data to TXDR only if XferCount not reach "0" */
  2916. /* A TXIS flag can be set, during STOP treatment */
  2917. /* Check if all Datas have already been sent */
  2918. /* If it is the case, this last write in TXDR is not sent, correspond to a dummy TXIS event */
  2919. if(hfmpi2c->XferCount > 0U)
  2920. {
  2921. /* Write data to TXDR */
  2922. hfmpi2c->Instance->TXDR = (*hfmpi2c->pBuffPtr++);
  2923. hfmpi2c->XferCount--;
  2924. hfmpi2c->XferSize--;
  2925. }
  2926. else
  2927. {
  2928. if((hfmpi2c->XferOptions == FMPI2C_NEXT_FRAME) || (hfmpi2c->XferOptions == FMPI2C_FIRST_FRAME))
  2929. {
  2930. /* Last Byte is Transmitted */
  2931. /* Call FMPI2C Slave Sequential complete process */
  2932. FMPI2C_ITSlaveSequentialCplt(hfmpi2c);
  2933. }
  2934. }
  2935. }
  2936. /* Check if STOPF is set */
  2937. if(((ITFlags & FMPI2C_FLAG_STOPF) != RESET) && ((ITSources & FMPI2C_IT_STOPI) != RESET))
  2938. {
  2939. /* Call FMPI2C Slave complete process */
  2940. FMPI2C_ITSlaveCplt(hfmpi2c, ITFlags);
  2941. }
  2942. /* Process Unlocked */
  2943. __HAL_UNLOCK(hfmpi2c);
  2944. return HAL_OK;
  2945. }
  2946. /**
  2947. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Master Mode with DMA.
  2948. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  2949. * the configuration information for the specified FMPI2C.
  2950. * @param ITFlags Interrupt flags to handle.
  2951. * @param ITSources Interrupt sources enabled.
  2952. * @retval HAL status
  2953. */
  2954. static HAL_StatusTypeDef FMPI2C_Master_ISR_DMA(struct __FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags, uint32_t ITSources)
  2955. {
  2956. uint16_t devaddress = 0;
  2957. uint32_t xfermode = 0U;
  2958. /* Process Locked */
  2959. __HAL_LOCK(hfmpi2c);
  2960. if(((ITFlags & FMPI2C_FLAG_AF) != RESET) && ((ITSources & FMPI2C_IT_NACKI) != RESET))
  2961. {
  2962. /* Clear NACK Flag */
  2963. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  2964. /* Set corresponding Error Code */
  2965. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  2966. /* No need to generate STOP, it is automatically done */
  2967. /* But enable STOP interrupt, to treat it */
  2968. /* Error callback will be send during stop flag treatment */
  2969. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_CPLT_IT);
  2970. /* Flush TX register */
  2971. FMPI2C_Flush_TXDR(hfmpi2c);
  2972. }
  2973. else if(((ITFlags & FMPI2C_FLAG_TCR) != RESET) && ((ITSources & FMPI2C_IT_TCI) != RESET))
  2974. {
  2975. /* Disable TC interrupt */
  2976. __HAL_FMPI2C_DISABLE_IT(hfmpi2c, FMPI2C_IT_TCI);
  2977. if(hfmpi2c->XferCount != 0U)
  2978. {
  2979. /* Recover Slave address */
  2980. devaddress = (hfmpi2c->Instance->CR2 & FMPI2C_CR2_SADD);
  2981. /* Prepare the new XferSize to transfer */
  2982. if(hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  2983. {
  2984. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  2985. xfermode = FMPI2C_RELOAD_MODE;
  2986. }
  2987. else
  2988. {
  2989. hfmpi2c->XferSize = hfmpi2c->XferCount;
  2990. xfermode = FMPI2C_AUTOEND_MODE;
  2991. }
  2992. /* Set the new XferSize in Nbytes register */
  2993. FMPI2C_TransferConfig(hfmpi2c, devaddress, hfmpi2c->XferSize, xfermode, FMPI2C_NO_STARTSTOP);
  2994. /* Update XferCount value */
  2995. hfmpi2c->XferCount -= hfmpi2c->XferSize;
  2996. /* Enable DMA Request */
  2997. if(hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_RX)
  2998. {
  2999. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_RXDMAEN;
  3000. }
  3001. else
  3002. {
  3003. hfmpi2c->Instance->CR1 |= FMPI2C_CR1_TXDMAEN;
  3004. }
  3005. }
  3006. else
  3007. {
  3008. /* Wrong size Status regarding TCR flag event */
  3009. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3010. FMPI2C_ITError(hfmpi2c, HAL_FMPI2C_ERROR_SIZE);
  3011. }
  3012. }
  3013. else if(((ITFlags & FMPI2C_FLAG_STOPF) != RESET) && ((ITSources & FMPI2C_IT_STOPI) != RESET))
  3014. {
  3015. /* Call FMPI2C Master complete process */
  3016. FMPI2C_ITMasterCplt(hfmpi2c, ITFlags);
  3017. }
  3018. /* Process Unlocked */
  3019. __HAL_UNLOCK(hfmpi2c);
  3020. return HAL_OK;
  3021. }
  3022. /**
  3023. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Slave Mode with DMA.
  3024. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3025. * the configuration information for the specified FMPI2C.
  3026. * @param ITFlags Interrupt flags to handle.
  3027. * @param ITSources Interrupt sources enabled.
  3028. * @retval HAL status
  3029. */
  3030. static HAL_StatusTypeDef FMPI2C_Slave_ISR_DMA(struct __FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags, uint32_t ITSources)
  3031. {
  3032. /* Process locked */
  3033. __HAL_LOCK(hfmpi2c);
  3034. if(((ITFlags & FMPI2C_FLAG_AF) != RESET) && ((ITSources & FMPI2C_IT_NACKI) != RESET))
  3035. {
  3036. /* Check that FMPI2C transfer finished */
  3037. /* if yes, normal use case, a NACK is sent by the MASTER when Transfer is finished */
  3038. /* Mean XferCount == 0 */
  3039. /* So clear Flag NACKF only */
  3040. if(FMPI2C_GET_DMA_REMAIN_DATA(hfmpi2c) == 0U)
  3041. {
  3042. /* Clear NACK Flag */
  3043. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  3044. }
  3045. else
  3046. {
  3047. /* if no, error use case, a Non-Acknowledge of last Data is generated by the MASTER*/
  3048. /* Clear NACK Flag */
  3049. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  3050. /* Set ErrorCode corresponding to a Non-Acknowledge */
  3051. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  3052. }
  3053. }
  3054. else if(((ITFlags & FMPI2C_FLAG_ADDR) != RESET) && ((ITSources & FMPI2C_IT_ADDRI) != RESET))
  3055. {
  3056. /* Clear ADDR flag */
  3057. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_ADDR);
  3058. }
  3059. else if(((ITFlags & FMPI2C_FLAG_STOPF) != RESET) && ((ITSources & FMPI2C_IT_STOPI) != RESET))
  3060. {
  3061. /* Call FMPI2C Slave complete process */
  3062. FMPI2C_ITSlaveCplt(hfmpi2c, ITFlags);
  3063. }
  3064. /* Process Unlocked */
  3065. __HAL_UNLOCK(hfmpi2c);
  3066. return HAL_OK;
  3067. }
  3068. /**
  3069. * @brief Master sends target device address followed by internal memory address for write request.
  3070. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3071. * the configuration information for the specified FMPI2C.
  3072. * @param DevAddress Target device address
  3073. * @param MemAddress Internal memory address
  3074. * @param MemAddSize Size of internal memory address
  3075. * @param Timeout Timeout duration
  3076. * @param Tickstart Tick start value
  3077. * @retval HAL status
  3078. */
  3079. static HAL_StatusTypeDef FMPI2C_RequestMemoryWrite(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart)
  3080. {
  3081. FMPI2C_TransferConfig(hfmpi2c,DevAddress,MemAddSize, FMPI2C_RELOAD_MODE, FMPI2C_GENERATE_START_WRITE);
  3082. /* Wait until TXIS flag is set */
  3083. if(FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, Timeout, Tickstart) != HAL_OK)
  3084. {
  3085. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  3086. {
  3087. return HAL_ERROR;
  3088. }
  3089. else
  3090. {
  3091. return HAL_TIMEOUT;
  3092. }
  3093. }
  3094. /* If Memory address size is 8Bit */
  3095. if(MemAddSize == FMPI2C_MEMADD_SIZE_8BIT)
  3096. {
  3097. /* Send Memory Address */
  3098. hfmpi2c->Instance->TXDR = FMPI2C_MEM_ADD_LSB(MemAddress);
  3099. }
  3100. /* If Memory address size is 16Bit */
  3101. else
  3102. {
  3103. /* Send MSB of Memory Address */
  3104. hfmpi2c->Instance->TXDR = FMPI2C_MEM_ADD_MSB(MemAddress);
  3105. /* Wait until TXIS flag is set */
  3106. if(FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, Timeout, Tickstart) != HAL_OK)
  3107. {
  3108. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  3109. {
  3110. return HAL_ERROR;
  3111. }
  3112. else
  3113. {
  3114. return HAL_TIMEOUT;
  3115. }
  3116. }
  3117. /* Send LSB of Memory Address */
  3118. hfmpi2c->Instance->TXDR = FMPI2C_MEM_ADD_LSB(MemAddress);
  3119. }
  3120. /* Wait until TCR flag is set */
  3121. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_TCR, RESET, Timeout, Tickstart) != HAL_OK)
  3122. {
  3123. return HAL_TIMEOUT;
  3124. }
  3125. return HAL_OK;
  3126. }
  3127. /**
  3128. * @brief Master sends target device address followed by internal memory address for read request.
  3129. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3130. * the configuration information for the specified FMPI2C.
  3131. * @param DevAddress Target device address
  3132. * @param MemAddress Internal memory address
  3133. * @param MemAddSize Size of internal memory address
  3134. * @param Timeout Timeout duration
  3135. * @param Tickstart Tick start value
  3136. * @retval HAL status
  3137. */
  3138. static HAL_StatusTypeDef FMPI2C_RequestMemoryRead(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart)
  3139. {
  3140. FMPI2C_TransferConfig(hfmpi2c,DevAddress,MemAddSize, FMPI2C_SOFTEND_MODE, FMPI2C_GENERATE_START_WRITE);
  3141. /* Wait until TXIS flag is set */
  3142. if(FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, Timeout, Tickstart) != HAL_OK)
  3143. {
  3144. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  3145. {
  3146. return HAL_ERROR;
  3147. }
  3148. else
  3149. {
  3150. return HAL_TIMEOUT;
  3151. }
  3152. }
  3153. /* If Memory address size is 8Bit */
  3154. if(MemAddSize == FMPI2C_MEMADD_SIZE_8BIT)
  3155. {
  3156. /* Send Memory Address */
  3157. hfmpi2c->Instance->TXDR = FMPI2C_MEM_ADD_LSB(MemAddress);
  3158. }
  3159. /* If Memory address size is 16Bit */
  3160. else
  3161. {
  3162. /* Send MSB of Memory Address */
  3163. hfmpi2c->Instance->TXDR = FMPI2C_MEM_ADD_MSB(MemAddress);
  3164. /* Wait until TXIS flag is set */
  3165. if(FMPI2C_WaitOnTXISFlagUntilTimeout(hfmpi2c, Timeout, Tickstart) != HAL_OK)
  3166. {
  3167. if(hfmpi2c->ErrorCode == HAL_FMPI2C_ERROR_AF)
  3168. {
  3169. return HAL_ERROR;
  3170. }
  3171. else
  3172. {
  3173. return HAL_TIMEOUT;
  3174. }
  3175. }
  3176. /* Send LSB of Memory Address */
  3177. hfmpi2c->Instance->TXDR = FMPI2C_MEM_ADD_LSB(MemAddress);
  3178. }
  3179. /* Wait until TC flag is set */
  3180. if(FMPI2C_WaitOnFlagUntilTimeout(hfmpi2c, FMPI2C_FLAG_TC, RESET, Timeout, Tickstart) != HAL_OK)
  3181. {
  3182. return HAL_TIMEOUT;
  3183. }
  3184. return HAL_OK;
  3185. }
  3186. /**
  3187. * @brief FMPI2C Address complete process callback.
  3188. * @param hfmpi2c FMPI2C handle.
  3189. * @param ITFlags Interrupt flags to handle.
  3190. * @retval None
  3191. */
  3192. static void FMPI2C_ITAddrCplt(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags)
  3193. {
  3194. uint8_t transferdirection = 0;
  3195. uint16_t slaveaddrcode = 0;
  3196. uint16_t ownadd1code = 0;
  3197. uint16_t ownadd2code = 0;
  3198. /* In case of Listen state, need to inform upper layer of address match code event */
  3199. if((hfmpi2c->State & HAL_FMPI2C_STATE_LISTEN) == HAL_FMPI2C_STATE_LISTEN)
  3200. {
  3201. transferdirection = FMPI2C_GET_DIR(hfmpi2c);
  3202. slaveaddrcode = FMPI2C_GET_ADDR_MATCH(hfmpi2c);
  3203. ownadd1code = FMPI2C_GET_OWN_ADDRESS1(hfmpi2c);
  3204. ownadd2code = FMPI2C_GET_OWN_ADDRESS2(hfmpi2c);
  3205. /* If 10bits addressing mode is selected */
  3206. if(hfmpi2c->Init.AddressingMode == FMPI2C_ADDRESSINGMODE_10BIT)
  3207. {
  3208. if((slaveaddrcode & SlaveAddr_MSK) == ((ownadd1code >> SlaveAddr_SHIFT) & SlaveAddr_MSK))
  3209. {
  3210. slaveaddrcode = ownadd1code;
  3211. hfmpi2c->AddrEventCount++;
  3212. if(hfmpi2c->AddrEventCount == 2U)
  3213. {
  3214. /* Reset Address Event counter */
  3215. hfmpi2c->AddrEventCount = 0U;
  3216. /* Clear ADDR flag */
  3217. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c,FMPI2C_FLAG_ADDR);
  3218. /* Process Unlocked */
  3219. __HAL_UNLOCK(hfmpi2c);
  3220. /* Call Slave Addr callback */
  3221. HAL_FMPI2C_AddrCallback(hfmpi2c, transferdirection, slaveaddrcode);
  3222. }
  3223. }
  3224. else
  3225. {
  3226. slaveaddrcode = ownadd2code;
  3227. /* Disable ADDR Interrupts */
  3228. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT);
  3229. /* Process Unlocked */
  3230. __HAL_UNLOCK(hfmpi2c);
  3231. /* Call Slave Addr callback */
  3232. HAL_FMPI2C_AddrCallback(hfmpi2c, transferdirection, slaveaddrcode);
  3233. }
  3234. }
  3235. /* else 7 bits addressing mode is selected */
  3236. else
  3237. {
  3238. /* Disable ADDR Interrupts */
  3239. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT);
  3240. /* Process Unlocked */
  3241. __HAL_UNLOCK(hfmpi2c);
  3242. /* Call Slave Addr callback */
  3243. HAL_FMPI2C_AddrCallback(hfmpi2c, transferdirection, slaveaddrcode);
  3244. }
  3245. }
  3246. /* Else clear address flag only */
  3247. else
  3248. {
  3249. /* Clear ADDR flag */
  3250. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_ADDR);
  3251. /* Process Unlocked */
  3252. __HAL_UNLOCK(hfmpi2c);
  3253. }
  3254. }
  3255. /**
  3256. * @brief FMPI2C Master sequential complete process.
  3257. * @param hfmpi2c FMPI2C handle.
  3258. * @retval None
  3259. */
  3260. static void FMPI2C_ITMasterSequentialCplt(FMPI2C_HandleTypeDef *hfmpi2c)
  3261. {
  3262. /* Reset FMPI2C handle mode */
  3263. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3264. /* No Generate Stop, to permit restart mode */
  3265. /* The stop will be done at the end of transfer, when FMPI2C_AUTOEND_MODE enable */
  3266. if (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_TX)
  3267. {
  3268. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3269. hfmpi2c->PreviousState = FMPI2C_STATE_MASTER_BUSY_TX;
  3270. hfmpi2c->XferISR = NULL;
  3271. /* Disable Interrupts */
  3272. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT);
  3273. /* Process Unlocked */
  3274. __HAL_UNLOCK(hfmpi2c);
  3275. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3276. HAL_FMPI2C_MasterTxCpltCallback(hfmpi2c);
  3277. }
  3278. /* hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_RX */
  3279. else
  3280. {
  3281. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3282. hfmpi2c->PreviousState = FMPI2C_STATE_MASTER_BUSY_RX;
  3283. hfmpi2c->XferISR = NULL;
  3284. /* Disable Interrupts */
  3285. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT);
  3286. /* Process Unlocked */
  3287. __HAL_UNLOCK(hfmpi2c);
  3288. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3289. HAL_FMPI2C_MasterRxCpltCallback(hfmpi2c);
  3290. }
  3291. }
  3292. /**
  3293. * @brief FMPI2C Slave sequential complete process.
  3294. * @param hfmpi2c FMPI2C handle.
  3295. * @retval None
  3296. */
  3297. static void FMPI2C_ITSlaveSequentialCplt(FMPI2C_HandleTypeDef *hfmpi2c)
  3298. {
  3299. /* Reset FMPI2C handle mode */
  3300. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3301. if(hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_TX_LISTEN)
  3302. {
  3303. /* Remove HAL_FMPI2C_STATE_SLAVE_BUSY_TX, keep only HAL_FMPI2C_STATE_LISTEN */
  3304. hfmpi2c->State = HAL_FMPI2C_STATE_LISTEN;
  3305. hfmpi2c->PreviousState = FMPI2C_STATE_SLAVE_BUSY_TX;
  3306. /* Disable Interrupts */
  3307. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT);
  3308. /* Process Unlocked */
  3309. __HAL_UNLOCK(hfmpi2c);
  3310. /* Call the Tx complete callback to inform upper layer of the end of transmit process */
  3311. HAL_FMPI2C_SlaveTxCpltCallback(hfmpi2c);
  3312. }
  3313. else if(hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_RX_LISTEN)
  3314. {
  3315. /* Remove HAL_FMPI2C_STATE_SLAVE_BUSY_RX, keep only HAL_FMPI2C_STATE_LISTEN */
  3316. hfmpi2c->State = HAL_FMPI2C_STATE_LISTEN;
  3317. hfmpi2c->PreviousState = FMPI2C_STATE_SLAVE_BUSY_RX;
  3318. /* Disable Interrupts */
  3319. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT);
  3320. /* Process Unlocked */
  3321. __HAL_UNLOCK(hfmpi2c);
  3322. /* Call the Rx complete callback to inform upper layer of the end of receive process */
  3323. HAL_FMPI2C_SlaveRxCpltCallback(hfmpi2c);
  3324. }
  3325. }
  3326. /**
  3327. * @brief FMPI2C Master complete process.
  3328. * @param hfmpi2c FMPI2C handle.
  3329. * @param ITFlags Interrupt flags to handle.
  3330. * @retval None
  3331. */
  3332. static void FMPI2C_ITMasterCplt(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags)
  3333. {
  3334. /* Clear STOP Flag */
  3335. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  3336. /* Clear Configuration Register 2 */
  3337. FMPI2C_RESET_CR2(hfmpi2c);
  3338. /* Reset handle parameters */
  3339. hfmpi2c->PreviousState = FMPI2C_STATE_NONE;
  3340. hfmpi2c->XferISR = NULL;
  3341. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  3342. if((ITFlags & FMPI2C_FLAG_AF) != RESET)
  3343. {
  3344. /* Clear NACK Flag */
  3345. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  3346. /* Set acknowledge error code */
  3347. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  3348. }
  3349. /* Flush TX register */
  3350. FMPI2C_Flush_TXDR(hfmpi2c);
  3351. /* Disable Interrupts */
  3352. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_TX_IT| FMPI2C_XFER_RX_IT);
  3353. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3354. if((hfmpi2c->ErrorCode != HAL_FMPI2C_ERROR_NONE) || (hfmpi2c->State == HAL_FMPI2C_STATE_ABORT))
  3355. {
  3356. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3357. FMPI2C_ITError(hfmpi2c, hfmpi2c->ErrorCode);
  3358. }
  3359. /* hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_TX */
  3360. else if(hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_TX)
  3361. {
  3362. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3363. if (hfmpi2c->Mode == HAL_FMPI2C_MODE_MEM)
  3364. {
  3365. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3366. /* Process Unlocked */
  3367. __HAL_UNLOCK(hfmpi2c);
  3368. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3369. HAL_FMPI2C_MemTxCpltCallback(hfmpi2c);
  3370. }
  3371. else
  3372. {
  3373. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3374. /* Process Unlocked */
  3375. __HAL_UNLOCK(hfmpi2c);
  3376. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3377. HAL_FMPI2C_MasterTxCpltCallback(hfmpi2c);
  3378. }
  3379. }
  3380. /* hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_RX */
  3381. else if(hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_RX)
  3382. {
  3383. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3384. if (hfmpi2c->Mode == HAL_FMPI2C_MODE_MEM)
  3385. {
  3386. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3387. /* Process Unlocked */
  3388. __HAL_UNLOCK(hfmpi2c);
  3389. HAL_FMPI2C_MemRxCpltCallback(hfmpi2c);
  3390. }
  3391. else
  3392. {
  3393. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3394. /* Process Unlocked */
  3395. __HAL_UNLOCK(hfmpi2c);
  3396. HAL_FMPI2C_MasterRxCpltCallback(hfmpi2c);
  3397. }
  3398. }
  3399. }
  3400. /**
  3401. * @brief FMPI2C Slave complete process.
  3402. * @param hfmpi2c FMPI2C handle.
  3403. * @param ITFlags Interrupt flags to handle.
  3404. * @retval None
  3405. */
  3406. static void FMPI2C_ITSlaveCplt(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags)
  3407. {
  3408. /* Clear STOP Flag */
  3409. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  3410. /* Clear ADDR flag */
  3411. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c,FMPI2C_FLAG_ADDR);
  3412. /* Disable all interrupts */
  3413. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT | FMPI2C_XFER_TX_IT | FMPI2C_XFER_RX_IT);
  3414. /* Disable Address Acknowledge */
  3415. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  3416. /* Clear Configuration Register 2 */
  3417. FMPI2C_RESET_CR2(hfmpi2c);
  3418. /* Flush TX register */
  3419. FMPI2C_Flush_TXDR(hfmpi2c);
  3420. /* If a DMA is ongoing, Update handle size context */
  3421. if(((hfmpi2c->Instance->CR1 & FMPI2C_CR1_TXDMAEN) == FMPI2C_CR1_TXDMAEN) ||
  3422. ((hfmpi2c->Instance->CR1 & FMPI2C_CR1_RXDMAEN) == FMPI2C_CR1_RXDMAEN))
  3423. {
  3424. hfmpi2c->XferCount = FMPI2C_GET_DMA_REMAIN_DATA(hfmpi2c);
  3425. }
  3426. /* All data are not transferred, so set error code accordingly */
  3427. if(hfmpi2c->XferCount != 0U)
  3428. {
  3429. /* Set ErrorCode corresponding to a Non-Acknowledge */
  3430. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  3431. }
  3432. /* Store Last receive data if any */
  3433. if(((ITFlags & FMPI2C_FLAG_RXNE) != RESET))
  3434. {
  3435. /* Read data from RXDR */
  3436. (*hfmpi2c->pBuffPtr++) = hfmpi2c->Instance->RXDR;
  3437. if((hfmpi2c->XferSize > 0U))
  3438. {
  3439. hfmpi2c->XferSize--;
  3440. hfmpi2c->XferCount--;
  3441. /* Set ErrorCode corresponding to a Non-Acknowledge */
  3442. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  3443. }
  3444. }
  3445. hfmpi2c->PreviousState = FMPI2C_STATE_NONE;
  3446. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3447. hfmpi2c->XferISR = NULL;
  3448. if(hfmpi2c->ErrorCode != HAL_FMPI2C_ERROR_NONE)
  3449. {
  3450. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3451. FMPI2C_ITError(hfmpi2c, hfmpi2c->ErrorCode);
  3452. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  3453. if(hfmpi2c->State == HAL_FMPI2C_STATE_LISTEN)
  3454. {
  3455. /* Call FMPI2C Listen complete process */
  3456. FMPI2C_ITListenCplt(hfmpi2c, ITFlags);
  3457. }
  3458. }
  3459. else if(hfmpi2c->XferOptions != FMPI2C_NO_OPTION_FRAME)
  3460. {
  3461. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  3462. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3463. /* Process Unlocked */
  3464. __HAL_UNLOCK(hfmpi2c);
  3465. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  3466. HAL_FMPI2C_ListenCpltCallback(hfmpi2c);
  3467. }
  3468. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3469. else if(hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_RX)
  3470. {
  3471. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3472. /* Process Unlocked */
  3473. __HAL_UNLOCK(hfmpi2c);
  3474. /* Call the Slave Rx Complete callback */
  3475. HAL_FMPI2C_SlaveRxCpltCallback(hfmpi2c);
  3476. }
  3477. else
  3478. {
  3479. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3480. /* Process Unlocked */
  3481. __HAL_UNLOCK(hfmpi2c);
  3482. /* Call the Slave Tx Complete callback */
  3483. HAL_FMPI2C_SlaveTxCpltCallback(hfmpi2c);
  3484. }
  3485. }
  3486. /**
  3487. * @brief FMPI2C Listen complete process.
  3488. * @param hfmpi2c FMPI2C handle.
  3489. * @param ITFlags Interrupt flags to handle.
  3490. * @retval None
  3491. */
  3492. static void FMPI2C_ITListenCplt(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ITFlags)
  3493. {
  3494. /* Reset handle parameters */
  3495. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  3496. hfmpi2c->PreviousState = FMPI2C_STATE_NONE;
  3497. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3498. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3499. hfmpi2c->XferISR = NULL;
  3500. /* Store Last receive data if any */
  3501. if(((ITFlags & FMPI2C_FLAG_RXNE) != RESET))
  3502. {
  3503. /* Read data from RXDR */
  3504. (*hfmpi2c->pBuffPtr++) = hfmpi2c->Instance->RXDR;
  3505. if((hfmpi2c->XferSize > 0U))
  3506. {
  3507. hfmpi2c->XferSize--;
  3508. hfmpi2c->XferCount--;
  3509. /* Set ErrorCode corresponding to a Non-Acknowledge */
  3510. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_AF;
  3511. }
  3512. }
  3513. /* Disable all Interrupts*/
  3514. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT | FMPI2C_XFER_RX_IT | FMPI2C_XFER_TX_IT);
  3515. /* Clear NACK Flag */
  3516. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  3517. /* Process Unlocked */
  3518. __HAL_UNLOCK(hfmpi2c);
  3519. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  3520. HAL_FMPI2C_ListenCpltCallback(hfmpi2c);
  3521. }
  3522. /**
  3523. * @brief FMPI2C interrupts error process.
  3524. * @param hfmpi2c FMPI2C handle.
  3525. * @param ErrorCode Error code to handle.
  3526. * @retval None
  3527. */
  3528. static void FMPI2C_ITError(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t ErrorCode)
  3529. {
  3530. /* Reset handle parameters */
  3531. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3532. hfmpi2c->XferOptions = FMPI2C_NO_OPTION_FRAME;
  3533. hfmpi2c->XferCount = 0U;
  3534. /* Set new error code */
  3535. hfmpi2c->ErrorCode |= ErrorCode;
  3536. /* Disable Interrupts */
  3537. if((hfmpi2c->State == HAL_FMPI2C_STATE_LISTEN) ||
  3538. (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_TX_LISTEN) ||
  3539. (hfmpi2c->State == HAL_FMPI2C_STATE_BUSY_RX_LISTEN))
  3540. {
  3541. /* Disable all interrupts, except interrupts related to LISTEN state */
  3542. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_RX_IT | FMPI2C_XFER_TX_IT);
  3543. /* keep HAL_FMPI2C_STATE_LISTEN if set */
  3544. hfmpi2c->State = HAL_FMPI2C_STATE_LISTEN;
  3545. hfmpi2c->PreviousState = FMPI2C_STATE_NONE;
  3546. hfmpi2c->XferISR = FMPI2C_Slave_ISR_IT;
  3547. }
  3548. else
  3549. {
  3550. /* Disable all interrupts */
  3551. FMPI2C_Disable_IRQ(hfmpi2c, FMPI2C_XFER_LISTEN_IT | FMPI2C_XFER_RX_IT | FMPI2C_XFER_TX_IT);
  3552. /* If state is an abort treatment on goind, don't change state */
  3553. /* This change will be do later */
  3554. if(hfmpi2c->State != HAL_FMPI2C_STATE_ABORT)
  3555. {
  3556. /* Set HAL_FMPI2C_STATE_READY */
  3557. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3558. }
  3559. hfmpi2c->PreviousState = FMPI2C_STATE_NONE;
  3560. hfmpi2c->XferISR = NULL;
  3561. }
  3562. /* Abort DMA TX transfer if any */
  3563. if((hfmpi2c->Instance->CR1 & FMPI2C_CR1_TXDMAEN) == FMPI2C_CR1_TXDMAEN)
  3564. {
  3565. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_TXDMAEN;
  3566. /* Set the FMPI2C DMA Abort callback :
  3567. will lead to call HAL_FMPI2C_ErrorCallback() at end of DMA abort procedure */
  3568. hfmpi2c->hdmatx->XferAbortCallback = FMPI2C_DMAAbort;
  3569. /* Process Unlocked */
  3570. __HAL_UNLOCK(hfmpi2c);
  3571. if(HAL_DMA_Abort_IT(hfmpi2c->hdmatx) != HAL_OK)
  3572. {
  3573. /* Call Directly XferAbortCallback function in case of error */
  3574. hfmpi2c->hdmatx->XferAbortCallback(hfmpi2c->hdmatx);
  3575. }
  3576. }
  3577. /* Abort DMA RX transfer if any */
  3578. else if((hfmpi2c->Instance->CR1 & FMPI2C_CR1_RXDMAEN) == FMPI2C_CR1_RXDMAEN)
  3579. {
  3580. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_RXDMAEN;
  3581. /* Set the FMPI2C DMA Abort callback :
  3582. will lead to call HAL_FMPI2C_ErrorCallback() at end of DMA abort procedure */
  3583. hfmpi2c->hdmarx->XferAbortCallback = FMPI2C_DMAAbort;
  3584. /* Process Unlocked */
  3585. __HAL_UNLOCK(hfmpi2c);
  3586. if(HAL_DMA_Abort_IT(hfmpi2c->hdmarx) != HAL_OK)
  3587. {
  3588. /* Call Directly XferAbortCallback function in case of error */
  3589. hfmpi2c->hdmarx->XferAbortCallback(hfmpi2c->hdmarx);
  3590. }
  3591. }
  3592. else if(hfmpi2c->State == HAL_FMPI2C_STATE_ABORT)
  3593. {
  3594. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3595. /* Process Unlocked */
  3596. __HAL_UNLOCK(hfmpi2c);
  3597. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3598. HAL_FMPI2C_AbortCpltCallback(hfmpi2c);
  3599. }
  3600. else
  3601. {
  3602. /* Process Unlocked */
  3603. __HAL_UNLOCK(hfmpi2c);
  3604. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3605. HAL_FMPI2C_ErrorCallback(hfmpi2c);
  3606. }
  3607. }
  3608. /**
  3609. * @brief FMPI2C Tx data register flush process.
  3610. * @param hfmpi2c FMPI2C handle.
  3611. * @retval None
  3612. */
  3613. static void FMPI2C_Flush_TXDR(FMPI2C_HandleTypeDef *hfmpi2c)
  3614. {
  3615. /* If a pending TXIS flag is set */
  3616. /* Write a dummy data in TXDR to clear it */
  3617. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_TXIS) != RESET)
  3618. {
  3619. hfmpi2c->Instance->TXDR = 0x00U;
  3620. }
  3621. /* Flush TX register if not empty */
  3622. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_TXE) == RESET)
  3623. {
  3624. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_TXE);
  3625. }
  3626. }
  3627. /**
  3628. * @brief DMA FMPI2C master transmit process complete callback.
  3629. * @param hdma DMA handle
  3630. * @retval None
  3631. */
  3632. static void FMPI2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma)
  3633. {
  3634. FMPI2C_HandleTypeDef* hfmpi2c = (FMPI2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  3635. /* Disable DMA Request */
  3636. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_TXDMAEN;
  3637. /* If last transfer, enable STOP interrupt */
  3638. if(hfmpi2c->XferCount == 0U)
  3639. {
  3640. /* Enable STOP interrupt */
  3641. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_CPLT_IT);
  3642. }
  3643. /* else prepare a new DMA transfer and enable TCReload interrupt */
  3644. else
  3645. {
  3646. /* Update Buffer pointer */
  3647. hfmpi2c->pBuffPtr += hfmpi2c->XferSize;
  3648. /* Set the XferSize to transfer */
  3649. if(hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  3650. {
  3651. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  3652. }
  3653. else
  3654. {
  3655. hfmpi2c->XferSize = hfmpi2c->XferCount;
  3656. }
  3657. /* Enable the DMA channel */
  3658. HAL_DMA_Start_IT(hfmpi2c->hdmatx, (uint32_t)hfmpi2c->pBuffPtr, (uint32_t)&hfmpi2c->Instance->TXDR, hfmpi2c->XferSize);
  3659. /* Enable TC interrupts */
  3660. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_RELOAD_IT);
  3661. }
  3662. }
  3663. /**
  3664. * @brief DMA FMPI2C slave transmit process complete callback.
  3665. * @param hdma DMA handle
  3666. * @retval None
  3667. */
  3668. static void FMPI2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma)
  3669. {
  3670. /* No specific action, Master fully manage the generation of STOP condition */
  3671. /* Mean that this generation can arrive at any time, at the end or during DMA process */
  3672. /* So STOP condition should be manage through Interrupt treatment */
  3673. }
  3674. /**
  3675. * @brief DMA FMPI2C master receive process complete callback.
  3676. * @param hdma DMA handle
  3677. * @retval None
  3678. */
  3679. static void FMPI2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma)
  3680. {
  3681. FMPI2C_HandleTypeDef* hfmpi2c = (FMPI2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  3682. /* Disable DMA Request */
  3683. hfmpi2c->Instance->CR1 &= ~FMPI2C_CR1_RXDMAEN;
  3684. /* If last transfer, enable STOP interrupt */
  3685. if(hfmpi2c->XferCount == 0U)
  3686. {
  3687. /* Enable STOP interrupt */
  3688. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_CPLT_IT);
  3689. }
  3690. /* else prepare a new DMA transfer and enable TCReload interrupt */
  3691. else
  3692. {
  3693. /* Update Buffer pointer */
  3694. hfmpi2c->pBuffPtr += hfmpi2c->XferSize;
  3695. /* Set the XferSize to transfer */
  3696. if(hfmpi2c->XferCount > MAX_NBYTE_SIZE)
  3697. {
  3698. hfmpi2c->XferSize = MAX_NBYTE_SIZE;
  3699. }
  3700. else
  3701. {
  3702. hfmpi2c->XferSize = hfmpi2c->XferCount;
  3703. }
  3704. /* Enable the DMA channel */
  3705. HAL_DMA_Start_IT(hfmpi2c->hdmarx, (uint32_t)&hfmpi2c->Instance->RXDR, (uint32_t)hfmpi2c->pBuffPtr, hfmpi2c->XferSize);
  3706. /* Enable TC interrupts */
  3707. FMPI2C_Enable_IRQ(hfmpi2c, FMPI2C_XFER_RELOAD_IT);
  3708. }
  3709. }
  3710. /**
  3711. * @brief DMA FMPI2C slave receive process complete callback.
  3712. * @param hdma DMA handle
  3713. * @retval None
  3714. */
  3715. static void FMPI2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma)
  3716. {
  3717. /* No specific action, Master fully manage the generation of STOP condition */
  3718. /* Mean that this generation can arrive at any time, at the end or during DMA process */
  3719. /* So STOP condition should be manage through Interrupt treatment */
  3720. }
  3721. /**
  3722. * @brief DMA FMPI2C communication error callback.
  3723. * @param hdma DMA handle
  3724. * @retval None
  3725. */
  3726. static void FMPI2C_DMAError(DMA_HandleTypeDef *hdma)
  3727. {
  3728. FMPI2C_HandleTypeDef* hfmpi2c = ( FMPI2C_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  3729. /* Disable Acknowledge */
  3730. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  3731. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3732. FMPI2C_ITError(hfmpi2c, HAL_FMPI2C_ERROR_DMA);
  3733. }
  3734. /**
  3735. * @brief DMA FMPI2C communication abort callback
  3736. * (To be called at end of DMA Abort procedure).
  3737. * @param hdma: DMA handle.
  3738. * @retval None
  3739. */
  3740. static void FMPI2C_DMAAbort(DMA_HandleTypeDef *hdma)
  3741. {
  3742. FMPI2C_HandleTypeDef* hfmpi2c = ( FMPI2C_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  3743. /* Disable Acknowledge */
  3744. hfmpi2c->Instance->CR2 |= FMPI2C_CR2_NACK;
  3745. /* Reset AbortCpltCallback */
  3746. hfmpi2c->hdmatx->XferAbortCallback = NULL;
  3747. hfmpi2c->hdmarx->XferAbortCallback = NULL;
  3748. /* Check if come from abort from user */
  3749. if(hfmpi2c->State == HAL_FMPI2C_STATE_ABORT)
  3750. {
  3751. hfmpi2c->State = HAL_FMPI2C_STATE_READY;
  3752. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3753. HAL_FMPI2C_AbortCpltCallback(hfmpi2c);
  3754. }
  3755. else
  3756. {
  3757. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3758. HAL_FMPI2C_ErrorCallback(hfmpi2c);
  3759. }
  3760. }
  3761. /**
  3762. * @brief This function handles FMPI2C Communication Timeout.
  3763. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3764. * the configuration information for the specified FMPI2C.
  3765. * @param Flag Specifies the FMPI2C flag to check.
  3766. * @param Status The new Flag status (SET or RESET).
  3767. * @param Timeout Timeout duration
  3768. * @param Tickstart Tick start value
  3769. * @retval HAL status
  3770. */
  3771. static HAL_StatusTypeDef FMPI2C_WaitOnFlagUntilTimeout(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t Tickstart)
  3772. {
  3773. while(__HAL_FMPI2C_GET_FLAG(hfmpi2c, Flag) == Status)
  3774. {
  3775. /* Check for the Timeout */
  3776. if(Timeout != HAL_MAX_DELAY)
  3777. {
  3778. if((Timeout == 0U)||((HAL_GetTick() - Tickstart ) > Timeout))
  3779. {
  3780. hfmpi2c->State= HAL_FMPI2C_STATE_READY;
  3781. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3782. /* Process Unlocked */
  3783. __HAL_UNLOCK(hfmpi2c);
  3784. return HAL_TIMEOUT;
  3785. }
  3786. }
  3787. }
  3788. return HAL_OK;
  3789. }
  3790. /**
  3791. * @brief This function handles FMPI2C Communication Timeout for specific usage of TXIS flag.
  3792. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3793. * the configuration information for the specified FMPI2C.
  3794. * @param Timeout Timeout duration
  3795. * @param Tickstart Tick start value
  3796. * @retval HAL status
  3797. */
  3798. static HAL_StatusTypeDef FMPI2C_WaitOnTXISFlagUntilTimeout(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout, uint32_t Tickstart)
  3799. {
  3800. while(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_TXIS) == RESET)
  3801. {
  3802. /* Check if a NACK is detected */
  3803. if(FMPI2C_IsAcknowledgeFailed(hfmpi2c, Timeout, Tickstart) != HAL_OK)
  3804. {
  3805. return HAL_ERROR;
  3806. }
  3807. /* Check for the Timeout */
  3808. if(Timeout != HAL_MAX_DELAY)
  3809. {
  3810. if((Timeout == 0U)||((HAL_GetTick() - Tickstart) > Timeout))
  3811. {
  3812. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  3813. hfmpi2c->State= HAL_FMPI2C_STATE_READY;
  3814. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3815. /* Process Unlocked */
  3816. __HAL_UNLOCK(hfmpi2c);
  3817. return HAL_TIMEOUT;
  3818. }
  3819. }
  3820. }
  3821. return HAL_OK;
  3822. }
  3823. /**
  3824. * @brief This function handles FMPI2C Communication Timeout for specific usage of STOP flag.
  3825. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3826. * the configuration information for the specified FMPI2C.
  3827. * @param Timeout Timeout duration
  3828. * @param Tickstart Tick start value
  3829. * @retval HAL status
  3830. */
  3831. static HAL_StatusTypeDef FMPI2C_WaitOnSTOPFlagUntilTimeout(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout, uint32_t Tickstart)
  3832. {
  3833. while(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF) == RESET)
  3834. {
  3835. /* Check if a NACK is detected */
  3836. if(FMPI2C_IsAcknowledgeFailed(hfmpi2c, Timeout, Tickstart) != HAL_OK)
  3837. {
  3838. return HAL_ERROR;
  3839. }
  3840. /* Check for the Timeout */
  3841. if((Timeout == 0U)||((HAL_GetTick() - Tickstart) > Timeout))
  3842. {
  3843. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  3844. hfmpi2c->State= HAL_FMPI2C_STATE_READY;
  3845. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3846. /* Process Unlocked */
  3847. __HAL_UNLOCK(hfmpi2c);
  3848. return HAL_TIMEOUT;
  3849. }
  3850. }
  3851. return HAL_OK;
  3852. }
  3853. /**
  3854. * @brief This function handles FMPI2C Communication Timeout for specific usage of RXNE flag.
  3855. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3856. * the configuration information for the specified FMPI2C.
  3857. * @param Timeout Timeout duration
  3858. * @param Tickstart Tick start value
  3859. * @retval HAL status
  3860. */
  3861. static HAL_StatusTypeDef FMPI2C_WaitOnRXNEFlagUntilTimeout(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout, uint32_t Tickstart)
  3862. {
  3863. while(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_RXNE) == RESET)
  3864. {
  3865. /* Check if a NACK is detected */
  3866. if(FMPI2C_IsAcknowledgeFailed(hfmpi2c, Timeout, Tickstart) != HAL_OK)
  3867. {
  3868. return HAL_ERROR;
  3869. }
  3870. /* Check if a STOPF is detected */
  3871. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF) == SET)
  3872. {
  3873. /* Clear STOP Flag */
  3874. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  3875. /* Clear Configuration Register 2 */
  3876. FMPI2C_RESET_CR2(hfmpi2c);
  3877. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_NONE;
  3878. hfmpi2c->State= HAL_FMPI2C_STATE_READY;
  3879. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3880. /* Process Unlocked */
  3881. __HAL_UNLOCK(hfmpi2c);
  3882. return HAL_ERROR;
  3883. }
  3884. /* Check for the Timeout */
  3885. if((Timeout == 0U)||((HAL_GetTick() - Tickstart) > Timeout))
  3886. {
  3887. hfmpi2c->ErrorCode |= HAL_FMPI2C_ERROR_TIMEOUT;
  3888. hfmpi2c->State= HAL_FMPI2C_STATE_READY;
  3889. /* Process Unlocked */
  3890. __HAL_UNLOCK(hfmpi2c);
  3891. return HAL_TIMEOUT;
  3892. }
  3893. }
  3894. return HAL_OK;
  3895. }
  3896. /**
  3897. * @brief This function handles Acknowledge failed detection during an FMPI2C Communication.
  3898. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3899. * the configuration information for the specified FMPI2C.
  3900. * @param Timeout Timeout duration
  3901. * @param Tickstart Tick start value
  3902. * @retval HAL status
  3903. */
  3904. static HAL_StatusTypeDef FMPI2C_IsAcknowledgeFailed(FMPI2C_HandleTypeDef *hfmpi2c, uint32_t Timeout, uint32_t Tickstart)
  3905. {
  3906. if(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_AF) == SET)
  3907. {
  3908. /* Wait until STOP Flag is reset */
  3909. /* AutoEnd should be initiate after AF */
  3910. while(__HAL_FMPI2C_GET_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF) == RESET)
  3911. {
  3912. /* Check for the Timeout */
  3913. if(Timeout != HAL_MAX_DELAY)
  3914. {
  3915. if((Timeout == 0U)||((HAL_GetTick() - Tickstart) > Timeout))
  3916. {
  3917. hfmpi2c->State= HAL_FMPI2C_STATE_READY;
  3918. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3919. /* Process Unlocked */
  3920. __HAL_UNLOCK(hfmpi2c);
  3921. return HAL_TIMEOUT;
  3922. }
  3923. }
  3924. }
  3925. /* Clear NACKF Flag */
  3926. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_AF);
  3927. /* Clear STOP Flag */
  3928. __HAL_FMPI2C_CLEAR_FLAG(hfmpi2c, FMPI2C_FLAG_STOPF);
  3929. /* Flush TX register */
  3930. FMPI2C_Flush_TXDR(hfmpi2c);
  3931. /* Clear Configuration Register 2 */
  3932. FMPI2C_RESET_CR2(hfmpi2c);
  3933. hfmpi2c->ErrorCode = HAL_FMPI2C_ERROR_AF;
  3934. hfmpi2c->State= HAL_FMPI2C_STATE_READY;
  3935. hfmpi2c->Mode = HAL_FMPI2C_MODE_NONE;
  3936. /* Process Unlocked */
  3937. __HAL_UNLOCK(hfmpi2c);
  3938. return HAL_ERROR;
  3939. }
  3940. return HAL_OK;
  3941. }
  3942. /**
  3943. * @brief Handles FMPI2Cx communication when starting transfer or during transfer (TC or TCR flag are set).
  3944. * @param hfmpi2c FMPI2C handle.
  3945. * @param DevAddress Specifies the slave address to be programmed.
  3946. * @param Size Specifies the number of bytes to be programmed.
  3947. * This parameter must be a value between 0 and 255.
  3948. * @param Mode New state of the FMPI2C START condition generation.
  3949. * This parameter can be one of the following values:
  3950. * @arg @ref FMPI2C_RELOAD_MODE Enable Reload mode .
  3951. * @arg @ref FMPI2C_AUTOEND_MODE Enable Automatic end mode.
  3952. * @arg @ref FMPI2C_SOFTEND_MODE Enable Software end mode.
  3953. * @param Request New state of the FMPI2C START condition generation.
  3954. * This parameter can be one of the following values:
  3955. * @arg @ref FMPI2C_NO_STARTSTOP Don't Generate stop and start condition.
  3956. * @arg @ref FMPI2C_GENERATE_STOP Generate stop condition (Size should be set to 0).
  3957. * @arg @ref FMPI2C_GENERATE_START_READ Generate Restart for read request.
  3958. * @arg @ref FMPI2C_GENERATE_START_WRITE Generate Restart for write request.
  3959. * @retval None
  3960. */
  3961. static void FMPI2C_TransferConfig(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t DevAddress, uint8_t Size, uint32_t Mode, uint32_t Request)
  3962. {
  3963. uint32_t tmpreg = 0U;
  3964. /* Check the parameters */
  3965. assert_param(IS_FMPI2C_ALL_INSTANCE(hfmpi2c->Instance));
  3966. assert_param(IS_TRANSFER_MODE(Mode));
  3967. assert_param(IS_TRANSFER_REQUEST(Request));
  3968. /* Get the CR2 register value */
  3969. tmpreg = hfmpi2c->Instance->CR2;
  3970. /* clear tmpreg specific bits */
  3971. tmpreg &= (uint32_t)~((uint32_t)(FMPI2C_CR2_SADD | FMPI2C_CR2_NBYTES | FMPI2C_CR2_RELOAD | FMPI2C_CR2_AUTOEND | FMPI2C_CR2_RD_WRN | FMPI2C_CR2_START | FMPI2C_CR2_STOP));
  3972. /* update tmpreg */
  3973. tmpreg |= (uint32_t)(((uint32_t)DevAddress & FMPI2C_CR2_SADD) | (((uint32_t)Size << 16U) & FMPI2C_CR2_NBYTES) | \
  3974. (uint32_t)Mode | (uint32_t)Request);
  3975. /* update CR2 register */
  3976. hfmpi2c->Instance->CR2 = tmpreg;
  3977. }
  3978. /**
  3979. * @brief Manage the enabling of Interrupts.
  3980. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  3981. * the configuration information for the specified FMPI2C.
  3982. * @param InterruptRequest Value of @ref FMPI2C_Interrupt_configuration_definition.
  3983. * @retval HAL status
  3984. */
  3985. static HAL_StatusTypeDef FMPI2C_Enable_IRQ(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t InterruptRequest)
  3986. {
  3987. uint32_t tmpisr = 0U;
  3988. if((hfmpi2c->XferISR == FMPI2C_Master_ISR_DMA) || \
  3989. (hfmpi2c->XferISR == FMPI2C_Slave_ISR_DMA))
  3990. {
  3991. if((InterruptRequest & FMPI2C_XFER_LISTEN_IT) == FMPI2C_XFER_LISTEN_IT)
  3992. {
  3993. /* Enable ERR, STOP, NACK and ADDR interrupts */
  3994. tmpisr |= FMPI2C_IT_ADDRI | FMPI2C_IT_STOPI | FMPI2C_IT_NACKI | FMPI2C_IT_ERRI;
  3995. }
  3996. if((InterruptRequest & FMPI2C_XFER_ERROR_IT) == FMPI2C_XFER_ERROR_IT)
  3997. {
  3998. /* Enable ERR and NACK interrupts */
  3999. tmpisr |= FMPI2C_IT_ERRI | FMPI2C_IT_NACKI;
  4000. }
  4001. if((InterruptRequest & FMPI2C_XFER_CPLT_IT) == FMPI2C_XFER_CPLT_IT)
  4002. {
  4003. /* Enable STOP interrupts */
  4004. tmpisr |= FMPI2C_IT_STOPI;
  4005. }
  4006. if((InterruptRequest & FMPI2C_XFER_RELOAD_IT) == FMPI2C_XFER_RELOAD_IT)
  4007. {
  4008. /* Enable TC interrupts */
  4009. tmpisr |= FMPI2C_IT_TCI;
  4010. }
  4011. }
  4012. else
  4013. {
  4014. if((InterruptRequest & FMPI2C_XFER_LISTEN_IT) == FMPI2C_XFER_LISTEN_IT)
  4015. {
  4016. /* Enable ERR, STOP, NACK, and ADDR interrupts */
  4017. tmpisr |= FMPI2C_IT_ADDRI | FMPI2C_IT_STOPI | FMPI2C_IT_NACKI | FMPI2C_IT_ERRI;
  4018. }
  4019. if((InterruptRequest & FMPI2C_XFER_TX_IT) == FMPI2C_XFER_TX_IT)
  4020. {
  4021. /* Enable ERR, TC, STOP, NACK and RXI interrupts */
  4022. tmpisr |= FMPI2C_IT_ERRI | FMPI2C_IT_TCI | FMPI2C_IT_STOPI | FMPI2C_IT_NACKI | FMPI2C_IT_TXI;
  4023. }
  4024. if((InterruptRequest & FMPI2C_XFER_RX_IT) == FMPI2C_XFER_RX_IT)
  4025. {
  4026. /* Enable ERR, TC, STOP, NACK and TXI interrupts */
  4027. tmpisr |= FMPI2C_IT_ERRI | FMPI2C_IT_TCI | FMPI2C_IT_STOPI | FMPI2C_IT_NACKI | FMPI2C_IT_RXI;
  4028. }
  4029. if((InterruptRequest & FMPI2C_XFER_CPLT_IT) == FMPI2C_XFER_CPLT_IT)
  4030. {
  4031. /* Enable STOP interrupts */
  4032. tmpisr |= FMPI2C_IT_STOPI;
  4033. }
  4034. }
  4035. /* Enable interrupts only at the end */
  4036. /* to avoid the risk of FMPI2C interrupt handle execution before */
  4037. /* all interrupts requested done */
  4038. __HAL_FMPI2C_ENABLE_IT(hfmpi2c, tmpisr);
  4039. return HAL_OK;
  4040. }
  4041. /**
  4042. * @brief Manage the disabling of Interrupts.
  4043. * @param hfmpi2c Pointer to a FMPI2C_HandleTypeDef structure that contains
  4044. * the configuration information for the specified FMPI2C.
  4045. * @param InterruptRequest Value of @ref FMPI2C_Interrupt_configuration_definition.
  4046. * @retval HAL status
  4047. */
  4048. static HAL_StatusTypeDef FMPI2C_Disable_IRQ(FMPI2C_HandleTypeDef *hfmpi2c, uint16_t InterruptRequest)
  4049. {
  4050. uint32_t tmpisr = 0U;
  4051. if((InterruptRequest & FMPI2C_XFER_TX_IT) == FMPI2C_XFER_TX_IT)
  4052. {
  4053. /* Disable TC and TXI interrupts */
  4054. tmpisr |= FMPI2C_IT_TCI | FMPI2C_IT_TXI;
  4055. if((hfmpi2c->State & HAL_FMPI2C_STATE_LISTEN) != HAL_FMPI2C_STATE_LISTEN)
  4056. {
  4057. /* Disable NACK and STOP interrupts */
  4058. tmpisr |= FMPI2C_IT_STOPI | FMPI2C_IT_NACKI | FMPI2C_IT_ERRI;
  4059. }
  4060. }
  4061. if((InterruptRequest & FMPI2C_XFER_RX_IT) == FMPI2C_XFER_RX_IT)
  4062. {
  4063. /* Disable TC and RXI interrupts */
  4064. tmpisr |= FMPI2C_IT_TCI | FMPI2C_IT_RXI;
  4065. if((hfmpi2c->State & HAL_FMPI2C_STATE_LISTEN) != HAL_FMPI2C_STATE_LISTEN)
  4066. {
  4067. /* Disable NACK and STOP interrupts */
  4068. tmpisr |= FMPI2C_IT_STOPI | FMPI2C_IT_NACKI | FMPI2C_IT_ERRI;
  4069. }
  4070. }
  4071. if((InterruptRequest & FMPI2C_XFER_LISTEN_IT) == FMPI2C_XFER_LISTEN_IT)
  4072. {
  4073. /* Disable ADDR, NACK and STOP interrupts */
  4074. tmpisr |= FMPI2C_IT_ADDRI | FMPI2C_IT_STOPI | FMPI2C_IT_NACKI | FMPI2C_IT_ERRI;
  4075. }
  4076. if((InterruptRequest & FMPI2C_XFER_ERROR_IT) == FMPI2C_XFER_ERROR_IT)
  4077. {
  4078. /* Enable ERR and NACK interrupts */
  4079. tmpisr |= FMPI2C_IT_ERRI | FMPI2C_IT_NACKI;
  4080. }
  4081. if((InterruptRequest & FMPI2C_XFER_CPLT_IT) == FMPI2C_XFER_CPLT_IT)
  4082. {
  4083. /* Enable STOP interrupts */
  4084. tmpisr |= FMPI2C_IT_STOPI;
  4085. }
  4086. if((InterruptRequest & FMPI2C_XFER_RELOAD_IT) == FMPI2C_XFER_RELOAD_IT)
  4087. {
  4088. /* Enable TC interrupts */
  4089. tmpisr |= FMPI2C_IT_TCI;
  4090. }
  4091. /* Disable interrupts only at the end */
  4092. /* to avoid a breaking situation like at "t" time */
  4093. /* all disable interrupts request are not done */
  4094. __HAL_FMPI2C_DISABLE_IT(hfmpi2c, tmpisr);
  4095. return HAL_OK;
  4096. }
  4097. /**
  4098. * @}
  4099. */
  4100. #endif /* STM32F410xx || STM32F446xx || STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx || STM32F413xx || STM32F423xx */
  4101. #endif /* HAL_FMPI2C_MODULE_ENABLED */
  4102. /**
  4103. * @}
  4104. */
  4105. /**
  4106. * @}
  4107. */
  4108. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/