zain aftab / mbed-src

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Fri Nov 14 09:00:07 2014 +0000
Revision:
402:09075a3b15e3
Parent:
383:0564d9840d0d
Child:
414:4ec4c5b614b0
Synchronized with git revision dffeedc96e731e30785ca4a7842fb9769412bea1

Full URL: https://github.com/mbedmicro/mbed/commit/dffeedc96e731e30785ca4a7842fb9769412bea1/

Targets: add USBTX and USBRX pin definitions for targets that don't provide them

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 305:1f0269907d8b 58 int i2c1_inited = 0;
mbed_official 305:1f0269907d8b 59 int i2c2_inited = 0;
mbed_official 305:1f0269907d8b 60
mbed_official 402:09075a3b15e3 61 void i2c_init(i2c_t *obj, PinName sda, PinName scl)
mbed_official 402:09075a3b15e3 62 {
mbed_official 56:99eb381a3269 63 // Determine the I2C to use
mbed_official 56:99eb381a3269 64 I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
mbed_official 56:99eb381a3269 65 I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
mbed_official 56:99eb381a3269 66
mbed_official 56:99eb381a3269 67 obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
mbed_official 227:7bd0639b8911 68 MBED_ASSERT(obj->i2c != (I2CName)NC);
mbed_official 56:99eb381a3269 69
mbed_official 305:1f0269907d8b 70 // Enable I2C clock and configure I2C pins if not done before
mbed_official 402:09075a3b15e3 71 if ((obj->i2c == I2C_1) && !i2c1_inited) {
mbed_official 305:1f0269907d8b 72 i2c1_inited = 1;
mbed_official 56:99eb381a3269 73 RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
mbed_official 305:1f0269907d8b 74 // Configure I2C pins
mbed_official 305:1f0269907d8b 75 pinmap_pinout(scl, PinMap_I2C_SCL);
mbed_official 305:1f0269907d8b 76 pin_mode(scl, OpenDrain);
mbed_official 305:1f0269907d8b 77 pinmap_pinout(sda, PinMap_I2C_SDA);
mbed_official 305:1f0269907d8b 78 pin_mode(sda, OpenDrain);
mbed_official 56:99eb381a3269 79 }
mbed_official 402:09075a3b15e3 80 if ((obj->i2c == I2C_2) && !i2c2_inited) {
mbed_official 305:1f0269907d8b 81 i2c2_inited = 1;
mbed_official 56:99eb381a3269 82 RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
mbed_official 383:0564d9840d0d 83 // Configure I2C pins
mbed_official 383:0564d9840d0d 84 pinmap_pinout(scl, PinMap_I2C_SCL);
mbed_official 383:0564d9840d0d 85 pin_mode(scl, OpenDrain);
mbed_official 383:0564d9840d0d 86 pinmap_pinout(sda, PinMap_I2C_SDA);
mbed_official 383:0564d9840d0d 87 pin_mode(sda, OpenDrain);
mbed_official 383:0564d9840d0d 88 }
mbed_official 174:8bb9f3a33240 89
mbed_official 56:99eb381a3269 90 // Reset to clear pending flags if any
mbed_official 56:99eb381a3269 91 i2c_reset(obj);
mbed_official 174:8bb9f3a33240 92
mbed_official 56:99eb381a3269 93 // I2C configuration
mbed_official 174:8bb9f3a33240 94 i2c_frequency(obj, 100000); // 100 kHz per default
mbed_official 56:99eb381a3269 95 }
mbed_official 56:99eb381a3269 96
mbed_official 402:09075a3b15e3 97 void i2c_frequency(i2c_t *obj, int hz)
mbed_official 402:09075a3b15e3 98 {
mbed_official 383:0564d9840d0d 99 int timeout;
mbed_official 383:0564d9840d0d 100
mbed_official 383:0564d9840d0d 101 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 56:99eb381a3269 102 I2C_InitTypeDef I2C_InitStructure;
mbed_official 174:8bb9f3a33240 103
mbed_official 56:99eb381a3269 104 if ((hz != 0) && (hz <= 400000)) {
mbed_official 305:1f0269907d8b 105 // wait before init
mbed_official 305:1f0269907d8b 106 timeout = LONG_TIMEOUT;
mbed_official 383:0564d9840d0d 107 while ((I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY)) && (timeout-- != 0)) {
mbed_official 383:0564d9840d0d 108 }
mbed_official 383:0564d9840d0d 109
mbed_official 80:66393a7b209d 110 I2C_DeInit(i2c);
mbed_official 174:8bb9f3a33240 111
mbed_official 56:99eb381a3269 112 // I2C configuration
mbed_official 167:d5744491c362 113 I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
mbed_official 167:d5744491c362 114 I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
mbed_official 167:d5744491c362 115 I2C_InitStructure.I2C_OwnAddress1 = 0;
mbed_official 167:d5744491c362 116 I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
mbed_official 56:99eb381a3269 117 I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
mbed_official 167:d5744491c362 118 I2C_InitStructure.I2C_ClockSpeed = hz;
mbed_official 80:66393a7b209d 119 I2C_Init(i2c, &I2C_InitStructure);
mbed_official 174:8bb9f3a33240 120
mbed_official 56:99eb381a3269 121 I2C_Cmd(i2c, ENABLE);
mbed_official 56:99eb381a3269 122 }
mbed_official 56:99eb381a3269 123 }
mbed_official 56:99eb381a3269 124
mbed_official 402:09075a3b15e3 125 inline int i2c_start(i2c_t *obj)
mbed_official 402:09075a3b15e3 126 {
mbed_official 56:99eb381a3269 127 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 383:0564d9840d0d 128 int timeout;
mbed_official 174:8bb9f3a33240 129
mbed_official 56:99eb381a3269 130 I2C_ClearFlag(i2c, I2C_FLAG_AF); // Clear Acknowledge failure flag
mbed_official 174:8bb9f3a33240 131
mbed_official 56:99eb381a3269 132 // Generate the START condition
mbed_official 174:8bb9f3a33240 133 I2C_GenerateSTART(i2c, ENABLE);
mbed_official 174:8bb9f3a33240 134
mbed_official 56:99eb381a3269 135 // Wait the START condition has been correctly sent
mbed_official 56:99eb381a3269 136 timeout = FLAG_TIMEOUT;
mbed_official 56:99eb381a3269 137 while (I2C_GetFlagStatus(i2c, I2C_FLAG_SB) == RESET) {
mbed_official 80:66393a7b209d 138 timeout--;
mbed_official 80:66393a7b209d 139 if (timeout == 0) {
mbed_official 80:66393a7b209d 140 return 1;
mbed_official 80:66393a7b209d 141 }
mbed_official 56:99eb381a3269 142 }
mbed_official 174:8bb9f3a33240 143
mbed_official 58:3b55b7a41411 144 return 0;
mbed_official 56:99eb381a3269 145 }
mbed_official 56:99eb381a3269 146
mbed_official 402:09075a3b15e3 147 inline int i2c_stop(i2c_t *obj)
mbed_official 402:09075a3b15e3 148 {
mbed_official 56:99eb381a3269 149 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 174:8bb9f3a33240 150
mbed_official 56:99eb381a3269 151 I2C_GenerateSTOP(i2c, ENABLE);
mbed_official 174:8bb9f3a33240 152
mbed_official 58:3b55b7a41411 153 return 0;
mbed_official 56:99eb381a3269 154 }
mbed_official 56:99eb381a3269 155
mbed_official 402:09075a3b15e3 156 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
mbed_official 402:09075a3b15e3 157 {
mbed_official 56:99eb381a3269 158 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 383:0564d9840d0d 159 int timeout;
mbed_official 383:0564d9840d0d 160 int count;
mbed_official 383:0564d9840d0d 161 int value;
mbed_official 174:8bb9f3a33240 162
mbed_official 56:99eb381a3269 163 i2c_start(obj);
mbed_official 56:99eb381a3269 164
mbed_official 56:99eb381a3269 165 // Send slave address for read
mbed_official 174:8bb9f3a33240 166 I2C_Send7bitAddress(i2c, address, I2C_Direction_Receiver);
mbed_official 56:99eb381a3269 167
mbed_official 56:99eb381a3269 168 // Wait address is acknowledged
mbed_official 56:99eb381a3269 169 timeout = FLAG_TIMEOUT;
mbed_official 56:99eb381a3269 170 while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) == ERROR) {
mbed_official 80:66393a7b209d 171 timeout--;
mbed_official 80:66393a7b209d 172 if (timeout == 0) {
mbed_official 247:135e3186a638 173 return -1;
mbed_official 80:66393a7b209d 174 }
mbed_official 56:99eb381a3269 175 }
mbed_official 174:8bb9f3a33240 176
mbed_official 56:99eb381a3269 177 // Read all bytes except last one
mbed_official 56:99eb381a3269 178 for (count = 0; count < (length - 1); count++) {
mbed_official 383:0564d9840d0d 179 value = i2c_byte_read(obj, 0);
mbed_official 56:99eb381a3269 180 data[count] = (char)value;
mbed_official 56:99eb381a3269 181 }
mbed_official 174:8bb9f3a33240 182
mbed_official 56:99eb381a3269 183 // If not repeated start, send stop.
mbed_official 56:99eb381a3269 184 // Warning: must be done BEFORE the data is read.
mbed_official 56:99eb381a3269 185 if (stop) {
mbed_official 56:99eb381a3269 186 i2c_stop(obj);
mbed_official 56:99eb381a3269 187 }
mbed_official 56:99eb381a3269 188
mbed_official 56:99eb381a3269 189 // Read the last byte
mbed_official 383:0564d9840d0d 190 value = i2c_byte_read(obj, 1);
mbed_official 56:99eb381a3269 191 data[count] = (char)value;
mbed_official 174:8bb9f3a33240 192
mbed_official 56:99eb381a3269 193 return length;
mbed_official 56:99eb381a3269 194 }
mbed_official 56:99eb381a3269 195
mbed_official 402:09075a3b15e3 196 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
mbed_official 402:09075a3b15e3 197 {
mbed_official 56:99eb381a3269 198 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 383:0564d9840d0d 199 int timeout;
mbed_official 383:0564d9840d0d 200 int count;
mbed_official 208:4557f4bb2dd5 201
mbed_official 56:99eb381a3269 202 i2c_start(obj);
mbed_official 56:99eb381a3269 203
mbed_official 56:99eb381a3269 204 // Send slave address for write
mbed_official 56:99eb381a3269 205 I2C_Send7bitAddress(i2c, address, I2C_Direction_Transmitter);
mbed_official 174:8bb9f3a33240 206
mbed_official 56:99eb381a3269 207 // Wait address is acknowledged
mbed_official 56:99eb381a3269 208 timeout = FLAG_TIMEOUT;
mbed_official 56:99eb381a3269 209 while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) == ERROR) {
mbed_official 80:66393a7b209d 210 timeout--;
mbed_official 80:66393a7b209d 211 if (timeout == 0) {
mbed_official 247:135e3186a638 212 return -1;
mbed_official 80:66393a7b209d 213 }
mbed_official 56:99eb381a3269 214 }
mbed_official 56:99eb381a3269 215
mbed_official 56:99eb381a3269 216 for (count = 0; count < length; count++) {
mbed_official 58:3b55b7a41411 217 if (i2c_byte_write(obj, data[count]) != 1) {
mbed_official 58:3b55b7a41411 218 i2c_stop(obj);
mbed_official 58:3b55b7a41411 219 return 0;
mbed_official 56:99eb381a3269 220 }
mbed_official 56:99eb381a3269 221 }
mbed_official 56:99eb381a3269 222
mbed_official 56:99eb381a3269 223 // If not repeated start, send stop.
mbed_official 56:99eb381a3269 224 if (stop) {
mbed_official 56:99eb381a3269 225 i2c_stop(obj);
mbed_official 56:99eb381a3269 226 }
mbed_official 56:99eb381a3269 227
mbed_official 56:99eb381a3269 228 return count;
mbed_official 56:99eb381a3269 229 }
mbed_official 56:99eb381a3269 230
mbed_official 402:09075a3b15e3 231 int i2c_byte_read(i2c_t *obj, int last)
mbed_official 402:09075a3b15e3 232 {
mbed_official 56:99eb381a3269 233 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 383:0564d9840d0d 234 uint8_t data;
mbed_official 383:0564d9840d0d 235 int timeout;
mbed_official 174:8bb9f3a33240 236
mbed_official 56:99eb381a3269 237 if (last) {
mbed_official 56:99eb381a3269 238 // Don't acknowledge the last byte
mbed_official 56:99eb381a3269 239 I2C_AcknowledgeConfig(i2c, DISABLE);
mbed_official 56:99eb381a3269 240 } else {
mbed_official 56:99eb381a3269 241 // Acknowledge the byte
mbed_official 56:99eb381a3269 242 I2C_AcknowledgeConfig(i2c, ENABLE);
mbed_official 56:99eb381a3269 243 }
mbed_official 56:99eb381a3269 244
mbed_official 56:99eb381a3269 245 // Wait until the byte is received
mbed_official 56:99eb381a3269 246 timeout = FLAG_TIMEOUT;
mbed_official 56:99eb381a3269 247 while (I2C_GetFlagStatus(i2c, I2C_FLAG_RXNE) == RESET) {
mbed_official 80:66393a7b209d 248 timeout--;
mbed_official 80:66393a7b209d 249 if (timeout == 0) {
mbed_official 247:135e3186a638 250 return -1;
mbed_official 80:66393a7b209d 251 }
mbed_official 56:99eb381a3269 252 }
mbed_official 56:99eb381a3269 253
mbed_official 56:99eb381a3269 254 data = I2C_ReceiveData(i2c);
mbed_official 174:8bb9f3a33240 255
mbed_official 56:99eb381a3269 256 return (int)data;
mbed_official 56:99eb381a3269 257 }
mbed_official 56:99eb381a3269 258
mbed_official 402:09075a3b15e3 259 int i2c_byte_write(i2c_t *obj, int data)
mbed_official 402:09075a3b15e3 260 {
mbed_official 56:99eb381a3269 261 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 383:0564d9840d0d 262 int timeout;
mbed_official 56:99eb381a3269 263
mbed_official 56:99eb381a3269 264 I2C_SendData(i2c, (uint8_t)data);
mbed_official 56:99eb381a3269 265
mbed_official 56:99eb381a3269 266 // Wait until the byte is transmitted
mbed_official 208:4557f4bb2dd5 267 timeout = FLAG_TIMEOUT;
mbed_official 56:99eb381a3269 268 while ((I2C_GetFlagStatus(i2c, I2C_FLAG_TXE) == RESET) &&
mbed_official 402:09075a3b15e3 269 (I2C_GetFlagStatus(i2c, I2C_FLAG_BTF) == RESET)) {
mbed_official 80:66393a7b209d 270 timeout--;
mbed_official 80:66393a7b209d 271 if (timeout == 0) {
mbed_official 58:3b55b7a41411 272 return 0;
mbed_official 56:99eb381a3269 273 }
mbed_official 56:99eb381a3269 274 }
mbed_official 174:8bb9f3a33240 275
mbed_official 58:3b55b7a41411 276 return 1;
mbed_official 56:99eb381a3269 277 }
mbed_official 56:99eb381a3269 278
mbed_official 402:09075a3b15e3 279 void i2c_reset(i2c_t *obj)
mbed_official 402:09075a3b15e3 280 {
mbed_official 305:1f0269907d8b 281 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 383:0564d9840d0d 282 int timeout;
mbed_official 383:0564d9840d0d 283
mbed_official 305:1f0269907d8b 284 // wait before reset
mbed_official 305:1f0269907d8b 285 timeout = LONG_TIMEOUT;
mbed_official 383:0564d9840d0d 286 while ((I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY)) && (timeout-- != 0)) {
mbed_official 383:0564d9840d0d 287 }
mbed_official 383:0564d9840d0d 288
mbed_official 174:8bb9f3a33240 289 if (obj->i2c == I2C_1) {
mbed_official 56:99eb381a3269 290 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
mbed_official 56:99eb381a3269 291 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
mbed_official 56:99eb381a3269 292 }
mbed_official 56:99eb381a3269 293 if (obj->i2c == I2C_2) {
mbed_official 56:99eb381a3269 294 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
mbed_official 174:8bb9f3a33240 295 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);
mbed_official 56:99eb381a3269 296 }
mbed_official 56:99eb381a3269 297 }
mbed_official 56:99eb381a3269 298
mbed_official 56:99eb381a3269 299 #if DEVICE_I2CSLAVE
mbed_official 56:99eb381a3269 300
mbed_official 402:09075a3b15e3 301 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask)
mbed_official 402:09075a3b15e3 302 {
mbed_official 56:99eb381a3269 303 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 383:0564d9840d0d 304 uint16_t tmpreg;
mbed_official 174:8bb9f3a33240 305
mbed_official 56:99eb381a3269 306 // Get the old register value
mbed_official 56:99eb381a3269 307 tmpreg = i2c->OAR1;
mbed_official 56:99eb381a3269 308 // Reset address bits
mbed_official 56:99eb381a3269 309 tmpreg &= 0xFC00;
mbed_official 56:99eb381a3269 310 // Set new address
mbed_official 56:99eb381a3269 311 tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits
mbed_official 56:99eb381a3269 312 // Store the new register value
mbed_official 56:99eb381a3269 313 i2c->OAR1 = tmpreg;
mbed_official 56:99eb381a3269 314 }
mbed_official 56:99eb381a3269 315
mbed_official 402:09075a3b15e3 316 void i2c_slave_mode(i2c_t *obj, int enable_slave)
mbed_official 402:09075a3b15e3 317 {
mbed_official 56:99eb381a3269 318 // Nothing to do
mbed_official 56:99eb381a3269 319 }
mbed_official 56:99eb381a3269 320
mbed_official 58:3b55b7a41411 321 // See I2CSlave.h
mbed_official 58:3b55b7a41411 322 #define NoData 0 // the slave has not been addressed
mbed_official 58:3b55b7a41411 323 #define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter)
mbed_official 58:3b55b7a41411 324 #define WriteGeneral 2 // the master is writing to all slave
mbed_official 58:3b55b7a41411 325 #define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
mbed_official 56:99eb381a3269 326
mbed_official 402:09075a3b15e3 327 int i2c_slave_receive(i2c_t *obj)
mbed_official 402:09075a3b15e3 328 {
mbed_official 383:0564d9840d0d 329 int retValue = NoData;
mbed_official 383:0564d9840d0d 330 uint32_t event;
mbed_official 181:a4cbdfbbd2f4 331 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 181:a4cbdfbbd2f4 332
mbed_official 208:4557f4bb2dd5 333 event = I2C_GetLastEvent(i2c);
mbed_official 208:4557f4bb2dd5 334 if (event != 0) {
mbed_official 208:4557f4bb2dd5 335 switch (event) {
mbed_official 181:a4cbdfbbd2f4 336 case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED:
mbed_official 181:a4cbdfbbd2f4 337 retValue = WriteAddressed;
mbed_official 181:a4cbdfbbd2f4 338 break;
mbed_official 181:a4cbdfbbd2f4 339 case I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED:
mbed_official 181:a4cbdfbbd2f4 340 retValue = ReadAddressed;
mbed_official 181:a4cbdfbbd2f4 341 break;
mbed_official 181:a4cbdfbbd2f4 342 case I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED:
mbed_official 181:a4cbdfbbd2f4 343 retValue = WriteGeneral;
mbed_official 181:a4cbdfbbd2f4 344 break;
mbed_official 181:a4cbdfbbd2f4 345 default:
mbed_official 181:a4cbdfbbd2f4 346 retValue = NoData;
mbed_official 181:a4cbdfbbd2f4 347 break;
mbed_official 181:a4cbdfbbd2f4 348 }
mbed_official 181:a4cbdfbbd2f4 349
mbed_official 208:4557f4bb2dd5 350 // clear ADDR
mbed_official 208:4557f4bb2dd5 351 if ((retValue == WriteAddressed) || (retValue == ReadAddressed)) {
mbed_official 181:a4cbdfbbd2f4 352 // read SR to clear ADDR flag
mbed_official 181:a4cbdfbbd2f4 353 i2c->SR1;
mbed_official 181:a4cbdfbbd2f4 354 i2c->SR2;
mbed_official 181:a4cbdfbbd2f4 355 }
mbed_official 181:a4cbdfbbd2f4 356 // clear stopf
mbed_official 208:4557f4bb2dd5 357 if (I2C_GetFlagStatus(i2c, I2C_FLAG_STOPF) == SET) {
mbed_official 181:a4cbdfbbd2f4 358 // read SR1 and write CR1 to clear STOP flag
mbed_official 181:a4cbdfbbd2f4 359 i2c->SR1;
mbed_official 208:4557f4bb2dd5 360 I2C_Cmd(i2c, ENABLE);
mbed_official 181:a4cbdfbbd2f4 361 }
mbed_official 181:a4cbdfbbd2f4 362 // clear AF
mbed_official 208:4557f4bb2dd5 363 if (I2C_GetFlagStatus(i2c, I2C_FLAG_AF) == SET) {
mbed_official 181:a4cbdfbbd2f4 364 I2C_ClearFlag(i2c, I2C_FLAG_AF);
mbed_official 208:4557f4bb2dd5 365 }
mbed_official 181:a4cbdfbbd2f4 366 }
mbed_official 208:4557f4bb2dd5 367 return (retValue);
mbed_official 56:99eb381a3269 368 }
mbed_official 56:99eb381a3269 369
mbed_official 402:09075a3b15e3 370 int i2c_slave_read(i2c_t *obj, char *data, int length)
mbed_official 402:09075a3b15e3 371 {
mbed_official 58:3b55b7a41411 372 int count = 0;
mbed_official 174:8bb9f3a33240 373
mbed_official 58:3b55b7a41411 374 // Read all bytes
mbed_official 58:3b55b7a41411 375 for (count = 0; count < length; count++) {
mbed_official 58:3b55b7a41411 376 data[count] = i2c_byte_read(obj, 0);
mbed_official 58:3b55b7a41411 377 }
mbed_official 174:8bb9f3a33240 378
mbed_official 58:3b55b7a41411 379 return count;
mbed_official 56:99eb381a3269 380 }
mbed_official 56:99eb381a3269 381
mbed_official 402:09075a3b15e3 382 int i2c_slave_write(i2c_t *obj, const char *data, int length)
mbed_official 402:09075a3b15e3 383 {
mbed_official 58:3b55b7a41411 384 int count = 0;
mbed_official 174:8bb9f3a33240 385
mbed_official 58:3b55b7a41411 386 // Write all bytes
mbed_official 58:3b55b7a41411 387 for (count = 0; count < length; count++) {
mbed_official 58:3b55b7a41411 388 i2c_byte_write(obj, data[count]);
mbed_official 58:3b55b7a41411 389 }
mbed_official 174:8bb9f3a33240 390
mbed_official 58:3b55b7a41411 391 return count;
mbed_official 56:99eb381a3269 392 }
mbed_official 56:99eb381a3269 393
mbed_official 56:99eb381a3269 394 #endif // DEVICE_I2CSLAVE
mbed_official 56:99eb381a3269 395
mbed_official 56:99eb381a3269 396 #endif // DEVICE_I2C