mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
shaoziyang
Date:
Sat Sep 13 14:25:46 2014 +0000
Revision:
323:9e901b0a5aa1
Parent:
237:f3da66175598
test with CLOCK_SETUP = 0

Who changed what in which revision?

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