mbed w/ spi bug fig
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/i2c_api.c@247:135e3186a638, 2014-07-01 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Jul 01 15:00:09 2014 +0100
- Revision:
- 247:135e3186a638
- Parent:
- 227:7bd0639b8911
Synchronized with git revision 4f86d397190b9d18f22b4a189573d65b970f3662
Full URL: https://github.com/mbedmicro/mbed/commit/4f86d397190b9d18f22b4a189573d65b970f3662/
[NUCLEOs] enhance I2C API to make it work with EEPROM
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 | // Configure slave address, nbytes, reload, end mode and start or stop generation |
mbed_official | 197:36a724675865 | 182 | I2C_TransferHandling(i2c, address, length, I2C_SoftEnd_Mode, I2C_Generate_Start_Read); |
mbed_official | 216:577900467c9e | 183 | |
mbed_official | 80:66393a7b209d | 184 | // Read all bytes |
mbed_official | 80:66393a7b209d | 185 | for (count = 0; count < length; count++) { |
mbed_official | 80:66393a7b209d | 186 | value = i2c_byte_read(obj, 0); |
mbed_official | 80:66393a7b209d | 187 | data[count] = (char)value; |
mbed_official | 80:66393a7b209d | 188 | } |
mbed_official | 216:577900467c9e | 189 | |
mbed_official | 197:36a724675865 | 190 | timeout = FLAG_TIMEOUT; |
mbed_official | 216:577900467c9e | 191 | while (!I2C_GetFlagStatus(i2c, I2C_FLAG_TC)) { |
mbed_official | 197:36a724675865 | 192 | timeout--; |
mbed_official | 247:135e3186a638 | 193 | if (timeout == 0) return -1; |
mbed_official | 197:36a724675865 | 194 | } |
mbed_official | 216:577900467c9e | 195 | |
mbed_official | 216:577900467c9e | 196 | if (stop) i2c_stop(obj); |
mbed_official | 197:36a724675865 | 197 | |
mbed_official | 80:66393a7b209d | 198 | return length; |
mbed_official | 80:66393a7b209d | 199 | } |
mbed_official | 80:66393a7b209d | 200 | |
mbed_official | 80:66393a7b209d | 201 | int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { |
mbed_official | 80:66393a7b209d | 202 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 197:36a724675865 | 203 | int timeout; |
mbed_official | 80:66393a7b209d | 204 | int count; |
mbed_official | 216:577900467c9e | 205 | |
mbed_official | 197:36a724675865 | 206 | // Configure slave address, nbytes, reload, end mode and start generation |
mbed_official | 216:577900467c9e | 207 | I2C_TransferHandling(i2c, address, length, I2C_SoftEnd_Mode, I2C_Generate_Start_Write); |
mbed_official | 216:577900467c9e | 208 | |
mbed_official | 80:66393a7b209d | 209 | // Write all bytes |
mbed_official | 80:66393a7b209d | 210 | for (count = 0; count < length; count++) { |
mbed_official | 197:36a724675865 | 211 | i2c_byte_write(obj, data[count]); |
mbed_official | 216:577900467c9e | 212 | } |
mbed_official | 216:577900467c9e | 213 | |
mbed_official | 197:36a724675865 | 214 | timeout = FLAG_TIMEOUT; |
mbed_official | 216:577900467c9e | 215 | while (!I2C_GetFlagStatus(i2c, I2C_FLAG_TC)) { |
mbed_official | 197:36a724675865 | 216 | timeout--; |
mbed_official | 247:135e3186a638 | 217 | if (timeout == 0) return -1; |
mbed_official | 80:66393a7b209d | 218 | } |
mbed_official | 80:66393a7b209d | 219 | |
mbed_official | 216:577900467c9e | 220 | if (stop) i2c_stop(obj); |
mbed_official | 216:577900467c9e | 221 | |
mbed_official | 80:66393a7b209d | 222 | return count; |
mbed_official | 80:66393a7b209d | 223 | } |
mbed_official | 80:66393a7b209d | 224 | |
mbed_official | 80:66393a7b209d | 225 | int i2c_byte_read(i2c_t *obj, int last) { |
mbed_official | 80:66393a7b209d | 226 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 80:66393a7b209d | 227 | uint8_t data; |
mbed_official | 84:f54042cbc282 | 228 | int timeout; |
mbed_official | 216:577900467c9e | 229 | |
mbed_official | 80:66393a7b209d | 230 | // Wait until the byte is received |
mbed_official | 216:577900467c9e | 231 | timeout = FLAG_TIMEOUT; |
mbed_official | 80:66393a7b209d | 232 | while (I2C_GetFlagStatus(i2c, I2C_ISR_RXNE) == RESET) { |
mbed_official | 84:f54042cbc282 | 233 | timeout--; |
mbed_official | 84:f54042cbc282 | 234 | if (timeout == 0) { |
mbed_official | 247:135e3186a638 | 235 | return -1; |
mbed_official | 84:f54042cbc282 | 236 | } |
mbed_official | 80:66393a7b209d | 237 | } |
mbed_official | 80:66393a7b209d | 238 | |
mbed_official | 80:66393a7b209d | 239 | data = I2C_ReceiveData(i2c); |
mbed_official | 216:577900467c9e | 240 | |
mbed_official | 80:66393a7b209d | 241 | return (int)data; |
mbed_official | 80:66393a7b209d | 242 | } |
mbed_official | 80:66393a7b209d | 243 | |
mbed_official | 80:66393a7b209d | 244 | int i2c_byte_write(i2c_t *obj, int data) { |
mbed_official | 80:66393a7b209d | 245 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 84:f54042cbc282 | 246 | int timeout; |
mbed_official | 80:66393a7b209d | 247 | |
mbed_official | 84:f54042cbc282 | 248 | // Wait until the previous byte is transmitted |
mbed_official | 84:f54042cbc282 | 249 | timeout = FLAG_TIMEOUT; |
mbed_official | 84:f54042cbc282 | 250 | while (I2C_GetFlagStatus(i2c, I2C_ISR_TXIS) == RESET) { |
mbed_official | 84:f54042cbc282 | 251 | timeout--; |
mbed_official | 84:f54042cbc282 | 252 | if (timeout == 0) { |
mbed_official | 84:f54042cbc282 | 253 | return 0; |
mbed_official | 84:f54042cbc282 | 254 | } |
mbed_official | 80:66393a7b209d | 255 | } |
mbed_official | 216:577900467c9e | 256 | |
mbed_official | 80:66393a7b209d | 257 | I2C_SendData(i2c, (uint8_t)data); |
mbed_official | 216:577900467c9e | 258 | |
mbed_official | 80:66393a7b209d | 259 | return 1; |
mbed_official | 80:66393a7b209d | 260 | } |
mbed_official | 80:66393a7b209d | 261 | |
mbed_official | 80:66393a7b209d | 262 | void i2c_reset(i2c_t *obj) { |
mbed_official | 216:577900467c9e | 263 | if (obj->i2c == I2C_1) { |
mbed_official | 80:66393a7b209d | 264 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE); |
mbed_official | 80:66393a7b209d | 265 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE); |
mbed_official | 80:66393a7b209d | 266 | } |
mbed_official | 171:3d240fda1f07 | 267 | if (obj->i2c == I2C_2) { |
mbed_official | 171:3d240fda1f07 | 268 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE); |
mbed_official | 216:577900467c9e | 269 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE); |
mbed_official | 171:3d240fda1f07 | 270 | } |
mbed_official | 80:66393a7b209d | 271 | } |
mbed_official | 80:66393a7b209d | 272 | |
mbed_official | 80:66393a7b209d | 273 | #if DEVICE_I2CSLAVE |
mbed_official | 80:66393a7b209d | 274 | |
mbed_official | 80:66393a7b209d | 275 | void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) { |
mbed_official | 80:66393a7b209d | 276 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 80:66393a7b209d | 277 | uint16_t tmpreg; |
mbed_official | 216:577900467c9e | 278 | |
mbed_official | 171:3d240fda1f07 | 279 | // reset own address enable |
mbed_official | 216:577900467c9e | 280 | i2c->OAR1 &= ~ I2C_OAR1_OA1EN; |
mbed_official | 216:577900467c9e | 281 | |
mbed_official | 80:66393a7b209d | 282 | // Get the old register value |
mbed_official | 80:66393a7b209d | 283 | tmpreg = i2c->OAR1; |
mbed_official | 80:66393a7b209d | 284 | // Reset address bits |
mbed_official | 80:66393a7b209d | 285 | tmpreg &= 0xFC00; |
mbed_official | 80:66393a7b209d | 286 | // Set new address |
mbed_official | 80:66393a7b209d | 287 | tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits |
mbed_official | 80:66393a7b209d | 288 | // Store the new register value |
mbed_official | 171:3d240fda1f07 | 289 | i2c->OAR1 = tmpreg | I2C_OAR1_OA1EN; |
mbed_official | 80:66393a7b209d | 290 | } |
mbed_official | 80:66393a7b209d | 291 | |
mbed_official | 80:66393a7b209d | 292 | void i2c_slave_mode(i2c_t *obj, int enable_slave) { |
mbed_official | 80:66393a7b209d | 293 | // Nothing to do |
mbed_official | 80:66393a7b209d | 294 | } |
mbed_official | 80:66393a7b209d | 295 | |
mbed_official | 80:66393a7b209d | 296 | // See I2CSlave.h |
mbed_official | 80:66393a7b209d | 297 | #define NoData 0 // the slave has not been addressed |
mbed_official | 80:66393a7b209d | 298 | #define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter) |
mbed_official | 80:66393a7b209d | 299 | #define WriteGeneral 2 // the master is writing to all slave |
mbed_official | 80:66393a7b209d | 300 | #define WriteAddressed 3 // the master is writing to this slave (slave = receiver) |
mbed_official | 80:66393a7b209d | 301 | |
mbed_official | 80:66393a7b209d | 302 | int i2c_slave_receive(i2c_t *obj) { |
mbed_official | 171:3d240fda1f07 | 303 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 176:2d0c9ad7ef62 | 304 | int event = NoData; |
mbed_official | 216:577900467c9e | 305 | |
mbed_official | 216:577900467c9e | 306 | if (I2C_GetFlagStatus(i2c, I2C_ISR_BUSY) == SET) { |
mbed_official | 216:577900467c9e | 307 | if (I2C_GetFlagStatus(i2c, I2C_ISR_ADDR) == SET) { |
mbed_official | 216:577900467c9e | 308 | // Check direction |
mbed_official | 176:2d0c9ad7ef62 | 309 | if (I2C_GetFlagStatus(i2c, I2C_ISR_DIR) == SET) { |
mbed_official | 216:577900467c9e | 310 | event = ReadAddressed; |
mbed_official | 216:577900467c9e | 311 | } else event = WriteAddressed; |
mbed_official | 216:577900467c9e | 312 | // Clear adress match flag to generate an acknowledge |
mbed_official | 216:577900467c9e | 313 | i2c->ICR |= I2C_ICR_ADDRCF; |
mbed_official | 176:2d0c9ad7ef62 | 314 | } |
mbed_official | 176:2d0c9ad7ef62 | 315 | } |
mbed_official | 171:3d240fda1f07 | 316 | return event; |
mbed_official | 80:66393a7b209d | 317 | } |
mbed_official | 80:66393a7b209d | 318 | |
mbed_official | 80:66393a7b209d | 319 | int i2c_slave_read(i2c_t *obj, char *data, int length) { |
mbed_official | 80:66393a7b209d | 320 | int count = 0; |
mbed_official | 216:577900467c9e | 321 | |
mbed_official | 80:66393a7b209d | 322 | // Read all bytes |
mbed_official | 80:66393a7b209d | 323 | for (count = 0; count < length; count++) { |
mbed_official | 80:66393a7b209d | 324 | data[count] = i2c_byte_read(obj, 0); |
mbed_official | 80:66393a7b209d | 325 | } |
mbed_official | 216:577900467c9e | 326 | |
mbed_official | 80:66393a7b209d | 327 | return count; |
mbed_official | 80:66393a7b209d | 328 | } |
mbed_official | 80:66393a7b209d | 329 | |
mbed_official | 80:66393a7b209d | 330 | int i2c_slave_write(i2c_t *obj, const char *data, int length) { |
mbed_official | 80:66393a7b209d | 331 | int count = 0; |
mbed_official | 216:577900467c9e | 332 | |
mbed_official | 80:66393a7b209d | 333 | // Write all bytes |
mbed_official | 80:66393a7b209d | 334 | for (count = 0; count < length; count++) { |
mbed_official | 80:66393a7b209d | 335 | i2c_byte_write(obj, data[count]); |
mbed_official | 80:66393a7b209d | 336 | } |
mbed_official | 216:577900467c9e | 337 | |
mbed_official | 80:66393a7b209d | 338 | return count; |
mbed_official | 80:66393a7b209d | 339 | } |
mbed_official | 80:66393a7b209d | 340 | |
mbed_official | 80:66393a7b209d | 341 | |
mbed_official | 80:66393a7b209d | 342 | #endif // DEVICE_I2CSLAVE |
mbed_official | 80:66393a7b209d | 343 | |
mbed_official | 80:66393a7b209d | 344 | #endif // DEVICE_I2C |