mbed-os
Fork of mbed-os by
targets/TARGET_STM/TARGET_STM32F7/i2c_api.c@0:f269e3021894, 2016-10-23 (annotated)
- Committer:
- elessair
- Date:
- Sun Oct 23 15:10:02 2016 +0000
- Revision:
- 0:f269e3021894
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | /* mbed Microcontroller Library |
elessair | 0:f269e3021894 | 2 | ******************************************************************************* |
elessair | 0:f269e3021894 | 3 | * Copyright (c) 2015, STMicroelectronics |
elessair | 0:f269e3021894 | 4 | * All rights reserved. |
elessair | 0:f269e3021894 | 5 | * |
elessair | 0:f269e3021894 | 6 | * Redistribution and use in source and binary forms, with or without |
elessair | 0:f269e3021894 | 7 | * modification, are permitted provided that the following conditions are met: |
elessair | 0:f269e3021894 | 8 | * |
elessair | 0:f269e3021894 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
elessair | 0:f269e3021894 | 10 | * this list of conditions and the following disclaimer. |
elessair | 0:f269e3021894 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
elessair | 0:f269e3021894 | 12 | * this list of conditions and the following disclaimer in the documentation |
elessair | 0:f269e3021894 | 13 | * and/or other materials provided with the distribution. |
elessair | 0:f269e3021894 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
elessair | 0:f269e3021894 | 15 | * may be used to endorse or promote products derived from this software |
elessair | 0:f269e3021894 | 16 | * without specific prior written permission. |
elessair | 0:f269e3021894 | 17 | * |
elessair | 0:f269e3021894 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
elessair | 0:f269e3021894 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
elessair | 0:f269e3021894 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
elessair | 0:f269e3021894 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
elessair | 0:f269e3021894 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
elessair | 0:f269e3021894 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
elessair | 0:f269e3021894 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
elessair | 0:f269e3021894 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
elessair | 0:f269e3021894 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
elessair | 0:f269e3021894 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
elessair | 0:f269e3021894 | 28 | ******************************************************************************* |
elessair | 0:f269e3021894 | 29 | */ |
elessair | 0:f269e3021894 | 30 | #include "mbed_assert.h" |
elessair | 0:f269e3021894 | 31 | #include "i2c_api.h" |
elessair | 0:f269e3021894 | 32 | |
elessair | 0:f269e3021894 | 33 | #if DEVICE_I2C |
elessair | 0:f269e3021894 | 34 | |
elessair | 0:f269e3021894 | 35 | #include "cmsis.h" |
elessair | 0:f269e3021894 | 36 | #include "pinmap.h" |
elessair | 0:f269e3021894 | 37 | #include "PeripheralPins.h" |
elessair | 0:f269e3021894 | 38 | #include "mbed_error.h" |
elessair | 0:f269e3021894 | 39 | |
elessair | 0:f269e3021894 | 40 | /* Timeout values for flags and events waiting loops. These timeouts are |
elessair | 0:f269e3021894 | 41 | not based on accurate values, they just guarantee that the application will |
elessair | 0:f269e3021894 | 42 | not remain stuck if the I2C communication is corrupted. */ |
elessair | 0:f269e3021894 | 43 | #define FLAG_TIMEOUT ((int)0x4000) |
elessair | 0:f269e3021894 | 44 | #define LONG_TIMEOUT ((int)0x8000) |
elessair | 0:f269e3021894 | 45 | |
elessair | 0:f269e3021894 | 46 | I2C_HandleTypeDef I2cHandle; |
elessair | 0:f269e3021894 | 47 | |
elessair | 0:f269e3021894 | 48 | int i2c1_inited = 0; |
elessair | 0:f269e3021894 | 49 | int i2c2_inited = 0; |
elessair | 0:f269e3021894 | 50 | int i2c3_inited = 0; |
elessair | 0:f269e3021894 | 51 | int i2c4_inited = 0; |
elessair | 0:f269e3021894 | 52 | |
elessair | 0:f269e3021894 | 53 | |
elessair | 0:f269e3021894 | 54 | void i2c_init(i2c_t *obj, PinName sda, PinName scl) |
elessair | 0:f269e3021894 | 55 | { |
elessair | 0:f269e3021894 | 56 | // Determine the I2C to use |
elessair | 0:f269e3021894 | 57 | I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); |
elessair | 0:f269e3021894 | 58 | I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); |
elessair | 0:f269e3021894 | 59 | |
elessair | 0:f269e3021894 | 60 | obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); |
elessair | 0:f269e3021894 | 61 | MBED_ASSERT(obj->i2c != (I2CName)NC); |
elessair | 0:f269e3021894 | 62 | |
elessair | 0:f269e3021894 | 63 | // Enable I2C clock and pinout if not done |
elessair | 0:f269e3021894 | 64 | if ((obj->i2c == I2C_1) && !i2c1_inited) { |
elessair | 0:f269e3021894 | 65 | i2c1_inited = 1; |
elessair | 0:f269e3021894 | 66 | __HAL_RCC_I2C1_CONFIG(RCC_I2C1CLKSOURCE_PCLK1); |
elessair | 0:f269e3021894 | 67 | __HAL_RCC_I2C1_CLK_ENABLE(); |
elessair | 0:f269e3021894 | 68 | // Configure I2C1 pins |
elessair | 0:f269e3021894 | 69 | pinmap_pinout(sda, PinMap_I2C_SDA); |
elessair | 0:f269e3021894 | 70 | pinmap_pinout(scl, PinMap_I2C_SCL); |
elessair | 0:f269e3021894 | 71 | pin_mode(sda, OpenDrain); |
elessair | 0:f269e3021894 | 72 | pin_mode(scl, OpenDrain); |
elessair | 0:f269e3021894 | 73 | } |
elessair | 0:f269e3021894 | 74 | |
elessair | 0:f269e3021894 | 75 | #if defined(I2C2_BASE) |
elessair | 0:f269e3021894 | 76 | if ((obj->i2c == I2C_2) && !i2c2_inited) { |
elessair | 0:f269e3021894 | 77 | i2c2_inited = 1; |
elessair | 0:f269e3021894 | 78 | __HAL_RCC_I2C2_CONFIG(RCC_I2C2CLKSOURCE_PCLK1); |
elessair | 0:f269e3021894 | 79 | __HAL_RCC_I2C2_CLK_ENABLE(); |
elessair | 0:f269e3021894 | 80 | // Configure I2C2 pins |
elessair | 0:f269e3021894 | 81 | pinmap_pinout(sda, PinMap_I2C_SDA); |
elessair | 0:f269e3021894 | 82 | pinmap_pinout(scl, PinMap_I2C_SCL); |
elessair | 0:f269e3021894 | 83 | pin_mode(sda, OpenDrain); |
elessair | 0:f269e3021894 | 84 | pin_mode(scl, OpenDrain); |
elessair | 0:f269e3021894 | 85 | } |
elessair | 0:f269e3021894 | 86 | #endif |
elessair | 0:f269e3021894 | 87 | |
elessair | 0:f269e3021894 | 88 | #if defined(I2C3_BASE) |
elessair | 0:f269e3021894 | 89 | if ((obj->i2c == I2C_3) && !i2c3_inited) { |
elessair | 0:f269e3021894 | 90 | i2c3_inited = 1; |
elessair | 0:f269e3021894 | 91 | __HAL_RCC_I2C3_CONFIG(RCC_I2C3CLKSOURCE_PCLK1); |
elessair | 0:f269e3021894 | 92 | __HAL_RCC_I2C3_CLK_ENABLE(); |
elessair | 0:f269e3021894 | 93 | // Configure I2C3 pins |
elessair | 0:f269e3021894 | 94 | pinmap_pinout(sda, PinMap_I2C_SDA); |
elessair | 0:f269e3021894 | 95 | pinmap_pinout(scl, PinMap_I2C_SCL); |
elessair | 0:f269e3021894 | 96 | pin_mode(sda, OpenDrain); |
elessair | 0:f269e3021894 | 97 | pin_mode(scl, OpenDrain); |
elessair | 0:f269e3021894 | 98 | } |
elessair | 0:f269e3021894 | 99 | #endif |
elessair | 0:f269e3021894 | 100 | |
elessair | 0:f269e3021894 | 101 | #if defined(I2C4_BASE) |
elessair | 0:f269e3021894 | 102 | if ((obj->i2c == I2C_4) && !i2c4_inited) { |
elessair | 0:f269e3021894 | 103 | i2c4_inited = 1; |
elessair | 0:f269e3021894 | 104 | __HAL_RCC_I2C4_CONFIG(RCC_I2C4CLKSOURCE_PCLK1); |
elessair | 0:f269e3021894 | 105 | __HAL_RCC_I2C4_CLK_ENABLE(); |
elessair | 0:f269e3021894 | 106 | // Configure I2C4 pins |
elessair | 0:f269e3021894 | 107 | pinmap_pinout(sda, PinMap_I2C_SDA); |
elessair | 0:f269e3021894 | 108 | pinmap_pinout(scl, PinMap_I2C_SCL); |
elessair | 0:f269e3021894 | 109 | pin_mode(sda, OpenDrain); |
elessair | 0:f269e3021894 | 110 | pin_mode(scl, OpenDrain); |
elessair | 0:f269e3021894 | 111 | } |
elessair | 0:f269e3021894 | 112 | #endif |
elessair | 0:f269e3021894 | 113 | |
elessair | 0:f269e3021894 | 114 | // Reset to clear pending flags if any |
elessair | 0:f269e3021894 | 115 | i2c_reset(obj); |
elessair | 0:f269e3021894 | 116 | |
elessair | 0:f269e3021894 | 117 | // I2C configuration |
elessair | 0:f269e3021894 | 118 | i2c_frequency(obj, 100000); // 100 kHz per default |
elessair | 0:f269e3021894 | 119 | } |
elessair | 0:f269e3021894 | 120 | |
elessair | 0:f269e3021894 | 121 | void i2c_frequency(i2c_t *obj, int hz) |
elessair | 0:f269e3021894 | 122 | { |
elessair | 0:f269e3021894 | 123 | uint32_t tim = 0; |
elessair | 0:f269e3021894 | 124 | |
elessair | 0:f269e3021894 | 125 | MBED_ASSERT((hz == 100000) || (hz == 400000) || (hz == 1000000)); |
elessair | 0:f269e3021894 | 126 | |
elessair | 0:f269e3021894 | 127 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
elessair | 0:f269e3021894 | 128 | int timeout; |
elessair | 0:f269e3021894 | 129 | |
elessair | 0:f269e3021894 | 130 | // wait before init |
elessair | 0:f269e3021894 | 131 | timeout = LONG_TIMEOUT; |
elessair | 0:f269e3021894 | 132 | while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0)) {} |
elessair | 0:f269e3021894 | 133 | |
elessair | 0:f269e3021894 | 134 | /* |
elessair | 0:f269e3021894 | 135 | Values calculated with I2C_Timing_Configuration tool (excel file) |
elessair | 0:f269e3021894 | 136 | * Standard mode (up to 100 kHz) |
elessair | 0:f269e3021894 | 137 | * Fast Mode (up to 400 kHz) |
elessair | 0:f269e3021894 | 138 | * Fast Mode Plus (up to 1 MHz) |
elessair | 0:f269e3021894 | 139 | Below values obtained with: |
elessair | 0:f269e3021894 | 140 | - I2Cx clock source = APB1CLK = 54 MHz |
elessair | 0:f269e3021894 | 141 | - Analog filter delay = ON |
elessair | 0:f269e3021894 | 142 | - Digital filter coefficient = 0 |
elessair | 0:f269e3021894 | 143 | */ |
elessair | 0:f269e3021894 | 144 | switch (hz) { |
elessair | 0:f269e3021894 | 145 | case 100000: |
elessair | 0:f269e3021894 | 146 | tim = 0x10916998; // Standard mode with Rise time = 120ns, Fall time = 120ns |
elessair | 0:f269e3021894 | 147 | break; |
elessair | 0:f269e3021894 | 148 | case 400000: |
elessair | 0:f269e3021894 | 149 | tim = 0x00B11B54; // Fast Mode with Rise time = 120ns, Fall time = 120ns |
elessair | 0:f269e3021894 | 150 | break; |
elessair | 0:f269e3021894 | 151 | case 1000000: |
elessair | 0:f269e3021894 | 152 | tim = 0x0090091B; // Fast Mode Plus with Rise time = 120ns, Fall time = 10ns |
elessair | 0:f269e3021894 | 153 | break; |
elessair | 0:f269e3021894 | 154 | default: |
elessair | 0:f269e3021894 | 155 | break; |
elessair | 0:f269e3021894 | 156 | } |
elessair | 0:f269e3021894 | 157 | |
elessair | 0:f269e3021894 | 158 | // I2C configuration |
elessair | 0:f269e3021894 | 159 | I2cHandle.Init.Timing = tim; |
elessair | 0:f269e3021894 | 160 | I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; |
elessair | 0:f269e3021894 | 161 | I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; |
elessair | 0:f269e3021894 | 162 | I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; |
elessair | 0:f269e3021894 | 163 | I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; |
elessair | 0:f269e3021894 | 164 | I2cHandle.Init.OwnAddress1 = 0; |
elessair | 0:f269e3021894 | 165 | I2cHandle.Init.OwnAddress2 = 0; |
elessair | 0:f269e3021894 | 166 | I2cHandle.Init.OwnAddress2Masks = I2C_OA2_NOMASK; |
elessair | 0:f269e3021894 | 167 | |
elessair | 0:f269e3021894 | 168 | if (HAL_I2C_Init(&I2cHandle) != HAL_OK) { |
elessair | 0:f269e3021894 | 169 | error("Cannot initialize I2C"); |
elessair | 0:f269e3021894 | 170 | } |
elessair | 0:f269e3021894 | 171 | } |
elessair | 0:f269e3021894 | 172 | |
elessair | 0:f269e3021894 | 173 | inline int i2c_start(i2c_t *obj) |
elessair | 0:f269e3021894 | 174 | { |
elessair | 0:f269e3021894 | 175 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
elessair | 0:f269e3021894 | 176 | int timeout; |
elessair | 0:f269e3021894 | 177 | |
elessair | 0:f269e3021894 | 178 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
elessair | 0:f269e3021894 | 179 | |
elessair | 0:f269e3021894 | 180 | // Clear Acknowledge failure flag |
elessair | 0:f269e3021894 | 181 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF); |
elessair | 0:f269e3021894 | 182 | |
elessair | 0:f269e3021894 | 183 | // Wait the STOP condition has been previously correctly sent |
elessair | 0:f269e3021894 | 184 | timeout = FLAG_TIMEOUT; |
elessair | 0:f269e3021894 | 185 | while ((i2c->CR2 & I2C_CR2_STOP) == I2C_CR2_STOP){ |
elessair | 0:f269e3021894 | 186 | if ((timeout--) == 0) { |
elessair | 0:f269e3021894 | 187 | return 1; |
elessair | 0:f269e3021894 | 188 | } |
elessair | 0:f269e3021894 | 189 | } |
elessair | 0:f269e3021894 | 190 | |
elessair | 0:f269e3021894 | 191 | // Generate the START condition |
elessair | 0:f269e3021894 | 192 | i2c->CR2 |= I2C_CR2_START; |
elessair | 0:f269e3021894 | 193 | |
elessair | 0:f269e3021894 | 194 | // Wait the START condition has been correctly sent |
elessair | 0:f269e3021894 | 195 | timeout = FLAG_TIMEOUT; |
elessair | 0:f269e3021894 | 196 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == RESET) { |
elessair | 0:f269e3021894 | 197 | if ((timeout--) == 0) { |
elessair | 0:f269e3021894 | 198 | return 1; |
elessair | 0:f269e3021894 | 199 | } |
elessair | 0:f269e3021894 | 200 | } |
elessair | 0:f269e3021894 | 201 | |
elessair | 0:f269e3021894 | 202 | return 0; |
elessair | 0:f269e3021894 | 203 | } |
elessair | 0:f269e3021894 | 204 | |
elessair | 0:f269e3021894 | 205 | inline int i2c_stop(i2c_t *obj) |
elessair | 0:f269e3021894 | 206 | { |
elessair | 0:f269e3021894 | 207 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
elessair | 0:f269e3021894 | 208 | |
elessair | 0:f269e3021894 | 209 | // Generate the STOP condition |
elessair | 0:f269e3021894 | 210 | i2c->CR2 |= I2C_CR2_STOP; |
elessair | 0:f269e3021894 | 211 | |
elessair | 0:f269e3021894 | 212 | return 0; |
elessair | 0:f269e3021894 | 213 | } |
elessair | 0:f269e3021894 | 214 | |
elessair | 0:f269e3021894 | 215 | int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) |
elessair | 0:f269e3021894 | 216 | { |
elessair | 0:f269e3021894 | 217 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
elessair | 0:f269e3021894 | 218 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
elessair | 0:f269e3021894 | 219 | int timeout; |
elessair | 0:f269e3021894 | 220 | int count; |
elessair | 0:f269e3021894 | 221 | int value; |
elessair | 0:f269e3021894 | 222 | |
elessair | 0:f269e3021894 | 223 | /* update CR2 register */ |
elessair | 0:f269e3021894 | 224 | i2c->CR2 = (i2c->CR2 & (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP))) |
elessair | 0:f269e3021894 | 225 | | (uint32_t)(((uint32_t)address & I2C_CR2_SADD) | (((uint32_t)length << 16) & I2C_CR2_NBYTES) | (uint32_t)I2C_SOFTEND_MODE | (uint32_t)I2C_GENERATE_START_READ); |
elessair | 0:f269e3021894 | 226 | |
elessair | 0:f269e3021894 | 227 | // Read all bytes |
elessair | 0:f269e3021894 | 228 | for (count = 0; count < length; count++) { |
elessair | 0:f269e3021894 | 229 | value = i2c_byte_read(obj, 0); |
elessair | 0:f269e3021894 | 230 | data[count] = (char)value; |
elessair | 0:f269e3021894 | 231 | } |
elessair | 0:f269e3021894 | 232 | |
elessair | 0:f269e3021894 | 233 | // Wait transfer complete |
elessair | 0:f269e3021894 | 234 | timeout = LONG_TIMEOUT; |
elessair | 0:f269e3021894 | 235 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) { |
elessair | 0:f269e3021894 | 236 | timeout--; |
elessair | 0:f269e3021894 | 237 | if (timeout == 0) { |
elessair | 0:f269e3021894 | 238 | return -1; |
elessair | 0:f269e3021894 | 239 | } |
elessair | 0:f269e3021894 | 240 | } |
elessair | 0:f269e3021894 | 241 | |
elessair | 0:f269e3021894 | 242 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC); |
elessair | 0:f269e3021894 | 243 | |
elessair | 0:f269e3021894 | 244 | // If not repeated start, send stop. |
elessair | 0:f269e3021894 | 245 | if (stop) { |
elessair | 0:f269e3021894 | 246 | i2c_stop(obj); |
elessair | 0:f269e3021894 | 247 | /* Wait until STOPF flag is set */ |
elessair | 0:f269e3021894 | 248 | timeout = FLAG_TIMEOUT; |
elessair | 0:f269e3021894 | 249 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) { |
elessair | 0:f269e3021894 | 250 | timeout--; |
elessair | 0:f269e3021894 | 251 | if (timeout == 0) { |
elessair | 0:f269e3021894 | 252 | return -1; |
elessair | 0:f269e3021894 | 253 | } |
elessair | 0:f269e3021894 | 254 | } |
elessair | 0:f269e3021894 | 255 | /* Clear STOP Flag */ |
elessair | 0:f269e3021894 | 256 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF); |
elessair | 0:f269e3021894 | 257 | } |
elessair | 0:f269e3021894 | 258 | |
elessair | 0:f269e3021894 | 259 | return length; |
elessair | 0:f269e3021894 | 260 | } |
elessair | 0:f269e3021894 | 261 | |
elessair | 0:f269e3021894 | 262 | int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) |
elessair | 0:f269e3021894 | 263 | { |
elessair | 0:f269e3021894 | 264 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
elessair | 0:f269e3021894 | 265 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
elessair | 0:f269e3021894 | 266 | int timeout; |
elessair | 0:f269e3021894 | 267 | int count; |
elessair | 0:f269e3021894 | 268 | |
elessair | 0:f269e3021894 | 269 | /* update CR2 register */ |
elessair | 0:f269e3021894 | 270 | i2c->CR2 = (i2c->CR2 & (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP))) |
elessair | 0:f269e3021894 | 271 | | (uint32_t)(((uint32_t)address & I2C_CR2_SADD) | (((uint32_t)length << 16) & I2C_CR2_NBYTES) | (uint32_t)I2C_SOFTEND_MODE | (uint32_t)I2C_GENERATE_START_WRITE); |
elessair | 0:f269e3021894 | 272 | |
elessair | 0:f269e3021894 | 273 | for (count = 0; count < length; count++) { |
elessair | 0:f269e3021894 | 274 | i2c_byte_write(obj, data[count]); |
elessair | 0:f269e3021894 | 275 | } |
elessair | 0:f269e3021894 | 276 | |
elessair | 0:f269e3021894 | 277 | // Wait transfer complete |
elessair | 0:f269e3021894 | 278 | timeout = FLAG_TIMEOUT; |
elessair | 0:f269e3021894 | 279 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) { |
elessair | 0:f269e3021894 | 280 | timeout--; |
elessair | 0:f269e3021894 | 281 | if (timeout == 0) { |
elessair | 0:f269e3021894 | 282 | return -1; |
elessair | 0:f269e3021894 | 283 | } |
elessair | 0:f269e3021894 | 284 | } |
elessair | 0:f269e3021894 | 285 | |
elessair | 0:f269e3021894 | 286 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC); |
elessair | 0:f269e3021894 | 287 | |
elessair | 0:f269e3021894 | 288 | // If not repeated start, send stop. |
elessair | 0:f269e3021894 | 289 | if (stop) { |
elessair | 0:f269e3021894 | 290 | i2c_stop(obj); |
elessair | 0:f269e3021894 | 291 | /* Wait until STOPF flag is set */ |
elessair | 0:f269e3021894 | 292 | timeout = FLAG_TIMEOUT; |
elessair | 0:f269e3021894 | 293 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) { |
elessair | 0:f269e3021894 | 294 | timeout--; |
elessair | 0:f269e3021894 | 295 | if (timeout == 0) { |
elessair | 0:f269e3021894 | 296 | return -1; |
elessair | 0:f269e3021894 | 297 | } |
elessair | 0:f269e3021894 | 298 | } |
elessair | 0:f269e3021894 | 299 | /* Clear STOP Flag */ |
elessair | 0:f269e3021894 | 300 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF); |
elessair | 0:f269e3021894 | 301 | } |
elessair | 0:f269e3021894 | 302 | |
elessair | 0:f269e3021894 | 303 | return count; |
elessair | 0:f269e3021894 | 304 | } |
elessair | 0:f269e3021894 | 305 | |
elessair | 0:f269e3021894 | 306 | int i2c_byte_read(i2c_t *obj, int last) |
elessair | 0:f269e3021894 | 307 | { |
elessair | 0:f269e3021894 | 308 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
elessair | 0:f269e3021894 | 309 | int timeout; |
elessair | 0:f269e3021894 | 310 | |
elessair | 0:f269e3021894 | 311 | // Wait until the byte is received |
elessair | 0:f269e3021894 | 312 | timeout = FLAG_TIMEOUT; |
elessair | 0:f269e3021894 | 313 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) { |
elessair | 0:f269e3021894 | 314 | if ((timeout--) == 0) { |
elessair | 0:f269e3021894 | 315 | return -1; |
elessair | 0:f269e3021894 | 316 | } |
elessair | 0:f269e3021894 | 317 | } |
elessair | 0:f269e3021894 | 318 | |
elessair | 0:f269e3021894 | 319 | return (int)i2c->RXDR; |
elessair | 0:f269e3021894 | 320 | } |
elessair | 0:f269e3021894 | 321 | |
elessair | 0:f269e3021894 | 322 | int i2c_byte_write(i2c_t *obj, int data) |
elessair | 0:f269e3021894 | 323 | { |
elessair | 0:f269e3021894 | 324 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
elessair | 0:f269e3021894 | 325 | int timeout; |
elessair | 0:f269e3021894 | 326 | |
elessair | 0:f269e3021894 | 327 | // Wait until the previous byte is transmitted |
elessair | 0:f269e3021894 | 328 | timeout = FLAG_TIMEOUT; |
elessair | 0:f269e3021894 | 329 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXIS) == RESET) { |
elessair | 0:f269e3021894 | 330 | if ((timeout--) == 0) { |
elessair | 0:f269e3021894 | 331 | return 0; |
elessair | 0:f269e3021894 | 332 | } |
elessair | 0:f269e3021894 | 333 | } |
elessair | 0:f269e3021894 | 334 | |
elessair | 0:f269e3021894 | 335 | i2c->TXDR = (uint8_t)data; |
elessair | 0:f269e3021894 | 336 | |
elessair | 0:f269e3021894 | 337 | return 1; |
elessair | 0:f269e3021894 | 338 | } |
elessair | 0:f269e3021894 | 339 | |
elessair | 0:f269e3021894 | 340 | void i2c_reset(i2c_t *obj) |
elessair | 0:f269e3021894 | 341 | { |
elessair | 0:f269e3021894 | 342 | int timeout; |
elessair | 0:f269e3021894 | 343 | |
elessair | 0:f269e3021894 | 344 | // wait before reset |
elessair | 0:f269e3021894 | 345 | timeout = LONG_TIMEOUT; |
elessair | 0:f269e3021894 | 346 | while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0)); |
elessair | 0:f269e3021894 | 347 | |
elessair | 0:f269e3021894 | 348 | __I2C1_FORCE_RESET(); |
elessair | 0:f269e3021894 | 349 | __I2C1_RELEASE_RESET(); |
elessair | 0:f269e3021894 | 350 | } |
elessair | 0:f269e3021894 | 351 | |
elessair | 0:f269e3021894 | 352 | #if DEVICE_I2CSLAVE |
elessair | 0:f269e3021894 | 353 | |
elessair | 0:f269e3021894 | 354 | void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) |
elessair | 0:f269e3021894 | 355 | { |
elessair | 0:f269e3021894 | 356 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
elessair | 0:f269e3021894 | 357 | uint16_t tmpreg; |
elessair | 0:f269e3021894 | 358 | |
elessair | 0:f269e3021894 | 359 | // disable |
elessair | 0:f269e3021894 | 360 | i2c->OAR1 &= (uint32_t)(~I2C_OAR1_OA1EN); |
elessair | 0:f269e3021894 | 361 | // Get the old register value |
elessair | 0:f269e3021894 | 362 | tmpreg = i2c->OAR1; |
elessair | 0:f269e3021894 | 363 | // Reset address bits |
elessair | 0:f269e3021894 | 364 | tmpreg &= 0xFC00; |
elessair | 0:f269e3021894 | 365 | // Set new address |
elessair | 0:f269e3021894 | 366 | tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits |
elessair | 0:f269e3021894 | 367 | // Store the new register value |
elessair | 0:f269e3021894 | 368 | i2c->OAR1 = tmpreg; |
elessair | 0:f269e3021894 | 369 | // enable |
elessair | 0:f269e3021894 | 370 | i2c->OAR1 |= I2C_OAR1_OA1EN; |
elessair | 0:f269e3021894 | 371 | } |
elessair | 0:f269e3021894 | 372 | |
elessair | 0:f269e3021894 | 373 | void i2c_slave_mode(i2c_t *obj, int enable_slave) |
elessair | 0:f269e3021894 | 374 | { |
elessair | 0:f269e3021894 | 375 | |
elessair | 0:f269e3021894 | 376 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
elessair | 0:f269e3021894 | 377 | uint16_t tmpreg; |
elessair | 0:f269e3021894 | 378 | |
elessair | 0:f269e3021894 | 379 | // Get the old register value |
elessair | 0:f269e3021894 | 380 | tmpreg = i2c->OAR1; |
elessair | 0:f269e3021894 | 381 | |
elessair | 0:f269e3021894 | 382 | // Enable / disable slave |
elessair | 0:f269e3021894 | 383 | if (enable_slave == 1) { |
elessair | 0:f269e3021894 | 384 | tmpreg |= I2C_OAR1_OA1EN; |
elessair | 0:f269e3021894 | 385 | } else { |
elessair | 0:f269e3021894 | 386 | tmpreg &= (uint32_t)(~I2C_OAR1_OA1EN); |
elessair | 0:f269e3021894 | 387 | } |
elessair | 0:f269e3021894 | 388 | |
elessair | 0:f269e3021894 | 389 | // Set new mode |
elessair | 0:f269e3021894 | 390 | i2c->OAR1 = tmpreg; |
elessair | 0:f269e3021894 | 391 | |
elessair | 0:f269e3021894 | 392 | } |
elessair | 0:f269e3021894 | 393 | |
elessair | 0:f269e3021894 | 394 | // See I2CSlave.h |
elessair | 0:f269e3021894 | 395 | #define NoData 0 // the slave has not been addressed |
elessair | 0:f269e3021894 | 396 | #define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter) |
elessair | 0:f269e3021894 | 397 | #define WriteGeneral 2 // the master is writing to all slave |
elessair | 0:f269e3021894 | 398 | #define WriteAddressed 3 // the master is writing to this slave (slave = receiver) |
elessair | 0:f269e3021894 | 399 | |
elessair | 0:f269e3021894 | 400 | int i2c_slave_receive(i2c_t *obj) |
elessair | 0:f269e3021894 | 401 | { |
elessair | 0:f269e3021894 | 402 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
elessair | 0:f269e3021894 | 403 | int retValue = NoData; |
elessair | 0:f269e3021894 | 404 | |
elessair | 0:f269e3021894 | 405 | if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == 1) { |
elessair | 0:f269e3021894 | 406 | if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == 1) { |
elessair | 0:f269e3021894 | 407 | if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_DIR) == 1) |
elessair | 0:f269e3021894 | 408 | retValue = ReadAddressed; |
elessair | 0:f269e3021894 | 409 | else |
elessair | 0:f269e3021894 | 410 | retValue = WriteAddressed; |
elessair | 0:f269e3021894 | 411 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_ADDR); |
elessair | 0:f269e3021894 | 412 | } |
elessair | 0:f269e3021894 | 413 | } |
elessair | 0:f269e3021894 | 414 | |
elessair | 0:f269e3021894 | 415 | return (retValue); |
elessair | 0:f269e3021894 | 416 | } |
elessair | 0:f269e3021894 | 417 | |
elessair | 0:f269e3021894 | 418 | int i2c_slave_read(i2c_t *obj, char *data, int length) |
elessair | 0:f269e3021894 | 419 | { |
elessair | 0:f269e3021894 | 420 | char size = 0; |
elessair | 0:f269e3021894 | 421 | |
elessair | 0:f269e3021894 | 422 | while (size < length) data[size++] = (char)i2c_byte_read(obj, 0); |
elessair | 0:f269e3021894 | 423 | |
elessair | 0:f269e3021894 | 424 | return size; |
elessair | 0:f269e3021894 | 425 | } |
elessair | 0:f269e3021894 | 426 | |
elessair | 0:f269e3021894 | 427 | int i2c_slave_write(i2c_t *obj, const char *data, int length) |
elessair | 0:f269e3021894 | 428 | { |
elessair | 0:f269e3021894 | 429 | char size = 0; |
elessair | 0:f269e3021894 | 430 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
elessair | 0:f269e3021894 | 431 | |
elessair | 0:f269e3021894 | 432 | do { |
elessair | 0:f269e3021894 | 433 | i2c_byte_write(obj, data[size]); |
elessair | 0:f269e3021894 | 434 | size++; |
elessair | 0:f269e3021894 | 435 | } while (size < length); |
elessair | 0:f269e3021894 | 436 | |
elessair | 0:f269e3021894 | 437 | return size; |
elessair | 0:f269e3021894 | 438 | } |
elessair | 0:f269e3021894 | 439 | |
elessair | 0:f269e3021894 | 440 | |
elessair | 0:f269e3021894 | 441 | #endif // DEVICE_I2CSLAVE |
elessair | 0:f269e3021894 | 442 | |
elessair | 0:f269e3021894 | 443 | #endif // DEVICE_I2C |