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