mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Mon Nov 03 10:30:07 2014 +0000
Revision:
381:5460fc57b6e4
Synchronized with git revision 02478cd1f27fc7b9643486472635eb515b2bca81

Full URL: https://github.com/mbedmicro/mbed/commit/02478cd1f27fc7b9643486472635eb515b2bca81/

Target: LPC1549 - Fix serial interrupt issues (issue report #616)

Who changed what in which revision?

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