mbed w/ spi bug fig

Dependents:   display-puck

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue Jul 01 15:00:09 2014 +0100
Revision:
247:135e3186a638
Parent:
227:7bd0639b8911
Synchronized with git revision 4f86d397190b9d18f22b4a189573d65b970f3662

Full URL: https://github.com/mbedmicro/mbed/commit/4f86d397190b9d18f22b4a189573d65b970f3662/

[NUCLEOs] enhance I2C API to make it work with EEPROM

Who changed what in which revision?

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