Toyomasa Watarai / mbed-src

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Wed Mar 19 10:15:22 2014 +0000
Revision:
125:23cc3068a9e4
Child:
135:067cc8ba23da
Synchronized with git revision ace35dfba3748c7cdc102eb38ec6b9e1067c3252

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

[NUCLEO_F302R8] Add cmsis and hal files + change F401RE clock to 84MHz

Who changed what in which revision?

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