mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/i2c_api.c@84:f54042cbc282, 2014-02-03 (annotated)
- Committer:
- mbed_official
- Date:
- Mon Feb 03 09:30:05 2014 +0000
- Revision:
- 84:f54042cbc282
- Parent:
- 80:66393a7b209d
- Child:
- 164:90c6009cba07
Synchronized with git revision bbbd8699601c42149ccf0c37bc42bb6856ccc4c6
Full URL: https://github.com/mbedmicro/mbed/commit/bbbd8699601c42149ccf0c37bc42bb6856ccc4c6/
[NUCLEO_L152RE/F030_R8] SPI, I2C, Ticker, PWM updates
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 | 84:f54042cbc282 | 45 | {PB_9, I2C_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, 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 | 84:f54042cbc282 | 50 | {PB_8, I2C_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, 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 | 84:f54042cbc282 | 74 | pinmap_pinout(scl, PinMap_I2C_SCL); |
mbed_official | 84:f54042cbc282 | 75 | pin_mode(scl, OpenDrain); |
mbed_official | 80:66393a7b209d | 76 | pinmap_pinout(sda, PinMap_I2C_SDA); |
mbed_official | 80:66393a7b209d | 77 | pin_mode(sda, 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 | 84:f54042cbc282 | 123 | int timeout; |
mbed_official | 80:66393a7b209d | 124 | |
mbed_official | 80:66393a7b209d | 125 | // Test BUSY Flag |
mbed_official | 84:f54042cbc282 | 126 | timeout = LONG_TIMEOUT; |
mbed_official | 84:f54042cbc282 | 127 | while (I2C_GetFlagStatus(i2c, I2C_ISR_BUSY) != RESET) { |
mbed_official | 84:f54042cbc282 | 128 | timeout--; |
mbed_official | 84:f54042cbc282 | 129 | if (timeout == 0) { |
mbed_official | 84:f54042cbc282 | 130 | return 0; |
mbed_official | 84:f54042cbc282 | 131 | } |
mbed_official | 80:66393a7b209d | 132 | } |
mbed_official | 80:66393a7b209d | 133 | |
mbed_official | 80:66393a7b209d | 134 | I2C_GenerateSTART(i2c, ENABLE); |
mbed_official | 80:66393a7b209d | 135 | |
mbed_official | 80:66393a7b209d | 136 | return 0; |
mbed_official | 80:66393a7b209d | 137 | } |
mbed_official | 80:66393a7b209d | 138 | |
mbed_official | 80:66393a7b209d | 139 | inline int i2c_stop(i2c_t *obj) { |
mbed_official | 80:66393a7b209d | 140 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 84:f54042cbc282 | 141 | |
mbed_official | 80:66393a7b209d | 142 | I2C_GenerateSTOP(i2c, ENABLE); |
mbed_official | 84:f54042cbc282 | 143 | |
mbed_official | 80:66393a7b209d | 144 | return 0; |
mbed_official | 80:66393a7b209d | 145 | } |
mbed_official | 80:66393a7b209d | 146 | |
mbed_official | 80:66393a7b209d | 147 | int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { |
mbed_official | 80:66393a7b209d | 148 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 80:66393a7b209d | 149 | int count; |
mbed_official | 80:66393a7b209d | 150 | int value; |
mbed_official | 84:f54042cbc282 | 151 | |
mbed_official | 80:66393a7b209d | 152 | if (length == 0) return 0; |
mbed_official | 80:66393a7b209d | 153 | |
mbed_official | 80:66393a7b209d | 154 | // Configure slave address, nbytes, reload, end mode and start or stop generation |
mbed_official | 84:f54042cbc282 | 155 | I2C_TransferHandling(i2c, address, length, I2C_AutoEnd_Mode, I2C_Generate_Start_Read); |
mbed_official | 80:66393a7b209d | 156 | |
mbed_official | 80:66393a7b209d | 157 | // Read all bytes |
mbed_official | 80:66393a7b209d | 158 | for (count = 0; count < length; count++) { |
mbed_official | 80:66393a7b209d | 159 | value = i2c_byte_read(obj, 0); |
mbed_official | 80:66393a7b209d | 160 | data[count] = (char)value; |
mbed_official | 80:66393a7b209d | 161 | } |
mbed_official | 80:66393a7b209d | 162 | |
mbed_official | 80:66393a7b209d | 163 | return length; |
mbed_official | 80:66393a7b209d | 164 | } |
mbed_official | 80:66393a7b209d | 165 | |
mbed_official | 80:66393a7b209d | 166 | int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { |
mbed_official | 80:66393a7b209d | 167 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 84:f54042cbc282 | 168 | //int timeout; |
mbed_official | 80:66393a7b209d | 169 | int count; |
mbed_official | 84:f54042cbc282 | 170 | |
mbed_official | 84:f54042cbc282 | 171 | if (length == 0) return 0; |
mbed_official | 80:66393a7b209d | 172 | |
mbed_official | 84:f54042cbc282 | 173 | // TODO: the stop is always sent even with I2C_SoftEnd_Mode. To be corrected. |
mbed_official | 84:f54042cbc282 | 174 | |
mbed_official | 84:f54042cbc282 | 175 | // Configure slave address, nbytes, reload, end mode and start or stop generation |
mbed_official | 84:f54042cbc282 | 176 | //if (stop) { |
mbed_official | 84:f54042cbc282 | 177 | I2C_TransferHandling(i2c, address, length, I2C_AutoEnd_Mode, I2C_Generate_Start_Write); |
mbed_official | 80:66393a7b209d | 178 | //} |
mbed_official | 84:f54042cbc282 | 179 | //else { |
mbed_official | 84:f54042cbc282 | 180 | // I2C_TransferHandling(i2c, address, length, I2C_SoftEnd_Mode, I2C_Generate_Start_Write); |
mbed_official | 84:f54042cbc282 | 181 | //} |
mbed_official | 80:66393a7b209d | 182 | |
mbed_official | 80:66393a7b209d | 183 | // Write all bytes |
mbed_official | 80:66393a7b209d | 184 | for (count = 0; count < length; count++) { |
mbed_official | 80:66393a7b209d | 185 | if (i2c_byte_write(obj, data[count]) != 1) { |
mbed_official | 80:66393a7b209d | 186 | i2c_stop(obj); |
mbed_official | 80:66393a7b209d | 187 | return 0; |
mbed_official | 80:66393a7b209d | 188 | } |
mbed_official | 80:66393a7b209d | 189 | } |
mbed_official | 80:66393a7b209d | 190 | |
mbed_official | 84:f54042cbc282 | 191 | /* |
mbed_official | 80:66393a7b209d | 192 | if (stop) { |
mbed_official | 80:66393a7b209d | 193 | // Wait until STOPF flag is set |
mbed_official | 80:66393a7b209d | 194 | timeout = LONG_TIMEOUT; |
mbed_official | 84:f54042cbc282 | 195 | while (I2C_GetFlagStatus(i2c, I2C_ISR_STOPF) == RESET) { |
mbed_official | 84:f54042cbc282 | 196 | timeout--; |
mbed_official | 84:f54042cbc282 | 197 | if (timeout == 0) { |
mbed_official | 84:f54042cbc282 | 198 | return 0; |
mbed_official | 84:f54042cbc282 | 199 | } |
mbed_official | 84:f54042cbc282 | 200 | } |
mbed_official | 80:66393a7b209d | 201 | // Clear STOPF flag |
mbed_official | 80:66393a7b209d | 202 | I2C_ClearFlag(i2c, I2C_ICR_STOPCF); |
mbed_official | 80:66393a7b209d | 203 | } |
mbed_official | 84:f54042cbc282 | 204 | */ |
mbed_official | 80:66393a7b209d | 205 | |
mbed_official | 80:66393a7b209d | 206 | return count; |
mbed_official | 80:66393a7b209d | 207 | } |
mbed_official | 80:66393a7b209d | 208 | |
mbed_official | 80:66393a7b209d | 209 | int i2c_byte_read(i2c_t *obj, int last) { |
mbed_official | 80:66393a7b209d | 210 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 80:66393a7b209d | 211 | uint8_t data; |
mbed_official | 84:f54042cbc282 | 212 | int timeout; |
mbed_official | 80:66393a7b209d | 213 | |
mbed_official | 80:66393a7b209d | 214 | // Wait until the byte is received |
mbed_official | 84:f54042cbc282 | 215 | timeout = FLAG_TIMEOUT; |
mbed_official | 80:66393a7b209d | 216 | while (I2C_GetFlagStatus(i2c, I2C_ISR_RXNE) == RESET) { |
mbed_official | 84:f54042cbc282 | 217 | timeout--; |
mbed_official | 84:f54042cbc282 | 218 | if (timeout == 0) { |
mbed_official | 84:f54042cbc282 | 219 | return 0; |
mbed_official | 84:f54042cbc282 | 220 | } |
mbed_official | 80:66393a7b209d | 221 | } |
mbed_official | 80:66393a7b209d | 222 | |
mbed_official | 80:66393a7b209d | 223 | data = I2C_ReceiveData(i2c); |
mbed_official | 80:66393a7b209d | 224 | |
mbed_official | 80:66393a7b209d | 225 | return (int)data; |
mbed_official | 80:66393a7b209d | 226 | } |
mbed_official | 80:66393a7b209d | 227 | |
mbed_official | 80:66393a7b209d | 228 | int i2c_byte_write(i2c_t *obj, int data) { |
mbed_official | 80:66393a7b209d | 229 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 84:f54042cbc282 | 230 | int timeout; |
mbed_official | 80:66393a7b209d | 231 | |
mbed_official | 84:f54042cbc282 | 232 | // Wait until the previous byte is transmitted |
mbed_official | 84:f54042cbc282 | 233 | timeout = FLAG_TIMEOUT; |
mbed_official | 84:f54042cbc282 | 234 | while (I2C_GetFlagStatus(i2c, I2C_ISR_TXIS) == RESET) { |
mbed_official | 84:f54042cbc282 | 235 | timeout--; |
mbed_official | 84:f54042cbc282 | 236 | if (timeout == 0) { |
mbed_official | 84:f54042cbc282 | 237 | return 0; |
mbed_official | 84:f54042cbc282 | 238 | } |
mbed_official | 80:66393a7b209d | 239 | } |
mbed_official | 84:f54042cbc282 | 240 | |
mbed_official | 80:66393a7b209d | 241 | I2C_SendData(i2c, (uint8_t)data); |
mbed_official | 80:66393a7b209d | 242 | |
mbed_official | 80:66393a7b209d | 243 | return 1; |
mbed_official | 80:66393a7b209d | 244 | } |
mbed_official | 80:66393a7b209d | 245 | |
mbed_official | 80:66393a7b209d | 246 | void i2c_reset(i2c_t *obj) { |
mbed_official | 80:66393a7b209d | 247 | if (obj->i2c == I2C_1) { |
mbed_official | 80:66393a7b209d | 248 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE); |
mbed_official | 80:66393a7b209d | 249 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE); |
mbed_official | 80:66393a7b209d | 250 | } |
mbed_official | 80:66393a7b209d | 251 | //if (obj->i2c == I2C_2) { |
mbed_official | 80:66393a7b209d | 252 | // RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE); |
mbed_official | 80:66393a7b209d | 253 | // RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE); |
mbed_official | 80:66393a7b209d | 254 | //} |
mbed_official | 80:66393a7b209d | 255 | } |
mbed_official | 80:66393a7b209d | 256 | |
mbed_official | 80:66393a7b209d | 257 | #if DEVICE_I2CSLAVE |
mbed_official | 80:66393a7b209d | 258 | |
mbed_official | 80:66393a7b209d | 259 | void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) { |
mbed_official | 80:66393a7b209d | 260 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 80:66393a7b209d | 261 | uint16_t tmpreg; |
mbed_official | 80:66393a7b209d | 262 | |
mbed_official | 80:66393a7b209d | 263 | // Get the old register value |
mbed_official | 80:66393a7b209d | 264 | tmpreg = i2c->OAR1; |
mbed_official | 80:66393a7b209d | 265 | // Reset address bits |
mbed_official | 80:66393a7b209d | 266 | tmpreg &= 0xFC00; |
mbed_official | 80:66393a7b209d | 267 | // Set new address |
mbed_official | 80:66393a7b209d | 268 | tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits |
mbed_official | 80:66393a7b209d | 269 | // Store the new register value |
mbed_official | 80:66393a7b209d | 270 | i2c->OAR1 = tmpreg; |
mbed_official | 80:66393a7b209d | 271 | } |
mbed_official | 80:66393a7b209d | 272 | |
mbed_official | 80:66393a7b209d | 273 | void i2c_slave_mode(i2c_t *obj, int enable_slave) { |
mbed_official | 80:66393a7b209d | 274 | // Nothing to do |
mbed_official | 80:66393a7b209d | 275 | } |
mbed_official | 80:66393a7b209d | 276 | |
mbed_official | 80:66393a7b209d | 277 | // See I2CSlave.h |
mbed_official | 80:66393a7b209d | 278 | #define NoData 0 // the slave has not been addressed |
mbed_official | 80:66393a7b209d | 279 | #define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter) |
mbed_official | 80:66393a7b209d | 280 | #define WriteGeneral 2 // the master is writing to all slave |
mbed_official | 80:66393a7b209d | 281 | #define WriteAddressed 3 // the master is writing to this slave (slave = receiver) |
mbed_official | 80:66393a7b209d | 282 | |
mbed_official | 80:66393a7b209d | 283 | int i2c_slave_receive(i2c_t *obj) { |
mbed_official | 80:66393a7b209d | 284 | // TO BE DONE |
mbed_official | 80:66393a7b209d | 285 | return(0); |
mbed_official | 80:66393a7b209d | 286 | } |
mbed_official | 80:66393a7b209d | 287 | |
mbed_official | 80:66393a7b209d | 288 | int i2c_slave_read(i2c_t *obj, char *data, int length) { |
mbed_official | 80:66393a7b209d | 289 | int count = 0; |
mbed_official | 80:66393a7b209d | 290 | |
mbed_official | 80:66393a7b209d | 291 | // Read all bytes |
mbed_official | 80:66393a7b209d | 292 | for (count = 0; count < length; count++) { |
mbed_official | 80:66393a7b209d | 293 | data[count] = i2c_byte_read(obj, 0); |
mbed_official | 80:66393a7b209d | 294 | } |
mbed_official | 80:66393a7b209d | 295 | |
mbed_official | 80:66393a7b209d | 296 | return count; |
mbed_official | 80:66393a7b209d | 297 | } |
mbed_official | 80:66393a7b209d | 298 | |
mbed_official | 80:66393a7b209d | 299 | int i2c_slave_write(i2c_t *obj, const char *data, int length) { |
mbed_official | 80:66393a7b209d | 300 | int count = 0; |
mbed_official | 80:66393a7b209d | 301 | |
mbed_official | 80:66393a7b209d | 302 | // Write all bytes |
mbed_official | 80:66393a7b209d | 303 | for (count = 0; count < length; count++) { |
mbed_official | 80:66393a7b209d | 304 | i2c_byte_write(obj, data[count]); |
mbed_official | 80:66393a7b209d | 305 | } |
mbed_official | 80:66393a7b209d | 306 | |
mbed_official | 80:66393a7b209d | 307 | return count; |
mbed_official | 80:66393a7b209d | 308 | } |
mbed_official | 80:66393a7b209d | 309 | |
mbed_official | 80:66393a7b209d | 310 | |
mbed_official | 80:66393a7b209d | 311 | #endif // DEVICE_I2CSLAVE |
mbed_official | 80:66393a7b209d | 312 | |
mbed_official | 80:66393a7b209d | 313 | #endif // DEVICE_I2C |