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