mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Mon Sep 28 10:45:10 2015 +0100
Revision:
630:825f75ca301e
Parent:
469:fc4922e0c183
Synchronized with git revision 54fbe4144faf309c37205a5d39fa665daa919f10

Full URL: https://github.com/mbedmicro/mbed/commit/54fbe4144faf309c37205a5d39fa665daa919f10/

NUCLEO_F031K6 : Add new target

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 340:28d1f895c6fe 1 /* mbed Microcontroller Library
mbed_official 340:28d1f895c6fe 2 *******************************************************************************
mbed_official 340:28d1f895c6fe 3 * Copyright (c) 2014, STMicroelectronics
mbed_official 340:28d1f895c6fe 4 * All rights reserved.
mbed_official 340:28d1f895c6fe 5 *
mbed_official 340:28d1f895c6fe 6 * Redistribution and use in source and binary forms, with or without
mbed_official 340:28d1f895c6fe 7 * modification, are permitted provided that the following conditions are met:
mbed_official 340:28d1f895c6fe 8 *
mbed_official 340:28d1f895c6fe 9 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 340:28d1f895c6fe 10 * this list of conditions and the following disclaimer.
mbed_official 340:28d1f895c6fe 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 340:28d1f895c6fe 12 * this list of conditions and the following disclaimer in the documentation
mbed_official 340:28d1f895c6fe 13 * and/or other materials provided with the distribution.
mbed_official 340:28d1f895c6fe 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 340:28d1f895c6fe 15 * may be used to endorse or promote products derived from this software
mbed_official 340:28d1f895c6fe 16 * without specific prior written permission.
mbed_official 340:28d1f895c6fe 17 *
mbed_official 340:28d1f895c6fe 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 340:28d1f895c6fe 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 340:28d1f895c6fe 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 340:28d1f895c6fe 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 340:28d1f895c6fe 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 340:28d1f895c6fe 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 340:28d1f895c6fe 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 340:28d1f895c6fe 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 340:28d1f895c6fe 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 340:28d1f895c6fe 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 340:28d1f895c6fe 28 *******************************************************************************
mbed_official 340:28d1f895c6fe 29 */
mbed_official 340:28d1f895c6fe 30 #include "mbed_assert.h"
mbed_official 340:28d1f895c6fe 31 #include "i2c_api.h"
mbed_official 340:28d1f895c6fe 32
mbed_official 340:28d1f895c6fe 33 #if DEVICE_I2C
mbed_official 340:28d1f895c6fe 34
mbed_official 340:28d1f895c6fe 35 #include "cmsis.h"
mbed_official 340:28d1f895c6fe 36 #include "pinmap.h"
mbed_official 414:4ec4c5b614b0 37 #include "PeripheralPins.h"
mbed_official 340:28d1f895c6fe 38
mbed_official 340:28d1f895c6fe 39 /* Timeout values for flags and events waiting loops. These timeouts are
mbed_official 340:28d1f895c6fe 40 not based on accurate values, they just guarantee that the application will
mbed_official 340:28d1f895c6fe 41 not remain stuck if the I2C communication is corrupted. */
mbed_official 340:28d1f895c6fe 42 #define FLAG_TIMEOUT ((int)0x1000)
mbed_official 340:28d1f895c6fe 43 #define LONG_TIMEOUT ((int)0x8000)
mbed_official 340:28d1f895c6fe 44
mbed_official 340:28d1f895c6fe 45 I2C_HandleTypeDef I2cHandle;
mbed_official 340:28d1f895c6fe 46
mbed_official 340:28d1f895c6fe 47 int i2c1_inited = 0;
mbed_official 630:825f75ca301e 48 #if defined(I2C2_BASE)
mbed_official 340:28d1f895c6fe 49 int i2c2_inited = 0;
mbed_official 630:825f75ca301e 50 #endif
mbed_official 340:28d1f895c6fe 51
mbed_official 630:825f75ca301e 52 void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
mbed_official 340:28d1f895c6fe 53 // Determine the I2C to use
mbed_official 340:28d1f895c6fe 54 I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
mbed_official 340:28d1f895c6fe 55 I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
mbed_official 340:28d1f895c6fe 56
mbed_official 340:28d1f895c6fe 57 obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
mbed_official 340:28d1f895c6fe 58 MBED_ASSERT(obj->i2c != (I2CName)NC);
mbed_official 340:28d1f895c6fe 59
mbed_official 340:28d1f895c6fe 60 // Enable I2C1 clock and pinout if not done
mbed_official 340:28d1f895c6fe 61 if ((obj->i2c == I2C_1) && !i2c1_inited) {
mbed_official 340:28d1f895c6fe 62 i2c1_inited = 1;
mbed_official 340:28d1f895c6fe 63 __HAL_RCC_I2C1_CONFIG(RCC_I2C1CLKSOURCE_SYSCLK);
mbed_official 340:28d1f895c6fe 64 __I2C1_CLK_ENABLE();
mbed_official 340:28d1f895c6fe 65 // Configure I2C pins
mbed_official 340:28d1f895c6fe 66 pinmap_pinout(sda, PinMap_I2C_SDA);
mbed_official 340:28d1f895c6fe 67 pinmap_pinout(scl, PinMap_I2C_SCL);
mbed_official 340:28d1f895c6fe 68 pin_mode(sda, OpenDrain);
mbed_official 340:28d1f895c6fe 69 pin_mode(scl, OpenDrain);
mbed_official 340:28d1f895c6fe 70 }
mbed_official 340:28d1f895c6fe 71
mbed_official 630:825f75ca301e 72 #if defined(I2C2_BASE)
mbed_official 340:28d1f895c6fe 73 // Enable I2C2 clock and pinout if not done
mbed_official 340:28d1f895c6fe 74 if ((obj->i2c == I2C_2) && !i2c2_inited) {
mbed_official 340:28d1f895c6fe 75 i2c2_inited = 1;
mbed_official 340:28d1f895c6fe 76 __I2C2_CLK_ENABLE();
mbed_official 340:28d1f895c6fe 77 // Configure I2C pins
mbed_official 340:28d1f895c6fe 78 pinmap_pinout(sda, PinMap_I2C_SDA);
mbed_official 340:28d1f895c6fe 79 pinmap_pinout(scl, PinMap_I2C_SCL);
mbed_official 340:28d1f895c6fe 80 pin_mode(sda, OpenDrain);
mbed_official 340:28d1f895c6fe 81 pin_mode(scl, OpenDrain);
mbed_official 340:28d1f895c6fe 82 }
mbed_official 630:825f75ca301e 83 #endif
mbed_official 340:28d1f895c6fe 84
mbed_official 340:28d1f895c6fe 85 // Reset to clear pending flags if any
mbed_official 340:28d1f895c6fe 86 i2c_reset(obj);
mbed_official 340:28d1f895c6fe 87
mbed_official 340:28d1f895c6fe 88 // I2C configuration
mbed_official 340:28d1f895c6fe 89 i2c_frequency(obj, 100000); // 100 kHz per default
mbed_official 340:28d1f895c6fe 90 }
mbed_official 340:28d1f895c6fe 91
mbed_official 630:825f75ca301e 92 void i2c_frequency(i2c_t *obj, int hz) {
mbed_official 340:28d1f895c6fe 93 MBED_ASSERT((hz == 100000) || (hz == 400000) || (hz == 1000000));
mbed_official 340:28d1f895c6fe 94 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
mbed_official 340:28d1f895c6fe 95 int timeout;
mbed_official 340:28d1f895c6fe 96
mbed_official 340:28d1f895c6fe 97 // wait before init
mbed_official 340:28d1f895c6fe 98 timeout = LONG_TIMEOUT;
mbed_official 340:28d1f895c6fe 99 while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));
mbed_official 340:28d1f895c6fe 100
mbed_official 340:28d1f895c6fe 101 // Common settings: I2C clock = 48 MHz, Analog filter = ON, Digital filter coefficient = 0
mbed_official 340:28d1f895c6fe 102 switch (hz) {
mbed_official 340:28d1f895c6fe 103 case 100000:
mbed_official 340:28d1f895c6fe 104 I2cHandle.Init.Timing = 0x10805E89; // Standard mode with Rise Time = 400ns and Fall Time = 100ns
mbed_official 340:28d1f895c6fe 105 break;
mbed_official 340:28d1f895c6fe 106 case 400000:
mbed_official 340:28d1f895c6fe 107 I2cHandle.Init.Timing = 0x00901850; // Fast mode with Rise Time = 250ns and Fall Time = 100ns
mbed_official 340:28d1f895c6fe 108 break;
mbed_official 340:28d1f895c6fe 109 case 1000000:
mbed_official 340:28d1f895c6fe 110 I2cHandle.Init.Timing = 0x00700818; // Fast mode Plus with Rise Time = 60ns and Fall Time = 100ns
mbed_official 340:28d1f895c6fe 111 break;
mbed_official 340:28d1f895c6fe 112 default:
mbed_official 340:28d1f895c6fe 113 break;
mbed_official 340:28d1f895c6fe 114 }
mbed_official 340:28d1f895c6fe 115
mbed_official 340:28d1f895c6fe 116 // I2C configuration
mbed_official 340:28d1f895c6fe 117 I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
mbed_official 340:28d1f895c6fe 118 I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
mbed_official 340:28d1f895c6fe 119 I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
mbed_official 340:28d1f895c6fe 120 I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
mbed_official 340:28d1f895c6fe 121 I2cHandle.Init.OwnAddress1 = 0;
mbed_official 340:28d1f895c6fe 122 I2cHandle.Init.OwnAddress2 = 0;
mbed_official 340:28d1f895c6fe 123 I2cHandle.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
mbed_official 340:28d1f895c6fe 124 HAL_I2C_Init(&I2cHandle);
mbed_official 340:28d1f895c6fe 125 }
mbed_official 340:28d1f895c6fe 126
mbed_official 630:825f75ca301e 127 inline int i2c_start(i2c_t *obj) {
mbed_official 340:28d1f895c6fe 128 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 340:28d1f895c6fe 129 int timeout;
mbed_official 340:28d1f895c6fe 130
mbed_official 340:28d1f895c6fe 131 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
mbed_official 340:28d1f895c6fe 132
mbed_official 340:28d1f895c6fe 133 // Clear Acknowledge failure flag
mbed_official 340:28d1f895c6fe 134 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF);
mbed_official 340:28d1f895c6fe 135
mbed_official 340:28d1f895c6fe 136 // Generate the START condition
mbed_official 340:28d1f895c6fe 137 i2c->CR2 |= I2C_CR2_START;
mbed_official 340:28d1f895c6fe 138
mbed_official 340:28d1f895c6fe 139 // Wait the START condition has been correctly sent
mbed_official 340:28d1f895c6fe 140 timeout = FLAG_TIMEOUT;
mbed_official 340:28d1f895c6fe 141 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == RESET) {
mbed_official 340:28d1f895c6fe 142 if ((timeout--) == 0) {
mbed_official 340:28d1f895c6fe 143 return 1;
mbed_official 340:28d1f895c6fe 144 }
mbed_official 340:28d1f895c6fe 145 }
mbed_official 340:28d1f895c6fe 146
mbed_official 340:28d1f895c6fe 147 return 0;
mbed_official 340:28d1f895c6fe 148 }
mbed_official 340:28d1f895c6fe 149
mbed_official 630:825f75ca301e 150 inline int i2c_stop(i2c_t *obj) {
mbed_official 340:28d1f895c6fe 151 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 340:28d1f895c6fe 152
mbed_official 340:28d1f895c6fe 153 // Generate the STOP condition
mbed_official 340:28d1f895c6fe 154 i2c->CR2 |= I2C_CR2_STOP;
mbed_official 340:28d1f895c6fe 155
mbed_official 340:28d1f895c6fe 156 return 0;
mbed_official 340:28d1f895c6fe 157 }
mbed_official 340:28d1f895c6fe 158
mbed_official 630:825f75ca301e 159 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
mbed_official 340:28d1f895c6fe 160 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 340:28d1f895c6fe 161 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
mbed_official 340:28d1f895c6fe 162 int timeout;
mbed_official 340:28d1f895c6fe 163 int count;
mbed_official 340:28d1f895c6fe 164 int value;
mbed_official 340:28d1f895c6fe 165
mbed_official 340:28d1f895c6fe 166 // Update CR2 register
mbed_official 340:28d1f895c6fe 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 340:28d1f895c6fe 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 340:28d1f895c6fe 169
mbed_official 340:28d1f895c6fe 170 // Read all bytes
mbed_official 340:28d1f895c6fe 171 for (count = 0; count < length; count++) {
mbed_official 340:28d1f895c6fe 172 value = i2c_byte_read(obj, 0);
mbed_official 340:28d1f895c6fe 173 data[count] = (char)value;
mbed_official 340:28d1f895c6fe 174 }
mbed_official 340:28d1f895c6fe 175
mbed_official 340:28d1f895c6fe 176 // Wait transfer complete
mbed_official 340:28d1f895c6fe 177 timeout = FLAG_TIMEOUT;
mbed_official 340:28d1f895c6fe 178 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) {
mbed_official 340:28d1f895c6fe 179 timeout--;
mbed_official 340:28d1f895c6fe 180 if (timeout == 0) {
mbed_official 340:28d1f895c6fe 181 return -1;
mbed_official 340:28d1f895c6fe 182 }
mbed_official 340:28d1f895c6fe 183 }
mbed_official 340:28d1f895c6fe 184 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC);
mbed_official 340:28d1f895c6fe 185
mbed_official 340:28d1f895c6fe 186 // If not repeated start, send stop.
mbed_official 340:28d1f895c6fe 187 if (stop) {
mbed_official 340:28d1f895c6fe 188 i2c_stop(obj);
mbed_official 340:28d1f895c6fe 189 // Wait until STOPF flag is set
mbed_official 340:28d1f895c6fe 190 timeout = FLAG_TIMEOUT;
mbed_official 340:28d1f895c6fe 191 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
mbed_official 340:28d1f895c6fe 192 timeout--;
mbed_official 340:28d1f895c6fe 193 if (timeout == 0) {
mbed_official 340:28d1f895c6fe 194 return -1;
mbed_official 340:28d1f895c6fe 195 }
mbed_official 340:28d1f895c6fe 196 }
mbed_official 340:28d1f895c6fe 197 // Clear STOP Flag
mbed_official 340:28d1f895c6fe 198 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF);
mbed_official 340:28d1f895c6fe 199 }
mbed_official 340:28d1f895c6fe 200
mbed_official 340:28d1f895c6fe 201 return length;
mbed_official 340:28d1f895c6fe 202 }
mbed_official 340:28d1f895c6fe 203
mbed_official 630:825f75ca301e 204 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
mbed_official 340:28d1f895c6fe 205 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 340:28d1f895c6fe 206 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
mbed_official 340:28d1f895c6fe 207 int timeout;
mbed_official 340:28d1f895c6fe 208 int count;
mbed_official 340:28d1f895c6fe 209
mbed_official 340:28d1f895c6fe 210 // Update CR2 register
mbed_official 340:28d1f895c6fe 211 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 340:28d1f895c6fe 212 | (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 340:28d1f895c6fe 213
mbed_official 340:28d1f895c6fe 214 for (count = 0; count < length; count++) {
mbed_official 340:28d1f895c6fe 215 i2c_byte_write(obj, data[count]);
mbed_official 340:28d1f895c6fe 216 }
mbed_official 340:28d1f895c6fe 217
mbed_official 340:28d1f895c6fe 218 // Wait transfer complete
mbed_official 340:28d1f895c6fe 219 timeout = FLAG_TIMEOUT;
mbed_official 340:28d1f895c6fe 220 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) {
mbed_official 340:28d1f895c6fe 221 timeout--;
mbed_official 340:28d1f895c6fe 222 if (timeout == 0) {
mbed_official 340:28d1f895c6fe 223 return -1;
mbed_official 340:28d1f895c6fe 224 }
mbed_official 340:28d1f895c6fe 225 }
mbed_official 340:28d1f895c6fe 226 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC);
mbed_official 340:28d1f895c6fe 227
mbed_official 340:28d1f895c6fe 228 // If not repeated start, send stop
mbed_official 340:28d1f895c6fe 229 if (stop) {
mbed_official 340:28d1f895c6fe 230 i2c_stop(obj);
mbed_official 340:28d1f895c6fe 231 // Wait until STOPF flag is set
mbed_official 340:28d1f895c6fe 232 timeout = FLAG_TIMEOUT;
mbed_official 340:28d1f895c6fe 233 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
mbed_official 340:28d1f895c6fe 234 timeout--;
mbed_official 340:28d1f895c6fe 235 if (timeout == 0) {
mbed_official 340:28d1f895c6fe 236 return -1;
mbed_official 340:28d1f895c6fe 237 }
mbed_official 340:28d1f895c6fe 238 }
mbed_official 340:28d1f895c6fe 239 // Clear STOP Flag
mbed_official 340:28d1f895c6fe 240 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF);
mbed_official 340:28d1f895c6fe 241 }
mbed_official 340:28d1f895c6fe 242
mbed_official 340:28d1f895c6fe 243 return count;
mbed_official 340:28d1f895c6fe 244 }
mbed_official 340:28d1f895c6fe 245
mbed_official 630:825f75ca301e 246 int i2c_byte_read(i2c_t *obj, int last) {
mbed_official 340:28d1f895c6fe 247 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 340:28d1f895c6fe 248 int timeout;
mbed_official 340:28d1f895c6fe 249
mbed_official 340:28d1f895c6fe 250 // Wait until the byte is received
mbed_official 340:28d1f895c6fe 251 timeout = FLAG_TIMEOUT;
mbed_official 340:28d1f895c6fe 252 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) {
mbed_official 340:28d1f895c6fe 253 if ((timeout--) == 0) {
mbed_official 340:28d1f895c6fe 254 return -1;
mbed_official 340:28d1f895c6fe 255 }
mbed_official 340:28d1f895c6fe 256 }
mbed_official 340:28d1f895c6fe 257
mbed_official 340:28d1f895c6fe 258 return (int)i2c->RXDR;
mbed_official 340:28d1f895c6fe 259 }
mbed_official 340:28d1f895c6fe 260
mbed_official 630:825f75ca301e 261 int i2c_byte_write(i2c_t *obj, int data) {
mbed_official 340:28d1f895c6fe 262 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 340:28d1f895c6fe 263 int timeout;
mbed_official 340:28d1f895c6fe 264
mbed_official 340:28d1f895c6fe 265 // Wait until the previous byte is transmitted
mbed_official 340:28d1f895c6fe 266 timeout = FLAG_TIMEOUT;
mbed_official 340:28d1f895c6fe 267 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXIS) == RESET) {
mbed_official 340:28d1f895c6fe 268 if ((timeout--) == 0) {
mbed_official 340:28d1f895c6fe 269 return 0;
mbed_official 340:28d1f895c6fe 270 }
mbed_official 340:28d1f895c6fe 271 }
mbed_official 340:28d1f895c6fe 272
mbed_official 340:28d1f895c6fe 273 i2c->TXDR = (uint8_t)data;
mbed_official 340:28d1f895c6fe 274
mbed_official 340:28d1f895c6fe 275 return 1;
mbed_official 340:28d1f895c6fe 276 }
mbed_official 340:28d1f895c6fe 277
mbed_official 630:825f75ca301e 278 void i2c_reset(i2c_t *obj) {
mbed_official 340:28d1f895c6fe 279 int timeout;
mbed_official 340:28d1f895c6fe 280
mbed_official 340:28d1f895c6fe 281 // Wait before reset
mbed_official 340:28d1f895c6fe 282 timeout = LONG_TIMEOUT;
mbed_official 340:28d1f895c6fe 283 while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));
mbed_official 340:28d1f895c6fe 284
mbed_official 340:28d1f895c6fe 285 if (obj->i2c == I2C_1) {
mbed_official 340:28d1f895c6fe 286 __I2C1_FORCE_RESET();
mbed_official 340:28d1f895c6fe 287 __I2C1_RELEASE_RESET();
mbed_official 340:28d1f895c6fe 288 }
mbed_official 630:825f75ca301e 289 #if defined(I2C2_BASE)
mbed_official 340:28d1f895c6fe 290 if (obj->i2c == I2C_2) {
mbed_official 340:28d1f895c6fe 291 __I2C2_FORCE_RESET();
mbed_official 340:28d1f895c6fe 292 __I2C2_RELEASE_RESET();
mbed_official 340:28d1f895c6fe 293 }
mbed_official 630:825f75ca301e 294 #endif
mbed_official 340:28d1f895c6fe 295 }
mbed_official 340:28d1f895c6fe 296
mbed_official 340:28d1f895c6fe 297 #if DEVICE_I2CSLAVE
mbed_official 340:28d1f895c6fe 298
mbed_official 630:825f75ca301e 299 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) {
mbed_official 340:28d1f895c6fe 300 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 340:28d1f895c6fe 301 uint16_t tmpreg = 0;
mbed_official 340:28d1f895c6fe 302
mbed_official 340:28d1f895c6fe 303 // disable
mbed_official 340:28d1f895c6fe 304 i2c->OAR1 &= (uint32_t)(~I2C_OAR1_OA1EN);
mbed_official 340:28d1f895c6fe 305 // Get the old register value
mbed_official 340:28d1f895c6fe 306 tmpreg = i2c->OAR1;
mbed_official 340:28d1f895c6fe 307 // Reset address bits
mbed_official 340:28d1f895c6fe 308 tmpreg &= 0xFC00;
mbed_official 340:28d1f895c6fe 309 // Set new address
mbed_official 340:28d1f895c6fe 310 tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits
mbed_official 340:28d1f895c6fe 311 // Store the new register value
mbed_official 340:28d1f895c6fe 312 i2c->OAR1 = tmpreg;
mbed_official 340:28d1f895c6fe 313 // enable
mbed_official 340:28d1f895c6fe 314 i2c->OAR1 |= I2C_OAR1_OA1EN;
mbed_official 340:28d1f895c6fe 315 }
mbed_official 340:28d1f895c6fe 316
mbed_official 630:825f75ca301e 317 void i2c_slave_mode(i2c_t *obj, int enable_slave) {
mbed_official 340:28d1f895c6fe 318 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 340:28d1f895c6fe 319 uint16_t tmpreg;
mbed_official 340:28d1f895c6fe 320
mbed_official 340:28d1f895c6fe 321 // Get the old register value
mbed_official 340:28d1f895c6fe 322 tmpreg = i2c->OAR1;
mbed_official 340:28d1f895c6fe 323
mbed_official 340:28d1f895c6fe 324 // Enable / disable slave
mbed_official 340:28d1f895c6fe 325 if (enable_slave == 1) {
mbed_official 340:28d1f895c6fe 326 tmpreg |= I2C_OAR1_OA1EN;
mbed_official 340:28d1f895c6fe 327 } else {
mbed_official 340:28d1f895c6fe 328 tmpreg &= (uint32_t)(~I2C_OAR1_OA1EN);
mbed_official 340:28d1f895c6fe 329 }
mbed_official 340:28d1f895c6fe 330
mbed_official 340:28d1f895c6fe 331 // Set new mode
mbed_official 340:28d1f895c6fe 332 i2c->OAR1 = tmpreg;
mbed_official 340:28d1f895c6fe 333 }
mbed_official 340:28d1f895c6fe 334
mbed_official 340:28d1f895c6fe 335 // See I2CSlave.h
mbed_official 340:28d1f895c6fe 336 #define NoData 0 // the slave has not been addressed
mbed_official 340:28d1f895c6fe 337 #define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter)
mbed_official 340:28d1f895c6fe 338 #define WriteGeneral 2 // the master is writing to all slave
mbed_official 340:28d1f895c6fe 339 #define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
mbed_official 340:28d1f895c6fe 340
mbed_official 630:825f75ca301e 341 int i2c_slave_receive(i2c_t *obj) {
mbed_official 340:28d1f895c6fe 342 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
mbed_official 340:28d1f895c6fe 343 int retValue = NoData;
mbed_official 340:28d1f895c6fe 344
mbed_official 340:28d1f895c6fe 345 if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == 1) {
mbed_official 340:28d1f895c6fe 346 if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == 1) {
mbed_official 340:28d1f895c6fe 347 if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_DIR) == 1)
mbed_official 340:28d1f895c6fe 348 retValue = ReadAddressed;
mbed_official 340:28d1f895c6fe 349 else
mbed_official 340:28d1f895c6fe 350 retValue = WriteAddressed;
mbed_official 340:28d1f895c6fe 351
mbed_official 340:28d1f895c6fe 352 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_ADDR);
mbed_official 340:28d1f895c6fe 353 }
mbed_official 340:28d1f895c6fe 354 }
mbed_official 340:28d1f895c6fe 355
mbed_official 340:28d1f895c6fe 356 return (retValue);
mbed_official 340:28d1f895c6fe 357 }
mbed_official 340:28d1f895c6fe 358
mbed_official 630:825f75ca301e 359 int i2c_slave_read(i2c_t *obj, char *data, int length) {
mbed_official 340:28d1f895c6fe 360 char size = 0;
mbed_official 340:28d1f895c6fe 361
mbed_official 340:28d1f895c6fe 362 while (size < length) data[size++] = (char)i2c_byte_read(obj, 0);
mbed_official 340:28d1f895c6fe 363
mbed_official 340:28d1f895c6fe 364 return size;
mbed_official 340:28d1f895c6fe 365 }
mbed_official 340:28d1f895c6fe 366
mbed_official 630:825f75ca301e 367 int i2c_slave_write(i2c_t *obj, const char *data, int length) {
mbed_official 340:28d1f895c6fe 368 char size = 0;
mbed_official 340:28d1f895c6fe 369 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
mbed_official 340:28d1f895c6fe 370
mbed_official 340:28d1f895c6fe 371 do {
mbed_official 340:28d1f895c6fe 372 i2c_byte_write(obj, data[size]);
mbed_official 340:28d1f895c6fe 373 size++;
mbed_official 340:28d1f895c6fe 374 } while (size < length);
mbed_official 340:28d1f895c6fe 375
mbed_official 340:28d1f895c6fe 376 return size;
mbed_official 340:28d1f895c6fe 377 }
mbed_official 340:28d1f895c6fe 378
mbed_official 340:28d1f895c6fe 379
mbed_official 340:28d1f895c6fe 380 #endif // DEVICE_I2CSLAVE
mbed_official 340:28d1f895c6fe 381
mbed_official 340:28d1f895c6fe 382 #endif // DEVICE_I2C