mbed library sources. With a patch for the can_api
Fork of mbed-dev by
targets/TARGET_STM/TARGET_STM32F3/i2c_api.c@151:91825d030f9b, 2016-11-14 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 149:156823d33999 | 1 | /* mbed Microcontroller Library |
<> | 149:156823d33999 | 2 | ******************************************************************************* |
<> | 149:156823d33999 | 3 | * Copyright (c) 2014, 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)0x4000) |
<> | 149:156823d33999 | 43 | #define LONG_TIMEOUT ((int)0x8000) |
<> | 149:156823d33999 | 44 | |
<> | 149:156823d33999 | 45 | I2C_HandleTypeDef I2cHandle; |
<> | 149:156823d33999 | 46 | |
<> | 149:156823d33999 | 47 | int i2c1_inited = 0; |
<> | 149:156823d33999 | 48 | int i2c2_inited = 0; |
<> | 149:156823d33999 | 49 | int i2c3_inited = 0; |
<> | 149:156823d33999 | 50 | |
<> | 149:156823d33999 | 51 | void i2c_init(i2c_t *obj, PinName sda, PinName scl) |
<> | 149:156823d33999 | 52 | { |
<> | 149:156823d33999 | 53 | // Determine the I2C to use |
<> | 149:156823d33999 | 54 | I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); |
<> | 149:156823d33999 | 55 | I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); |
<> | 149:156823d33999 | 56 | |
<> | 149:156823d33999 | 57 | obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); |
<> | 149:156823d33999 | 58 | MBED_ASSERT(obj->i2c != (I2CName)NC); |
<> | 149:156823d33999 | 59 | |
<> | 149:156823d33999 | 60 | // Enable I2C clock and pinout if not done |
<> | 149:156823d33999 | 61 | if ((obj->i2c == I2C_1) && !i2c1_inited) { |
<> | 149:156823d33999 | 62 | i2c1_inited = 1; |
<> | 149:156823d33999 | 63 | __HAL_RCC_I2C1_CONFIG(RCC_I2C1CLKSOURCE_SYSCLK); |
<> | 149:156823d33999 | 64 | __I2C1_CLK_ENABLE(); |
<> | 149:156823d33999 | 65 | // Configure I2C1 pins |
<> | 149:156823d33999 | 66 | pinmap_pinout(sda, PinMap_I2C_SDA); |
<> | 149:156823d33999 | 67 | pinmap_pinout(scl, PinMap_I2C_SCL); |
<> | 149:156823d33999 | 68 | pin_mode(sda, OpenDrain); |
<> | 149:156823d33999 | 69 | pin_mode(scl, OpenDrain); |
<> | 149:156823d33999 | 70 | } |
<> | 149:156823d33999 | 71 | |
<> | 149:156823d33999 | 72 | #if defined(I2C2_BASE) |
<> | 149:156823d33999 | 73 | if ((obj->i2c == I2C_2) && !i2c2_inited) { |
<> | 149:156823d33999 | 74 | i2c2_inited = 1; |
<> | 149:156823d33999 | 75 | __I2C2_CLK_ENABLE(); |
<> | 149:156823d33999 | 76 | // Configure I2C2 pins |
<> | 149:156823d33999 | 77 | pinmap_pinout(sda, PinMap_I2C_SDA); |
<> | 149:156823d33999 | 78 | pinmap_pinout(scl, PinMap_I2C_SCL); |
<> | 149:156823d33999 | 79 | pin_mode(sda, OpenDrain); |
<> | 149:156823d33999 | 80 | pin_mode(scl, OpenDrain); |
<> | 149:156823d33999 | 81 | } |
<> | 149:156823d33999 | 82 | #endif |
<> | 149:156823d33999 | 83 | |
<> | 149:156823d33999 | 84 | #if defined(I2C3_BASE) |
<> | 149:156823d33999 | 85 | if ((obj->i2c == I2C_3) && !i2c3_inited) { |
<> | 149:156823d33999 | 86 | i2c3_inited = 1; |
<> | 149:156823d33999 | 87 | __I2C3_CLK_ENABLE(); |
<> | 149:156823d33999 | 88 | // Configure I2C3 pins |
<> | 149:156823d33999 | 89 | pinmap_pinout(sda, PinMap_I2C_SDA); |
<> | 149:156823d33999 | 90 | pinmap_pinout(scl, PinMap_I2C_SCL); |
<> | 149:156823d33999 | 91 | pin_mode(sda, OpenDrain); |
<> | 149:156823d33999 | 92 | pin_mode(scl, OpenDrain); |
<> | 149:156823d33999 | 93 | } |
<> | 149:156823d33999 | 94 | #endif |
<> | 149:156823d33999 | 95 | |
<> | 149:156823d33999 | 96 | // Reset to clear pending flags if any |
<> | 149:156823d33999 | 97 | i2c_reset(obj); |
<> | 149:156823d33999 | 98 | |
<> | 149:156823d33999 | 99 | // I2C configuration |
<> | 149:156823d33999 | 100 | i2c_frequency(obj, 100000); // 100 kHz per default |
<> | 149:156823d33999 | 101 | } |
<> | 149:156823d33999 | 102 | |
<> | 149:156823d33999 | 103 | void i2c_frequency(i2c_t *obj, int hz) |
<> | 149:156823d33999 | 104 | { |
<> | 149:156823d33999 | 105 | uint32_t tim = 0; |
<> | 149:156823d33999 | 106 | |
<> | 149:156823d33999 | 107 | MBED_ASSERT((hz == 100000) || (hz == 400000) || (hz == 1000000)); |
<> | 149:156823d33999 | 108 | |
<> | 149:156823d33999 | 109 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
<> | 149:156823d33999 | 110 | int timeout; |
<> | 149:156823d33999 | 111 | |
<> | 149:156823d33999 | 112 | // wait before init |
<> | 149:156823d33999 | 113 | timeout = LONG_TIMEOUT; |
<> | 149:156823d33999 | 114 | while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0)); |
<> | 149:156823d33999 | 115 | |
<> | 149:156823d33999 | 116 | /* |
<> | 149:156823d33999 | 117 | Values calculated with I2C_Timing_Configuration_V1.0.1.xls file (see AN4235) |
<> | 149:156823d33999 | 118 | * Standard mode (up to 100 kHz) |
<> | 149:156823d33999 | 119 | * Fast Mode (up to 400 kHz) |
<> | 149:156823d33999 | 120 | * Fast Mode Plus (up to 1 MHz) |
<> | 149:156823d33999 | 121 | Below values obtained with: |
<> | 149:156823d33999 | 122 | - I2C clock source = 64 MHz (System Clock w/ HSI) or 72 (System Clock w/ HSE) |
<> | 149:156823d33999 | 123 | - Analog filter delay = ON |
<> | 149:156823d33999 | 124 | - Digital filter coefficient = 0 |
<> | 149:156823d33999 | 125 | */ |
<> | 149:156823d33999 | 126 | if (SystemCoreClock == 64000000) { |
<> | 149:156823d33999 | 127 | switch (hz) { |
<> | 149:156823d33999 | 128 | case 100000: |
<> | 149:156823d33999 | 129 | tim = 0x10B17DB4; // Standard mode with Rise time = 120ns, Fall time = 120ns |
<> | 149:156823d33999 | 130 | break; |
<> | 149:156823d33999 | 131 | case 400000: |
<> | 149:156823d33999 | 132 | tim = 0x00E22163; // Fast Mode with Rise time = 120ns, Fall time = 120ns |
<> | 149:156823d33999 | 133 | break; |
<> | 149:156823d33999 | 134 | case 1000000: |
<> | 149:156823d33999 | 135 | tim = 0x00A00D1E; // Fast Mode Plus with Rise time = 120ns, Fall time = 10ns |
<> | 149:156823d33999 | 136 | break; |
<> | 149:156823d33999 | 137 | default: |
<> | 149:156823d33999 | 138 | break; |
<> | 149:156823d33999 | 139 | } |
<> | 149:156823d33999 | 140 | } else if (SystemCoreClock == 72000000) { |
<> | 149:156823d33999 | 141 | switch (hz) { |
<> | 149:156823d33999 | 142 | case 100000: |
<> | 149:156823d33999 | 143 | tim = 0x10D28DCB; // Standard mode with Rise time = 120ns, Fall time = 120ns |
<> | 149:156823d33999 | 144 | break; |
<> | 149:156823d33999 | 145 | case 400000: |
<> | 149:156823d33999 | 146 | tim = 0x00F32571; // Fast Mode with Rise time = 120ns, Fall time = 120ns |
<> | 149:156823d33999 | 147 | break; |
<> | 149:156823d33999 | 148 | case 1000000: |
<> | 149:156823d33999 | 149 | tim = 0x00C00D24; // Fast Mode Plus with Rise time = 120ns, Fall time = 10ns |
<> | 149:156823d33999 | 150 | break; |
<> | 149:156823d33999 | 151 | default: |
<> | 149:156823d33999 | 152 | break; |
<> | 149:156823d33999 | 153 | } |
<> | 149:156823d33999 | 154 | } |
<> | 149:156823d33999 | 155 | |
<> | 149:156823d33999 | 156 | // Enable the Fast Mode Plus capability |
<> | 149:156823d33999 | 157 | if (hz == 1000000) { |
<> | 149:156823d33999 | 158 | if (obj->i2c == I2C_1) { |
<> | 149:156823d33999 | 159 | __HAL_SYSCFG_FASTMODEPLUS_ENABLE(HAL_SYSCFG_FASTMODEPLUS_I2C1); |
<> | 149:156823d33999 | 160 | } |
<> | 149:156823d33999 | 161 | #if defined(I2C2_BASE) |
<> | 149:156823d33999 | 162 | if (obj->i2c == I2C_2) { |
<> | 149:156823d33999 | 163 | __HAL_SYSCFG_FASTMODEPLUS_ENABLE(HAL_SYSCFG_FASTMODEPLUS_I2C2); |
<> | 149:156823d33999 | 164 | } |
<> | 149:156823d33999 | 165 | #endif |
<> | 149:156823d33999 | 166 | #if defined(I2C3_BASE) |
<> | 149:156823d33999 | 167 | if (obj->i2c == I2C_3) { |
<> | 149:156823d33999 | 168 | __HAL_SYSCFG_FASTMODEPLUS_ENABLE(HAL_SYSCFG_FASTMODEPLUS_I2C3); |
<> | 149:156823d33999 | 169 | } |
<> | 149:156823d33999 | 170 | #endif |
<> | 149:156823d33999 | 171 | } |
<> | 149:156823d33999 | 172 | |
<> | 149:156823d33999 | 173 | // I2C configuration |
<> | 149:156823d33999 | 174 | I2cHandle.Init.Timing = tim; |
<> | 149:156823d33999 | 175 | I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; |
<> | 149:156823d33999 | 176 | I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; |
<> | 149:156823d33999 | 177 | I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; |
<> | 149:156823d33999 | 178 | I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; |
<> | 149:156823d33999 | 179 | I2cHandle.Init.OwnAddress1 = 0; |
<> | 149:156823d33999 | 180 | I2cHandle.Init.OwnAddress2 = 0; |
<> | 149:156823d33999 | 181 | I2cHandle.Init.OwnAddress2Masks = I2C_OA2_NOMASK; |
<> | 149:156823d33999 | 182 | HAL_I2C_Init(&I2cHandle); |
<> | 149:156823d33999 | 183 | } |
<> | 149:156823d33999 | 184 | |
<> | 149:156823d33999 | 185 | inline int i2c_start(i2c_t *obj) |
<> | 149:156823d33999 | 186 | { |
<> | 149:156823d33999 | 187 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
<> | 149:156823d33999 | 188 | int timeout; |
<> | 149:156823d33999 | 189 | |
<> | 149:156823d33999 | 190 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
<> | 149:156823d33999 | 191 | |
<> | 149:156823d33999 | 192 | // Clear Acknowledge failure flag |
<> | 149:156823d33999 | 193 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF); |
<> | 149:156823d33999 | 194 | |
<> | 149:156823d33999 | 195 | // Wait the STOP condition has been previously correctly sent |
<> | 149:156823d33999 | 196 | timeout = FLAG_TIMEOUT; |
<> | 149:156823d33999 | 197 | while ((i2c->CR2 & I2C_CR2_STOP) == I2C_CR2_STOP){ |
<> | 149:156823d33999 | 198 | if ((timeout--) == 0) { |
<> | 149:156823d33999 | 199 | return 1; |
<> | 149:156823d33999 | 200 | } |
<> | 149:156823d33999 | 201 | } |
<> | 149:156823d33999 | 202 | |
<> | 149:156823d33999 | 203 | // Generate the START condition |
<> | 149:156823d33999 | 204 | i2c->CR2 |= I2C_CR2_START; |
<> | 149:156823d33999 | 205 | |
<> | 149:156823d33999 | 206 | // Wait the START condition has been correctly sent |
<> | 149:156823d33999 | 207 | timeout = FLAG_TIMEOUT; |
<> | 149:156823d33999 | 208 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == RESET) { |
<> | 149:156823d33999 | 209 | if ((timeout--) == 0) { |
<> | 149:156823d33999 | 210 | return 1; |
<> | 149:156823d33999 | 211 | } |
<> | 149:156823d33999 | 212 | } |
<> | 149:156823d33999 | 213 | |
<> | 149:156823d33999 | 214 | return 0; |
<> | 149:156823d33999 | 215 | } |
<> | 149:156823d33999 | 216 | |
<> | 149:156823d33999 | 217 | inline int i2c_stop(i2c_t *obj) |
<> | 149:156823d33999 | 218 | { |
<> | 149:156823d33999 | 219 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
<> | 149:156823d33999 | 220 | |
<> | 149:156823d33999 | 221 | // Generate the STOP condition |
<> | 149:156823d33999 | 222 | i2c->CR2 |= I2C_CR2_STOP; |
<> | 149:156823d33999 | 223 | |
<> | 149:156823d33999 | 224 | return 0; |
<> | 149:156823d33999 | 225 | } |
<> | 149:156823d33999 | 226 | |
<> | 149:156823d33999 | 227 | int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) |
<> | 149:156823d33999 | 228 | { |
<> | 149:156823d33999 | 229 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
<> | 149:156823d33999 | 230 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
<> | 149:156823d33999 | 231 | int timeout; |
<> | 149:156823d33999 | 232 | int count; |
<> | 149:156823d33999 | 233 | int value; |
<> | 149:156823d33999 | 234 | |
<> | 149:156823d33999 | 235 | /* update CR2 register */ |
<> | 149:156823d33999 | 236 | i2c->CR2 = (i2c->CR2 & (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP))) |
<> | 149:156823d33999 | 237 | | (uint32_t)(((uint32_t)address & I2C_CR2_SADD) | (((uint32_t)length << 16) & I2C_CR2_NBYTES) | (uint32_t)I2C_SOFTEND_MODE | (uint32_t)I2C_GENERATE_START_READ); |
<> | 149:156823d33999 | 238 | |
<> | 149:156823d33999 | 239 | // Read all bytes |
<> | 149:156823d33999 | 240 | for (count = 0; count < length; count++) { |
<> | 149:156823d33999 | 241 | value = i2c_byte_read(obj, 0); |
<> | 149:156823d33999 | 242 | data[count] = (char)value; |
<> | 149:156823d33999 | 243 | } |
<> | 149:156823d33999 | 244 | |
<> | 149:156823d33999 | 245 | // Wait transfer complete |
<> | 149:156823d33999 | 246 | timeout = LONG_TIMEOUT; |
<> | 149:156823d33999 | 247 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) { |
<> | 149:156823d33999 | 248 | timeout--; |
<> | 149:156823d33999 | 249 | if (timeout == 0) { |
<> | 149:156823d33999 | 250 | return -1; |
<> | 149:156823d33999 | 251 | } |
<> | 149:156823d33999 | 252 | } |
<> | 149:156823d33999 | 253 | |
<> | 149:156823d33999 | 254 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC); |
<> | 149:156823d33999 | 255 | |
<> | 149:156823d33999 | 256 | // If not repeated start, send stop. |
<> | 149:156823d33999 | 257 | if (stop) { |
<> | 149:156823d33999 | 258 | i2c_stop(obj); |
<> | 149:156823d33999 | 259 | /* Wait until STOPF flag is set */ |
<> | 149:156823d33999 | 260 | timeout = FLAG_TIMEOUT; |
<> | 149:156823d33999 | 261 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) { |
<> | 149:156823d33999 | 262 | timeout--; |
<> | 149:156823d33999 | 263 | if (timeout == 0) { |
<> | 149:156823d33999 | 264 | return -1; |
<> | 149:156823d33999 | 265 | } |
<> | 149:156823d33999 | 266 | } |
<> | 149:156823d33999 | 267 | /* Clear STOP Flag */ |
<> | 149:156823d33999 | 268 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF); |
<> | 149:156823d33999 | 269 | } |
<> | 149:156823d33999 | 270 | |
<> | 149:156823d33999 | 271 | return length; |
<> | 149:156823d33999 | 272 | } |
<> | 149:156823d33999 | 273 | |
<> | 149:156823d33999 | 274 | int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) |
<> | 149:156823d33999 | 275 | { |
<> | 149:156823d33999 | 276 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
<> | 149:156823d33999 | 277 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
<> | 149:156823d33999 | 278 | int timeout; |
<> | 149:156823d33999 | 279 | int count; |
<> | 149:156823d33999 | 280 | |
<> | 149:156823d33999 | 281 | /* update CR2 register */ |
<> | 149:156823d33999 | 282 | i2c->CR2 = (i2c->CR2 & (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP))) |
<> | 149:156823d33999 | 283 | | (uint32_t)(((uint32_t)address & I2C_CR2_SADD) | (((uint32_t)length << 16) & I2C_CR2_NBYTES) | (uint32_t)I2C_SOFTEND_MODE | (uint32_t)I2C_GENERATE_START_WRITE); |
<> | 149:156823d33999 | 284 | |
<> | 149:156823d33999 | 285 | for (count = 0; count < length; count++) { |
<> | 149:156823d33999 | 286 | i2c_byte_write(obj, data[count]); |
<> | 149:156823d33999 | 287 | } |
<> | 149:156823d33999 | 288 | |
<> | 149:156823d33999 | 289 | // Wait transfer complete |
<> | 149:156823d33999 | 290 | timeout = FLAG_TIMEOUT; |
<> | 149:156823d33999 | 291 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) { |
<> | 149:156823d33999 | 292 | timeout--; |
<> | 149:156823d33999 | 293 | if (timeout == 0) { |
<> | 149:156823d33999 | 294 | return -1; |
<> | 149:156823d33999 | 295 | } |
<> | 149:156823d33999 | 296 | } |
<> | 149:156823d33999 | 297 | |
<> | 149:156823d33999 | 298 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC); |
<> | 149:156823d33999 | 299 | |
<> | 149:156823d33999 | 300 | // If not repeated start, send stop. |
<> | 149:156823d33999 | 301 | if (stop) { |
<> | 149:156823d33999 | 302 | i2c_stop(obj); |
<> | 149:156823d33999 | 303 | /* Wait until STOPF flag is set */ |
<> | 149:156823d33999 | 304 | timeout = FLAG_TIMEOUT; |
<> | 149:156823d33999 | 305 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) { |
<> | 149:156823d33999 | 306 | timeout--; |
<> | 149:156823d33999 | 307 | if (timeout == 0) { |
<> | 149:156823d33999 | 308 | return -1; |
<> | 149:156823d33999 | 309 | } |
<> | 149:156823d33999 | 310 | } |
<> | 149:156823d33999 | 311 | /* Clear STOP Flag */ |
<> | 149:156823d33999 | 312 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF); |
<> | 149:156823d33999 | 313 | } |
<> | 149:156823d33999 | 314 | |
<> | 149:156823d33999 | 315 | return count; |
<> | 149:156823d33999 | 316 | } |
<> | 149:156823d33999 | 317 | |
<> | 149:156823d33999 | 318 | int i2c_byte_read(i2c_t *obj, int last) |
<> | 149:156823d33999 | 319 | { |
<> | 149:156823d33999 | 320 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
<> | 149:156823d33999 | 321 | int timeout; |
<> | 149:156823d33999 | 322 | |
<> | 149:156823d33999 | 323 | // Wait until the byte is received |
<> | 149:156823d33999 | 324 | timeout = FLAG_TIMEOUT; |
<> | 149:156823d33999 | 325 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) { |
<> | 149:156823d33999 | 326 | if ((timeout--) == 0) { |
<> | 149:156823d33999 | 327 | return -1; |
<> | 149:156823d33999 | 328 | } |
<> | 149:156823d33999 | 329 | } |
<> | 149:156823d33999 | 330 | |
<> | 149:156823d33999 | 331 | return (int)i2c->RXDR; |
<> | 149:156823d33999 | 332 | } |
<> | 149:156823d33999 | 333 | |
<> | 149:156823d33999 | 334 | int i2c_byte_write(i2c_t *obj, int data) |
<> | 149:156823d33999 | 335 | { |
<> | 149:156823d33999 | 336 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
<> | 149:156823d33999 | 337 | int timeout; |
<> | 149:156823d33999 | 338 | |
<> | 149:156823d33999 | 339 | // Wait until the previous byte is transmitted |
<> | 149:156823d33999 | 340 | timeout = FLAG_TIMEOUT; |
<> | 149:156823d33999 | 341 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXIS) == RESET) { |
<> | 149:156823d33999 | 342 | if ((timeout--) == 0) { |
<> | 149:156823d33999 | 343 | return 0; |
<> | 149:156823d33999 | 344 | } |
<> | 149:156823d33999 | 345 | } |
<> | 149:156823d33999 | 346 | |
<> | 149:156823d33999 | 347 | i2c->TXDR = (uint8_t)data; |
<> | 149:156823d33999 | 348 | |
<> | 149:156823d33999 | 349 | return 1; |
<> | 149:156823d33999 | 350 | } |
<> | 149:156823d33999 | 351 | |
<> | 149:156823d33999 | 352 | void i2c_reset(i2c_t *obj) |
<> | 149:156823d33999 | 353 | { |
<> | 149:156823d33999 | 354 | int timeout; |
<> | 149:156823d33999 | 355 | |
<> | 149:156823d33999 | 356 | // wait before reset |
<> | 149:156823d33999 | 357 | timeout = LONG_TIMEOUT; |
<> | 149:156823d33999 | 358 | while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0)); |
<> | 149:156823d33999 | 359 | |
<> | 149:156823d33999 | 360 | __I2C1_FORCE_RESET(); |
<> | 149:156823d33999 | 361 | __I2C1_RELEASE_RESET(); |
<> | 149:156823d33999 | 362 | } |
<> | 149:156823d33999 | 363 | |
<> | 149:156823d33999 | 364 | #if DEVICE_I2CSLAVE |
<> | 149:156823d33999 | 365 | |
<> | 149:156823d33999 | 366 | void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) |
<> | 149:156823d33999 | 367 | { |
<> | 149:156823d33999 | 368 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
<> | 149:156823d33999 | 369 | uint16_t tmpreg; |
<> | 149:156823d33999 | 370 | |
<> | 149:156823d33999 | 371 | // disable |
<> | 149:156823d33999 | 372 | i2c->OAR1 &= (uint32_t)(~I2C_OAR1_OA1EN); |
<> | 149:156823d33999 | 373 | // Get the old register value |
<> | 149:156823d33999 | 374 | tmpreg = i2c->OAR1; |
<> | 149:156823d33999 | 375 | // Reset address bits |
<> | 149:156823d33999 | 376 | tmpreg &= 0xFC00; |
<> | 149:156823d33999 | 377 | // Set new address |
<> | 149:156823d33999 | 378 | tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits |
<> | 149:156823d33999 | 379 | // Store the new register value |
<> | 149:156823d33999 | 380 | i2c->OAR1 = tmpreg; |
<> | 149:156823d33999 | 381 | // enable |
<> | 149:156823d33999 | 382 | i2c->OAR1 |= I2C_OAR1_OA1EN; |
<> | 149:156823d33999 | 383 | } |
<> | 149:156823d33999 | 384 | |
<> | 149:156823d33999 | 385 | void i2c_slave_mode(i2c_t *obj, int enable_slave) |
<> | 149:156823d33999 | 386 | { |
<> | 149:156823d33999 | 387 | |
<> | 149:156823d33999 | 388 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
<> | 149:156823d33999 | 389 | uint16_t tmpreg; |
<> | 149:156823d33999 | 390 | |
<> | 149:156823d33999 | 391 | // Get the old register value |
<> | 149:156823d33999 | 392 | tmpreg = i2c->OAR1; |
<> | 149:156823d33999 | 393 | |
<> | 149:156823d33999 | 394 | // Enable / disable slave |
<> | 149:156823d33999 | 395 | if (enable_slave == 1) { |
<> | 149:156823d33999 | 396 | tmpreg |= I2C_OAR1_OA1EN; |
<> | 149:156823d33999 | 397 | } else { |
<> | 149:156823d33999 | 398 | tmpreg &= (uint32_t)(~I2C_OAR1_OA1EN); |
<> | 149:156823d33999 | 399 | } |
<> | 149:156823d33999 | 400 | |
<> | 149:156823d33999 | 401 | // Set new mode |
<> | 149:156823d33999 | 402 | i2c->OAR1 = tmpreg; |
<> | 149:156823d33999 | 403 | |
<> | 149:156823d33999 | 404 | } |
<> | 149:156823d33999 | 405 | |
<> | 149:156823d33999 | 406 | // See I2CSlave.h |
<> | 149:156823d33999 | 407 | #define NoData 0 // the slave has not been addressed |
<> | 149:156823d33999 | 408 | #define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter) |
<> | 149:156823d33999 | 409 | #define WriteGeneral 2 // the master is writing to all slave |
<> | 149:156823d33999 | 410 | #define WriteAddressed 3 // the master is writing to this slave (slave = receiver) |
<> | 149:156823d33999 | 411 | |
<> | 149:156823d33999 | 412 | int i2c_slave_receive(i2c_t *obj) |
<> | 149:156823d33999 | 413 | { |
<> | 149:156823d33999 | 414 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
<> | 149:156823d33999 | 415 | int retValue = NoData; |
<> | 149:156823d33999 | 416 | |
<> | 149:156823d33999 | 417 | if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == 1) { |
<> | 149:156823d33999 | 418 | if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == 1) { |
<> | 149:156823d33999 | 419 | if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_DIR) == 1) |
<> | 149:156823d33999 | 420 | retValue = ReadAddressed; |
<> | 149:156823d33999 | 421 | else |
<> | 149:156823d33999 | 422 | retValue = WriteAddressed; |
<> | 149:156823d33999 | 423 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_ADDR); |
<> | 149:156823d33999 | 424 | } |
<> | 149:156823d33999 | 425 | } |
<> | 149:156823d33999 | 426 | |
<> | 149:156823d33999 | 427 | return (retValue); |
<> | 149:156823d33999 | 428 | } |
<> | 149:156823d33999 | 429 | |
<> | 149:156823d33999 | 430 | int i2c_slave_read(i2c_t *obj, char *data, int length) |
<> | 149:156823d33999 | 431 | { |
<> | 149:156823d33999 | 432 | char size = 0; |
<> | 149:156823d33999 | 433 | |
<> | 149:156823d33999 | 434 | while (size < length) data[size++] = (char)i2c_byte_read(obj, 0); |
<> | 149:156823d33999 | 435 | |
<> | 149:156823d33999 | 436 | return size; |
<> | 149:156823d33999 | 437 | } |
<> | 149:156823d33999 | 438 | |
<> | 149:156823d33999 | 439 | int i2c_slave_write(i2c_t *obj, const char *data, int length) |
<> | 149:156823d33999 | 440 | { |
<> | 149:156823d33999 | 441 | char size = 0; |
<> | 149:156823d33999 | 442 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
<> | 149:156823d33999 | 443 | |
<> | 149:156823d33999 | 444 | do { |
<> | 149:156823d33999 | 445 | i2c_byte_write(obj, data[size]); |
<> | 149:156823d33999 | 446 | size++; |
<> | 149:156823d33999 | 447 | } while (size < length); |
<> | 149:156823d33999 | 448 | |
<> | 149:156823d33999 | 449 | return size; |
<> | 149:156823d33999 | 450 | } |
<> | 149:156823d33999 | 451 | |
<> | 149:156823d33999 | 452 | |
<> | 149:156823d33999 | 453 | #endif // DEVICE_I2CSLAVE |
<> | 149:156823d33999 | 454 | |
<> | 149:156823d33999 | 455 | #endif // DEVICE_I2C |