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