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