mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Tue Dec 16 08:15:08 2014 +0000
Revision:
440:8a0b45cd594f
Parent:
414:4ec4c5b614b0
Child:
489:119543c9f674
Synchronized with git revision 67fbbf0b635d0c0d93fbe433306c537c2ad206aa

Full URL: https://github.com/mbedmicro/mbed/commit/67fbbf0b635d0c0d93fbe433306c537c2ad206aa/

Targets: nrf51 - updating app_timer.c from Norid'c SDKv7.1.0

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