mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_L053R8/i2c_api.c@251:de9a1e4ffd79, 2014-07-08 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Jul 08 14:15:07 2014 +0100
- Revision:
- 251:de9a1e4ffd79
- Parent:
- 250:a49055e7a707
- Child:
- 285:31249416b6f9
Synchronized with git revision b0ac1e3485a9aa1cb98d1ca68df9d1cdbcd6ce63
Full URL: https://github.com/mbedmicro/mbed/commit/b0ac1e3485a9aa1cb98d1ca68df9d1cdbcd6ce63/
Revert "error.h -> mbed_error.h"
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 | 251:de9a1e4ffd79 | 37 | #include "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 | 181:a4cbdfbbd2f4 | 63 | void i2c_init(i2c_t *obj, PinName sda, PinName scl) { |
mbed_official | 181:a4cbdfbbd2f4 | 64 | // Determine the I2C to use |
mbed_official | 181:a4cbdfbbd2f4 | 65 | I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); |
mbed_official | 181:a4cbdfbbd2f4 | 66 | I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); |
mbed_official | 181:a4cbdfbbd2f4 | 67 | |
mbed_official | 181:a4cbdfbbd2f4 | 68 | obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); |
mbed_official | 227:7bd0639b8911 | 69 | MBED_ASSERT(obj->i2c != (I2CName)NC); |
mbed_official | 181:a4cbdfbbd2f4 | 70 | |
mbed_official | 181:a4cbdfbbd2f4 | 71 | // Enable I2C clock |
mbed_official | 181:a4cbdfbbd2f4 | 72 | if (obj->i2c == I2C_1) { |
mbed_official | 210:45934c3d812a | 73 | __HAL_RCC_I2C1_CONFIG(RCC_I2C1CLKSOURCE_SYSCLK); |
mbed_official | 181:a4cbdfbbd2f4 | 74 | __I2C1_CLK_ENABLE(); |
mbed_official | 181:a4cbdfbbd2f4 | 75 | } |
mbed_official | 181:a4cbdfbbd2f4 | 76 | if (obj->i2c == I2C_2) { |
mbed_official | 181:a4cbdfbbd2f4 | 77 | __I2C2_CLK_ENABLE(); |
mbed_official | 181:a4cbdfbbd2f4 | 78 | } |
mbed_official | 181:a4cbdfbbd2f4 | 79 | |
mbed_official | 181:a4cbdfbbd2f4 | 80 | // Configure I2C pins |
mbed_official | 181:a4cbdfbbd2f4 | 81 | pinmap_pinout(sda, PinMap_I2C_SDA); |
mbed_official | 181:a4cbdfbbd2f4 | 82 | pinmap_pinout(scl, PinMap_I2C_SCL); |
mbed_official | 181:a4cbdfbbd2f4 | 83 | pin_mode(sda, OpenDrain); |
mbed_official | 181:a4cbdfbbd2f4 | 84 | pin_mode(scl, OpenDrain); |
mbed_official | 181:a4cbdfbbd2f4 | 85 | |
mbed_official | 181:a4cbdfbbd2f4 | 86 | // Reset to clear pending flags if any |
mbed_official | 181:a4cbdfbbd2f4 | 87 | i2c_reset(obj); |
mbed_official | 181:a4cbdfbbd2f4 | 88 | |
mbed_official | 181:a4cbdfbbd2f4 | 89 | // I2C configuration |
mbed_official | 181:a4cbdfbbd2f4 | 90 | i2c_frequency(obj, 100000); // 100 kHz per default |
mbed_official | 181:a4cbdfbbd2f4 | 91 | } |
mbed_official | 181:a4cbdfbbd2f4 | 92 | |
mbed_official | 181:a4cbdfbbd2f4 | 93 | void i2c_frequency(i2c_t *obj, int hz) { |
mbed_official | 227:7bd0639b8911 | 94 | MBED_ASSERT((hz == 100000) || (hz == 400000) || (hz == 1000000)); |
mbed_official | 181:a4cbdfbbd2f4 | 95 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 181:a4cbdfbbd2f4 | 96 | |
mbed_official | 181:a4cbdfbbd2f4 | 97 | // Common settings: I2C clock = 32 MHz, Analog filter = ON, Digital filter coefficient = 0 |
mbed_official | 181:a4cbdfbbd2f4 | 98 | switch (hz) { |
mbed_official | 181:a4cbdfbbd2f4 | 99 | case 100000: |
mbed_official | 181:a4cbdfbbd2f4 | 100 | I2cHandle.Init.Timing = 0x20602938; // Standard mode with Rise Time = 400ns and Fall Time = 100ns |
mbed_official | 181:a4cbdfbbd2f4 | 101 | break; |
mbed_official | 181:a4cbdfbbd2f4 | 102 | case 400000: |
mbed_official | 181:a4cbdfbbd2f4 | 103 | I2cHandle.Init.Timing = 0x00B0122A; // Fast mode with Rise Time = 250ns and Fall Time = 100ns |
mbed_official | 181:a4cbdfbbd2f4 | 104 | break; |
mbed_official | 181:a4cbdfbbd2f4 | 105 | case 1000000: |
mbed_official | 181:a4cbdfbbd2f4 | 106 | I2cHandle.Init.Timing = 0x0030040E; // Fast mode Plus with Rise Time = 60ns and Fall Time = 100ns |
mbed_official | 181:a4cbdfbbd2f4 | 107 | break; |
mbed_official | 181:a4cbdfbbd2f4 | 108 | default: |
mbed_official | 181:a4cbdfbbd2f4 | 109 | break; |
mbed_official | 181:a4cbdfbbd2f4 | 110 | } |
mbed_official | 181:a4cbdfbbd2f4 | 111 | |
mbed_official | 181:a4cbdfbbd2f4 | 112 | // I2C configuration |
mbed_official | 181:a4cbdfbbd2f4 | 113 | I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; |
mbed_official | 181:a4cbdfbbd2f4 | 114 | I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; |
mbed_official | 181:a4cbdfbbd2f4 | 115 | I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; |
mbed_official | 181:a4cbdfbbd2f4 | 116 | I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; |
mbed_official | 181:a4cbdfbbd2f4 | 117 | I2cHandle.Init.OwnAddress1 = 0; |
mbed_official | 181:a4cbdfbbd2f4 | 118 | I2cHandle.Init.OwnAddress2 = 0; |
mbed_official | 181:a4cbdfbbd2f4 | 119 | I2cHandle.Init.OwnAddress2Masks = I2C_OA2_NOMASK; |
mbed_official | 181:a4cbdfbbd2f4 | 120 | HAL_I2C_Init(&I2cHandle); |
mbed_official | 181:a4cbdfbbd2f4 | 121 | } |
mbed_official | 181:a4cbdfbbd2f4 | 122 | |
mbed_official | 181:a4cbdfbbd2f4 | 123 | inline int i2c_start(i2c_t *obj) { |
mbed_official | 181:a4cbdfbbd2f4 | 124 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 181:a4cbdfbbd2f4 | 125 | int timeout; |
mbed_official | 181:a4cbdfbbd2f4 | 126 | |
mbed_official | 181:a4cbdfbbd2f4 | 127 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 181:a4cbdfbbd2f4 | 128 | |
mbed_official | 181:a4cbdfbbd2f4 | 129 | // Clear Acknowledge failure flag |
mbed_official | 181:a4cbdfbbd2f4 | 130 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF); |
mbed_official | 181:a4cbdfbbd2f4 | 131 | |
mbed_official | 181:a4cbdfbbd2f4 | 132 | // Generate the START condition |
mbed_official | 181:a4cbdfbbd2f4 | 133 | i2c->CR2 |= I2C_CR2_START; |
mbed_official | 181:a4cbdfbbd2f4 | 134 | |
mbed_official | 181:a4cbdfbbd2f4 | 135 | // Wait the START condition has been correctly sent |
mbed_official | 181:a4cbdfbbd2f4 | 136 | timeout = FLAG_TIMEOUT; |
mbed_official | 181:a4cbdfbbd2f4 | 137 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == RESET) { |
mbed_official | 181:a4cbdfbbd2f4 | 138 | if ((timeout--) == 0) { |
mbed_official | 181:a4cbdfbbd2f4 | 139 | return 1; |
mbed_official | 181:a4cbdfbbd2f4 | 140 | } |
mbed_official | 181:a4cbdfbbd2f4 | 141 | } |
mbed_official | 181:a4cbdfbbd2f4 | 142 | |
mbed_official | 181:a4cbdfbbd2f4 | 143 | return 0; |
mbed_official | 181:a4cbdfbbd2f4 | 144 | } |
mbed_official | 181:a4cbdfbbd2f4 | 145 | |
mbed_official | 181:a4cbdfbbd2f4 | 146 | inline int i2c_stop(i2c_t *obj) { |
mbed_official | 181:a4cbdfbbd2f4 | 147 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 181:a4cbdfbbd2f4 | 148 | |
mbed_official | 181:a4cbdfbbd2f4 | 149 | // Generate the STOP condition |
mbed_official | 181:a4cbdfbbd2f4 | 150 | i2c->CR2 |= I2C_CR2_STOP; |
mbed_official | 181:a4cbdfbbd2f4 | 151 | |
mbed_official | 181:a4cbdfbbd2f4 | 152 | return 0; |
mbed_official | 181:a4cbdfbbd2f4 | 153 | } |
mbed_official | 181:a4cbdfbbd2f4 | 154 | |
mbed_official | 181:a4cbdfbbd2f4 | 155 | int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { |
mbed_official | 202:bd6461c07541 | 156 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 202:bd6461c07541 | 157 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 202:bd6461c07541 | 158 | int timeout; |
mbed_official | 202:bd6461c07541 | 159 | int count; |
mbed_official | 202:bd6461c07541 | 160 | int value; |
mbed_official | 202:bd6461c07541 | 161 | |
mbed_official | 202:bd6461c07541 | 162 | /* update CR2 register */ |
mbed_official | 202:bd6461c07541 | 163 | 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 | 164 | | (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 | 165 | |
mbed_official | 202:bd6461c07541 | 166 | // Read all bytes |
mbed_official | 202:bd6461c07541 | 167 | for (count = 0; count < length; count++) { |
mbed_official | 202:bd6461c07541 | 168 | value = i2c_byte_read(obj, 0); |
mbed_official | 202:bd6461c07541 | 169 | data[count] = (char)value; |
mbed_official | 202:bd6461c07541 | 170 | } |
mbed_official | 181:a4cbdfbbd2f4 | 171 | |
mbed_official | 202:bd6461c07541 | 172 | // Wait transfer complete |
mbed_official | 202:bd6461c07541 | 173 | timeout = FLAG_TIMEOUT; |
mbed_official | 202:bd6461c07541 | 174 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) { |
mbed_official | 202:bd6461c07541 | 175 | timeout--; |
mbed_official | 202:bd6461c07541 | 176 | if (timeout == 0) { |
mbed_official | 247:135e3186a638 | 177 | return -1; |
mbed_official | 202:bd6461c07541 | 178 | } |
mbed_official | 202:bd6461c07541 | 179 | } |
mbed_official | 211:fb8edfff6ae1 | 180 | |
mbed_official | 211:fb8edfff6ae1 | 181 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC); |
mbed_official | 202:bd6461c07541 | 182 | |
mbed_official | 202:bd6461c07541 | 183 | // If not repeated start, send stop. |
mbed_official | 202:bd6461c07541 | 184 | if (stop) { |
mbed_official | 202:bd6461c07541 | 185 | i2c_stop(obj); |
mbed_official | 202:bd6461c07541 | 186 | /* Wait until STOPF flag is set */ |
mbed_official | 202:bd6461c07541 | 187 | timeout = FLAG_TIMEOUT; |
mbed_official | 202:bd6461c07541 | 188 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) { |
mbed_official | 202:bd6461c07541 | 189 | timeout--; |
mbed_official | 202:bd6461c07541 | 190 | if (timeout == 0) { |
mbed_official | 247:135e3186a638 | 191 | return -1; |
mbed_official | 202:bd6461c07541 | 192 | } |
mbed_official | 202:bd6461c07541 | 193 | } |
mbed_official | 202:bd6461c07541 | 194 | /* Clear STOP Flag */ |
mbed_official | 202:bd6461c07541 | 195 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF); |
mbed_official | 181:a4cbdfbbd2f4 | 196 | } |
mbed_official | 181:a4cbdfbbd2f4 | 197 | |
mbed_official | 181:a4cbdfbbd2f4 | 198 | return length; |
mbed_official | 181:a4cbdfbbd2f4 | 199 | } |
mbed_official | 181:a4cbdfbbd2f4 | 200 | |
mbed_official | 181:a4cbdfbbd2f4 | 201 | int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { |
mbed_official | 202:bd6461c07541 | 202 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 202:bd6461c07541 | 203 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 202:bd6461c07541 | 204 | int timeout; |
mbed_official | 202:bd6461c07541 | 205 | int count; |
mbed_official | 202:bd6461c07541 | 206 | |
mbed_official | 202:bd6461c07541 | 207 | /* update CR2 register */ |
mbed_official | 202:bd6461c07541 | 208 | 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 | 209 | | (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 | 210 | |
mbed_official | 202:bd6461c07541 | 211 | for (count = 0; count < length; count++) { |
mbed_official | 202:bd6461c07541 | 212 | i2c_byte_write(obj, data[count]); |
mbed_official | 181:a4cbdfbbd2f4 | 213 | } |
mbed_official | 181:a4cbdfbbd2f4 | 214 | |
mbed_official | 202:bd6461c07541 | 215 | // Wait transfer complete |
mbed_official | 202:bd6461c07541 | 216 | timeout = FLAG_TIMEOUT; |
mbed_official | 202:bd6461c07541 | 217 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) { |
mbed_official | 202:bd6461c07541 | 218 | timeout--; |
mbed_official | 202:bd6461c07541 | 219 | if (timeout == 0) { |
mbed_official | 247:135e3186a638 | 220 | return -1; |
mbed_official | 211:fb8edfff6ae1 | 221 | } |
mbed_official | 202:bd6461c07541 | 222 | } |
mbed_official | 211:fb8edfff6ae1 | 223 | |
mbed_official | 211:fb8edfff6ae1 | 224 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC); |
mbed_official | 202:bd6461c07541 | 225 | |
mbed_official | 202:bd6461c07541 | 226 | // If not repeated start, send stop. |
mbed_official | 202:bd6461c07541 | 227 | if (stop) { |
mbed_official | 202:bd6461c07541 | 228 | i2c_stop(obj); |
mbed_official | 202:bd6461c07541 | 229 | /* Wait until STOPF flag is set */ |
mbed_official | 202:bd6461c07541 | 230 | timeout = FLAG_TIMEOUT; |
mbed_official | 202:bd6461c07541 | 231 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) { |
mbed_official | 202:bd6461c07541 | 232 | timeout--; |
mbed_official | 202:bd6461c07541 | 233 | if (timeout == 0) { |
mbed_official | 247:135e3186a638 | 234 | return -1; |
mbed_official | 202:bd6461c07541 | 235 | } |
mbed_official | 202:bd6461c07541 | 236 | } |
mbed_official | 202:bd6461c07541 | 237 | /* Clear STOP Flag */ |
mbed_official | 202:bd6461c07541 | 238 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF); |
mbed_official | 202:bd6461c07541 | 239 | } |
mbed_official | 202:bd6461c07541 | 240 | |
mbed_official | 202:bd6461c07541 | 241 | return count; |
mbed_official | 181:a4cbdfbbd2f4 | 242 | } |
mbed_official | 181:a4cbdfbbd2f4 | 243 | |
mbed_official | 181:a4cbdfbbd2f4 | 244 | int i2c_byte_read(i2c_t *obj, int last) { |
mbed_official | 181:a4cbdfbbd2f4 | 245 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 181:a4cbdfbbd2f4 | 246 | int timeout; |
mbed_official | 181:a4cbdfbbd2f4 | 247 | |
mbed_official | 181:a4cbdfbbd2f4 | 248 | // Wait until the byte is received |
mbed_official | 181:a4cbdfbbd2f4 | 249 | timeout = FLAG_TIMEOUT; |
mbed_official | 181:a4cbdfbbd2f4 | 250 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) { |
mbed_official | 181:a4cbdfbbd2f4 | 251 | if ((timeout--) == 0) { |
mbed_official | 247:135e3186a638 | 252 | return -1; |
mbed_official | 181:a4cbdfbbd2f4 | 253 | } |
mbed_official | 181:a4cbdfbbd2f4 | 254 | } |
mbed_official | 181:a4cbdfbbd2f4 | 255 | |
mbed_official | 181:a4cbdfbbd2f4 | 256 | return (int)i2c->RXDR; |
mbed_official | 181:a4cbdfbbd2f4 | 257 | } |
mbed_official | 181:a4cbdfbbd2f4 | 258 | |
mbed_official | 181:a4cbdfbbd2f4 | 259 | int i2c_byte_write(i2c_t *obj, int data) { |
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 | 202:bd6461c07541 | 263 | // Wait until the previous byte is transmitted |
mbed_official | 181:a4cbdfbbd2f4 | 264 | timeout = FLAG_TIMEOUT; |
mbed_official | 202:bd6461c07541 | 265 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXIS) == RESET) { |
mbed_official | 181:a4cbdfbbd2f4 | 266 | if ((timeout--) == 0) { |
mbed_official | 181:a4cbdfbbd2f4 | 267 | return 0; |
mbed_official | 181:a4cbdfbbd2f4 | 268 | } |
mbed_official | 181:a4cbdfbbd2f4 | 269 | } |
mbed_official | 181:a4cbdfbbd2f4 | 270 | |
mbed_official | 202:bd6461c07541 | 271 | i2c->TXDR = (uint8_t)data; |
mbed_official | 211:fb8edfff6ae1 | 272 | |
mbed_official | 181:a4cbdfbbd2f4 | 273 | return 1; |
mbed_official | 181:a4cbdfbbd2f4 | 274 | } |
mbed_official | 181:a4cbdfbbd2f4 | 275 | |
mbed_official | 181:a4cbdfbbd2f4 | 276 | void i2c_reset(i2c_t *obj) { |
mbed_official | 181:a4cbdfbbd2f4 | 277 | if (obj->i2c == I2C_1) { |
mbed_official | 181:a4cbdfbbd2f4 | 278 | __I2C1_FORCE_RESET(); |
mbed_official | 181:a4cbdfbbd2f4 | 279 | __I2C1_RELEASE_RESET(); |
mbed_official | 181:a4cbdfbbd2f4 | 280 | } |
mbed_official | 181:a4cbdfbbd2f4 | 281 | if (obj->i2c == I2C_2) { |
mbed_official | 181:a4cbdfbbd2f4 | 282 | __I2C2_FORCE_RESET(); |
mbed_official | 181:a4cbdfbbd2f4 | 283 | __I2C2_RELEASE_RESET(); |
mbed_official | 181:a4cbdfbbd2f4 | 284 | } |
mbed_official | 181:a4cbdfbbd2f4 | 285 | } |
mbed_official | 181:a4cbdfbbd2f4 | 286 | |
mbed_official | 181:a4cbdfbbd2f4 | 287 | #if DEVICE_I2CSLAVE |
mbed_official | 181:a4cbdfbbd2f4 | 288 | |
mbed_official | 181:a4cbdfbbd2f4 | 289 | void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) { |
mbed_official | 181:a4cbdfbbd2f4 | 290 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 181:a4cbdfbbd2f4 | 291 | uint16_t tmpreg; |
mbed_official | 181:a4cbdfbbd2f4 | 292 | |
mbed_official | 181:a4cbdfbbd2f4 | 293 | // disable |
mbed_official | 202:bd6461c07541 | 294 | i2c->OAR1 &= (uint32_t)(~I2C_OAR1_OA1EN); |
mbed_official | 181:a4cbdfbbd2f4 | 295 | // Get the old register value |
mbed_official | 181:a4cbdfbbd2f4 | 296 | tmpreg = i2c->OAR1; |
mbed_official | 181:a4cbdfbbd2f4 | 297 | // Reset address bits |
mbed_official | 181:a4cbdfbbd2f4 | 298 | tmpreg &= 0xFC00; |
mbed_official | 181:a4cbdfbbd2f4 | 299 | // Set new address |
mbed_official | 181:a4cbdfbbd2f4 | 300 | tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits |
mbed_official | 181:a4cbdfbbd2f4 | 301 | // Store the new register value |
mbed_official | 181:a4cbdfbbd2f4 | 302 | i2c->OAR1 = tmpreg; |
mbed_official | 181:a4cbdfbbd2f4 | 303 | // enable |
mbed_official | 181:a4cbdfbbd2f4 | 304 | i2c->OAR1 |= I2C_OAR1_OA1EN; |
mbed_official | 181:a4cbdfbbd2f4 | 305 | } |
mbed_official | 181:a4cbdfbbd2f4 | 306 | |
mbed_official | 181:a4cbdfbbd2f4 | 307 | void i2c_slave_mode(i2c_t *obj, int enable_slave) { |
mbed_official | 181:a4cbdfbbd2f4 | 308 | |
mbed_official | 181:a4cbdfbbd2f4 | 309 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 181:a4cbdfbbd2f4 | 310 | uint16_t tmpreg; |
mbed_official | 181:a4cbdfbbd2f4 | 311 | |
mbed_official | 181:a4cbdfbbd2f4 | 312 | // Get the old register value |
mbed_official | 181:a4cbdfbbd2f4 | 313 | tmpreg = i2c->OAR1; |
mbed_official | 181:a4cbdfbbd2f4 | 314 | |
mbed_official | 181:a4cbdfbbd2f4 | 315 | // Enable / disable slave |
mbed_official | 181:a4cbdfbbd2f4 | 316 | if (enable_slave == 1) { |
mbed_official | 181:a4cbdfbbd2f4 | 317 | tmpreg |= I2C_OAR1_OA1EN; |
mbed_official | 181:a4cbdfbbd2f4 | 318 | } else { |
mbed_official | 202:bd6461c07541 | 319 | tmpreg &= (uint32_t)(~I2C_OAR1_OA1EN); |
mbed_official | 181:a4cbdfbbd2f4 | 320 | } |
mbed_official | 181:a4cbdfbbd2f4 | 321 | |
mbed_official | 181:a4cbdfbbd2f4 | 322 | // Set new mode |
mbed_official | 181:a4cbdfbbd2f4 | 323 | i2c->OAR1 = tmpreg; |
mbed_official | 181:a4cbdfbbd2f4 | 324 | |
mbed_official | 181:a4cbdfbbd2f4 | 325 | } |
mbed_official | 181:a4cbdfbbd2f4 | 326 | |
mbed_official | 181:a4cbdfbbd2f4 | 327 | // See I2CSlave.h |
mbed_official | 181:a4cbdfbbd2f4 | 328 | #define NoData 0 // the slave has not been addressed |
mbed_official | 181:a4cbdfbbd2f4 | 329 | #define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter) |
mbed_official | 181:a4cbdfbbd2f4 | 330 | #define WriteGeneral 2 // the master is writing to all slave |
mbed_official | 181:a4cbdfbbd2f4 | 331 | #define WriteAddressed 3 // the master is writing to this slave (slave = receiver) |
mbed_official | 181:a4cbdfbbd2f4 | 332 | |
mbed_official | 181:a4cbdfbbd2f4 | 333 | int i2c_slave_receive(i2c_t *obj) { |
mbed_official | 202:bd6461c07541 | 334 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 181:a4cbdfbbd2f4 | 335 | int retValue = NoData; |
mbed_official | 181:a4cbdfbbd2f4 | 336 | |
mbed_official | 181:a4cbdfbbd2f4 | 337 | if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == 1) { |
mbed_official | 181:a4cbdfbbd2f4 | 338 | if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == 1) { |
mbed_official | 181:a4cbdfbbd2f4 | 339 | if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_DIR) == 1) |
mbed_official | 181:a4cbdfbbd2f4 | 340 | retValue = ReadAddressed; |
mbed_official | 181:a4cbdfbbd2f4 | 341 | else |
mbed_official | 181:a4cbdfbbd2f4 | 342 | retValue = WriteAddressed; |
mbed_official | 181:a4cbdfbbd2f4 | 343 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_ADDR); |
mbed_official | 181:a4cbdfbbd2f4 | 344 | } |
mbed_official | 181:a4cbdfbbd2f4 | 345 | } |
mbed_official | 181:a4cbdfbbd2f4 | 346 | |
mbed_official | 181:a4cbdfbbd2f4 | 347 | return (retValue); |
mbed_official | 181:a4cbdfbbd2f4 | 348 | } |
mbed_official | 181:a4cbdfbbd2f4 | 349 | |
mbed_official | 181:a4cbdfbbd2f4 | 350 | int i2c_slave_read(i2c_t *obj, char *data, int length) { |
mbed_official | 202:bd6461c07541 | 351 | char size = 0; |
mbed_official | 211:fb8edfff6ae1 | 352 | |
mbed_official | 202:bd6461c07541 | 353 | while (size < length) data[size++] = (char)i2c_byte_read(obj, 0); |
mbed_official | 181:a4cbdfbbd2f4 | 354 | |
mbed_official | 202:bd6461c07541 | 355 | return size; |
mbed_official | 181:a4cbdfbbd2f4 | 356 | } |
mbed_official | 181:a4cbdfbbd2f4 | 357 | |
mbed_official | 181:a4cbdfbbd2f4 | 358 | int i2c_slave_write(i2c_t *obj, const char *data, int length) { |
mbed_official | 181:a4cbdfbbd2f4 | 359 | char size = 0; |
mbed_official | 202:bd6461c07541 | 360 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 181:a4cbdfbbd2f4 | 361 | |
mbed_official | 181:a4cbdfbbd2f4 | 362 | do { |
mbed_official | 181:a4cbdfbbd2f4 | 363 | i2c_byte_write(obj, data[size]); |
mbed_official | 181:a4cbdfbbd2f4 | 364 | size++; |
mbed_official | 181:a4cbdfbbd2f4 | 365 | } while (size < length); |
mbed_official | 181:a4cbdfbbd2f4 | 366 | |
mbed_official | 181:a4cbdfbbd2f4 | 367 | return size; |
mbed_official | 181:a4cbdfbbd2f4 | 368 | } |
mbed_official | 181:a4cbdfbbd2f4 | 369 | |
mbed_official | 181:a4cbdfbbd2f4 | 370 | |
mbed_official | 181:a4cbdfbbd2f4 | 371 | #endif // DEVICE_I2CSLAVE |
mbed_official | 181:a4cbdfbbd2f4 | 372 | |
mbed_official | 181:a4cbdfbbd2f4 | 373 | #endif // DEVICE_I2C |