mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue May 27 10:00:08 2014 +0100
Revision:
216:577900467c9e
Parent:
197:36a724675865
Child:
227:7bd0639b8911
Synchronized with git revision 1cdfe81b13a631cf282176dd176df20a6ebbc297

Full URL: https://github.com/mbedmicro/mbed/commit/1cdfe81b13a631cf282176dd176df20a6ebbc297/

[NUCLEO_xxx] Update xxx_free() functions + typo

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