Piyamate Wisanuvej / mbed-src-overclock

Fork of mbed-src by mbed official

Committer:
piyamate
Date:
Tue Jul 01 00:03:52 2014 +0000
Revision:
246:15713155d815
Parent:
227:7bd0639b8911
Overclock K64F to 128.125MHz

Who changed what in which revision?

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