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