mbed

Fork of mbed-dev by mbed official

Committer:
<>
Date:
Wed Jan 04 16:58:05 2017 +0000
Revision:
154:37f96f9d4de2
Parent:
153:fa9ff456f731
Child:
156:95d6b41a828b
This updates the lib to the mbed lib v133

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 153:fa9ff456f731 1 /* mbed Microcontroller Library
<> 153:fa9ff456f731 2 *******************************************************************************
<> 153:fa9ff456f731 3 * Copyright (c) 2015, STMicroelectronics
<> 153:fa9ff456f731 4 * All rights reserved.
<> 153:fa9ff456f731 5 *
<> 153:fa9ff456f731 6 * Redistribution and use in source and binary forms, with or without
<> 153:fa9ff456f731 7 * modification, are permitted provided that the following conditions are met:
<> 153:fa9ff456f731 8 *
<> 153:fa9ff456f731 9 * 1. Redistributions of source code must retain the above copyright notice,
<> 153:fa9ff456f731 10 * this list of conditions and the following disclaimer.
<> 153:fa9ff456f731 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
<> 153:fa9ff456f731 12 * this list of conditions and the following disclaimer in the documentation
<> 153:fa9ff456f731 13 * and/or other materials provided with the distribution.
<> 153:fa9ff456f731 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
<> 153:fa9ff456f731 15 * may be used to endorse or promote products derived from this software
<> 153:fa9ff456f731 16 * without specific prior written permission.
<> 153:fa9ff456f731 17 *
<> 153:fa9ff456f731 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
<> 153:fa9ff456f731 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
<> 153:fa9ff456f731 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
<> 153:fa9ff456f731 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
<> 153:fa9ff456f731 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
<> 153:fa9ff456f731 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
<> 153:fa9ff456f731 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
<> 153:fa9ff456f731 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
<> 153:fa9ff456f731 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
<> 153:fa9ff456f731 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<> 153:fa9ff456f731 28 *******************************************************************************
<> 153:fa9ff456f731 29 */
<> 153:fa9ff456f731 30
<> 153:fa9ff456f731 31
<> 153:fa9ff456f731 32 #include "mbed_assert.h"
<> 153:fa9ff456f731 33 #include "i2c_api.h"
<> 153:fa9ff456f731 34 #include "platform/wait_api.h"
<> 153:fa9ff456f731 35
<> 153:fa9ff456f731 36 #if DEVICE_I2C
<> 153:fa9ff456f731 37
<> 153:fa9ff456f731 38 #include "cmsis.h"
<> 153:fa9ff456f731 39 #include "pinmap.h"
<> 153:fa9ff456f731 40 #include "PeripheralPins.h"
<> 153:fa9ff456f731 41 #include "i2c_device.h" // family specific defines
<> 153:fa9ff456f731 42
<> 153:fa9ff456f731 43 #ifndef DEBUG_STDIO
<> 153:fa9ff456f731 44 # define DEBUG_STDIO 0
<> 153:fa9ff456f731 45 #endif
<> 153:fa9ff456f731 46
<> 153:fa9ff456f731 47 #if DEBUG_STDIO
<> 153:fa9ff456f731 48 # include <stdio.h>
<> 153:fa9ff456f731 49 # define DEBUG_PRINTF(...) do { printf(__VA_ARGS__); } while(0)
<> 153:fa9ff456f731 50 #else
<> 153:fa9ff456f731 51 # define DEBUG_PRINTF(...) {}
<> 153:fa9ff456f731 52 #endif
<> 153:fa9ff456f731 53
<> 153:fa9ff456f731 54 #if DEVICE_I2C_ASYNCH
<> 153:fa9ff456f731 55 #define I2C_S(obj) (struct i2c_s *) (&((obj)->i2c))
<> 153:fa9ff456f731 56 #else
<> 153:fa9ff456f731 57 #define I2C_S(obj) (struct i2c_s *) (obj)
<> 153:fa9ff456f731 58 #endif
<> 153:fa9ff456f731 59
<> 153:fa9ff456f731 60 /* Family specific description for I2C */
<> 153:fa9ff456f731 61 #define I2C_NUM (5)
<> 153:fa9ff456f731 62 static I2C_HandleTypeDef* i2c_handles[I2C_NUM];
<> 153:fa9ff456f731 63
<> 153:fa9ff456f731 64 /* Timeout values are based on core clock and I2C clock.
<> 153:fa9ff456f731 65 The BYTE_TIMEOUT is computed as twice the number of cycles it would
<> 153:fa9ff456f731 66 take to send 10 bits over I2C. Most Flags should take less than that.
<> 153:fa9ff456f731 67 This is for immediate FLAG or ACK check.
<> 153:fa9ff456f731 68 */
<> 153:fa9ff456f731 69 #define BYTE_TIMEOUT ((SystemCoreClock / obj_s->hz) * 2 * 10)
<> 153:fa9ff456f731 70 /* Timeout values based on I2C clock.
<> 153:fa9ff456f731 71 The BYTE_TIMEOUT_US is computed as 3x the time in us it would
<> 153:fa9ff456f731 72 take to send 10 bits over I2C. Most Flags should take less than that.
<> 153:fa9ff456f731 73 This is for complete transfers check.
<> 153:fa9ff456f731 74 */
<> 153:fa9ff456f731 75 #define BYTE_TIMEOUT_US ((SystemCoreClock / obj_s->hz) * 3 * 10)
<> 153:fa9ff456f731 76 /* Timeout values for flags and events waiting loops. These timeouts are
<> 153:fa9ff456f731 77 not based on accurate values, they just guarantee that the application will
<> 153:fa9ff456f731 78 not remain stuck if the I2C communication is corrupted.
<> 153:fa9ff456f731 79 */
<> 153:fa9ff456f731 80 #define FLAG_TIMEOUT ((int)0x1000)
<> 153:fa9ff456f731 81
<> 153:fa9ff456f731 82 /* GENERIC INIT and HELPERS FUNCTIONS */
<> 153:fa9ff456f731 83
<> 153:fa9ff456f731 84 #if defined(I2C1_BASE)
<> 153:fa9ff456f731 85 static void i2c1_irq(void)
<> 153:fa9ff456f731 86 {
<> 153:fa9ff456f731 87 I2C_HandleTypeDef * handle = i2c_handles[0];
<> 153:fa9ff456f731 88 HAL_I2C_EV_IRQHandler(handle);
<> 153:fa9ff456f731 89 HAL_I2C_ER_IRQHandler(handle);
<> 153:fa9ff456f731 90 }
<> 153:fa9ff456f731 91 #endif
<> 153:fa9ff456f731 92 #if defined(I2C2_BASE)
<> 153:fa9ff456f731 93 static void i2c2_irq(void)
<> 153:fa9ff456f731 94 {
<> 153:fa9ff456f731 95 I2C_HandleTypeDef * handle = i2c_handles[1];
<> 153:fa9ff456f731 96 HAL_I2C_EV_IRQHandler(handle);
<> 153:fa9ff456f731 97 HAL_I2C_ER_IRQHandler(handle);
<> 153:fa9ff456f731 98 }
<> 153:fa9ff456f731 99 #endif
<> 153:fa9ff456f731 100 #if defined(I2C3_BASE)
<> 153:fa9ff456f731 101 static void i2c3_irq(void)
<> 153:fa9ff456f731 102 {
<> 153:fa9ff456f731 103 I2C_HandleTypeDef * handle = i2c_handles[2];
<> 153:fa9ff456f731 104 HAL_I2C_EV_IRQHandler(handle);
<> 153:fa9ff456f731 105 HAL_I2C_ER_IRQHandler(handle);
<> 153:fa9ff456f731 106 }
<> 153:fa9ff456f731 107 #endif
<> 153:fa9ff456f731 108 #if defined(I2C4_BASE)
<> 153:fa9ff456f731 109 static void i2c4_irq(void)
<> 153:fa9ff456f731 110 {
<> 153:fa9ff456f731 111 I2C_HandleTypeDef * handle = i2c_handles[3];
<> 153:fa9ff456f731 112 HAL_I2C_EV_IRQHandler(handle);
<> 153:fa9ff456f731 113 HAL_I2C_ER_IRQHandler(handle);
<> 153:fa9ff456f731 114 }
<> 153:fa9ff456f731 115 #endif
<> 153:fa9ff456f731 116 #if defined(FMPI2C1_BASE)
<> 153:fa9ff456f731 117 static void i2c5_irq(void)
<> 153:fa9ff456f731 118 {
<> 153:fa9ff456f731 119 I2C_HandleTypeDef * handle = i2c_handles[4];
<> 153:fa9ff456f731 120 HAL_I2C_EV_IRQHandler(handle);
<> 153:fa9ff456f731 121 HAL_I2C_ER_IRQHandler(handle);
<> 153:fa9ff456f731 122 }
<> 153:fa9ff456f731 123 #endif
<> 153:fa9ff456f731 124
<> 153:fa9ff456f731 125 void i2c_ev_err_enable(i2c_t *obj, uint32_t handler) {
<> 153:fa9ff456f731 126 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 127 IRQn_Type irq_event_n = obj_s->event_i2cIRQ;
<> 153:fa9ff456f731 128 IRQn_Type irq_error_n = obj_s->error_i2cIRQ;
<> 153:fa9ff456f731 129 /* default prio in master case is set to 2 */
<> 153:fa9ff456f731 130 uint32_t prio = 2;
<> 153:fa9ff456f731 131
<> 153:fa9ff456f731 132 /* Set up ITs using IRQ and handler tables */
<> 153:fa9ff456f731 133 NVIC_SetVector(irq_event_n, handler);
<> 153:fa9ff456f731 134 NVIC_SetVector(irq_error_n, handler);
<> 153:fa9ff456f731 135
<> 153:fa9ff456f731 136 #if DEVICE_I2CSLAVE
<> 153:fa9ff456f731 137 /* Set higher priority to slave device than master.
<> 153:fa9ff456f731 138 * In case a device makes use of both master and slave, the
<> 153:fa9ff456f731 139 * slave needs higher responsiveness.
<> 153:fa9ff456f731 140 */
<> 153:fa9ff456f731 141 if (obj_s->slave) {
<> 153:fa9ff456f731 142 prio = 1;
<> 153:fa9ff456f731 143 }
<> 153:fa9ff456f731 144 #endif
<> 153:fa9ff456f731 145
<> 153:fa9ff456f731 146 NVIC_SetPriority(irq_event_n, prio);
<> 153:fa9ff456f731 147 NVIC_SetPriority(irq_error_n, prio);
<> 153:fa9ff456f731 148 NVIC_EnableIRQ(irq_event_n);
<> 153:fa9ff456f731 149 NVIC_EnableIRQ(irq_error_n);
<> 153:fa9ff456f731 150 }
<> 153:fa9ff456f731 151
<> 153:fa9ff456f731 152 void i2c_ev_err_disable(i2c_t *obj) {
<> 153:fa9ff456f731 153 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 154 IRQn_Type irq_event_n = obj_s->event_i2cIRQ;
<> 153:fa9ff456f731 155 IRQn_Type irq_error_n = obj_s->error_i2cIRQ;
<> 153:fa9ff456f731 156
<> 153:fa9ff456f731 157 HAL_NVIC_DisableIRQ(irq_event_n);
<> 153:fa9ff456f731 158 HAL_NVIC_DisableIRQ(irq_error_n);
<> 153:fa9ff456f731 159 }
<> 153:fa9ff456f731 160
<> 153:fa9ff456f731 161 uint32_t i2c_get_irq_handler(i2c_t *obj)
<> 153:fa9ff456f731 162 {
<> 153:fa9ff456f731 163 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 164 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 153:fa9ff456f731 165 uint32_t handler = 0;
<> 153:fa9ff456f731 166
<> 153:fa9ff456f731 167 switch (obj_s->index) {
<> 153:fa9ff456f731 168 #if defined(I2C1_BASE)
<> 153:fa9ff456f731 169 case 0:
<> 153:fa9ff456f731 170 handler = (uint32_t)&i2c1_irq;
<> 153:fa9ff456f731 171 break;
<> 153:fa9ff456f731 172 #endif
<> 153:fa9ff456f731 173 #if defined(I2C2_BASE)
<> 153:fa9ff456f731 174 case 1:
<> 153:fa9ff456f731 175 handler = (uint32_t)&i2c2_irq;
<> 153:fa9ff456f731 176 break;
<> 153:fa9ff456f731 177 #endif
<> 153:fa9ff456f731 178 #if defined(I2C3_BASE)
<> 153:fa9ff456f731 179 case 2:
<> 153:fa9ff456f731 180 handler = (uint32_t)&i2c3_irq;
<> 153:fa9ff456f731 181 break;
<> 153:fa9ff456f731 182 #endif
<> 153:fa9ff456f731 183 #if defined(I2C4_BASE)
<> 153:fa9ff456f731 184 case 3:
<> 153:fa9ff456f731 185 handler = (uint32_t)&i2c4_irq;
<> 153:fa9ff456f731 186 break;
<> 153:fa9ff456f731 187 #endif
<> 153:fa9ff456f731 188 #if defined(FMPI2C1_BASE)
<> 153:fa9ff456f731 189 case 4:
<> 153:fa9ff456f731 190 handler = (uint32_t)&i2c5_irq;
<> 153:fa9ff456f731 191 break;
<> 153:fa9ff456f731 192 #endif
<> 153:fa9ff456f731 193 }
<> 153:fa9ff456f731 194
<> 153:fa9ff456f731 195 i2c_handles[obj_s->index] = handle;
<> 153:fa9ff456f731 196 return handler;
<> 153:fa9ff456f731 197 }
<> 153:fa9ff456f731 198
<> 153:fa9ff456f731 199 void i2c_hw_reset(i2c_t *obj) {
<> 153:fa9ff456f731 200 int timeout;
<> 153:fa9ff456f731 201 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 202 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 153:fa9ff456f731 203
<> 153:fa9ff456f731 204 handle->Instance = (I2C_TypeDef *)(obj_s->i2c);
<> 153:fa9ff456f731 205
<> 153:fa9ff456f731 206 // wait before reset
<> 153:fa9ff456f731 207 timeout = BYTE_TIMEOUT;
<> 153:fa9ff456f731 208 while ((__HAL_I2C_GET_FLAG(handle, I2C_FLAG_BUSY)) && (--timeout != 0));
<> 153:fa9ff456f731 209 #if defined I2C1_BASE
<> 153:fa9ff456f731 210 if (obj_s->i2c == I2C_1) {
<> 153:fa9ff456f731 211 __HAL_RCC_I2C1_FORCE_RESET();
<> 153:fa9ff456f731 212 __HAL_RCC_I2C1_RELEASE_RESET();
<> 153:fa9ff456f731 213 }
<> 153:fa9ff456f731 214 #endif
<> 153:fa9ff456f731 215 #if defined I2C2_BASE
<> 153:fa9ff456f731 216 if (obj_s->i2c == I2C_2) {
<> 153:fa9ff456f731 217 __HAL_RCC_I2C2_FORCE_RESET();
<> 153:fa9ff456f731 218 __HAL_RCC_I2C2_RELEASE_RESET();
<> 153:fa9ff456f731 219 }
<> 153:fa9ff456f731 220 #endif
<> 153:fa9ff456f731 221 #if defined I2C3_BASE
<> 153:fa9ff456f731 222 if (obj_s->i2c == I2C_3) {
<> 153:fa9ff456f731 223 __HAL_RCC_I2C3_FORCE_RESET();
<> 153:fa9ff456f731 224 __HAL_RCC_I2C3_RELEASE_RESET();
<> 153:fa9ff456f731 225 }
<> 153:fa9ff456f731 226 #endif
<> 153:fa9ff456f731 227 #if defined I2C4_BASE
<> 153:fa9ff456f731 228 if (obj_s->i2c == I2C_4) {
<> 153:fa9ff456f731 229 __HAL_RCC_I2C4_FORCE_RESET();
<> 153:fa9ff456f731 230 __HAL_RCC_I2C4_RELEASE_RESET();
<> 153:fa9ff456f731 231 }
<> 153:fa9ff456f731 232 #endif
<> 153:fa9ff456f731 233 #if defined FMPI2C1_BASE
<> 153:fa9ff456f731 234 if (obj_s->i2c == FMPI2C_1) {
<> 153:fa9ff456f731 235 __HAL_RCC_FMPI2C1_FORCE_RESET();
<> 153:fa9ff456f731 236 __HAL_RCC_FMPI2C1_RELEASE_RESET();
<> 153:fa9ff456f731 237 }
<> 153:fa9ff456f731 238 #endif
<> 153:fa9ff456f731 239 }
<> 153:fa9ff456f731 240
<> 153:fa9ff456f731 241 void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
<> 153:fa9ff456f731 242
<> 153:fa9ff456f731 243 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 244
<> 153:fa9ff456f731 245 // Determine the I2C to use
<> 153:fa9ff456f731 246 I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
<> 153:fa9ff456f731 247 I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
<> 153:fa9ff456f731 248 obj_s->sda = sda;
<> 153:fa9ff456f731 249 obj_s->scl = scl;
<> 153:fa9ff456f731 250
<> 153:fa9ff456f731 251 obj_s->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
<> 153:fa9ff456f731 252 MBED_ASSERT(obj_s->i2c != (I2CName)NC);
<> 153:fa9ff456f731 253
<> 153:fa9ff456f731 254 #if defined I2C1_BASE
<> 153:fa9ff456f731 255 // Enable I2C1 clock and pinout if not done
<> 153:fa9ff456f731 256 if (obj_s->i2c == I2C_1) {
<> 153:fa9ff456f731 257 obj_s->index = 0;
<> 153:fa9ff456f731 258 __HAL_RCC_I2C1_CLK_ENABLE();
<> 153:fa9ff456f731 259 // Configure I2C pins
<> 153:fa9ff456f731 260 pinmap_pinout(sda, PinMap_I2C_SDA);
<> 153:fa9ff456f731 261 pinmap_pinout(scl, PinMap_I2C_SCL);
<> 153:fa9ff456f731 262 pin_mode(sda, PullUp);
<> 153:fa9ff456f731 263 pin_mode(scl, PullUp);
<> 153:fa9ff456f731 264 obj_s->event_i2cIRQ = I2C1_EV_IRQn;
<> 153:fa9ff456f731 265 obj_s->error_i2cIRQ = I2C1_ER_IRQn;
<> 153:fa9ff456f731 266 }
<> 153:fa9ff456f731 267 #endif
<> 153:fa9ff456f731 268 #if defined I2C2_BASE
<> 153:fa9ff456f731 269 // Enable I2C2 clock and pinout if not done
<> 153:fa9ff456f731 270 if (obj_s->i2c == I2C_2) {
<> 153:fa9ff456f731 271 obj_s->index = 1;
<> 153:fa9ff456f731 272 __HAL_RCC_I2C2_CLK_ENABLE();
<> 153:fa9ff456f731 273 // Configure I2C pins
<> 153:fa9ff456f731 274 pinmap_pinout(sda, PinMap_I2C_SDA);
<> 153:fa9ff456f731 275 pinmap_pinout(scl, PinMap_I2C_SCL);
<> 153:fa9ff456f731 276 pin_mode(sda, PullUp);
<> 153:fa9ff456f731 277 pin_mode(scl, PullUp);
<> 153:fa9ff456f731 278 obj_s->event_i2cIRQ = I2C2_EV_IRQn;
<> 153:fa9ff456f731 279 obj_s->error_i2cIRQ = I2C2_ER_IRQn;
<> 153:fa9ff456f731 280 }
<> 153:fa9ff456f731 281 #endif
<> 153:fa9ff456f731 282 #if defined I2C3_BASE
<> 153:fa9ff456f731 283 // Enable I2C3 clock and pinout if not done
<> 153:fa9ff456f731 284 if (obj_s->i2c == I2C_3) {
<> 153:fa9ff456f731 285 obj_s->index = 2;
<> 153:fa9ff456f731 286 __HAL_RCC_I2C3_CLK_ENABLE();
<> 153:fa9ff456f731 287 // Configure I2C pins
<> 153:fa9ff456f731 288 pinmap_pinout(sda, PinMap_I2C_SDA);
<> 153:fa9ff456f731 289 pinmap_pinout(scl, PinMap_I2C_SCL);
<> 153:fa9ff456f731 290 pin_mode(sda, PullUp);
<> 153:fa9ff456f731 291 pin_mode(scl, PullUp);
<> 153:fa9ff456f731 292 obj_s->event_i2cIRQ = I2C3_EV_IRQn;
<> 153:fa9ff456f731 293 obj_s->error_i2cIRQ = I2C3_ER_IRQn;
<> 153:fa9ff456f731 294 }
<> 153:fa9ff456f731 295 #endif
<> 153:fa9ff456f731 296 #if defined I2C4_BASE
<> 153:fa9ff456f731 297 // Enable I2C3 clock and pinout if not done
<> 153:fa9ff456f731 298 if (obj_s->i2c == I2C_4) {
<> 153:fa9ff456f731 299 obj_s->index = 3;
<> 153:fa9ff456f731 300 __HAL_RCC_I2C4_CLK_ENABLE();
<> 153:fa9ff456f731 301 // Configure I2C pins
<> 153:fa9ff456f731 302 pinmap_pinout(sda, PinMap_I2C_SDA);
<> 153:fa9ff456f731 303 pinmap_pinout(scl, PinMap_I2C_SCL);
<> 153:fa9ff456f731 304 pin_mode(sda, PullUp);
<> 153:fa9ff456f731 305 pin_mode(scl, PullUp);
<> 153:fa9ff456f731 306 obj_s->event_i2cIRQ = I2C4_EV_IRQn;
<> 153:fa9ff456f731 307 obj_s->error_i2cIRQ = I2C4_ER_IRQn;
<> 153:fa9ff456f731 308 }
<> 153:fa9ff456f731 309 #endif
<> 153:fa9ff456f731 310 #if defined FMPI2C1_BASE
<> 153:fa9ff456f731 311 // Enable I2C3 clock and pinout if not done
<> 153:fa9ff456f731 312 if (obj_s->i2c == FMPI2C_1) {
<> 153:fa9ff456f731 313 obj_s->index = 4;
<> 153:fa9ff456f731 314 __HAL_RCC_FMPI2C1_CLK_ENABLE();
<> 153:fa9ff456f731 315 // Configure I2C pins
<> 153:fa9ff456f731 316 pinmap_pinout(sda, PinMap_I2C_SDA);
<> 153:fa9ff456f731 317 pinmap_pinout(scl, PinMap_I2C_SCL);
<> 153:fa9ff456f731 318 pin_mode(sda, PullUp);
<> 153:fa9ff456f731 319 pin_mode(scl, PullUp);
<> 153:fa9ff456f731 320 obj_s->event_i2cIRQ = FMPI2C1_EV_IRQn;
<> 153:fa9ff456f731 321 obj_s->error_i2cIRQ = FMPI2C1_ER_IRQn;
<> 153:fa9ff456f731 322 }
<> 153:fa9ff456f731 323 #endif
<> 153:fa9ff456f731 324
<> 153:fa9ff456f731 325 // I2C configuration
<> 153:fa9ff456f731 326 // Default hz value used for timeout computation
<> 153:fa9ff456f731 327 if(!obj_s->hz)
<> 153:fa9ff456f731 328 obj_s->hz = 100000; // 100 kHz per default
<> 153:fa9ff456f731 329
<> 153:fa9ff456f731 330 // Reset to clear pending flags if any
<> 153:fa9ff456f731 331 i2c_hw_reset(obj);
<> 153:fa9ff456f731 332 i2c_frequency(obj, obj_s->hz );
<> 153:fa9ff456f731 333
<> 153:fa9ff456f731 334 #if DEVICE_I2CSLAVE
<> 153:fa9ff456f731 335 // I2C master by default
<> 153:fa9ff456f731 336 obj_s->slave = 0;
<> 153:fa9ff456f731 337 obj_s->pending_slave_tx_master_rx = 0;
<> 153:fa9ff456f731 338 obj_s->pending_slave_rx_maxter_tx = 0;
<> 153:fa9ff456f731 339 #endif
<> 153:fa9ff456f731 340
<> 153:fa9ff456f731 341 // I2C Xfer operation init
<> 153:fa9ff456f731 342 obj_s->event = 0;
<> 153:fa9ff456f731 343 obj_s->XferOperation = I2C_FIRST_AND_LAST_FRAME;
<> 153:fa9ff456f731 344 }
<> 153:fa9ff456f731 345
<> 153:fa9ff456f731 346 void i2c_frequency(i2c_t *obj, int hz)
<> 153:fa9ff456f731 347 {
<> 153:fa9ff456f731 348 int timeout;
<> 153:fa9ff456f731 349 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 350 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 153:fa9ff456f731 351
<> 153:fa9ff456f731 352 // wait before init
<> 153:fa9ff456f731 353 timeout = BYTE_TIMEOUT;
<> 153:fa9ff456f731 354 while ((__HAL_I2C_GET_FLAG(handle, I2C_FLAG_BUSY)) && (--timeout != 0));
<> 153:fa9ff456f731 355
<> 153:fa9ff456f731 356 #ifdef I2C_IP_VERSION_V1
<> 153:fa9ff456f731 357 handle->Init.ClockSpeed = hz;
<> 153:fa9ff456f731 358 handle->Init.DutyCycle = I2C_DUTYCYCLE_2;
<> 153:fa9ff456f731 359 #endif
<> 153:fa9ff456f731 360 #ifdef I2C_IP_VERSION_V2
<> 153:fa9ff456f731 361 /* Only predefined timing for below frequencies are supported */
<> 153:fa9ff456f731 362 MBED_ASSERT((hz == 100000) || (hz == 400000) || (hz == 1000000));
<> 153:fa9ff456f731 363 handle->Init.Timing = get_i2c_timing(hz);
<> 153:fa9ff456f731 364
<> 153:fa9ff456f731 365 // Enable the Fast Mode Plus capability
<> 153:fa9ff456f731 366 if (hz == 1000000) {
<> 153:fa9ff456f731 367 #if defined(I2C1_BASE) && defined(__HAL_SYSCFG_FASTMODEPLUS_ENABLE) && defined (I2C_FASTMODEPLUS_I2C1)
<> 153:fa9ff456f731 368 if (obj_s->i2c == I2C_1) {
<> 153:fa9ff456f731 369 __HAL_SYSCFG_FASTMODEPLUS_ENABLE(I2C_FASTMODEPLUS_I2C1);
<> 153:fa9ff456f731 370 }
<> 153:fa9ff456f731 371 #endif
<> 153:fa9ff456f731 372 #if defined(I2C2_BASE) && defined(__HAL_SYSCFG_FASTMODEPLUS_ENABLE) && defined (I2C_FASTMODEPLUS_I2C2)
<> 153:fa9ff456f731 373 if (obj_s->i2c == I2C_2) {
<> 153:fa9ff456f731 374 __HAL_SYSCFG_FASTMODEPLUS_ENABLE(I2C_FASTMODEPLUS_I2C2);
<> 153:fa9ff456f731 375 }
<> 153:fa9ff456f731 376 #endif
<> 153:fa9ff456f731 377 #if defined(I2C3_BASE) && defined(__HAL_SYSCFG_FASTMODEPLUS_ENABLE) && defined (I2C_FASTMODEPLUS_I2C3)
<> 153:fa9ff456f731 378 if (obj_s->i2c == I2C_3) {
<> 153:fa9ff456f731 379 __HAL_SYSCFG_FASTMODEPLUS_ENABLE(I2C_FASTMODEPLUS_I2C3);
<> 153:fa9ff456f731 380 }
<> 153:fa9ff456f731 381 #endif
<> 153:fa9ff456f731 382 #if defined(I2C4_BASE) && defined(__HAL_SYSCFG_FASTMODEPLUS_ENABLE) && defined (I2C_FASTMODEPLUS_I2C4)
<> 153:fa9ff456f731 383 if (obj_s->i2c == I2C_4) {
<> 153:fa9ff456f731 384 __HAL_SYSCFG_FASTMODEPLUS_ENABLE(I2C_FASTMODEPLUS_I2C4);
<> 153:fa9ff456f731 385 }
<> 153:fa9ff456f731 386 #endif
<> 153:fa9ff456f731 387 }
<> 153:fa9ff456f731 388 #endif //I2C_IP_VERSION_V2
<> 153:fa9ff456f731 389
<> 153:fa9ff456f731 390 /*##-1- Configure the I2C clock source. The clock is derived from the SYSCLK #*/
<> 153:fa9ff456f731 391 #if defined(I2C1_BASE) && defined (__HAL_RCC_I2C1_CONFIG)
<> 153:fa9ff456f731 392 if (obj_s->i2c == I2C_1) {
<> 153:fa9ff456f731 393 __HAL_RCC_I2C1_CONFIG(I2CAPI_I2C1_CLKSRC);
<> 153:fa9ff456f731 394 }
<> 153:fa9ff456f731 395 #endif
<> 153:fa9ff456f731 396 #if defined(I2C2_BASE) && defined(__HAL_RCC_I2C2_CONFIG)
<> 153:fa9ff456f731 397 if (obj_s->i2c == I2C_2) {
<> 153:fa9ff456f731 398 __HAL_RCC_I2C2_CONFIG(I2CAPI_I2C2_CLKSRC);
<> 153:fa9ff456f731 399 }
<> 153:fa9ff456f731 400 #endif
<> 153:fa9ff456f731 401 #if defined(I2C3_BASE) && defined(__HAL_RCC_I2C3_CONFIG)
<> 153:fa9ff456f731 402 if (obj_s->i2c == I2C_3) {
<> 153:fa9ff456f731 403 __HAL_RCC_I2C3_CONFIG(I2CAPI_I2C3_CLKSRC);
<> 153:fa9ff456f731 404 }
<> 153:fa9ff456f731 405 #endif
<> 153:fa9ff456f731 406 #if defined(I2C4_BASE) && defined(__HAL_RCC_I2C4_CONFIG)
<> 153:fa9ff456f731 407 if (obj_s->i2c == I2C_4) {
<> 153:fa9ff456f731 408 __HAL_RCC_I2C4_CONFIG(I2CAPI_I2C4_CLKSRC);
<> 153:fa9ff456f731 409 }
<> 153:fa9ff456f731 410 #endif
<> 153:fa9ff456f731 411
<> 153:fa9ff456f731 412 #ifdef I2C_ANALOGFILTER_ENABLE
<> 153:fa9ff456f731 413 /* Enable the Analog I2C Filter */
<> 153:fa9ff456f731 414 HAL_I2CEx_AnalogFilter_Config(handle,I2C_ANALOGFILTER_ENABLE);
<> 153:fa9ff456f731 415 #endif
<> 153:fa9ff456f731 416
<> 153:fa9ff456f731 417 // I2C configuration
<> 153:fa9ff456f731 418 handle->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
<> 153:fa9ff456f731 419 handle->Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
<> 153:fa9ff456f731 420 handle->Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
<> 153:fa9ff456f731 421 handle->Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
<> 153:fa9ff456f731 422 handle->Init.OwnAddress1 = 0;
<> 153:fa9ff456f731 423 handle->Init.OwnAddress2 = 0;
<> 153:fa9ff456f731 424 HAL_I2C_Init(handle);
<> 153:fa9ff456f731 425
<> 153:fa9ff456f731 426 /* store frequency for timeout computation */
<> 153:fa9ff456f731 427 obj_s->hz = hz;
<> 153:fa9ff456f731 428 }
<> 153:fa9ff456f731 429
<> 153:fa9ff456f731 430 i2c_t *get_i2c_obj(I2C_HandleTypeDef *hi2c){
<> 153:fa9ff456f731 431 /* Aim of the function is to get i2c_s pointer using hi2c pointer */
<> 153:fa9ff456f731 432 /* Highly inspired from magical linux kernel's "container_of" */
<> 153:fa9ff456f731 433 /* (which was not directly used since not compatible with IAR toolchain) */
<> 153:fa9ff456f731 434 struct i2c_s *obj_s;
<> 153:fa9ff456f731 435 i2c_t *obj;
<> 153:fa9ff456f731 436
<> 153:fa9ff456f731 437 obj_s = (struct i2c_s *)( (char *)hi2c - offsetof(struct i2c_s,handle));
<> 153:fa9ff456f731 438 obj = (i2c_t *)( (char *)obj_s - offsetof(i2c_t,i2c));
<> 153:fa9ff456f731 439
<> 153:fa9ff456f731 440 return (obj);
<> 153:fa9ff456f731 441 }
<> 153:fa9ff456f731 442
<> 153:fa9ff456f731 443 /*
<> 153:fa9ff456f731 444 * UNITARY APIS.
<> 153:fa9ff456f731 445 * For very basic operations, direct registers access is needed
<> 153:fa9ff456f731 446 * There are 2 different IPs version that need to be supported
<> 153:fa9ff456f731 447 */
<> 153:fa9ff456f731 448 #ifdef I2C_IP_VERSION_V1
<> 153:fa9ff456f731 449 int i2c_start(i2c_t *obj) {
<> 153:fa9ff456f731 450
<> 153:fa9ff456f731 451 int timeout;
<> 153:fa9ff456f731 452 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 453 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 153:fa9ff456f731 454
<> 153:fa9ff456f731 455 // Clear Acknowledge failure flag
<> 153:fa9ff456f731 456 __HAL_I2C_CLEAR_FLAG(handle, I2C_FLAG_AF);
<> 153:fa9ff456f731 457
<> 153:fa9ff456f731 458 // Wait the STOP condition has been previously correctly sent
<> 153:fa9ff456f731 459 // This timeout can be avoid in some specific cases by simply clearing the STOP bit
<> 153:fa9ff456f731 460 timeout = FLAG_TIMEOUT;
<> 153:fa9ff456f731 461 while ((handle->Instance->CR1 & I2C_CR1_STOP) == I2C_CR1_STOP) {
<> 153:fa9ff456f731 462 if ((timeout--) == 0) {
<> 153:fa9ff456f731 463 return 1;
<> 153:fa9ff456f731 464 }
<> 153:fa9ff456f731 465 }
<> 153:fa9ff456f731 466
<> 153:fa9ff456f731 467 // Generate the START condition
<> 153:fa9ff456f731 468 handle->Instance->CR1 |= I2C_CR1_START;
<> 153:fa9ff456f731 469
<> 153:fa9ff456f731 470 // Wait the START condition has been correctly sent
<> 153:fa9ff456f731 471 timeout = FLAG_TIMEOUT;
<> 153:fa9ff456f731 472 while (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_SB) == RESET) {
<> 153:fa9ff456f731 473 if ((timeout--) == 0) {
<> 153:fa9ff456f731 474 return 1;
<> 153:fa9ff456f731 475 }
<> 153:fa9ff456f731 476 }
<> 153:fa9ff456f731 477
<> 153:fa9ff456f731 478 return 0;
<> 153:fa9ff456f731 479 }
<> 153:fa9ff456f731 480
<> 153:fa9ff456f731 481 int i2c_stop(i2c_t *obj) {
<> 153:fa9ff456f731 482 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 483 I2C_TypeDef *i2c = (I2C_TypeDef *)obj_s->i2c;
<> 154:37f96f9d4de2 484 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 154:37f96f9d4de2 485 int timeout;
<> 153:fa9ff456f731 486
<> 153:fa9ff456f731 487 // Generate the STOP condition
<> 153:fa9ff456f731 488 i2c->CR1 |= I2C_CR1_STOP;
<> 153:fa9ff456f731 489
<> 154:37f96f9d4de2 490 /* In case of mixed usage of the APIs (unitary + SYNC)
<> 154:37f96f9d4de2 491 * re-inti HAL state
<> 154:37f96f9d4de2 492 */
<> 154:37f96f9d4de2 493 if(obj_s->XferOperation != I2C_FIRST_AND_LAST_FRAME)
<> 154:37f96f9d4de2 494 i2c_init(obj, obj_s->sda, obj_s->scl);
<> 154:37f96f9d4de2 495
<> 153:fa9ff456f731 496 return 0;
<> 153:fa9ff456f731 497 }
<> 153:fa9ff456f731 498
<> 153:fa9ff456f731 499 int i2c_byte_read(i2c_t *obj, int last) {
<> 153:fa9ff456f731 500
<> 153:fa9ff456f731 501 int timeout;
<> 153:fa9ff456f731 502 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 503 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 153:fa9ff456f731 504
<> 153:fa9ff456f731 505 if (last) {
<> 153:fa9ff456f731 506 // Don't acknowledge the last byte
<> 153:fa9ff456f731 507 handle->Instance->CR1 &= ~I2C_CR1_ACK;
<> 153:fa9ff456f731 508 } else {
<> 153:fa9ff456f731 509 // Acknowledge the byte
<> 153:fa9ff456f731 510 handle->Instance->CR1 |= I2C_CR1_ACK;
<> 153:fa9ff456f731 511 }
<> 153:fa9ff456f731 512
<> 153:fa9ff456f731 513 // Wait until the byte is received
<> 153:fa9ff456f731 514 timeout = FLAG_TIMEOUT;
<> 153:fa9ff456f731 515 while (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_RXNE) == RESET) {
<> 153:fa9ff456f731 516 if ((timeout--) == 0) {
<> 153:fa9ff456f731 517 return -1;
<> 153:fa9ff456f731 518 }
<> 153:fa9ff456f731 519 }
<> 153:fa9ff456f731 520
<> 153:fa9ff456f731 521 return (int)handle->Instance->DR;
<> 153:fa9ff456f731 522 }
<> 153:fa9ff456f731 523
<> 153:fa9ff456f731 524 int i2c_byte_write(i2c_t *obj, int data) {
<> 153:fa9ff456f731 525
<> 153:fa9ff456f731 526 int timeout;
<> 153:fa9ff456f731 527 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 528 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 153:fa9ff456f731 529
<> 153:fa9ff456f731 530 handle->Instance->DR = (uint8_t)data;
<> 153:fa9ff456f731 531
<> 153:fa9ff456f731 532 // Wait until the byte (might be the address) is transmitted
<> 153:fa9ff456f731 533 timeout = FLAG_TIMEOUT;
<> 153:fa9ff456f731 534 while ((__HAL_I2C_GET_FLAG(handle, I2C_FLAG_TXE) == RESET) &&
<> 153:fa9ff456f731 535 (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_BTF) == RESET) &&
<> 153:fa9ff456f731 536 (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_ADDR) == RESET)) {
<> 153:fa9ff456f731 537 if ((timeout--) == 0) {
<> 153:fa9ff456f731 538 return 0;
<> 153:fa9ff456f731 539 }
<> 153:fa9ff456f731 540 }
<> 153:fa9ff456f731 541
<> 153:fa9ff456f731 542 if (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_ADDR) != RESET)
<> 153:fa9ff456f731 543 {
<> 153:fa9ff456f731 544 __HAL_I2C_CLEAR_ADDRFLAG(handle);
<> 153:fa9ff456f731 545 }
<> 153:fa9ff456f731 546
<> 153:fa9ff456f731 547 return 1;
<> 153:fa9ff456f731 548 }
<> 153:fa9ff456f731 549 #endif //I2C_IP_VERSION_V1
<> 153:fa9ff456f731 550 #ifdef I2C_IP_VERSION_V2
<> 153:fa9ff456f731 551 int i2c_start(i2c_t *obj) {
<> 153:fa9ff456f731 552 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 553 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 153:fa9ff456f731 554 I2C_TypeDef *i2c = (I2C_TypeDef *)obj_s->i2c;
<> 153:fa9ff456f731 555 int timeout;
<> 153:fa9ff456f731 556
<> 153:fa9ff456f731 557 // Clear Acknowledge failure flag
<> 153:fa9ff456f731 558 __HAL_I2C_CLEAR_FLAG(handle, I2C_FLAG_AF);
<> 153:fa9ff456f731 559
<> 153:fa9ff456f731 560 // Wait the STOP condition has been previously correctly sent
<> 153:fa9ff456f731 561 timeout = FLAG_TIMEOUT;
<> 153:fa9ff456f731 562 while ((i2c->CR2 & I2C_CR2_STOP) == I2C_CR2_STOP){
<> 153:fa9ff456f731 563 if ((timeout--) == 0) {
<> 153:fa9ff456f731 564 return 1;
<> 153:fa9ff456f731 565 }
<> 153:fa9ff456f731 566 }
<> 153:fa9ff456f731 567
<> 153:fa9ff456f731 568 // Generate the START condition
<> 153:fa9ff456f731 569 i2c->CR2 |= I2C_CR2_START;
<> 153:fa9ff456f731 570
<> 153:fa9ff456f731 571 // Wait the START condition has been correctly sent
<> 153:fa9ff456f731 572 timeout = FLAG_TIMEOUT;
<> 153:fa9ff456f731 573 while (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_BUSY) == RESET) {
<> 153:fa9ff456f731 574 if ((timeout--) == 0) {
<> 153:fa9ff456f731 575 return 1;
<> 153:fa9ff456f731 576 }
<> 153:fa9ff456f731 577 }
<> 153:fa9ff456f731 578
<> 153:fa9ff456f731 579 return 0;
<> 153:fa9ff456f731 580 }
<> 153:fa9ff456f731 581
<> 153:fa9ff456f731 582 int i2c_stop(i2c_t *obj) {
<> 153:fa9ff456f731 583 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 584 I2C_TypeDef *i2c = (I2C_TypeDef *)obj_s->i2c;
<> 153:fa9ff456f731 585
<> 153:fa9ff456f731 586 // Generate the STOP condition
<> 153:fa9ff456f731 587 i2c->CR2 |= I2C_CR2_STOP;
<> 153:fa9ff456f731 588
<> 153:fa9ff456f731 589 return 0;
<> 153:fa9ff456f731 590 }
<> 153:fa9ff456f731 591
<> 153:fa9ff456f731 592 int i2c_byte_read(i2c_t *obj, int last) {
<> 153:fa9ff456f731 593 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 594 I2C_TypeDef *i2c = (I2C_TypeDef *)obj_s->i2c;
<> 153:fa9ff456f731 595 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 153:fa9ff456f731 596 int timeout;
<> 153:fa9ff456f731 597
<> 153:fa9ff456f731 598 // Wait until the byte is received
<> 153:fa9ff456f731 599 timeout = FLAG_TIMEOUT;
<> 153:fa9ff456f731 600 while (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_RXNE) == RESET) {
<> 153:fa9ff456f731 601 if ((timeout--) == 0) {
<> 153:fa9ff456f731 602 return -1;
<> 153:fa9ff456f731 603 }
<> 153:fa9ff456f731 604 }
<> 153:fa9ff456f731 605
<> 153:fa9ff456f731 606 return (int)i2c->RXDR;
<> 153:fa9ff456f731 607 }
<> 153:fa9ff456f731 608
<> 153:fa9ff456f731 609 int i2c_byte_write(i2c_t *obj, int data) {
<> 153:fa9ff456f731 610 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 611 I2C_TypeDef *i2c = (I2C_TypeDef *)obj_s->i2c;
<> 153:fa9ff456f731 612 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 153:fa9ff456f731 613 int timeout;
<> 153:fa9ff456f731 614
<> 153:fa9ff456f731 615 // Wait until the previous byte is transmitted
<> 153:fa9ff456f731 616 timeout = FLAG_TIMEOUT;
<> 153:fa9ff456f731 617 while (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_TXIS) == RESET) {
<> 153:fa9ff456f731 618 if ((timeout--) == 0) {
<> 153:fa9ff456f731 619 return 0;
<> 153:fa9ff456f731 620 }
<> 153:fa9ff456f731 621 }
<> 153:fa9ff456f731 622
<> 153:fa9ff456f731 623 i2c->TXDR = (uint8_t)data;
<> 153:fa9ff456f731 624
<> 153:fa9ff456f731 625 return 1;
<> 153:fa9ff456f731 626 }
<> 153:fa9ff456f731 627 #endif //I2C_IP_VERSION_V2
<> 153:fa9ff456f731 628
<> 153:fa9ff456f731 629 void i2c_reset(i2c_t *obj) {
<> 153:fa9ff456f731 630 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 631 /* As recommended in i2c_api.h, mainly send stop */
<> 153:fa9ff456f731 632 i2c_stop(obj);
<> 153:fa9ff456f731 633 /* then re-init */
<> 153:fa9ff456f731 634 i2c_init(obj, obj_s->sda, obj_s->scl);
<> 153:fa9ff456f731 635 }
<> 153:fa9ff456f731 636
<> 153:fa9ff456f731 637 /*
<> 153:fa9ff456f731 638 * SYNC APIS
<> 153:fa9ff456f731 639 */
<> 154:37f96f9d4de2 640 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
<> 154:37f96f9d4de2 641 struct i2c_s *obj_s = I2C_S(obj);
<> 154:37f96f9d4de2 642 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 154:37f96f9d4de2 643 int count = I2C_ERROR_BUS_BUSY, ret = 0;
<> 154:37f96f9d4de2 644 uint32_t timeout = 0;
<> 154:37f96f9d4de2 645
<> 154:37f96f9d4de2 646 if((length == 0) || (data == 0)) {
<> 154:37f96f9d4de2 647 if(HAL_I2C_IsDeviceReady(handle, address, 1, 10) == HAL_OK)
<> 154:37f96f9d4de2 648 return 0;
<> 154:37f96f9d4de2 649 else
<> 154:37f96f9d4de2 650 return I2C_ERROR_BUS_BUSY;
<> 154:37f96f9d4de2 651 }
<> 154:37f96f9d4de2 652
<> 154:37f96f9d4de2 653 if ((obj_s->XferOperation == I2C_FIRST_AND_LAST_FRAME) ||
<> 154:37f96f9d4de2 654 (obj_s->XferOperation == I2C_LAST_FRAME)) {
<> 154:37f96f9d4de2 655 if (stop)
<> 154:37f96f9d4de2 656 obj_s->XferOperation = I2C_FIRST_AND_LAST_FRAME;
<> 154:37f96f9d4de2 657 else
<> 154:37f96f9d4de2 658 obj_s->XferOperation = I2C_FIRST_FRAME;
<> 154:37f96f9d4de2 659 } else if ((obj_s->XferOperation == I2C_FIRST_FRAME) ||
<> 154:37f96f9d4de2 660 (obj_s->XferOperation == I2C_NEXT_FRAME)) {
<> 154:37f96f9d4de2 661 if (stop)
<> 154:37f96f9d4de2 662 obj_s->XferOperation = I2C_LAST_FRAME;
<> 154:37f96f9d4de2 663 else
<> 154:37f96f9d4de2 664 obj_s->XferOperation = I2C_NEXT_FRAME;
<> 154:37f96f9d4de2 665 }
<> 154:37f96f9d4de2 666
<> 154:37f96f9d4de2 667 obj_s->event = 0;
<> 154:37f96f9d4de2 668
<> 154:37f96f9d4de2 669 /* Activate default IRQ handlers for sync mode
<> 154:37f96f9d4de2 670 * which would be overwritten in async mode
<> 154:37f96f9d4de2 671 */
<> 154:37f96f9d4de2 672 i2c_ev_err_enable(obj, i2c_get_irq_handler(obj));
<> 154:37f96f9d4de2 673
<> 154:37f96f9d4de2 674 ret = HAL_I2C_Master_Sequential_Receive_IT(handle, address, (uint8_t *) data, length, obj_s->XferOperation);
<> 154:37f96f9d4de2 675
<> 154:37f96f9d4de2 676 if(ret == HAL_OK) {
<> 154:37f96f9d4de2 677 timeout = BYTE_TIMEOUT_US * (length + 1);
<> 154:37f96f9d4de2 678 /* transfer started : wait completion or timeout */
<> 154:37f96f9d4de2 679 while(!(obj_s->event & I2C_EVENT_ALL) && (--timeout != 0)) {
<> 154:37f96f9d4de2 680 wait_us(1);
<> 154:37f96f9d4de2 681 }
<> 154:37f96f9d4de2 682
<> 154:37f96f9d4de2 683 i2c_ev_err_disable(obj);
<> 154:37f96f9d4de2 684
<> 154:37f96f9d4de2 685 if((timeout == 0) || (obj_s->event != I2C_EVENT_TRANSFER_COMPLETE)) {
<> 154:37f96f9d4de2 686 DEBUG_PRINTF(" TIMEOUT or error in i2c_read\r\n");
<> 154:37f96f9d4de2 687 /* re-init IP to try and get back in a working state */
<> 154:37f96f9d4de2 688 i2c_init(obj, obj_s->sda, obj_s->scl);
<> 154:37f96f9d4de2 689 } else {
<> 154:37f96f9d4de2 690 count = length;
<> 154:37f96f9d4de2 691 }
<> 154:37f96f9d4de2 692 } else {
<> 154:37f96f9d4de2 693 DEBUG_PRINTF("ERROR in i2c_read:%d\r\n", ret);
<> 154:37f96f9d4de2 694 }
<> 154:37f96f9d4de2 695
<> 154:37f96f9d4de2 696 return count;
<> 154:37f96f9d4de2 697 }
<> 154:37f96f9d4de2 698
<> 153:fa9ff456f731 699 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
<> 153:fa9ff456f731 700 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 701 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 154:37f96f9d4de2 702 int count = I2C_ERROR_BUS_BUSY, ret = 0;
<> 153:fa9ff456f731 703 uint32_t timeout = 0;
<> 153:fa9ff456f731 704
<> 154:37f96f9d4de2 705 if((length == 0) || (data == 0)) {
<> 154:37f96f9d4de2 706 if(HAL_I2C_IsDeviceReady(handle, address, 1, 10) == HAL_OK)
<> 154:37f96f9d4de2 707 return 0;
<> 154:37f96f9d4de2 708 else
<> 154:37f96f9d4de2 709 return I2C_ERROR_BUS_BUSY;
<> 154:37f96f9d4de2 710 }
<> 154:37f96f9d4de2 711
<> 153:fa9ff456f731 712 if ((obj_s->XferOperation == I2C_FIRST_AND_LAST_FRAME) ||
<> 153:fa9ff456f731 713 (obj_s->XferOperation == I2C_LAST_FRAME)) {
<> 153:fa9ff456f731 714 if (stop)
<> 153:fa9ff456f731 715 obj_s->XferOperation = I2C_FIRST_AND_LAST_FRAME;
<> 153:fa9ff456f731 716 else
<> 153:fa9ff456f731 717 obj_s->XferOperation = I2C_FIRST_FRAME;
<> 153:fa9ff456f731 718 } else if ((obj_s->XferOperation == I2C_FIRST_FRAME) ||
<> 153:fa9ff456f731 719 (obj_s->XferOperation == I2C_NEXT_FRAME)) {
<> 153:fa9ff456f731 720 if (stop)
<> 153:fa9ff456f731 721 obj_s->XferOperation = I2C_LAST_FRAME;
<> 153:fa9ff456f731 722 else
<> 153:fa9ff456f731 723 obj_s->XferOperation = I2C_NEXT_FRAME;
<> 153:fa9ff456f731 724 }
<> 153:fa9ff456f731 725
<> 153:fa9ff456f731 726 obj_s->event = 0;
<> 153:fa9ff456f731 727
<> 153:fa9ff456f731 728 i2c_ev_err_enable(obj, i2c_get_irq_handler(obj));
<> 153:fa9ff456f731 729
<> 153:fa9ff456f731 730 ret = HAL_I2C_Master_Sequential_Transmit_IT(handle, address, (uint8_t *) data, length, obj_s->XferOperation);
<> 153:fa9ff456f731 731
<> 153:fa9ff456f731 732 if(ret == HAL_OK) {
<> 154:37f96f9d4de2 733 timeout = BYTE_TIMEOUT_US * (length + 1);
<> 153:fa9ff456f731 734 /* transfer started : wait completion or timeout */
<> 153:fa9ff456f731 735 while(!(obj_s->event & I2C_EVENT_ALL) && (--timeout != 0)) {
<> 153:fa9ff456f731 736 wait_us(1);
<> 153:fa9ff456f731 737 }
<> 153:fa9ff456f731 738
<> 153:fa9ff456f731 739 i2c_ev_err_disable(obj);
<> 153:fa9ff456f731 740
<> 153:fa9ff456f731 741 if((timeout == 0) || (obj_s->event != I2C_EVENT_TRANSFER_COMPLETE)) {
<> 153:fa9ff456f731 742 DEBUG_PRINTF(" TIMEOUT or error in i2c_write\r\n");
<> 153:fa9ff456f731 743 /* re-init IP to try and get back in a working state */
<> 153:fa9ff456f731 744 i2c_init(obj, obj_s->sda, obj_s->scl);
<> 153:fa9ff456f731 745 } else {
<> 153:fa9ff456f731 746 count = length;
<> 153:fa9ff456f731 747 }
<> 153:fa9ff456f731 748 } else {
<> 153:fa9ff456f731 749 DEBUG_PRINTF("ERROR in i2c_read\r\n");
<> 153:fa9ff456f731 750 }
<> 153:fa9ff456f731 751
<> 153:fa9ff456f731 752 return count;
<> 153:fa9ff456f731 753 }
<> 153:fa9ff456f731 754
<> 153:fa9ff456f731 755 void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c){
<> 153:fa9ff456f731 756 /* Get object ptr based on handler ptr */
<> 153:fa9ff456f731 757 i2c_t *obj = get_i2c_obj(hi2c);
<> 153:fa9ff456f731 758 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 759
<> 153:fa9ff456f731 760 #if DEVICE_I2C_ASYNCH
<> 153:fa9ff456f731 761 /* Handle potential Tx/Rx use case */
<> 153:fa9ff456f731 762 if ((obj->tx_buff.length) && (obj->rx_buff.length)) {
<> 153:fa9ff456f731 763 if (obj_s->stop) {
<> 153:fa9ff456f731 764 obj_s->XferOperation = I2C_LAST_FRAME;
<> 153:fa9ff456f731 765 } else {
<> 153:fa9ff456f731 766 obj_s->XferOperation = I2C_NEXT_FRAME;
<> 153:fa9ff456f731 767 }
<> 153:fa9ff456f731 768
<> 153:fa9ff456f731 769 HAL_I2C_Master_Sequential_Receive_IT(hi2c, obj_s->address, (uint8_t*)obj->rx_buff.buffer , obj->rx_buff.length, obj_s->XferOperation);
<> 153:fa9ff456f731 770 }
<> 153:fa9ff456f731 771 else
<> 153:fa9ff456f731 772 #endif
<> 153:fa9ff456f731 773 {
<> 153:fa9ff456f731 774 /* Set event flag */
<> 153:fa9ff456f731 775 obj_s->event = I2C_EVENT_TRANSFER_COMPLETE;
<> 153:fa9ff456f731 776 }
<> 153:fa9ff456f731 777 }
<> 153:fa9ff456f731 778
<> 153:fa9ff456f731 779 void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c){
<> 153:fa9ff456f731 780 /* Get object ptr based on handler ptr */
<> 153:fa9ff456f731 781 i2c_t *obj = get_i2c_obj(hi2c);
<> 153:fa9ff456f731 782 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 783
<> 153:fa9ff456f731 784 /* Set event flag */
<> 153:fa9ff456f731 785 obj_s->event = I2C_EVENT_TRANSFER_COMPLETE;
<> 153:fa9ff456f731 786 }
<> 153:fa9ff456f731 787
<> 153:fa9ff456f731 788 void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c){
<> 153:fa9ff456f731 789 /* Get object ptr based on handler ptr */
<> 153:fa9ff456f731 790 i2c_t *obj = get_i2c_obj(hi2c);
<> 153:fa9ff456f731 791 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 792 #if DEVICE_I2CSLAVE
<> 153:fa9ff456f731 793 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 153:fa9ff456f731 794 uint32_t address = 0;
<> 153:fa9ff456f731 795 /* Store address to handle it after reset */
<> 153:fa9ff456f731 796 if(obj_s->slave)
<> 153:fa9ff456f731 797 address = handle->Init.OwnAddress1;
<> 153:fa9ff456f731 798 #endif
<> 153:fa9ff456f731 799
<> 153:fa9ff456f731 800 DEBUG_PRINTF("HAL_I2C_ErrorCallback:%d, index=%d\r\n", (int) hi2c->ErrorCode, obj_s->index);
<> 153:fa9ff456f731 801
<> 153:fa9ff456f731 802 /* re-init IP to try and get back in a working state */
<> 153:fa9ff456f731 803 i2c_init(obj, obj_s->sda, obj_s->scl);
<> 153:fa9ff456f731 804
<> 153:fa9ff456f731 805 #if DEVICE_I2CSLAVE
<> 153:fa9ff456f731 806 /* restore slave address */
<> 153:fa9ff456f731 807 i2c_slave_address(obj, 0, address, 0);
<> 153:fa9ff456f731 808 #endif
<> 153:fa9ff456f731 809
<> 153:fa9ff456f731 810 /* Keep Set event flag */
<> 153:fa9ff456f731 811 obj_s->event = I2C_EVENT_ERROR;
<> 153:fa9ff456f731 812 }
<> 153:fa9ff456f731 813
<> 153:fa9ff456f731 814 #if DEVICE_I2CSLAVE
<> 153:fa9ff456f731 815 /* SLAVE API FUNCTIONS */
<> 153:fa9ff456f731 816 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) {
<> 153:fa9ff456f731 817 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 818 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 153:fa9ff456f731 819
<> 153:fa9ff456f731 820 // I2C configuration
<> 153:fa9ff456f731 821 handle->Init.OwnAddress1 = address;
<> 153:fa9ff456f731 822 HAL_I2C_Init(handle);
<> 153:fa9ff456f731 823
<> 153:fa9ff456f731 824 i2c_ev_err_enable(obj, i2c_get_irq_handler(obj));
<> 153:fa9ff456f731 825
<> 153:fa9ff456f731 826 HAL_I2C_EnableListen_IT(handle);
<> 153:fa9ff456f731 827 }
<> 153:fa9ff456f731 828
<> 153:fa9ff456f731 829 void i2c_slave_mode(i2c_t *obj, int enable_slave) {
<> 153:fa9ff456f731 830
<> 153:fa9ff456f731 831 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 832 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 153:fa9ff456f731 833
<> 153:fa9ff456f731 834 if (enable_slave) {
<> 153:fa9ff456f731 835 obj_s->slave = 1;
<> 153:fa9ff456f731 836 HAL_I2C_EnableListen_IT(handle);
<> 153:fa9ff456f731 837 } else {
<> 153:fa9ff456f731 838 obj_s->slave = 0;
<> 153:fa9ff456f731 839 HAL_I2C_DisableListen_IT(handle);
<> 153:fa9ff456f731 840 }
<> 153:fa9ff456f731 841 }
<> 153:fa9ff456f731 842
<> 153:fa9ff456f731 843 // See I2CSlave.h
<> 153:fa9ff456f731 844 #define NoData 0 // the slave has not been addressed
<> 153:fa9ff456f731 845 #define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter)
<> 153:fa9ff456f731 846 #define WriteGeneral 2 // the master is writing to all slave
<> 153:fa9ff456f731 847 #define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
<> 153:fa9ff456f731 848
<> 153:fa9ff456f731 849
<> 153:fa9ff456f731 850 void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode) {
<> 153:fa9ff456f731 851 /* Get object ptr based on handler ptr */
<> 153:fa9ff456f731 852 i2c_t *obj = get_i2c_obj(hi2c);
<> 153:fa9ff456f731 853 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 854
<> 153:fa9ff456f731 855 /* Transfer direction in HAL is from Master point of view */
<> 153:fa9ff456f731 856 if(TransferDirection == I2C_DIRECTION_RECEIVE) {
<> 153:fa9ff456f731 857 obj_s->pending_slave_tx_master_rx = 1;
<> 153:fa9ff456f731 858 }
<> 153:fa9ff456f731 859
<> 153:fa9ff456f731 860 if(TransferDirection == I2C_DIRECTION_TRANSMIT) {
<> 153:fa9ff456f731 861 obj_s->pending_slave_rx_maxter_tx = 1;
<> 153:fa9ff456f731 862 }
<> 153:fa9ff456f731 863 }
<> 153:fa9ff456f731 864
<> 153:fa9ff456f731 865 void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *I2cHandle){
<> 153:fa9ff456f731 866 /* Get object ptr based on handler ptr */
<> 153:fa9ff456f731 867 i2c_t *obj = get_i2c_obj(I2cHandle);
<> 153:fa9ff456f731 868 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 869 obj_s->pending_slave_tx_master_rx = 0;
<> 153:fa9ff456f731 870 }
<> 153:fa9ff456f731 871
<> 153:fa9ff456f731 872 void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *I2cHandle){
<> 153:fa9ff456f731 873 /* Get object ptr based on handler ptr */
<> 153:fa9ff456f731 874 i2c_t *obj = get_i2c_obj(I2cHandle);
<> 153:fa9ff456f731 875 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 876 obj_s->pending_slave_rx_maxter_tx = 0;
<> 153:fa9ff456f731 877 }
<> 153:fa9ff456f731 878
<> 153:fa9ff456f731 879 void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c)
<> 153:fa9ff456f731 880 {
<> 153:fa9ff456f731 881 /* restart listening for master requests */
<> 153:fa9ff456f731 882 HAL_I2C_EnableListen_IT(hi2c);
<> 153:fa9ff456f731 883 }
<> 153:fa9ff456f731 884
<> 153:fa9ff456f731 885 int i2c_slave_receive(i2c_t *obj) {
<> 153:fa9ff456f731 886
<> 153:fa9ff456f731 887 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 888 int retValue = NoData;
<> 153:fa9ff456f731 889
<> 153:fa9ff456f731 890 if(obj_s->pending_slave_rx_maxter_tx) {
<> 153:fa9ff456f731 891 retValue = WriteAddressed;
<> 153:fa9ff456f731 892 }
<> 153:fa9ff456f731 893
<> 153:fa9ff456f731 894 if(obj_s->pending_slave_tx_master_rx) {
<> 153:fa9ff456f731 895 retValue = ReadAddressed;
<> 153:fa9ff456f731 896 }
<> 153:fa9ff456f731 897
<> 153:fa9ff456f731 898 return (retValue);
<> 153:fa9ff456f731 899 }
<> 153:fa9ff456f731 900
<> 153:fa9ff456f731 901 int i2c_slave_read(i2c_t *obj, char *data, int length) {
<> 153:fa9ff456f731 902 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 903 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 153:fa9ff456f731 904 int count = 0;
<> 153:fa9ff456f731 905 int ret = 0;
<> 153:fa9ff456f731 906 uint32_t timeout = 0;
<> 153:fa9ff456f731 907
<> 153:fa9ff456f731 908 /* Always use I2C_NEXT_FRAME as slave will just adapt to master requests */
<> 153:fa9ff456f731 909 ret = HAL_I2C_Slave_Sequential_Receive_IT(handle, (uint8_t *) data, length, I2C_NEXT_FRAME);
<> 153:fa9ff456f731 910
<> 153:fa9ff456f731 911 if(ret == HAL_OK) {
<> 154:37f96f9d4de2 912 timeout = BYTE_TIMEOUT_US * (length + 1);
<> 153:fa9ff456f731 913 while(obj_s->pending_slave_rx_maxter_tx && (--timeout != 0)) {
<> 153:fa9ff456f731 914 wait_us(1);
<> 153:fa9ff456f731 915 }
<> 153:fa9ff456f731 916
<> 153:fa9ff456f731 917 if(timeout != 0) {
<> 153:fa9ff456f731 918 count = length;
<> 153:fa9ff456f731 919 } else {
<> 153:fa9ff456f731 920 DEBUG_PRINTF("TIMEOUT or error in i2c_slave_read\r\n");
<> 153:fa9ff456f731 921 }
<> 153:fa9ff456f731 922 }
<> 153:fa9ff456f731 923 return count;
<> 153:fa9ff456f731 924 }
<> 153:fa9ff456f731 925
<> 153:fa9ff456f731 926 int i2c_slave_write(i2c_t *obj, const char *data, int length) {
<> 153:fa9ff456f731 927 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 928 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 153:fa9ff456f731 929 int count = 0;
<> 153:fa9ff456f731 930 int ret = 0;
<> 153:fa9ff456f731 931 uint32_t timeout = 0;
<> 153:fa9ff456f731 932
<> 153:fa9ff456f731 933 /* Always use I2C_NEXT_FRAME as slave will just adapt to master requests */
<> 153:fa9ff456f731 934 ret = HAL_I2C_Slave_Sequential_Transmit_IT(handle, (uint8_t *) data, length, I2C_NEXT_FRAME);
<> 153:fa9ff456f731 935
<> 153:fa9ff456f731 936 if(ret == HAL_OK) {
<> 154:37f96f9d4de2 937 timeout = BYTE_TIMEOUT_US * (length + 1);
<> 153:fa9ff456f731 938 while(obj_s->pending_slave_tx_master_rx && (--timeout != 0)) {
<> 153:fa9ff456f731 939 wait_us(1);
<> 153:fa9ff456f731 940 }
<> 153:fa9ff456f731 941
<> 153:fa9ff456f731 942 if(timeout != 0) {
<> 153:fa9ff456f731 943 count = length;
<> 153:fa9ff456f731 944 } else {
<> 153:fa9ff456f731 945 DEBUG_PRINTF("TIMEOUT or error in i2c_slave_write\r\n");
<> 153:fa9ff456f731 946 }
<> 153:fa9ff456f731 947 }
<> 153:fa9ff456f731 948
<> 153:fa9ff456f731 949 return count;
<> 153:fa9ff456f731 950 }
<> 153:fa9ff456f731 951 #endif // DEVICE_I2CSLAVE
<> 153:fa9ff456f731 952
<> 153:fa9ff456f731 953 #if DEVICE_I2C_ASYNCH
<> 153:fa9ff456f731 954 /* ASYNCH MASTER API FUNCTIONS */
<> 153:fa9ff456f731 955 void HAL_I2C_AbortCpltCallback(I2C_HandleTypeDef *hi2c){
<> 153:fa9ff456f731 956 /* Get object ptr based on handler ptr */
<> 153:fa9ff456f731 957 i2c_t *obj = get_i2c_obj(hi2c);
<> 153:fa9ff456f731 958 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 959 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 153:fa9ff456f731 960
<> 153:fa9ff456f731 961 /* Disable IT. Not always done before calling macro */
<> 153:fa9ff456f731 962 __HAL_I2C_DISABLE_IT(handle, I2C_IT_ALL);
<> 153:fa9ff456f731 963 i2c_ev_err_disable(obj);
<> 153:fa9ff456f731 964
<> 153:fa9ff456f731 965 /* Set event flag */
<> 153:fa9ff456f731 966 obj_s->event = I2C_EVENT_ERROR;
<> 153:fa9ff456f731 967 }
<> 153:fa9ff456f731 968
<> 153:fa9ff456f731 969 void i2c_transfer_asynch(i2c_t *obj, const void *tx, size_t tx_length, void *rx, size_t rx_length, uint32_t address, uint32_t stop, uint32_t handler, uint32_t event, DMAUsage hint) {
<> 153:fa9ff456f731 970
<> 153:fa9ff456f731 971 // TODO: DMA usage is currently ignored by this way
<> 153:fa9ff456f731 972 (void) hint;
<> 153:fa9ff456f731 973
<> 153:fa9ff456f731 974 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 975 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 153:fa9ff456f731 976
<> 153:fa9ff456f731 977 /* Update object */
<> 153:fa9ff456f731 978 obj->tx_buff.buffer = (void *)tx;
<> 153:fa9ff456f731 979 obj->tx_buff.length = tx_length;
<> 153:fa9ff456f731 980 obj->tx_buff.pos = 0;
<> 153:fa9ff456f731 981 obj->tx_buff.width = 8;
<> 153:fa9ff456f731 982
<> 153:fa9ff456f731 983 obj->rx_buff.buffer = (void *)rx;
<> 153:fa9ff456f731 984 obj->rx_buff.length = rx_length;
<> 153:fa9ff456f731 985 obj->rx_buff.pos = SIZE_MAX;
<> 153:fa9ff456f731 986 obj->rx_buff.width = 8;
<> 153:fa9ff456f731 987
<> 153:fa9ff456f731 988 obj_s->available_events = event;
<> 153:fa9ff456f731 989 obj_s->event = 0;
<> 153:fa9ff456f731 990 obj_s->address = address;
<> 153:fa9ff456f731 991 obj_s->stop = stop;
<> 153:fa9ff456f731 992
<> 153:fa9ff456f731 993 i2c_ev_err_enable(obj, handler);
<> 153:fa9ff456f731 994
<> 153:fa9ff456f731 995 /* Set operation step depending if stop sending required or not */
<> 153:fa9ff456f731 996 if ((tx_length && !rx_length) || (!tx_length && rx_length)) {
<> 153:fa9ff456f731 997 if ((obj_s->XferOperation == I2C_FIRST_AND_LAST_FRAME) ||
<> 153:fa9ff456f731 998 (obj_s->XferOperation == I2C_LAST_FRAME)) {
<> 153:fa9ff456f731 999 if (stop)
<> 153:fa9ff456f731 1000 obj_s->XferOperation = I2C_FIRST_AND_LAST_FRAME;
<> 153:fa9ff456f731 1001 else
<> 153:fa9ff456f731 1002 obj_s->XferOperation = I2C_FIRST_FRAME;
<> 153:fa9ff456f731 1003 } else if ((obj_s->XferOperation == I2C_FIRST_FRAME) ||
<> 153:fa9ff456f731 1004 (obj_s->XferOperation == I2C_NEXT_FRAME)) {
<> 153:fa9ff456f731 1005 if (stop)
<> 153:fa9ff456f731 1006 obj_s->XferOperation = I2C_LAST_FRAME;
<> 153:fa9ff456f731 1007 else
<> 153:fa9ff456f731 1008 obj_s->XferOperation = I2C_NEXT_FRAME;
<> 153:fa9ff456f731 1009 }
<> 153:fa9ff456f731 1010
<> 153:fa9ff456f731 1011 if (tx_length > 0) {
<> 153:fa9ff456f731 1012 HAL_I2C_Master_Sequential_Transmit_IT(handle, address, (uint8_t*)tx, tx_length, obj_s->XferOperation);
<> 153:fa9ff456f731 1013 }
<> 153:fa9ff456f731 1014 if (rx_length > 0) {
<> 153:fa9ff456f731 1015 HAL_I2C_Master_Sequential_Receive_IT(handle, address, (uint8_t*)rx, rx_length, obj_s->XferOperation);
<> 153:fa9ff456f731 1016 }
<> 153:fa9ff456f731 1017 }
<> 153:fa9ff456f731 1018 else if (tx_length && rx_length) {
<> 153:fa9ff456f731 1019 /* Two steps operation, don't modify XferOperation, keep it for next step */
<> 153:fa9ff456f731 1020 if ((obj_s->XferOperation == I2C_FIRST_AND_LAST_FRAME) ||
<> 153:fa9ff456f731 1021 (obj_s->XferOperation == I2C_LAST_FRAME)) {
<> 153:fa9ff456f731 1022 HAL_I2C_Master_Sequential_Transmit_IT(handle, address, (uint8_t*)tx, tx_length, I2C_FIRST_FRAME);
<> 153:fa9ff456f731 1023 } else if ((obj_s->XferOperation == I2C_FIRST_FRAME) ||
<> 153:fa9ff456f731 1024 (obj_s->XferOperation == I2C_NEXT_FRAME)) {
<> 153:fa9ff456f731 1025 HAL_I2C_Master_Sequential_Transmit_IT(handle, address, (uint8_t*)tx, tx_length, I2C_NEXT_FRAME);
<> 153:fa9ff456f731 1026 }
<> 153:fa9ff456f731 1027 }
<> 153:fa9ff456f731 1028 }
<> 153:fa9ff456f731 1029
<> 153:fa9ff456f731 1030
<> 153:fa9ff456f731 1031 uint32_t i2c_irq_handler_asynch(i2c_t *obj) {
<> 153:fa9ff456f731 1032
<> 153:fa9ff456f731 1033 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 1034 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 153:fa9ff456f731 1035
<> 153:fa9ff456f731 1036 HAL_I2C_EV_IRQHandler(handle);
<> 153:fa9ff456f731 1037 HAL_I2C_ER_IRQHandler(handle);
<> 153:fa9ff456f731 1038
<> 153:fa9ff456f731 1039 /* Return I2C event status */
<> 153:fa9ff456f731 1040 return (obj_s->event & obj_s->available_events);
<> 153:fa9ff456f731 1041 }
<> 153:fa9ff456f731 1042
<> 153:fa9ff456f731 1043 uint8_t i2c_active(i2c_t *obj) {
<> 153:fa9ff456f731 1044
<> 153:fa9ff456f731 1045 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 1046 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 153:fa9ff456f731 1047
<> 153:fa9ff456f731 1048 if (handle->State == HAL_I2C_STATE_READY) {
<> 153:fa9ff456f731 1049 return 0;
<> 153:fa9ff456f731 1050 }
<> 153:fa9ff456f731 1051 else {
<> 153:fa9ff456f731 1052 return 1;
<> 153:fa9ff456f731 1053 }
<> 153:fa9ff456f731 1054 }
<> 153:fa9ff456f731 1055
<> 153:fa9ff456f731 1056 void i2c_abort_asynch(i2c_t *obj) {
<> 153:fa9ff456f731 1057
<> 153:fa9ff456f731 1058 struct i2c_s *obj_s = I2C_S(obj);
<> 153:fa9ff456f731 1059 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 153:fa9ff456f731 1060
<> 153:fa9ff456f731 1061 /* Abort HAL requires DevAddress, but is not used. Use Dummy */
<> 153:fa9ff456f731 1062 uint16_t Dummy_DevAddress = 0x00;
<> 153:fa9ff456f731 1063
<> 153:fa9ff456f731 1064 HAL_I2C_Master_Abort_IT(handle, Dummy_DevAddress);
<> 153:fa9ff456f731 1065 }
<> 153:fa9ff456f731 1066
<> 153:fa9ff456f731 1067 #endif // DEVICE_I2C_ASYNCH
<> 153:fa9ff456f731 1068
<> 153:fa9ff456f731 1069 #endif // DEVICE_I2C