mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/i2c_api.c@197:36a724675865, 2014-05-19 (annotated)
- Committer:
- mbed_official
- Date:
- Mon May 19 11:00:07 2014 +0100
- Revision:
- 197:36a724675865
- Parent:
- 162:937d965048d3
- Child:
- 216:577900467c9e
Synchronized with git revision ffcaadfc684a5938533d664b7ef3aa138d51dafe
Full URL: https://github.com/mbedmicro/mbed/commit/ffcaadfc684a5938533d664b7ef3aa138d51dafe/
[NUCLEO_F302R8] Add I2C slave, enhance I2C master
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 | 125:23cc3068a9e4 | 30 | #include "i2c_api.h" |
mbed_official | 125:23cc3068a9e4 | 31 | |
mbed_official | 125:23cc3068a9e4 | 32 | #if DEVICE_I2C |
mbed_official | 125:23cc3068a9e4 | 33 | |
mbed_official | 125:23cc3068a9e4 | 34 | #include "cmsis.h" |
mbed_official | 125:23cc3068a9e4 | 35 | #include "pinmap.h" |
mbed_official | 125:23cc3068a9e4 | 36 | #include "error.h" |
mbed_official | 125:23cc3068a9e4 | 37 | |
mbed_official | 125:23cc3068a9e4 | 38 | /* Timeout values for flags and events waiting loops. These timeouts are |
mbed_official | 135:067cc8ba23da | 39 | not based on accurate values, they just guarantee that the application will |
mbed_official | 135:067cc8ba23da | 40 | not remain stuck if the I2C communication is corrupted. */ |
mbed_official | 125:23cc3068a9e4 | 41 | #define FLAG_TIMEOUT ((int)0x1000) |
mbed_official | 125:23cc3068a9e4 | 42 | #define LONG_TIMEOUT ((int)0x8000) |
mbed_official | 125:23cc3068a9e4 | 43 | |
mbed_official | 125:23cc3068a9e4 | 44 | static const PinMap PinMap_I2C_SDA[] = { |
mbed_official | 125:23cc3068a9e4 | 45 | {PA_10, I2C_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_4)}, |
mbed_official | 125:23cc3068a9e4 | 46 | {PA_14, I2C_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_4)}, |
mbed_official | 125:23cc3068a9e4 | 47 | {PB_5, I2C_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_8)}, |
mbed_official | 125:23cc3068a9e4 | 48 | {PB_7, I2C_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_4)}, |
mbed_official | 125:23cc3068a9e4 | 49 | {PB_9, I2C_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_4)}, |
mbed_official | 125:23cc3068a9e4 | 50 | {PC_9, I2C_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_3)}, |
mbed_official | 125:23cc3068a9e4 | 51 | {PF_0, I2C_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_4)}, |
mbed_official | 125:23cc3068a9e4 | 52 | {NC, NC, 0} |
mbed_official | 125:23cc3068a9e4 | 53 | }; |
mbed_official | 125:23cc3068a9e4 | 54 | |
mbed_official | 125:23cc3068a9e4 | 55 | static const PinMap PinMap_I2C_SCL[] = { |
mbed_official | 125:23cc3068a9e4 | 56 | {PA_8, I2C_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_3)}, |
mbed_official | 125:23cc3068a9e4 | 57 | {PA_9, I2C_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_4)}, |
mbed_official | 125:23cc3068a9e4 | 58 | {PA_15, I2C_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_4)}, |
mbed_official | 125:23cc3068a9e4 | 59 | {PB_6, I2C_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_4)}, |
mbed_official | 125:23cc3068a9e4 | 60 | {PB_8, I2C_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_4)}, |
mbed_official | 125:23cc3068a9e4 | 61 | {PF_1, I2C_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_4)}, |
mbed_official | 125:23cc3068a9e4 | 62 | {NC, NC, 0} |
mbed_official | 125:23cc3068a9e4 | 63 | }; |
mbed_official | 125:23cc3068a9e4 | 64 | |
mbed_official | 135:067cc8ba23da | 65 | void i2c_init(i2c_t *obj, PinName sda, PinName scl) { |
mbed_official | 125:23cc3068a9e4 | 66 | // Determine the I2C to use |
mbed_official | 125:23cc3068a9e4 | 67 | I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); |
mbed_official | 125:23cc3068a9e4 | 68 | I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); |
mbed_official | 125:23cc3068a9e4 | 69 | |
mbed_official | 125:23cc3068a9e4 | 70 | obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); |
mbed_official | 135:067cc8ba23da | 71 | |
mbed_official | 125:23cc3068a9e4 | 72 | if (obj->i2c == (I2CName)NC) { |
mbed_official | 125:23cc3068a9e4 | 73 | error("I2C pin mapping failed"); |
mbed_official | 125:23cc3068a9e4 | 74 | } |
mbed_official | 125:23cc3068a9e4 | 75 | |
mbed_official | 125:23cc3068a9e4 | 76 | // Enable I2C clock |
mbed_official | 135:067cc8ba23da | 77 | if (obj->i2c == I2C_1) { |
mbed_official | 125:23cc3068a9e4 | 78 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); |
mbed_official | 197:36a724675865 | 79 | RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK); |
mbed_official | 125:23cc3068a9e4 | 80 | } |
mbed_official | 125:23cc3068a9e4 | 81 | if (obj->i2c == I2C_2) { |
mbed_official | 125:23cc3068a9e4 | 82 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE); |
mbed_official | 125:23cc3068a9e4 | 83 | } |
mbed_official | 125:23cc3068a9e4 | 84 | if (obj->i2c == I2C_3) { |
mbed_official | 125:23cc3068a9e4 | 85 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C3, ENABLE); |
mbed_official | 125:23cc3068a9e4 | 86 | } |
mbed_official | 135:067cc8ba23da | 87 | |
mbed_official | 125:23cc3068a9e4 | 88 | // Configure I2C pins |
mbed_official | 125:23cc3068a9e4 | 89 | pinmap_pinout(scl, PinMap_I2C_SCL); |
mbed_official | 125:23cc3068a9e4 | 90 | pin_mode(scl, OpenDrain); |
mbed_official | 125:23cc3068a9e4 | 91 | pinmap_pinout(sda, PinMap_I2C_SDA); |
mbed_official | 125:23cc3068a9e4 | 92 | pin_mode(sda, OpenDrain); |
mbed_official | 135:067cc8ba23da | 93 | |
mbed_official | 125:23cc3068a9e4 | 94 | // Reset to clear pending flags if any |
mbed_official | 125:23cc3068a9e4 | 95 | i2c_reset(obj); |
mbed_official | 135:067cc8ba23da | 96 | |
mbed_official | 125:23cc3068a9e4 | 97 | // I2C configuration |
mbed_official | 135:067cc8ba23da | 98 | i2c_frequency(obj, 100000); // 100 kHz per default |
mbed_official | 125:23cc3068a9e4 | 99 | } |
mbed_official | 125:23cc3068a9e4 | 100 | |
mbed_official | 125:23cc3068a9e4 | 101 | void i2c_frequency(i2c_t *obj, int hz) { |
mbed_official | 125:23cc3068a9e4 | 102 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 125:23cc3068a9e4 | 103 | I2C_InitTypeDef I2C_InitStructure; |
mbed_official | 162:937d965048d3 | 104 | uint32_t tim = 0; |
mbed_official | 125:23cc3068a9e4 | 105 | |
mbed_official | 125:23cc3068a9e4 | 106 | // Disable the Fast Mode Plus capability |
mbed_official | 125:23cc3068a9e4 | 107 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); // Enable SYSCFG clock |
mbed_official | 125:23cc3068a9e4 | 108 | SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C1, DISABLE); |
mbed_official | 125:23cc3068a9e4 | 109 | SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C2, DISABLE); |
mbed_official | 135:067cc8ba23da | 110 | |
mbed_official | 125:23cc3068a9e4 | 111 | /* |
mbed_official | 125:23cc3068a9e4 | 112 | Values calculated with I2C_Timing_Configuration_V1.0.1.xls file (see AN4235) |
mbed_official | 125:23cc3068a9e4 | 113 | * Standard mode (up to 100 kHz) |
mbed_official | 125:23cc3068a9e4 | 114 | * Fast Mode (up to 400 kHz) |
mbed_official | 125:23cc3068a9e4 | 115 | * Fast Mode Plus (up to 1 MHz) |
mbed_official | 125:23cc3068a9e4 | 116 | Below values obtained with: |
mbed_official | 197:36a724675865 | 117 | - I2C clock source = 64 MHz (System Clock w/ HSI) or 72 (System Clock w/ HSE) |
mbed_official | 125:23cc3068a9e4 | 118 | - Analog filter delay = ON |
mbed_official | 125:23cc3068a9e4 | 119 | - Digital filter coefficient = 0 |
mbed_official | 125:23cc3068a9e4 | 120 | - Rise time = 100 ns |
mbed_official | 125:23cc3068a9e4 | 121 | - Fall time = 10ns |
mbed_official | 125:23cc3068a9e4 | 122 | */ |
mbed_official | 197:36a724675865 | 123 | if (SystemCoreClock == 64000000) { |
mbed_official | 197:36a724675865 | 124 | switch (hz) { |
mbed_official | 197:36a724675865 | 125 | case 100000: |
mbed_official | 197:36a724675865 | 126 | tim = 0x60302730; // Standard mode |
mbed_official | 197:36a724675865 | 127 | break; |
mbed_official | 197:36a724675865 | 128 | case 200000: |
mbed_official | 197:36a724675865 | 129 | tim = 0x00C07AB3; // Fast Mode |
mbed_official | 197:36a724675865 | 130 | break; |
mbed_official | 197:36a724675865 | 131 | case 400000: |
mbed_official | 197:36a724675865 | 132 | tim = 0x00C0216C; // Fast Mode |
mbed_official | 135:067cc8ba23da | 133 | break; |
mbed_official | 197:36a724675865 | 134 | case 1000000: |
mbed_official | 197:36a724675865 | 135 | tim = 0x00900B22; // Fast Mode Plus |
mbed_official | 197:36a724675865 | 136 | // Enable the Fast Mode Plus capability |
mbed_official | 197:36a724675865 | 137 | if (obj->i2c == I2C_1) { |
mbed_official | 197:36a724675865 | 138 | SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C1, ENABLE); |
mbed_official | 197:36a724675865 | 139 | } |
mbed_official | 197:36a724675865 | 140 | if (obj->i2c == I2C_2) { |
mbed_official | 197:36a724675865 | 141 | SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C2, ENABLE); |
mbed_official | 197:36a724675865 | 142 | } |
mbed_official | 135:067cc8ba23da | 143 | break; |
mbed_official | 197:36a724675865 | 144 | default: |
mbed_official | 197:36a724675865 | 145 | error("Only 100kHz, 200kHz, 400kHz and 1MHz I2C frequencies are supported."); |
mbed_official | 135:067cc8ba23da | 146 | break; |
mbed_official | 197:36a724675865 | 147 | } |
mbed_official | 197:36a724675865 | 148 | } |
mbed_official | 197:36a724675865 | 149 | else if (SystemCoreClock == 72000000) { |
mbed_official | 197:36a724675865 | 150 | switch (hz) { |
mbed_official | 197:36a724675865 | 151 | case 100000: |
mbed_official | 197:36a724675865 | 152 | tim = 0x10C08DCF; // Standard mode |
mbed_official | 197:36a724675865 | 153 | break; |
mbed_official | 197:36a724675865 | 154 | case 200000: |
mbed_official | 197:36a724675865 | 155 | tim = 0xA010031A; // Fast Mode |
mbed_official | 197:36a724675865 | 156 | break; |
mbed_official | 197:36a724675865 | 157 | case 400000: |
mbed_official | 197:36a724675865 | 158 | tim = 0x00E0257A; // Fast Mode |
mbed_official | 197:36a724675865 | 159 | break; |
mbed_official | 197:36a724675865 | 160 | case 1000000: |
mbed_official | 197:36a724675865 | 161 | tim = 0x00A00D26; // Fast Mode Plus |
mbed_official | 197:36a724675865 | 162 | // Enable the Fast Mode Plus capability |
mbed_official | 197:36a724675865 | 163 | if (obj->i2c == I2C_1) { |
mbed_official | 197:36a724675865 | 164 | SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C1, ENABLE); |
mbed_official | 197:36a724675865 | 165 | } |
mbed_official | 197:36a724675865 | 166 | if (obj->i2c == I2C_2) { |
mbed_official | 197:36a724675865 | 167 | SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C2, ENABLE); |
mbed_official | 197:36a724675865 | 168 | } |
mbed_official | 197:36a724675865 | 169 | break; |
mbed_official | 197:36a724675865 | 170 | default: |
mbed_official | 197:36a724675865 | 171 | error("Only 100kHz, 200kHz, 400kHz and 1MHz I2C frequencies are supported."); |
mbed_official | 197:36a724675865 | 172 | break; |
mbed_official | 197:36a724675865 | 173 | } |
mbed_official | 197:36a724675865 | 174 | } |
mbed_official | 197:36a724675865 | 175 | else { |
mbed_official | 197:36a724675865 | 176 | error("System clock setting is not supported."); |
mbed_official | 125:23cc3068a9e4 | 177 | } |
mbed_official | 135:067cc8ba23da | 178 | |
mbed_official | 125:23cc3068a9e4 | 179 | // I2C configuration |
mbed_official | 125:23cc3068a9e4 | 180 | I2C_DeInit(i2c); |
mbed_official | 125:23cc3068a9e4 | 181 | I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; |
mbed_official | 125:23cc3068a9e4 | 182 | I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable; |
mbed_official | 125:23cc3068a9e4 | 183 | I2C_InitStructure.I2C_DigitalFilter = 0x00; |
mbed_official | 125:23cc3068a9e4 | 184 | I2C_InitStructure.I2C_OwnAddress1 = 0x00; |
mbed_official | 125:23cc3068a9e4 | 185 | I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; |
mbed_official | 125:23cc3068a9e4 | 186 | I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; |
mbed_official | 125:23cc3068a9e4 | 187 | I2C_InitStructure.I2C_Timing = tim; |
mbed_official | 125:23cc3068a9e4 | 188 | I2C_Init(i2c, &I2C_InitStructure); |
mbed_official | 135:067cc8ba23da | 189 | |
mbed_official | 125:23cc3068a9e4 | 190 | I2C_Cmd(i2c, ENABLE); |
mbed_official | 125:23cc3068a9e4 | 191 | } |
mbed_official | 125:23cc3068a9e4 | 192 | |
mbed_official | 125:23cc3068a9e4 | 193 | inline int i2c_start(i2c_t *obj) { |
mbed_official | 125:23cc3068a9e4 | 194 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 125:23cc3068a9e4 | 195 | int timeout; |
mbed_official | 125:23cc3068a9e4 | 196 | |
mbed_official | 125:23cc3068a9e4 | 197 | // Test BUSY Flag |
mbed_official | 125:23cc3068a9e4 | 198 | timeout = LONG_TIMEOUT; |
mbed_official | 125:23cc3068a9e4 | 199 | while (I2C_GetFlagStatus(i2c, I2C_ISR_BUSY) != RESET) { |
mbed_official | 125:23cc3068a9e4 | 200 | timeout--; |
mbed_official | 125:23cc3068a9e4 | 201 | if (timeout == 0) { |
mbed_official | 125:23cc3068a9e4 | 202 | return 0; |
mbed_official | 125:23cc3068a9e4 | 203 | } |
mbed_official | 125:23cc3068a9e4 | 204 | } |
mbed_official | 125:23cc3068a9e4 | 205 | |
mbed_official | 125:23cc3068a9e4 | 206 | I2C_GenerateSTART(i2c, ENABLE); |
mbed_official | 125:23cc3068a9e4 | 207 | |
mbed_official | 125:23cc3068a9e4 | 208 | return 0; |
mbed_official | 125:23cc3068a9e4 | 209 | } |
mbed_official | 125:23cc3068a9e4 | 210 | |
mbed_official | 125:23cc3068a9e4 | 211 | inline int i2c_stop(i2c_t *obj) { |
mbed_official | 125:23cc3068a9e4 | 212 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 135:067cc8ba23da | 213 | |
mbed_official | 125:23cc3068a9e4 | 214 | I2C_GenerateSTOP(i2c, ENABLE); |
mbed_official | 135:067cc8ba23da | 215 | |
mbed_official | 125:23cc3068a9e4 | 216 | return 0; |
mbed_official | 125:23cc3068a9e4 | 217 | } |
mbed_official | 125:23cc3068a9e4 | 218 | |
mbed_official | 197:36a724675865 | 219 | |
mbed_official | 125:23cc3068a9e4 | 220 | int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { |
mbed_official | 125:23cc3068a9e4 | 221 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 125:23cc3068a9e4 | 222 | int count; |
mbed_official | 197:36a724675865 | 223 | int timeout; |
mbed_official | 125:23cc3068a9e4 | 224 | int value; |
mbed_official | 135:067cc8ba23da | 225 | |
mbed_official | 125:23cc3068a9e4 | 226 | if (length == 0) return 0; |
mbed_official | 125:23cc3068a9e4 | 227 | |
mbed_official | 125:23cc3068a9e4 | 228 | // Configure slave address, nbytes, reload, end mode and start or stop generation |
mbed_official | 197:36a724675865 | 229 | I2C_TransferHandling(i2c, address, length, I2C_SoftEnd_Mode, I2C_Generate_Start_Read); |
mbed_official | 135:067cc8ba23da | 230 | |
mbed_official | 125:23cc3068a9e4 | 231 | // Read all bytes |
mbed_official | 125:23cc3068a9e4 | 232 | for (count = 0; count < length; count++) { |
mbed_official | 125:23cc3068a9e4 | 233 | value = i2c_byte_read(obj, 0); |
mbed_official | 125:23cc3068a9e4 | 234 | data[count] = (char)value; |
mbed_official | 125:23cc3068a9e4 | 235 | } |
mbed_official | 135:067cc8ba23da | 236 | |
mbed_official | 197:36a724675865 | 237 | timeout = FLAG_TIMEOUT; |
mbed_official | 197:36a724675865 | 238 | while(!I2C_GetFlagStatus(i2c, I2C_FLAG_TC)) { |
mbed_official | 197:36a724675865 | 239 | timeout--; |
mbed_official | 197:36a724675865 | 240 | if (timeout == 0) return 0; |
mbed_official | 197:36a724675865 | 241 | } |
mbed_official | 197:36a724675865 | 242 | |
mbed_official | 197:36a724675865 | 243 | if(stop) i2c_stop(obj); |
mbed_official | 197:36a724675865 | 244 | |
mbed_official | 125:23cc3068a9e4 | 245 | return length; |
mbed_official | 125:23cc3068a9e4 | 246 | } |
mbed_official | 125:23cc3068a9e4 | 247 | |
mbed_official | 197:36a724675865 | 248 | |
mbed_official | 125:23cc3068a9e4 | 249 | int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { |
mbed_official | 125:23cc3068a9e4 | 250 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 197:36a724675865 | 251 | int timeout; |
mbed_official | 125:23cc3068a9e4 | 252 | int count; |
mbed_official | 135:067cc8ba23da | 253 | |
mbed_official | 125:23cc3068a9e4 | 254 | if (length == 0) return 0; |
mbed_official | 125:23cc3068a9e4 | 255 | |
mbed_official | 197:36a724675865 | 256 | // Configure slave address, nbytes, reload, end mode and start generation |
mbed_official | 197:36a724675865 | 257 | I2C_TransferHandling(i2c, address, length, I2C_SoftEnd_Mode, I2C_Generate_Start_Write); |
mbed_official | 135:067cc8ba23da | 258 | |
mbed_official | 125:23cc3068a9e4 | 259 | // Write all bytes |
mbed_official | 125:23cc3068a9e4 | 260 | for (count = 0; count < length; count++) { |
mbed_official | 197:36a724675865 | 261 | i2c_byte_write(obj, data[count]); |
mbed_official | 125:23cc3068a9e4 | 262 | } |
mbed_official | 125:23cc3068a9e4 | 263 | |
mbed_official | 197:36a724675865 | 264 | timeout = FLAG_TIMEOUT; |
mbed_official | 197:36a724675865 | 265 | while(!I2C_GetFlagStatus(i2c, I2C_FLAG_TC)) { |
mbed_official | 197:36a724675865 | 266 | timeout--; |
mbed_official | 197:36a724675865 | 267 | if (timeout == 0) return 0; |
mbed_official | 125:23cc3068a9e4 | 268 | } |
mbed_official | 197:36a724675865 | 269 | |
mbed_official | 197:36a724675865 | 270 | if(stop) i2c_stop(obj); |
mbed_official | 135:067cc8ba23da | 271 | |
mbed_official | 125:23cc3068a9e4 | 272 | return count; |
mbed_official | 125:23cc3068a9e4 | 273 | } |
mbed_official | 125:23cc3068a9e4 | 274 | |
mbed_official | 125:23cc3068a9e4 | 275 | int i2c_byte_read(i2c_t *obj, int last) { |
mbed_official | 125:23cc3068a9e4 | 276 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 125:23cc3068a9e4 | 277 | uint8_t data; |
mbed_official | 125:23cc3068a9e4 | 278 | int timeout; |
mbed_official | 135:067cc8ba23da | 279 | |
mbed_official | 125:23cc3068a9e4 | 280 | // Wait until the byte is received |
mbed_official | 135:067cc8ba23da | 281 | timeout = FLAG_TIMEOUT; |
mbed_official | 125:23cc3068a9e4 | 282 | while (I2C_GetFlagStatus(i2c, I2C_ISR_RXNE) == RESET) { |
mbed_official | 125:23cc3068a9e4 | 283 | timeout--; |
mbed_official | 125:23cc3068a9e4 | 284 | if (timeout == 0) { |
mbed_official | 125:23cc3068a9e4 | 285 | return 0; |
mbed_official | 125:23cc3068a9e4 | 286 | } |
mbed_official | 125:23cc3068a9e4 | 287 | } |
mbed_official | 125:23cc3068a9e4 | 288 | |
mbed_official | 125:23cc3068a9e4 | 289 | data = I2C_ReceiveData(i2c); |
mbed_official | 135:067cc8ba23da | 290 | |
mbed_official | 125:23cc3068a9e4 | 291 | return (int)data; |
mbed_official | 125:23cc3068a9e4 | 292 | } |
mbed_official | 125:23cc3068a9e4 | 293 | |
mbed_official | 125:23cc3068a9e4 | 294 | int i2c_byte_write(i2c_t *obj, int data) { |
mbed_official | 125:23cc3068a9e4 | 295 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 125:23cc3068a9e4 | 296 | int timeout; |
mbed_official | 125:23cc3068a9e4 | 297 | |
mbed_official | 125:23cc3068a9e4 | 298 | // Wait until the previous byte is transmitted |
mbed_official | 125:23cc3068a9e4 | 299 | timeout = FLAG_TIMEOUT; |
mbed_official | 125:23cc3068a9e4 | 300 | while (I2C_GetFlagStatus(i2c, I2C_ISR_TXIS) == RESET) { |
mbed_official | 125:23cc3068a9e4 | 301 | timeout--; |
mbed_official | 125:23cc3068a9e4 | 302 | if (timeout == 0) { |
mbed_official | 125:23cc3068a9e4 | 303 | return 0; |
mbed_official | 125:23cc3068a9e4 | 304 | } |
mbed_official | 125:23cc3068a9e4 | 305 | } |
mbed_official | 135:067cc8ba23da | 306 | |
mbed_official | 125:23cc3068a9e4 | 307 | I2C_SendData(i2c, (uint8_t)data); |
mbed_official | 135:067cc8ba23da | 308 | |
mbed_official | 125:23cc3068a9e4 | 309 | return 1; |
mbed_official | 125:23cc3068a9e4 | 310 | } |
mbed_official | 125:23cc3068a9e4 | 311 | |
mbed_official | 125:23cc3068a9e4 | 312 | void i2c_reset(i2c_t *obj) { |
mbed_official | 135:067cc8ba23da | 313 | if (obj->i2c == I2C_1) { |
mbed_official | 125:23cc3068a9e4 | 314 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE); |
mbed_official | 125:23cc3068a9e4 | 315 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE); |
mbed_official | 125:23cc3068a9e4 | 316 | } |
mbed_official | 125:23cc3068a9e4 | 317 | if (obj->i2c == I2C_2) { |
mbed_official | 125:23cc3068a9e4 | 318 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE); |
mbed_official | 125:23cc3068a9e4 | 319 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE); |
mbed_official | 125:23cc3068a9e4 | 320 | } |
mbed_official | 125:23cc3068a9e4 | 321 | if (obj->i2c == I2C_3) { |
mbed_official | 125:23cc3068a9e4 | 322 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C3, ENABLE); |
mbed_official | 135:067cc8ba23da | 323 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C3, DISABLE); |
mbed_official | 125:23cc3068a9e4 | 324 | } |
mbed_official | 125:23cc3068a9e4 | 325 | } |
mbed_official | 125:23cc3068a9e4 | 326 | |
mbed_official | 125:23cc3068a9e4 | 327 | #if DEVICE_I2CSLAVE |
mbed_official | 125:23cc3068a9e4 | 328 | |
mbed_official | 125:23cc3068a9e4 | 329 | void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) { |
mbed_official | 125:23cc3068a9e4 | 330 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 125:23cc3068a9e4 | 331 | uint16_t tmpreg; |
mbed_official | 135:067cc8ba23da | 332 | |
mbed_official | 197:36a724675865 | 333 | // reset own address enable |
mbed_official | 197:36a724675865 | 334 | i2c->OAR1 &=~ I2C_OAR1_OA1EN; |
mbed_official | 197:36a724675865 | 335 | |
mbed_official | 125:23cc3068a9e4 | 336 | // Get the old register value |
mbed_official | 125:23cc3068a9e4 | 337 | tmpreg = i2c->OAR1; |
mbed_official | 125:23cc3068a9e4 | 338 | // Reset address bits |
mbed_official | 125:23cc3068a9e4 | 339 | tmpreg &= 0xFC00; |
mbed_official | 125:23cc3068a9e4 | 340 | // Set new address |
mbed_official | 125:23cc3068a9e4 | 341 | tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits |
mbed_official | 125:23cc3068a9e4 | 342 | // Store the new register value |
mbed_official | 197:36a724675865 | 343 | i2c->OAR1 = tmpreg | I2C_OAR1_OA1EN; |
mbed_official | 125:23cc3068a9e4 | 344 | } |
mbed_official | 125:23cc3068a9e4 | 345 | |
mbed_official | 125:23cc3068a9e4 | 346 | void i2c_slave_mode(i2c_t *obj, int enable_slave) { |
mbed_official | 125:23cc3068a9e4 | 347 | // Nothing to do |
mbed_official | 125:23cc3068a9e4 | 348 | } |
mbed_official | 125:23cc3068a9e4 | 349 | |
mbed_official | 125:23cc3068a9e4 | 350 | // See I2CSlave.h |
mbed_official | 125:23cc3068a9e4 | 351 | #define NoData 0 // the slave has not been addressed |
mbed_official | 125:23cc3068a9e4 | 352 | #define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter) |
mbed_official | 125:23cc3068a9e4 | 353 | #define WriteGeneral 2 // the master is writing to all slave |
mbed_official | 125:23cc3068a9e4 | 354 | #define WriteAddressed 3 // the master is writing to this slave (slave = receiver) |
mbed_official | 125:23cc3068a9e4 | 355 | |
mbed_official | 125:23cc3068a9e4 | 356 | int i2c_slave_receive(i2c_t *obj) { |
mbed_official | 197:36a724675865 | 357 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 197:36a724675865 | 358 | int event = NoData; |
mbed_official | 197:36a724675865 | 359 | |
mbed_official | 197:36a724675865 | 360 | if(I2C_GetFlagStatus(i2c, I2C_ISR_BUSY) == SET) { |
mbed_official | 197:36a724675865 | 361 | if(I2C_GetFlagStatus(i2c, I2C_ISR_ADDR) == SET) { |
mbed_official | 197:36a724675865 | 362 | // Check direction |
mbed_official | 197:36a724675865 | 363 | if (I2C_GetFlagStatus(i2c, I2C_ISR_DIR) == SET) { |
mbed_official | 197:36a724675865 | 364 | event = ReadAddressed; |
mbed_official | 197:36a724675865 | 365 | } |
mbed_official | 197:36a724675865 | 366 | else event = WriteAddressed; |
mbed_official | 197:36a724675865 | 367 | // Clear adress match flag to generate an acknowledge |
mbed_official | 197:36a724675865 | 368 | i2c->ICR |= I2C_ICR_ADDRCF; |
mbed_official | 197:36a724675865 | 369 | } |
mbed_official | 197:36a724675865 | 370 | } |
mbed_official | 197:36a724675865 | 371 | return event; |
mbed_official | 125:23cc3068a9e4 | 372 | } |
mbed_official | 125:23cc3068a9e4 | 373 | |
mbed_official | 125:23cc3068a9e4 | 374 | int i2c_slave_read(i2c_t *obj, char *data, int length) { |
mbed_official | 125:23cc3068a9e4 | 375 | int count = 0; |
mbed_official | 135:067cc8ba23da | 376 | |
mbed_official | 125:23cc3068a9e4 | 377 | // Read all bytes |
mbed_official | 125:23cc3068a9e4 | 378 | for (count = 0; count < length; count++) { |
mbed_official | 125:23cc3068a9e4 | 379 | data[count] = i2c_byte_read(obj, 0); |
mbed_official | 125:23cc3068a9e4 | 380 | } |
mbed_official | 135:067cc8ba23da | 381 | |
mbed_official | 125:23cc3068a9e4 | 382 | return count; |
mbed_official | 125:23cc3068a9e4 | 383 | } |
mbed_official | 125:23cc3068a9e4 | 384 | |
mbed_official | 125:23cc3068a9e4 | 385 | int i2c_slave_write(i2c_t *obj, const char *data, int length) { |
mbed_official | 125:23cc3068a9e4 | 386 | int count = 0; |
mbed_official | 135:067cc8ba23da | 387 | |
mbed_official | 125:23cc3068a9e4 | 388 | // Write all bytes |
mbed_official | 125:23cc3068a9e4 | 389 | for (count = 0; count < length; count++) { |
mbed_official | 125:23cc3068a9e4 | 390 | i2c_byte_write(obj, data[count]); |
mbed_official | 125:23cc3068a9e4 | 391 | } |
mbed_official | 135:067cc8ba23da | 392 | |
mbed_official | 125:23cc3068a9e4 | 393 | return count; |
mbed_official | 125:23cc3068a9e4 | 394 | } |
mbed_official | 125:23cc3068a9e4 | 395 | |
mbed_official | 125:23cc3068a9e4 | 396 | |
mbed_official | 125:23cc3068a9e4 | 397 | #endif // DEVICE_I2CSLAVE |
mbed_official | 125:23cc3068a9e4 | 398 | |
mbed_official | 125:23cc3068a9e4 | 399 | #endif // DEVICE_I2C |