Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: SANFAN_read_analog_value nucleo-wdg Nucleo_sleep_copy
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_STM32F7/i2c_api.c@573:ad23fe03a082, 2015-06-19 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Jun 19 09:15:11 2015 +0100
- Revision:
- 573:ad23fe03a082
- Child:
- 582:a89625bcd809
Synchronized with git revision d47834cd4d729e5b36b4c1ad4650f8b8f6a9ab86
Full URL: https://github.com/mbedmicro/mbed/commit/d47834cd4d729e5b36b4c1ad4650f8b8f6a9ab86/
DISCO_F746NG - Add new target
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| mbed_official | 573:ad23fe03a082 | 1 | /* mbed Microcontroller Library | 
| mbed_official | 573:ad23fe03a082 | 2 | ******************************************************************************* | 
| mbed_official | 573:ad23fe03a082 | 3 | * Copyright (c) 2015, STMicroelectronics | 
| mbed_official | 573:ad23fe03a082 | 4 | * All rights reserved. | 
| mbed_official | 573:ad23fe03a082 | 5 | * | 
| mbed_official | 573:ad23fe03a082 | 6 | * Redistribution and use in source and binary forms, with or without | 
| mbed_official | 573:ad23fe03a082 | 7 | * modification, are permitted provided that the following conditions are met: | 
| mbed_official | 573:ad23fe03a082 | 8 | * | 
| mbed_official | 573:ad23fe03a082 | 9 | * 1. Redistributions of source code must retain the above copyright notice, | 
| mbed_official | 573:ad23fe03a082 | 10 | * this list of conditions and the following disclaimer. | 
| mbed_official | 573:ad23fe03a082 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, | 
| mbed_official | 573:ad23fe03a082 | 12 | * this list of conditions and the following disclaimer in the documentation | 
| mbed_official | 573:ad23fe03a082 | 13 | * and/or other materials provided with the distribution. | 
| mbed_official | 573:ad23fe03a082 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors | 
| mbed_official | 573:ad23fe03a082 | 15 | * may be used to endorse or promote products derived from this software | 
| mbed_official | 573:ad23fe03a082 | 16 | * without specific prior written permission. | 
| mbed_official | 573:ad23fe03a082 | 17 | * | 
| mbed_official | 573:ad23fe03a082 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
| mbed_official | 573:ad23fe03a082 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
| mbed_official | 573:ad23fe03a082 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
| mbed_official | 573:ad23fe03a082 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
| mbed_official | 573:ad23fe03a082 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
| mbed_official | 573:ad23fe03a082 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
| mbed_official | 573:ad23fe03a082 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
| mbed_official | 573:ad23fe03a082 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
| mbed_official | 573:ad23fe03a082 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
| mbed_official | 573:ad23fe03a082 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
| mbed_official | 573:ad23fe03a082 | 28 | ******************************************************************************* | 
| mbed_official | 573:ad23fe03a082 | 29 | */ | 
| mbed_official | 573:ad23fe03a082 | 30 | #include "mbed_assert.h" | 
| mbed_official | 573:ad23fe03a082 | 31 | #include "i2c_api.h" | 
| mbed_official | 573:ad23fe03a082 | 32 | |
| mbed_official | 573:ad23fe03a082 | 33 | #if DEVICE_I2C | 
| mbed_official | 573:ad23fe03a082 | 34 | |
| mbed_official | 573:ad23fe03a082 | 35 | #include "cmsis.h" | 
| mbed_official | 573:ad23fe03a082 | 36 | #include "pinmap.h" | 
| mbed_official | 573:ad23fe03a082 | 37 | #include "PeripheralPins.h" | 
| mbed_official | 573:ad23fe03a082 | 38 | |
| mbed_official | 573:ad23fe03a082 | 39 | /* Timeout values for flags and events waiting loops. These timeouts are | 
| mbed_official | 573:ad23fe03a082 | 40 | not based on accurate values, they just guarantee that the application will | 
| mbed_official | 573:ad23fe03a082 | 41 | not remain stuck if the I2C communication is corrupted. */ | 
| mbed_official | 573:ad23fe03a082 | 42 | #define FLAG_TIMEOUT ((int)0x1000) | 
| mbed_official | 573:ad23fe03a082 | 43 | #define LONG_TIMEOUT ((int)0x8000) | 
| mbed_official | 573:ad23fe03a082 | 44 | |
| mbed_official | 573:ad23fe03a082 | 45 | I2C_HandleTypeDef I2cHandle; | 
| mbed_official | 573:ad23fe03a082 | 46 | |
| mbed_official | 573:ad23fe03a082 | 47 | int i2c1_inited = 0; | 
| mbed_official | 573:ad23fe03a082 | 48 | int i2c2_inited = 0; | 
| mbed_official | 573:ad23fe03a082 | 49 | int i2c3_inited = 0; | 
| mbed_official | 573:ad23fe03a082 | 50 | |
| mbed_official | 573:ad23fe03a082 | 51 | void i2c_init(i2c_t *obj, PinName sda, PinName scl) | 
| mbed_official | 573:ad23fe03a082 | 52 | { | 
| mbed_official | 573:ad23fe03a082 | 53 | // Determine the I2C to use | 
| mbed_official | 573:ad23fe03a082 | 54 | I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); | 
| mbed_official | 573:ad23fe03a082 | 55 | I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); | 
| mbed_official | 573:ad23fe03a082 | 56 | |
| mbed_official | 573:ad23fe03a082 | 57 | obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); | 
| mbed_official | 573:ad23fe03a082 | 58 | MBED_ASSERT(obj->i2c != (I2CName)NC); | 
| mbed_official | 573:ad23fe03a082 | 59 | |
| mbed_official | 573:ad23fe03a082 | 60 | // Enable I2C1 clock and pinout if not done | 
| mbed_official | 573:ad23fe03a082 | 61 | if ((obj->i2c == I2C_1) && !i2c1_inited) { | 
| mbed_official | 573:ad23fe03a082 | 62 | i2c1_inited = 1; | 
| mbed_official | 573:ad23fe03a082 | 63 | __I2C1_CLK_ENABLE(); | 
| mbed_official | 573:ad23fe03a082 | 64 | // Configure I2C pins | 
| mbed_official | 573:ad23fe03a082 | 65 | pinmap_pinout(sda, PinMap_I2C_SDA); | 
| mbed_official | 573:ad23fe03a082 | 66 | pinmap_pinout(scl, PinMap_I2C_SCL); | 
| mbed_official | 573:ad23fe03a082 | 67 | pin_mode(sda, OpenDrain); | 
| mbed_official | 573:ad23fe03a082 | 68 | pin_mode(scl, OpenDrain); | 
| mbed_official | 573:ad23fe03a082 | 69 | } | 
| mbed_official | 573:ad23fe03a082 | 70 | // Enable I2C2 clock and pinout if not done | 
| mbed_official | 573:ad23fe03a082 | 71 | if ((obj->i2c == I2C_2) && !i2c2_inited) { | 
| mbed_official | 573:ad23fe03a082 | 72 | i2c2_inited = 1; | 
| mbed_official | 573:ad23fe03a082 | 73 | __I2C2_CLK_ENABLE(); | 
| mbed_official | 573:ad23fe03a082 | 74 | // Configure I2C pins | 
| mbed_official | 573:ad23fe03a082 | 75 | pinmap_pinout(sda, PinMap_I2C_SDA); | 
| mbed_official | 573:ad23fe03a082 | 76 | pinmap_pinout(scl, PinMap_I2C_SCL); | 
| mbed_official | 573:ad23fe03a082 | 77 | pin_mode(sda, OpenDrain); | 
| mbed_official | 573:ad23fe03a082 | 78 | pin_mode(scl, OpenDrain); | 
| mbed_official | 573:ad23fe03a082 | 79 | } | 
| mbed_official | 573:ad23fe03a082 | 80 | // Enable I2C3 clock and pinout if not done | 
| mbed_official | 573:ad23fe03a082 | 81 | if ((obj->i2c == I2C_3) && !i2c3_inited) { | 
| mbed_official | 573:ad23fe03a082 | 82 | i2c3_inited = 1; | 
| mbed_official | 573:ad23fe03a082 | 83 | __I2C3_CLK_ENABLE(); | 
| mbed_official | 573:ad23fe03a082 | 84 | // Configure I2C pins | 
| mbed_official | 573:ad23fe03a082 | 85 | pinmap_pinout(sda, PinMap_I2C_SDA); | 
| mbed_official | 573:ad23fe03a082 | 86 | pinmap_pinout(scl, PinMap_I2C_SCL); | 
| mbed_official | 573:ad23fe03a082 | 87 | pin_mode(sda, OpenDrain); | 
| mbed_official | 573:ad23fe03a082 | 88 | pin_mode(scl, OpenDrain); | 
| mbed_official | 573:ad23fe03a082 | 89 | } | 
| mbed_official | 573:ad23fe03a082 | 90 | |
| mbed_official | 573:ad23fe03a082 | 91 | // Reset to clear pending flags if any | 
| mbed_official | 573:ad23fe03a082 | 92 | i2c_reset(obj); | 
| mbed_official | 573:ad23fe03a082 | 93 | |
| mbed_official | 573:ad23fe03a082 | 94 | // I2C configuration | 
| mbed_official | 573:ad23fe03a082 | 95 | i2c_frequency(obj, 100000); // 100 kHz per default | 
| mbed_official | 573:ad23fe03a082 | 96 | |
| mbed_official | 573:ad23fe03a082 | 97 | // I2C master by default | 
| mbed_official | 573:ad23fe03a082 | 98 | obj->slave = 0; | 
| mbed_official | 573:ad23fe03a082 | 99 | } | 
| mbed_official | 573:ad23fe03a082 | 100 | |
| mbed_official | 573:ad23fe03a082 | 101 | void i2c_frequency(i2c_t *obj, int hz) | 
| mbed_official | 573:ad23fe03a082 | 102 | { | 
| mbed_official | 573:ad23fe03a082 | 103 | MBED_ASSERT((hz != 0) && (hz <= 400000)); | 
| mbed_official | 573:ad23fe03a082 | 104 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); | 
| mbed_official | 573:ad23fe03a082 | 105 | int timeout; | 
| mbed_official | 573:ad23fe03a082 | 106 | |
| mbed_official | 573:ad23fe03a082 | 107 | // wait before init | 
| mbed_official | 573:ad23fe03a082 | 108 | timeout = LONG_TIMEOUT; | 
| mbed_official | 573:ad23fe03a082 | 109 | while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0)); | 
| mbed_official | 573:ad23fe03a082 | 110 | |
| mbed_official | 573:ad23fe03a082 | 111 | // I2C configuration | 
| mbed_official | 573:ad23fe03a082 | 112 | I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; | 
| mbed_official | 573:ad23fe03a082 | 113 | I2cHandle.Init.ClockSpeed = hz; | 
| mbed_official | 573:ad23fe03a082 | 114 | I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; | 
| mbed_official | 573:ad23fe03a082 | 115 | I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2; | 
| mbed_official | 573:ad23fe03a082 | 116 | I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; | 
| mbed_official | 573:ad23fe03a082 | 117 | I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; | 
| mbed_official | 573:ad23fe03a082 | 118 | I2cHandle.Init.OwnAddress1 = 0; | 
| mbed_official | 573:ad23fe03a082 | 119 | I2cHandle.Init.OwnAddress2 = 0; | 
| mbed_official | 573:ad23fe03a082 | 120 | HAL_I2C_Init(&I2cHandle); | 
| mbed_official | 573:ad23fe03a082 | 121 | if (obj->slave) { | 
| mbed_official | 573:ad23fe03a082 | 122 | /* Enable Address Acknowledge */ | 
| mbed_official | 573:ad23fe03a082 | 123 | I2cHandle.Instance->CR1 |= I2C_CR1_ACK; | 
| mbed_official | 573:ad23fe03a082 | 124 | } | 
| mbed_official | 573:ad23fe03a082 | 125 | |
| mbed_official | 573:ad23fe03a082 | 126 | } | 
| mbed_official | 573:ad23fe03a082 | 127 | |
| mbed_official | 573:ad23fe03a082 | 128 | inline int i2c_start(i2c_t *obj) | 
| mbed_official | 573:ad23fe03a082 | 129 | { | 
| mbed_official | 573:ad23fe03a082 | 130 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); | 
| mbed_official | 573:ad23fe03a082 | 131 | int timeout; | 
| mbed_official | 573:ad23fe03a082 | 132 | |
| mbed_official | 573:ad23fe03a082 | 133 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); | 
| mbed_official | 573:ad23fe03a082 | 134 | |
| mbed_official | 573:ad23fe03a082 | 135 | // Clear Acknowledge failure flag | 
| mbed_official | 573:ad23fe03a082 | 136 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF); | 
| mbed_official | 573:ad23fe03a082 | 137 | |
| mbed_official | 573:ad23fe03a082 | 138 | // Generate the START condition | 
| mbed_official | 573:ad23fe03a082 | 139 | i2c->CR1 |= I2C_CR1_START; | 
| mbed_official | 573:ad23fe03a082 | 140 | |
| mbed_official | 573:ad23fe03a082 | 141 | // Wait the START condition has been correctly sent | 
| mbed_official | 573:ad23fe03a082 | 142 | timeout = FLAG_TIMEOUT; | 
| mbed_official | 573:ad23fe03a082 | 143 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_SB) == RESET) { | 
| mbed_official | 573:ad23fe03a082 | 144 | if ((timeout--) == 0) { | 
| mbed_official | 573:ad23fe03a082 | 145 | return 1; | 
| mbed_official | 573:ad23fe03a082 | 146 | } | 
| mbed_official | 573:ad23fe03a082 | 147 | } | 
| mbed_official | 573:ad23fe03a082 | 148 | |
| mbed_official | 573:ad23fe03a082 | 149 | return 0; | 
| mbed_official | 573:ad23fe03a082 | 150 | } | 
| mbed_official | 573:ad23fe03a082 | 151 | |
| mbed_official | 573:ad23fe03a082 | 152 | inline int i2c_stop(i2c_t *obj) | 
| mbed_official | 573:ad23fe03a082 | 153 | { | 
| mbed_official | 573:ad23fe03a082 | 154 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); | 
| mbed_official | 573:ad23fe03a082 | 155 | |
| mbed_official | 573:ad23fe03a082 | 156 | // Generate the STOP condition | 
| mbed_official | 573:ad23fe03a082 | 157 | i2c->CR1 |= I2C_CR1_STOP; | 
| mbed_official | 573:ad23fe03a082 | 158 | |
| mbed_official | 573:ad23fe03a082 | 159 | return 0; | 
| mbed_official | 573:ad23fe03a082 | 160 | } | 
| mbed_official | 573:ad23fe03a082 | 161 | |
| mbed_official | 573:ad23fe03a082 | 162 | int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) | 
| mbed_official | 573:ad23fe03a082 | 163 | { | 
| mbed_official | 573:ad23fe03a082 | 164 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); | 
| mbed_official | 573:ad23fe03a082 | 165 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); | 
| mbed_official | 573:ad23fe03a082 | 166 | int timeout; | 
| mbed_official | 573:ad23fe03a082 | 167 | int count; | 
| mbed_official | 573:ad23fe03a082 | 168 | int value; | 
| mbed_official | 573:ad23fe03a082 | 169 | |
| mbed_official | 573:ad23fe03a082 | 170 | i2c_start(obj); | 
| mbed_official | 573:ad23fe03a082 | 171 | |
| mbed_official | 573:ad23fe03a082 | 172 | // Wait until SB flag is set | 
| mbed_official | 573:ad23fe03a082 | 173 | timeout = FLAG_TIMEOUT; | 
| mbed_official | 573:ad23fe03a082 | 174 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_SB) == RESET) { | 
| mbed_official | 573:ad23fe03a082 | 175 | timeout--; | 
| mbed_official | 573:ad23fe03a082 | 176 | if (timeout == 0) { | 
| mbed_official | 573:ad23fe03a082 | 177 | return -1; | 
| mbed_official | 573:ad23fe03a082 | 178 | } | 
| mbed_official | 573:ad23fe03a082 | 179 | } | 
| mbed_official | 573:ad23fe03a082 | 180 | |
| mbed_official | 573:ad23fe03a082 | 181 | i2c->DR = __HAL_I2C_7BIT_ADD_READ(address); | 
| mbed_official | 573:ad23fe03a082 | 182 | |
| mbed_official | 573:ad23fe03a082 | 183 | |
| mbed_official | 573:ad23fe03a082 | 184 | // Wait address is acknowledged | 
| mbed_official | 573:ad23fe03a082 | 185 | timeout = FLAG_TIMEOUT; | 
| mbed_official | 573:ad23fe03a082 | 186 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == RESET) { | 
| mbed_official | 573:ad23fe03a082 | 187 | timeout--; | 
| mbed_official | 573:ad23fe03a082 | 188 | if (timeout == 0) { | 
| mbed_official | 573:ad23fe03a082 | 189 | return -1; | 
| mbed_official | 573:ad23fe03a082 | 190 | } | 
| mbed_official | 573:ad23fe03a082 | 191 | } | 
| mbed_official | 573:ad23fe03a082 | 192 | __HAL_I2C_CLEAR_ADDRFLAG(&I2cHandle); | 
| mbed_official | 573:ad23fe03a082 | 193 | |
| mbed_official | 573:ad23fe03a082 | 194 | // Read all bytes except last one | 
| mbed_official | 573:ad23fe03a082 | 195 | for (count = 0; count < (length - 1); count++) { | 
| mbed_official | 573:ad23fe03a082 | 196 | value = i2c_byte_read(obj, 0); | 
| mbed_official | 573:ad23fe03a082 | 197 | data[count] = (char)value; | 
| mbed_official | 573:ad23fe03a082 | 198 | } | 
| mbed_official | 573:ad23fe03a082 | 199 | |
| mbed_official | 573:ad23fe03a082 | 200 | // If not repeated start, send stop. | 
| mbed_official | 573:ad23fe03a082 | 201 | // Warning: must be done BEFORE the data is read. | 
| mbed_official | 573:ad23fe03a082 | 202 | if (stop) { | 
| mbed_official | 573:ad23fe03a082 | 203 | i2c_stop(obj); | 
| mbed_official | 573:ad23fe03a082 | 204 | } | 
| mbed_official | 573:ad23fe03a082 | 205 | |
| mbed_official | 573:ad23fe03a082 | 206 | // Read the last byte | 
| mbed_official | 573:ad23fe03a082 | 207 | value = i2c_byte_read(obj, 1); | 
| mbed_official | 573:ad23fe03a082 | 208 | data[count] = (char)value; | 
| mbed_official | 573:ad23fe03a082 | 209 | |
| mbed_official | 573:ad23fe03a082 | 210 | return length; | 
| mbed_official | 573:ad23fe03a082 | 211 | } | 
| mbed_official | 573:ad23fe03a082 | 212 | |
| mbed_official | 573:ad23fe03a082 | 213 | int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) | 
| mbed_official | 573:ad23fe03a082 | 214 | { | 
| mbed_official | 573:ad23fe03a082 | 215 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); | 
| mbed_official | 573:ad23fe03a082 | 216 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); | 
| mbed_official | 573:ad23fe03a082 | 217 | int timeout; | 
| mbed_official | 573:ad23fe03a082 | 218 | int count; | 
| mbed_official | 573:ad23fe03a082 | 219 | |
| mbed_official | 573:ad23fe03a082 | 220 | i2c_start(obj); | 
| mbed_official | 573:ad23fe03a082 | 221 | |
| mbed_official | 573:ad23fe03a082 | 222 | // Wait until SB flag is set | 
| mbed_official | 573:ad23fe03a082 | 223 | timeout = FLAG_TIMEOUT; | 
| mbed_official | 573:ad23fe03a082 | 224 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_SB) == RESET) { | 
| mbed_official | 573:ad23fe03a082 | 225 | timeout--; | 
| mbed_official | 573:ad23fe03a082 | 226 | if (timeout == 0) { | 
| mbed_official | 573:ad23fe03a082 | 227 | return -1; | 
| mbed_official | 573:ad23fe03a082 | 228 | } | 
| mbed_official | 573:ad23fe03a082 | 229 | } | 
| mbed_official | 573:ad23fe03a082 | 230 | |
| mbed_official | 573:ad23fe03a082 | 231 | i2c->DR = __HAL_I2C_7BIT_ADD_WRITE(address); | 
| mbed_official | 573:ad23fe03a082 | 232 | |
| mbed_official | 573:ad23fe03a082 | 233 | |
| mbed_official | 573:ad23fe03a082 | 234 | // Wait address is acknowledged | 
| mbed_official | 573:ad23fe03a082 | 235 | timeout = FLAG_TIMEOUT; | 
| mbed_official | 573:ad23fe03a082 | 236 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == RESET) { | 
| mbed_official | 573:ad23fe03a082 | 237 | timeout--; | 
| mbed_official | 573:ad23fe03a082 | 238 | if (timeout == 0) { | 
| mbed_official | 573:ad23fe03a082 | 239 | return -1; | 
| mbed_official | 573:ad23fe03a082 | 240 | } | 
| mbed_official | 573:ad23fe03a082 | 241 | } | 
| mbed_official | 573:ad23fe03a082 | 242 | __HAL_I2C_CLEAR_ADDRFLAG(&I2cHandle); | 
| mbed_official | 573:ad23fe03a082 | 243 | |
| mbed_official | 573:ad23fe03a082 | 244 | for (count = 0; count < length; count++) { | 
| mbed_official | 573:ad23fe03a082 | 245 | if (i2c_byte_write(obj, data[count]) != 1) { | 
| mbed_official | 573:ad23fe03a082 | 246 | i2c_stop(obj); | 
| mbed_official | 573:ad23fe03a082 | 247 | return -1; | 
| mbed_official | 573:ad23fe03a082 | 248 | } | 
| mbed_official | 573:ad23fe03a082 | 249 | } | 
| mbed_official | 573:ad23fe03a082 | 250 | |
| mbed_official | 573:ad23fe03a082 | 251 | // If not repeated start, send stop. | 
| mbed_official | 573:ad23fe03a082 | 252 | if (stop) { | 
| mbed_official | 573:ad23fe03a082 | 253 | i2c_stop(obj); | 
| mbed_official | 573:ad23fe03a082 | 254 | } | 
| mbed_official | 573:ad23fe03a082 | 255 | |
| mbed_official | 573:ad23fe03a082 | 256 | return count; | 
| mbed_official | 573:ad23fe03a082 | 257 | } | 
| mbed_official | 573:ad23fe03a082 | 258 | |
| mbed_official | 573:ad23fe03a082 | 259 | int i2c_byte_read(i2c_t *obj, int last) | 
| mbed_official | 573:ad23fe03a082 | 260 | { | 
| mbed_official | 573:ad23fe03a082 | 261 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); | 
| mbed_official | 573:ad23fe03a082 | 262 | int timeout; | 
| mbed_official | 573:ad23fe03a082 | 263 | |
| mbed_official | 573:ad23fe03a082 | 264 | if (last) { | 
| mbed_official | 573:ad23fe03a082 | 265 | // Don't acknowledge the last byte | 
| mbed_official | 573:ad23fe03a082 | 266 | i2c->CR1 &= ~I2C_CR1_ACK; | 
| mbed_official | 573:ad23fe03a082 | 267 | } else { | 
| mbed_official | 573:ad23fe03a082 | 268 | // Acknowledge the byte | 
| mbed_official | 573:ad23fe03a082 | 269 | i2c->CR1 |= I2C_CR1_ACK; | 
| mbed_official | 573:ad23fe03a082 | 270 | } | 
| mbed_official | 573:ad23fe03a082 | 271 | |
| mbed_official | 573:ad23fe03a082 | 272 | // Wait until the byte is received | 
| mbed_official | 573:ad23fe03a082 | 273 | timeout = FLAG_TIMEOUT; | 
| mbed_official | 573:ad23fe03a082 | 274 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) { | 
| mbed_official | 573:ad23fe03a082 | 275 | if ((timeout--) == 0) { | 
| mbed_official | 573:ad23fe03a082 | 276 | return -1; | 
| mbed_official | 573:ad23fe03a082 | 277 | } | 
| mbed_official | 573:ad23fe03a082 | 278 | } | 
| mbed_official | 573:ad23fe03a082 | 279 | |
| mbed_official | 573:ad23fe03a082 | 280 | return (int)i2c->DR; | 
| mbed_official | 573:ad23fe03a082 | 281 | } | 
| mbed_official | 573:ad23fe03a082 | 282 | |
| mbed_official | 573:ad23fe03a082 | 283 | int i2c_byte_write(i2c_t *obj, int data) | 
| mbed_official | 573:ad23fe03a082 | 284 | { | 
| mbed_official | 573:ad23fe03a082 | 285 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); | 
| mbed_official | 573:ad23fe03a082 | 286 | int timeout; | 
| mbed_official | 573:ad23fe03a082 | 287 | |
| mbed_official | 573:ad23fe03a082 | 288 | i2c->DR = (uint8_t)data; | 
| mbed_official | 573:ad23fe03a082 | 289 | |
| mbed_official | 573:ad23fe03a082 | 290 | // Wait until the byte is transmitted | 
| mbed_official | 573:ad23fe03a082 | 291 | timeout = FLAG_TIMEOUT; | 
| mbed_official | 573:ad23fe03a082 | 292 | while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXE) == RESET) && | 
| mbed_official | 573:ad23fe03a082 | 293 | (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == RESET)) { | 
| mbed_official | 573:ad23fe03a082 | 294 | if ((timeout--) == 0) { | 
| mbed_official | 573:ad23fe03a082 | 295 | return 0; | 
| mbed_official | 573:ad23fe03a082 | 296 | } | 
| mbed_official | 573:ad23fe03a082 | 297 | } | 
| mbed_official | 573:ad23fe03a082 | 298 | |
| mbed_official | 573:ad23fe03a082 | 299 | return 1; | 
| mbed_official | 573:ad23fe03a082 | 300 | } | 
| mbed_official | 573:ad23fe03a082 | 301 | |
| mbed_official | 573:ad23fe03a082 | 302 | void i2c_reset(i2c_t *obj) | 
| mbed_official | 573:ad23fe03a082 | 303 | { | 
| mbed_official | 573:ad23fe03a082 | 304 | int timeout; | 
| mbed_official | 573:ad23fe03a082 | 305 | |
| mbed_official | 573:ad23fe03a082 | 306 | // wait before reset | 
| mbed_official | 573:ad23fe03a082 | 307 | timeout = LONG_TIMEOUT; | 
| mbed_official | 573:ad23fe03a082 | 308 | while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0)); | 
| mbed_official | 573:ad23fe03a082 | 309 | |
| mbed_official | 573:ad23fe03a082 | 310 | if (obj->i2c == I2C_1) { | 
| mbed_official | 573:ad23fe03a082 | 311 | __I2C1_FORCE_RESET(); | 
| mbed_official | 573:ad23fe03a082 | 312 | __I2C1_RELEASE_RESET(); | 
| mbed_official | 573:ad23fe03a082 | 313 | } | 
| mbed_official | 573:ad23fe03a082 | 314 | if (obj->i2c == I2C_2) { | 
| mbed_official | 573:ad23fe03a082 | 315 | __I2C2_FORCE_RESET(); | 
| mbed_official | 573:ad23fe03a082 | 316 | __I2C2_RELEASE_RESET(); | 
| mbed_official | 573:ad23fe03a082 | 317 | } | 
| mbed_official | 573:ad23fe03a082 | 318 | if (obj->i2c == I2C_3) { | 
| mbed_official | 573:ad23fe03a082 | 319 | __I2C3_FORCE_RESET(); | 
| mbed_official | 573:ad23fe03a082 | 320 | __I2C3_RELEASE_RESET(); | 
| mbed_official | 573:ad23fe03a082 | 321 | } | 
| mbed_official | 573:ad23fe03a082 | 322 | } | 
| mbed_official | 573:ad23fe03a082 | 323 | |
| mbed_official | 573:ad23fe03a082 | 324 | #if DEVICE_I2CSLAVE | 
| mbed_official | 573:ad23fe03a082 | 325 | |
| mbed_official | 573:ad23fe03a082 | 326 | void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) | 
| mbed_official | 573:ad23fe03a082 | 327 | { | 
| mbed_official | 573:ad23fe03a082 | 328 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); | 
| mbed_official | 573:ad23fe03a082 | 329 | uint16_t tmpreg = 0; | 
| mbed_official | 573:ad23fe03a082 | 330 | |
| mbed_official | 573:ad23fe03a082 | 331 | // Get the old register value | 
| mbed_official | 573:ad23fe03a082 | 332 | tmpreg = i2c->OAR1; | 
| mbed_official | 573:ad23fe03a082 | 333 | // Reset address bits | 
| mbed_official | 573:ad23fe03a082 | 334 | tmpreg &= 0xFC00; | 
| mbed_official | 573:ad23fe03a082 | 335 | // Set new address | 
| mbed_official | 573:ad23fe03a082 | 336 | tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits | 
| mbed_official | 573:ad23fe03a082 | 337 | // Store the new register value | 
| mbed_official | 573:ad23fe03a082 | 338 | i2c->OAR1 = tmpreg; | 
| mbed_official | 573:ad23fe03a082 | 339 | } | 
| mbed_official | 573:ad23fe03a082 | 340 | |
| mbed_official | 573:ad23fe03a082 | 341 | void i2c_slave_mode(i2c_t *obj, int enable_slave) | 
| mbed_official | 573:ad23fe03a082 | 342 | { | 
| mbed_official | 573:ad23fe03a082 | 343 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); | 
| mbed_official | 573:ad23fe03a082 | 344 | if (enable_slave) { | 
| mbed_official | 573:ad23fe03a082 | 345 | obj->slave = 1; | 
| mbed_official | 573:ad23fe03a082 | 346 | /* Enable Address Acknowledge */ | 
| mbed_official | 573:ad23fe03a082 | 347 | I2cHandle.Instance->CR1 |= I2C_CR1_ACK; | 
| mbed_official | 573:ad23fe03a082 | 348 | } | 
| mbed_official | 573:ad23fe03a082 | 349 | } | 
| mbed_official | 573:ad23fe03a082 | 350 | |
| mbed_official | 573:ad23fe03a082 | 351 | // See I2CSlave.h | 
| mbed_official | 573:ad23fe03a082 | 352 | #define NoData 0 // the slave has not been addressed | 
| mbed_official | 573:ad23fe03a082 | 353 | #define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter) | 
| mbed_official | 573:ad23fe03a082 | 354 | #define WriteGeneral 2 // the master is writing to all slave | 
| mbed_official | 573:ad23fe03a082 | 355 | #define WriteAddressed 3 // the master is writing to this slave (slave = receiver) | 
| mbed_official | 573:ad23fe03a082 | 356 | |
| mbed_official | 573:ad23fe03a082 | 357 | int i2c_slave_receive(i2c_t *obj) | 
| mbed_official | 573:ad23fe03a082 | 358 | { | 
| mbed_official | 573:ad23fe03a082 | 359 | int retValue = NoData; | 
| mbed_official | 573:ad23fe03a082 | 360 | |
| mbed_official | 573:ad23fe03a082 | 361 | if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == 1) { | 
| mbed_official | 573:ad23fe03a082 | 362 | if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == 1) { | 
| mbed_official | 573:ad23fe03a082 | 363 | if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TRA) == 1) | 
| mbed_official | 573:ad23fe03a082 | 364 | retValue = ReadAddressed; | 
| mbed_official | 573:ad23fe03a082 | 365 | else | 
| mbed_official | 573:ad23fe03a082 | 366 | retValue = WriteAddressed; | 
| mbed_official | 573:ad23fe03a082 | 367 | |
| mbed_official | 573:ad23fe03a082 | 368 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_ADDR); | 
| mbed_official | 573:ad23fe03a082 | 369 | } | 
| mbed_official | 573:ad23fe03a082 | 370 | } | 
| mbed_official | 573:ad23fe03a082 | 371 | |
| mbed_official | 573:ad23fe03a082 | 372 | return (retValue); | 
| mbed_official | 573:ad23fe03a082 | 373 | } | 
| mbed_official | 573:ad23fe03a082 | 374 | |
| mbed_official | 573:ad23fe03a082 | 375 | int i2c_slave_read(i2c_t *obj, char *data, int length) | 
| mbed_official | 573:ad23fe03a082 | 376 | { | 
| mbed_official | 573:ad23fe03a082 | 377 | uint32_t Timeout; | 
| mbed_official | 573:ad23fe03a082 | 378 | int size = 0; | 
| mbed_official | 573:ad23fe03a082 | 379 | |
| mbed_official | 573:ad23fe03a082 | 380 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); | 
| mbed_official | 573:ad23fe03a082 | 381 | |
| mbed_official | 573:ad23fe03a082 | 382 | while (length > 0) { | 
| mbed_official | 573:ad23fe03a082 | 383 | /* Wait until RXNE flag is set */ | 
| mbed_official | 573:ad23fe03a082 | 384 | // Wait until the byte is received | 
| mbed_official | 573:ad23fe03a082 | 385 | Timeout = FLAG_TIMEOUT; | 
| mbed_official | 573:ad23fe03a082 | 386 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) { | 
| mbed_official | 573:ad23fe03a082 | 387 | Timeout--; | 
| mbed_official | 573:ad23fe03a082 | 388 | if (Timeout == 0) { | 
| mbed_official | 573:ad23fe03a082 | 389 | return -1; | 
| mbed_official | 573:ad23fe03a082 | 390 | } | 
| mbed_official | 573:ad23fe03a082 | 391 | } | 
| mbed_official | 573:ad23fe03a082 | 392 | |
| mbed_official | 573:ad23fe03a082 | 393 | /* Read data from DR */ | 
| mbed_official | 573:ad23fe03a082 | 394 | (*data++) = I2cHandle.Instance->DR; | 
| mbed_official | 573:ad23fe03a082 | 395 | length--; | 
| mbed_official | 573:ad23fe03a082 | 396 | size++; | 
| mbed_official | 573:ad23fe03a082 | 397 | |
| mbed_official | 573:ad23fe03a082 | 398 | if ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == SET) && (length != 0)) { | 
| mbed_official | 573:ad23fe03a082 | 399 | /* Read data from DR */ | 
| mbed_official | 573:ad23fe03a082 | 400 | (*data++) = I2cHandle.Instance->DR; | 
| mbed_official | 573:ad23fe03a082 | 401 | length--; | 
| mbed_official | 573:ad23fe03a082 | 402 | size++; | 
| mbed_official | 573:ad23fe03a082 | 403 | } | 
| mbed_official | 573:ad23fe03a082 | 404 | } | 
| mbed_official | 573:ad23fe03a082 | 405 | |
| mbed_official | 573:ad23fe03a082 | 406 | /* Wait until STOP flag is set */ | 
| mbed_official | 573:ad23fe03a082 | 407 | Timeout = FLAG_TIMEOUT; | 
| mbed_official | 573:ad23fe03a082 | 408 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) { | 
| mbed_official | 573:ad23fe03a082 | 409 | Timeout--; | 
| mbed_official | 573:ad23fe03a082 | 410 | if (Timeout == 0) { | 
| mbed_official | 573:ad23fe03a082 | 411 | return -1; | 
| mbed_official | 573:ad23fe03a082 | 412 | } | 
| mbed_official | 573:ad23fe03a082 | 413 | } | 
| mbed_official | 573:ad23fe03a082 | 414 | |
| mbed_official | 573:ad23fe03a082 | 415 | /* Clear STOP flag */ | 
| mbed_official | 573:ad23fe03a082 | 416 | __HAL_I2C_CLEAR_STOPFLAG(&I2cHandle); | 
| mbed_official | 573:ad23fe03a082 | 417 | |
| mbed_official | 573:ad23fe03a082 | 418 | /* Wait until BUSY flag is reset */ | 
| mbed_official | 573:ad23fe03a082 | 419 | Timeout = FLAG_TIMEOUT; | 
| mbed_official | 573:ad23fe03a082 | 420 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == SET) { | 
| mbed_official | 573:ad23fe03a082 | 421 | Timeout--; | 
| mbed_official | 573:ad23fe03a082 | 422 | if (Timeout == 0) { | 
| mbed_official | 573:ad23fe03a082 | 423 | return -1; | 
| mbed_official | 573:ad23fe03a082 | 424 | } | 
| mbed_official | 573:ad23fe03a082 | 425 | } | 
| mbed_official | 573:ad23fe03a082 | 426 | |
| mbed_official | 573:ad23fe03a082 | 427 | return size; | 
| mbed_official | 573:ad23fe03a082 | 428 | } | 
| mbed_official | 573:ad23fe03a082 | 429 | |
| mbed_official | 573:ad23fe03a082 | 430 | int i2c_slave_write(i2c_t *obj, const char *data, int length) | 
| mbed_official | 573:ad23fe03a082 | 431 | { | 
| mbed_official | 573:ad23fe03a082 | 432 | uint32_t Timeout; | 
| mbed_official | 573:ad23fe03a082 | 433 | int size = 0; | 
| mbed_official | 573:ad23fe03a082 | 434 | |
| mbed_official | 573:ad23fe03a082 | 435 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); | 
| mbed_official | 573:ad23fe03a082 | 436 | |
| mbed_official | 573:ad23fe03a082 | 437 | while (length > 0) { | 
| mbed_official | 573:ad23fe03a082 | 438 | /* Wait until TXE flag is set */ | 
| mbed_official | 573:ad23fe03a082 | 439 | Timeout = FLAG_TIMEOUT; | 
| mbed_official | 573:ad23fe03a082 | 440 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXE) == RESET) { | 
| mbed_official | 573:ad23fe03a082 | 441 | Timeout--; | 
| mbed_official | 573:ad23fe03a082 | 442 | if (Timeout == 0) { | 
| mbed_official | 573:ad23fe03a082 | 443 | return -1; | 
| mbed_official | 573:ad23fe03a082 | 444 | } | 
| mbed_official | 573:ad23fe03a082 | 445 | } | 
| mbed_official | 573:ad23fe03a082 | 446 | |
| mbed_official | 573:ad23fe03a082 | 447 | |
| mbed_official | 573:ad23fe03a082 | 448 | /* Write data to DR */ | 
| mbed_official | 573:ad23fe03a082 | 449 | I2cHandle.Instance->DR = (*data++); | 
| mbed_official | 573:ad23fe03a082 | 450 | length--; | 
| mbed_official | 573:ad23fe03a082 | 451 | size++; | 
| mbed_official | 573:ad23fe03a082 | 452 | |
| mbed_official | 573:ad23fe03a082 | 453 | if ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == SET) && (length != 0)) { | 
| mbed_official | 573:ad23fe03a082 | 454 | /* Write data to DR */ | 
| mbed_official | 573:ad23fe03a082 | 455 | I2cHandle.Instance->DR = (*data++); | 
| mbed_official | 573:ad23fe03a082 | 456 | length--; | 
| mbed_official | 573:ad23fe03a082 | 457 | size++; | 
| mbed_official | 573:ad23fe03a082 | 458 | } | 
| mbed_official | 573:ad23fe03a082 | 459 | } | 
| mbed_official | 573:ad23fe03a082 | 460 | |
| mbed_official | 573:ad23fe03a082 | 461 | /* Wait until AF flag is set */ | 
| mbed_official | 573:ad23fe03a082 | 462 | Timeout = FLAG_TIMEOUT; | 
| mbed_official | 573:ad23fe03a082 | 463 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_AF) == RESET) { | 
| mbed_official | 573:ad23fe03a082 | 464 | Timeout--; | 
| mbed_official | 573:ad23fe03a082 | 465 | if (Timeout == 0) { | 
| mbed_official | 573:ad23fe03a082 | 466 | return -1; | 
| mbed_official | 573:ad23fe03a082 | 467 | } | 
| mbed_official | 573:ad23fe03a082 | 468 | } | 
| mbed_official | 573:ad23fe03a082 | 469 | |
| mbed_official | 573:ad23fe03a082 | 470 | |
| mbed_official | 573:ad23fe03a082 | 471 | /* Clear AF flag */ | 
| mbed_official | 573:ad23fe03a082 | 472 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF); | 
| mbed_official | 573:ad23fe03a082 | 473 | |
| mbed_official | 573:ad23fe03a082 | 474 | |
| mbed_official | 573:ad23fe03a082 | 475 | /* Wait until BUSY flag is reset */ | 
| mbed_official | 573:ad23fe03a082 | 476 | Timeout = FLAG_TIMEOUT; | 
| mbed_official | 573:ad23fe03a082 | 477 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == SET) { | 
| mbed_official | 573:ad23fe03a082 | 478 | Timeout--; | 
| mbed_official | 573:ad23fe03a082 | 479 | if (Timeout == 0) { | 
| mbed_official | 573:ad23fe03a082 | 480 | return -1; | 
| mbed_official | 573:ad23fe03a082 | 481 | } | 
| mbed_official | 573:ad23fe03a082 | 482 | } | 
| mbed_official | 573:ad23fe03a082 | 483 | |
| mbed_official | 573:ad23fe03a082 | 484 | I2cHandle.State = HAL_I2C_STATE_READY; | 
| mbed_official | 573:ad23fe03a082 | 485 | |
| mbed_official | 573:ad23fe03a082 | 486 | /* Process Unlocked */ | 
| mbed_official | 573:ad23fe03a082 | 487 | __HAL_UNLOCK(&I2cHandle); | 
| mbed_official | 573:ad23fe03a082 | 488 | |
| mbed_official | 573:ad23fe03a082 | 489 | return size; | 
| mbed_official | 573:ad23fe03a082 | 490 | } | 
| mbed_official | 573:ad23fe03a082 | 491 | |
| mbed_official | 573:ad23fe03a082 | 492 | |
| mbed_official | 573:ad23fe03a082 | 493 | #endif // DEVICE_I2CSLAVE | 
| mbed_official | 573:ad23fe03a082 | 494 | |
| mbed_official | 573:ad23fe03a082 | 495 | #endif // DEVICE_I2C | 
