Toyomasa Watarai / mbed-src

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Mon Nov 03 10:30:07 2014 +0000
Revision:
381:5460fc57b6e4
Synchronized with git revision 02478cd1f27fc7b9643486472635eb515b2bca81

Full URL: https://github.com/mbedmicro/mbed/commit/02478cd1f27fc7b9643486472635eb515b2bca81/

Target: LPC1549 - Fix serial interrupt issues (issue report #616)

Who changed what in which revision?

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