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