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