mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Wed Jun 11 16:00:09 2014 +0100
Revision:
227:7bd0639b8911
Parent:
133:d4dda5c437f0
Child:
242:7074e42da0b2
Synchronized with git revision d58d532ebc0e0a96f4fffb8edefc082b71b964af

Full URL: https://github.com/mbedmicro/mbed/commit/d58d532ebc0e0a96f4fffb8edefc082b71b964af/

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 133:d4dda5c437f0 1 /* mbed Microcontroller Library
mbed_official 133:d4dda5c437f0 2 *******************************************************************************
mbed_official 133:d4dda5c437f0 3 * Copyright (c) 2014, STMicroelectronics
mbed_official 133:d4dda5c437f0 4 * All rights reserved.
mbed_official 133:d4dda5c437f0 5 *
mbed_official 133:d4dda5c437f0 6 * Redistribution and use in source and binary forms, with or without
mbed_official 133:d4dda5c437f0 7 * modification, are permitted provided that the following conditions are met:
mbed_official 133:d4dda5c437f0 8 *
mbed_official 133:d4dda5c437f0 9 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 133:d4dda5c437f0 10 * this list of conditions and the following disclaimer.
mbed_official 133:d4dda5c437f0 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 133:d4dda5c437f0 12 * this list of conditions and the following disclaimer in the documentation
mbed_official 133:d4dda5c437f0 13 * and/or other materials provided with the distribution.
mbed_official 133:d4dda5c437f0 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 133:d4dda5c437f0 15 * may be used to endorse or promote products derived from this software
mbed_official 133:d4dda5c437f0 16 * without specific prior written permission.
mbed_official 133:d4dda5c437f0 17 *
mbed_official 133:d4dda5c437f0 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 133:d4dda5c437f0 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 133:d4dda5c437f0 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 133:d4dda5c437f0 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 133:d4dda5c437f0 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 133:d4dda5c437f0 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 133:d4dda5c437f0 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 133:d4dda5c437f0 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 133:d4dda5c437f0 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 133:d4dda5c437f0 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 133:d4dda5c437f0 28 *******************************************************************************
mbed_official 133:d4dda5c437f0 29 */
mbed_official 227:7bd0639b8911 30 #include "mbed_assert.h"
mbed_official 133:d4dda5c437f0 31 #include "i2c_api.h"
mbed_official 133:d4dda5c437f0 32
mbed_official 133:d4dda5c437f0 33 #if DEVICE_I2C
mbed_official 133:d4dda5c437f0 34
mbed_official 133:d4dda5c437f0 35 #include "cmsis.h"
mbed_official 133:d4dda5c437f0 36 #include "pinmap.h"
mbed_official 133:d4dda5c437f0 37 #include "stm32f4xx_hal.h"
mbed_official 133:d4dda5c437f0 38
mbed_official 133:d4dda5c437f0 39 /* Timeout values for flags and events waiting loops. These timeouts are
mbed_official 227:7bd0639b8911 40 not based on accurate values, they just guarantee that the application will
mbed_official 227:7bd0639b8911 41 not remain stuck if the I2C communication is corrupted. */
mbed_official 133:d4dda5c437f0 42 #define FLAG_TIMEOUT ((int)0x1000)
mbed_official 133:d4dda5c437f0 43 #define LONG_TIMEOUT ((int)0x8000)
mbed_official 133:d4dda5c437f0 44
mbed_official 133:d4dda5c437f0 45 static const PinMap PinMap_I2C_SDA[] = {
mbed_official 133:d4dda5c437f0 46 {PB_11, I2C_2, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF4_I2C2)},
mbed_official 133:d4dda5c437f0 47 {PF_0 , I2C_2, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF4_I2C2)},
mbed_official 133:d4dda5c437f0 48 {PH_5 , I2C_2, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF4_I2C2)},
mbed_official 133:d4dda5c437f0 49 {PB_7, I2C_1, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF4_I2C1)},
mbed_official 133:d4dda5c437f0 50 {PB_9, I2C_1, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF4_I2C1)},
mbed_official 133:d4dda5c437f0 51 {PC_9, I2C_3, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF4_I2C3)},
mbed_official 133:d4dda5c437f0 52 {PH_8, I2C_3, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF4_I2C3)},
mbed_official 133:d4dda5c437f0 53 {NC, NC, 0}
mbed_official 133:d4dda5c437f0 54 };
mbed_official 133:d4dda5c437f0 55
mbed_official 133:d4dda5c437f0 56 static const PinMap PinMap_I2C_SCL[] = {
mbed_official 133:d4dda5c437f0 57 {PA_8, I2C_3, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF4_I2C3)},
mbed_official 133:d4dda5c437f0 58 {PB_6, I2C_1, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF4_I2C1)},
mbed_official 133:d4dda5c437f0 59 {PB_8, I2C_1, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF4_I2C1)},
mbed_official 133:d4dda5c437f0 60 {PB_10, I2C_2, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF4_I2C2)},
mbed_official 133:d4dda5c437f0 61 {PF_1 , I2C_2, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF4_I2C2)},
mbed_official 133:d4dda5c437f0 62 {PH_4 , I2C_2, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF4_I2C2)},
mbed_official 133:d4dda5c437f0 63 {NC, NC, 0}
mbed_official 133:d4dda5c437f0 64 };
mbed_official 133:d4dda5c437f0 65
mbed_official 133:d4dda5c437f0 66 I2C_HandleTypeDef I2cHandle;
mbed_official 133:d4dda5c437f0 67
mbed_official 227:7bd0639b8911 68 void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
mbed_official 133:d4dda5c437f0 69 // Determine the I2C to use
mbed_official 133:d4dda5c437f0 70 I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
mbed_official 133:d4dda5c437f0 71 I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
mbed_official 133:d4dda5c437f0 72
mbed_official 133:d4dda5c437f0 73 obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
mbed_official 227:7bd0639b8911 74 MBED_ASSERT(obj->i2c != (I2CName)NC);
mbed_official 133:d4dda5c437f0 75
mbed_official 133:d4dda5c437f0 76 // Enable I2C clock
mbed_official 227:7bd0639b8911 77 if (obj->i2c == I2C_1) {
mbed_official 133:d4dda5c437f0 78 __I2C1_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 79 }
mbed_official 133:d4dda5c437f0 80 if (obj->i2c == I2C_2) {
mbed_official 133:d4dda5c437f0 81 __I2C2_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 82 }
mbed_official 133:d4dda5c437f0 83 if (obj->i2c == I2C_3) {
mbed_official 133:d4dda5c437f0 84 __I2C3_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 85 }
mbed_official 133:d4dda5c437f0 86
mbed_official 133:d4dda5c437f0 87 // Configure I2C pins
mbed_official 133:d4dda5c437f0 88 pinmap_pinout(sda, PinMap_I2C_SDA);
mbed_official 133:d4dda5c437f0 89 pinmap_pinout(scl, PinMap_I2C_SCL);
mbed_official 133:d4dda5c437f0 90 pin_mode(sda, OpenDrain);
mbed_official 133:d4dda5c437f0 91 pin_mode(scl, OpenDrain);
mbed_official 133:d4dda5c437f0 92
mbed_official 133:d4dda5c437f0 93 // Reset to clear pending flags if any
mbed_official 133:d4dda5c437f0 94 i2c_reset(obj);
mbed_official 133:d4dda5c437f0 95
mbed_official 133:d4dda5c437f0 96 // I2C configuration
mbed_official 227:7bd0639b8911 97 i2c_frequency(obj, 100000); // 100 kHz per default
mbed_official 133:d4dda5c437f0 98 }
mbed_official 133:d4dda5c437f0 99
mbed_official 133:d4dda5c437f0 100 void i2c_frequency(i2c_t *obj, int hz) {
mbed_official 227:7bd0639b8911 101 MBED_ASSERT((hz != 0) && (hz <= 400000));
mbed_official 133:d4dda5c437f0 102 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
mbed_official 133:d4dda5c437f0 103
mbed_official 227:7bd0639b8911 104
mbed_official 227:7bd0639b8911 105 // I2C configuration
mbed_official 227:7bd0639b8911 106 I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
mbed_official 227:7bd0639b8911 107 I2cHandle.Init.ClockSpeed = hz;
mbed_official 227:7bd0639b8911 108 I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
mbed_official 227:7bd0639b8911 109 I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2;
mbed_official 227:7bd0639b8911 110 I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
mbed_official 227:7bd0639b8911 111 I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
mbed_official 227:7bd0639b8911 112 I2cHandle.Init.OwnAddress1 = 0;
mbed_official 227:7bd0639b8911 113 I2cHandle.Init.OwnAddress2 = 0;
mbed_official 227:7bd0639b8911 114 HAL_I2C_Init(&I2cHandle);
mbed_official 227:7bd0639b8911 115
mbed_official 133:d4dda5c437f0 116 }
mbed_official 133:d4dda5c437f0 117
mbed_official 133:d4dda5c437f0 118 inline int i2c_start(i2c_t *obj) {
mbed_official 133:d4dda5c437f0 119 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 133:d4dda5c437f0 120 int timeout;
mbed_official 133:d4dda5c437f0 121
mbed_official 133:d4dda5c437f0 122 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
mbed_official 133:d4dda5c437f0 123
mbed_official 133:d4dda5c437f0 124 // Clear Acknowledge failure flag
mbed_official 133:d4dda5c437f0 125 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF);
mbed_official 133:d4dda5c437f0 126
mbed_official 133:d4dda5c437f0 127 // Generate the START condition
mbed_official 133:d4dda5c437f0 128 i2c->CR1 |= I2C_CR1_START;
mbed_official 133:d4dda5c437f0 129
mbed_official 133:d4dda5c437f0 130 // Wait the START condition has been correctly sent
mbed_official 133:d4dda5c437f0 131 timeout = FLAG_TIMEOUT;
mbed_official 133:d4dda5c437f0 132 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_SB) == RESET) {
mbed_official 133:d4dda5c437f0 133 if ((timeout--) == 0) {
mbed_official 133:d4dda5c437f0 134 return 1;
mbed_official 133:d4dda5c437f0 135 }
mbed_official 133:d4dda5c437f0 136 }
mbed_official 133:d4dda5c437f0 137
mbed_official 133:d4dda5c437f0 138 return 0;
mbed_official 133:d4dda5c437f0 139 }
mbed_official 133:d4dda5c437f0 140
mbed_official 133:d4dda5c437f0 141 inline int i2c_stop(i2c_t *obj) {
mbed_official 133:d4dda5c437f0 142 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 133:d4dda5c437f0 143
mbed_official 133:d4dda5c437f0 144 // Generate the STOP condition
mbed_official 133:d4dda5c437f0 145 i2c->CR1 |= I2C_CR1_STOP;
mbed_official 133:d4dda5c437f0 146
mbed_official 133:d4dda5c437f0 147 return 0;
mbed_official 133:d4dda5c437f0 148 }
mbed_official 133:d4dda5c437f0 149
mbed_official 227:7bd0639b8911 150 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
mbed_official 133:d4dda5c437f0 151 if (length == 0) return 0;
mbed_official 133:d4dda5c437f0 152
mbed_official 133:d4dda5c437f0 153 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
mbed_official 133:d4dda5c437f0 154
mbed_official 133:d4dda5c437f0 155 // Reception process with 5 seconds timeout
mbed_official 133:d4dda5c437f0 156 if (HAL_I2C_Master_Receive(&I2cHandle, (uint16_t)address, (uint8_t *)data, length, 5000) != HAL_OK) {
mbed_official 133:d4dda5c437f0 157 return 0; // Error
mbed_official 133:d4dda5c437f0 158 }
mbed_official 133:d4dda5c437f0 159
mbed_official 133:d4dda5c437f0 160 return length;
mbed_official 133:d4dda5c437f0 161 }
mbed_official 133:d4dda5c437f0 162
mbed_official 133:d4dda5c437f0 163 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
mbed_official 133:d4dda5c437f0 164 if (length == 0) return 0;
mbed_official 133:d4dda5c437f0 165
mbed_official 133:d4dda5c437f0 166 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
mbed_official 133:d4dda5c437f0 167
mbed_official 133:d4dda5c437f0 168 // Transmission process with 5 seconds timeout
mbed_official 133:d4dda5c437f0 169 if (HAL_I2C_Master_Transmit(&I2cHandle, (uint16_t)address, (uint8_t *)data, length, 5000) != HAL_OK) {
mbed_official 133:d4dda5c437f0 170 return 0; // Error
mbed_official 133:d4dda5c437f0 171 }
mbed_official 133:d4dda5c437f0 172
mbed_official 133:d4dda5c437f0 173 return length;
mbed_official 133:d4dda5c437f0 174 }
mbed_official 133:d4dda5c437f0 175
mbed_official 133:d4dda5c437f0 176 int i2c_byte_read(i2c_t *obj, int last) {
mbed_official 133:d4dda5c437f0 177 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 133:d4dda5c437f0 178 int timeout;
mbed_official 133:d4dda5c437f0 179
mbed_official 133:d4dda5c437f0 180 if (last) {
mbed_official 133:d4dda5c437f0 181 // Don't acknowledge the last byte
mbed_official 133:d4dda5c437f0 182 i2c->CR1 &= ~I2C_CR1_ACK;
mbed_official 133:d4dda5c437f0 183 } else {
mbed_official 133:d4dda5c437f0 184 // Acknowledge the byte
mbed_official 133:d4dda5c437f0 185 i2c->CR1 |= I2C_CR1_ACK;
mbed_official 133:d4dda5c437f0 186 }
mbed_official 133:d4dda5c437f0 187
mbed_official 133:d4dda5c437f0 188 // Wait until the byte is received
mbed_official 133:d4dda5c437f0 189 timeout = FLAG_TIMEOUT;
mbed_official 133:d4dda5c437f0 190 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) {
mbed_official 133:d4dda5c437f0 191 if ((timeout--) == 0) {
mbed_official 133:d4dda5c437f0 192 return 0;
mbed_official 133:d4dda5c437f0 193 }
mbed_official 133:d4dda5c437f0 194 }
mbed_official 133:d4dda5c437f0 195
mbed_official 133:d4dda5c437f0 196 return (int)i2c->DR;
mbed_official 133:d4dda5c437f0 197 }
mbed_official 133:d4dda5c437f0 198
mbed_official 133:d4dda5c437f0 199 int i2c_byte_write(i2c_t *obj, int data) {
mbed_official 133:d4dda5c437f0 200 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 133:d4dda5c437f0 201 int timeout;
mbed_official 133:d4dda5c437f0 202
mbed_official 133:d4dda5c437f0 203 i2c->DR = (uint8_t)data;
mbed_official 133:d4dda5c437f0 204
mbed_official 133:d4dda5c437f0 205 // Wait until the byte is transmitted
mbed_official 227:7bd0639b8911 206 timeout = FLAG_TIMEOUT;
mbed_official 133:d4dda5c437f0 207 while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXE) == RESET) &&
mbed_official 133:d4dda5c437f0 208 (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == RESET)) {
mbed_official 133:d4dda5c437f0 209 if ((timeout--) == 0) {
mbed_official 133:d4dda5c437f0 210 return 0;
mbed_official 133:d4dda5c437f0 211 }
mbed_official 133:d4dda5c437f0 212 }
mbed_official 133:d4dda5c437f0 213
mbed_official 133:d4dda5c437f0 214 return 1;
mbed_official 133:d4dda5c437f0 215 }
mbed_official 133:d4dda5c437f0 216
mbed_official 133:d4dda5c437f0 217 void i2c_reset(i2c_t *obj) {
mbed_official 227:7bd0639b8911 218 if (obj->i2c == I2C_1) {
mbed_official 133:d4dda5c437f0 219 __I2C1_FORCE_RESET();
mbed_official 133:d4dda5c437f0 220 __I2C1_RELEASE_RESET();
mbed_official 133:d4dda5c437f0 221 }
mbed_official 133:d4dda5c437f0 222 if (obj->i2c == I2C_2) {
mbed_official 133:d4dda5c437f0 223 __I2C2_FORCE_RESET();
mbed_official 133:d4dda5c437f0 224 __I2C2_RELEASE_RESET();
mbed_official 133:d4dda5c437f0 225 }
mbed_official 133:d4dda5c437f0 226 if (obj->i2c == I2C_3) {
mbed_official 133:d4dda5c437f0 227 __I2C3_FORCE_RESET();
mbed_official 133:d4dda5c437f0 228 __I2C3_RELEASE_RESET();
mbed_official 133:d4dda5c437f0 229 }
mbed_official 133:d4dda5c437f0 230 }
mbed_official 133:d4dda5c437f0 231
mbed_official 133:d4dda5c437f0 232 #if DEVICE_I2CSLAVE
mbed_official 133:d4dda5c437f0 233
mbed_official 133:d4dda5c437f0 234 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) {
mbed_official 133:d4dda5c437f0 235 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
mbed_official 133:d4dda5c437f0 236 uint16_t tmpreg;
mbed_official 133:d4dda5c437f0 237
mbed_official 133:d4dda5c437f0 238 // Get the old register value
mbed_official 133:d4dda5c437f0 239 tmpreg = i2c->OAR1;
mbed_official 133:d4dda5c437f0 240 // Reset address bits
mbed_official 133:d4dda5c437f0 241 tmpreg &= 0xFC00;
mbed_official 133:d4dda5c437f0 242 // Set new address
mbed_official 133:d4dda5c437f0 243 tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits
mbed_official 133:d4dda5c437f0 244 // Store the new register value
mbed_official 133:d4dda5c437f0 245 i2c->OAR1 = tmpreg;
mbed_official 133:d4dda5c437f0 246 }
mbed_official 133:d4dda5c437f0 247
mbed_official 133:d4dda5c437f0 248 void i2c_slave_mode(i2c_t *obj, int enable_slave) {
mbed_official 133:d4dda5c437f0 249 // Nothing to do
mbed_official 133:d4dda5c437f0 250 }
mbed_official 133:d4dda5c437f0 251
mbed_official 133:d4dda5c437f0 252 // See I2CSlave.h
mbed_official 133:d4dda5c437f0 253 #define NoData 0 // the slave has not been addressed
mbed_official 133:d4dda5c437f0 254 #define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter)
mbed_official 133:d4dda5c437f0 255 #define WriteGeneral 2 // the master is writing to all slave
mbed_official 133:d4dda5c437f0 256 #define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
mbed_official 133:d4dda5c437f0 257
mbed_official 133:d4dda5c437f0 258 int i2c_slave_receive(i2c_t *obj) {
mbed_official 133:d4dda5c437f0 259 // TO BE DONE
mbed_official 133:d4dda5c437f0 260 return(0);
mbed_official 133:d4dda5c437f0 261 }
mbed_official 133:d4dda5c437f0 262
mbed_official 133:d4dda5c437f0 263 int i2c_slave_read(i2c_t *obj, char *data, int length) {
mbed_official 133:d4dda5c437f0 264 if (length == 0) return 0;
mbed_official 133:d4dda5c437f0 265
mbed_official 133:d4dda5c437f0 266 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
mbed_official 133:d4dda5c437f0 267
mbed_official 133:d4dda5c437f0 268 // Reception process with 5 seconds timeout
mbed_official 133:d4dda5c437f0 269 if (HAL_I2C_Slave_Receive(&I2cHandle, (uint8_t *)data, length, 5000) != HAL_OK) {
mbed_official 133:d4dda5c437f0 270 return 0; // Error
mbed_official 133:d4dda5c437f0 271 }
mbed_official 133:d4dda5c437f0 272
mbed_official 133:d4dda5c437f0 273 return length;
mbed_official 133:d4dda5c437f0 274 }
mbed_official 133:d4dda5c437f0 275
mbed_official 133:d4dda5c437f0 276 int i2c_slave_write(i2c_t *obj, const char *data, int length) {
mbed_official 133:d4dda5c437f0 277 if (length == 0) return 0;
mbed_official 133:d4dda5c437f0 278
mbed_official 133:d4dda5c437f0 279 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
mbed_official 133:d4dda5c437f0 280
mbed_official 133:d4dda5c437f0 281 // Transmission process with 5 seconds timeout
mbed_official 133:d4dda5c437f0 282 if (HAL_I2C_Slave_Transmit(&I2cHandle, (uint8_t *)data, length, 5000) != HAL_OK) {
mbed_official 133:d4dda5c437f0 283 return 0; // Error
mbed_official 133:d4dda5c437f0 284 }
mbed_official 133:d4dda5c437f0 285
mbed_official 133:d4dda5c437f0 286 return length;
mbed_official 133:d4dda5c437f0 287 }
mbed_official 133:d4dda5c437f0 288
mbed_official 133:d4dda5c437f0 289
mbed_official 133:d4dda5c437f0 290 #endif // DEVICE_I2CSLAVE
mbed_official 133:d4dda5c437f0 291
mbed_official 133:d4dda5c437f0 292 #endif // DEVICE_I2C