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)0x1000)
43 #define LONG_TIMEOUT ((int)0x8000)
45 I2C_HandleTypeDef I2cHandle;
47 void i2c_init(i2c_t *obj, PinName sda, PinName scl)
49 static int i2c1_inited = 0;
50 static int i2c2_inited = 0;
52 // Determine the I2C to use
53 I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
54 I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
56 obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
57 MBED_ASSERT(obj->i2c != (I2CName)NC);
59 // Check if I2C peripherals are already configured
60 if ((obj->i2c == I2C_1) && i2c1_inited) return;
61 if ((obj->i2c == I2C_2) && i2c2_inited) return;
64 if (obj->i2c == I2C_1) {
69 if (obj->i2c == I2C_2) {
75 pinmap_pinout(sda, PinMap_I2C_SDA);
76 pinmap_pinout(scl, PinMap_I2C_SCL);
77 pin_mode(sda, OpenDrain);
78 pin_mode(scl, OpenDrain);
80 // Reset to clear pending flags if any
84 i2c_frequency(obj, 100000); // 100 kHz per default
86 // I2C master by default
90 void i2c_frequency(i2c_t *obj, int hz)
92 MBED_ASSERT((hz != 0) && (hz <= 400000));
93 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
97 timeout = LONG_TIMEOUT;
98 while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));
101 I2cHandle.Init.ClockSpeed = hz;
102 I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2;
103 I2cHandle.Init.OwnAddress1 = 0;
104 I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
105 I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
106 I2cHandle.Init.OwnAddress2 = 0;
107 I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
108 I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
109 HAL_I2C_Init(&I2cHandle);
112 // Enable Address Acknowledge
113 I2cHandle.Instance->CR1 |= I2C_CR1_ACK;
117 inline int i2c_start(i2c_t *obj)
119 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
122 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
124 // Clear Acknowledge failure flag
125 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF);
127 // Generate the START condition
128 i2c->CR1 |= I2C_CR1_START;
130 // Wait the START condition has been correctly sent
131 timeout = FLAG_TIMEOUT;
132 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_SB) == RESET) {
133 if ((timeout--) == 0) {
141 inline int i2c_stop(i2c_t *obj)
143 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
145 // Generate the STOP condition
146 i2c->CR1 |= I2C_CR1_STOP;
151 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
153 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
154 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
159 // Generate start condition
162 // Send address for read
163 i2c->DR = __HAL_I2C_7BIT_ADD_READ(address);
165 // Wait address is acknowledged
166 timeout = FLAG_TIMEOUT;
167 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == RESET) {
173 __HAL_I2C_CLEAR_ADDRFLAG(&I2cHandle);
175 // Read all bytes except last one
176 for (count = 0; count < (length - 1); count++) {
177 value = i2c_byte_read(obj, 0);
178 data[count] = (char)value;
181 // If not repeated start, send stop.
182 // Warning: must be done BEFORE the data is read.
187 // Read the last byte
188 value = i2c_byte_read(obj, 1);
189 data[count] = (char)value;
194 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
196 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
197 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
201 // Generate start condition
204 // Send address for write
205 i2c->DR = __HAL_I2C_7BIT_ADD_WRITE(address);
207 // Wait address is acknowledged
208 timeout = FLAG_TIMEOUT;
209 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == RESET) {
215 __HAL_I2C_CLEAR_ADDRFLAG(&I2cHandle);
218 for (count = 0; count < length; count++) {
219 if (i2c_byte_write(obj, data[count]) != 1) {
225 // If not repeated start, send stop.
233 int i2c_byte_read(i2c_t *obj, int last)
235 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
239 // Don't acknowledge the last byte
240 i2c->CR1 &= ~I2C_CR1_ACK;
242 // Acknowledge the byte
243 i2c->CR1 |= I2C_CR1_ACK;
246 // Wait until the byte is received
247 timeout = FLAG_TIMEOUT;
248 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) {
249 if ((timeout--) == 0) {
257 int i2c_byte_write(i2c_t *obj, int data)
259 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
262 i2c->DR = (uint8_t)data;
264 // Wait until the byte is transmitted
265 timeout = FLAG_TIMEOUT;
266 while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXE) == RESET) &&
267 (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == RESET)) {
268 if ((timeout--) == 0) {
276 void i2c_reset(i2c_t *obj)
281 timeout = LONG_TIMEOUT;
282 while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));
284 if (obj->i2c == I2C_1) {
285 __I2C1_FORCE_RESET();
286 __I2C1_RELEASE_RESET();
289 if (obj->i2c == I2C_2) {
290 __I2C2_FORCE_RESET();
291 __I2C2_RELEASE_RESET();
297 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask)
299 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
302 // Get the old register value
304 // Reset address bits
307 tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits
308 // Store the new register value
312 void i2c_slave_mode(i2c_t *obj, int enable_slave)
314 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
317 /* Enable Address Acknowledge */
318 I2cHandle.Instance->CR1 |= I2C_CR1_ACK;
323 #define NoData 0 // the slave has not been addressed
324 #define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter)
325 #define WriteGeneral 2 // the master is writing to all slave
326 #define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
328 int i2c_slave_receive(i2c_t *obj)
330 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
331 int retValue = NoData;
333 if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == 1) {
334 if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == 1) {
335 if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TRA) == 1) {
336 retValue = ReadAddressed;
338 retValue = WriteAddressed;
340 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_ADDR);
347 int i2c_slave_read(i2c_t *obj, char *data, int length)
352 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
355 // Wait until RXNE flag is set
356 // Wait until the byte is received
357 Timeout = FLAG_TIMEOUT;
358 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) {
366 (*data++) = I2cHandle.Instance->DR;
370 if ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == SET) && (length != 0)) {
372 (*data++) = I2cHandle.Instance->DR;
378 // Wait until STOP flag is set
379 Timeout = FLAG_TIMEOUT;
380 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
388 __HAL_I2C_CLEAR_STOPFLAG(&I2cHandle);
390 // Wait until BUSY flag is reset
391 Timeout = FLAG_TIMEOUT;
392 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == SET) {
402 int i2c_slave_write(i2c_t *obj, const char *data, int length)
407 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
410 // Wait until TXE flag is set
411 Timeout = FLAG_TIMEOUT;
412 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXE) == RESET) {
420 I2cHandle.Instance->DR = (*data++);
424 if ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == SET) && (length != 0)) {
426 I2cHandle.Instance->DR = (*data++);
432 // Wait until AF flag is set
433 Timeout = FLAG_TIMEOUT;
434 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_AF) == RESET) {
442 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF);
444 // Wait until BUSY flag is reset
445 Timeout = FLAG_TIMEOUT;
446 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == SET) {
457 #endif // DEVICE_I2CSLAVE