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