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