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