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