1 /* mbed Microcontroller Library
2 *******************************************************************************
3 * Copyright (c) 2014, STMicroelectronics
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *******************************************************************************
30 #include "mbed_assert.h"
37 #include "PeripheralPins.h"
39 /* Timeout values for flags and events waiting loops. These timeouts are
40 not based on accurate values, they just guarantee that the application will
41 not remain stuck if the I2C communication is corrupted. */
42 #define FLAG_TIMEOUT ((int)0x4000)
43 #define LONG_TIMEOUT ((int)0x8000)
45 I2C_HandleTypeDef I2cHandle;
51 void i2c_init(i2c_t *obj, PinName sda, PinName scl)
53 // Determine the I2C to use
54 I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
55 I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
57 obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
58 MBED_ASSERT(obj->i2c != (I2CName)NC);
60 // Enable I2C clock and pinout if not done
61 if ((obj->i2c == I2C_1) && !i2c1_inited) {
63 __HAL_RCC_I2C1_CONFIG(RCC_I2C1CLKSOURCE_SYSCLK);
65 // Configure I2C1 pins
66 pinmap_pinout(sda, PinMap_I2C_SDA);
67 pinmap_pinout(scl, PinMap_I2C_SCL);
68 pin_mode(sda, OpenDrain);
69 pin_mode(scl, OpenDrain);
72 #if defined(I2C2_BASE)
73 if ((obj->i2c == I2C_2) && !i2c2_inited) {
76 // Configure I2C2 pins
77 pinmap_pinout(sda, PinMap_I2C_SDA);
78 pinmap_pinout(scl, PinMap_I2C_SCL);
79 pin_mode(sda, OpenDrain);
80 pin_mode(scl, OpenDrain);
84 #if defined(I2C3_BASE)
85 if ((obj->i2c == I2C_3) && !i2c3_inited) {
88 // Configure I2C3 pins
89 pinmap_pinout(sda, PinMap_I2C_SDA);
90 pinmap_pinout(scl, PinMap_I2C_SCL);
91 pin_mode(sda, OpenDrain);
92 pin_mode(scl, OpenDrain);
96 // Reset to clear pending flags if any
100 i2c_frequency(obj, 100000); // 100 kHz per default
103 void i2c_frequency(i2c_t *obj, int hz)
107 MBED_ASSERT((hz == 100000) || (hz == 400000) || (hz == 1000000));
109 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
113 timeout = LONG_TIMEOUT;
114 while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));
116 // Update the SystemCoreClock variable.
117 SystemCoreClockUpdate();
120 Values calculated with I2C_Timing_Configuration_V1.0.1.xls file (see AN4235)
121 * Standard mode (up to 100 kHz)
122 * Fast Mode (up to 400 kHz)
123 * Fast Mode Plus (up to 1 MHz)
124 Below values obtained with:
125 - I2C clock source = 64 MHz (System Clock w/ HSI) or 72 (System Clock w/ HSE)
126 - Analog filter delay = ON
127 - Digital filter coefficient = 0
129 if (SystemCoreClock == 64000000) {
132 tim = 0x10B17DB4; // Standard mode with Rise time = 120ns, Fall time = 120ns
135 tim = 0x00E22163; // Fast Mode with Rise time = 120ns, Fall time = 120ns
138 tim = 0x00A00D1E; // Fast Mode Plus with Rise time = 120ns, Fall time = 10ns
143 } else if (SystemCoreClock == 72000000) {
146 tim = 0x10D28DCB; // Standard mode with Rise time = 120ns, Fall time = 120ns
149 tim = 0x00F32571; // Fast Mode with Rise time = 120ns, Fall time = 120ns
152 tim = 0x00C00D24; // Fast Mode Plus with Rise time = 120ns, Fall time = 10ns
159 // Enable the Fast Mode Plus capability
161 if (obj->i2c == I2C_1) {
162 __HAL_SYSCFG_FASTMODEPLUS_ENABLE(HAL_SYSCFG_FASTMODEPLUS_I2C1);
164 #if defined(I2C2_BASE)
165 if (obj->i2c == I2C_2) {
166 __HAL_SYSCFG_FASTMODEPLUS_ENABLE(HAL_SYSCFG_FASTMODEPLUS_I2C2);
169 #if defined(I2C3_BASE)
170 if (obj->i2c == I2C_3) {
171 __HAL_SYSCFG_FASTMODEPLUS_ENABLE(HAL_SYSCFG_FASTMODEPLUS_I2C3);
177 I2cHandle.Init.Timing = tim;
178 I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
179 I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
180 I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
181 I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
182 I2cHandle.Init.OwnAddress1 = 0;
183 I2cHandle.Init.OwnAddress2 = 0;
184 I2cHandle.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
185 HAL_I2C_Init(&I2cHandle);
188 inline int i2c_start(i2c_t *obj)
190 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
193 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
195 // Clear Acknowledge failure flag
196 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF);
198 // Generate the START condition
199 i2c->CR2 |= I2C_CR2_START;
201 // Wait the START condition has been correctly sent
202 timeout = FLAG_TIMEOUT;
203 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == RESET) {
204 if ((timeout--) == 0) {
212 inline int i2c_stop(i2c_t *obj)
214 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
216 // Generate the STOP condition
217 i2c->CR2 |= I2C_CR2_STOP;
222 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
224 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
225 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
230 /* update CR2 register */
231 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)))
232 | (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);
235 for (count = 0; count < length; count++) {
236 value = i2c_byte_read(obj, 0);
237 data[count] = (char)value;
240 // Wait transfer complete
241 timeout = LONG_TIMEOUT;
242 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) {
249 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC);
251 // If not repeated start, send stop.
254 /* Wait until STOPF flag is set */
255 timeout = FLAG_TIMEOUT;
256 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
262 /* Clear STOP Flag */
263 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF);
269 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
271 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
272 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
276 /* update CR2 register */
277 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)))
278 | (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);
280 for (count = 0; count < length; count++) {
281 i2c_byte_write(obj, data[count]);
284 // Wait transfer complete
285 timeout = FLAG_TIMEOUT;
286 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) {
293 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC);
295 // If not repeated start, send stop.
298 /* Wait until STOPF flag is set */
299 timeout = FLAG_TIMEOUT;
300 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
306 /* Clear STOP Flag */
307 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF);
313 int i2c_byte_read(i2c_t *obj, int last)
315 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
318 // Wait until the byte is received
319 timeout = FLAG_TIMEOUT;
320 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) {
321 if ((timeout--) == 0) {
326 return (int)i2c->RXDR;
329 int i2c_byte_write(i2c_t *obj, int data)
331 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
334 // Wait until the previous byte is transmitted
335 timeout = FLAG_TIMEOUT;
336 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXIS) == RESET) {
337 if ((timeout--) == 0) {
342 i2c->TXDR = (uint8_t)data;
347 void i2c_reset(i2c_t *obj)
352 timeout = LONG_TIMEOUT;
353 while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));
355 __I2C1_FORCE_RESET();
356 __I2C1_RELEASE_RESET();
361 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask)
363 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
367 i2c->OAR1 &= (uint32_t)(~I2C_OAR1_OA1EN);
368 // Get the old register value
370 // Reset address bits
373 tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits
374 // Store the new register value
377 i2c->OAR1 |= I2C_OAR1_OA1EN;
380 void i2c_slave_mode(i2c_t *obj, int enable_slave)
383 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
386 // Get the old register value
389 // Enable / disable slave
390 if (enable_slave == 1) {
391 tmpreg |= I2C_OAR1_OA1EN;
393 tmpreg &= (uint32_t)(~I2C_OAR1_OA1EN);
402 #define NoData 0 // the slave has not been addressed
403 #define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter)
404 #define WriteGeneral 2 // the master is writing to all slave
405 #define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
407 int i2c_slave_receive(i2c_t *obj)
409 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
410 int retValue = NoData;
412 if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == 1) {
413 if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == 1) {
414 if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_DIR) == 1)
415 retValue = ReadAddressed;
417 retValue = WriteAddressed;
418 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_ADDR);
425 int i2c_slave_read(i2c_t *obj, char *data, int length)
429 while (size < length) data[size++] = (char)i2c_byte_read(obj, 0);
434 int i2c_slave_write(i2c_t *obj, const char *data, int length)
437 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
440 i2c_byte_write(obj, data[size]);
442 } while (size < length);
448 #endif // DEVICE_I2CSLAVE