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 Jul 29 19:00:07 2014 +0100
Revision:
268:402bcc0c870b
Parent:
247:135e3186a638
Child:
305:1f0269907d8b
Synchronized with git revision 490d1a6606b3138f165c5edf2f2370ca616587c0

Full URL: https://github.com/mbedmicro/mbed/commit/490d1a6606b3138f165c5edf2f2370ca616587c0/

[LPC1114] Sleep fix + some device.h settings

Who changed what in which revision?

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