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