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