mbed SDK library sources

Fork of mbed-src by mbed official

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:
Thu Jan 30 12:15:05 2014 +0000
Revision:
80:66393a7b209d
Parent:
76:aeb1df146756
Synchronized with git revision dba523f83fe09b7fce11fc1299dd1216e9776359

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

Update of I2C, SPI, SLEEP for NUCLEO_F103RB and L152RE

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