mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Fri Apr 25 14:45:06 2014 +0100
Revision:
171:3d240fda1f07
Parent:
166:cb4253f91ada
Child:
176:2d0c9ad7ef62
Synchronized with git revision 16b63bab2ce6d726a0ec70ecc4f6dff2c17377ac

Full URL: https://github.com/mbedmicro/mbed/commit/16b63bab2ce6d726a0ec70ecc4f6dff2c17377ac/

[NUCLEO_F030R8] add more pins for I2C, SPI, ADC

Who changed what in which revision?

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