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