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