SPKT

Dependencies:   F746_GUI SD_PlayerSkeleton F746_SAI_IO

Committer:
phungductung
Date:
Tue Jun 04 21:37:21 2019 +0000
Revision:
0:8ede47d38d10
SPKT

Who changed what in which revision?

UserRevisionLine numberNew contents of line
phungductung 0:8ede47d38d10 1 /* mbed Microcontroller Library
phungductung 0:8ede47d38d10 2 *******************************************************************************
phungductung 0:8ede47d38d10 3 * Copyright (c) 2015, STMicroelectronics
phungductung 0:8ede47d38d10 4 * All rights reserved.
phungductung 0:8ede47d38d10 5 *
phungductung 0:8ede47d38d10 6 * Redistribution and use in source and binary forms, with or without
phungductung 0:8ede47d38d10 7 * modification, are permitted provided that the following conditions are met:
phungductung 0:8ede47d38d10 8 *
phungductung 0:8ede47d38d10 9 * 1. Redistributions of source code must retain the above copyright notice,
phungductung 0:8ede47d38d10 10 * this list of conditions and the following disclaimer.
phungductung 0:8ede47d38d10 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
phungductung 0:8ede47d38d10 12 * this list of conditions and the following disclaimer in the documentation
phungductung 0:8ede47d38d10 13 * and/or other materials provided with the distribution.
phungductung 0:8ede47d38d10 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
phungductung 0:8ede47d38d10 15 * may be used to endorse or promote products derived from this software
phungductung 0:8ede47d38d10 16 * without specific prior written permission.
phungductung 0:8ede47d38d10 17 *
phungductung 0:8ede47d38d10 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
phungductung 0:8ede47d38d10 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
phungductung 0:8ede47d38d10 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
phungductung 0:8ede47d38d10 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
phungductung 0:8ede47d38d10 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
phungductung 0:8ede47d38d10 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
phungductung 0:8ede47d38d10 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
phungductung 0:8ede47d38d10 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
phungductung 0:8ede47d38d10 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
phungductung 0:8ede47d38d10 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
phungductung 0:8ede47d38d10 28 *******************************************************************************
phungductung 0:8ede47d38d10 29 */
phungductung 0:8ede47d38d10 30 #include "mbed_assert.h"
phungductung 0:8ede47d38d10 31 #include "i2c_api.h"
phungductung 0:8ede47d38d10 32
phungductung 0:8ede47d38d10 33 #if DEVICE_I2C
phungductung 0:8ede47d38d10 34
phungductung 0:8ede47d38d10 35 #include "cmsis.h"
phungductung 0:8ede47d38d10 36 #include "pinmap.h"
phungductung 0:8ede47d38d10 37 #include "PeripheralPins.h"
phungductung 0:8ede47d38d10 38 #include "mbed_error.h"
phungductung 0:8ede47d38d10 39
phungductung 0:8ede47d38d10 40 /* Timeout values for flags and events waiting loops. These timeouts are
phungductung 0:8ede47d38d10 41 not based on accurate values, they just guarantee that the application will
phungductung 0:8ede47d38d10 42 not remain stuck if the I2C communication is corrupted. */
phungductung 0:8ede47d38d10 43 #define FLAG_TIMEOUT ((int)0x4000)
phungductung 0:8ede47d38d10 44 #define LONG_TIMEOUT ((int)0x8000)
phungductung 0:8ede47d38d10 45
phungductung 0:8ede47d38d10 46 I2C_HandleTypeDef I2cHandle;
phungductung 0:8ede47d38d10 47
phungductung 0:8ede47d38d10 48 int i2c1_inited = 0;
phungductung 0:8ede47d38d10 49 int i2c2_inited = 0;
phungductung 0:8ede47d38d10 50 int i2c3_inited = 0;
phungductung 0:8ede47d38d10 51 int i2c4_inited = 0;
phungductung 0:8ede47d38d10 52
phungductung 0:8ede47d38d10 53
phungductung 0:8ede47d38d10 54 void i2c_init(i2c_t *obj, PinName sda, PinName scl)
phungductung 0:8ede47d38d10 55 {
phungductung 0:8ede47d38d10 56 // Determine the I2C to use
phungductung 0:8ede47d38d10 57 I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
phungductung 0:8ede47d38d10 58 I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
phungductung 0:8ede47d38d10 59
phungductung 0:8ede47d38d10 60 obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
phungductung 0:8ede47d38d10 61 MBED_ASSERT(obj->i2c != (I2CName)NC);
phungductung 0:8ede47d38d10 62
phungductung 0:8ede47d38d10 63 // Enable I2C clock and pinout if not done
phungductung 0:8ede47d38d10 64 if ((obj->i2c == I2C_1) && !i2c1_inited) {
phungductung 0:8ede47d38d10 65 i2c1_inited = 1;
phungductung 0:8ede47d38d10 66 __HAL_RCC_I2C1_CONFIG(RCC_I2C1CLKSOURCE_PCLK1);
phungductung 0:8ede47d38d10 67 __HAL_RCC_I2C1_CLK_ENABLE();
phungductung 0:8ede47d38d10 68 // Configure I2C1 pins
phungductung 0:8ede47d38d10 69 pinmap_pinout(sda, PinMap_I2C_SDA);
phungductung 0:8ede47d38d10 70 pinmap_pinout(scl, PinMap_I2C_SCL);
phungductung 0:8ede47d38d10 71 pin_mode(sda, OpenDrain);
phungductung 0:8ede47d38d10 72 pin_mode(scl, OpenDrain);
phungductung 0:8ede47d38d10 73 }
phungductung 0:8ede47d38d10 74
phungductung 0:8ede47d38d10 75 #if defined(I2C2_BASE)
phungductung 0:8ede47d38d10 76 if ((obj->i2c == I2C_2) && !i2c2_inited) {
phungductung 0:8ede47d38d10 77 i2c2_inited = 1;
phungductung 0:8ede47d38d10 78 __HAL_RCC_I2C2_CONFIG(RCC_I2C2CLKSOURCE_PCLK1);
phungductung 0:8ede47d38d10 79 __HAL_RCC_I2C2_CLK_ENABLE();
phungductung 0:8ede47d38d10 80 // Configure I2C2 pins
phungductung 0:8ede47d38d10 81 pinmap_pinout(sda, PinMap_I2C_SDA);
phungductung 0:8ede47d38d10 82 pinmap_pinout(scl, PinMap_I2C_SCL);
phungductung 0:8ede47d38d10 83 pin_mode(sda, OpenDrain);
phungductung 0:8ede47d38d10 84 pin_mode(scl, OpenDrain);
phungductung 0:8ede47d38d10 85 }
phungductung 0:8ede47d38d10 86 #endif
phungductung 0:8ede47d38d10 87
phungductung 0:8ede47d38d10 88 #if defined(I2C3_BASE)
phungductung 0:8ede47d38d10 89 if ((obj->i2c == I2C_3) && !i2c3_inited) {
phungductung 0:8ede47d38d10 90 i2c3_inited = 1;
phungductung 0:8ede47d38d10 91 __HAL_RCC_I2C3_CONFIG(RCC_I2C3CLKSOURCE_PCLK1);
phungductung 0:8ede47d38d10 92 __HAL_RCC_I2C3_CLK_ENABLE();
phungductung 0:8ede47d38d10 93 // Configure I2C3 pins
phungductung 0:8ede47d38d10 94 pinmap_pinout(sda, PinMap_I2C_SDA);
phungductung 0:8ede47d38d10 95 pinmap_pinout(scl, PinMap_I2C_SCL);
phungductung 0:8ede47d38d10 96 pin_mode(sda, OpenDrain);
phungductung 0:8ede47d38d10 97 pin_mode(scl, OpenDrain);
phungductung 0:8ede47d38d10 98 }
phungductung 0:8ede47d38d10 99 #endif
phungductung 0:8ede47d38d10 100
phungductung 0:8ede47d38d10 101 #if defined(I2C4_BASE)
phungductung 0:8ede47d38d10 102 if ((obj->i2c == I2C_4) && !i2c4_inited) {
phungductung 0:8ede47d38d10 103 i2c4_inited = 1;
phungductung 0:8ede47d38d10 104 __HAL_RCC_I2C4_CONFIG(RCC_I2C4CLKSOURCE_PCLK1);
phungductung 0:8ede47d38d10 105 __HAL_RCC_I2C4_CLK_ENABLE();
phungductung 0:8ede47d38d10 106 // Configure I2C4 pins
phungductung 0:8ede47d38d10 107 pinmap_pinout(sda, PinMap_I2C_SDA);
phungductung 0:8ede47d38d10 108 pinmap_pinout(scl, PinMap_I2C_SCL);
phungductung 0:8ede47d38d10 109 pin_mode(sda, OpenDrain);
phungductung 0:8ede47d38d10 110 pin_mode(scl, OpenDrain);
phungductung 0:8ede47d38d10 111 }
phungductung 0:8ede47d38d10 112 #endif
phungductung 0:8ede47d38d10 113
phungductung 0:8ede47d38d10 114 // Reset to clear pending flags if any
phungductung 0:8ede47d38d10 115 i2c_reset(obj);
phungductung 0:8ede47d38d10 116
phungductung 0:8ede47d38d10 117 // I2C configuration
phungductung 0:8ede47d38d10 118 i2c_frequency(obj, 100000); // 100 kHz per default
phungductung 0:8ede47d38d10 119 }
phungductung 0:8ede47d38d10 120
phungductung 0:8ede47d38d10 121 void i2c_frequency(i2c_t *obj, int hz)
phungductung 0:8ede47d38d10 122 {
phungductung 0:8ede47d38d10 123 uint32_t tim = 0;
phungductung 0:8ede47d38d10 124
phungductung 0:8ede47d38d10 125 MBED_ASSERT((hz == 100000) || (hz == 400000) || (hz == 1000000));
phungductung 0:8ede47d38d10 126
phungductung 0:8ede47d38d10 127 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
phungductung 0:8ede47d38d10 128 int timeout;
phungductung 0:8ede47d38d10 129
phungductung 0:8ede47d38d10 130 // wait before init
phungductung 0:8ede47d38d10 131 timeout = LONG_TIMEOUT;
phungductung 0:8ede47d38d10 132 while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0)) {}
phungductung 0:8ede47d38d10 133
phungductung 0:8ede47d38d10 134 /*
phungductung 0:8ede47d38d10 135 Values calculated with I2C_Timing_Configuration tool (excel file)
phungductung 0:8ede47d38d10 136 * Standard mode (up to 100 kHz)
phungductung 0:8ede47d38d10 137 * Fast Mode (up to 400 kHz)
phungductung 0:8ede47d38d10 138 * Fast Mode Plus (up to 1 MHz)
phungductung 0:8ede47d38d10 139 Below values obtained with:
phungductung 0:8ede47d38d10 140 - I2Cx clock source = APB1CLK = 54 MHz
phungductung 0:8ede47d38d10 141 - Analog filter delay = ON
phungductung 0:8ede47d38d10 142 - Digital filter coefficient = 0
phungductung 0:8ede47d38d10 143 */
phungductung 0:8ede47d38d10 144 switch (hz) {
phungductung 0:8ede47d38d10 145 case 100000:
phungductung 0:8ede47d38d10 146 tim = 0x10916998; // Standard mode with Rise time = 120ns, Fall time = 120ns
phungductung 0:8ede47d38d10 147 break;
phungductung 0:8ede47d38d10 148 case 400000:
phungductung 0:8ede47d38d10 149 tim = 0x00B11B54; // Fast Mode with Rise time = 120ns, Fall time = 120ns
phungductung 0:8ede47d38d10 150 break;
phungductung 0:8ede47d38d10 151 case 1000000:
phungductung 0:8ede47d38d10 152 tim = 0x0090091B; // Fast Mode Plus with Rise time = 120ns, Fall time = 10ns
phungductung 0:8ede47d38d10 153 break;
phungductung 0:8ede47d38d10 154 default:
phungductung 0:8ede47d38d10 155 break;
phungductung 0:8ede47d38d10 156 }
phungductung 0:8ede47d38d10 157
phungductung 0:8ede47d38d10 158 // I2C configuration
phungductung 0:8ede47d38d10 159 I2cHandle.Init.Timing = tim;
phungductung 0:8ede47d38d10 160 I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
phungductung 0:8ede47d38d10 161 I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
phungductung 0:8ede47d38d10 162 I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
phungductung 0:8ede47d38d10 163 I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
phungductung 0:8ede47d38d10 164 I2cHandle.Init.OwnAddress1 = 0;
phungductung 0:8ede47d38d10 165 I2cHandle.Init.OwnAddress2 = 0;
phungductung 0:8ede47d38d10 166 I2cHandle.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
phungductung 0:8ede47d38d10 167
phungductung 0:8ede47d38d10 168 if (HAL_I2C_Init(&I2cHandle) != HAL_OK) {
phungductung 0:8ede47d38d10 169 error("Cannot initialize I2C");
phungductung 0:8ede47d38d10 170 }
phungductung 0:8ede47d38d10 171 }
phungductung 0:8ede47d38d10 172
phungductung 0:8ede47d38d10 173 inline int i2c_start(i2c_t *obj)
phungductung 0:8ede47d38d10 174 {
phungductung 0:8ede47d38d10 175 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
phungductung 0:8ede47d38d10 176 int timeout;
phungductung 0:8ede47d38d10 177
phungductung 0:8ede47d38d10 178 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
phungductung 0:8ede47d38d10 179
phungductung 0:8ede47d38d10 180 // Clear Acknowledge failure flag
phungductung 0:8ede47d38d10 181 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF);
phungductung 0:8ede47d38d10 182
phungductung 0:8ede47d38d10 183 // Wait the STOP condition has been previously correctly sent
phungductung 0:8ede47d38d10 184 timeout = FLAG_TIMEOUT;
phungductung 0:8ede47d38d10 185 while ((i2c->CR2 & I2C_CR2_STOP) == I2C_CR2_STOP){
phungductung 0:8ede47d38d10 186 if ((timeout--) == 0) {
phungductung 0:8ede47d38d10 187 return 1;
phungductung 0:8ede47d38d10 188 }
phungductung 0:8ede47d38d10 189 }
phungductung 0:8ede47d38d10 190
phungductung 0:8ede47d38d10 191 // Generate the START condition
phungductung 0:8ede47d38d10 192 i2c->CR2 |= I2C_CR2_START;
phungductung 0:8ede47d38d10 193
phungductung 0:8ede47d38d10 194 // Wait the START condition has been correctly sent
phungductung 0:8ede47d38d10 195 timeout = FLAG_TIMEOUT;
phungductung 0:8ede47d38d10 196 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == RESET) {
phungductung 0:8ede47d38d10 197 if ((timeout--) == 0) {
phungductung 0:8ede47d38d10 198 return 1;
phungductung 0:8ede47d38d10 199 }
phungductung 0:8ede47d38d10 200 }
phungductung 0:8ede47d38d10 201
phungductung 0:8ede47d38d10 202 return 0;
phungductung 0:8ede47d38d10 203 }
phungductung 0:8ede47d38d10 204
phungductung 0:8ede47d38d10 205 inline int i2c_stop(i2c_t *obj)
phungductung 0:8ede47d38d10 206 {
phungductung 0:8ede47d38d10 207 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
phungductung 0:8ede47d38d10 208
phungductung 0:8ede47d38d10 209 // Generate the STOP condition
phungductung 0:8ede47d38d10 210 i2c->CR2 |= I2C_CR2_STOP;
phungductung 0:8ede47d38d10 211
phungductung 0:8ede47d38d10 212 return 0;
phungductung 0:8ede47d38d10 213 }
phungductung 0:8ede47d38d10 214
phungductung 0:8ede47d38d10 215 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
phungductung 0:8ede47d38d10 216 {
phungductung 0:8ede47d38d10 217 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
phungductung 0:8ede47d38d10 218 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
phungductung 0:8ede47d38d10 219 int timeout;
phungductung 0:8ede47d38d10 220 int count;
phungductung 0:8ede47d38d10 221 int value;
phungductung 0:8ede47d38d10 222
phungductung 0:8ede47d38d10 223 /* update CR2 register */
phungductung 0:8ede47d38d10 224 i2c->CR2 = (i2c->CR2 & (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP)))
phungductung 0:8ede47d38d10 225 | (uint32_t)(((uint32_t)address & I2C_CR2_SADD) | (((uint32_t)length << 16) & I2C_CR2_NBYTES) | (uint32_t)I2C_SOFTEND_MODE | (uint32_t)I2C_GENERATE_START_READ);
phungductung 0:8ede47d38d10 226
phungductung 0:8ede47d38d10 227 // Read all bytes
phungductung 0:8ede47d38d10 228 for (count = 0; count < length; count++) {
phungductung 0:8ede47d38d10 229 value = i2c_byte_read(obj, 0);
phungductung 0:8ede47d38d10 230 data[count] = (char)value;
phungductung 0:8ede47d38d10 231 }
phungductung 0:8ede47d38d10 232
phungductung 0:8ede47d38d10 233 // Wait transfer complete
phungductung 0:8ede47d38d10 234 timeout = LONG_TIMEOUT;
phungductung 0:8ede47d38d10 235 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) {
phungductung 0:8ede47d38d10 236 timeout--;
phungductung 0:8ede47d38d10 237 if (timeout == 0) {
phungductung 0:8ede47d38d10 238 return -1;
phungductung 0:8ede47d38d10 239 }
phungductung 0:8ede47d38d10 240 }
phungductung 0:8ede47d38d10 241
phungductung 0:8ede47d38d10 242 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC);
phungductung 0:8ede47d38d10 243
phungductung 0:8ede47d38d10 244 // If not repeated start, send stop.
phungductung 0:8ede47d38d10 245 if (stop) {
phungductung 0:8ede47d38d10 246 i2c_stop(obj);
phungductung 0:8ede47d38d10 247 /* Wait until STOPF flag is set */
phungductung 0:8ede47d38d10 248 timeout = FLAG_TIMEOUT;
phungductung 0:8ede47d38d10 249 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
phungductung 0:8ede47d38d10 250 timeout--;
phungductung 0:8ede47d38d10 251 if (timeout == 0) {
phungductung 0:8ede47d38d10 252 return -1;
phungductung 0:8ede47d38d10 253 }
phungductung 0:8ede47d38d10 254 }
phungductung 0:8ede47d38d10 255 /* Clear STOP Flag */
phungductung 0:8ede47d38d10 256 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF);
phungductung 0:8ede47d38d10 257 }
phungductung 0:8ede47d38d10 258
phungductung 0:8ede47d38d10 259 return length;
phungductung 0:8ede47d38d10 260 }
phungductung 0:8ede47d38d10 261
phungductung 0:8ede47d38d10 262 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
phungductung 0:8ede47d38d10 263 {
phungductung 0:8ede47d38d10 264 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
phungductung 0:8ede47d38d10 265 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
phungductung 0:8ede47d38d10 266 int timeout;
phungductung 0:8ede47d38d10 267 int count;
phungductung 0:8ede47d38d10 268
phungductung 0:8ede47d38d10 269 /* update CR2 register */
phungductung 0:8ede47d38d10 270 i2c->CR2 = (i2c->CR2 & (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP)))
phungductung 0:8ede47d38d10 271 | (uint32_t)(((uint32_t)address & I2C_CR2_SADD) | (((uint32_t)length << 16) & I2C_CR2_NBYTES) | (uint32_t)I2C_SOFTEND_MODE | (uint32_t)I2C_GENERATE_START_WRITE);
phungductung 0:8ede47d38d10 272
phungductung 0:8ede47d38d10 273 for (count = 0; count < length; count++) {
phungductung 0:8ede47d38d10 274 i2c_byte_write(obj, data[count]);
phungductung 0:8ede47d38d10 275 }
phungductung 0:8ede47d38d10 276
phungductung 0:8ede47d38d10 277 // Wait transfer complete
phungductung 0:8ede47d38d10 278 timeout = FLAG_TIMEOUT;
phungductung 0:8ede47d38d10 279 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) {
phungductung 0:8ede47d38d10 280 timeout--;
phungductung 0:8ede47d38d10 281 if (timeout == 0) {
phungductung 0:8ede47d38d10 282 return -1;
phungductung 0:8ede47d38d10 283 }
phungductung 0:8ede47d38d10 284 }
phungductung 0:8ede47d38d10 285
phungductung 0:8ede47d38d10 286 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC);
phungductung 0:8ede47d38d10 287
phungductung 0:8ede47d38d10 288 // If not repeated start, send stop.
phungductung 0:8ede47d38d10 289 if (stop) {
phungductung 0:8ede47d38d10 290 i2c_stop(obj);
phungductung 0:8ede47d38d10 291 /* Wait until STOPF flag is set */
phungductung 0:8ede47d38d10 292 timeout = FLAG_TIMEOUT;
phungductung 0:8ede47d38d10 293 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
phungductung 0:8ede47d38d10 294 timeout--;
phungductung 0:8ede47d38d10 295 if (timeout == 0) {
phungductung 0:8ede47d38d10 296 return -1;
phungductung 0:8ede47d38d10 297 }
phungductung 0:8ede47d38d10 298 }
phungductung 0:8ede47d38d10 299 /* Clear STOP Flag */
phungductung 0:8ede47d38d10 300 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF);
phungductung 0:8ede47d38d10 301 }
phungductung 0:8ede47d38d10 302
phungductung 0:8ede47d38d10 303 return count;
phungductung 0:8ede47d38d10 304 }
phungductung 0:8ede47d38d10 305
phungductung 0:8ede47d38d10 306 int i2c_byte_read(i2c_t *obj, int last)
phungductung 0:8ede47d38d10 307 {
phungductung 0:8ede47d38d10 308 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
phungductung 0:8ede47d38d10 309 int timeout;
phungductung 0:8ede47d38d10 310
phungductung 0:8ede47d38d10 311 // Wait until the byte is received
phungductung 0:8ede47d38d10 312 timeout = FLAG_TIMEOUT;
phungductung 0:8ede47d38d10 313 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) {
phungductung 0:8ede47d38d10 314 if ((timeout--) == 0) {
phungductung 0:8ede47d38d10 315 return -1;
phungductung 0:8ede47d38d10 316 }
phungductung 0:8ede47d38d10 317 }
phungductung 0:8ede47d38d10 318
phungductung 0:8ede47d38d10 319 return (int)i2c->RXDR;
phungductung 0:8ede47d38d10 320 }
phungductung 0:8ede47d38d10 321
phungductung 0:8ede47d38d10 322 int i2c_byte_write(i2c_t *obj, int data)
phungductung 0:8ede47d38d10 323 {
phungductung 0:8ede47d38d10 324 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
phungductung 0:8ede47d38d10 325 int timeout;
phungductung 0:8ede47d38d10 326
phungductung 0:8ede47d38d10 327 // Wait until the previous byte is transmitted
phungductung 0:8ede47d38d10 328 timeout = FLAG_TIMEOUT;
phungductung 0:8ede47d38d10 329 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXIS) == RESET) {
phungductung 0:8ede47d38d10 330 if ((timeout--) == 0) {
phungductung 0:8ede47d38d10 331 return 0;
phungductung 0:8ede47d38d10 332 }
phungductung 0:8ede47d38d10 333 }
phungductung 0:8ede47d38d10 334
phungductung 0:8ede47d38d10 335 i2c->TXDR = (uint8_t)data;
phungductung 0:8ede47d38d10 336
phungductung 0:8ede47d38d10 337 return 1;
phungductung 0:8ede47d38d10 338 }
phungductung 0:8ede47d38d10 339
phungductung 0:8ede47d38d10 340 void i2c_reset(i2c_t *obj)
phungductung 0:8ede47d38d10 341 {
phungductung 0:8ede47d38d10 342 int timeout;
phungductung 0:8ede47d38d10 343
phungductung 0:8ede47d38d10 344 // wait before reset
phungductung 0:8ede47d38d10 345 timeout = LONG_TIMEOUT;
phungductung 0:8ede47d38d10 346 while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));
phungductung 0:8ede47d38d10 347
phungductung 0:8ede47d38d10 348 __I2C1_FORCE_RESET();
phungductung 0:8ede47d38d10 349 __I2C1_RELEASE_RESET();
phungductung 0:8ede47d38d10 350 }
phungductung 0:8ede47d38d10 351
phungductung 0:8ede47d38d10 352 #if DEVICE_I2CSLAVE
phungductung 0:8ede47d38d10 353
phungductung 0:8ede47d38d10 354 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask)
phungductung 0:8ede47d38d10 355 {
phungductung 0:8ede47d38d10 356 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
phungductung 0:8ede47d38d10 357 uint16_t tmpreg;
phungductung 0:8ede47d38d10 358
phungductung 0:8ede47d38d10 359 // disable
phungductung 0:8ede47d38d10 360 i2c->OAR1 &= (uint32_t)(~I2C_OAR1_OA1EN);
phungductung 0:8ede47d38d10 361 // Get the old register value
phungductung 0:8ede47d38d10 362 tmpreg = i2c->OAR1;
phungductung 0:8ede47d38d10 363 // Reset address bits
phungductung 0:8ede47d38d10 364 tmpreg &= 0xFC00;
phungductung 0:8ede47d38d10 365 // Set new address
phungductung 0:8ede47d38d10 366 tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits
phungductung 0:8ede47d38d10 367 // Store the new register value
phungductung 0:8ede47d38d10 368 i2c->OAR1 = tmpreg;
phungductung 0:8ede47d38d10 369 // enable
phungductung 0:8ede47d38d10 370 i2c->OAR1 |= I2C_OAR1_OA1EN;
phungductung 0:8ede47d38d10 371 }
phungductung 0:8ede47d38d10 372
phungductung 0:8ede47d38d10 373 void i2c_slave_mode(i2c_t *obj, int enable_slave)
phungductung 0:8ede47d38d10 374 {
phungductung 0:8ede47d38d10 375
phungductung 0:8ede47d38d10 376 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
phungductung 0:8ede47d38d10 377 uint16_t tmpreg;
phungductung 0:8ede47d38d10 378
phungductung 0:8ede47d38d10 379 // Get the old register value
phungductung 0:8ede47d38d10 380 tmpreg = i2c->OAR1;
phungductung 0:8ede47d38d10 381
phungductung 0:8ede47d38d10 382 // Enable / disable slave
phungductung 0:8ede47d38d10 383 if (enable_slave == 1) {
phungductung 0:8ede47d38d10 384 tmpreg |= I2C_OAR1_OA1EN;
phungductung 0:8ede47d38d10 385 } else {
phungductung 0:8ede47d38d10 386 tmpreg &= (uint32_t)(~I2C_OAR1_OA1EN);
phungductung 0:8ede47d38d10 387 }
phungductung 0:8ede47d38d10 388
phungductung 0:8ede47d38d10 389 // Set new mode
phungductung 0:8ede47d38d10 390 i2c->OAR1 = tmpreg;
phungductung 0:8ede47d38d10 391
phungductung 0:8ede47d38d10 392 }
phungductung 0:8ede47d38d10 393
phungductung 0:8ede47d38d10 394 // See I2CSlave.h
phungductung 0:8ede47d38d10 395 #define NoData 0 // the slave has not been addressed
phungductung 0:8ede47d38d10 396 #define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter)
phungductung 0:8ede47d38d10 397 #define WriteGeneral 2 // the master is writing to all slave
phungductung 0:8ede47d38d10 398 #define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
phungductung 0:8ede47d38d10 399
phungductung 0:8ede47d38d10 400 int i2c_slave_receive(i2c_t *obj)
phungductung 0:8ede47d38d10 401 {
phungductung 0:8ede47d38d10 402 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
phungductung 0:8ede47d38d10 403 int retValue = NoData;
phungductung 0:8ede47d38d10 404
phungductung 0:8ede47d38d10 405 if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == 1) {
phungductung 0:8ede47d38d10 406 if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == 1) {
phungductung 0:8ede47d38d10 407 if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_DIR) == 1)
phungductung 0:8ede47d38d10 408 retValue = ReadAddressed;
phungductung 0:8ede47d38d10 409 else
phungductung 0:8ede47d38d10 410 retValue = WriteAddressed;
phungductung 0:8ede47d38d10 411 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_ADDR);
phungductung 0:8ede47d38d10 412 }
phungductung 0:8ede47d38d10 413 }
phungductung 0:8ede47d38d10 414
phungductung 0:8ede47d38d10 415 return (retValue);
phungductung 0:8ede47d38d10 416 }
phungductung 0:8ede47d38d10 417
phungductung 0:8ede47d38d10 418 int i2c_slave_read(i2c_t *obj, char *data, int length)
phungductung 0:8ede47d38d10 419 {
phungductung 0:8ede47d38d10 420 char size = 0;
phungductung 0:8ede47d38d10 421
phungductung 0:8ede47d38d10 422 while (size < length) data[size++] = (char)i2c_byte_read(obj, 0);
phungductung 0:8ede47d38d10 423
phungductung 0:8ede47d38d10 424 return size;
phungductung 0:8ede47d38d10 425 }
phungductung 0:8ede47d38d10 426
phungductung 0:8ede47d38d10 427 int i2c_slave_write(i2c_t *obj, const char *data, int length)
phungductung 0:8ede47d38d10 428 {
phungductung 0:8ede47d38d10 429 char size = 0;
phungductung 0:8ede47d38d10 430 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
phungductung 0:8ede47d38d10 431
phungductung 0:8ede47d38d10 432 do {
phungductung 0:8ede47d38d10 433 i2c_byte_write(obj, data[size]);
phungductung 0:8ede47d38d10 434 size++;
phungductung 0:8ede47d38d10 435 } while (size < length);
phungductung 0:8ede47d38d10 436
phungductung 0:8ede47d38d10 437 return size;
phungductung 0:8ede47d38d10 438 }
phungductung 0:8ede47d38d10 439
phungductung 0:8ede47d38d10 440
phungductung 0:8ede47d38d10 441 #endif // DEVICE_I2CSLAVE
phungductung 0:8ede47d38d10 442
phungductung 0:8ede47d38d10 443 #endif // DEVICE_I2C