mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_F401RE/i2c_api.c@181:a4cbdfbbd2f4, 2014-05-07 (annotated)
- Committer:
- mbed_official
- Date:
- Wed May 07 13:15:08 2014 +0100
- Revision:
- 181:a4cbdfbbd2f4
- Parent:
- 172:2f4f8c56b261
- Child:
- 183:46ae3190e86c
Synchronized with git revision 7751e759576c6fd68deccb81ea82bac19ed41745
Full URL: https://github.com/mbedmicro/mbed/commit/7751e759576c6fd68deccb81ea82bac19ed41745/
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 87:085cde657901 | 1 | /* mbed Microcontroller Library |
mbed_official | 87:085cde657901 | 2 | ******************************************************************************* |
mbed_official | 87:085cde657901 | 3 | * Copyright (c) 2014, STMicroelectronics |
mbed_official | 87:085cde657901 | 4 | * All rights reserved. |
mbed_official | 87:085cde657901 | 5 | * |
mbed_official | 87:085cde657901 | 6 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 87:085cde657901 | 7 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 87:085cde657901 | 8 | * |
mbed_official | 87:085cde657901 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 87:085cde657901 | 10 | * this list of conditions and the following disclaimer. |
mbed_official | 87:085cde657901 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 87:085cde657901 | 12 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 87:085cde657901 | 13 | * and/or other materials provided with the distribution. |
mbed_official | 87:085cde657901 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 87:085cde657901 | 15 | * may be used to endorse or promote products derived from this software |
mbed_official | 87:085cde657901 | 16 | * without specific prior written permission. |
mbed_official | 87:085cde657901 | 17 | * |
mbed_official | 87:085cde657901 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 87:085cde657901 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 87:085cde657901 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 87:085cde657901 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 87:085cde657901 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 87:085cde657901 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 87:085cde657901 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 87:085cde657901 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 87:085cde657901 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 87:085cde657901 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 87:085cde657901 | 28 | ******************************************************************************* |
mbed_official | 87:085cde657901 | 29 | */ |
mbed_official | 87:085cde657901 | 30 | #include "i2c_api.h" |
mbed_official | 87:085cde657901 | 31 | |
mbed_official | 87:085cde657901 | 32 | #if DEVICE_I2C |
mbed_official | 87:085cde657901 | 33 | |
mbed_official | 87:085cde657901 | 34 | #include "cmsis.h" |
mbed_official | 87:085cde657901 | 35 | #include "pinmap.h" |
mbed_official | 87:085cde657901 | 36 | #include "error.h" |
mbed_official | 87:085cde657901 | 37 | |
mbed_official | 87:085cde657901 | 38 | /* Timeout values for flags and events waiting loops. These timeouts are |
mbed_official | 172:2f4f8c56b261 | 39 | not based on accurate values, they just guarantee that the application will |
mbed_official | 172:2f4f8c56b261 | 40 | not remain stuck if the I2C communication is corrupted. */ |
mbed_official | 87:085cde657901 | 41 | #define FLAG_TIMEOUT ((int)0x1000) |
mbed_official | 87:085cde657901 | 42 | #define LONG_TIMEOUT ((int)0x8000) |
mbed_official | 87:085cde657901 | 43 | |
mbed_official | 87:085cde657901 | 44 | static const PinMap PinMap_I2C_SDA[] = { |
mbed_official | 115:4f0cb9cdc478 | 45 | {PB_3, I2C_2, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF9_I2C2)}, |
mbed_official | 115:4f0cb9cdc478 | 46 | {PB_4, I2C_3, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF9_I2C3)}, |
mbed_official | 125:23cc3068a9e4 | 47 | {PB_7, I2C_1, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF4_I2C1)}, |
mbed_official | 125:23cc3068a9e4 | 48 | {PB_9, I2C_1, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF4_I2C1)}, |
mbed_official | 125:23cc3068a9e4 | 49 | {PC_9, I2C_3, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF4_I2C3)}, |
mbed_official | 87:085cde657901 | 50 | {NC, NC, 0} |
mbed_official | 87:085cde657901 | 51 | }; |
mbed_official | 87:085cde657901 | 52 | |
mbed_official | 87:085cde657901 | 53 | static const PinMap PinMap_I2C_SCL[] = { |
mbed_official | 125:23cc3068a9e4 | 54 | {PA_8, I2C_3, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF4_I2C3)}, |
mbed_official | 125:23cc3068a9e4 | 55 | {PB_6, I2C_1, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF4_I2C1)}, |
mbed_official | 88:81f18c97d490 | 56 | {PB_8, I2C_1, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF4_I2C1)}, |
mbed_official | 115:4f0cb9cdc478 | 57 | {PB_10, I2C_2, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF4_I2C2)}, |
mbed_official | 87:085cde657901 | 58 | {NC, NC, 0} |
mbed_official | 87:085cde657901 | 59 | }; |
mbed_official | 87:085cde657901 | 60 | |
mbed_official | 87:085cde657901 | 61 | I2C_HandleTypeDef I2cHandle; |
mbed_official | 87:085cde657901 | 62 | |
mbed_official | 172:2f4f8c56b261 | 63 | void i2c_init(i2c_t *obj, PinName sda, PinName scl) { |
mbed_official | 87:085cde657901 | 64 | // Determine the I2C to use |
mbed_official | 87:085cde657901 | 65 | I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); |
mbed_official | 87:085cde657901 | 66 | I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); |
mbed_official | 87:085cde657901 | 67 | |
mbed_official | 87:085cde657901 | 68 | obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); |
mbed_official | 172:2f4f8c56b261 | 69 | |
mbed_official | 87:085cde657901 | 70 | if (obj->i2c == (I2CName)NC) { |
mbed_official | 87:085cde657901 | 71 | error("I2C error: pinout mapping failed."); |
mbed_official | 87:085cde657901 | 72 | } |
mbed_official | 87:085cde657901 | 73 | |
mbed_official | 87:085cde657901 | 74 | // Enable I2C clock |
mbed_official | 172:2f4f8c56b261 | 75 | if (obj->i2c == I2C_1) { |
mbed_official | 87:085cde657901 | 76 | __I2C1_CLK_ENABLE(); |
mbed_official | 87:085cde657901 | 77 | } |
mbed_official | 115:4f0cb9cdc478 | 78 | if (obj->i2c == I2C_2) { |
mbed_official | 115:4f0cb9cdc478 | 79 | __I2C2_CLK_ENABLE(); |
mbed_official | 115:4f0cb9cdc478 | 80 | } |
mbed_official | 115:4f0cb9cdc478 | 81 | if (obj->i2c == I2C_3) { |
mbed_official | 115:4f0cb9cdc478 | 82 | __I2C3_CLK_ENABLE(); |
mbed_official | 115:4f0cb9cdc478 | 83 | } |
mbed_official | 87:085cde657901 | 84 | |
mbed_official | 87:085cde657901 | 85 | // Configure I2C pins |
mbed_official | 87:085cde657901 | 86 | pinmap_pinout(sda, PinMap_I2C_SDA); |
mbed_official | 87:085cde657901 | 87 | pinmap_pinout(scl, PinMap_I2C_SCL); |
mbed_official | 87:085cde657901 | 88 | pin_mode(sda, OpenDrain); |
mbed_official | 87:085cde657901 | 89 | pin_mode(scl, OpenDrain); |
mbed_official | 172:2f4f8c56b261 | 90 | |
mbed_official | 87:085cde657901 | 91 | // Reset to clear pending flags if any |
mbed_official | 87:085cde657901 | 92 | i2c_reset(obj); |
mbed_official | 172:2f4f8c56b261 | 93 | |
mbed_official | 87:085cde657901 | 94 | // I2C configuration |
mbed_official | 172:2f4f8c56b261 | 95 | i2c_frequency(obj, 100000); // 100 kHz per default |
mbed_official | 181:a4cbdfbbd2f4 | 96 | |
mbed_official | 181:a4cbdfbbd2f4 | 97 | // I2C master by default |
mbed_official | 181:a4cbdfbbd2f4 | 98 | obj->slave = 0; |
mbed_official | 87:085cde657901 | 99 | } |
mbed_official | 87:085cde657901 | 100 | |
mbed_official | 87:085cde657901 | 101 | void i2c_frequency(i2c_t *obj, int hz) { |
mbed_official | 87:085cde657901 | 102 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 172:2f4f8c56b261 | 103 | |
mbed_official | 87:085cde657901 | 104 | if ((hz != 0) && (hz <= 400000)) { |
mbed_official | 87:085cde657901 | 105 | // I2C configuration |
mbed_official | 87:085cde657901 | 106 | I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; |
mbed_official | 87:085cde657901 | 107 | I2cHandle.Init.ClockSpeed = hz; |
mbed_official | 87:085cde657901 | 108 | I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; |
mbed_official | 87:085cde657901 | 109 | I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2; |
mbed_official | 87:085cde657901 | 110 | I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; |
mbed_official | 87:085cde657901 | 111 | I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; |
mbed_official | 87:085cde657901 | 112 | I2cHandle.Init.OwnAddress1 = 0; |
mbed_official | 87:085cde657901 | 113 | I2cHandle.Init.OwnAddress2 = 0; |
mbed_official | 87:085cde657901 | 114 | HAL_I2C_Init(&I2cHandle); |
mbed_official | 181:a4cbdfbbd2f4 | 115 | if(obj->slave) { |
mbed_official | 181:a4cbdfbbd2f4 | 116 | /* Enable Address Acknowledge */ |
mbed_official | 181:a4cbdfbbd2f4 | 117 | I2cHandle.Instance->CR1 |= I2C_CR1_ACK; |
mbed_official | 181:a4cbdfbbd2f4 | 118 | } |
mbed_official | 87:085cde657901 | 119 | } |
mbed_official | 87:085cde657901 | 120 | else { |
mbed_official | 87:085cde657901 | 121 | error("I2C error: frequency setting failed (max 400kHz)."); |
mbed_official | 87:085cde657901 | 122 | } |
mbed_official | 87:085cde657901 | 123 | } |
mbed_official | 87:085cde657901 | 124 | |
mbed_official | 87:085cde657901 | 125 | inline int i2c_start(i2c_t *obj) { |
mbed_official | 87:085cde657901 | 126 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 87:085cde657901 | 127 | int timeout; |
mbed_official | 172:2f4f8c56b261 | 128 | |
mbed_official | 87:085cde657901 | 129 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 172:2f4f8c56b261 | 130 | |
mbed_official | 87:085cde657901 | 131 | // Clear Acknowledge failure flag |
mbed_official | 87:085cde657901 | 132 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF); |
mbed_official | 172:2f4f8c56b261 | 133 | |
mbed_official | 87:085cde657901 | 134 | // Generate the START condition |
mbed_official | 87:085cde657901 | 135 | i2c->CR1 |= I2C_CR1_START; |
mbed_official | 172:2f4f8c56b261 | 136 | |
mbed_official | 87:085cde657901 | 137 | // Wait the START condition has been correctly sent |
mbed_official | 87:085cde657901 | 138 | timeout = FLAG_TIMEOUT; |
mbed_official | 87:085cde657901 | 139 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_SB) == RESET) { |
mbed_official | 172:2f4f8c56b261 | 140 | if ((timeout--) == 0) { |
mbed_official | 172:2f4f8c56b261 | 141 | return 1; |
mbed_official | 172:2f4f8c56b261 | 142 | } |
mbed_official | 87:085cde657901 | 143 | } |
mbed_official | 172:2f4f8c56b261 | 144 | |
mbed_official | 87:085cde657901 | 145 | return 0; |
mbed_official | 87:085cde657901 | 146 | } |
mbed_official | 87:085cde657901 | 147 | |
mbed_official | 87:085cde657901 | 148 | inline int i2c_stop(i2c_t *obj) { |
mbed_official | 87:085cde657901 | 149 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 172:2f4f8c56b261 | 150 | |
mbed_official | 87:085cde657901 | 151 | // Generate the STOP condition |
mbed_official | 87:085cde657901 | 152 | i2c->CR1 |= I2C_CR1_STOP; |
mbed_official | 87:085cde657901 | 153 | |
mbed_official | 87:085cde657901 | 154 | return 0; |
mbed_official | 87:085cde657901 | 155 | } |
mbed_official | 87:085cde657901 | 156 | |
mbed_official | 172:2f4f8c56b261 | 157 | int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { |
mbed_official | 87:085cde657901 | 158 | if (length == 0) return 0; |
mbed_official | 172:2f4f8c56b261 | 159 | |
mbed_official | 87:085cde657901 | 160 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 87:085cde657901 | 161 | |
mbed_official | 87:085cde657901 | 162 | // Reception process with 5 seconds timeout |
mbed_official | 87:085cde657901 | 163 | if (HAL_I2C_Master_Receive(&I2cHandle, (uint16_t)address, (uint8_t *)data, length, 5000) != HAL_OK) { |
mbed_official | 87:085cde657901 | 164 | return 0; // Error |
mbed_official | 87:085cde657901 | 165 | } |
mbed_official | 87:085cde657901 | 166 | |
mbed_official | 87:085cde657901 | 167 | return length; |
mbed_official | 87:085cde657901 | 168 | } |
mbed_official | 87:085cde657901 | 169 | |
mbed_official | 87:085cde657901 | 170 | int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { |
mbed_official | 87:085cde657901 | 171 | if (length == 0) return 0; |
mbed_official | 172:2f4f8c56b261 | 172 | |
mbed_official | 87:085cde657901 | 173 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 87:085cde657901 | 174 | |
mbed_official | 87:085cde657901 | 175 | // Transmission process with 5 seconds timeout |
mbed_official | 87:085cde657901 | 176 | if (HAL_I2C_Master_Transmit(&I2cHandle, (uint16_t)address, (uint8_t *)data, length, 5000) != HAL_OK) { |
mbed_official | 87:085cde657901 | 177 | return 0; // Error |
mbed_official | 87:085cde657901 | 178 | } |
mbed_official | 172:2f4f8c56b261 | 179 | |
mbed_official | 87:085cde657901 | 180 | return length; |
mbed_official | 87:085cde657901 | 181 | } |
mbed_official | 87:085cde657901 | 182 | |
mbed_official | 87:085cde657901 | 183 | int i2c_byte_read(i2c_t *obj, int last) { |
mbed_official | 87:085cde657901 | 184 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 87:085cde657901 | 185 | int timeout; |
mbed_official | 172:2f4f8c56b261 | 186 | |
mbed_official | 87:085cde657901 | 187 | if (last) { |
mbed_official | 87:085cde657901 | 188 | // Don't acknowledge the last byte |
mbed_official | 87:085cde657901 | 189 | i2c->CR1 &= ~I2C_CR1_ACK; |
mbed_official | 87:085cde657901 | 190 | } else { |
mbed_official | 87:085cde657901 | 191 | // Acknowledge the byte |
mbed_official | 87:085cde657901 | 192 | i2c->CR1 |= I2C_CR1_ACK; |
mbed_official | 87:085cde657901 | 193 | } |
mbed_official | 87:085cde657901 | 194 | |
mbed_official | 87:085cde657901 | 195 | // Wait until the byte is received |
mbed_official | 87:085cde657901 | 196 | timeout = FLAG_TIMEOUT; |
mbed_official | 87:085cde657901 | 197 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) { |
mbed_official | 172:2f4f8c56b261 | 198 | if ((timeout--) == 0) { |
mbed_official | 172:2f4f8c56b261 | 199 | return 0; |
mbed_official | 172:2f4f8c56b261 | 200 | } |
mbed_official | 87:085cde657901 | 201 | } |
mbed_official | 172:2f4f8c56b261 | 202 | |
mbed_official | 87:085cde657901 | 203 | return (int)i2c->DR; |
mbed_official | 87:085cde657901 | 204 | } |
mbed_official | 87:085cde657901 | 205 | |
mbed_official | 87:085cde657901 | 206 | int i2c_byte_write(i2c_t *obj, int data) { |
mbed_official | 87:085cde657901 | 207 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 87:085cde657901 | 208 | int timeout; |
mbed_official | 87:085cde657901 | 209 | |
mbed_official | 87:085cde657901 | 210 | i2c->DR = (uint8_t)data; |
mbed_official | 87:085cde657901 | 211 | |
mbed_official | 87:085cde657901 | 212 | // Wait until the byte is transmitted |
mbed_official | 172:2f4f8c56b261 | 213 | timeout = FLAG_TIMEOUT; |
mbed_official | 87:085cde657901 | 214 | while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXE) == RESET) && |
mbed_official | 87:085cde657901 | 215 | (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == RESET)) { |
mbed_official | 87:085cde657901 | 216 | if ((timeout--) == 0) { |
mbed_official | 87:085cde657901 | 217 | return 0; |
mbed_official | 87:085cde657901 | 218 | } |
mbed_official | 87:085cde657901 | 219 | } |
mbed_official | 172:2f4f8c56b261 | 220 | |
mbed_official | 87:085cde657901 | 221 | return 1; |
mbed_official | 87:085cde657901 | 222 | } |
mbed_official | 87:085cde657901 | 223 | |
mbed_official | 87:085cde657901 | 224 | void i2c_reset(i2c_t *obj) { |
mbed_official | 172:2f4f8c56b261 | 225 | if (obj->i2c == I2C_1) { |
mbed_official | 87:085cde657901 | 226 | __I2C1_FORCE_RESET(); |
mbed_official | 87:085cde657901 | 227 | __I2C1_RELEASE_RESET(); |
mbed_official | 87:085cde657901 | 228 | } |
mbed_official | 115:4f0cb9cdc478 | 229 | if (obj->i2c == I2C_2) { |
mbed_official | 115:4f0cb9cdc478 | 230 | __I2C2_FORCE_RESET(); |
mbed_official | 115:4f0cb9cdc478 | 231 | __I2C2_RELEASE_RESET(); |
mbed_official | 115:4f0cb9cdc478 | 232 | } |
mbed_official | 115:4f0cb9cdc478 | 233 | if (obj->i2c == I2C_3) { |
mbed_official | 115:4f0cb9cdc478 | 234 | __I2C3_FORCE_RESET(); |
mbed_official | 115:4f0cb9cdc478 | 235 | __I2C3_RELEASE_RESET(); |
mbed_official | 115:4f0cb9cdc478 | 236 | } |
mbed_official | 87:085cde657901 | 237 | } |
mbed_official | 87:085cde657901 | 238 | |
mbed_official | 87:085cde657901 | 239 | #if DEVICE_I2CSLAVE |
mbed_official | 87:085cde657901 | 240 | |
mbed_official | 87:085cde657901 | 241 | void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) { |
mbed_official | 87:085cde657901 | 242 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 181:a4cbdfbbd2f4 | 243 | uint16_t tmpreg = 0; |
mbed_official | 87:085cde657901 | 244 | |
mbed_official | 87:085cde657901 | 245 | // Get the old register value |
mbed_official | 87:085cde657901 | 246 | tmpreg = i2c->OAR1; |
mbed_official | 87:085cde657901 | 247 | // Reset address bits |
mbed_official | 87:085cde657901 | 248 | tmpreg &= 0xFC00; |
mbed_official | 87:085cde657901 | 249 | // Set new address |
mbed_official | 87:085cde657901 | 250 | tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits |
mbed_official | 87:085cde657901 | 251 | // Store the new register value |
mbed_official | 87:085cde657901 | 252 | i2c->OAR1 = tmpreg; |
mbed_official | 87:085cde657901 | 253 | } |
mbed_official | 87:085cde657901 | 254 | |
mbed_official | 87:085cde657901 | 255 | void i2c_slave_mode(i2c_t *obj, int enable_slave) { |
mbed_official | 181:a4cbdfbbd2f4 | 256 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 181:a4cbdfbbd2f4 | 257 | if(enable_slave) { |
mbed_official | 181:a4cbdfbbd2f4 | 258 | obj->slave = 1; |
mbed_official | 181:a4cbdfbbd2f4 | 259 | /* Enable Address Acknowledge */ |
mbed_official | 181:a4cbdfbbd2f4 | 260 | I2cHandle.Instance->CR1 |= I2C_CR1_ACK; |
mbed_official | 181:a4cbdfbbd2f4 | 261 | } |
mbed_official | 87:085cde657901 | 262 | } |
mbed_official | 87:085cde657901 | 263 | |
mbed_official | 87:085cde657901 | 264 | // See I2CSlave.h |
mbed_official | 87:085cde657901 | 265 | #define NoData 0 // the slave has not been addressed |
mbed_official | 87:085cde657901 | 266 | #define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter) |
mbed_official | 87:085cde657901 | 267 | #define WriteGeneral 2 // the master is writing to all slave |
mbed_official | 87:085cde657901 | 268 | #define WriteAddressed 3 // the master is writing to this slave (slave = receiver) |
mbed_official | 87:085cde657901 | 269 | |
mbed_official | 87:085cde657901 | 270 | int i2c_slave_receive(i2c_t *obj) { |
mbed_official | 181:a4cbdfbbd2f4 | 271 | int retValue = NoData; |
mbed_official | 181:a4cbdfbbd2f4 | 272 | |
mbed_official | 181:a4cbdfbbd2f4 | 273 | if(__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == 1) { |
mbed_official | 181:a4cbdfbbd2f4 | 274 | if(__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == 1) { |
mbed_official | 181:a4cbdfbbd2f4 | 275 | if(__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TRA) == 1) |
mbed_official | 181:a4cbdfbbd2f4 | 276 | retValue = ReadAddressed; |
mbed_official | 181:a4cbdfbbd2f4 | 277 | else |
mbed_official | 181:a4cbdfbbd2f4 | 278 | retValue = WriteAddressed; |
mbed_official | 181:a4cbdfbbd2f4 | 279 | |
mbed_official | 181:a4cbdfbbd2f4 | 280 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_ADDR); |
mbed_official | 181:a4cbdfbbd2f4 | 281 | } |
mbed_official | 181:a4cbdfbbd2f4 | 282 | } |
mbed_official | 181:a4cbdfbbd2f4 | 283 | |
mbed_official | 181:a4cbdfbbd2f4 | 284 | return(retValue); |
mbed_official | 87:085cde657901 | 285 | } |
mbed_official | 87:085cde657901 | 286 | |
mbed_official | 87:085cde657901 | 287 | int i2c_slave_read(i2c_t *obj, char *data, int length) { |
mbed_official | 181:a4cbdfbbd2f4 | 288 | uint32_t Timeout; |
mbed_official | 181:a4cbdfbbd2f4 | 289 | int size = 0; |
mbed_official | 87:085cde657901 | 290 | if (length == 0) return 0; |
mbed_official | 172:2f4f8c56b261 | 291 | |
mbed_official | 87:085cde657901 | 292 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 87:085cde657901 | 293 | |
mbed_official | 181:a4cbdfbbd2f4 | 294 | while(length > 0) |
mbed_official | 181:a4cbdfbbd2f4 | 295 | { |
mbed_official | 181:a4cbdfbbd2f4 | 296 | /* Wait until RXNE flag is set */ |
mbed_official | 181:a4cbdfbbd2f4 | 297 | // Wait until the byte is received |
mbed_official | 181:a4cbdfbbd2f4 | 298 | Timeout = FLAG_TIMEOUT; |
mbed_official | 181:a4cbdfbbd2f4 | 299 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) { |
mbed_official | 181:a4cbdfbbd2f4 | 300 | Timeout--; |
mbed_official | 181:a4cbdfbbd2f4 | 301 | if (Timeout == 0) { |
mbed_official | 181:a4cbdfbbd2f4 | 302 | return 0; |
mbed_official | 181:a4cbdfbbd2f4 | 303 | } |
mbed_official | 181:a4cbdfbbd2f4 | 304 | } |
mbed_official | 181:a4cbdfbbd2f4 | 305 | |
mbed_official | 181:a4cbdfbbd2f4 | 306 | /* Read data from DR */ |
mbed_official | 181:a4cbdfbbd2f4 | 307 | (*data++) = I2cHandle.Instance->DR; |
mbed_official | 181:a4cbdfbbd2f4 | 308 | length--; |
mbed_official | 181:a4cbdfbbd2f4 | 309 | size++; |
mbed_official | 181:a4cbdfbbd2f4 | 310 | |
mbed_official | 181:a4cbdfbbd2f4 | 311 | if((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == SET) && (length != 0)){ |
mbed_official | 181:a4cbdfbbd2f4 | 312 | /* Read data from DR */ |
mbed_official | 181:a4cbdfbbd2f4 | 313 | (*data++) = I2cHandle.Instance->DR; |
mbed_official | 181:a4cbdfbbd2f4 | 314 | length--; |
mbed_official | 181:a4cbdfbbd2f4 | 315 | size++; |
mbed_official | 181:a4cbdfbbd2f4 | 316 | } |
mbed_official | 87:085cde657901 | 317 | } |
mbed_official | 87:085cde657901 | 318 | |
mbed_official | 181:a4cbdfbbd2f4 | 319 | /* Wait until STOP flag is set */ |
mbed_official | 181:a4cbdfbbd2f4 | 320 | Timeout = FLAG_TIMEOUT; |
mbed_official | 181:a4cbdfbbd2f4 | 321 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) { |
mbed_official | 181:a4cbdfbbd2f4 | 322 | Timeout--; |
mbed_official | 181:a4cbdfbbd2f4 | 323 | if (Timeout == 0) { |
mbed_official | 181:a4cbdfbbd2f4 | 324 | return 0; |
mbed_official | 181:a4cbdfbbd2f4 | 325 | } |
mbed_official | 181:a4cbdfbbd2f4 | 326 | } |
mbed_official | 181:a4cbdfbbd2f4 | 327 | |
mbed_official | 181:a4cbdfbbd2f4 | 328 | /* Clear STOP flag */ |
mbed_official | 181:a4cbdfbbd2f4 | 329 | __HAL_I2C_CLEAR_STOPFLAG(&I2cHandle); |
mbed_official | 181:a4cbdfbbd2f4 | 330 | |
mbed_official | 181:a4cbdfbbd2f4 | 331 | /* Wait until BUSY flag is reset */ |
mbed_official | 181:a4cbdfbbd2f4 | 332 | Timeout = FLAG_TIMEOUT; |
mbed_official | 181:a4cbdfbbd2f4 | 333 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == SET) { |
mbed_official | 181:a4cbdfbbd2f4 | 334 | Timeout--; |
mbed_official | 181:a4cbdfbbd2f4 | 335 | if (Timeout == 0) { |
mbed_official | 181:a4cbdfbbd2f4 | 336 | return 0; |
mbed_official | 181:a4cbdfbbd2f4 | 337 | } |
mbed_official | 181:a4cbdfbbd2f4 | 338 | } |
mbed_official | 181:a4cbdfbbd2f4 | 339 | |
mbed_official | 181:a4cbdfbbd2f4 | 340 | return size; |
mbed_official | 87:085cde657901 | 341 | } |
mbed_official | 87:085cde657901 | 342 | |
mbed_official | 87:085cde657901 | 343 | int i2c_slave_write(i2c_t *obj, const char *data, int length) { |
mbed_official | 181:a4cbdfbbd2f4 | 344 | uint32_t Timeout; |
mbed_official | 181:a4cbdfbbd2f4 | 345 | int size = 0; |
mbed_official | 87:085cde657901 | 346 | if (length == 0) return 0; |
mbed_official | 172:2f4f8c56b261 | 347 | |
mbed_official | 87:085cde657901 | 348 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
mbed_official | 87:085cde657901 | 349 | |
mbed_official | 181:a4cbdfbbd2f4 | 350 | while(length > 0) |
mbed_official | 181:a4cbdfbbd2f4 | 351 | { |
mbed_official | 181:a4cbdfbbd2f4 | 352 | /* Wait until TXE flag is set */ |
mbed_official | 181:a4cbdfbbd2f4 | 353 | Timeout = FLAG_TIMEOUT; |
mbed_official | 181:a4cbdfbbd2f4 | 354 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXE) == RESET) { |
mbed_official | 181:a4cbdfbbd2f4 | 355 | Timeout--; |
mbed_official | 181:a4cbdfbbd2f4 | 356 | if (Timeout == 0) { |
mbed_official | 181:a4cbdfbbd2f4 | 357 | return 0; |
mbed_official | 181:a4cbdfbbd2f4 | 358 | } |
mbed_official | 181:a4cbdfbbd2f4 | 359 | } |
mbed_official | 181:a4cbdfbbd2f4 | 360 | |
mbed_official | 181:a4cbdfbbd2f4 | 361 | |
mbed_official | 181:a4cbdfbbd2f4 | 362 | /* Write data to DR */ |
mbed_official | 181:a4cbdfbbd2f4 | 363 | I2cHandle.Instance->DR = (*data++); |
mbed_official | 181:a4cbdfbbd2f4 | 364 | length--; |
mbed_official | 181:a4cbdfbbd2f4 | 365 | size++; |
mbed_official | 181:a4cbdfbbd2f4 | 366 | |
mbed_official | 181:a4cbdfbbd2f4 | 367 | if((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == SET) && (length != 0)) |
mbed_official | 181:a4cbdfbbd2f4 | 368 | { |
mbed_official | 181:a4cbdfbbd2f4 | 369 | /* Write data to DR */ |
mbed_official | 181:a4cbdfbbd2f4 | 370 | I2cHandle.Instance->DR = (*data++); |
mbed_official | 181:a4cbdfbbd2f4 | 371 | length--; |
mbed_official | 181:a4cbdfbbd2f4 | 372 | size++; |
mbed_official | 181:a4cbdfbbd2f4 | 373 | } |
mbed_official | 181:a4cbdfbbd2f4 | 374 | } |
mbed_official | 181:a4cbdfbbd2f4 | 375 | |
mbed_official | 181:a4cbdfbbd2f4 | 376 | /* Wait until AF flag is set */ |
mbed_official | 181:a4cbdfbbd2f4 | 377 | Timeout = FLAG_TIMEOUT; |
mbed_official | 181:a4cbdfbbd2f4 | 378 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_AF) == RESET) { |
mbed_official | 181:a4cbdfbbd2f4 | 379 | Timeout--; |
mbed_official | 181:a4cbdfbbd2f4 | 380 | if (Timeout == 0) { |
mbed_official | 181:a4cbdfbbd2f4 | 381 | return 0; |
mbed_official | 181:a4cbdfbbd2f4 | 382 | } |
mbed_official | 181:a4cbdfbbd2f4 | 383 | } |
mbed_official | 181:a4cbdfbbd2f4 | 384 | |
mbed_official | 181:a4cbdfbbd2f4 | 385 | |
mbed_official | 181:a4cbdfbbd2f4 | 386 | /* Clear AF flag */ |
mbed_official | 181:a4cbdfbbd2f4 | 387 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF); |
mbed_official | 181:a4cbdfbbd2f4 | 388 | |
mbed_official | 181:a4cbdfbbd2f4 | 389 | |
mbed_official | 181:a4cbdfbbd2f4 | 390 | /* Wait until BUSY flag is reset */ |
mbed_official | 181:a4cbdfbbd2f4 | 391 | Timeout = FLAG_TIMEOUT; |
mbed_official | 181:a4cbdfbbd2f4 | 392 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == SET) { |
mbed_official | 181:a4cbdfbbd2f4 | 393 | Timeout--; |
mbed_official | 181:a4cbdfbbd2f4 | 394 | if (Timeout == 0) { |
mbed_official | 181:a4cbdfbbd2f4 | 395 | return 0; |
mbed_official | 181:a4cbdfbbd2f4 | 396 | } |
mbed_official | 87:085cde657901 | 397 | } |
mbed_official | 87:085cde657901 | 398 | |
mbed_official | 181:a4cbdfbbd2f4 | 399 | I2cHandle.State = HAL_I2C_STATE_READY; |
mbed_official | 181:a4cbdfbbd2f4 | 400 | |
mbed_official | 181:a4cbdfbbd2f4 | 401 | /* Process Unlocked */ |
mbed_official | 181:a4cbdfbbd2f4 | 402 | __HAL_UNLOCK(&I2cHandle); |
mbed_official | 181:a4cbdfbbd2f4 | 403 | |
mbed_official | 181:a4cbdfbbd2f4 | 404 | return size; |
mbed_official | 87:085cde657901 | 405 | } |
mbed_official | 87:085cde657901 | 406 | |
mbed_official | 87:085cde657901 | 407 | |
mbed_official | 87:085cde657901 | 408 | #endif // DEVICE_I2CSLAVE |
mbed_official | 87:085cde657901 | 409 | |
mbed_official | 87:085cde657901 | 410 | #endif // DEVICE_I2C |