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) {
62 __HAL_RCC_I2C1_CONFIG(RCC_I2C1CLKSOURCE_SYSCLK);
65 pinmap_pinout(sda, PinMap_I2C_SDA);
66 pinmap_pinout(scl, PinMap_I2C_SCL);
67 pin_mode(sda, OpenDrain);
68 pin_mode(scl, OpenDrain);
71 // Enable I2C2 clock and pinout if not done
72 if ((obj->i2c == I2C_2) && !i2c2_inited) {
76 pinmap_pinout(sda, PinMap_I2C_SDA);
77 pinmap_pinout(scl, PinMap_I2C_SCL);
78 pin_mode(sda, OpenDrain);
79 pin_mode(scl, OpenDrain);
82 // Reset to clear pending flags if any
86 i2c_frequency(obj, 100000); // 100 kHz per default
89 void i2c_frequency(i2c_t *obj, int hz)
91 MBED_ASSERT((hz == 100000) || (hz == 400000) || (hz == 1000000));
92 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
96 timeout = LONG_TIMEOUT;
97 while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));
99 // Common settings: I2C clock = 48 MHz, Analog filter = ON, Digital filter coefficient = 0
102 I2cHandle.Init.Timing = 0x10805E89; // Standard mode with Rise Time = 400ns and Fall Time = 100ns
105 I2cHandle.Init.Timing = 0x00901850; // Fast mode with Rise Time = 250ns and Fall Time = 100ns
108 I2cHandle.Init.Timing = 0x00700818; // Fast mode Plus with Rise Time = 60ns and Fall Time = 100ns
115 I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
116 I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
117 I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
118 I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
119 I2cHandle.Init.OwnAddress1 = 0;
120 I2cHandle.Init.OwnAddress2 = 0;
121 I2cHandle.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
122 HAL_I2C_Init(&I2cHandle);
125 inline int i2c_start(i2c_t *obj)
127 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
130 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
132 // Clear Acknowledge failure flag
133 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF);
135 // Generate the START condition
136 i2c->CR2 |= I2C_CR2_START;
138 // Wait the START condition has been correctly sent
139 timeout = FLAG_TIMEOUT;
140 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == RESET) {
141 if ((timeout--) == 0) {
149 inline int i2c_stop(i2c_t *obj)
151 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
153 // Generate the STOP condition
154 i2c->CR2 |= I2C_CR2_STOP;
159 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
161 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
162 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
167 // Update CR2 register
168 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)))
169 | (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);
172 for (count = 0; count < length; count++) {
173 value = i2c_byte_read(obj, 0);
174 data[count] = (char)value;
177 // Wait transfer complete
178 timeout = FLAG_TIMEOUT;
179 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) {
185 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC);
187 // If not repeated start, send stop.
190 // Wait until STOPF flag is set
191 timeout = FLAG_TIMEOUT;
192 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
199 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF);
205 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
207 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
208 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
212 // Update CR2 register
213 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)))
214 | (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);
216 for (count = 0; count < length; count++) {
217 i2c_byte_write(obj, data[count]);
220 // Wait transfer complete
221 timeout = FLAG_TIMEOUT;
222 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) {
228 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC);
230 // If not repeated start, send stop
233 // Wait until STOPF flag is set
234 timeout = FLAG_TIMEOUT;
235 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
242 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF);
248 int i2c_byte_read(i2c_t *obj, int last)
250 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
253 // Wait until the byte is received
254 timeout = FLAG_TIMEOUT;
255 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) {
256 if ((timeout--) == 0) {
261 return (int)i2c->RXDR;
264 int i2c_byte_write(i2c_t *obj, int data)
266 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
269 // Wait until the previous byte is transmitted
270 timeout = FLAG_TIMEOUT;
271 while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXIS) == RESET) {
272 if ((timeout--) == 0) {
277 i2c->TXDR = (uint8_t)data;
282 void i2c_reset(i2c_t *obj)
287 timeout = LONG_TIMEOUT;
288 while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));
290 if (obj->i2c == I2C_1) {
291 __I2C1_FORCE_RESET();
292 __I2C1_RELEASE_RESET();
294 if (obj->i2c == I2C_2) {
295 __I2C2_FORCE_RESET();
296 __I2C2_RELEASE_RESET();
302 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask)
304 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
308 i2c->OAR1 &= (uint32_t)(~I2C_OAR1_OA1EN);
309 // Get the old register value
311 // Reset address bits
314 tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits
315 // Store the new register value
318 i2c->OAR1 |= I2C_OAR1_OA1EN;
321 void i2c_slave_mode(i2c_t *obj, int enable_slave)
323 I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
326 // Get the old register value
329 // Enable / disable slave
330 if (enable_slave == 1) {
331 tmpreg |= I2C_OAR1_OA1EN;
333 tmpreg &= (uint32_t)(~I2C_OAR1_OA1EN);
341 #define NoData 0 // the slave has not been addressed
342 #define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter)
343 #define WriteGeneral 2 // the master is writing to all slave
344 #define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
346 int i2c_slave_receive(i2c_t *obj)
348 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
349 int retValue = NoData;
351 if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == 1) {
352 if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == 1) {
353 if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_DIR) == 1)
354 retValue = ReadAddressed;
356 retValue = WriteAddressed;
358 __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_ADDR);
365 int i2c_slave_read(i2c_t *obj, char *data, int length)
369 while (size < length) data[size++] = (char)i2c_byte_read(obj, 0);
374 int i2c_slave_write(i2c_t *obj, const char *data, int length)
377 I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
380 i2c_byte_write(obj, data[size]);
382 } while (size < length);
388 #endif // DEVICE_I2CSLAVE