mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/i2c_api.c@80:66393a7b209d, 2014-01-30 (annotated)
- Committer:
- mbed_official
- Date:
- Thu Jan 30 12:15:05 2014 +0000
- Revision:
- 80:66393a7b209d
- Child:
- 84:f54042cbc282
Synchronized with git revision dba523f83fe09b7fce11fc1299dd1216e9776359
Full URL: https://github.com/mbedmicro/mbed/commit/dba523f83fe09b7fce11fc1299dd1216e9776359/
Update of I2C, SPI, SLEEP for NUCLEO_F103RB and L152RE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 80:66393a7b209d | 1 | /* mbed Microcontroller Library |
mbed_official | 80:66393a7b209d | 2 | ******************************************************************************* |
mbed_official | 80:66393a7b209d | 3 | * Copyright (c) 2014, STMicroelectronics |
mbed_official | 80:66393a7b209d | 4 | * All rights reserved. |
mbed_official | 80:66393a7b209d | 5 | * |
mbed_official | 80:66393a7b209d | 6 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 80:66393a7b209d | 7 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 80:66393a7b209d | 8 | * |
mbed_official | 80:66393a7b209d | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 80:66393a7b209d | 10 | * this list of conditions and the following disclaimer. |
mbed_official | 80:66393a7b209d | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 80:66393a7b209d | 12 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 80:66393a7b209d | 13 | * and/or other materials provided with the distribution. |
mbed_official | 80:66393a7b209d | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 80:66393a7b209d | 15 | * may be used to endorse or promote products derived from this software |
mbed_official | 80:66393a7b209d | 16 | * without specific prior written permission. |
mbed_official | 80:66393a7b209d | 17 | * |
mbed_official | 80:66393a7b209d | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 80:66393a7b209d | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 80:66393a7b209d | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 80:66393a7b209d | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 80:66393a7b209d | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 80:66393a7b209d | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 80:66393a7b209d | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 80:66393a7b209d | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 80:66393a7b209d | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 80:66393a7b209d | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 80:66393a7b209d | 28 | ******************************************************************************* |
mbed_official | 80:66393a7b209d | 29 | */ |
mbed_official | 80:66393a7b209d | 30 | #include "i2c_api.h" |
mbed_official | 80:66393a7b209d | 31 | |
mbed_official | 80:66393a7b209d | 32 | #if DEVICE_I2C |
mbed_official | 80:66393a7b209d | 33 | |
mbed_official | 80:66393a7b209d | 34 | #include "cmsis.h" |
mbed_official | 80:66393a7b209d | 35 | #include "pinmap.h" |
mbed_official | 80:66393a7b209d | 36 | #include "error.h" |
mbed_official | 80:66393a7b209d | 37 | |
mbed_official | 80:66393a7b209d | 38 | /* Timeout values for flags and events waiting loops. These timeouts are |
mbed_official | 80:66393a7b209d | 39 | not based on accurate values, they just guarantee that the application will |
mbed_official | 80:66393a7b209d | 40 | not remain stuck if the I2C communication is corrupted. */ |
mbed_official | 80:66393a7b209d | 41 | #define FLAG_TIMEOUT ((int)0x1000) |
mbed_official | 80:66393a7b209d | 42 | #define LONG_TIMEOUT ((int)0x8000) |
mbed_official | 80:66393a7b209d | 43 | |
mbed_official | 80:66393a7b209d | 44 | static const PinMap PinMap_I2C_SDA[] = { |
mbed_official | 80:66393a7b209d | 45 | {PB_9, I2C_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_NOPULL, GPIO_AF_1)}, |
mbed_official | 80:66393a7b209d | 46 | {NC, NC, 0} |
mbed_official | 80:66393a7b209d | 47 | }; |
mbed_official | 80:66393a7b209d | 48 | |
mbed_official | 80:66393a7b209d | 49 | static const PinMap PinMap_I2C_SCL[] = { |
mbed_official | 80:66393a7b209d | 50 | {PB_8, I2C_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_NOPULL, GPIO_AF_1)}, |
mbed_official | 80:66393a7b209d | 51 | {NC, NC, 0} |
mbed_official | 80:66393a7b209d | 52 | }; |
mbed_official | 80:66393a7b209d | 53 | |
mbed_official | 80:66393a7b209d | 54 | void i2c_init(i2c_t *obj, PinName sda, PinName scl) { |
mbed_official | 80:66393a7b209d | 55 | // Determine the I2C to use |
mbed_official | 80:66393a7b209d | 56 | I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); |
mbed_official | 80:66393a7b209d | 57 | I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); |
mbed_official | 80:66393a7b209d | 58 | |
mbed_official | 80:66393a7b209d | 59 | obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); |
mbed_official | 80:66393a7b209d | 60 | |
mbed_official | 80:66393a7b209d | 61 | if (obj->i2c == (I2CName)NC) { |
mbed_official | 80:66393a7b209d | 62 | error("I2C pin mapping failed"); |
mbed_official | 80:66393a7b209d | 63 | } |
mbed_official | 80:66393a7b209d | 64 | |
mbed_official | 80:66393a7b209d | 65 | // Enable I2C clock |
mbed_official | 80:66393a7b209d | 66 | if (obj->i2c == I2C_1) { |
mbed_official | 80:66393a7b209d | 67 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); |
mbed_official | 80:66393a7b209d | 68 | } |
mbed_official | 80:66393a7b209d | 69 | //if (obj->i2c == I2C_2) { |
mbed_official | 80:66393a7b209d | 70 | // RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE); |
mbed_official | 80:66393a7b209d | 71 | //} |
mbed_official | 80:66393a7b209d | 72 | |
mbed_official | 80:66393a7b209d | 73 | // Configure I2C pins |
mbed_official | 80:66393a7b209d | 74 | pinmap_pinout(sda, PinMap_I2C_SDA); |
mbed_official | 80:66393a7b209d | 75 | pinmap_pinout(scl, PinMap_I2C_SCL); |
mbed_official | 80:66393a7b209d | 76 | pin_mode(sda, OpenDrain); |
mbed_official | 80:66393a7b209d | 77 | pin_mode(scl, OpenDrain); |
mbed_official | 80:66393a7b209d | 78 | |
mbed_official | 80:66393a7b209d | 79 | // Reset to clear pending flags if any |
mbed_official | 80:66393a7b209d | 80 | i2c_reset(obj); |
mbed_official | 80:66393a7b209d | 81 | |
mbed_official | 80:66393a7b209d | 82 | // I2C configuration |
mbed_official | 80:66393a7b209d | 83 | i2c_frequency(obj, 100000); // 100 kHz per default |
mbed_official | 80:66393a7b209d | 84 | } |
mbed_official | 80:66393a7b209d | 85 | |
mbed_official | 80:66393a7b209d | 86 | void i2c_frequency(i2c_t *obj, int hz) { |
mbed_official | 80:66393a7b209d | 87 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 80:66393a7b209d | 88 | I2C_InitTypeDef I2C_InitStructure; |
mbed_official | 80:66393a7b209d | 89 | uint32_t tim; |
mbed_official | 80:66393a7b209d | 90 | |
mbed_official | 80:66393a7b209d | 91 | // Values calculated with I2C_Timing_Configuration_V1.0.1.xls file (see AN4235) |
mbed_official | 80:66393a7b209d | 92 | // with Rise time = 100ns and Fall time = 10ns |
mbed_official | 80:66393a7b209d | 93 | switch (hz) { |
mbed_official | 80:66393a7b209d | 94 | case 100000: |
mbed_official | 80:66393a7b209d | 95 | tim = 0x00201D2B; // Standard mode |
mbed_official | 80:66393a7b209d | 96 | break; |
mbed_official | 80:66393a7b209d | 97 | case 200000: |
mbed_official | 80:66393a7b209d | 98 | tim = 0x0010021E; // Fast mode |
mbed_official | 80:66393a7b209d | 99 | break; |
mbed_official | 80:66393a7b209d | 100 | case 400000: |
mbed_official | 80:66393a7b209d | 101 | tim = 0x0010020A; // Fast mode |
mbed_official | 80:66393a7b209d | 102 | break; |
mbed_official | 80:66393a7b209d | 103 | default: |
mbed_official | 80:66393a7b209d | 104 | error("Only 100kHz, 200kHz and 400kHz I2C frequencies are supported."); |
mbed_official | 80:66393a7b209d | 105 | break; |
mbed_official | 80:66393a7b209d | 106 | } |
mbed_official | 80:66393a7b209d | 107 | |
mbed_official | 80:66393a7b209d | 108 | // I2C configuration |
mbed_official | 80:66393a7b209d | 109 | I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; |
mbed_official | 80:66393a7b209d | 110 | I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable; |
mbed_official | 80:66393a7b209d | 111 | I2C_InitStructure.I2C_DigitalFilter = 0x00; |
mbed_official | 80:66393a7b209d | 112 | I2C_InitStructure.I2C_OwnAddress1 = 0x00; |
mbed_official | 80:66393a7b209d | 113 | I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; |
mbed_official | 80:66393a7b209d | 114 | I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; |
mbed_official | 80:66393a7b209d | 115 | I2C_InitStructure.I2C_Timing = tim; |
mbed_official | 80:66393a7b209d | 116 | I2C_Init(i2c, &I2C_InitStructure); |
mbed_official | 80:66393a7b209d | 117 | |
mbed_official | 80:66393a7b209d | 118 | I2C_Cmd(i2c, ENABLE); |
mbed_official | 80:66393a7b209d | 119 | } |
mbed_official | 80:66393a7b209d | 120 | |
mbed_official | 80:66393a7b209d | 121 | inline int i2c_start(i2c_t *obj) { |
mbed_official | 80:66393a7b209d | 122 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 80:66393a7b209d | 123 | int timeout = LONG_TIMEOUT; |
mbed_official | 80:66393a7b209d | 124 | |
mbed_official | 80:66393a7b209d | 125 | // Test BUSY Flag |
mbed_official | 80:66393a7b209d | 126 | while (I2C_GetFlagStatus(i2c, I2C_ISR_BUSY) != RESET) |
mbed_official | 80:66393a7b209d | 127 | { |
mbed_official | 80:66393a7b209d | 128 | if ((timeout--) == 0) return 0; |
mbed_official | 80:66393a7b209d | 129 | } |
mbed_official | 80:66393a7b209d | 130 | |
mbed_official | 80:66393a7b209d | 131 | I2C_GenerateSTART(i2c, ENABLE); |
mbed_official | 80:66393a7b209d | 132 | |
mbed_official | 80:66393a7b209d | 133 | return 0; |
mbed_official | 80:66393a7b209d | 134 | } |
mbed_official | 80:66393a7b209d | 135 | |
mbed_official | 80:66393a7b209d | 136 | inline int i2c_stop(i2c_t *obj) { |
mbed_official | 80:66393a7b209d | 137 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 80:66393a7b209d | 138 | |
mbed_official | 80:66393a7b209d | 139 | I2C_GenerateSTOP(i2c, ENABLE); |
mbed_official | 80:66393a7b209d | 140 | |
mbed_official | 80:66393a7b209d | 141 | return 0; |
mbed_official | 80:66393a7b209d | 142 | } |
mbed_official | 80:66393a7b209d | 143 | |
mbed_official | 80:66393a7b209d | 144 | int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { |
mbed_official | 80:66393a7b209d | 145 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 80:66393a7b209d | 146 | int timeout; |
mbed_official | 80:66393a7b209d | 147 | int count; |
mbed_official | 80:66393a7b209d | 148 | int value; |
mbed_official | 80:66393a7b209d | 149 | |
mbed_official | 80:66393a7b209d | 150 | if (length == 0) return 0; |
mbed_official | 80:66393a7b209d | 151 | |
mbed_official | 80:66393a7b209d | 152 | // Configure slave address, nbytes, reload, end mode and start or stop generation |
mbed_official | 80:66393a7b209d | 153 | if (stop) { |
mbed_official | 80:66393a7b209d | 154 | I2C_TransferHandling(i2c, address, length, I2C_AutoEnd_Mode, I2C_Generate_Start_Read); |
mbed_official | 80:66393a7b209d | 155 | } |
mbed_official | 80:66393a7b209d | 156 | else { |
mbed_official | 80:66393a7b209d | 157 | I2C_TransferHandling(i2c, address, length, I2C_Reload_Mode, I2C_Generate_Start_Read); |
mbed_official | 80:66393a7b209d | 158 | } |
mbed_official | 80:66393a7b209d | 159 | |
mbed_official | 80:66393a7b209d | 160 | // Read all bytes |
mbed_official | 80:66393a7b209d | 161 | for (count = 0; count < length; count++) { |
mbed_official | 80:66393a7b209d | 162 | value = i2c_byte_read(obj, 0); |
mbed_official | 80:66393a7b209d | 163 | data[count] = (char)value; |
mbed_official | 80:66393a7b209d | 164 | } |
mbed_official | 80:66393a7b209d | 165 | |
mbed_official | 80:66393a7b209d | 166 | if (stop) { |
mbed_official | 80:66393a7b209d | 167 | // Wait until STOPF flag is set |
mbed_official | 80:66393a7b209d | 168 | timeout = LONG_TIMEOUT; |
mbed_official | 80:66393a7b209d | 169 | while (I2C_GetFlagStatus(i2c, I2C_ISR_STOPF) == RESET) |
mbed_official | 80:66393a7b209d | 170 | { |
mbed_official | 80:66393a7b209d | 171 | if ((timeout--) == 0) return 0; |
mbed_official | 80:66393a7b209d | 172 | } |
mbed_official | 80:66393a7b209d | 173 | |
mbed_official | 80:66393a7b209d | 174 | // Clear STOPF flag |
mbed_official | 80:66393a7b209d | 175 | I2C_ClearFlag(i2c, I2C_ICR_STOPCF); |
mbed_official | 80:66393a7b209d | 176 | } |
mbed_official | 80:66393a7b209d | 177 | |
mbed_official | 80:66393a7b209d | 178 | return length; |
mbed_official | 80:66393a7b209d | 179 | } |
mbed_official | 80:66393a7b209d | 180 | |
mbed_official | 80:66393a7b209d | 181 | int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { |
mbed_official | 80:66393a7b209d | 182 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 80:66393a7b209d | 183 | int timeout; |
mbed_official | 80:66393a7b209d | 184 | int count; |
mbed_official | 80:66393a7b209d | 185 | |
mbed_official | 80:66393a7b209d | 186 | // Test BUSY Flag |
mbed_official | 80:66393a7b209d | 187 | //timeout = LONG_TIMEOUT; |
mbed_official | 80:66393a7b209d | 188 | //while (I2C_GetFlagStatus(i2c, I2C_ISR_BUSY) != RESET) |
mbed_official | 80:66393a7b209d | 189 | //{ |
mbed_official | 80:66393a7b209d | 190 | // if((timeout--) == 0) return 0; |
mbed_official | 80:66393a7b209d | 191 | //} |
mbed_official | 80:66393a7b209d | 192 | |
mbed_official | 80:66393a7b209d | 193 | // Configure slave address, nbytes, reload, end mode and start or stop generation |
mbed_official | 80:66393a7b209d | 194 | if (stop) { |
mbed_official | 80:66393a7b209d | 195 | I2C_TransferHandling(i2c, address, length, I2C_AutoEnd_Mode, I2C_Generate_Start_Write); |
mbed_official | 80:66393a7b209d | 196 | } |
mbed_official | 80:66393a7b209d | 197 | else { |
mbed_official | 80:66393a7b209d | 198 | I2C_TransferHandling(i2c, address, length, I2C_Reload_Mode, I2C_Generate_Start_Write); |
mbed_official | 80:66393a7b209d | 199 | } |
mbed_official | 80:66393a7b209d | 200 | |
mbed_official | 80:66393a7b209d | 201 | // Write all bytes |
mbed_official | 80:66393a7b209d | 202 | for (count = 0; count < length; count++) { |
mbed_official | 80:66393a7b209d | 203 | if (i2c_byte_write(obj, data[count]) != 1) { |
mbed_official | 80:66393a7b209d | 204 | i2c_stop(obj); |
mbed_official | 80:66393a7b209d | 205 | return 0; |
mbed_official | 80:66393a7b209d | 206 | } |
mbed_official | 80:66393a7b209d | 207 | } |
mbed_official | 80:66393a7b209d | 208 | |
mbed_official | 80:66393a7b209d | 209 | if (stop) { |
mbed_official | 80:66393a7b209d | 210 | // Wait until STOPF flag is set |
mbed_official | 80:66393a7b209d | 211 | timeout = LONG_TIMEOUT; |
mbed_official | 80:66393a7b209d | 212 | while (I2C_GetFlagStatus(i2c, I2C_ISR_STOPF) == RESET) |
mbed_official | 80:66393a7b209d | 213 | { |
mbed_official | 80:66393a7b209d | 214 | if ((timeout--) == 0) return 0; |
mbed_official | 80:66393a7b209d | 215 | } |
mbed_official | 80:66393a7b209d | 216 | |
mbed_official | 80:66393a7b209d | 217 | // Clear STOPF flag |
mbed_official | 80:66393a7b209d | 218 | I2C_ClearFlag(i2c, I2C_ICR_STOPCF); |
mbed_official | 80:66393a7b209d | 219 | } |
mbed_official | 80:66393a7b209d | 220 | |
mbed_official | 80:66393a7b209d | 221 | return count; |
mbed_official | 80:66393a7b209d | 222 | } |
mbed_official | 80:66393a7b209d | 223 | |
mbed_official | 80:66393a7b209d | 224 | int i2c_byte_read(i2c_t *obj, int last) { |
mbed_official | 80:66393a7b209d | 225 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 80:66393a7b209d | 226 | uint8_t data; |
mbed_official | 80:66393a7b209d | 227 | int timeout = FLAG_TIMEOUT; |
mbed_official | 80:66393a7b209d | 228 | |
mbed_official | 80:66393a7b209d | 229 | // Wait until the byte is received |
mbed_official | 80:66393a7b209d | 230 | while (I2C_GetFlagStatus(i2c, I2C_ISR_RXNE) == RESET) { |
mbed_official | 80:66393a7b209d | 231 | if ((timeout--) == 0) return 0; |
mbed_official | 80:66393a7b209d | 232 | } |
mbed_official | 80:66393a7b209d | 233 | |
mbed_official | 80:66393a7b209d | 234 | data = I2C_ReceiveData(i2c); |
mbed_official | 80:66393a7b209d | 235 | |
mbed_official | 80:66393a7b209d | 236 | return (int)data; |
mbed_official | 80:66393a7b209d | 237 | } |
mbed_official | 80:66393a7b209d | 238 | |
mbed_official | 80:66393a7b209d | 239 | int i2c_byte_write(i2c_t *obj, int data) { |
mbed_official | 80:66393a7b209d | 240 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 80:66393a7b209d | 241 | int timeout = FLAG_TIMEOUT; |
mbed_official | 80:66393a7b209d | 242 | |
mbed_official | 80:66393a7b209d | 243 | // Wait until TXIS flag is set |
mbed_official | 80:66393a7b209d | 244 | timeout = LONG_TIMEOUT; |
mbed_official | 80:66393a7b209d | 245 | while (I2C_GetFlagStatus(i2c, I2C_ISR_TXIS) == RESET) |
mbed_official | 80:66393a7b209d | 246 | { |
mbed_official | 80:66393a7b209d | 247 | if ((timeout--) == 0) return 0; |
mbed_official | 80:66393a7b209d | 248 | } |
mbed_official | 80:66393a7b209d | 249 | |
mbed_official | 80:66393a7b209d | 250 | I2C_SendData(i2c, (uint8_t)data); |
mbed_official | 80:66393a7b209d | 251 | |
mbed_official | 80:66393a7b209d | 252 | // Wait until the byte is transmitted |
mbed_official | 80:66393a7b209d | 253 | //while (I2C_GetFlagStatus(i2c, I2C_ISR_TCR) == RESET) { |
mbed_official | 80:66393a7b209d | 254 | // if ((timeout--) == 0) return 0; |
mbed_official | 80:66393a7b209d | 255 | //} |
mbed_official | 80:66393a7b209d | 256 | |
mbed_official | 80:66393a7b209d | 257 | return 1; |
mbed_official | 80:66393a7b209d | 258 | } |
mbed_official | 80:66393a7b209d | 259 | |
mbed_official | 80:66393a7b209d | 260 | void i2c_reset(i2c_t *obj) { |
mbed_official | 80:66393a7b209d | 261 | if (obj->i2c == I2C_1) { |
mbed_official | 80:66393a7b209d | 262 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE); |
mbed_official | 80:66393a7b209d | 263 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE); |
mbed_official | 80:66393a7b209d | 264 | } |
mbed_official | 80:66393a7b209d | 265 | //if (obj->i2c == I2C_2) { |
mbed_official | 80:66393a7b209d | 266 | // RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE); |
mbed_official | 80:66393a7b209d | 267 | // RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE); |
mbed_official | 80:66393a7b209d | 268 | //} |
mbed_official | 80:66393a7b209d | 269 | } |
mbed_official | 80:66393a7b209d | 270 | |
mbed_official | 80:66393a7b209d | 271 | #if DEVICE_I2CSLAVE |
mbed_official | 80:66393a7b209d | 272 | |
mbed_official | 80:66393a7b209d | 273 | void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) { |
mbed_official | 80:66393a7b209d | 274 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 80:66393a7b209d | 275 | uint16_t tmpreg; |
mbed_official | 80:66393a7b209d | 276 | |
mbed_official | 80:66393a7b209d | 277 | // Get the old register value |
mbed_official | 80:66393a7b209d | 278 | tmpreg = i2c->OAR1; |
mbed_official | 80:66393a7b209d | 279 | // Reset address bits |
mbed_official | 80:66393a7b209d | 280 | tmpreg &= 0xFC00; |
mbed_official | 80:66393a7b209d | 281 | // Set new address |
mbed_official | 80:66393a7b209d | 282 | tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits |
mbed_official | 80:66393a7b209d | 283 | // Store the new register value |
mbed_official | 80:66393a7b209d | 284 | i2c->OAR1 = tmpreg; |
mbed_official | 80:66393a7b209d | 285 | } |
mbed_official | 80:66393a7b209d | 286 | |
mbed_official | 80:66393a7b209d | 287 | void i2c_slave_mode(i2c_t *obj, int enable_slave) { |
mbed_official | 80:66393a7b209d | 288 | // Nothing to do |
mbed_official | 80:66393a7b209d | 289 | } |
mbed_official | 80:66393a7b209d | 290 | |
mbed_official | 80:66393a7b209d | 291 | // See I2CSlave.h |
mbed_official | 80:66393a7b209d | 292 | #define NoData 0 // the slave has not been addressed |
mbed_official | 80:66393a7b209d | 293 | #define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter) |
mbed_official | 80:66393a7b209d | 294 | #define WriteGeneral 2 // the master is writing to all slave |
mbed_official | 80:66393a7b209d | 295 | #define WriteAddressed 3 // the master is writing to this slave (slave = receiver) |
mbed_official | 80:66393a7b209d | 296 | |
mbed_official | 80:66393a7b209d | 297 | int i2c_slave_receive(i2c_t *obj) { |
mbed_official | 80:66393a7b209d | 298 | // TO BE DONE |
mbed_official | 80:66393a7b209d | 299 | return(0); |
mbed_official | 80:66393a7b209d | 300 | } |
mbed_official | 80:66393a7b209d | 301 | |
mbed_official | 80:66393a7b209d | 302 | int i2c_slave_read(i2c_t *obj, char *data, int length) { |
mbed_official | 80:66393a7b209d | 303 | int count = 0; |
mbed_official | 80:66393a7b209d | 304 | |
mbed_official | 80:66393a7b209d | 305 | // Read all bytes |
mbed_official | 80:66393a7b209d | 306 | for (count = 0; count < length; count++) { |
mbed_official | 80:66393a7b209d | 307 | data[count] = i2c_byte_read(obj, 0); |
mbed_official | 80:66393a7b209d | 308 | } |
mbed_official | 80:66393a7b209d | 309 | |
mbed_official | 80:66393a7b209d | 310 | return count; |
mbed_official | 80:66393a7b209d | 311 | } |
mbed_official | 80:66393a7b209d | 312 | |
mbed_official | 80:66393a7b209d | 313 | int i2c_slave_write(i2c_t *obj, const char *data, int length) { |
mbed_official | 80:66393a7b209d | 314 | int count = 0; |
mbed_official | 80:66393a7b209d | 315 | |
mbed_official | 80:66393a7b209d | 316 | // Write all bytes |
mbed_official | 80:66393a7b209d | 317 | for (count = 0; count < length; count++) { |
mbed_official | 80:66393a7b209d | 318 | i2c_byte_write(obj, data[count]); |
mbed_official | 80:66393a7b209d | 319 | } |
mbed_official | 80:66393a7b209d | 320 | |
mbed_official | 80:66393a7b209d | 321 | return count; |
mbed_official | 80:66393a7b209d | 322 | } |
mbed_official | 80:66393a7b209d | 323 | |
mbed_official | 80:66393a7b209d | 324 | |
mbed_official | 80:66393a7b209d | 325 | #endif // DEVICE_I2CSLAVE |
mbed_official | 80:66393a7b209d | 326 | |
mbed_official | 80:66393a7b209d | 327 | #endif // DEVICE_I2C |