mbed library sources for GR-PEACH rev.B.
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_DISCO_F100RB/i2c_api.c@497:d54623194236, 2015-03-26 (annotated)
- Committer:
- mbed_official
- Date:
- Thu Mar 26 13:45:12 2015 +0000
- Revision:
- 497:d54623194236
- Parent:
- 227:7bd0639b8911
Synchronized with git revision 0251fefac99a5b59beb481e845c6fbf48736bac6
Full URL: https://github.com/mbedmicro/mbed/commit/0251fefac99a5b59beb481e845c6fbf48736bac6/
DISCO_F100RB -Support HAL driver
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 126:549ba18ddd81 | 1 | /* mbed Microcontroller Library |
mbed_official | 126:549ba18ddd81 | 2 | ******************************************************************************* |
mbed_official | 126:549ba18ddd81 | 3 | * Copyright (c) 2014, STMicroelectronics |
mbed_official | 126:549ba18ddd81 | 4 | * All rights reserved. |
mbed_official | 126:549ba18ddd81 | 5 | * |
mbed_official | 126:549ba18ddd81 | 6 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 126:549ba18ddd81 | 7 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 126:549ba18ddd81 | 8 | * |
mbed_official | 126:549ba18ddd81 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 126:549ba18ddd81 | 10 | * this list of conditions and the following disclaimer. |
mbed_official | 126:549ba18ddd81 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 126:549ba18ddd81 | 12 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 126:549ba18ddd81 | 13 | * and/or other materials provided with the distribution. |
mbed_official | 126:549ba18ddd81 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 126:549ba18ddd81 | 15 | * may be used to endorse or promote products derived from this software |
mbed_official | 126:549ba18ddd81 | 16 | * without specific prior written permission. |
mbed_official | 126:549ba18ddd81 | 17 | * |
mbed_official | 126:549ba18ddd81 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 126:549ba18ddd81 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 126:549ba18ddd81 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 126:549ba18ddd81 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 126:549ba18ddd81 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 126:549ba18ddd81 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 126:549ba18ddd81 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 126:549ba18ddd81 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 126:549ba18ddd81 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 126:549ba18ddd81 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 126:549ba18ddd81 | 28 | ******************************************************************************* |
mbed_official | 126:549ba18ddd81 | 29 | */ |
mbed_official | 227:7bd0639b8911 | 30 | #include "mbed_assert.h" |
mbed_official | 126:549ba18ddd81 | 31 | #include "i2c_api.h" |
mbed_official | 126:549ba18ddd81 | 32 | |
mbed_official | 126:549ba18ddd81 | 33 | #if DEVICE_I2C |
mbed_official | 126:549ba18ddd81 | 34 | |
mbed_official | 126:549ba18ddd81 | 35 | #include "cmsis.h" |
mbed_official | 126:549ba18ddd81 | 36 | #include "pinmap.h" |
mbed_official | 497:d54623194236 | 37 | #include "PeripheralPins.h" |
mbed_official | 126:549ba18ddd81 | 38 | |
mbed_official | 126:549ba18ddd81 | 39 | /* Timeout values for flags and events waiting loops. These timeouts are |
mbed_official | 227:7bd0639b8911 | 40 | not based on accurate values, they just guarantee that the application will |
mbed_official | 227:7bd0639b8911 | 41 | not remain stuck if the I2C communication is corrupted. */ |
mbed_official | 126:549ba18ddd81 | 42 | #define FLAG_TIMEOUT ((int)0x1000) |
mbed_official | 126:549ba18ddd81 | 43 | #define LONG_TIMEOUT ((int)0x8000) |
mbed_official | 126:549ba18ddd81 | 44 | |
mbed_official | 497:d54623194236 | 45 | I2C_HandleTypeDef I2cHandle; |
mbed_official | 126:549ba18ddd81 | 46 | |
mbed_official | 497:d54623194236 | 47 | void i2c_init(i2c_t *obj, PinName sda, PinName scl) |
mbed_official | 497:d54623194236 | 48 | { |
mbed_official | 497:d54623194236 | 49 | static int i2c1_inited = 0; |
mbed_official | 497:d54623194236 | 50 | static int i2c2_inited = 0; |
mbed_official | 126:549ba18ddd81 | 51 | |
mbed_official | 126:549ba18ddd81 | 52 | // Determine the I2C to use |
mbed_official | 126:549ba18ddd81 | 53 | I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); |
mbed_official | 126:549ba18ddd81 | 54 | I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); |
mbed_official | 126:549ba18ddd81 | 55 | |
mbed_official | 126:549ba18ddd81 | 56 | obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); |
mbed_official | 227:7bd0639b8911 | 57 | MBED_ASSERT(obj->i2c != (I2CName)NC); |
mbed_official | 126:549ba18ddd81 | 58 | |
mbed_official | 497:d54623194236 | 59 | // Check if I2C peripherals are already configured |
mbed_official | 497:d54623194236 | 60 | if ((obj->i2c == I2C_1) && i2c1_inited) return; |
mbed_official | 497:d54623194236 | 61 | if ((obj->i2c == I2C_2) && i2c2_inited) return; |
mbed_official | 497:d54623194236 | 62 | |
mbed_official | 497:d54623194236 | 63 | // Set I2C clock |
mbed_official | 227:7bd0639b8911 | 64 | if (obj->i2c == I2C_1) { |
mbed_official | 497:d54623194236 | 65 | i2c1_inited = 1; |
mbed_official | 497:d54623194236 | 66 | __I2C1_CLK_ENABLE(); |
mbed_official | 126:549ba18ddd81 | 67 | } |
mbed_official | 126:549ba18ddd81 | 68 | if (obj->i2c == I2C_2) { |
mbed_official | 497:d54623194236 | 69 | i2c2_inited = 1; |
mbed_official | 497:d54623194236 | 70 | __I2C2_CLK_ENABLE(); |
mbed_official | 126:549ba18ddd81 | 71 | } |
mbed_official | 126:549ba18ddd81 | 72 | |
mbed_official | 126:549ba18ddd81 | 73 | // Configure I2C pins |
mbed_official | 497:d54623194236 | 74 | pinmap_pinout(sda, PinMap_I2C_SDA); |
mbed_official | 126:549ba18ddd81 | 75 | pinmap_pinout(scl, PinMap_I2C_SCL); |
mbed_official | 497:d54623194236 | 76 | pin_mode(sda, OpenDrain); |
mbed_official | 126:549ba18ddd81 | 77 | pin_mode(scl, OpenDrain); |
mbed_official | 126:549ba18ddd81 | 78 | |
mbed_official | 126:549ba18ddd81 | 79 | // Reset to clear pending flags if any |
mbed_official | 126:549ba18ddd81 | 80 | i2c_reset(obj); |
mbed_official | 126:549ba18ddd81 | 81 | |
mbed_official | 126:549ba18ddd81 | 82 | // I2C configuration |
mbed_official | 227:7bd0639b8911 | 83 | i2c_frequency(obj, 100000); // 100 kHz per default |
mbed_official | 497:d54623194236 | 84 | |
mbed_official | 497:d54623194236 | 85 | // I2C master by default |
mbed_official | 497:d54623194236 | 86 | obj->slave = 0; |
mbed_official | 126:549ba18ddd81 | 87 | } |
mbed_official | 126:549ba18ddd81 | 88 | |
mbed_official | 497:d54623194236 | 89 | void i2c_frequency(i2c_t *obj, int hz) |
mbed_official | 497:d54623194236 | 90 | { |
mbed_official | 497:d54623194236 | 91 | MBED_ASSERT((hz != 0) && (hz <= 400000)); |
mbed_official | 497:d54623194236 | 92 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 497:d54623194236 | 93 | int timeout; |
mbed_official | 126:549ba18ddd81 | 94 | |
mbed_official | 497:d54623194236 | 95 | // wait before init |
mbed_official | 497:d54623194236 | 96 | timeout = LONG_TIMEOUT; |
mbed_official | 497:d54623194236 | 97 | while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0)); |
mbed_official | 126:549ba18ddd81 | 98 | |
mbed_official | 126:549ba18ddd81 | 99 | // I2C configuration |
mbed_official | 497:d54623194236 | 100 | I2cHandle.Init.ClockSpeed = hz; |
mbed_official | 497:d54623194236 | 101 | I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2; |
mbed_official | 497:d54623194236 | 102 | I2cHandle.Init.OwnAddress1 = 0; |
mbed_official | 497:d54623194236 | 103 | I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; |
mbed_official | 497:d54623194236 | 104 | I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; |
mbed_official | 497:d54623194236 | 105 | I2cHandle.Init.OwnAddress2 = 0; |
mbed_official | 497:d54623194236 | 106 | I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; |
mbed_official | 497:d54623194236 | 107 | I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; |
mbed_official | 497:d54623194236 | 108 | HAL_I2C_Init(&I2cHandle); |
mbed_official | 497:d54623194236 | 109 | |
mbed_official | 497:d54623194236 | 110 | if (obj->slave) { |
mbed_official | 497:d54623194236 | 111 | // Enable Address Acknowledge |
mbed_official | 497:d54623194236 | 112 | I2cHandle.Instance->CR1 |= I2C_CR1_ACK; |
mbed_official | 126:549ba18ddd81 | 113 | } |
mbed_official | 126:549ba18ddd81 | 114 | } |
mbed_official | 126:549ba18ddd81 | 115 | |
mbed_official | 497:d54623194236 | 116 | inline int i2c_start(i2c_t *obj) |
mbed_official | 497:d54623194236 | 117 | { |
mbed_official | 126:549ba18ddd81 | 118 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 126:549ba18ddd81 | 119 | int timeout; |
mbed_official | 126:549ba18ddd81 | 120 | |
mbed_official | 497:d54623194236 | 121 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 497:d54623194236 | 122 | |
mbed_official | 497:d54623194236 | 123 | // Clear Acknowledge failure flag |
mbed_official | 497:d54623194236 | 124 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF); |
mbed_official | 126:549ba18ddd81 | 125 | |
mbed_official | 126:549ba18ddd81 | 126 | // Generate the START condition |
mbed_official | 497:d54623194236 | 127 | i2c->CR1 |= I2C_CR1_START; |
mbed_official | 126:549ba18ddd81 | 128 | |
mbed_official | 126:549ba18ddd81 | 129 | // Wait the START condition has been correctly sent |
mbed_official | 126:549ba18ddd81 | 130 | timeout = FLAG_TIMEOUT; |
mbed_official | 497:d54623194236 | 131 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_SB) == RESET) { |
mbed_official | 497:d54623194236 | 132 | if ((timeout--) == 0) { |
mbed_official | 126:549ba18ddd81 | 133 | return 1; |
mbed_official | 126:549ba18ddd81 | 134 | } |
mbed_official | 126:549ba18ddd81 | 135 | } |
mbed_official | 126:549ba18ddd81 | 136 | |
mbed_official | 126:549ba18ddd81 | 137 | return 0; |
mbed_official | 126:549ba18ddd81 | 138 | } |
mbed_official | 126:549ba18ddd81 | 139 | |
mbed_official | 497:d54623194236 | 140 | inline int i2c_stop(i2c_t *obj) |
mbed_official | 497:d54623194236 | 141 | { |
mbed_official | 126:549ba18ddd81 | 142 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 126:549ba18ddd81 | 143 | |
mbed_official | 497:d54623194236 | 144 | // Generate the STOP condition |
mbed_official | 497:d54623194236 | 145 | i2c->CR1 |= I2C_CR1_STOP; |
mbed_official | 126:549ba18ddd81 | 146 | |
mbed_official | 126:549ba18ddd81 | 147 | return 0; |
mbed_official | 126:549ba18ddd81 | 148 | } |
mbed_official | 126:549ba18ddd81 | 149 | |
mbed_official | 497:d54623194236 | 150 | int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) |
mbed_official | 497:d54623194236 | 151 | { |
mbed_official | 126:549ba18ddd81 | 152 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 497:d54623194236 | 153 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 126:549ba18ddd81 | 154 | int timeout; |
mbed_official | 126:549ba18ddd81 | 155 | int count; |
mbed_official | 126:549ba18ddd81 | 156 | int value; |
mbed_official | 126:549ba18ddd81 | 157 | |
mbed_official | 497:d54623194236 | 158 | // Generate start condition |
mbed_official | 126:549ba18ddd81 | 159 | i2c_start(obj); |
mbed_official | 126:549ba18ddd81 | 160 | |
mbed_official | 497:d54623194236 | 161 | // Send address for read |
mbed_official | 497:d54623194236 | 162 | i2c->DR = __HAL_I2C_7BIT_ADD_READ(address); |
mbed_official | 126:549ba18ddd81 | 163 | |
mbed_official | 126:549ba18ddd81 | 164 | // Wait address is acknowledged |
mbed_official | 126:549ba18ddd81 | 165 | timeout = FLAG_TIMEOUT; |
mbed_official | 497:d54623194236 | 166 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == RESET) { |
mbed_official | 126:549ba18ddd81 | 167 | timeout--; |
mbed_official | 126:549ba18ddd81 | 168 | if (timeout == 0) { |
mbed_official | 497:d54623194236 | 169 | return -1; |
mbed_official | 126:549ba18ddd81 | 170 | } |
mbed_official | 126:549ba18ddd81 | 171 | } |
mbed_official | 497:d54623194236 | 172 | __HAL_I2C_CLEAR_ADDRFLAG(&I2cHandle); |
mbed_official | 126:549ba18ddd81 | 173 | |
mbed_official | 126:549ba18ddd81 | 174 | // Read all bytes except last one |
mbed_official | 126:549ba18ddd81 | 175 | for (count = 0; count < (length - 1); count++) { |
mbed_official | 126:549ba18ddd81 | 176 | value = i2c_byte_read(obj, 0); |
mbed_official | 126:549ba18ddd81 | 177 | data[count] = (char)value; |
mbed_official | 126:549ba18ddd81 | 178 | } |
mbed_official | 126:549ba18ddd81 | 179 | |
mbed_official | 126:549ba18ddd81 | 180 | // If not repeated start, send stop. |
mbed_official | 126:549ba18ddd81 | 181 | // Warning: must be done BEFORE the data is read. |
mbed_official | 126:549ba18ddd81 | 182 | if (stop) { |
mbed_official | 126:549ba18ddd81 | 183 | i2c_stop(obj); |
mbed_official | 126:549ba18ddd81 | 184 | } |
mbed_official | 126:549ba18ddd81 | 185 | |
mbed_official | 126:549ba18ddd81 | 186 | // Read the last byte |
mbed_official | 126:549ba18ddd81 | 187 | value = i2c_byte_read(obj, 1); |
mbed_official | 126:549ba18ddd81 | 188 | data[count] = (char)value; |
mbed_official | 126:549ba18ddd81 | 189 | |
mbed_official | 126:549ba18ddd81 | 190 | return length; |
mbed_official | 126:549ba18ddd81 | 191 | } |
mbed_official | 126:549ba18ddd81 | 192 | |
mbed_official | 497:d54623194236 | 193 | int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) |
mbed_official | 497:d54623194236 | 194 | { |
mbed_official | 126:549ba18ddd81 | 195 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 497:d54623194236 | 196 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 126:549ba18ddd81 | 197 | int timeout; |
mbed_official | 126:549ba18ddd81 | 198 | int count; |
mbed_official | 126:549ba18ddd81 | 199 | |
mbed_official | 497:d54623194236 | 200 | // Generate start condition |
mbed_official | 126:549ba18ddd81 | 201 | i2c_start(obj); |
mbed_official | 126:549ba18ddd81 | 202 | |
mbed_official | 497:d54623194236 | 203 | // Send address for write |
mbed_official | 497:d54623194236 | 204 | i2c->DR = __HAL_I2C_7BIT_ADD_WRITE(address); |
mbed_official | 126:549ba18ddd81 | 205 | |
mbed_official | 126:549ba18ddd81 | 206 | // Wait address is acknowledged |
mbed_official | 126:549ba18ddd81 | 207 | timeout = FLAG_TIMEOUT; |
mbed_official | 497:d54623194236 | 208 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == RESET) { |
mbed_official | 126:549ba18ddd81 | 209 | timeout--; |
mbed_official | 126:549ba18ddd81 | 210 | if (timeout == 0) { |
mbed_official | 497:d54623194236 | 211 | return -1; |
mbed_official | 126:549ba18ddd81 | 212 | } |
mbed_official | 126:549ba18ddd81 | 213 | } |
mbed_official | 497:d54623194236 | 214 | __HAL_I2C_CLEAR_ADDRFLAG(&I2cHandle); |
mbed_official | 126:549ba18ddd81 | 215 | |
mbed_official | 497:d54623194236 | 216 | // Write all bytes |
mbed_official | 126:549ba18ddd81 | 217 | for (count = 0; count < length; count++) { |
mbed_official | 126:549ba18ddd81 | 218 | if (i2c_byte_write(obj, data[count]) != 1) { |
mbed_official | 126:549ba18ddd81 | 219 | i2c_stop(obj); |
mbed_official | 497:d54623194236 | 220 | return -1; |
mbed_official | 126:549ba18ddd81 | 221 | } |
mbed_official | 126:549ba18ddd81 | 222 | } |
mbed_official | 126:549ba18ddd81 | 223 | |
mbed_official | 126:549ba18ddd81 | 224 | // If not repeated start, send stop. |
mbed_official | 126:549ba18ddd81 | 225 | if (stop) { |
mbed_official | 126:549ba18ddd81 | 226 | i2c_stop(obj); |
mbed_official | 126:549ba18ddd81 | 227 | } |
mbed_official | 126:549ba18ddd81 | 228 | |
mbed_official | 126:549ba18ddd81 | 229 | return count; |
mbed_official | 126:549ba18ddd81 | 230 | } |
mbed_official | 126:549ba18ddd81 | 231 | |
mbed_official | 497:d54623194236 | 232 | int i2c_byte_read(i2c_t *obj, int last) |
mbed_official | 497:d54623194236 | 233 | { |
mbed_official | 126:549ba18ddd81 | 234 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 126:549ba18ddd81 | 235 | int timeout; |
mbed_official | 126:549ba18ddd81 | 236 | |
mbed_official | 126:549ba18ddd81 | 237 | if (last) { |
mbed_official | 126:549ba18ddd81 | 238 | // Don't acknowledge the last byte |
mbed_official | 497:d54623194236 | 239 | i2c->CR1 &= ~I2C_CR1_ACK; |
mbed_official | 126:549ba18ddd81 | 240 | } else { |
mbed_official | 126:549ba18ddd81 | 241 | // Acknowledge the byte |
mbed_official | 497:d54623194236 | 242 | i2c->CR1 |= I2C_CR1_ACK; |
mbed_official | 126:549ba18ddd81 | 243 | } |
mbed_official | 126:549ba18ddd81 | 244 | |
mbed_official | 126:549ba18ddd81 | 245 | // Wait until the byte is received |
mbed_official | 126:549ba18ddd81 | 246 | timeout = FLAG_TIMEOUT; |
mbed_official | 497:d54623194236 | 247 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) { |
mbed_official | 497:d54623194236 | 248 | if ((timeout--) == 0) { |
mbed_official | 497:d54623194236 | 249 | return -1; |
mbed_official | 126:549ba18ddd81 | 250 | } |
mbed_official | 126:549ba18ddd81 | 251 | } |
mbed_official | 126:549ba18ddd81 | 252 | |
mbed_official | 497:d54623194236 | 253 | return (int)i2c->DR; |
mbed_official | 126:549ba18ddd81 | 254 | } |
mbed_official | 126:549ba18ddd81 | 255 | |
mbed_official | 497:d54623194236 | 256 | int i2c_byte_write(i2c_t *obj, int data) |
mbed_official | 497:d54623194236 | 257 | { |
mbed_official | 126:549ba18ddd81 | 258 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 126:549ba18ddd81 | 259 | int timeout; |
mbed_official | 126:549ba18ddd81 | 260 | |
mbed_official | 497:d54623194236 | 261 | i2c->DR = (uint8_t)data; |
mbed_official | 126:549ba18ddd81 | 262 | |
mbed_official | 126:549ba18ddd81 | 263 | // Wait until the byte is transmitted |
mbed_official | 227:7bd0639b8911 | 264 | timeout = FLAG_TIMEOUT; |
mbed_official | 497:d54623194236 | 265 | while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXE) == RESET) && |
mbed_official | 497:d54623194236 | 266 | (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == RESET)) { |
mbed_official | 497:d54623194236 | 267 | if ((timeout--) == 0) { |
mbed_official | 126:549ba18ddd81 | 268 | return 0; |
mbed_official | 126:549ba18ddd81 | 269 | } |
mbed_official | 126:549ba18ddd81 | 270 | } |
mbed_official | 126:549ba18ddd81 | 271 | |
mbed_official | 126:549ba18ddd81 | 272 | return 1; |
mbed_official | 126:549ba18ddd81 | 273 | } |
mbed_official | 126:549ba18ddd81 | 274 | |
mbed_official | 497:d54623194236 | 275 | void i2c_reset(i2c_t *obj) |
mbed_official | 497:d54623194236 | 276 | { |
mbed_official | 497:d54623194236 | 277 | int timeout; |
mbed_official | 497:d54623194236 | 278 | |
mbed_official | 497:d54623194236 | 279 | // Wait before reset |
mbed_official | 497:d54623194236 | 280 | timeout = LONG_TIMEOUT; |
mbed_official | 497:d54623194236 | 281 | while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0)); |
mbed_official | 497:d54623194236 | 282 | |
mbed_official | 227:7bd0639b8911 | 283 | if (obj->i2c == I2C_1) { |
mbed_official | 497:d54623194236 | 284 | __I2C1_FORCE_RESET(); |
mbed_official | 497:d54623194236 | 285 | __I2C1_RELEASE_RESET(); |
mbed_official | 126:549ba18ddd81 | 286 | } |
mbed_official | 126:549ba18ddd81 | 287 | if (obj->i2c == I2C_2) { |
mbed_official | 497:d54623194236 | 288 | __I2C2_FORCE_RESET(); |
mbed_official | 497:d54623194236 | 289 | __I2C2_RELEASE_RESET(); |
mbed_official | 126:549ba18ddd81 | 290 | } |
mbed_official | 126:549ba18ddd81 | 291 | } |
mbed_official | 126:549ba18ddd81 | 292 | |
mbed_official | 126:549ba18ddd81 | 293 | #if DEVICE_I2CSLAVE |
mbed_official | 126:549ba18ddd81 | 294 | |
mbed_official | 497:d54623194236 | 295 | void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) |
mbed_official | 497:d54623194236 | 296 | { |
mbed_official | 126:549ba18ddd81 | 297 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 497:d54623194236 | 298 | uint16_t tmpreg = 0; |
mbed_official | 126:549ba18ddd81 | 299 | |
mbed_official | 126:549ba18ddd81 | 300 | // Get the old register value |
mbed_official | 126:549ba18ddd81 | 301 | tmpreg = i2c->OAR1; |
mbed_official | 126:549ba18ddd81 | 302 | // Reset address bits |
mbed_official | 126:549ba18ddd81 | 303 | tmpreg &= 0xFC00; |
mbed_official | 126:549ba18ddd81 | 304 | // Set new address |
mbed_official | 126:549ba18ddd81 | 305 | tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits |
mbed_official | 126:549ba18ddd81 | 306 | // Store the new register value |
mbed_official | 126:549ba18ddd81 | 307 | i2c->OAR1 = tmpreg; |
mbed_official | 126:549ba18ddd81 | 308 | } |
mbed_official | 126:549ba18ddd81 | 309 | |
mbed_official | 497:d54623194236 | 310 | void i2c_slave_mode(i2c_t *obj, int enable_slave) |
mbed_official | 497:d54623194236 | 311 | { |
mbed_official | 497:d54623194236 | 312 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 497:d54623194236 | 313 | if (enable_slave) { |
mbed_official | 497:d54623194236 | 314 | obj->slave = 1; |
mbed_official | 497:d54623194236 | 315 | /* Enable Address Acknowledge */ |
mbed_official | 497:d54623194236 | 316 | I2cHandle.Instance->CR1 |= I2C_CR1_ACK; |
mbed_official | 497:d54623194236 | 317 | } |
mbed_official | 126:549ba18ddd81 | 318 | } |
mbed_official | 126:549ba18ddd81 | 319 | |
mbed_official | 126:549ba18ddd81 | 320 | // See I2CSlave.h |
mbed_official | 126:549ba18ddd81 | 321 | #define NoData 0 // the slave has not been addressed |
mbed_official | 126:549ba18ddd81 | 322 | #define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter) |
mbed_official | 126:549ba18ddd81 | 323 | #define WriteGeneral 2 // the master is writing to all slave |
mbed_official | 126:549ba18ddd81 | 324 | #define WriteAddressed 3 // the master is writing to this slave (slave = receiver) |
mbed_official | 126:549ba18ddd81 | 325 | |
mbed_official | 497:d54623194236 | 326 | int i2c_slave_receive(i2c_t *obj) |
mbed_official | 497:d54623194236 | 327 | { |
mbed_official | 497:d54623194236 | 328 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 497:d54623194236 | 329 | int retValue = NoData; |
mbed_official | 497:d54623194236 | 330 | |
mbed_official | 497:d54623194236 | 331 | if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == 1) { |
mbed_official | 497:d54623194236 | 332 | if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == 1) { |
mbed_official | 497:d54623194236 | 333 | if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TRA) == 1) { |
mbed_official | 497:d54623194236 | 334 | retValue = ReadAddressed; |
mbed_official | 497:d54623194236 | 335 | } else { |
mbed_official | 497:d54623194236 | 336 | retValue = WriteAddressed; |
mbed_official | 497:d54623194236 | 337 | } |
mbed_official | 497:d54623194236 | 338 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_ADDR); |
mbed_official | 497:d54623194236 | 339 | } |
mbed_official | 126:549ba18ddd81 | 340 | } |
mbed_official | 126:549ba18ddd81 | 341 | |
mbed_official | 497:d54623194236 | 342 | return (retValue); |
mbed_official | 497:d54623194236 | 343 | } |
mbed_official | 126:549ba18ddd81 | 344 | |
mbed_official | 497:d54623194236 | 345 | int i2c_slave_read(i2c_t *obj, char *data, int length) |
mbed_official | 497:d54623194236 | 346 | { |
mbed_official | 497:d54623194236 | 347 | uint32_t Timeout; |
mbed_official | 497:d54623194236 | 348 | int size = 0; |
mbed_official | 497:d54623194236 | 349 | |
mbed_official | 497:d54623194236 | 350 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 497:d54623194236 | 351 | |
mbed_official | 497:d54623194236 | 352 | while (length > 0) { |
mbed_official | 497:d54623194236 | 353 | // Wait until RXNE flag is set |
mbed_official | 497:d54623194236 | 354 | // Wait until the byte is received |
mbed_official | 497:d54623194236 | 355 | Timeout = FLAG_TIMEOUT; |
mbed_official | 497:d54623194236 | 356 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) { |
mbed_official | 497:d54623194236 | 357 | Timeout--; |
mbed_official | 497:d54623194236 | 358 | if (Timeout == 0) { |
mbed_official | 497:d54623194236 | 359 | return -1; |
mbed_official | 497:d54623194236 | 360 | } |
mbed_official | 126:549ba18ddd81 | 361 | } |
mbed_official | 126:549ba18ddd81 | 362 | |
mbed_official | 497:d54623194236 | 363 | // Read data |
mbed_official | 497:d54623194236 | 364 | (*data++) = I2cHandle.Instance->DR; |
mbed_official | 497:d54623194236 | 365 | length--; |
mbed_official | 497:d54623194236 | 366 | size++; |
mbed_official | 497:d54623194236 | 367 | |
mbed_official | 497:d54623194236 | 368 | if ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == SET) && (length != 0)) { |
mbed_official | 497:d54623194236 | 369 | // Read data |
mbed_official | 497:d54623194236 | 370 | (*data++) = I2cHandle.Instance->DR; |
mbed_official | 497:d54623194236 | 371 | length--; |
mbed_official | 497:d54623194236 | 372 | size++; |
mbed_official | 497:d54623194236 | 373 | } |
mbed_official | 497:d54623194236 | 374 | } |
mbed_official | 497:d54623194236 | 375 | |
mbed_official | 497:d54623194236 | 376 | // Wait until STOP flag is set |
mbed_official | 497:d54623194236 | 377 | Timeout = FLAG_TIMEOUT; |
mbed_official | 497:d54623194236 | 378 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) { |
mbed_official | 497:d54623194236 | 379 | Timeout--; |
mbed_official | 497:d54623194236 | 380 | if (Timeout == 0) { |
mbed_official | 497:d54623194236 | 381 | return -1; |
mbed_official | 497:d54623194236 | 382 | } |
mbed_official | 497:d54623194236 | 383 | } |
mbed_official | 497:d54623194236 | 384 | |
mbed_official | 497:d54623194236 | 385 | // Clear STOP flag |
mbed_official | 497:d54623194236 | 386 | __HAL_I2C_CLEAR_STOPFLAG(&I2cHandle); |
mbed_official | 497:d54623194236 | 387 | |
mbed_official | 497:d54623194236 | 388 | // Wait until BUSY flag is reset |
mbed_official | 497:d54623194236 | 389 | Timeout = FLAG_TIMEOUT; |
mbed_official | 497:d54623194236 | 390 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == SET) { |
mbed_official | 497:d54623194236 | 391 | Timeout--; |
mbed_official | 497:d54623194236 | 392 | if (Timeout == 0) { |
mbed_official | 497:d54623194236 | 393 | return -1; |
mbed_official | 497:d54623194236 | 394 | } |
mbed_official | 497:d54623194236 | 395 | } |
mbed_official | 497:d54623194236 | 396 | |
mbed_official | 497:d54623194236 | 397 | return size; |
mbed_official | 126:549ba18ddd81 | 398 | } |
mbed_official | 126:549ba18ddd81 | 399 | |
mbed_official | 497:d54623194236 | 400 | int i2c_slave_write(i2c_t *obj, const char *data, int length) |
mbed_official | 497:d54623194236 | 401 | { |
mbed_official | 497:d54623194236 | 402 | uint32_t Timeout; |
mbed_official | 497:d54623194236 | 403 | int size = 0; |
mbed_official | 497:d54623194236 | 404 | |
mbed_official | 497:d54623194236 | 405 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 497:d54623194236 | 406 | |
mbed_official | 497:d54623194236 | 407 | while (length > 0) { |
mbed_official | 497:d54623194236 | 408 | // Wait until TXE flag is set |
mbed_official | 497:d54623194236 | 409 | Timeout = FLAG_TIMEOUT; |
mbed_official | 497:d54623194236 | 410 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXE) == RESET) { |
mbed_official | 497:d54623194236 | 411 | Timeout--; |
mbed_official | 497:d54623194236 | 412 | if (Timeout == 0) { |
mbed_official | 497:d54623194236 | 413 | return -1; |
mbed_official | 497:d54623194236 | 414 | } |
mbed_official | 497:d54623194236 | 415 | } |
mbed_official | 497:d54623194236 | 416 | |
mbed_official | 497:d54623194236 | 417 | // Write data |
mbed_official | 497:d54623194236 | 418 | I2cHandle.Instance->DR = (*data++); |
mbed_official | 497:d54623194236 | 419 | length--; |
mbed_official | 497:d54623194236 | 420 | size++; |
mbed_official | 497:d54623194236 | 421 | |
mbed_official | 497:d54623194236 | 422 | if ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == SET) && (length != 0)) { |
mbed_official | 497:d54623194236 | 423 | // Write data to DR |
mbed_official | 497:d54623194236 | 424 | I2cHandle.Instance->DR = (*data++); |
mbed_official | 497:d54623194236 | 425 | length--; |
mbed_official | 497:d54623194236 | 426 | size++; |
mbed_official | 497:d54623194236 | 427 | } |
mbed_official | 126:549ba18ddd81 | 428 | } |
mbed_official | 497:d54623194236 | 429 | |
mbed_official | 497:d54623194236 | 430 | // Wait until AF flag is set |
mbed_official | 497:d54623194236 | 431 | Timeout = FLAG_TIMEOUT; |
mbed_official | 497:d54623194236 | 432 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_AF) == RESET) { |
mbed_official | 497:d54623194236 | 433 | Timeout--; |
mbed_official | 497:d54623194236 | 434 | if (Timeout == 0) { |
mbed_official | 497:d54623194236 | 435 | return -1; |
mbed_official | 497:d54623194236 | 436 | } |
mbed_official | 497:d54623194236 | 437 | } |
mbed_official | 497:d54623194236 | 438 | |
mbed_official | 497:d54623194236 | 439 | // Clear AF flag |
mbed_official | 497:d54623194236 | 440 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF); |
mbed_official | 497:d54623194236 | 441 | |
mbed_official | 497:d54623194236 | 442 | // Wait until BUSY flag is reset |
mbed_official | 497:d54623194236 | 443 | Timeout = FLAG_TIMEOUT; |
mbed_official | 497:d54623194236 | 444 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == SET) { |
mbed_official | 497:d54623194236 | 445 | Timeout--; |
mbed_official | 497:d54623194236 | 446 | if (Timeout == 0) { |
mbed_official | 497:d54623194236 | 447 | return -1; |
mbed_official | 497:d54623194236 | 448 | } |
mbed_official | 497:d54623194236 | 449 | } |
mbed_official | 497:d54623194236 | 450 | |
mbed_official | 497:d54623194236 | 451 | return size; |
mbed_official | 126:549ba18ddd81 | 452 | } |
mbed_official | 126:549ba18ddd81 | 453 | |
mbed_official | 126:549ba18ddd81 | 454 | |
mbed_official | 126:549ba18ddd81 | 455 | #endif // DEVICE_I2CSLAVE |
mbed_official | 126:549ba18ddd81 | 456 | |
mbed_official | 126:549ba18ddd81 | 457 | #endif // DEVICE_I2C |