mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/i2c_api.c@58:3b55b7a41411, 2013-12-13 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Dec 13 09:30:05 2013 +0000
- Revision:
- 58:3b55b7a41411
- Parent:
- 57:c7e83bc5e387
- Child:
- 70:c1fbde68b492
Synchronized with git revision da605b82a5ca02e18b987f366969d615bec94035
Full URL: https://github.com/mbedmicro/mbed/commit/da605b82a5ca02e18b987f366969d615bec94035/
[NUCLEO_F103RB] Update PWM IOs used + I2C 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 | static const PinMap PinMap_I2C_SDA[] = { |
mbed_official | 58:3b55b7a41411 | 31 | {PB_9, I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 8)}, // GPIO_Remap_I2C1 |
mbed_official | 56:99eb381a3269 | 32 | {NC, NC, 0} |
mbed_official | 56:99eb381a3269 | 33 | }; |
mbed_official | 56:99eb381a3269 | 34 | |
mbed_official | 56:99eb381a3269 | 35 | static const PinMap PinMap_I2C_SCL[] = { |
mbed_official | 58:3b55b7a41411 | 36 | {PB_8, I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 8)}, // GPIO_Remap_I2C1 |
mbed_official | 56:99eb381a3269 | 37 | {NC, NC, 0} |
mbed_official | 56:99eb381a3269 | 38 | }; |
mbed_official | 56:99eb381a3269 | 39 | |
mbed_official | 56:99eb381a3269 | 40 | void i2c_init(i2c_t *obj, PinName sda, PinName scl) { |
mbed_official | 56:99eb381a3269 | 41 | // Determine the I2C to use |
mbed_official | 56:99eb381a3269 | 42 | I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); |
mbed_official | 56:99eb381a3269 | 43 | I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); |
mbed_official | 56:99eb381a3269 | 44 | |
mbed_official | 56:99eb381a3269 | 45 | obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); |
mbed_official | 56:99eb381a3269 | 46 | |
mbed_official | 56:99eb381a3269 | 47 | if (obj->i2c == (I2CName)NC) { |
mbed_official | 56:99eb381a3269 | 48 | error("I2C pin mapping failed"); |
mbed_official | 56:99eb381a3269 | 49 | } |
mbed_official | 56:99eb381a3269 | 50 | |
mbed_official | 56:99eb381a3269 | 51 | // Enable I2C clock |
mbed_official | 56:99eb381a3269 | 52 | if (obj->i2c == I2C_1) { |
mbed_official | 56:99eb381a3269 | 53 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); |
mbed_official | 56:99eb381a3269 | 54 | } |
mbed_official | 56:99eb381a3269 | 55 | if (obj->i2c == I2C_2) { |
mbed_official | 56:99eb381a3269 | 56 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE); |
mbed_official | 56:99eb381a3269 | 57 | } |
mbed_official | 56:99eb381a3269 | 58 | |
mbed_official | 56:99eb381a3269 | 59 | // Configure I2C pins |
mbed_official | 56:99eb381a3269 | 60 | pinmap_pinout(sda, PinMap_I2C_SDA); |
mbed_official | 56:99eb381a3269 | 61 | pinmap_pinout(scl, PinMap_I2C_SCL); |
mbed_official | 56:99eb381a3269 | 62 | pin_mode(sda, OpenDrain); |
mbed_official | 56:99eb381a3269 | 63 | pin_mode(scl, OpenDrain); |
mbed_official | 56:99eb381a3269 | 64 | |
mbed_official | 56:99eb381a3269 | 65 | // Reset to clear pending flags if any |
mbed_official | 56:99eb381a3269 | 66 | i2c_reset(obj); |
mbed_official | 56:99eb381a3269 | 67 | |
mbed_official | 56:99eb381a3269 | 68 | // I2C configuration |
mbed_official | 56:99eb381a3269 | 69 | i2c_frequency(obj, 100000); // 100 kHz per default |
mbed_official | 56:99eb381a3269 | 70 | } |
mbed_official | 56:99eb381a3269 | 71 | |
mbed_official | 56:99eb381a3269 | 72 | void i2c_frequency(i2c_t *obj, int hz) { |
mbed_official | 56:99eb381a3269 | 73 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 56:99eb381a3269 | 74 | I2C_InitTypeDef I2C_InitStructure; |
mbed_official | 56:99eb381a3269 | 75 | |
mbed_official | 56:99eb381a3269 | 76 | if ((hz != 0) && (hz <= 400000)) { |
mbed_official | 56:99eb381a3269 | 77 | // I2C configuration |
mbed_official | 56:99eb381a3269 | 78 | I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; |
mbed_official | 56:99eb381a3269 | 79 | I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; |
mbed_official | 56:99eb381a3269 | 80 | I2C_InitStructure.I2C_OwnAddress1 = 0; |
mbed_official | 56:99eb381a3269 | 81 | I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; |
mbed_official | 56:99eb381a3269 | 82 | I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; |
mbed_official | 56:99eb381a3269 | 83 | I2C_InitStructure.I2C_ClockSpeed = hz; |
mbed_official | 56:99eb381a3269 | 84 | I2C_Cmd(i2c, ENABLE); |
mbed_official | 56:99eb381a3269 | 85 | I2C_Init(i2c, &I2C_InitStructure); |
mbed_official | 56:99eb381a3269 | 86 | } |
mbed_official | 56:99eb381a3269 | 87 | } |
mbed_official | 56:99eb381a3269 | 88 | |
mbed_official | 56:99eb381a3269 | 89 | inline int i2c_start(i2c_t *obj) { |
mbed_official | 56:99eb381a3269 | 90 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 56:99eb381a3269 | 91 | int timeout; |
mbed_official | 56:99eb381a3269 | 92 | |
mbed_official | 56:99eb381a3269 | 93 | I2C_ClearFlag(i2c, I2C_FLAG_AF); // Clear Acknowledge failure flag |
mbed_official | 56:99eb381a3269 | 94 | |
mbed_official | 56:99eb381a3269 | 95 | // Generate the START condition |
mbed_official | 56:99eb381a3269 | 96 | I2C_GenerateSTART(i2c, ENABLE); |
mbed_official | 56:99eb381a3269 | 97 | |
mbed_official | 56:99eb381a3269 | 98 | // Wait the START condition has been correctly sent |
mbed_official | 56:99eb381a3269 | 99 | timeout = FLAG_TIMEOUT; |
mbed_official | 56:99eb381a3269 | 100 | //while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_MODE_SELECT) == ERROR) { |
mbed_official | 56:99eb381a3269 | 101 | while (I2C_GetFlagStatus(i2c, I2C_FLAG_SB) == RESET) { |
mbed_official | 56:99eb381a3269 | 102 | if ((timeout--) == 0) { |
mbed_official | 58:3b55b7a41411 | 103 | return 1; |
mbed_official | 56:99eb381a3269 | 104 | } |
mbed_official | 56:99eb381a3269 | 105 | } |
mbed_official | 56:99eb381a3269 | 106 | |
mbed_official | 58:3b55b7a41411 | 107 | return 0; |
mbed_official | 56:99eb381a3269 | 108 | } |
mbed_official | 56:99eb381a3269 | 109 | |
mbed_official | 56:99eb381a3269 | 110 | inline int i2c_stop(i2c_t *obj) { |
mbed_official | 56:99eb381a3269 | 111 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 58:3b55b7a41411 | 112 | |
mbed_official | 56:99eb381a3269 | 113 | I2C_GenerateSTOP(i2c, ENABLE); |
mbed_official | 58:3b55b7a41411 | 114 | |
mbed_official | 58:3b55b7a41411 | 115 | return 0; |
mbed_official | 56:99eb381a3269 | 116 | } |
mbed_official | 56:99eb381a3269 | 117 | |
mbed_official | 56:99eb381a3269 | 118 | int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { |
mbed_official | 56:99eb381a3269 | 119 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 56:99eb381a3269 | 120 | int timeout; |
mbed_official | 56:99eb381a3269 | 121 | int count; |
mbed_official | 56:99eb381a3269 | 122 | int value; |
mbed_official | 56:99eb381a3269 | 123 | |
mbed_official | 56:99eb381a3269 | 124 | if (length == 0) return 0; |
mbed_official | 56:99eb381a3269 | 125 | |
mbed_official | 56:99eb381a3269 | 126 | /* |
mbed_official | 56:99eb381a3269 | 127 | // Wait until the bus is not busy anymore |
mbed_official | 56:99eb381a3269 | 128 | timeout = LONG_TIMEOUT; |
mbed_official | 56:99eb381a3269 | 129 | while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) { |
mbed_official | 56:99eb381a3269 | 130 | if ((timeout--) == 0) { |
mbed_official | 58:3b55b7a41411 | 131 | return 0; |
mbed_official | 56:99eb381a3269 | 132 | } |
mbed_official | 56:99eb381a3269 | 133 | } |
mbed_official | 56:99eb381a3269 | 134 | */ |
mbed_official | 56:99eb381a3269 | 135 | |
mbed_official | 56:99eb381a3269 | 136 | i2c_start(obj); |
mbed_official | 56:99eb381a3269 | 137 | |
mbed_official | 56:99eb381a3269 | 138 | // Send slave address for read |
mbed_official | 56:99eb381a3269 | 139 | I2C_Send7bitAddress(i2c, address, I2C_Direction_Receiver); |
mbed_official | 56:99eb381a3269 | 140 | |
mbed_official | 56:99eb381a3269 | 141 | // Wait address is acknowledged |
mbed_official | 56:99eb381a3269 | 142 | timeout = FLAG_TIMEOUT; |
mbed_official | 56:99eb381a3269 | 143 | while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) == ERROR) { |
mbed_official | 56:99eb381a3269 | 144 | if ((timeout--) == 0) { |
mbed_official | 58:3b55b7a41411 | 145 | return 0; |
mbed_official | 56:99eb381a3269 | 146 | } |
mbed_official | 56:99eb381a3269 | 147 | } |
mbed_official | 56:99eb381a3269 | 148 | |
mbed_official | 56:99eb381a3269 | 149 | // Read all bytes except last one |
mbed_official | 56:99eb381a3269 | 150 | for (count = 0; count < (length - 1); count++) { |
mbed_official | 56:99eb381a3269 | 151 | value = i2c_byte_read(obj, 0); |
mbed_official | 56:99eb381a3269 | 152 | data[count] = (char)value; |
mbed_official | 56:99eb381a3269 | 153 | } |
mbed_official | 56:99eb381a3269 | 154 | |
mbed_official | 56:99eb381a3269 | 155 | // If not repeated start, send stop. |
mbed_official | 56:99eb381a3269 | 156 | // Warning: must be done BEFORE the data is read. |
mbed_official | 56:99eb381a3269 | 157 | if (stop) { |
mbed_official | 56:99eb381a3269 | 158 | i2c_stop(obj); |
mbed_official | 56:99eb381a3269 | 159 | } |
mbed_official | 56:99eb381a3269 | 160 | |
mbed_official | 56:99eb381a3269 | 161 | // Read the last byte |
mbed_official | 56:99eb381a3269 | 162 | value = i2c_byte_read(obj, 1); |
mbed_official | 56:99eb381a3269 | 163 | data[count] = (char)value; |
mbed_official | 56:99eb381a3269 | 164 | |
mbed_official | 56:99eb381a3269 | 165 | return length; |
mbed_official | 56:99eb381a3269 | 166 | } |
mbed_official | 56:99eb381a3269 | 167 | |
mbed_official | 56:99eb381a3269 | 168 | int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { |
mbed_official | 56:99eb381a3269 | 169 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 56:99eb381a3269 | 170 | int timeout; |
mbed_official | 56:99eb381a3269 | 171 | int count; |
mbed_official | 56:99eb381a3269 | 172 | |
mbed_official | 56:99eb381a3269 | 173 | /* |
mbed_official | 56:99eb381a3269 | 174 | // Wait until the bus is not busy anymore |
mbed_official | 56:99eb381a3269 | 175 | timeout = LONG_TIMEOUT; |
mbed_official | 56:99eb381a3269 | 176 | while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) { |
mbed_official | 56:99eb381a3269 | 177 | if ((timeout--) == 0) { |
mbed_official | 58:3b55b7a41411 | 178 | return 0; |
mbed_official | 56:99eb381a3269 | 179 | } |
mbed_official | 56:99eb381a3269 | 180 | } |
mbed_official | 56:99eb381a3269 | 181 | */ |
mbed_official | 56:99eb381a3269 | 182 | |
mbed_official | 56:99eb381a3269 | 183 | i2c_start(obj); |
mbed_official | 56:99eb381a3269 | 184 | |
mbed_official | 56:99eb381a3269 | 185 | // Send slave address for write |
mbed_official | 56:99eb381a3269 | 186 | I2C_Send7bitAddress(i2c, address, I2C_Direction_Transmitter); |
mbed_official | 56:99eb381a3269 | 187 | |
mbed_official | 56:99eb381a3269 | 188 | // Wait address is acknowledged |
mbed_official | 56:99eb381a3269 | 189 | timeout = FLAG_TIMEOUT; |
mbed_official | 56:99eb381a3269 | 190 | while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) == ERROR) { |
mbed_official | 56:99eb381a3269 | 191 | if ((timeout--) == 0) { |
mbed_official | 58:3b55b7a41411 | 192 | return 0; |
mbed_official | 56:99eb381a3269 | 193 | } |
mbed_official | 56:99eb381a3269 | 194 | } |
mbed_official | 56:99eb381a3269 | 195 | |
mbed_official | 56:99eb381a3269 | 196 | for (count = 0; count < length; count++) { |
mbed_official | 58:3b55b7a41411 | 197 | if (i2c_byte_write(obj, data[count]) != 1) { |
mbed_official | 58:3b55b7a41411 | 198 | i2c_stop(obj); |
mbed_official | 58:3b55b7a41411 | 199 | return 0; |
mbed_official | 56:99eb381a3269 | 200 | } |
mbed_official | 56:99eb381a3269 | 201 | } |
mbed_official | 56:99eb381a3269 | 202 | |
mbed_official | 56:99eb381a3269 | 203 | // If not repeated start, send stop. |
mbed_official | 56:99eb381a3269 | 204 | if (stop) { |
mbed_official | 56:99eb381a3269 | 205 | i2c_stop(obj); |
mbed_official | 56:99eb381a3269 | 206 | } |
mbed_official | 56:99eb381a3269 | 207 | |
mbed_official | 56:99eb381a3269 | 208 | return count; |
mbed_official | 56:99eb381a3269 | 209 | } |
mbed_official | 56:99eb381a3269 | 210 | |
mbed_official | 56:99eb381a3269 | 211 | int i2c_byte_read(i2c_t *obj, int last) { |
mbed_official | 56:99eb381a3269 | 212 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 56:99eb381a3269 | 213 | uint8_t data; |
mbed_official | 56:99eb381a3269 | 214 | int timeout; |
mbed_official | 56:99eb381a3269 | 215 | |
mbed_official | 56:99eb381a3269 | 216 | if (last) { |
mbed_official | 56:99eb381a3269 | 217 | // Don't acknowledge the last byte |
mbed_official | 56:99eb381a3269 | 218 | I2C_AcknowledgeConfig(i2c, DISABLE); |
mbed_official | 56:99eb381a3269 | 219 | } else { |
mbed_official | 56:99eb381a3269 | 220 | // Acknowledge the byte |
mbed_official | 56:99eb381a3269 | 221 | I2C_AcknowledgeConfig(i2c, ENABLE); |
mbed_official | 56:99eb381a3269 | 222 | } |
mbed_official | 56:99eb381a3269 | 223 | |
mbed_official | 56:99eb381a3269 | 224 | // Wait until the byte is received |
mbed_official | 56:99eb381a3269 | 225 | timeout = FLAG_TIMEOUT; |
mbed_official | 56:99eb381a3269 | 226 | while (I2C_GetFlagStatus(i2c, I2C_FLAG_RXNE) == RESET) { |
mbed_official | 56:99eb381a3269 | 227 | if ((timeout--) == 0) { |
mbed_official | 58:3b55b7a41411 | 228 | return 0; |
mbed_official | 56:99eb381a3269 | 229 | } |
mbed_official | 56:99eb381a3269 | 230 | } |
mbed_official | 56:99eb381a3269 | 231 | |
mbed_official | 56:99eb381a3269 | 232 | data = I2C_ReceiveData(i2c); |
mbed_official | 56:99eb381a3269 | 233 | |
mbed_official | 56:99eb381a3269 | 234 | return (int)data; |
mbed_official | 56:99eb381a3269 | 235 | } |
mbed_official | 56:99eb381a3269 | 236 | |
mbed_official | 56:99eb381a3269 | 237 | int i2c_byte_write(i2c_t *obj, int data) { |
mbed_official | 56:99eb381a3269 | 238 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 56:99eb381a3269 | 239 | int timeout; |
mbed_official | 56:99eb381a3269 | 240 | |
mbed_official | 56:99eb381a3269 | 241 | I2C_SendData(i2c, (uint8_t)data); |
mbed_official | 56:99eb381a3269 | 242 | |
mbed_official | 56:99eb381a3269 | 243 | // Wait until the byte is transmitted |
mbed_official | 56:99eb381a3269 | 244 | timeout = FLAG_TIMEOUT; |
mbed_official | 56:99eb381a3269 | 245 | //while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_BYTE_TRANSMITTED) == ERROR) { |
mbed_official | 56:99eb381a3269 | 246 | while ((I2C_GetFlagStatus(i2c, I2C_FLAG_TXE) == RESET) && |
mbed_official | 56:99eb381a3269 | 247 | (I2C_GetFlagStatus(i2c, I2C_FLAG_BTF) == RESET)) { |
mbed_official | 56:99eb381a3269 | 248 | if ((timeout--) == 0) { |
mbed_official | 58:3b55b7a41411 | 249 | return 0; |
mbed_official | 56:99eb381a3269 | 250 | } |
mbed_official | 56:99eb381a3269 | 251 | } |
mbed_official | 56:99eb381a3269 | 252 | |
mbed_official | 58:3b55b7a41411 | 253 | return 1; |
mbed_official | 56:99eb381a3269 | 254 | } |
mbed_official | 56:99eb381a3269 | 255 | |
mbed_official | 56:99eb381a3269 | 256 | void i2c_reset(i2c_t *obj) { |
mbed_official | 56:99eb381a3269 | 257 | if (obj->i2c == I2C_1) { |
mbed_official | 56:99eb381a3269 | 258 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE); |
mbed_official | 56:99eb381a3269 | 259 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE); |
mbed_official | 56:99eb381a3269 | 260 | } |
mbed_official | 56:99eb381a3269 | 261 | if (obj->i2c == I2C_2) { |
mbed_official | 56:99eb381a3269 | 262 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE); |
mbed_official | 56:99eb381a3269 | 263 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE); |
mbed_official | 56:99eb381a3269 | 264 | } |
mbed_official | 56:99eb381a3269 | 265 | } |
mbed_official | 56:99eb381a3269 | 266 | |
mbed_official | 56:99eb381a3269 | 267 | #if DEVICE_I2CSLAVE |
mbed_official | 56:99eb381a3269 | 268 | |
mbed_official | 56:99eb381a3269 | 269 | void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) { |
mbed_official | 56:99eb381a3269 | 270 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 56:99eb381a3269 | 271 | uint16_t tmpreg; |
mbed_official | 56:99eb381a3269 | 272 | |
mbed_official | 56:99eb381a3269 | 273 | // Get the old register value |
mbed_official | 56:99eb381a3269 | 274 | tmpreg = i2c->OAR1; |
mbed_official | 56:99eb381a3269 | 275 | // Reset address bits |
mbed_official | 56:99eb381a3269 | 276 | tmpreg &= 0xFC00; |
mbed_official | 56:99eb381a3269 | 277 | // Set new address |
mbed_official | 56:99eb381a3269 | 278 | tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits |
mbed_official | 56:99eb381a3269 | 279 | // Store the new register value |
mbed_official | 56:99eb381a3269 | 280 | i2c->OAR1 = tmpreg; |
mbed_official | 56:99eb381a3269 | 281 | } |
mbed_official | 56:99eb381a3269 | 282 | |
mbed_official | 56:99eb381a3269 | 283 | void i2c_slave_mode(i2c_t *obj, int enable_slave) { |
mbed_official | 56:99eb381a3269 | 284 | // Nothing to do |
mbed_official | 56:99eb381a3269 | 285 | } |
mbed_official | 56:99eb381a3269 | 286 | |
mbed_official | 58:3b55b7a41411 | 287 | // See I2CSlave.h |
mbed_official | 58:3b55b7a41411 | 288 | #define NoData 0 // the slave has not been addressed |
mbed_official | 58:3b55b7a41411 | 289 | #define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter) |
mbed_official | 58:3b55b7a41411 | 290 | #define WriteGeneral 2 // the master is writing to all slave |
mbed_official | 58:3b55b7a41411 | 291 | #define WriteAddressed 3 // the master is writing to this slave (slave = receiver) |
mbed_official | 56:99eb381a3269 | 292 | |
mbed_official | 56:99eb381a3269 | 293 | int i2c_slave_receive(i2c_t *obj) { |
mbed_official | 58:3b55b7a41411 | 294 | // TO BE DONE |
mbed_official | 58:3b55b7a41411 | 295 | return(0); |
mbed_official | 56:99eb381a3269 | 296 | } |
mbed_official | 56:99eb381a3269 | 297 | |
mbed_official | 56:99eb381a3269 | 298 | int i2c_slave_read(i2c_t *obj, char *data, int length) { |
mbed_official | 58:3b55b7a41411 | 299 | int count = 0; |
mbed_official | 58:3b55b7a41411 | 300 | |
mbed_official | 58:3b55b7a41411 | 301 | // Read all bytes |
mbed_official | 58:3b55b7a41411 | 302 | for (count = 0; count < length; count++) { |
mbed_official | 58:3b55b7a41411 | 303 | data[count] = i2c_byte_read(obj, 0); |
mbed_official | 58:3b55b7a41411 | 304 | } |
mbed_official | 58:3b55b7a41411 | 305 | |
mbed_official | 58:3b55b7a41411 | 306 | return count; |
mbed_official | 56:99eb381a3269 | 307 | } |
mbed_official | 56:99eb381a3269 | 308 | |
mbed_official | 56:99eb381a3269 | 309 | int i2c_slave_write(i2c_t *obj, const char *data, int length) { |
mbed_official | 58:3b55b7a41411 | 310 | int count = 0; |
mbed_official | 58:3b55b7a41411 | 311 | |
mbed_official | 58:3b55b7a41411 | 312 | // Write all bytes |
mbed_official | 58:3b55b7a41411 | 313 | for (count = 0; count < length; count++) { |
mbed_official | 58:3b55b7a41411 | 314 | i2c_byte_write(obj, data[count]); |
mbed_official | 58:3b55b7a41411 | 315 | } |
mbed_official | 58:3b55b7a41411 | 316 | |
mbed_official | 58:3b55b7a41411 | 317 | return count; |
mbed_official | 56:99eb381a3269 | 318 | } |
mbed_official | 56:99eb381a3269 | 319 | |
mbed_official | 56:99eb381a3269 | 320 | |
mbed_official | 56:99eb381a3269 | 321 | #endif // DEVICE_I2CSLAVE |
mbed_official | 56:99eb381a3269 | 322 | |
mbed_official | 56:99eb381a3269 | 323 | #endif // DEVICE_I2C |