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