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;
50 void i2c_init(i2c_t *obj, PinName sda, PinName scl)
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 // Enable I2C1 clock and pinout if not done
60 if ((obj->i2c == I2C_1) && !i2c1_inited) {
64 pinmap_pinout(sda, PinMap_I2C_SDA);
65 pinmap_pinout(scl, PinMap_I2C_SCL);
66 pin_mode(sda, OpenDrain);
67 pin_mode(scl, OpenDrain);
69 // Enable I2C2 clock and pinout if not done
70 if ((obj->i2c == I2C_2) && !i2c2_inited) {
74 pinmap_pinout(sda, PinMap_I2C_SDA);
75 pinmap_pinout(scl, PinMap_I2C_SCL);
76 pin_mode(sda, OpenDrain);
77 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.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
102 I2cHandle.Init.ClockSpeed = hz;
103 I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
104 I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2;
105 I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
106 I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
107 I2cHandle.Init.OwnAddress1 = 0;
108 I2cHandle.Init.OwnAddress2 = 0;
109 HAL_I2C_Init(&I2cHandle);
111 /* Enable Address Acknowledge */
112 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);
161 // Wait until SB flag is set
162 timeout = FLAG_TIMEOUT;
163 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_SB) == RESET) {
170 i2c->DR = I2C_7BIT_ADD_READ(address);
173 // Wait address is acknowledged
174 timeout = FLAG_TIMEOUT;
175 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == RESET) {
181 __HAL_I2C_CLEAR_ADDRFLAG(&I2cHandle);
183 // Read all bytes except last one
184 for (count = 0; count < (length - 1); count++) {
185 value = i2c_byte_read(obj, 0);
186 data[count] = (char)value;
189 // If not repeated start, send stop.
190 // Warning: must be done BEFORE the data is read.
195 // Read the last byte
196 value = i2c_byte_read(obj, 1);
197 data[count] = (char)value;
202 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
204 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
205 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
211 // Wait until SB flag is set
212 timeout = FLAG_TIMEOUT;
213 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_SB) == RESET) {
220 i2c->DR = I2C_7BIT_ADD_WRITE(address);
223 // Wait address is acknowledged
224 timeout = FLAG_TIMEOUT;
225 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == RESET) {
231 __HAL_I2C_CLEAR_ADDRFLAG(&I2cHandle);
233 for (count = 0; count < length; count++) {
234 if (i2c_byte_write(obj, data[count]) != 1) {
240 // If not repeated start, send stop.
248 int i2c_byte_read(i2c_t *obj, int last)
250 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
254 // Don't acknowledge the last byte
255 i2c->CR1 &= ~I2C_CR1_ACK;
257 // Acknowledge the byte
258 i2c->CR1 |= I2C_CR1_ACK;
261 // Wait until the byte is received
262 timeout = FLAG_TIMEOUT;
263 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) {
264 if ((timeout--) == 0) {
272 int i2c_byte_write(i2c_t *obj, int data)
274 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
277 i2c->DR = (uint8_t)data;
279 // Wait until the byte is transmitted
280 timeout = FLAG_TIMEOUT;
281 while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXE) == RESET) &&
282 (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == RESET)) {
283 if ((timeout--) == 0) {
291 void i2c_reset(i2c_t *obj)
296 timeout = LONG_TIMEOUT;
297 while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));
299 if (obj->i2c == I2C_1) {
300 __I2C1_FORCE_RESET();
301 __I2C1_RELEASE_RESET();
303 if (obj->i2c == I2C_2) {
304 __I2C2_FORCE_RESET();
305 __I2C2_RELEASE_RESET();
311 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask)
313 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
316 // Get the old register value
318 // Reset address bits
321 tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits
322 // Store the new register value
326 void i2c_slave_mode(i2c_t *obj, int enable_slave)
328 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
331 /* Enable Address Acknowledge */
332 I2cHandle.Instance->CR1 |= I2C_CR1_ACK;
337 #define NoData 0 // the slave has not been addressed
338 #define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter)
339 #define WriteGeneral 2 // the master is writing to all slave
340 #define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
342 int i2c_slave_receive(i2c_t *obj)
344 int retValue = NoData;
346 if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == 1) {
347 if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == 1) {
348 if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TRA) == 1)
349 retValue = ReadAddressed;
351 retValue = WriteAddressed;
353 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_ADDR);
360 int i2c_slave_read(i2c_t *obj, char *data, int length)
365 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
368 /* Wait until RXNE flag is set */
369 // Wait until the byte is received
370 Timeout = FLAG_TIMEOUT;
371 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) {
378 /* Read data from DR */
379 (*data++) = I2cHandle.Instance->DR;
383 if ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == SET) && (length != 0)) {
384 /* Read data from DR */
385 (*data++) = I2cHandle.Instance->DR;
391 /* Wait until STOP flag is set */
392 Timeout = FLAG_TIMEOUT;
393 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
400 /* Clear STOP flag */
401 __HAL_I2C_CLEAR_STOPFLAG(&I2cHandle);
403 /* Wait until BUSY flag is reset */
404 Timeout = FLAG_TIMEOUT;
405 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == SET) {
415 int i2c_slave_write(i2c_t *obj, const char *data, int length)
420 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
423 /* Wait until TXE flag is set */
424 Timeout = FLAG_TIMEOUT;
425 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXE) == RESET) {
433 /* Write data to DR */
434 I2cHandle.Instance->DR = (*data++);
438 if ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == SET) && (length != 0)) {
439 /* Write data to DR */
440 I2cHandle.Instance->DR = (*data++);
446 /* Wait until AF flag is set */
447 Timeout = FLAG_TIMEOUT;
448 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_AF) == RESET) {
457 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF);
460 /* Wait until BUSY flag is reset */
461 Timeout = FLAG_TIMEOUT;
462 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == SET) {
469 I2cHandle.State = HAL_I2C_STATE_READY;
471 /* Process Unlocked */
472 __HAL_UNLOCK(&I2cHandle);
478 #endif // DEVICE_I2CSLAVE