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