mbed library sources

Dependents:   Nucleo_blink_led

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue Mar 24 09:00:08 2015 +0000
Revision:
496:543871686697
Parent:
targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/i2c_api.c@414:4ec4c5b614b0
Synchronized with git revision ea01d61fa18430564b78226045b196bb6bf6b66a

Full URL: https://github.com/mbedmicro/mbed/commit/ea01d61fa18430564b78226045b196bb6bf6b66a/

NUCLEO_L152RE - reorg hal

Who changed what in which revision?

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