mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/i2c_api.c@56:99eb381a3269, 2013-12-12 (annotated)
- Committer:
- mbed_official
- Date:
- Thu Dec 12 10:45:05 2013 +0000
- Revision:
- 56:99eb381a3269
- Child:
- 57:c7e83bc5e387
Synchronized with git revision bd51e4eb73a1706f1d5379ec5cebcbd6d978cb4f
Full URL: https://github.com/mbedmicro/mbed/commit/bd51e4eb73a1706f1d5379ec5cebcbd6d978cb4f/
[NUCLEO_F103RB] Add I2C master, code cleanup, ...
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 56:99eb381a3269 | 1 | /* mbed Microcontroller Library |
mbed_official | 56:99eb381a3269 | 2 | * Copyright (c) 2006-2013 ARM Limited |
mbed_official | 56:99eb381a3269 | 3 | * |
mbed_official | 56:99eb381a3269 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 56:99eb381a3269 | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 56:99eb381a3269 | 6 | * You may obtain a copy of the License at |
mbed_official | 56:99eb381a3269 | 7 | * |
mbed_official | 56:99eb381a3269 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 56:99eb381a3269 | 9 | * |
mbed_official | 56:99eb381a3269 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 56:99eb381a3269 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 56:99eb381a3269 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 56:99eb381a3269 | 13 | * See the License for the specific language governing permissions and |
mbed_official | 56:99eb381a3269 | 14 | * limitations under the License. |
mbed_official | 56:99eb381a3269 | 15 | */ |
mbed_official | 56:99eb381a3269 | 16 | #include "i2c_api.h" |
mbed_official | 56:99eb381a3269 | 17 | |
mbed_official | 56:99eb381a3269 | 18 | #if DEVICE_I2C |
mbed_official | 56:99eb381a3269 | 19 | |
mbed_official | 56:99eb381a3269 | 20 | #include "cmsis.h" |
mbed_official | 56:99eb381a3269 | 21 | #include "pinmap.h" |
mbed_official | 56:99eb381a3269 | 22 | #include "error.h" |
mbed_official | 56:99eb381a3269 | 23 | |
mbed_official | 56:99eb381a3269 | 24 | /* Timeout values for flags and events waiting loops. These timeouts are |
mbed_official | 56:99eb381a3269 | 25 | not based on accurate values, they just guarantee that the application will |
mbed_official | 56:99eb381a3269 | 26 | not remain stuck if the I2C communication is corrupted. */ |
mbed_official | 56:99eb381a3269 | 27 | #define FLAG_TIMEOUT ((int)0x1000) |
mbed_official | 56:99eb381a3269 | 28 | #define LONG_TIMEOUT ((int)0x8000) |
mbed_official | 56:99eb381a3269 | 29 | |
mbed_official | 56:99eb381a3269 | 30 | // Functions exit codes |
mbed_official | 56:99eb381a3269 | 31 | #define EXIT_OK (0) |
mbed_official | 56:99eb381a3269 | 32 | #define EXIT_FAIL (1) |
mbed_official | 56:99eb381a3269 | 33 | #define EXIT_TIMEOUT (0xFFFFFFFF) |
mbed_official | 56:99eb381a3269 | 34 | |
mbed_official | 56:99eb381a3269 | 35 | static const PinMap PinMap_I2C_SDA[] = { |
mbed_official | 56:99eb381a3269 | 36 | //{PB_7, I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 0)}, // Cannot be used due to TIM4 |
mbed_official | 56:99eb381a3269 | 37 | {PB_9, I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 7)}, // GPIO_Remap_I2C1 |
mbed_official | 56:99eb381a3269 | 38 | {NC, NC, 0} |
mbed_official | 56:99eb381a3269 | 39 | }; |
mbed_official | 56:99eb381a3269 | 40 | |
mbed_official | 56:99eb381a3269 | 41 | static const PinMap PinMap_I2C_SCL[] = { |
mbed_official | 56:99eb381a3269 | 42 | //{PB_6, I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 0)}, // // Cannot be used due to TIM4 |
mbed_official | 56:99eb381a3269 | 43 | {PB_8, I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 7)}, // GPIO_Remap_I2C1 |
mbed_official | 56:99eb381a3269 | 44 | {NC, NC, 0} |
mbed_official | 56:99eb381a3269 | 45 | }; |
mbed_official | 56:99eb381a3269 | 46 | |
mbed_official | 56:99eb381a3269 | 47 | void i2c_init(i2c_t *obj, PinName sda, PinName scl) { |
mbed_official | 56:99eb381a3269 | 48 | // Determine the I2C to use |
mbed_official | 56:99eb381a3269 | 49 | I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); |
mbed_official | 56:99eb381a3269 | 50 | I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); |
mbed_official | 56:99eb381a3269 | 51 | |
mbed_official | 56:99eb381a3269 | 52 | obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); |
mbed_official | 56:99eb381a3269 | 53 | |
mbed_official | 56:99eb381a3269 | 54 | if (obj->i2c == (I2CName)NC) { |
mbed_official | 56:99eb381a3269 | 55 | error("I2C pin mapping failed"); |
mbed_official | 56:99eb381a3269 | 56 | } |
mbed_official | 56:99eb381a3269 | 57 | |
mbed_official | 56:99eb381a3269 | 58 | // Enable I2C clock |
mbed_official | 56:99eb381a3269 | 59 | if (obj->i2c == I2C_1) { |
mbed_official | 56:99eb381a3269 | 60 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); |
mbed_official | 56:99eb381a3269 | 61 | } |
mbed_official | 56:99eb381a3269 | 62 | if (obj->i2c == I2C_2) { |
mbed_official | 56:99eb381a3269 | 63 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE); |
mbed_official | 56:99eb381a3269 | 64 | } |
mbed_official | 56:99eb381a3269 | 65 | |
mbed_official | 56:99eb381a3269 | 66 | // Configure I2C pins |
mbed_official | 56:99eb381a3269 | 67 | pinmap_pinout(sda, PinMap_I2C_SDA); |
mbed_official | 56:99eb381a3269 | 68 | pinmap_pinout(scl, PinMap_I2C_SCL); |
mbed_official | 56:99eb381a3269 | 69 | pin_mode(sda, OpenDrain); |
mbed_official | 56:99eb381a3269 | 70 | pin_mode(scl, OpenDrain); |
mbed_official | 56:99eb381a3269 | 71 | |
mbed_official | 56:99eb381a3269 | 72 | // Reset to clear pending flags if any |
mbed_official | 56:99eb381a3269 | 73 | i2c_reset(obj); |
mbed_official | 56:99eb381a3269 | 74 | |
mbed_official | 56:99eb381a3269 | 75 | // I2C configuration |
mbed_official | 56:99eb381a3269 | 76 | i2c_frequency(obj, 100000); // 100 kHz per default |
mbed_official | 56:99eb381a3269 | 77 | } |
mbed_official | 56:99eb381a3269 | 78 | |
mbed_official | 56:99eb381a3269 | 79 | void i2c_frequency(i2c_t *obj, int hz) { |
mbed_official | 56:99eb381a3269 | 80 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 56:99eb381a3269 | 81 | I2C_InitTypeDef I2C_InitStructure; |
mbed_official | 56:99eb381a3269 | 82 | |
mbed_official | 56:99eb381a3269 | 83 | if ((hz != 0) && (hz <= 400000)) { |
mbed_official | 56:99eb381a3269 | 84 | // I2C configuration |
mbed_official | 56:99eb381a3269 | 85 | I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; |
mbed_official | 56:99eb381a3269 | 86 | I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; |
mbed_official | 56:99eb381a3269 | 87 | I2C_InitStructure.I2C_OwnAddress1 = 0; |
mbed_official | 56:99eb381a3269 | 88 | I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; |
mbed_official | 56:99eb381a3269 | 89 | I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; |
mbed_official | 56:99eb381a3269 | 90 | I2C_InitStructure.I2C_ClockSpeed = hz; |
mbed_official | 56:99eb381a3269 | 91 | I2C_Cmd(i2c, ENABLE); |
mbed_official | 56:99eb381a3269 | 92 | I2C_Init(i2c, &I2C_InitStructure); |
mbed_official | 56:99eb381a3269 | 93 | } |
mbed_official | 56:99eb381a3269 | 94 | } |
mbed_official | 56:99eb381a3269 | 95 | |
mbed_official | 56:99eb381a3269 | 96 | inline int i2c_start(i2c_t *obj) { |
mbed_official | 56:99eb381a3269 | 97 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 56:99eb381a3269 | 98 | int timeout; |
mbed_official | 56:99eb381a3269 | 99 | |
mbed_official | 56:99eb381a3269 | 100 | I2C_ClearFlag(i2c, I2C_FLAG_AF); // Clear Acknowledge failure flag |
mbed_official | 56:99eb381a3269 | 101 | |
mbed_official | 56:99eb381a3269 | 102 | // Generate the START condition |
mbed_official | 56:99eb381a3269 | 103 | I2C_GenerateSTART(i2c, ENABLE); |
mbed_official | 56:99eb381a3269 | 104 | |
mbed_official | 56:99eb381a3269 | 105 | // Wait the START condition has been correctly sent |
mbed_official | 56:99eb381a3269 | 106 | timeout = FLAG_TIMEOUT; |
mbed_official | 56:99eb381a3269 | 107 | //while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_MODE_SELECT) == ERROR) { |
mbed_official | 56:99eb381a3269 | 108 | while (I2C_GetFlagStatus(i2c, I2C_FLAG_SB) == RESET) { |
mbed_official | 56:99eb381a3269 | 109 | if ((timeout--) == 0) { |
mbed_official | 56:99eb381a3269 | 110 | return EXIT_TIMEOUT; |
mbed_official | 56:99eb381a3269 | 111 | } |
mbed_official | 56:99eb381a3269 | 112 | } |
mbed_official | 56:99eb381a3269 | 113 | |
mbed_official | 56:99eb381a3269 | 114 | return EXIT_OK; |
mbed_official | 56:99eb381a3269 | 115 | } |
mbed_official | 56:99eb381a3269 | 116 | |
mbed_official | 56:99eb381a3269 | 117 | inline int i2c_stop(i2c_t *obj) { |
mbed_official | 56:99eb381a3269 | 118 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 56:99eb381a3269 | 119 | I2C_GenerateSTOP(i2c, ENABLE); |
mbed_official | 56:99eb381a3269 | 120 | return EXIT_OK; |
mbed_official | 56:99eb381a3269 | 121 | } |
mbed_official | 56:99eb381a3269 | 122 | |
mbed_official | 56:99eb381a3269 | 123 | int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { |
mbed_official | 56:99eb381a3269 | 124 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 56:99eb381a3269 | 125 | int timeout; |
mbed_official | 56:99eb381a3269 | 126 | int count; |
mbed_official | 56:99eb381a3269 | 127 | int value; |
mbed_official | 56:99eb381a3269 | 128 | |
mbed_official | 56:99eb381a3269 | 129 | if (length == 0) return 0; |
mbed_official | 56:99eb381a3269 | 130 | |
mbed_official | 56:99eb381a3269 | 131 | /* |
mbed_official | 56:99eb381a3269 | 132 | // Wait until the bus is not busy anymore |
mbed_official | 56:99eb381a3269 | 133 | timeout = LONG_TIMEOUT; |
mbed_official | 56:99eb381a3269 | 134 | while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) { |
mbed_official | 56:99eb381a3269 | 135 | if ((timeout--) == 0) { |
mbed_official | 56:99eb381a3269 | 136 | return EXIT_TIMEOUT; |
mbed_official | 56:99eb381a3269 | 137 | } |
mbed_official | 56:99eb381a3269 | 138 | } |
mbed_official | 56:99eb381a3269 | 139 | */ |
mbed_official | 56:99eb381a3269 | 140 | |
mbed_official | 56:99eb381a3269 | 141 | i2c_start(obj); |
mbed_official | 56:99eb381a3269 | 142 | |
mbed_official | 56:99eb381a3269 | 143 | // Send slave address for read |
mbed_official | 56:99eb381a3269 | 144 | I2C_Send7bitAddress(i2c, address, I2C_Direction_Receiver); |
mbed_official | 56:99eb381a3269 | 145 | |
mbed_official | 56:99eb381a3269 | 146 | // Wait address is acknowledged |
mbed_official | 56:99eb381a3269 | 147 | timeout = FLAG_TIMEOUT; |
mbed_official | 56:99eb381a3269 | 148 | while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) == ERROR) { |
mbed_official | 56:99eb381a3269 | 149 | if ((timeout--) == 0) { |
mbed_official | 56:99eb381a3269 | 150 | return EXIT_TIMEOUT; |
mbed_official | 56:99eb381a3269 | 151 | } |
mbed_official | 56:99eb381a3269 | 152 | } |
mbed_official | 56:99eb381a3269 | 153 | |
mbed_official | 56:99eb381a3269 | 154 | // Read all bytes except last one |
mbed_official | 56:99eb381a3269 | 155 | for (count = 0; count < (length - 1); count++) { |
mbed_official | 56:99eb381a3269 | 156 | value = i2c_byte_read(obj, 0); |
mbed_official | 56:99eb381a3269 | 157 | data[count] = (char)value; |
mbed_official | 56:99eb381a3269 | 158 | } |
mbed_official | 56:99eb381a3269 | 159 | |
mbed_official | 56:99eb381a3269 | 160 | // If not repeated start, send stop. |
mbed_official | 56:99eb381a3269 | 161 | // Warning: must be done BEFORE the data is read. |
mbed_official | 56:99eb381a3269 | 162 | if (stop) { |
mbed_official | 56:99eb381a3269 | 163 | i2c_stop(obj); |
mbed_official | 56:99eb381a3269 | 164 | } |
mbed_official | 56:99eb381a3269 | 165 | |
mbed_official | 56:99eb381a3269 | 166 | // Read the last byte |
mbed_official | 56:99eb381a3269 | 167 | value = i2c_byte_read(obj, 1); |
mbed_official | 56:99eb381a3269 | 168 | data[count] = (char)value; |
mbed_official | 56:99eb381a3269 | 169 | |
mbed_official | 56:99eb381a3269 | 170 | return length; |
mbed_official | 56:99eb381a3269 | 171 | } |
mbed_official | 56:99eb381a3269 | 172 | |
mbed_official | 56:99eb381a3269 | 173 | int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { |
mbed_official | 56:99eb381a3269 | 174 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 56:99eb381a3269 | 175 | int timeout; |
mbed_official | 56:99eb381a3269 | 176 | int count; |
mbed_official | 56:99eb381a3269 | 177 | |
mbed_official | 56:99eb381a3269 | 178 | if (length == 0) return 0; |
mbed_official | 56:99eb381a3269 | 179 | |
mbed_official | 56:99eb381a3269 | 180 | /* |
mbed_official | 56:99eb381a3269 | 181 | // Wait until the bus is not busy anymore |
mbed_official | 56:99eb381a3269 | 182 | timeout = LONG_TIMEOUT; |
mbed_official | 56:99eb381a3269 | 183 | while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) { |
mbed_official | 56:99eb381a3269 | 184 | if ((timeout--) == 0) { |
mbed_official | 56:99eb381a3269 | 185 | return EXIT_TIMEOUT; |
mbed_official | 56:99eb381a3269 | 186 | } |
mbed_official | 56:99eb381a3269 | 187 | } |
mbed_official | 56:99eb381a3269 | 188 | */ |
mbed_official | 56:99eb381a3269 | 189 | |
mbed_official | 56:99eb381a3269 | 190 | i2c_start(obj); |
mbed_official | 56:99eb381a3269 | 191 | |
mbed_official | 56:99eb381a3269 | 192 | // Send slave address for write |
mbed_official | 56:99eb381a3269 | 193 | I2C_Send7bitAddress(i2c, address, I2C_Direction_Transmitter); |
mbed_official | 56:99eb381a3269 | 194 | |
mbed_official | 56:99eb381a3269 | 195 | // Wait address is acknowledged |
mbed_official | 56:99eb381a3269 | 196 | timeout = FLAG_TIMEOUT; |
mbed_official | 56:99eb381a3269 | 197 | while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) == ERROR) { |
mbed_official | 56:99eb381a3269 | 198 | if ((timeout--) == 0) { |
mbed_official | 56:99eb381a3269 | 199 | return EXIT_TIMEOUT; |
mbed_official | 56:99eb381a3269 | 200 | } |
mbed_official | 56:99eb381a3269 | 201 | } |
mbed_official | 56:99eb381a3269 | 202 | |
mbed_official | 56:99eb381a3269 | 203 | for (count = 0; count < length; count++) { |
mbed_official | 56:99eb381a3269 | 204 | if (i2c_byte_write(obj, data[count]) != EXIT_OK) { |
mbed_official | 56:99eb381a3269 | 205 | return EXIT_FAIL; |
mbed_official | 56:99eb381a3269 | 206 | } |
mbed_official | 56:99eb381a3269 | 207 | } |
mbed_official | 56:99eb381a3269 | 208 | |
mbed_official | 56:99eb381a3269 | 209 | // If not repeated start, send stop. |
mbed_official | 56:99eb381a3269 | 210 | if (stop) { |
mbed_official | 56:99eb381a3269 | 211 | i2c_stop(obj); |
mbed_official | 56:99eb381a3269 | 212 | } |
mbed_official | 56:99eb381a3269 | 213 | |
mbed_official | 56:99eb381a3269 | 214 | return count; |
mbed_official | 56:99eb381a3269 | 215 | } |
mbed_official | 56:99eb381a3269 | 216 | |
mbed_official | 56:99eb381a3269 | 217 | int i2c_byte_read(i2c_t *obj, int last) { |
mbed_official | 56:99eb381a3269 | 218 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 56:99eb381a3269 | 219 | uint8_t data; |
mbed_official | 56:99eb381a3269 | 220 | int timeout; |
mbed_official | 56:99eb381a3269 | 221 | |
mbed_official | 56:99eb381a3269 | 222 | if (last) { |
mbed_official | 56:99eb381a3269 | 223 | // Don't acknowledge the last byte |
mbed_official | 56:99eb381a3269 | 224 | I2C_AcknowledgeConfig(i2c, DISABLE); |
mbed_official | 56:99eb381a3269 | 225 | } else { |
mbed_official | 56:99eb381a3269 | 226 | // Acknowledge the byte |
mbed_official | 56:99eb381a3269 | 227 | I2C_AcknowledgeConfig(i2c, ENABLE); |
mbed_official | 56:99eb381a3269 | 228 | } |
mbed_official | 56:99eb381a3269 | 229 | |
mbed_official | 56:99eb381a3269 | 230 | // Wait until the byte is received |
mbed_official | 56:99eb381a3269 | 231 | timeout = FLAG_TIMEOUT; |
mbed_official | 56:99eb381a3269 | 232 | while (I2C_GetFlagStatus(i2c, I2C_FLAG_RXNE) == RESET) { |
mbed_official | 56:99eb381a3269 | 233 | if ((timeout--) == 0) { |
mbed_official | 56:99eb381a3269 | 234 | return EXIT_TIMEOUT; |
mbed_official | 56:99eb381a3269 | 235 | } |
mbed_official | 56:99eb381a3269 | 236 | } |
mbed_official | 56:99eb381a3269 | 237 | |
mbed_official | 56:99eb381a3269 | 238 | data = I2C_ReceiveData(i2c); |
mbed_official | 56:99eb381a3269 | 239 | |
mbed_official | 56:99eb381a3269 | 240 | return (int)data; |
mbed_official | 56:99eb381a3269 | 241 | } |
mbed_official | 56:99eb381a3269 | 242 | |
mbed_official | 56:99eb381a3269 | 243 | int i2c_byte_write(i2c_t *obj, int data) { |
mbed_official | 56:99eb381a3269 | 244 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 56:99eb381a3269 | 245 | int timeout; |
mbed_official | 56:99eb381a3269 | 246 | |
mbed_official | 56:99eb381a3269 | 247 | I2C_SendData(i2c, (uint8_t)data); |
mbed_official | 56:99eb381a3269 | 248 | |
mbed_official | 56:99eb381a3269 | 249 | // Wait until the byte is transmitted |
mbed_official | 56:99eb381a3269 | 250 | timeout = FLAG_TIMEOUT; |
mbed_official | 56:99eb381a3269 | 251 | //while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_BYTE_TRANSMITTED) == ERROR) { |
mbed_official | 56:99eb381a3269 | 252 | while ((I2C_GetFlagStatus(i2c, I2C_FLAG_TXE) == RESET) && |
mbed_official | 56:99eb381a3269 | 253 | (I2C_GetFlagStatus(i2c, I2C_FLAG_BTF) == RESET)) { |
mbed_official | 56:99eb381a3269 | 254 | if ((timeout--) == 0) { |
mbed_official | 56:99eb381a3269 | 255 | return EXIT_TIMEOUT; |
mbed_official | 56:99eb381a3269 | 256 | } |
mbed_official | 56:99eb381a3269 | 257 | } |
mbed_official | 56:99eb381a3269 | 258 | |
mbed_official | 56:99eb381a3269 | 259 | return EXIT_OK; |
mbed_official | 56:99eb381a3269 | 260 | } |
mbed_official | 56:99eb381a3269 | 261 | |
mbed_official | 56:99eb381a3269 | 262 | void i2c_reset(i2c_t *obj) { |
mbed_official | 56:99eb381a3269 | 263 | if (obj->i2c == I2C_1) { |
mbed_official | 56:99eb381a3269 | 264 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE); |
mbed_official | 56:99eb381a3269 | 265 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE); |
mbed_official | 56:99eb381a3269 | 266 | } |
mbed_official | 56:99eb381a3269 | 267 | if (obj->i2c == I2C_2) { |
mbed_official | 56:99eb381a3269 | 268 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE); |
mbed_official | 56:99eb381a3269 | 269 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE); |
mbed_official | 56:99eb381a3269 | 270 | } |
mbed_official | 56:99eb381a3269 | 271 | } |
mbed_official | 56:99eb381a3269 | 272 | |
mbed_official | 56:99eb381a3269 | 273 | #if DEVICE_I2CSLAVE |
mbed_official | 56:99eb381a3269 | 274 | |
mbed_official | 56:99eb381a3269 | 275 | void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) { |
mbed_official | 56:99eb381a3269 | 276 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 56:99eb381a3269 | 277 | uint16_t tmpreg; |
mbed_official | 56:99eb381a3269 | 278 | |
mbed_official | 56:99eb381a3269 | 279 | // Get the old register value |
mbed_official | 56:99eb381a3269 | 280 | tmpreg = i2c->OAR1; |
mbed_official | 56:99eb381a3269 | 281 | // Reset address bits |
mbed_official | 56:99eb381a3269 | 282 | tmpreg &= 0xFC00; |
mbed_official | 56:99eb381a3269 | 283 | // Set new address |
mbed_official | 56:99eb381a3269 | 284 | tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits |
mbed_official | 56:99eb381a3269 | 285 | // Store the new register value |
mbed_official | 56:99eb381a3269 | 286 | i2c->OAR1 = tmpreg; |
mbed_official | 56:99eb381a3269 | 287 | } |
mbed_official | 56:99eb381a3269 | 288 | |
mbed_official | 56:99eb381a3269 | 289 | void i2c_slave_mode(i2c_t *obj, int enable_slave) { |
mbed_official | 56:99eb381a3269 | 290 | // Nothing to do |
mbed_official | 56:99eb381a3269 | 291 | } |
mbed_official | 56:99eb381a3269 | 292 | |
mbed_official | 56:99eb381a3269 | 293 | #define NoData 0 |
mbed_official | 56:99eb381a3269 | 294 | #define ReadAddressed 1 |
mbed_official | 56:99eb381a3269 | 295 | #define WriteGeneral 2 |
mbed_official | 56:99eb381a3269 | 296 | #define WriteAddressed 3 |
mbed_official | 56:99eb381a3269 | 297 | |
mbed_official | 56:99eb381a3269 | 298 | int i2c_slave_receive(i2c_t *obj) { |
mbed_official | 56:99eb381a3269 | 299 | //I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 56:99eb381a3269 | 300 | int retval = NoData; |
mbed_official | 56:99eb381a3269 | 301 | //int status; |
mbed_official | 56:99eb381a3269 | 302 | |
mbed_official | 56:99eb381a3269 | 303 | //if (I2C_GetFlagStatus(i2c, I2C_FLAG_GENCALL) == SET) retval = WriteGeneral; |
mbed_official | 56:99eb381a3269 | 304 | |
mbed_official | 56:99eb381a3269 | 305 | //status = I2C_GetLastEvent(i2c); |
mbed_official | 56:99eb381a3269 | 306 | |
mbed_official | 56:99eb381a3269 | 307 | return(retval); |
mbed_official | 56:99eb381a3269 | 308 | } |
mbed_official | 56:99eb381a3269 | 309 | |
mbed_official | 56:99eb381a3269 | 310 | int i2c_slave_read(i2c_t *obj, char *data, int length) { |
mbed_official | 56:99eb381a3269 | 311 | return 0; |
mbed_official | 56:99eb381a3269 | 312 | } |
mbed_official | 56:99eb381a3269 | 313 | |
mbed_official | 56:99eb381a3269 | 314 | int i2c_slave_write(i2c_t *obj, const char *data, int length) { |
mbed_official | 56:99eb381a3269 | 315 | return 0; |
mbed_official | 56:99eb381a3269 | 316 | } |
mbed_official | 56:99eb381a3269 | 317 | |
mbed_official | 56:99eb381a3269 | 318 | |
mbed_official | 56:99eb381a3269 | 319 | #endif // DEVICE_I2CSLAVE |
mbed_official | 56:99eb381a3269 | 320 | |
mbed_official | 56:99eb381a3269 | 321 | #endif // DEVICE_I2C |