mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/i2c_api.c@227:7bd0639b8911, 2014-06-11 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Jun 11 16:00:09 2014 +0100
- Revision:
- 227:7bd0639b8911
- Parent:
- 216:577900467c9e
- Child:
- 247:135e3186a638
Synchronized with git revision d58d532ebc0e0a96f4fffb8edefc082b71b964af
Full URL: https://github.com/mbedmicro/mbed/commit/d58d532ebc0e0a96f4fffb8edefc082b71b964af/
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 | 216:577900467c9e | 58 | void i2c_init(i2c_t *obj, PinName sda, PinName scl) { |
mbed_official | 80:66393a7b209d | 59 | // Determine the I2C to use |
mbed_official | 80:66393a7b209d | 60 | I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); |
mbed_official | 80:66393a7b209d | 61 | I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); |
mbed_official | 80:66393a7b209d | 62 | |
mbed_official | 80:66393a7b209d | 63 | obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); |
mbed_official | 227:7bd0639b8911 | 64 | MBED_ASSERT(obj->i2c != (I2CName)NC); |
mbed_official | 80:66393a7b209d | 65 | |
mbed_official | 80:66393a7b209d | 66 | // Enable I2C clock |
mbed_official | 216:577900467c9e | 67 | if (obj->i2c == I2C_1) { |
mbed_official | 80:66393a7b209d | 68 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); |
mbed_official | 176:2d0c9ad7ef62 | 69 | RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK); |
mbed_official | 80:66393a7b209d | 70 | } |
mbed_official | 171:3d240fda1f07 | 71 | if (obj->i2c == I2C_2) { |
mbed_official | 171:3d240fda1f07 | 72 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE); |
mbed_official | 171:3d240fda1f07 | 73 | } |
mbed_official | 80:66393a7b209d | 74 | |
mbed_official | 80:66393a7b209d | 75 | // Configure I2C pins |
mbed_official | 84:f54042cbc282 | 76 | pinmap_pinout(scl, PinMap_I2C_SCL); |
mbed_official | 84:f54042cbc282 | 77 | pin_mode(scl, OpenDrain); |
mbed_official | 80:66393a7b209d | 78 | pinmap_pinout(sda, PinMap_I2C_SDA); |
mbed_official | 80:66393a7b209d | 79 | pin_mode(sda, OpenDrain); |
mbed_official | 216:577900467c9e | 80 | |
mbed_official | 80:66393a7b209d | 81 | // Reset to clear pending flags if any |
mbed_official | 80:66393a7b209d | 82 | i2c_reset(obj); |
mbed_official | 216:577900467c9e | 83 | |
mbed_official | 80:66393a7b209d | 84 | // I2C configuration |
mbed_official | 216:577900467c9e | 85 | i2c_frequency(obj, 100000); // 100 kHz per default |
mbed_official | 80:66393a7b209d | 86 | } |
mbed_official | 80:66393a7b209d | 87 | |
mbed_official | 80:66393a7b209d | 88 | void i2c_frequency(i2c_t *obj, int hz) { |
mbed_official | 227:7bd0639b8911 | 89 | MBED_ASSERT((hz == 100000) || (hz == 200000) || (hz == 400000) || (hz == 1000000)); |
mbed_official | 80:66393a7b209d | 90 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 80:66393a7b209d | 91 | I2C_InitTypeDef I2C_InitStructure; |
mbed_official | 166:cb4253f91ada | 92 | uint32_t tim = 0; |
mbed_official | 166:cb4253f91ada | 93 | |
mbed_official | 166:cb4253f91ada | 94 | // Disable the Fast Mode Plus capability |
mbed_official | 166:cb4253f91ada | 95 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); // Enable SYSCFG clock |
mbed_official | 166:cb4253f91ada | 96 | SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C1, DISABLE); |
mbed_official | 166:cb4253f91ada | 97 | SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C2, DISABLE); |
mbed_official | 216:577900467c9e | 98 | |
mbed_official | 166:cb4253f91ada | 99 | /* |
mbed_official | 166:cb4253f91ada | 100 | Values calculated with I2C_Timing_Configuration_V1.0.1.xls file (see AN4235) |
mbed_official | 166:cb4253f91ada | 101 | * Standard mode (up to 100 kHz) |
mbed_official | 166:cb4253f91ada | 102 | * Fast Mode (up to 400 kHz) |
mbed_official | 166:cb4253f91ada | 103 | * Fast Mode Plus (up to 1 MHz) |
mbed_official | 166:cb4253f91ada | 104 | Below values obtained with: |
mbed_official | 176:2d0c9ad7ef62 | 105 | - I2C clock source = 48 MHz (System Clock) |
mbed_official | 166:cb4253f91ada | 106 | - Analog filter delay = ON |
mbed_official | 166:cb4253f91ada | 107 | - Digital filter coefficient = 0 |
mbed_official | 166:cb4253f91ada | 108 | - Rise time = 100 ns |
mbed_official | 166:cb4253f91ada | 109 | - Fall time = 10ns |
mbed_official | 166:cb4253f91ada | 110 | */ |
mbed_official | 80:66393a7b209d | 111 | switch (hz) { |
mbed_official | 227:7bd0639b8911 | 112 | case 100000: |
mbed_official | 227:7bd0639b8911 | 113 | tim = 0x10805E89; // Standard mode |
mbed_official | 227:7bd0639b8911 | 114 | break; |
mbed_official | 227:7bd0639b8911 | 115 | case 200000: |
mbed_official | 227:7bd0639b8911 | 116 | tim = 0x00905E82; // Fast Mode |
mbed_official | 227:7bd0639b8911 | 117 | break; |
mbed_official | 227:7bd0639b8911 | 118 | case 400000: |
mbed_official | 227:7bd0639b8911 | 119 | tim = 0x00901850; // Fast Mode |
mbed_official | 227:7bd0639b8911 | 120 | break; |
mbed_official | 227:7bd0639b8911 | 121 | case 1000000: |
mbed_official | 227:7bd0639b8911 | 122 | tim = 0x00700818; // Fast Mode Plus |
mbed_official | 227:7bd0639b8911 | 123 | // Enable the Fast Mode Plus capability |
mbed_official | 227:7bd0639b8911 | 124 | if (obj->i2c == I2C_1) { |
mbed_official | 227:7bd0639b8911 | 125 | SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C1, ENABLE); |
mbed_official | 227:7bd0639b8911 | 126 | } |
mbed_official | 227:7bd0639b8911 | 127 | if (obj->i2c == I2C_2) { |
mbed_official | 227:7bd0639b8911 | 128 | SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C2, ENABLE); |
mbed_official | 227:7bd0639b8911 | 129 | } |
mbed_official | 227:7bd0639b8911 | 130 | break; |
mbed_official | 227:7bd0639b8911 | 131 | default: |
mbed_official | 227:7bd0639b8911 | 132 | break; |
mbed_official | 80:66393a7b209d | 133 | } |
mbed_official | 216:577900467c9e | 134 | |
mbed_official | 80:66393a7b209d | 135 | // I2C configuration |
mbed_official | 166:cb4253f91ada | 136 | I2C_DeInit(i2c); |
mbed_official | 80:66393a7b209d | 137 | I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; |
mbed_official | 80:66393a7b209d | 138 | I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable; |
mbed_official | 80:66393a7b209d | 139 | I2C_InitStructure.I2C_DigitalFilter = 0x00; |
mbed_official | 80:66393a7b209d | 140 | I2C_InitStructure.I2C_OwnAddress1 = 0x00; |
mbed_official | 80:66393a7b209d | 141 | I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; |
mbed_official | 80:66393a7b209d | 142 | I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; |
mbed_official | 166:cb4253f91ada | 143 | I2C_InitStructure.I2C_Timing = tim; |
mbed_official | 80:66393a7b209d | 144 | I2C_Init(i2c, &I2C_InitStructure); |
mbed_official | 216:577900467c9e | 145 | |
mbed_official | 80:66393a7b209d | 146 | I2C_Cmd(i2c, ENABLE); |
mbed_official | 80:66393a7b209d | 147 | } |
mbed_official | 80:66393a7b209d | 148 | |
mbed_official | 80:66393a7b209d | 149 | inline int i2c_start(i2c_t *obj) { |
mbed_official | 80:66393a7b209d | 150 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 84:f54042cbc282 | 151 | int timeout; |
mbed_official | 80:66393a7b209d | 152 | |
mbed_official | 80:66393a7b209d | 153 | // Test BUSY Flag |
mbed_official | 84:f54042cbc282 | 154 | timeout = LONG_TIMEOUT; |
mbed_official | 84:f54042cbc282 | 155 | while (I2C_GetFlagStatus(i2c, I2C_ISR_BUSY) != RESET) { |
mbed_official | 84:f54042cbc282 | 156 | timeout--; |
mbed_official | 84:f54042cbc282 | 157 | if (timeout == 0) { |
mbed_official | 84:f54042cbc282 | 158 | return 0; |
mbed_official | 84:f54042cbc282 | 159 | } |
mbed_official | 80:66393a7b209d | 160 | } |
mbed_official | 80:66393a7b209d | 161 | |
mbed_official | 80:66393a7b209d | 162 | I2C_GenerateSTART(i2c, ENABLE); |
mbed_official | 80:66393a7b209d | 163 | |
mbed_official | 80:66393a7b209d | 164 | return 0; |
mbed_official | 80:66393a7b209d | 165 | } |
mbed_official | 80:66393a7b209d | 166 | |
mbed_official | 80:66393a7b209d | 167 | inline int i2c_stop(i2c_t *obj) { |
mbed_official | 80:66393a7b209d | 168 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 216:577900467c9e | 169 | |
mbed_official | 80:66393a7b209d | 170 | I2C_GenerateSTOP(i2c, ENABLE); |
mbed_official | 216:577900467c9e | 171 | |
mbed_official | 80:66393a7b209d | 172 | return 0; |
mbed_official | 80:66393a7b209d | 173 | } |
mbed_official | 80:66393a7b209d | 174 | |
mbed_official | 80:66393a7b209d | 175 | int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { |
mbed_official | 80:66393a7b209d | 176 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 80:66393a7b209d | 177 | int count; |
mbed_official | 197:36a724675865 | 178 | int timeout; |
mbed_official | 80:66393a7b209d | 179 | int value; |
mbed_official | 216:577900467c9e | 180 | |
mbed_official | 80:66393a7b209d | 181 | if (length == 0) return 0; |
mbed_official | 80:66393a7b209d | 182 | |
mbed_official | 80:66393a7b209d | 183 | // Configure slave address, nbytes, reload, end mode and start or stop generation |
mbed_official | 197:36a724675865 | 184 | I2C_TransferHandling(i2c, address, length, I2C_SoftEnd_Mode, I2C_Generate_Start_Read); |
mbed_official | 216:577900467c9e | 185 | |
mbed_official | 80:66393a7b209d | 186 | // Read all bytes |
mbed_official | 80:66393a7b209d | 187 | for (count = 0; count < length; count++) { |
mbed_official | 80:66393a7b209d | 188 | value = i2c_byte_read(obj, 0); |
mbed_official | 80:66393a7b209d | 189 | data[count] = (char)value; |
mbed_official | 80:66393a7b209d | 190 | } |
mbed_official | 216:577900467c9e | 191 | |
mbed_official | 197:36a724675865 | 192 | timeout = FLAG_TIMEOUT; |
mbed_official | 216:577900467c9e | 193 | while (!I2C_GetFlagStatus(i2c, I2C_FLAG_TC)) { |
mbed_official | 197:36a724675865 | 194 | timeout--; |
mbed_official | 197:36a724675865 | 195 | if (timeout == 0) return 0; |
mbed_official | 197:36a724675865 | 196 | } |
mbed_official | 216:577900467c9e | 197 | |
mbed_official | 216:577900467c9e | 198 | if (stop) i2c_stop(obj); |
mbed_official | 197:36a724675865 | 199 | |
mbed_official | 80:66393a7b209d | 200 | return length; |
mbed_official | 80:66393a7b209d | 201 | } |
mbed_official | 80:66393a7b209d | 202 | |
mbed_official | 80:66393a7b209d | 203 | int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { |
mbed_official | 80:66393a7b209d | 204 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 197:36a724675865 | 205 | int timeout; |
mbed_official | 80:66393a7b209d | 206 | int count; |
mbed_official | 216:577900467c9e | 207 | |
mbed_official | 84:f54042cbc282 | 208 | if (length == 0) return 0; |
mbed_official | 80:66393a7b209d | 209 | |
mbed_official | 197:36a724675865 | 210 | // Configure slave address, nbytes, reload, end mode and start generation |
mbed_official | 216:577900467c9e | 211 | I2C_TransferHandling(i2c, address, length, I2C_SoftEnd_Mode, I2C_Generate_Start_Write); |
mbed_official | 216:577900467c9e | 212 | |
mbed_official | 80:66393a7b209d | 213 | // Write all bytes |
mbed_official | 80:66393a7b209d | 214 | for (count = 0; count < length; count++) { |
mbed_official | 197:36a724675865 | 215 | i2c_byte_write(obj, data[count]); |
mbed_official | 216:577900467c9e | 216 | } |
mbed_official | 216:577900467c9e | 217 | |
mbed_official | 197:36a724675865 | 218 | timeout = FLAG_TIMEOUT; |
mbed_official | 216:577900467c9e | 219 | while (!I2C_GetFlagStatus(i2c, I2C_FLAG_TC)) { |
mbed_official | 197:36a724675865 | 220 | timeout--; |
mbed_official | 197:36a724675865 | 221 | if (timeout == 0) return 0; |
mbed_official | 80:66393a7b209d | 222 | } |
mbed_official | 80:66393a7b209d | 223 | |
mbed_official | 216:577900467c9e | 224 | if (stop) i2c_stop(obj); |
mbed_official | 216:577900467c9e | 225 | |
mbed_official | 80:66393a7b209d | 226 | return count; |
mbed_official | 80:66393a7b209d | 227 | } |
mbed_official | 80:66393a7b209d | 228 | |
mbed_official | 80:66393a7b209d | 229 | int i2c_byte_read(i2c_t *obj, int last) { |
mbed_official | 80:66393a7b209d | 230 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 80:66393a7b209d | 231 | uint8_t data; |
mbed_official | 84:f54042cbc282 | 232 | int timeout; |
mbed_official | 216:577900467c9e | 233 | |
mbed_official | 80:66393a7b209d | 234 | // Wait until the byte is received |
mbed_official | 216:577900467c9e | 235 | timeout = FLAG_TIMEOUT; |
mbed_official | 80:66393a7b209d | 236 | while (I2C_GetFlagStatus(i2c, I2C_ISR_RXNE) == RESET) { |
mbed_official | 84:f54042cbc282 | 237 | timeout--; |
mbed_official | 84:f54042cbc282 | 238 | if (timeout == 0) { |
mbed_official | 84:f54042cbc282 | 239 | return 0; |
mbed_official | 84:f54042cbc282 | 240 | } |
mbed_official | 80:66393a7b209d | 241 | } |
mbed_official | 80:66393a7b209d | 242 | |
mbed_official | 80:66393a7b209d | 243 | data = I2C_ReceiveData(i2c); |
mbed_official | 216:577900467c9e | 244 | |
mbed_official | 80:66393a7b209d | 245 | return (int)data; |
mbed_official | 80:66393a7b209d | 246 | } |
mbed_official | 80:66393a7b209d | 247 | |
mbed_official | 80:66393a7b209d | 248 | int i2c_byte_write(i2c_t *obj, int data) { |
mbed_official | 80:66393a7b209d | 249 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 84:f54042cbc282 | 250 | int timeout; |
mbed_official | 80:66393a7b209d | 251 | |
mbed_official | 84:f54042cbc282 | 252 | // Wait until the previous byte is transmitted |
mbed_official | 84:f54042cbc282 | 253 | timeout = FLAG_TIMEOUT; |
mbed_official | 84:f54042cbc282 | 254 | while (I2C_GetFlagStatus(i2c, I2C_ISR_TXIS) == RESET) { |
mbed_official | 84:f54042cbc282 | 255 | timeout--; |
mbed_official | 84:f54042cbc282 | 256 | if (timeout == 0) { |
mbed_official | 84:f54042cbc282 | 257 | return 0; |
mbed_official | 84:f54042cbc282 | 258 | } |
mbed_official | 80:66393a7b209d | 259 | } |
mbed_official | 216:577900467c9e | 260 | |
mbed_official | 80:66393a7b209d | 261 | I2C_SendData(i2c, (uint8_t)data); |
mbed_official | 216:577900467c9e | 262 | |
mbed_official | 80:66393a7b209d | 263 | return 1; |
mbed_official | 80:66393a7b209d | 264 | } |
mbed_official | 80:66393a7b209d | 265 | |
mbed_official | 80:66393a7b209d | 266 | void i2c_reset(i2c_t *obj) { |
mbed_official | 216:577900467c9e | 267 | if (obj->i2c == I2C_1) { |
mbed_official | 80:66393a7b209d | 268 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE); |
mbed_official | 80:66393a7b209d | 269 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE); |
mbed_official | 80:66393a7b209d | 270 | } |
mbed_official | 171:3d240fda1f07 | 271 | if (obj->i2c == I2C_2) { |
mbed_official | 171:3d240fda1f07 | 272 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE); |
mbed_official | 216:577900467c9e | 273 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE); |
mbed_official | 171:3d240fda1f07 | 274 | } |
mbed_official | 80:66393a7b209d | 275 | } |
mbed_official | 80:66393a7b209d | 276 | |
mbed_official | 80:66393a7b209d | 277 | #if DEVICE_I2CSLAVE |
mbed_official | 80:66393a7b209d | 278 | |
mbed_official | 80:66393a7b209d | 279 | void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) { |
mbed_official | 80:66393a7b209d | 280 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 80:66393a7b209d | 281 | uint16_t tmpreg; |
mbed_official | 216:577900467c9e | 282 | |
mbed_official | 171:3d240fda1f07 | 283 | // reset own address enable |
mbed_official | 216:577900467c9e | 284 | i2c->OAR1 &= ~ I2C_OAR1_OA1EN; |
mbed_official | 216:577900467c9e | 285 | |
mbed_official | 80:66393a7b209d | 286 | // Get the old register value |
mbed_official | 80:66393a7b209d | 287 | tmpreg = i2c->OAR1; |
mbed_official | 80:66393a7b209d | 288 | // Reset address bits |
mbed_official | 80:66393a7b209d | 289 | tmpreg &= 0xFC00; |
mbed_official | 80:66393a7b209d | 290 | // Set new address |
mbed_official | 80:66393a7b209d | 291 | tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits |
mbed_official | 80:66393a7b209d | 292 | // Store the new register value |
mbed_official | 171:3d240fda1f07 | 293 | i2c->OAR1 = tmpreg | I2C_OAR1_OA1EN; |
mbed_official | 80:66393a7b209d | 294 | } |
mbed_official | 80:66393a7b209d | 295 | |
mbed_official | 80:66393a7b209d | 296 | void i2c_slave_mode(i2c_t *obj, int enable_slave) { |
mbed_official | 80:66393a7b209d | 297 | // Nothing to do |
mbed_official | 80:66393a7b209d | 298 | } |
mbed_official | 80:66393a7b209d | 299 | |
mbed_official | 80:66393a7b209d | 300 | // See I2CSlave.h |
mbed_official | 80:66393a7b209d | 301 | #define NoData 0 // the slave has not been addressed |
mbed_official | 80:66393a7b209d | 302 | #define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter) |
mbed_official | 80:66393a7b209d | 303 | #define WriteGeneral 2 // the master is writing to all slave |
mbed_official | 80:66393a7b209d | 304 | #define WriteAddressed 3 // the master is writing to this slave (slave = receiver) |
mbed_official | 80:66393a7b209d | 305 | |
mbed_official | 80:66393a7b209d | 306 | int i2c_slave_receive(i2c_t *obj) { |
mbed_official | 171:3d240fda1f07 | 307 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 176:2d0c9ad7ef62 | 308 | int event = NoData; |
mbed_official | 216:577900467c9e | 309 | |
mbed_official | 216:577900467c9e | 310 | if (I2C_GetFlagStatus(i2c, I2C_ISR_BUSY) == SET) { |
mbed_official | 216:577900467c9e | 311 | if (I2C_GetFlagStatus(i2c, I2C_ISR_ADDR) == SET) { |
mbed_official | 216:577900467c9e | 312 | // Check direction |
mbed_official | 176:2d0c9ad7ef62 | 313 | if (I2C_GetFlagStatus(i2c, I2C_ISR_DIR) == SET) { |
mbed_official | 216:577900467c9e | 314 | event = ReadAddressed; |
mbed_official | 216:577900467c9e | 315 | } else event = WriteAddressed; |
mbed_official | 216:577900467c9e | 316 | // Clear adress match flag to generate an acknowledge |
mbed_official | 216:577900467c9e | 317 | i2c->ICR |= I2C_ICR_ADDRCF; |
mbed_official | 176:2d0c9ad7ef62 | 318 | } |
mbed_official | 176:2d0c9ad7ef62 | 319 | } |
mbed_official | 171:3d240fda1f07 | 320 | return event; |
mbed_official | 80:66393a7b209d | 321 | } |
mbed_official | 80:66393a7b209d | 322 | |
mbed_official | 80:66393a7b209d | 323 | int i2c_slave_read(i2c_t *obj, char *data, int length) { |
mbed_official | 80:66393a7b209d | 324 | int count = 0; |
mbed_official | 216:577900467c9e | 325 | |
mbed_official | 80:66393a7b209d | 326 | // Read all bytes |
mbed_official | 80:66393a7b209d | 327 | for (count = 0; count < length; count++) { |
mbed_official | 80:66393a7b209d | 328 | data[count] = i2c_byte_read(obj, 0); |
mbed_official | 80:66393a7b209d | 329 | } |
mbed_official | 216:577900467c9e | 330 | |
mbed_official | 80:66393a7b209d | 331 | return count; |
mbed_official | 80:66393a7b209d | 332 | } |
mbed_official | 80:66393a7b209d | 333 | |
mbed_official | 80:66393a7b209d | 334 | int i2c_slave_write(i2c_t *obj, const char *data, int length) { |
mbed_official | 80:66393a7b209d | 335 | int count = 0; |
mbed_official | 216:577900467c9e | 336 | |
mbed_official | 80:66393a7b209d | 337 | // Write all bytes |
mbed_official | 80:66393a7b209d | 338 | for (count = 0; count < length; count++) { |
mbed_official | 80:66393a7b209d | 339 | i2c_byte_write(obj, data[count]); |
mbed_official | 80:66393a7b209d | 340 | } |
mbed_official | 216:577900467c9e | 341 | |
mbed_official | 80:66393a7b209d | 342 | return count; |
mbed_official | 80:66393a7b209d | 343 | } |
mbed_official | 80:66393a7b209d | 344 | |
mbed_official | 80:66393a7b209d | 345 | |
mbed_official | 80:66393a7b209d | 346 | #endif // DEVICE_I2CSLAVE |
mbed_official | 80:66393a7b209d | 347 | |
mbed_official | 80:66393a7b209d | 348 | #endif // DEVICE_I2C |