mbed library sources modified for open wear
Dependents: openwear-lifelogger-example
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/i2c_api.c@306:1f0269907d8b, 2014-08-29 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Aug 29 20:45:07 2014 +0100
- Revision:
- 306:1f0269907d8b
- Parent:
- 247:135e3186a638
Synchronized with git revision f304c6ba83591678388024d30440e94781fa8d65
Full URL: https://github.com/mbedmicro/mbed/commit/f304c6ba83591678388024d30440e94781fa8d65/
[NUCLEOs] enhance i2c api
Who changed what in which revision?
User | Revision | Line number | New 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 | 227:7bd0639b8911 | 30 | #include "mbed_assert.h" |
mbed_official | 76:aeb1df146756 | 31 | #include "i2c_api.h" |
mbed_official | 76:aeb1df146756 | 32 | |
mbed_official | 76:aeb1df146756 | 33 | #if DEVICE_I2C |
mbed_official | 76:aeb1df146756 | 34 | |
mbed_official | 76:aeb1df146756 | 35 | #include "cmsis.h" |
mbed_official | 76:aeb1df146756 | 36 | #include "pinmap.h" |
mbed_official | 227:7bd0639b8911 | 37 | |
mbed_official | 76:aeb1df146756 | 38 | |
mbed_official | 76:aeb1df146756 | 39 | /* Timeout values for flags and events waiting loops. These timeouts are |
mbed_official | 174:8bb9f3a33240 | 40 | not based on accurate values, they just guarantee that the application will |
mbed_official | 174:8bb9f3a33240 | 41 | not remain stuck if the I2C communication is corrupted. */ |
mbed_official | 76:aeb1df146756 | 42 | #define FLAG_TIMEOUT ((int)0x1000) |
mbed_official | 76:aeb1df146756 | 43 | #define LONG_TIMEOUT ((int)0x8000) |
mbed_official | 76:aeb1df146756 | 44 | |
mbed_official | 76:aeb1df146756 | 45 | static const PinMap PinMap_I2C_SDA[] = { |
mbed_official | 118:b44c45162f28 | 46 | {PB_7, I2C_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_I2C1)}, |
mbed_official | 80:66393a7b209d | 47 | {PB_9, I2C_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_I2C1)}, |
mbed_official | 118:b44c45162f28 | 48 | {PB_11, I2C_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_I2C2)}, |
mbed_official | 76:aeb1df146756 | 49 | {NC, NC, 0} |
mbed_official | 76:aeb1df146756 | 50 | }; |
mbed_official | 76:aeb1df146756 | 51 | |
mbed_official | 76:aeb1df146756 | 52 | static const PinMap PinMap_I2C_SCL[] = { |
mbed_official | 118:b44c45162f28 | 53 | {PB_6, I2C_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_I2C1)}, |
mbed_official | 80:66393a7b209d | 54 | {PB_8, I2C_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_I2C1)}, |
mbed_official | 118:b44c45162f28 | 55 | {PB_10, I2C_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_I2C2)}, |
mbed_official | 76:aeb1df146756 | 56 | {NC, NC, 0} |
mbed_official | 76:aeb1df146756 | 57 | }; |
mbed_official | 76:aeb1df146756 | 58 | |
mbed_official | 306:1f0269907d8b | 59 | int i2c1_inited = 0; |
mbed_official | 306:1f0269907d8b | 60 | int i2c2_inited = 0; |
mbed_official | 306:1f0269907d8b | 61 | |
mbed_official | 174:8bb9f3a33240 | 62 | void i2c_init(i2c_t *obj, PinName sda, PinName scl) { |
mbed_official | 76:aeb1df146756 | 63 | // Determine the I2C to use |
mbed_official | 76:aeb1df146756 | 64 | I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); |
mbed_official | 76:aeb1df146756 | 65 | I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); |
mbed_official | 76:aeb1df146756 | 66 | |
mbed_official | 76:aeb1df146756 | 67 | obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); |
mbed_official | 227:7bd0639b8911 | 68 | MBED_ASSERT(obj->i2c != (I2CName)NC); |
mbed_official | 76:aeb1df146756 | 69 | |
mbed_official | 306:1f0269907d8b | 70 | // Enable I2C1 clock and pinout if not done |
mbed_official | 306:1f0269907d8b | 71 | if ((obj->i2c == I2C_1)&& !i2c1_inited) { |
mbed_official | 306:1f0269907d8b | 72 | i2c1_inited = 1; |
mbed_official | 76:aeb1df146756 | 73 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); |
mbed_official | 306:1f0269907d8b | 74 | // Configure I2C pins |
mbed_official | 306:1f0269907d8b | 75 | pinmap_pinout(scl, PinMap_I2C_SCL); |
mbed_official | 306:1f0269907d8b | 76 | pin_mode(scl, OpenDrain); |
mbed_official | 306:1f0269907d8b | 77 | pinmap_pinout(sda, PinMap_I2C_SDA); |
mbed_official | 306:1f0269907d8b | 78 | pin_mode(sda, OpenDrain); |
mbed_official | 76:aeb1df146756 | 79 | } |
mbed_official | 76:aeb1df146756 | 80 | |
mbed_official | 306:1f0269907d8b | 81 | // Enable I2C2 clock and pinout if not done |
mbed_official | 306:1f0269907d8b | 82 | if ((obj->i2c == I2C_2)&& !i2c2_inited) { |
mbed_official | 306:1f0269907d8b | 83 | i2c2_inited = 1; |
mbed_official | 306:1f0269907d8b | 84 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE); |
mbed_official | 306:1f0269907d8b | 85 | // Configure I2C pins |
mbed_official | 306:1f0269907d8b | 86 | pinmap_pinout(scl, PinMap_I2C_SCL); |
mbed_official | 306:1f0269907d8b | 87 | pin_mode(scl, OpenDrain); |
mbed_official | 306:1f0269907d8b | 88 | pinmap_pinout(sda, PinMap_I2C_SDA); |
mbed_official | 306:1f0269907d8b | 89 | pin_mode(sda, OpenDrain); |
mbed_official | 306:1f0269907d8b | 90 | } |
mbed_official | 174:8bb9f3a33240 | 91 | |
mbed_official | 76:aeb1df146756 | 92 | // Reset to clear pending flags if any |
mbed_official | 76:aeb1df146756 | 93 | i2c_reset(obj); |
mbed_official | 174:8bb9f3a33240 | 94 | |
mbed_official | 76:aeb1df146756 | 95 | // I2C configuration |
mbed_official | 174:8bb9f3a33240 | 96 | i2c_frequency(obj, 100000); // 100 kHz per default |
mbed_official | 76:aeb1df146756 | 97 | } |
mbed_official | 76:aeb1df146756 | 98 | |
mbed_official | 76:aeb1df146756 | 99 | void i2c_frequency(i2c_t *obj, int hz) { |
mbed_official | 76:aeb1df146756 | 100 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 76:aeb1df146756 | 101 | I2C_InitTypeDef I2C_InitStructure; |
mbed_official | 306:1f0269907d8b | 102 | int timeout; |
mbed_official | 129:0182c99221bc | 103 | |
mbed_official | 129:0182c99221bc | 104 | if (hz == 0) return; |
mbed_official | 129:0182c99221bc | 105 | if (hz > 400000) hz = 400000; |
mbed_official | 129:0182c99221bc | 106 | |
mbed_official | 306:1f0269907d8b | 107 | // wait before init |
mbed_official | 306:1f0269907d8b | 108 | timeout = LONG_TIMEOUT; |
mbed_official | 306:1f0269907d8b | 109 | while((I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY)) && (timeout-- != 0)); |
mbed_official | 306:1f0269907d8b | 110 | |
mbed_official | 129:0182c99221bc | 111 | /* Warning: To use the I2C at 400 kHz (in fast mode), the PCLK1 frequency |
mbed_official | 129:0182c99221bc | 112 | (I2C peripheral input clock) must be a multiple of 10 MHz. |
mbed_official | 129:0182c99221bc | 113 | With the actual clock configuration, the max frequency is measured at 296 kHz */ |
mbed_official | 174:8bb9f3a33240 | 114 | |
mbed_official | 129:0182c99221bc | 115 | // I2C configuration |
mbed_official | 129:0182c99221bc | 116 | I2C_DeInit(i2c); |
mbed_official | 129:0182c99221bc | 117 | I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; |
mbed_official | 129:0182c99221bc | 118 | I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; |
mbed_official | 129:0182c99221bc | 119 | I2C_InitStructure.I2C_OwnAddress1 = 0; |
mbed_official | 129:0182c99221bc | 120 | I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; |
mbed_official | 129:0182c99221bc | 121 | I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; |
mbed_official | 129:0182c99221bc | 122 | I2C_InitStructure.I2C_ClockSpeed = hz; |
mbed_official | 129:0182c99221bc | 123 | I2C_Init(i2c, &I2C_InitStructure); |
mbed_official | 129:0182c99221bc | 124 | I2C_Cmd(i2c, ENABLE); |
mbed_official | 76:aeb1df146756 | 125 | } |
mbed_official | 76:aeb1df146756 | 126 | |
mbed_official | 76:aeb1df146756 | 127 | inline int i2c_start(i2c_t *obj) { |
mbed_official | 76:aeb1df146756 | 128 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 76:aeb1df146756 | 129 | int timeout; |
mbed_official | 174:8bb9f3a33240 | 130 | |
mbed_official | 76:aeb1df146756 | 131 | I2C_ClearFlag(i2c, I2C_FLAG_AF); // Clear Acknowledge failure flag |
mbed_official | 174:8bb9f3a33240 | 132 | |
mbed_official | 76:aeb1df146756 | 133 | // Generate the START condition |
mbed_official | 174:8bb9f3a33240 | 134 | I2C_GenerateSTART(i2c, ENABLE); |
mbed_official | 174:8bb9f3a33240 | 135 | |
mbed_official | 76:aeb1df146756 | 136 | // Wait the START condition has been correctly sent |
mbed_official | 76:aeb1df146756 | 137 | timeout = FLAG_TIMEOUT; |
mbed_official | 76:aeb1df146756 | 138 | while (I2C_GetFlagStatus(i2c, I2C_FLAG_SB) == RESET) { |
mbed_official | 80:66393a7b209d | 139 | timeout--; |
mbed_official | 80:66393a7b209d | 140 | if (timeout == 0) { |
mbed_official | 80:66393a7b209d | 141 | return 1; |
mbed_official | 80:66393a7b209d | 142 | } |
mbed_official | 76:aeb1df146756 | 143 | } |
mbed_official | 174:8bb9f3a33240 | 144 | |
mbed_official | 76:aeb1df146756 | 145 | return 0; |
mbed_official | 76:aeb1df146756 | 146 | } |
mbed_official | 76:aeb1df146756 | 147 | |
mbed_official | 76:aeb1df146756 | 148 | inline int i2c_stop(i2c_t *obj) { |
mbed_official | 76:aeb1df146756 | 149 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 129:0182c99221bc | 150 | int timeout; |
mbed_official | 129:0182c99221bc | 151 | volatile int temp; |
mbed_official | 174:8bb9f3a33240 | 152 | |
mbed_official | 129:0182c99221bc | 153 | if (I2C_GetFlagStatus(i2c, I2C_FLAG_MSL) == RESET) { |
mbed_official | 129:0182c99221bc | 154 | timeout = LONG_TIMEOUT; |
mbed_official | 174:8bb9f3a33240 | 155 | // wait for STOP |
mbed_official | 129:0182c99221bc | 156 | while (I2C_GetFlagStatus(i2c, I2C_FLAG_STOPF) == RESET) { |
mbed_official | 129:0182c99221bc | 157 | timeout--; |
mbed_official | 129:0182c99221bc | 158 | if (timeout == 0) { |
mbed_official | 129:0182c99221bc | 159 | return 0; |
mbed_official | 129:0182c99221bc | 160 | } |
mbed_official | 129:0182c99221bc | 161 | } |
mbed_official | 129:0182c99221bc | 162 | temp = i2c->SR1; |
mbed_official | 129:0182c99221bc | 163 | I2C_Cmd(i2c, ENABLE); |
mbed_official | 174:8bb9f3a33240 | 164 | } else { |
mbed_official | 129:0182c99221bc | 165 | I2C_GenerateSTOP(i2c, ENABLE); |
mbed_official | 129:0182c99221bc | 166 | } |
mbed_official | 174:8bb9f3a33240 | 167 | |
mbed_official | 76:aeb1df146756 | 168 | return 0; |
mbed_official | 76:aeb1df146756 | 169 | } |
mbed_official | 76:aeb1df146756 | 170 | |
mbed_official | 76:aeb1df146756 | 171 | int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { |
mbed_official | 76:aeb1df146756 | 172 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 76:aeb1df146756 | 173 | int timeout; |
mbed_official | 76:aeb1df146756 | 174 | int count; |
mbed_official | 76:aeb1df146756 | 175 | int value; |
mbed_official | 174:8bb9f3a33240 | 176 | |
mbed_official | 76:aeb1df146756 | 177 | i2c_start(obj); |
mbed_official | 76:aeb1df146756 | 178 | |
mbed_official | 76:aeb1df146756 | 179 | // Send slave address for read |
mbed_official | 174:8bb9f3a33240 | 180 | I2C_Send7bitAddress(i2c, address, I2C_Direction_Receiver); |
mbed_official | 76:aeb1df146756 | 181 | |
mbed_official | 76:aeb1df146756 | 182 | // Wait address is acknowledged |
mbed_official | 76:aeb1df146756 | 183 | timeout = FLAG_TIMEOUT; |
mbed_official | 76:aeb1df146756 | 184 | while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) == ERROR) { |
mbed_official | 80:66393a7b209d | 185 | timeout--; |
mbed_official | 80:66393a7b209d | 186 | if (timeout == 0) { |
mbed_official | 247:135e3186a638 | 187 | return -1; |
mbed_official | 80:66393a7b209d | 188 | } |
mbed_official | 76:aeb1df146756 | 189 | } |
mbed_official | 174:8bb9f3a33240 | 190 | |
mbed_official | 76:aeb1df146756 | 191 | // Read all bytes except last one |
mbed_official | 76:aeb1df146756 | 192 | for (count = 0; count < (length - 1); count++) { |
mbed_official | 76:aeb1df146756 | 193 | value = i2c_byte_read(obj, 0); |
mbed_official | 76:aeb1df146756 | 194 | data[count] = (char)value; |
mbed_official | 76:aeb1df146756 | 195 | } |
mbed_official | 174:8bb9f3a33240 | 196 | |
mbed_official | 76:aeb1df146756 | 197 | // If not repeated start, send stop. |
mbed_official | 76:aeb1df146756 | 198 | // Warning: must be done BEFORE the data is read. |
mbed_official | 76:aeb1df146756 | 199 | if (stop) { |
mbed_official | 76:aeb1df146756 | 200 | i2c_stop(obj); |
mbed_official | 76:aeb1df146756 | 201 | } |
mbed_official | 76:aeb1df146756 | 202 | |
mbed_official | 76:aeb1df146756 | 203 | // Read the last byte |
mbed_official | 76:aeb1df146756 | 204 | value = i2c_byte_read(obj, 1); |
mbed_official | 76:aeb1df146756 | 205 | data[count] = (char)value; |
mbed_official | 174:8bb9f3a33240 | 206 | |
mbed_official | 76:aeb1df146756 | 207 | return length; |
mbed_official | 76:aeb1df146756 | 208 | } |
mbed_official | 76:aeb1df146756 | 209 | |
mbed_official | 76:aeb1df146756 | 210 | int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { |
mbed_official | 76:aeb1df146756 | 211 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 76:aeb1df146756 | 212 | int timeout; |
mbed_official | 76:aeb1df146756 | 213 | int count; |
mbed_official | 76:aeb1df146756 | 214 | |
mbed_official | 76:aeb1df146756 | 215 | i2c_start(obj); |
mbed_official | 76:aeb1df146756 | 216 | |
mbed_official | 76:aeb1df146756 | 217 | // Send slave address for write |
mbed_official | 76:aeb1df146756 | 218 | I2C_Send7bitAddress(i2c, address, I2C_Direction_Transmitter); |
mbed_official | 174:8bb9f3a33240 | 219 | |
mbed_official | 76:aeb1df146756 | 220 | // Wait address is acknowledged |
mbed_official | 76:aeb1df146756 | 221 | timeout = FLAG_TIMEOUT; |
mbed_official | 76:aeb1df146756 | 222 | while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) == ERROR) { |
mbed_official | 80:66393a7b209d | 223 | timeout--; |
mbed_official | 80:66393a7b209d | 224 | if (timeout == 0) { |
mbed_official | 247:135e3186a638 | 225 | return -1; |
mbed_official | 80:66393a7b209d | 226 | } |
mbed_official | 76:aeb1df146756 | 227 | } |
mbed_official | 76:aeb1df146756 | 228 | |
mbed_official | 76:aeb1df146756 | 229 | for (count = 0; count < length; count++) { |
mbed_official | 76:aeb1df146756 | 230 | if (i2c_byte_write(obj, data[count]) != 1) { |
mbed_official | 76:aeb1df146756 | 231 | i2c_stop(obj); |
mbed_official | 247:135e3186a638 | 232 | return -1; |
mbed_official | 76:aeb1df146756 | 233 | } |
mbed_official | 76:aeb1df146756 | 234 | } |
mbed_official | 76:aeb1df146756 | 235 | |
mbed_official | 76:aeb1df146756 | 236 | // If not repeated start, send stop. |
mbed_official | 76:aeb1df146756 | 237 | if (stop) { |
mbed_official | 76:aeb1df146756 | 238 | i2c_stop(obj); |
mbed_official | 76:aeb1df146756 | 239 | } |
mbed_official | 76:aeb1df146756 | 240 | |
mbed_official | 76:aeb1df146756 | 241 | return count; |
mbed_official | 76:aeb1df146756 | 242 | } |
mbed_official | 76:aeb1df146756 | 243 | |
mbed_official | 76:aeb1df146756 | 244 | int i2c_byte_read(i2c_t *obj, int last) { |
mbed_official | 76:aeb1df146756 | 245 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 76:aeb1df146756 | 246 | uint8_t data; |
mbed_official | 76:aeb1df146756 | 247 | int timeout; |
mbed_official | 174:8bb9f3a33240 | 248 | |
mbed_official | 76:aeb1df146756 | 249 | if (last) { |
mbed_official | 76:aeb1df146756 | 250 | // Don't acknowledge the last byte |
mbed_official | 76:aeb1df146756 | 251 | I2C_AcknowledgeConfig(i2c, DISABLE); |
mbed_official | 76:aeb1df146756 | 252 | } else { |
mbed_official | 76:aeb1df146756 | 253 | // Acknowledge the byte |
mbed_official | 76:aeb1df146756 | 254 | I2C_AcknowledgeConfig(i2c, ENABLE); |
mbed_official | 76:aeb1df146756 | 255 | } |
mbed_official | 76:aeb1df146756 | 256 | |
mbed_official | 76:aeb1df146756 | 257 | // Wait until the byte is received |
mbed_official | 76:aeb1df146756 | 258 | timeout = FLAG_TIMEOUT; |
mbed_official | 76:aeb1df146756 | 259 | while (I2C_GetFlagStatus(i2c, I2C_FLAG_RXNE) == RESET) { |
mbed_official | 80:66393a7b209d | 260 | timeout--; |
mbed_official | 80:66393a7b209d | 261 | if (timeout == 0) { |
mbed_official | 247:135e3186a638 | 262 | return -1; |
mbed_official | 80:66393a7b209d | 263 | } |
mbed_official | 76:aeb1df146756 | 264 | } |
mbed_official | 76:aeb1df146756 | 265 | |
mbed_official | 76:aeb1df146756 | 266 | data = I2C_ReceiveData(i2c); |
mbed_official | 174:8bb9f3a33240 | 267 | |
mbed_official | 76:aeb1df146756 | 268 | return (int)data; |
mbed_official | 76:aeb1df146756 | 269 | } |
mbed_official | 76:aeb1df146756 | 270 | |
mbed_official | 76:aeb1df146756 | 271 | int i2c_byte_write(i2c_t *obj, int data) { |
mbed_official | 76:aeb1df146756 | 272 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 76:aeb1df146756 | 273 | int timeout; |
mbed_official | 76:aeb1df146756 | 274 | |
mbed_official | 76:aeb1df146756 | 275 | I2C_SendData(i2c, (uint8_t)data); |
mbed_official | 76:aeb1df146756 | 276 | |
mbed_official | 76:aeb1df146756 | 277 | // Wait until the byte is transmitted |
mbed_official | 129:0182c99221bc | 278 | timeout = FLAG_TIMEOUT; |
mbed_official | 76:aeb1df146756 | 279 | while ((I2C_GetFlagStatus(i2c, I2C_FLAG_TXE) == RESET) && |
mbed_official | 174:8bb9f3a33240 | 280 | (I2C_GetFlagStatus(i2c, I2C_FLAG_BTF) == RESET)) { |
mbed_official | 80:66393a7b209d | 281 | timeout--; |
mbed_official | 80:66393a7b209d | 282 | if (timeout == 0) { |
mbed_official | 76:aeb1df146756 | 283 | return 0; |
mbed_official | 76:aeb1df146756 | 284 | } |
mbed_official | 76:aeb1df146756 | 285 | } |
mbed_official | 174:8bb9f3a33240 | 286 | |
mbed_official | 76:aeb1df146756 | 287 | return 1; |
mbed_official | 76:aeb1df146756 | 288 | } |
mbed_official | 76:aeb1df146756 | 289 | |
mbed_official | 76:aeb1df146756 | 290 | void i2c_reset(i2c_t *obj) { |
mbed_official | 306:1f0269907d8b | 291 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 306:1f0269907d8b | 292 | int timeout; |
mbed_official | 306:1f0269907d8b | 293 | |
mbed_official | 306:1f0269907d8b | 294 | // wait before reset |
mbed_official | 306:1f0269907d8b | 295 | timeout = LONG_TIMEOUT; |
mbed_official | 306:1f0269907d8b | 296 | while((I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY)) && (timeout-- != 0)); |
mbed_official | 306:1f0269907d8b | 297 | |
mbed_official | 174:8bb9f3a33240 | 298 | if (obj->i2c == I2C_1) { |
mbed_official | 76:aeb1df146756 | 299 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE); |
mbed_official | 76:aeb1df146756 | 300 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE); |
mbed_official | 76:aeb1df146756 | 301 | } |
mbed_official | 76:aeb1df146756 | 302 | if (obj->i2c == I2C_2) { |
mbed_official | 76:aeb1df146756 | 303 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE); |
mbed_official | 174:8bb9f3a33240 | 304 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE); |
mbed_official | 76:aeb1df146756 | 305 | } |
mbed_official | 76:aeb1df146756 | 306 | } |
mbed_official | 76:aeb1df146756 | 307 | |
mbed_official | 76:aeb1df146756 | 308 | #if DEVICE_I2CSLAVE |
mbed_official | 76:aeb1df146756 | 309 | |
mbed_official | 76:aeb1df146756 | 310 | void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) { |
mbed_official | 76:aeb1df146756 | 311 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 76:aeb1df146756 | 312 | uint16_t tmpreg; |
mbed_official | 174:8bb9f3a33240 | 313 | |
mbed_official | 76:aeb1df146756 | 314 | // Get the old register value |
mbed_official | 76:aeb1df146756 | 315 | tmpreg = i2c->OAR1; |
mbed_official | 76:aeb1df146756 | 316 | // Reset address bits |
mbed_official | 76:aeb1df146756 | 317 | tmpreg &= 0xFC00; |
mbed_official | 76:aeb1df146756 | 318 | // Set new address |
mbed_official | 76:aeb1df146756 | 319 | tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits |
mbed_official | 76:aeb1df146756 | 320 | // Store the new register value |
mbed_official | 76:aeb1df146756 | 321 | i2c->OAR1 = tmpreg; |
mbed_official | 76:aeb1df146756 | 322 | } |
mbed_official | 76:aeb1df146756 | 323 | |
mbed_official | 76:aeb1df146756 | 324 | void i2c_slave_mode(i2c_t *obj, int enable_slave) { |
mbed_official | 76:aeb1df146756 | 325 | // Nothing to do |
mbed_official | 76:aeb1df146756 | 326 | } |
mbed_official | 76:aeb1df146756 | 327 | |
mbed_official | 76:aeb1df146756 | 328 | // See I2CSlave.h |
mbed_official | 76:aeb1df146756 | 329 | #define NoData 0 // the slave has not been addressed |
mbed_official | 76:aeb1df146756 | 330 | #define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter) |
mbed_official | 76:aeb1df146756 | 331 | #define WriteGeneral 2 // the master is writing to all slave |
mbed_official | 76:aeb1df146756 | 332 | #define WriteAddressed 3 // the master is writing to this slave (slave = receiver) |
mbed_official | 76:aeb1df146756 | 333 | |
mbed_official | 76:aeb1df146756 | 334 | int i2c_slave_receive(i2c_t *obj) { |
mbed_official | 190:bde2479eef9e | 335 | int retValue = NoData; |
mbed_official | 190:bde2479eef9e | 336 | uint32_t event; |
mbed_official | 190:bde2479eef9e | 337 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 190:bde2479eef9e | 338 | |
mbed_official | 215:83cf97a28428 | 339 | event = I2C_GetLastEvent(i2c); |
mbed_official | 215:83cf97a28428 | 340 | if (event != 0) { |
mbed_official | 215:83cf97a28428 | 341 | switch (event) { |
mbed_official | 190:bde2479eef9e | 342 | case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED: |
mbed_official | 190:bde2479eef9e | 343 | retValue = WriteAddressed; |
mbed_official | 190:bde2479eef9e | 344 | break; |
mbed_official | 190:bde2479eef9e | 345 | case I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED: |
mbed_official | 190:bde2479eef9e | 346 | retValue = ReadAddressed; |
mbed_official | 190:bde2479eef9e | 347 | break; |
mbed_official | 190:bde2479eef9e | 348 | case I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED: |
mbed_official | 190:bde2479eef9e | 349 | retValue = WriteGeneral; |
mbed_official | 190:bde2479eef9e | 350 | break; |
mbed_official | 190:bde2479eef9e | 351 | default: |
mbed_official | 190:bde2479eef9e | 352 | retValue = NoData; |
mbed_official | 190:bde2479eef9e | 353 | break; |
mbed_official | 190:bde2479eef9e | 354 | } |
mbed_official | 190:bde2479eef9e | 355 | |
mbed_official | 215:83cf97a28428 | 356 | // clear ADDR |
mbed_official | 215:83cf97a28428 | 357 | if ((retValue == WriteAddressed) || (retValue == ReadAddressed)) { |
mbed_official | 190:bde2479eef9e | 358 | i2c->SR1;// read status register 1 |
mbed_official | 190:bde2479eef9e | 359 | i2c->SR2;// read status register 2 |
mbed_official | 190:bde2479eef9e | 360 | } |
mbed_official | 190:bde2479eef9e | 361 | // clear stopf |
mbed_official | 215:83cf97a28428 | 362 | if (I2C_GetFlagStatus(i2c, I2C_FLAG_STOPF) == SET) { |
mbed_official | 190:bde2479eef9e | 363 | i2c->SR1;// read status register 1 |
mbed_official | 215:83cf97a28428 | 364 | I2C_Cmd(i2c, ENABLE); |
mbed_official | 190:bde2479eef9e | 365 | } |
mbed_official | 190:bde2479eef9e | 366 | // clear AF |
mbed_official | 215:83cf97a28428 | 367 | if (I2C_GetFlagStatus(i2c, I2C_FLAG_AF) == SET) { |
mbed_official | 190:bde2479eef9e | 368 | I2C_ClearFlag(i2c, I2C_FLAG_AF); |
mbed_official | 215:83cf97a28428 | 369 | } |
mbed_official | 190:bde2479eef9e | 370 | } |
mbed_official | 215:83cf97a28428 | 371 | return (retValue); |
mbed_official | 76:aeb1df146756 | 372 | } |
mbed_official | 76:aeb1df146756 | 373 | |
mbed_official | 76:aeb1df146756 | 374 | int i2c_slave_read(i2c_t *obj, char *data, int length) { |
mbed_official | 76:aeb1df146756 | 375 | int count = 0; |
mbed_official | 174:8bb9f3a33240 | 376 | |
mbed_official | 76:aeb1df146756 | 377 | // Read all bytes |
mbed_official | 76:aeb1df146756 | 378 | for (count = 0; count < length; count++) { |
mbed_official | 76:aeb1df146756 | 379 | data[count] = i2c_byte_read(obj, 0); |
mbed_official | 76:aeb1df146756 | 380 | } |
mbed_official | 174:8bb9f3a33240 | 381 | |
mbed_official | 76:aeb1df146756 | 382 | return count; |
mbed_official | 76:aeb1df146756 | 383 | } |
mbed_official | 76:aeb1df146756 | 384 | |
mbed_official | 76:aeb1df146756 | 385 | int i2c_slave_write(i2c_t *obj, const char *data, int length) { |
mbed_official | 76:aeb1df146756 | 386 | int count = 0; |
mbed_official | 174:8bb9f3a33240 | 387 | |
mbed_official | 76:aeb1df146756 | 388 | // Write all bytes |
mbed_official | 76:aeb1df146756 | 389 | for (count = 0; count < length; count++) { |
mbed_official | 76:aeb1df146756 | 390 | i2c_byte_write(obj, data[count]); |
mbed_official | 76:aeb1df146756 | 391 | } |
mbed_official | 174:8bb9f3a33240 | 392 | |
mbed_official | 76:aeb1df146756 | 393 | return count; |
mbed_official | 76:aeb1df146756 | 394 | } |
mbed_official | 76:aeb1df146756 | 395 | |
mbed_official | 76:aeb1df146756 | 396 | |
mbed_official | 76:aeb1df146756 | 397 | #endif // DEVICE_I2CSLAVE |
mbed_official | 76:aeb1df146756 | 398 | |
mbed_official | 76:aeb1df146756 | 399 | #endif // DEVICE_I2C |