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