mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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