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