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:
Fri Sep 11 09:30:09 2015 +0100
Revision:
621:9c82b0f79f3d
Parent:
227:7bd0639b8911
Synchronized with git revision 6c1d63e069ab9bd86de92e8296ca783681257538

Full URL: https://github.com/mbedmicro/mbed/commit/6c1d63e069ab9bd86de92e8296ca783681257538/

ignore target files not supported by the yotta module

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 155:8435094ec241 1 /* mbed Microcontroller Library
mbed_official 155:8435094ec241 2 *******************************************************************************
mbed_official 155:8435094ec241 3 * Copyright (c) 2014, STMicroelectronics
mbed_official 155:8435094ec241 4 * All rights reserved.
mbed_official 155:8435094ec241 5 *
mbed_official 155:8435094ec241 6 * Redistribution and use in source and binary forms, with or without
mbed_official 155:8435094ec241 7 * modification, are permitted provided that the following conditions are met:
mbed_official 155:8435094ec241 8 *
mbed_official 155:8435094ec241 9 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 155:8435094ec241 10 * this list of conditions and the following disclaimer.
mbed_official 155:8435094ec241 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 155:8435094ec241 12 * this list of conditions and the following disclaimer in the documentation
mbed_official 155:8435094ec241 13 * and/or other materials provided with the distribution.
mbed_official 155:8435094ec241 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 155:8435094ec241 15 * may be used to endorse or promote products derived from this software
mbed_official 155:8435094ec241 16 * without specific prior written permission.
mbed_official 155:8435094ec241 17 *
mbed_official 155:8435094ec241 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 155:8435094ec241 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 155:8435094ec241 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 155:8435094ec241 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 155:8435094ec241 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 155:8435094ec241 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 155:8435094ec241 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 155:8435094ec241 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 155:8435094ec241 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 155:8435094ec241 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 155:8435094ec241 28 *******************************************************************************
mbed_official 155:8435094ec241 29 */
mbed_official 227:7bd0639b8911 30 #include "mbed_assert.h"
mbed_official 155:8435094ec241 31 #include "i2c_api.h"
mbed_official 155:8435094ec241 32
mbed_official 155:8435094ec241 33 #if DEVICE_I2C
mbed_official 155:8435094ec241 34
mbed_official 155:8435094ec241 35 #include "cmsis.h"
mbed_official 155:8435094ec241 36 #include "pinmap.h"
mbed_official 155:8435094ec241 37
mbed_official 155:8435094ec241 38 /* Timeout values for flags and events waiting loops. These timeouts are
mbed_official 155:8435094ec241 39 not based on accurate values, they just guarantee that the application will
mbed_official 155:8435094ec241 40 not remain stuck if the I2C communication is corrupted. */
mbed_official 155:8435094ec241 41 #define FLAG_TIMEOUT ((int)0x1000)
mbed_official 155:8435094ec241 42 #define LONG_TIMEOUT ((int)0x8000)
mbed_official 155:8435094ec241 43
mbed_official 155:8435094ec241 44 static const PinMap PinMap_I2C_SDA[] = {
mbed_official 155:8435094ec241 45 {PA_10, I2C_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_4)},
mbed_official 155:8435094ec241 46 {PA_14, I2C_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_4)},
mbed_official 155:8435094ec241 47 {PB_5, I2C_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_8)},
mbed_official 155:8435094ec241 48 {PB_7, I2C_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_4)},
mbed_official 155:8435094ec241 49 {PB_9, I2C_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_4)},
mbed_official 155:8435094ec241 50 {PC_9, I2C_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_3)},
mbed_official 155:8435094ec241 51 {PF_0, I2C_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_4)},
mbed_official 155:8435094ec241 52 {NC, NC, 0}
mbed_official 155:8435094ec241 53 };
mbed_official 155:8435094ec241 54
mbed_official 155:8435094ec241 55 static const PinMap PinMap_I2C_SCL[] = {
mbed_official 155:8435094ec241 56 {PA_8, I2C_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_3)},
mbed_official 155:8435094ec241 57 {PA_9, I2C_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_4)},
mbed_official 155:8435094ec241 58 {PA_15, I2C_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_4)},
mbed_official 155:8435094ec241 59 {PB_6, I2C_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_4)},
mbed_official 155:8435094ec241 60 {PB_8, I2C_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_4)},
mbed_official 155:8435094ec241 61 {PF_1, I2C_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_4)},
mbed_official 155:8435094ec241 62 {NC, NC, 0}
mbed_official 155:8435094ec241 63 };
mbed_official 155:8435094ec241 64
mbed_official 155:8435094ec241 65 void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
mbed_official 155:8435094ec241 66 // Determine the I2C to use
mbed_official 155:8435094ec241 67 I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
mbed_official 155:8435094ec241 68 I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
mbed_official 155:8435094ec241 69
mbed_official 155:8435094ec241 70 obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
mbed_official 227:7bd0639b8911 71 MBED_ASSERT(obj->i2c != (I2CName)NC);
mbed_official 155:8435094ec241 72
mbed_official 155:8435094ec241 73 // Enable I2C clock
mbed_official 155:8435094ec241 74 if (obj->i2c == I2C_1) {
mbed_official 155:8435094ec241 75 RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
mbed_official 155:8435094ec241 76 }
mbed_official 155:8435094ec241 77 if (obj->i2c == I2C_2) {
mbed_official 155:8435094ec241 78 RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
mbed_official 155:8435094ec241 79 }
mbed_official 155:8435094ec241 80 if (obj->i2c == I2C_3) {
mbed_official 155:8435094ec241 81 RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C3, ENABLE);
mbed_official 155:8435094ec241 82 }
mbed_official 155:8435094ec241 83
mbed_official 155:8435094ec241 84 // Configure I2C pins
mbed_official 155:8435094ec241 85 pinmap_pinout(scl, PinMap_I2C_SCL);
mbed_official 155:8435094ec241 86 pin_mode(scl, OpenDrain);
mbed_official 155:8435094ec241 87 pinmap_pinout(sda, PinMap_I2C_SDA);
mbed_official 155:8435094ec241 88 pin_mode(sda, OpenDrain);
mbed_official 155:8435094ec241 89
mbed_official 155:8435094ec241 90 // Reset to clear pending flags if any
mbed_official 155:8435094ec241 91 i2c_reset(obj);
mbed_official 155:8435094ec241 92
mbed_official 155:8435094ec241 93 // I2C configuration
mbed_official 155:8435094ec241 94 i2c_frequency(obj, 100000); // 100 kHz per default
mbed_official 155:8435094ec241 95 }
mbed_official 155:8435094ec241 96
mbed_official 155:8435094ec241 97 void i2c_frequency(i2c_t *obj, int hz) {
mbed_official 227:7bd0639b8911 98 MBED_ASSERT((hz == 100000) || (hz == 200000) || (hz == 400000) || (hz == 1000000));
mbed_official 155:8435094ec241 99 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 155:8435094ec241 100 I2C_InitTypeDef I2C_InitStructure;
mbed_official 155:8435094ec241 101 uint32_t tim;
mbed_official 155:8435094ec241 102
mbed_official 155:8435094ec241 103 // Disable the Fast Mode Plus capability
mbed_official 155:8435094ec241 104 RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); // Enable SYSCFG clock
mbed_official 155:8435094ec241 105 SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C1, DISABLE);
mbed_official 155:8435094ec241 106 SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C2, DISABLE);
mbed_official 155:8435094ec241 107
mbed_official 155:8435094ec241 108 /*
mbed_official 155:8435094ec241 109 Values calculated with I2C_Timing_Configuration_V1.0.1.xls file (see AN4235)
mbed_official 155:8435094ec241 110 * Standard mode (up to 100 kHz)
mbed_official 155:8435094ec241 111 * Fast Mode (up to 400 kHz)
mbed_official 155:8435094ec241 112 * Fast Mode Plus (up to 1 MHz)
mbed_official 155:8435094ec241 113 Below values obtained with:
mbed_official 155:8435094ec241 114 - I2C clock source = 8 MHz (HSI clock per default)
mbed_official 155:8435094ec241 115 - Analog filter delay = ON
mbed_official 155:8435094ec241 116 - Digital filter coefficient = 0
mbed_official 155:8435094ec241 117 - Rise time = 100 ns
mbed_official 155:8435094ec241 118 - Fall time = 10ns
mbed_official 155:8435094ec241 119 */
mbed_official 155:8435094ec241 120 switch (hz) {
mbed_official 155:8435094ec241 121 case 100000:
mbed_official 155:8435094ec241 122 tim = 0x00201D2B; // Standard mode
mbed_official 155:8435094ec241 123 break;
mbed_official 155:8435094ec241 124 case 200000:
mbed_official 155:8435094ec241 125 tim = 0x0010021E; // Fast Mode
mbed_official 155:8435094ec241 126 break;
mbed_official 155:8435094ec241 127 case 400000:
mbed_official 155:8435094ec241 128 tim = 0x0010020A; // Fast Mode
mbed_official 155:8435094ec241 129 break;
mbed_official 155:8435094ec241 130 case 1000000:
mbed_official 155:8435094ec241 131 tim = 0x00100001; // Fast Mode Plus
mbed_official 155:8435094ec241 132 // Enable the Fast Mode Plus capability
mbed_official 155:8435094ec241 133 if (obj->i2c == I2C_1) {
mbed_official 155:8435094ec241 134 SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C1, ENABLE);
mbed_official 155:8435094ec241 135 }
mbed_official 155:8435094ec241 136 if (obj->i2c == I2C_2) {
mbed_official 155:8435094ec241 137 SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C2, ENABLE);
mbed_official 155:8435094ec241 138 }
mbed_official 155:8435094ec241 139 break;
mbed_official 155:8435094ec241 140 default:
mbed_official 155:8435094ec241 141 break;
mbed_official 155:8435094ec241 142 }
mbed_official 155:8435094ec241 143
mbed_official 155:8435094ec241 144 // I2C configuration
mbed_official 155:8435094ec241 145 I2C_DeInit(i2c);
mbed_official 155:8435094ec241 146 I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
mbed_official 155:8435094ec241 147 I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
mbed_official 155:8435094ec241 148 I2C_InitStructure.I2C_DigitalFilter = 0x00;
mbed_official 155:8435094ec241 149 I2C_InitStructure.I2C_OwnAddress1 = 0x00;
mbed_official 155:8435094ec241 150 I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
mbed_official 155:8435094ec241 151 I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
mbed_official 155:8435094ec241 152 I2C_InitStructure.I2C_Timing = tim;
mbed_official 155:8435094ec241 153 I2C_Init(i2c, &I2C_InitStructure);
mbed_official 155:8435094ec241 154
mbed_official 155:8435094ec241 155 I2C_Cmd(i2c, ENABLE);
mbed_official 155:8435094ec241 156 }
mbed_official 155:8435094ec241 157
mbed_official 155:8435094ec241 158 inline int i2c_start(i2c_t *obj) {
mbed_official 155:8435094ec241 159 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 155:8435094ec241 160 int timeout;
mbed_official 155:8435094ec241 161
mbed_official 155:8435094ec241 162 // Test BUSY Flag
mbed_official 155:8435094ec241 163 timeout = LONG_TIMEOUT;
mbed_official 155:8435094ec241 164 while (I2C_GetFlagStatus(i2c, I2C_ISR_BUSY) != RESET) {
mbed_official 155:8435094ec241 165 timeout--;
mbed_official 155:8435094ec241 166 if (timeout == 0) {
mbed_official 155:8435094ec241 167 return 0;
mbed_official 155:8435094ec241 168 }
mbed_official 155:8435094ec241 169 }
mbed_official 155:8435094ec241 170
mbed_official 155:8435094ec241 171 I2C_GenerateSTART(i2c, ENABLE);
mbed_official 155:8435094ec241 172
mbed_official 155:8435094ec241 173 return 0;
mbed_official 155:8435094ec241 174 }
mbed_official 155:8435094ec241 175
mbed_official 155:8435094ec241 176 inline int i2c_stop(i2c_t *obj) {
mbed_official 155:8435094ec241 177 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 155:8435094ec241 178
mbed_official 155:8435094ec241 179 I2C_GenerateSTOP(i2c, ENABLE);
mbed_official 155:8435094ec241 180
mbed_official 155:8435094ec241 181 return 0;
mbed_official 155:8435094ec241 182 }
mbed_official 155:8435094ec241 183
mbed_official 155:8435094ec241 184 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
mbed_official 155:8435094ec241 185 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 155:8435094ec241 186 int count;
mbed_official 155:8435094ec241 187 int value;
mbed_official 155:8435094ec241 188
mbed_official 155:8435094ec241 189 if (length == 0) return 0;
mbed_official 155:8435094ec241 190
mbed_official 155:8435094ec241 191 // Configure slave address, nbytes, reload, end mode and start or stop generation
mbed_official 155:8435094ec241 192 I2C_TransferHandling(i2c, address, length, I2C_AutoEnd_Mode, I2C_Generate_Start_Read);
mbed_official 155:8435094ec241 193
mbed_official 155:8435094ec241 194 // Read all bytes
mbed_official 155:8435094ec241 195 for (count = 0; count < length; count++) {
mbed_official 155:8435094ec241 196 value = i2c_byte_read(obj, 0);
mbed_official 155:8435094ec241 197 data[count] = (char)value;
mbed_official 155:8435094ec241 198 }
mbed_official 155:8435094ec241 199
mbed_official 155:8435094ec241 200 return length;
mbed_official 155:8435094ec241 201 }
mbed_official 155:8435094ec241 202
mbed_official 155:8435094ec241 203 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
mbed_official 155:8435094ec241 204 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 155:8435094ec241 205 //int timeout;
mbed_official 155:8435094ec241 206 int count;
mbed_official 155:8435094ec241 207
mbed_official 155:8435094ec241 208 if (length == 0) return 0;
mbed_official 155:8435094ec241 209
mbed_official 155:8435094ec241 210 // [TODO] The stop is always sent even with I2C_SoftEnd_Mode. To be corrected.
mbed_official 155:8435094ec241 211
mbed_official 155:8435094ec241 212 // Configure slave address, nbytes, reload, end mode and start or stop generation
mbed_official 155:8435094ec241 213 //if (stop) {
mbed_official 155:8435094ec241 214 I2C_TransferHandling(i2c, address, length, I2C_AutoEnd_Mode, I2C_Generate_Start_Write);
mbed_official 155:8435094ec241 215 //}
mbed_official 155:8435094ec241 216 //else {
mbed_official 155:8435094ec241 217 // I2C_TransferHandling(i2c, address, length, I2C_SoftEnd_Mode, I2C_Generate_Start_Write);
mbed_official 155:8435094ec241 218 //}
mbed_official 155:8435094ec241 219
mbed_official 155:8435094ec241 220 // Write all bytes
mbed_official 155:8435094ec241 221 for (count = 0; count < length; count++) {
mbed_official 155:8435094ec241 222 if (i2c_byte_write(obj, data[count]) != 1) {
mbed_official 155:8435094ec241 223 i2c_stop(obj);
mbed_official 155:8435094ec241 224 return 0;
mbed_official 155:8435094ec241 225 }
mbed_official 155:8435094ec241 226 }
mbed_official 155:8435094ec241 227
mbed_official 155:8435094ec241 228 /*
mbed_official 155:8435094ec241 229 if (stop) {
mbed_official 155:8435094ec241 230 // Wait until STOPF flag is set
mbed_official 155:8435094ec241 231 timeout = LONG_TIMEOUT;
mbed_official 155:8435094ec241 232 while (I2C_GetFlagStatus(i2c, I2C_ISR_STOPF) == RESET) {
mbed_official 155:8435094ec241 233 timeout--;
mbed_official 155:8435094ec241 234 if (timeout == 0) {
mbed_official 155:8435094ec241 235 return 0;
mbed_official 155:8435094ec241 236 }
mbed_official 155:8435094ec241 237 }
mbed_official 155:8435094ec241 238 // Clear STOPF flag
mbed_official 155:8435094ec241 239 I2C_ClearFlag(i2c, I2C_ICR_STOPCF);
mbed_official 155:8435094ec241 240 }
mbed_official 155:8435094ec241 241 */
mbed_official 155:8435094ec241 242
mbed_official 155:8435094ec241 243 return count;
mbed_official 155:8435094ec241 244 }
mbed_official 155:8435094ec241 245
mbed_official 155:8435094ec241 246 int i2c_byte_read(i2c_t *obj, int last) {
mbed_official 155:8435094ec241 247 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 155:8435094ec241 248 uint8_t data;
mbed_official 155:8435094ec241 249 int timeout;
mbed_official 155:8435094ec241 250
mbed_official 155:8435094ec241 251 // Wait until the byte is received
mbed_official 155:8435094ec241 252 timeout = FLAG_TIMEOUT;
mbed_official 155:8435094ec241 253 while (I2C_GetFlagStatus(i2c, I2C_ISR_RXNE) == RESET) {
mbed_official 155:8435094ec241 254 timeout--;
mbed_official 155:8435094ec241 255 if (timeout == 0) {
mbed_official 155:8435094ec241 256 return 0;
mbed_official 155:8435094ec241 257 }
mbed_official 155:8435094ec241 258 }
mbed_official 155:8435094ec241 259
mbed_official 155:8435094ec241 260 data = I2C_ReceiveData(i2c);
mbed_official 155:8435094ec241 261
mbed_official 155:8435094ec241 262 return (int)data;
mbed_official 155:8435094ec241 263 }
mbed_official 155:8435094ec241 264
mbed_official 155:8435094ec241 265 int i2c_byte_write(i2c_t *obj, int data) {
mbed_official 155:8435094ec241 266 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 155:8435094ec241 267 int timeout;
mbed_official 155:8435094ec241 268
mbed_official 155:8435094ec241 269 // Wait until the previous byte is transmitted
mbed_official 155:8435094ec241 270 timeout = FLAG_TIMEOUT;
mbed_official 155:8435094ec241 271 while (I2C_GetFlagStatus(i2c, I2C_ISR_TXIS) == RESET) {
mbed_official 155:8435094ec241 272 timeout--;
mbed_official 155:8435094ec241 273 if (timeout == 0) {
mbed_official 155:8435094ec241 274 return 0;
mbed_official 155:8435094ec241 275 }
mbed_official 155:8435094ec241 276 }
mbed_official 155:8435094ec241 277
mbed_official 155:8435094ec241 278 I2C_SendData(i2c, (uint8_t)data);
mbed_official 155:8435094ec241 279
mbed_official 155:8435094ec241 280 return 1;
mbed_official 155:8435094ec241 281 }
mbed_official 155:8435094ec241 282
mbed_official 155:8435094ec241 283 void i2c_reset(i2c_t *obj) {
mbed_official 155:8435094ec241 284 if (obj->i2c == I2C_1) {
mbed_official 155:8435094ec241 285 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
mbed_official 155:8435094ec241 286 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
mbed_official 155:8435094ec241 287 }
mbed_official 155:8435094ec241 288 if (obj->i2c == I2C_2) {
mbed_official 155:8435094ec241 289 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
mbed_official 155:8435094ec241 290 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);
mbed_official 155:8435094ec241 291 }
mbed_official 155:8435094ec241 292 if (obj->i2c == I2C_3) {
mbed_official 155:8435094ec241 293 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C3, ENABLE);
mbed_official 155:8435094ec241 294 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C3, DISABLE);
mbed_official 155:8435094ec241 295 }
mbed_official 155:8435094ec241 296 }
mbed_official 155:8435094ec241 297
mbed_official 155:8435094ec241 298 #if DEVICE_I2CSLAVE
mbed_official 155:8435094ec241 299
mbed_official 155:8435094ec241 300 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) {
mbed_official 155:8435094ec241 301 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 155:8435094ec241 302 uint16_t tmpreg;
mbed_official 155:8435094ec241 303
mbed_official 155:8435094ec241 304 // Get the old register value
mbed_official 155:8435094ec241 305 tmpreg = i2c->OAR1;
mbed_official 155:8435094ec241 306 // Reset address bits
mbed_official 155:8435094ec241 307 tmpreg &= 0xFC00;
mbed_official 155:8435094ec241 308 // Set new address
mbed_official 155:8435094ec241 309 tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits
mbed_official 155:8435094ec241 310 // Store the new register value
mbed_official 155:8435094ec241 311 i2c->OAR1 = tmpreg;
mbed_official 155:8435094ec241 312 }
mbed_official 155:8435094ec241 313
mbed_official 155:8435094ec241 314 void i2c_slave_mode(i2c_t *obj, int enable_slave) {
mbed_official 155:8435094ec241 315 // Nothing to do
mbed_official 155:8435094ec241 316 }
mbed_official 155:8435094ec241 317
mbed_official 155:8435094ec241 318 // See I2CSlave.h
mbed_official 155:8435094ec241 319 #define NoData 0 // the slave has not been addressed
mbed_official 155:8435094ec241 320 #define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter)
mbed_official 155:8435094ec241 321 #define WriteGeneral 2 // the master is writing to all slave
mbed_official 155:8435094ec241 322 #define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
mbed_official 155:8435094ec241 323
mbed_official 155:8435094ec241 324 int i2c_slave_receive(i2c_t *obj) {
mbed_official 155:8435094ec241 325 // TO BE DONE
mbed_official 155:8435094ec241 326 return (0);
mbed_official 155:8435094ec241 327 }
mbed_official 155:8435094ec241 328
mbed_official 155:8435094ec241 329 int i2c_slave_read(i2c_t *obj, char *data, int length) {
mbed_official 155:8435094ec241 330 int count = 0;
mbed_official 155:8435094ec241 331
mbed_official 155:8435094ec241 332 // Read all bytes
mbed_official 155:8435094ec241 333 for (count = 0; count < length; count++) {
mbed_official 155:8435094ec241 334 data[count] = i2c_byte_read(obj, 0);
mbed_official 155:8435094ec241 335 }
mbed_official 155:8435094ec241 336
mbed_official 155:8435094ec241 337 return count;
mbed_official 155:8435094ec241 338 }
mbed_official 155:8435094ec241 339
mbed_official 155:8435094ec241 340 int i2c_slave_write(i2c_t *obj, const char *data, int length) {
mbed_official 155:8435094ec241 341 int count = 0;
mbed_official 155:8435094ec241 342
mbed_official 155:8435094ec241 343 // Write all bytes
mbed_official 155:8435094ec241 344 for (count = 0; count < length; count++) {
mbed_official 155:8435094ec241 345 i2c_byte_write(obj, data[count]);
mbed_official 155:8435094ec241 346 }
mbed_official 155:8435094ec241 347
mbed_official 155:8435094ec241 348 return count;
mbed_official 155:8435094ec241 349 }
mbed_official 155:8435094ec241 350
mbed_official 155:8435094ec241 351
mbed_official 155:8435094ec241 352 #endif // DEVICE_I2CSLAVE
mbed_official 155:8435094ec241 353
mbed_official 155:8435094ec241 354 #endif // DEVICE_I2C