mbed library sources. With a patch for the can_api

Fork of mbed-dev by mbed official

Committer:
DangerousElectrician
Date:
Mon Nov 14 04:39:23 2016 +0000
Revision:
151:91825d030f9b
Parent:
149:156823d33999
stuff changed?

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 149:156823d33999 1 /* mbed Microcontroller Library
<> 149:156823d33999 2 *******************************************************************************
<> 149:156823d33999 3 * Copyright (c) 2015, STMicroelectronics
<> 149:156823d33999 4 * All rights reserved.
<> 149:156823d33999 5 *
<> 149:156823d33999 6 * Redistribution and use in source and binary forms, with or without
<> 149:156823d33999 7 * modification, are permitted provided that the following conditions are met:
<> 149:156823d33999 8 *
<> 149:156823d33999 9 * 1. Redistributions of source code must retain the above copyright notice,
<> 149:156823d33999 10 * this list of conditions and the following disclaimer.
<> 149:156823d33999 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
<> 149:156823d33999 12 * this list of conditions and the following disclaimer in the documentation
<> 149:156823d33999 13 * and/or other materials provided with the distribution.
<> 149:156823d33999 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
<> 149:156823d33999 15 * may be used to endorse or promote products derived from this software
<> 149:156823d33999 16 * without specific prior written permission.
<> 149:156823d33999 17 *
<> 149:156823d33999 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
<> 149:156823d33999 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
<> 149:156823d33999 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
<> 149:156823d33999 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
<> 149:156823d33999 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
<> 149:156823d33999 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
<> 149:156823d33999 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
<> 149:156823d33999 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
<> 149:156823d33999 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
<> 149:156823d33999 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<> 149:156823d33999 28 *******************************************************************************
<> 149:156823d33999 29 */
<> 149:156823d33999 30 #include "mbed_assert.h"
<> 149:156823d33999 31 #include "i2c_api.h"
<> 149:156823d33999 32
<> 149:156823d33999 33 #if DEVICE_I2C
<> 149:156823d33999 34
<> 149:156823d33999 35 #include "cmsis.h"
<> 149:156823d33999 36 #include "pinmap.h"
<> 149:156823d33999 37 #include "PeripheralPins.h"
<> 149:156823d33999 38
<> 149:156823d33999 39 /* Timeout values for flags and events waiting loops. These timeouts are
<> 149:156823d33999 40 not based on accurate values, they just guarantee that the application will
<> 149:156823d33999 41 not remain stuck if the I2C communication is corrupted. */
<> 149:156823d33999 42 #define FLAG_TIMEOUT ((int)0x1000)
<> 149:156823d33999 43 #define LONG_TIMEOUT ((int)0x8000)
<> 149:156823d33999 44
<> 149:156823d33999 45 int i2c1_inited = 0;
<> 149:156823d33999 46 int i2c2_inited = 0;
<> 149:156823d33999 47 int i2c3_inited = 0;
<> 149:156823d33999 48 int fmpi2c1_inited = 0;
<> 149:156823d33999 49
<> 149:156823d33999 50 #if DEVICE_I2C_ASYNCH
<> 149:156823d33999 51 #define I2C_S(obj) (struct i2c_s *) (&((obj)->i2c))
<> 149:156823d33999 52 #else
<> 149:156823d33999 53 #define I2C_S(obj) (struct i2c_s *) (obj)
<> 149:156823d33999 54 #endif
<> 149:156823d33999 55
<> 149:156823d33999 56
<> 149:156823d33999 57 void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
<> 149:156823d33999 58
<> 149:156823d33999 59 struct i2c_s *obj_s = I2C_S(obj);
<> 149:156823d33999 60
<> 149:156823d33999 61 // Determine the I2C to use
<> 149:156823d33999 62 I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
<> 149:156823d33999 63 I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
<> 149:156823d33999 64
<> 149:156823d33999 65 obj_s->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
<> 149:156823d33999 66 MBED_ASSERT(obj_s->i2c != (I2CName)NC);
<> 149:156823d33999 67
<> 149:156823d33999 68 // Enable I2C1 clock and pinout if not done
<> 149:156823d33999 69 if ((obj_s->i2c == I2C_1) && !i2c1_inited) {
<> 149:156823d33999 70 i2c1_inited = 1;
<> 149:156823d33999 71 // Configure I2C pins
<> 149:156823d33999 72 pinmap_pinout(sda, PinMap_I2C_SDA);
<> 149:156823d33999 73 pinmap_pinout(scl, PinMap_I2C_SCL);
<> 149:156823d33999 74 pin_mode(sda, PullUp);
<> 149:156823d33999 75 pin_mode(scl, PullUp);
<> 149:156823d33999 76 #if DEVICE_I2C_ASYNCH
<> 149:156823d33999 77 obj_s->event_i2cIRQ = I2C1_EV_IRQn;
<> 149:156823d33999 78 obj_s->error_i2cIRQ = I2C1_ER_IRQn;
<> 149:156823d33999 79 #endif
<> 149:156823d33999 80 __I2C1_CLK_ENABLE();
<> 149:156823d33999 81 }
<> 149:156823d33999 82 // Enable I2C2 clock and pinout if not done
<> 149:156823d33999 83 if ((obj_s->i2c == I2C_2) && !i2c2_inited) {
<> 149:156823d33999 84 i2c2_inited = 1;
<> 149:156823d33999 85 // Configure I2C pins
<> 149:156823d33999 86 pinmap_pinout(sda, PinMap_I2C_SDA);
<> 149:156823d33999 87 pinmap_pinout(scl, PinMap_I2C_SCL);
<> 149:156823d33999 88 pin_mode(sda, PullUp);
<> 149:156823d33999 89 pin_mode(scl, PullUp);
<> 149:156823d33999 90 #if DEVICE_I2C_ASYNCH
<> 149:156823d33999 91 obj_s->event_i2cIRQ = I2C2_EV_IRQn;
<> 149:156823d33999 92 obj_s->error_i2cIRQ = I2C2_ER_IRQn;
<> 149:156823d33999 93 #endif
<> 149:156823d33999 94 __I2C2_CLK_ENABLE();
<> 149:156823d33999 95 }
<> 149:156823d33999 96 #if defined I2C3_BASE
<> 149:156823d33999 97 // Enable I2C3 clock and pinout if not done
<> 149:156823d33999 98 if ((obj_s->i2c == I2C_3) && !i2c3_inited) {
<> 149:156823d33999 99 i2c3_inited = 1;
<> 149:156823d33999 100 // Configure I2C pins
<> 149:156823d33999 101 pinmap_pinout(sda, PinMap_I2C_SDA);
<> 149:156823d33999 102 pinmap_pinout(scl, PinMap_I2C_SCL);
<> 149:156823d33999 103 pin_mode(sda, PullUp);
<> 149:156823d33999 104 pin_mode(scl, PullUp);
<> 149:156823d33999 105 #if DEVICE_I2C_ASYNCH
<> 149:156823d33999 106 obj_s->event_i2cIRQ = I2C3_EV_IRQn;
<> 149:156823d33999 107 obj_s->error_i2cIRQ = I2C3_ER_IRQn;
<> 149:156823d33999 108 #endif
<> 149:156823d33999 109 __I2C3_CLK_ENABLE();
<> 149:156823d33999 110 }
<> 149:156823d33999 111 #endif
<> 149:156823d33999 112
<> 149:156823d33999 113 #if defined FMPI2C1_BASE
<> 149:156823d33999 114 // Enable I2C3 clock and pinout if not done
<> 149:156823d33999 115 if ((obj_s->i2c == FMPI2C_1) && !fmpi2c1_inited) {
<> 149:156823d33999 116 fmpi2c1_inited = 1;
<> 149:156823d33999 117 // Configure I2C pins
<> 149:156823d33999 118 pinmap_pinout(sda, PinMap_I2C_SDA);
<> 149:156823d33999 119 pinmap_pinout(scl, PinMap_I2C_SCL);
<> 149:156823d33999 120 pin_mode(sda, PullUp);
<> 149:156823d33999 121 pin_mode(scl, PullUp);
<> 149:156823d33999 122 #if DEVICE_I2C_ASYNCH
<> 149:156823d33999 123 obj_s->event_i2cIRQ = FMPI2C1_EV_IRQn;
<> 149:156823d33999 124 obj_s->error_i2cIRQ = FMPI2C1_ER_IRQn;
<> 149:156823d33999 125 #endif
<> 149:156823d33999 126 __HAL_RCC_FMPI2C1_CLK_ENABLE();
<> 149:156823d33999 127 }
<> 149:156823d33999 128 #endif
<> 149:156823d33999 129
<> 149:156823d33999 130 // Reset to clear pending flags if any
<> 149:156823d33999 131 i2c_reset(obj);
<> 149:156823d33999 132
<> 149:156823d33999 133 // I2C configuration
<> 149:156823d33999 134 i2c_frequency(obj, 100000); // 100 kHz per default
<> 149:156823d33999 135
<> 149:156823d33999 136 #if DEVICE_I2CSLAVE
<> 149:156823d33999 137 // I2C master by default
<> 149:156823d33999 138 obj_s->slave = 0;
<> 149:156823d33999 139 #endif
<> 149:156823d33999 140
<> 149:156823d33999 141 #if DEVICE_I2C_ASYNCH
<> 149:156823d33999 142 // I2C Xfer operation init
<> 149:156823d33999 143 obj_s->XferOperation = I2C_FIRST_AND_LAST_FRAME;
<> 149:156823d33999 144 #endif
<> 149:156823d33999 145 }
<> 149:156823d33999 146
<> 149:156823d33999 147 void i2c_frequency(i2c_t *obj, int hz)
<> 149:156823d33999 148 {
<> 149:156823d33999 149
<> 149:156823d33999 150 int timeout;
<> 149:156823d33999 151 struct i2c_s *obj_s = I2C_S(obj);
<> 149:156823d33999 152 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 149:156823d33999 153
<> 149:156823d33999 154 MBED_ASSERT((hz > 0) && (hz <= 400000));
<> 149:156823d33999 155
<> 149:156823d33999 156 // wait before init
<> 149:156823d33999 157 timeout = LONG_TIMEOUT;
<> 149:156823d33999 158 while ((__HAL_I2C_GET_FLAG(handle, I2C_FLAG_BUSY)) && (timeout-- != 0));
<> 149:156823d33999 159
<> 149:156823d33999 160 // I2C configuration
<> 149:156823d33999 161 handle->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
<> 149:156823d33999 162 handle->Init.ClockSpeed = hz;
<> 149:156823d33999 163 handle->Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
<> 149:156823d33999 164 handle->Init.DutyCycle = I2C_DUTYCYCLE_2;
<> 149:156823d33999 165 handle->Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
<> 149:156823d33999 166 handle->Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
<> 149:156823d33999 167 handle->Init.OwnAddress1 = 0;
<> 149:156823d33999 168 handle->Init.OwnAddress2 = 0;
<> 149:156823d33999 169 HAL_I2C_Init(handle);
<> 149:156823d33999 170
<> 149:156823d33999 171 #if DEVICE_I2CSLAVE
<> 149:156823d33999 172 if (obj_s->slave) {
<> 149:156823d33999 173 /* Enable Address Acknowledge */
<> 149:156823d33999 174 handle->Instance->CR1 |= I2C_CR1_ACK;
<> 149:156823d33999 175 }
<> 149:156823d33999 176 #endif
<> 149:156823d33999 177
<> 149:156823d33999 178 }
<> 149:156823d33999 179
<> 149:156823d33999 180 inline int i2c_start(i2c_t *obj) {
<> 149:156823d33999 181
<> 149:156823d33999 182 int timeout;
<> 149:156823d33999 183 struct i2c_s *obj_s = I2C_S(obj);
<> 149:156823d33999 184 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 149:156823d33999 185
<> 149:156823d33999 186 // Clear Acknowledge failure flag
<> 149:156823d33999 187 __HAL_I2C_CLEAR_FLAG(handle, I2C_FLAG_AF);
<> 149:156823d33999 188
<> 149:156823d33999 189 // Wait the STOP condition has been previously correctly sent
<> 149:156823d33999 190 // This timeout can be avoid in some specific cases by simply clearing the STOP bit
<> 149:156823d33999 191 timeout = FLAG_TIMEOUT;
<> 149:156823d33999 192 while ((handle->Instance->CR1 & I2C_CR1_STOP) == I2C_CR1_STOP) {
<> 149:156823d33999 193 if ((timeout--) == 0) {
<> 149:156823d33999 194 return 1;
<> 149:156823d33999 195 }
<> 149:156823d33999 196 }
<> 149:156823d33999 197
<> 149:156823d33999 198 // Generate the START condition
<> 149:156823d33999 199 handle->Instance->CR1 |= I2C_CR1_START;
<> 149:156823d33999 200
<> 149:156823d33999 201 // Wait the START condition has been correctly sent
<> 149:156823d33999 202 timeout = FLAG_TIMEOUT;
<> 149:156823d33999 203 while (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_SB) == RESET) {
<> 149:156823d33999 204 if ((timeout--) == 0) {
<> 149:156823d33999 205 return 1;
<> 149:156823d33999 206 }
<> 149:156823d33999 207 }
<> 149:156823d33999 208
<> 149:156823d33999 209 return 0;
<> 149:156823d33999 210 }
<> 149:156823d33999 211
<> 149:156823d33999 212 inline int i2c_stop(i2c_t *obj) {
<> 149:156823d33999 213 struct i2c_s *obj_s = I2C_S(obj);
<> 149:156823d33999 214 I2C_TypeDef *i2c = (I2C_TypeDef *)obj_s->i2c;
<> 149:156823d33999 215
<> 149:156823d33999 216 // Generate the STOP condition
<> 149:156823d33999 217 i2c->CR1 |= I2C_CR1_STOP;
<> 149:156823d33999 218
<> 149:156823d33999 219 return 0;
<> 149:156823d33999 220 }
<> 149:156823d33999 221
<> 149:156823d33999 222 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
<> 149:156823d33999 223
<> 149:156823d33999 224 int timeout;
<> 149:156823d33999 225 int count;
<> 149:156823d33999 226 int value;
<> 149:156823d33999 227 struct i2c_s *obj_s = I2C_S(obj);
<> 149:156823d33999 228 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 149:156823d33999 229
<> 149:156823d33999 230 i2c_start(obj);
<> 149:156823d33999 231
<> 149:156823d33999 232 // Wait until SB flag is set
<> 149:156823d33999 233 timeout = FLAG_TIMEOUT;
<> 149:156823d33999 234 while (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_SB) == RESET) {
<> 149:156823d33999 235 timeout--;
<> 149:156823d33999 236 if (timeout == 0) {
<> 149:156823d33999 237 return -1;
<> 149:156823d33999 238 }
<> 149:156823d33999 239 }
<> 149:156823d33999 240
<> 149:156823d33999 241 handle->Instance->DR = __HAL_I2C_7BIT_ADD_READ(address);
<> 149:156823d33999 242
<> 149:156823d33999 243 // Wait address is acknowledged
<> 149:156823d33999 244 timeout = FLAG_TIMEOUT;
<> 149:156823d33999 245 while (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_ADDR) == RESET) {
<> 149:156823d33999 246 timeout--;
<> 149:156823d33999 247 if (timeout == 0) {
<> 149:156823d33999 248 return -1;
<> 149:156823d33999 249 }
<> 149:156823d33999 250 }
<> 149:156823d33999 251 __HAL_I2C_CLEAR_ADDRFLAG(handle);
<> 149:156823d33999 252
<> 149:156823d33999 253 // Read all bytes except last one
<> 149:156823d33999 254 for (count = 0; count < (length - 1); count++) {
<> 149:156823d33999 255 value = i2c_byte_read(obj, 0);
<> 149:156823d33999 256 data[count] = (char)value;
<> 149:156823d33999 257 }
<> 149:156823d33999 258
<> 149:156823d33999 259 // If not repeated start, send stop.
<> 149:156823d33999 260 // Warning: must be done BEFORE the data is read.
<> 149:156823d33999 261 if (stop) {
<> 149:156823d33999 262 i2c_stop(obj);
<> 149:156823d33999 263 }
<> 149:156823d33999 264
<> 149:156823d33999 265 // Read the last byte
<> 149:156823d33999 266 value = i2c_byte_read(obj, 1);
<> 149:156823d33999 267 data[count] = (char)value;
<> 149:156823d33999 268
<> 149:156823d33999 269 return length;
<> 149:156823d33999 270 }
<> 149:156823d33999 271
<> 149:156823d33999 272 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
<> 149:156823d33999 273
<> 149:156823d33999 274 int timeout;
<> 149:156823d33999 275 int count;
<> 149:156823d33999 276 struct i2c_s *obj_s = I2C_S(obj);
<> 149:156823d33999 277 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 149:156823d33999 278
<> 149:156823d33999 279 i2c_start(obj);
<> 149:156823d33999 280
<> 149:156823d33999 281 // Wait until SB flag is set
<> 149:156823d33999 282 timeout = FLAG_TIMEOUT;
<> 149:156823d33999 283 while (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_SB) == RESET) {
<> 149:156823d33999 284 timeout--;
<> 149:156823d33999 285 if (timeout == 0) {
<> 149:156823d33999 286 return -1;
<> 149:156823d33999 287 }
<> 149:156823d33999 288 }
<> 149:156823d33999 289
<> 149:156823d33999 290 handle->Instance->DR = __HAL_I2C_7BIT_ADD_WRITE(address);
<> 149:156823d33999 291
<> 149:156823d33999 292 // Wait address is acknowledged
<> 149:156823d33999 293 timeout = FLAG_TIMEOUT;
<> 149:156823d33999 294 while (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_ADDR) == RESET) {
<> 149:156823d33999 295 timeout--;
<> 149:156823d33999 296 if (timeout == 0) {
<> 149:156823d33999 297 return -1;
<> 149:156823d33999 298 }
<> 149:156823d33999 299 }
<> 149:156823d33999 300 __HAL_I2C_CLEAR_ADDRFLAG(handle);
<> 149:156823d33999 301
<> 149:156823d33999 302 for (count = 0; count < length; count++) {
<> 149:156823d33999 303 if (i2c_byte_write(obj, data[count]) != 1) {
<> 149:156823d33999 304 i2c_stop(obj);
<> 149:156823d33999 305 return -1;
<> 149:156823d33999 306 }
<> 149:156823d33999 307 }
<> 149:156823d33999 308
<> 149:156823d33999 309 // If not repeated start, send stop.
<> 149:156823d33999 310 if (stop) {
<> 149:156823d33999 311 i2c_stop(obj);
<> 149:156823d33999 312 }
<> 149:156823d33999 313
<> 149:156823d33999 314 return count;
<> 149:156823d33999 315 }
<> 149:156823d33999 316
<> 149:156823d33999 317 int i2c_byte_read(i2c_t *obj, int last) {
<> 149:156823d33999 318
<> 149:156823d33999 319 int timeout;
<> 149:156823d33999 320 struct i2c_s *obj_s = I2C_S(obj);
<> 149:156823d33999 321 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 149:156823d33999 322
<> 149:156823d33999 323 if (last) {
<> 149:156823d33999 324 // Don't acknowledge the last byte
<> 149:156823d33999 325 handle->Instance->CR1 &= ~I2C_CR1_ACK;
<> 149:156823d33999 326 } else {
<> 149:156823d33999 327 // Acknowledge the byte
<> 149:156823d33999 328 handle->Instance->CR1 |= I2C_CR1_ACK;
<> 149:156823d33999 329 }
<> 149:156823d33999 330
<> 149:156823d33999 331 // Wait until the byte is received
<> 149:156823d33999 332 timeout = FLAG_TIMEOUT;
<> 149:156823d33999 333 while (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_RXNE) == RESET) {
<> 149:156823d33999 334 if ((timeout--) == 0) {
<> 149:156823d33999 335 return -1;
<> 149:156823d33999 336 }
<> 149:156823d33999 337 }
<> 149:156823d33999 338
<> 149:156823d33999 339 return (int)handle->Instance->DR;
<> 149:156823d33999 340 }
<> 149:156823d33999 341
<> 149:156823d33999 342 int i2c_byte_write(i2c_t *obj, int data) {
<> 149:156823d33999 343
<> 149:156823d33999 344 int timeout;
<> 149:156823d33999 345 struct i2c_s *obj_s = I2C_S(obj);
<> 149:156823d33999 346 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 149:156823d33999 347
<> 149:156823d33999 348 handle->Instance->DR = (uint8_t)data;
<> 149:156823d33999 349
<> 149:156823d33999 350 // Wait until the byte (might be the address) is transmitted
<> 149:156823d33999 351 timeout = FLAG_TIMEOUT;
<> 149:156823d33999 352 while ((__HAL_I2C_GET_FLAG(handle, I2C_FLAG_TXE) == RESET) &&
<> 149:156823d33999 353 (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_BTF) == RESET) &&
<> 149:156823d33999 354 (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_ADDR) == RESET)) {
<> 149:156823d33999 355 if ((timeout--) == 0) {
<> 149:156823d33999 356 return 0;
<> 149:156823d33999 357 }
<> 149:156823d33999 358 }
<> 149:156823d33999 359
<> 149:156823d33999 360 if (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_ADDR) != RESET)
<> 149:156823d33999 361 {
<> 149:156823d33999 362 __HAL_I2C_CLEAR_ADDRFLAG(handle);
<> 149:156823d33999 363 }
<> 149:156823d33999 364
<> 149:156823d33999 365 return 1;
<> 149:156823d33999 366 }
<> 149:156823d33999 367
<> 149:156823d33999 368 void i2c_reset(i2c_t *obj) {
<> 149:156823d33999 369
<> 149:156823d33999 370 int timeout;
<> 149:156823d33999 371 struct i2c_s *obj_s = I2C_S(obj);
<> 149:156823d33999 372 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 149:156823d33999 373
<> 149:156823d33999 374 handle->Instance = (I2C_TypeDef *)(obj_s->i2c);
<> 149:156823d33999 375
<> 149:156823d33999 376 // wait before reset
<> 149:156823d33999 377 timeout = LONG_TIMEOUT;
<> 149:156823d33999 378 while ((__HAL_I2C_GET_FLAG(handle, I2C_FLAG_BUSY)) && (timeout-- != 0));
<> 149:156823d33999 379
<> 149:156823d33999 380 if (obj_s->i2c == I2C_1) {
<> 149:156823d33999 381 __I2C1_FORCE_RESET();
<> 149:156823d33999 382 __I2C1_RELEASE_RESET();
<> 149:156823d33999 383 }
<> 149:156823d33999 384
<> 149:156823d33999 385 if (obj_s->i2c == I2C_2) {
<> 149:156823d33999 386 __I2C2_FORCE_RESET();
<> 149:156823d33999 387 __I2C2_RELEASE_RESET();
<> 149:156823d33999 388 }
<> 149:156823d33999 389 #if defined I2C3_BASE
<> 149:156823d33999 390 if (obj_s->i2c == I2C_3) {
<> 149:156823d33999 391 __I2C3_FORCE_RESET();
<> 149:156823d33999 392 __I2C3_RELEASE_RESET();
<> 149:156823d33999 393 }
<> 149:156823d33999 394 #endif
<> 149:156823d33999 395
<> 149:156823d33999 396 #if defined FMPI2C1_BASE
<> 149:156823d33999 397 if (obj_s->i2c == FMPI2C_1) {
<> 149:156823d33999 398 __HAL_RCC_FMPI2C1_FORCE_RESET();
<> 149:156823d33999 399 __HAL_RCC_FMPI2C1_RELEASE_RESET();
<> 149:156823d33999 400 }
<> 149:156823d33999 401 #endif
<> 149:156823d33999 402 }
<> 149:156823d33999 403
<> 149:156823d33999 404 #if DEVICE_I2CSLAVE
<> 149:156823d33999 405
<> 149:156823d33999 406 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) {
<> 149:156823d33999 407
<> 149:156823d33999 408 uint16_t tmpreg = 0;
<> 149:156823d33999 409 struct i2c_s *obj_s = I2C_S(obj);
<> 149:156823d33999 410 I2C_TypeDef *i2c = (I2C_TypeDef *)obj_s->i2c;
<> 149:156823d33999 411
<> 149:156823d33999 412 // Get the old register value
<> 149:156823d33999 413 tmpreg = i2c->OAR1;
<> 149:156823d33999 414 // Reset address bits
<> 149:156823d33999 415 tmpreg &= 0xFC00;
<> 149:156823d33999 416 // Set new address
<> 149:156823d33999 417 tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits
<> 149:156823d33999 418 // Store the new register value
<> 149:156823d33999 419 i2c->OAR1 = tmpreg;
<> 149:156823d33999 420 }
<> 149:156823d33999 421
<> 149:156823d33999 422 void i2c_slave_mode(i2c_t *obj, int enable_slave) {
<> 149:156823d33999 423
<> 149:156823d33999 424 struct i2c_s *obj_s = I2C_S(obj);
<> 149:156823d33999 425 I2C_TypeDef *i2c = (I2C_TypeDef *)obj_s->i2c;
<> 149:156823d33999 426
<> 149:156823d33999 427 if (enable_slave) {
<> 149:156823d33999 428 obj_s->slave = 1;
<> 149:156823d33999 429
<> 149:156823d33999 430 /* Enable Address Acknowledge */
<> 149:156823d33999 431 i2c->CR1 |= I2C_CR1_ACK;
<> 149:156823d33999 432 }
<> 149:156823d33999 433 }
<> 149:156823d33999 434
<> 149:156823d33999 435 // See I2CSlave.h
<> 149:156823d33999 436 #define NoData 0 // the slave has not been addressed
<> 149:156823d33999 437 #define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter)
<> 149:156823d33999 438 #define WriteGeneral 2 // the master is writing to all slave
<> 149:156823d33999 439 #define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
<> 149:156823d33999 440
<> 149:156823d33999 441 int i2c_slave_receive(i2c_t *obj) {
<> 149:156823d33999 442
<> 149:156823d33999 443 struct i2c_s *obj_s = I2C_S(obj);
<> 149:156823d33999 444 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 149:156823d33999 445
<> 149:156823d33999 446 int retValue = NoData;
<> 149:156823d33999 447
<> 149:156823d33999 448 /* Reading BUSY flag before ADDR flag could clear ADDR */
<> 149:156823d33999 449 int addr = __HAL_I2C_GET_FLAG(handle, I2C_FLAG_ADDR);
<> 149:156823d33999 450
<> 149:156823d33999 451 if (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_BUSY) == 1) {
<> 149:156823d33999 452 if (addr == 1) {
<> 149:156823d33999 453 if (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_TRA) == 1) {
<> 149:156823d33999 454 retValue = ReadAddressed;
<> 149:156823d33999 455 } else {
<> 149:156823d33999 456 retValue = WriteAddressed;
<> 149:156823d33999 457 }
<> 149:156823d33999 458 __HAL_I2C_CLEAR_ADDRFLAG(handle);
<> 149:156823d33999 459 }
<> 149:156823d33999 460 }
<> 149:156823d33999 461
<> 149:156823d33999 462 return (retValue);
<> 149:156823d33999 463 }
<> 149:156823d33999 464
<> 149:156823d33999 465 int i2c_slave_read(i2c_t *obj, char *data, int length) {
<> 149:156823d33999 466
<> 149:156823d33999 467 struct i2c_s *obj_s = I2C_S(obj);
<> 149:156823d33999 468 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 149:156823d33999 469
<> 149:156823d33999 470 uint32_t Timeout;
<> 149:156823d33999 471 int size = 0;
<> 149:156823d33999 472
<> 149:156823d33999 473 while (length > 0) {
<> 149:156823d33999 474
<> 149:156823d33999 475 /* Wait until RXNE flag is set */
<> 149:156823d33999 476 // Wait until the byte is received
<> 149:156823d33999 477 Timeout = FLAG_TIMEOUT;
<> 149:156823d33999 478 while (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_RXNE) == RESET) {
<> 149:156823d33999 479 Timeout--;
<> 149:156823d33999 480 if (Timeout == 0) {
<> 149:156823d33999 481 return -1;
<> 149:156823d33999 482 }
<> 149:156823d33999 483 }
<> 149:156823d33999 484
<> 149:156823d33999 485 /* Read data from DR */
<> 149:156823d33999 486 (*data++) = handle->Instance->DR;
<> 149:156823d33999 487 length--;
<> 149:156823d33999 488 size++;
<> 149:156823d33999 489
<> 149:156823d33999 490 if ((__HAL_I2C_GET_FLAG(handle, I2C_FLAG_BTF) == SET) && (length != 0)) {
<> 149:156823d33999 491 /* Read data from DR */
<> 149:156823d33999 492 (*data++) = handle->Instance->DR;
<> 149:156823d33999 493 length--;
<> 149:156823d33999 494 size++;
<> 149:156823d33999 495 }
<> 149:156823d33999 496 }
<> 149:156823d33999 497
<> 149:156823d33999 498 /* Wait until STOP flag is set */
<> 149:156823d33999 499 Timeout = FLAG_TIMEOUT;
<> 149:156823d33999 500 while (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_STOPF) == RESET) {
<> 149:156823d33999 501 Timeout--;
<> 149:156823d33999 502 if (Timeout == 0) {
<> 149:156823d33999 503 return -1;
<> 149:156823d33999 504 }
<> 149:156823d33999 505 }
<> 149:156823d33999 506
<> 149:156823d33999 507 /* Clear STOP flag */
<> 149:156823d33999 508 __HAL_I2C_CLEAR_STOPFLAG(handle);
<> 149:156823d33999 509
<> 149:156823d33999 510 /* Wait until BUSY flag is reset */
<> 149:156823d33999 511 Timeout = FLAG_TIMEOUT;
<> 149:156823d33999 512 while (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_BUSY) == SET) {
<> 149:156823d33999 513 Timeout--;
<> 149:156823d33999 514 if (Timeout == 0) {
<> 149:156823d33999 515 return -1;
<> 149:156823d33999 516 }
<> 149:156823d33999 517 }
<> 149:156823d33999 518
<> 149:156823d33999 519 return size;
<> 149:156823d33999 520 }
<> 149:156823d33999 521
<> 149:156823d33999 522 int i2c_slave_write(i2c_t *obj, const char *data, int length) {
<> 149:156823d33999 523
<> 149:156823d33999 524 uint32_t Timeout;
<> 149:156823d33999 525 int size = 0;
<> 149:156823d33999 526 struct i2c_s *obj_s = I2C_S(obj);
<> 149:156823d33999 527 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 149:156823d33999 528
<> 149:156823d33999 529 while (length > 0) {
<> 149:156823d33999 530 /* Wait until TXE flag is set */
<> 149:156823d33999 531 Timeout = FLAG_TIMEOUT;
<> 149:156823d33999 532 while (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_TXE) == RESET) {
<> 149:156823d33999 533 Timeout--;
<> 149:156823d33999 534 if (Timeout == 0) {
<> 149:156823d33999 535 return -1;
<> 149:156823d33999 536 }
<> 149:156823d33999 537 }
<> 149:156823d33999 538
<> 149:156823d33999 539 /* Write data to DR */
<> 149:156823d33999 540 handle->Instance->DR = (*data++);
<> 149:156823d33999 541 length--;
<> 149:156823d33999 542 size++;
<> 149:156823d33999 543
<> 149:156823d33999 544 if ((__HAL_I2C_GET_FLAG(handle, I2C_FLAG_BTF) == SET) && (length != 0)) {
<> 149:156823d33999 545 /* Write data to DR */
<> 149:156823d33999 546 handle->Instance->DR = (*data++);
<> 149:156823d33999 547 length--;
<> 149:156823d33999 548 size++;
<> 149:156823d33999 549 }
<> 149:156823d33999 550 }
<> 149:156823d33999 551
<> 149:156823d33999 552 /* Wait until AF flag is set */
<> 149:156823d33999 553 Timeout = FLAG_TIMEOUT;
<> 149:156823d33999 554 while (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_AF) == RESET) {
<> 149:156823d33999 555 Timeout--;
<> 149:156823d33999 556 if (Timeout == 0) {
<> 149:156823d33999 557 return -1;
<> 149:156823d33999 558 }
<> 149:156823d33999 559 }
<> 149:156823d33999 560
<> 149:156823d33999 561 /* Clear AF flag */
<> 149:156823d33999 562 __HAL_I2C_CLEAR_FLAG(handle, I2C_FLAG_AF);
<> 149:156823d33999 563
<> 149:156823d33999 564
<> 149:156823d33999 565 /* Wait until BUSY flag is reset */
<> 149:156823d33999 566 Timeout = FLAG_TIMEOUT;
<> 149:156823d33999 567 while (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_BUSY) == SET) {
<> 149:156823d33999 568 Timeout--;
<> 149:156823d33999 569 if (Timeout == 0) {
<> 149:156823d33999 570 return -1;
<> 149:156823d33999 571 }
<> 149:156823d33999 572 }
<> 149:156823d33999 573
<> 149:156823d33999 574 handle->State = HAL_I2C_STATE_READY;
<> 149:156823d33999 575
<> 149:156823d33999 576 /* Process Unlocked */
<> 149:156823d33999 577 __HAL_UNLOCK(handle);
<> 149:156823d33999 578
<> 149:156823d33999 579 return size;
<> 149:156823d33999 580 }
<> 149:156823d33999 581
<> 149:156823d33999 582 #endif // DEVICE_I2CSLAVE
<> 149:156823d33999 583
<> 149:156823d33999 584 #if DEVICE_I2C_ASYNCH
<> 149:156823d33999 585
<> 149:156823d33999 586
<> 149:156823d33999 587 i2c_t *get_i2c_obj(I2C_HandleTypeDef *hi2c){
<> 149:156823d33999 588
<> 149:156823d33999 589 /* Aim of the function is to get i2c_s pointer using hi2c pointer */
<> 149:156823d33999 590 /* Highly inspired from magical linux kernel's "container_of" */
<> 149:156823d33999 591 /* (which was not directly used since not compatible with IAR toolchain) */
<> 149:156823d33999 592 struct i2c_s *obj_s;
<> 149:156823d33999 593 i2c_t *obj;
<> 149:156823d33999 594
<> 149:156823d33999 595 obj_s = (struct i2c_s *)( (char *)hi2c - offsetof(struct i2c_s,handle));
<> 149:156823d33999 596 obj = (i2c_t *)( (char *)obj_s - offsetof(i2c_t,i2c));
<> 149:156823d33999 597
<> 149:156823d33999 598 return (obj);
<> 149:156823d33999 599 }
<> 149:156823d33999 600
<> 149:156823d33999 601 void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c){
<> 149:156823d33999 602 /* Get object ptr based on handler ptr */
<> 149:156823d33999 603 i2c_t *obj = get_i2c_obj(hi2c);
<> 149:156823d33999 604 struct i2c_s *obj_s = I2C_S(obj);
<> 149:156823d33999 605
<> 149:156823d33999 606 /* Handle potential Tx/Rx use case */
<> 149:156823d33999 607 if ((obj->tx_buff.length) && (obj->rx_buff.length)) {
<> 149:156823d33999 608
<> 149:156823d33999 609 if (obj_s->stop) {
<> 149:156823d33999 610 obj_s->XferOperation = I2C_LAST_FRAME;
<> 149:156823d33999 611 }
<> 149:156823d33999 612 else {
<> 149:156823d33999 613 obj_s->XferOperation = I2C_NEXT_FRAME;
<> 149:156823d33999 614 }
<> 149:156823d33999 615
<> 149:156823d33999 616 HAL_I2C_Master_Sequential_Receive_IT(hi2c, obj_s->address, (uint8_t*)obj->rx_buff.buffer , obj->rx_buff.length, obj_s->XferOperation);
<> 149:156823d33999 617 }
<> 149:156823d33999 618 else {
<> 149:156823d33999 619 /* Set event flag */
<> 149:156823d33999 620 obj_s->event = I2C_EVENT_TRANSFER_COMPLETE;
<> 149:156823d33999 621 }
<> 149:156823d33999 622
<> 149:156823d33999 623 }
<> 149:156823d33999 624
<> 149:156823d33999 625 void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c){
<> 149:156823d33999 626 /* Get object ptr based on handler ptr */
<> 149:156823d33999 627 i2c_t *obj = get_i2c_obj(hi2c);
<> 149:156823d33999 628 struct i2c_s *obj_s = I2C_S(obj);
<> 149:156823d33999 629
<> 149:156823d33999 630 /* Set event flag */
<> 149:156823d33999 631 obj_s->event = I2C_EVENT_TRANSFER_COMPLETE;
<> 149:156823d33999 632 }
<> 149:156823d33999 633
<> 149:156823d33999 634 void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c){
<> 149:156823d33999 635 /* Get object ptr based on handler ptr */
<> 149:156823d33999 636 i2c_t *obj = get_i2c_obj(hi2c);
<> 149:156823d33999 637 struct i2c_s *obj_s = I2C_S(obj);
<> 149:156823d33999 638 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 149:156823d33999 639
<> 149:156823d33999 640 /* Disable IT. Not always done before calling macro */
<> 149:156823d33999 641 __HAL_I2C_DISABLE_IT(handle, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
<> 149:156823d33999 642
<> 149:156823d33999 643 /* Set event flag */
<> 149:156823d33999 644 obj_s->event = I2C_EVENT_ERROR;
<> 149:156823d33999 645 }
<> 149:156823d33999 646
<> 149:156823d33999 647 void HAL_I2C_AbortCpltCallback(I2C_HandleTypeDef *hi2c){
<> 149:156823d33999 648 /* Get object ptr based on handler ptr */
<> 149:156823d33999 649 i2c_t *obj = get_i2c_obj(hi2c);
<> 149:156823d33999 650 struct i2c_s *obj_s = I2C_S(obj);
<> 149:156823d33999 651 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 149:156823d33999 652
<> 149:156823d33999 653 /* Disable IT. Not always done before calling macro */
<> 149:156823d33999 654 __HAL_I2C_DISABLE_IT(handle, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
<> 149:156823d33999 655
<> 149:156823d33999 656 /* Set event flag */
<> 149:156823d33999 657 obj_s->event = I2C_EVENT_ERROR;
<> 149:156823d33999 658 }
<> 149:156823d33999 659
<> 149:156823d33999 660
<> 149:156823d33999 661
<> 149:156823d33999 662 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) {
<> 149:156823d33999 663
<> 149:156823d33999 664 // TODO: DMA usage is currently ignored by this way
<> 149:156823d33999 665 (void) hint;
<> 149:156823d33999 666
<> 149:156823d33999 667 struct i2c_s *obj_s = I2C_S(obj);
<> 149:156823d33999 668 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 149:156823d33999 669
<> 149:156823d33999 670 /* Update object */
<> 149:156823d33999 671 obj->tx_buff.buffer = (void *)tx;
<> 149:156823d33999 672 obj->tx_buff.length = tx_length;
<> 149:156823d33999 673 obj->tx_buff.pos = 0;
<> 149:156823d33999 674 obj->tx_buff.width = 8;
<> 149:156823d33999 675
<> 149:156823d33999 676 obj->rx_buff.buffer = (void *)rx;
<> 149:156823d33999 677 obj->rx_buff.length = rx_length;
<> 149:156823d33999 678 obj->rx_buff.pos = SIZE_MAX;
<> 149:156823d33999 679 obj->rx_buff.width = 8;
<> 149:156823d33999 680
<> 149:156823d33999 681 obj_s->available_events = event;
<> 149:156823d33999 682 obj_s->event = 0;
<> 149:156823d33999 683 obj_s->address = address;
<> 149:156823d33999 684 obj_s->stop = stop;
<> 149:156823d33999 685
<> 149:156823d33999 686 IRQn_Type irq_event_n = obj_s->event_i2cIRQ;
<> 149:156823d33999 687 IRQn_Type irq_error_n = obj_s->error_i2cIRQ;
<> 149:156823d33999 688
<> 149:156823d33999 689 /* Set up event IT using IRQ and handler tables */
<> 149:156823d33999 690 NVIC_SetVector(irq_event_n, handler);
<> 149:156823d33999 691 HAL_NVIC_SetPriority(irq_event_n, 0, 1);
<> 149:156823d33999 692 HAL_NVIC_EnableIRQ(irq_event_n);
<> 149:156823d33999 693
<> 149:156823d33999 694 /* Set up error IT using IRQ and handler tables */
<> 149:156823d33999 695 NVIC_SetVector(irq_error_n, handler);
<> 149:156823d33999 696 HAL_NVIC_SetPriority(irq_error_n, 0, 0);
<> 149:156823d33999 697 HAL_NVIC_EnableIRQ(irq_error_n);
<> 149:156823d33999 698
<> 149:156823d33999 699 /* Set operation step depending if stop sending required or not */
<> 149:156823d33999 700 if ((tx_length && !rx_length) || (!tx_length && rx_length)) {
<> 149:156823d33999 701 if ((obj_s->XferOperation == I2C_FIRST_AND_LAST_FRAME) ||
<> 149:156823d33999 702 (obj_s->XferOperation == I2C_LAST_FRAME)) {
<> 149:156823d33999 703 if (stop)
<> 149:156823d33999 704 obj_s->XferOperation = I2C_FIRST_AND_LAST_FRAME;
<> 149:156823d33999 705 else
<> 149:156823d33999 706 obj_s->XferOperation = I2C_FIRST_FRAME;
<> 149:156823d33999 707 } else if ((obj_s->XferOperation == I2C_FIRST_FRAME) ||
<> 149:156823d33999 708 (obj_s->XferOperation == I2C_NEXT_FRAME)) {
<> 149:156823d33999 709 if (stop)
<> 149:156823d33999 710 obj_s->XferOperation = I2C_LAST_FRAME;
<> 149:156823d33999 711 else
<> 149:156823d33999 712 obj_s->XferOperation = I2C_NEXT_FRAME;
<> 149:156823d33999 713 }
<> 149:156823d33999 714
<> 149:156823d33999 715 if (tx_length > 0) {
<> 149:156823d33999 716 HAL_I2C_Master_Sequential_Transmit_IT(handle, address, (uint8_t*)tx, tx_length, obj_s->XferOperation);
<> 149:156823d33999 717 }
<> 149:156823d33999 718 if (rx_length > 0) {
<> 149:156823d33999 719 HAL_I2C_Master_Sequential_Receive_IT(handle, address, (uint8_t*)rx, rx_length, obj_s->XferOperation);
<> 149:156823d33999 720 }
<> 149:156823d33999 721 }
<> 149:156823d33999 722 else if (tx_length && rx_length) {
<> 149:156823d33999 723 /* Two steps operation, don't modify XferOperation, keep it for next step */
<> 149:156823d33999 724 if ((obj_s->XferOperation == I2C_FIRST_AND_LAST_FRAME) ||
<> 149:156823d33999 725 (obj_s->XferOperation == I2C_LAST_FRAME)) {
<> 149:156823d33999 726 HAL_I2C_Master_Sequential_Transmit_IT(handle, address, (uint8_t*)tx, tx_length, I2C_FIRST_FRAME);
<> 149:156823d33999 727 } else if ((obj_s->XferOperation == I2C_FIRST_FRAME) ||
<> 149:156823d33999 728 (obj_s->XferOperation == I2C_NEXT_FRAME)) {
<> 149:156823d33999 729 HAL_I2C_Master_Sequential_Transmit_IT(handle, address, (uint8_t*)tx, tx_length, I2C_NEXT_FRAME);
<> 149:156823d33999 730 }
<> 149:156823d33999 731 }
<> 149:156823d33999 732 }
<> 149:156823d33999 733
<> 149:156823d33999 734
<> 149:156823d33999 735 uint32_t i2c_irq_handler_asynch(i2c_t *obj) {
<> 149:156823d33999 736
<> 149:156823d33999 737 struct i2c_s *obj_s = I2C_S(obj);
<> 149:156823d33999 738 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 149:156823d33999 739
<> 149:156823d33999 740 HAL_I2C_EV_IRQHandler(handle);
<> 149:156823d33999 741 HAL_I2C_ER_IRQHandler(handle);
<> 149:156823d33999 742
<> 149:156823d33999 743 /* Return I2C event status */
<> 149:156823d33999 744 return (obj_s->event & obj_s->available_events);
<> 149:156823d33999 745 }
<> 149:156823d33999 746
<> 149:156823d33999 747 uint8_t i2c_active(i2c_t *obj) {
<> 149:156823d33999 748
<> 149:156823d33999 749 struct i2c_s *obj_s = I2C_S(obj);
<> 149:156823d33999 750 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 149:156823d33999 751
<> 149:156823d33999 752 if (handle->State == HAL_I2C_STATE_READY) {
<> 149:156823d33999 753 return 0;
<> 149:156823d33999 754 }
<> 149:156823d33999 755 else {
<> 149:156823d33999 756 return 1;
<> 149:156823d33999 757 }
<> 149:156823d33999 758 }
<> 149:156823d33999 759
<> 149:156823d33999 760 void i2c_abort_asynch(i2c_t *obj) {
<> 149:156823d33999 761
<> 149:156823d33999 762 struct i2c_s *obj_s = I2C_S(obj);
<> 149:156823d33999 763 I2C_HandleTypeDef *handle = &(obj_s->handle);
<> 149:156823d33999 764
<> 149:156823d33999 765 /* Abort HAL requires DevAddress, but is not used. Use Dummy */
<> 149:156823d33999 766 uint16_t Dummy_DevAddress = 0x00;
<> 149:156823d33999 767
<> 149:156823d33999 768 HAL_I2C_Master_Abort_IT(handle, Dummy_DevAddress);
<> 149:156823d33999 769 }
<> 149:156823d33999 770
<> 149:156823d33999 771
<> 149:156823d33999 772 #endif // DEVICE_I2C_ASYNCH
<> 149:156823d33999 773
<> 149:156823d33999 774 #endif // DEVICE_I2C