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