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;
51 #if defined(I2C3_BASE)
52 static int i2c3_inited
= 0;
55 // Determine the I2C to use
56 I2CName i2c_sda
= (I2CName
)pinmap_peripheral(sda
, PinMap_I2C_SDA
);
57 I2CName i2c_scl
= (I2CName
)pinmap_peripheral(scl
, PinMap_I2C_SCL
);
59 obj
->i2c
= (I2CName
)pinmap_merge(i2c_sda
, i2c_scl
);
60 MBED_ASSERT(obj
->i2c
!= (I2CName
)NC
);
62 // Enable I2C1 clock and pinout if not done
63 if ((obj
->i2c
== I2C_1
) && !i2c1_inited
) {
65 __HAL_RCC_I2C1_CONFIG(RCC_I2C1CLKSOURCE_SYSCLK
);
68 pinmap_pinout(sda
, PinMap_I2C_SDA
);
69 pinmap_pinout(scl
, PinMap_I2C_SCL
);
70 pin_mode(sda
, OpenDrain
);
71 pin_mode(scl
, OpenDrain
);
74 // Enable I2C2 clock and pinout if not done
75 if ((obj
->i2c
== I2C_2
) && !i2c2_inited
) {
79 pinmap_pinout(sda
, PinMap_I2C_SDA
);
80 pinmap_pinout(scl
, PinMap_I2C_SCL
);
81 pin_mode(sda
, OpenDrain
);
82 pin_mode(scl
, OpenDrain
);
85 #if defined(I2C3_BASE)
86 // Enable I2C3 clock and pinout if not done
87 if ((obj
->i2c
== I2C_3
) && !i2c3_inited
) {
91 pinmap_pinout(sda
, PinMap_I2C_SDA
);
92 pinmap_pinout(scl
, PinMap_I2C_SCL
);
93 pin_mode(sda
, OpenDrain
);
94 pin_mode(scl
, OpenDrain
);
98 // Reset to clear pending flags if any
102 i2c_frequency(obj
, 100000); // 100 kHz per default
105 void i2c_frequency(i2c_t
*obj
, int hz
)
107 MBED_ASSERT((hz
== 100000) || (hz
== 400000) || (hz
== 1000000));
108 I2cHandle
.Instance
= (I2C_TypeDef
*)(obj
->i2c
);
112 timeout
= LONG_TIMEOUT
;
113 while ((__HAL_I2C_GET_FLAG(&I2cHandle
, I2C_FLAG_BUSY
)) && (timeout
-- != 0));
115 // Common settings: I2C clock = 32 MHz, Analog filter = ON, Digital filter coefficient = 0
118 I2cHandle
.Init
.Timing
= 0x20602938; // Standard mode with Rise Time = 400ns and Fall Time = 100ns
121 I2cHandle
.Init
.Timing
= 0x00B0122A; // Fast mode with Rise Time = 250ns and Fall Time = 100ns
124 I2cHandle
.Init
.Timing
= 0x0030040E; // Fast mode Plus with Rise Time = 60ns and Fall Time = 100ns
131 I2cHandle
.Init
.AddressingMode
= I2C_ADDRESSINGMODE_7BIT
;
132 I2cHandle
.Init
.DualAddressMode
= I2C_DUALADDRESS_DISABLED
;
133 I2cHandle
.Init
.GeneralCallMode
= I2C_GENERALCALL_DISABLED
;
134 I2cHandle
.Init
.NoStretchMode
= I2C_NOSTRETCH_DISABLED
;
135 I2cHandle
.Init
.OwnAddress1
= 0;
136 I2cHandle
.Init
.OwnAddress2
= 0;
137 I2cHandle
.Init
.OwnAddress2Masks
= I2C_OA2_NOMASK
;
138 HAL_I2C_Init(&I2cHandle
);
141 inline int i2c_start(i2c_t
*obj
)
143 I2C_TypeDef
*i2c
= (I2C_TypeDef
*)(obj
->i2c
);
146 I2cHandle
.Instance
= (I2C_TypeDef
*)(obj
->i2c
);
148 // Clear Acknowledge failure flag
149 __HAL_I2C_CLEAR_FLAG(&I2cHandle
, I2C_FLAG_AF
);
151 // Generate the START condition
152 i2c
->CR2
|= I2C_CR2_START
;
154 // Wait the START condition has been correctly sent
155 timeout
= FLAG_TIMEOUT
;
156 while (__HAL_I2C_GET_FLAG(&I2cHandle
, I2C_FLAG_BUSY
) == RESET
) {
157 if ((timeout
--) == 0) {
165 inline int i2c_stop(i2c_t
*obj
)
167 I2C_TypeDef
*i2c
= (I2C_TypeDef
*)(obj
->i2c
);
169 // Generate the STOP condition
170 i2c
->CR2
|= I2C_CR2_STOP
;
175 int i2c_read(i2c_t
*obj
, int address
, char *data
, int length
, int stop
)
177 I2C_TypeDef
*i2c
= (I2C_TypeDef
*)(obj
->i2c
);
178 I2cHandle
.Instance
= (I2C_TypeDef
*)(obj
->i2c
);
183 /* update CR2 register */
184 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
)))
185 | (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
);
188 for (count
= 0; count
< length
; count
++) {
189 value
= i2c_byte_read(obj
, 0);
190 data
[count
] = (char)value
;
193 // Wait transfer complete
194 timeout
= FLAG_TIMEOUT
;
195 while (__HAL_I2C_GET_FLAG(&I2cHandle
, I2C_FLAG_TC
) == RESET
) {
202 __HAL_I2C_CLEAR_FLAG(&I2cHandle
, I2C_FLAG_TC
);
204 // If not repeated start, send stop.
207 /* Wait until STOPF flag is set */
208 timeout
= FLAG_TIMEOUT
;
209 while (__HAL_I2C_GET_FLAG(&I2cHandle
, I2C_FLAG_STOPF
) == RESET
) {
215 /* Clear STOP Flag */
216 __HAL_I2C_CLEAR_FLAG(&I2cHandle
, I2C_FLAG_STOPF
);
222 int i2c_write(i2c_t
*obj
, int address
, const char *data
, int length
, int stop
)
224 I2C_TypeDef
*i2c
= (I2C_TypeDef
*)(obj
->i2c
);
225 I2cHandle
.Instance
= (I2C_TypeDef
*)(obj
->i2c
);
229 /* update CR2 register */
230 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
)))
231 | (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
);
233 for (count
= 0; count
< length
; count
++) {
234 i2c_byte_write(obj
, data
[count
]);
237 // Wait transfer complete
238 timeout
= FLAG_TIMEOUT
;
239 while (__HAL_I2C_GET_FLAG(&I2cHandle
, I2C_FLAG_TC
) == RESET
) {
246 __HAL_I2C_CLEAR_FLAG(&I2cHandle
, I2C_FLAG_TC
);
248 // If not repeated start, send stop.
251 /* Wait until STOPF flag is set */
252 timeout
= FLAG_TIMEOUT
;
253 while (__HAL_I2C_GET_FLAG(&I2cHandle
, I2C_FLAG_STOPF
) == RESET
) {
259 /* Clear STOP Flag */
260 __HAL_I2C_CLEAR_FLAG(&I2cHandle
, I2C_FLAG_STOPF
);
266 int i2c_byte_read(i2c_t
*obj
, int last
)
268 I2C_TypeDef
*i2c
= (I2C_TypeDef
*)(obj
->i2c
);
271 // Wait until the byte is received
272 timeout
= FLAG_TIMEOUT
;
273 while (__HAL_I2C_GET_FLAG(&I2cHandle
, I2C_FLAG_RXNE
) == RESET
) {
274 if ((timeout
--) == 0) {
279 return (int)i2c
->RXDR
;
282 int i2c_byte_write(i2c_t
*obj
, int data
)
284 I2C_TypeDef
*i2c
= (I2C_TypeDef
*)(obj
->i2c
);
287 // Wait until the previous byte is transmitted
288 timeout
= FLAG_TIMEOUT
;
289 while (__HAL_I2C_GET_FLAG(&I2cHandle
, I2C_FLAG_TXIS
) == RESET
) {
290 if ((timeout
--) == 0) {
295 i2c
->TXDR
= (uint8_t)data
;
300 void i2c_reset(i2c_t
*obj
)
305 timeout
= LONG_TIMEOUT
;
306 while ((__HAL_I2C_GET_FLAG(&I2cHandle
, I2C_FLAG_BUSY
)) && (timeout
-- != 0));
308 if (obj
->i2c
== I2C_1
) {
309 __I2C1_FORCE_RESET();
310 __I2C1_RELEASE_RESET();
312 if (obj
->i2c
== I2C_2
) {
313 __I2C2_FORCE_RESET();
314 __I2C2_RELEASE_RESET();
320 void i2c_slave_address(i2c_t
*obj
, int idx
, uint32_t address
, uint32_t mask
)
322 I2C_TypeDef
*i2c
= (I2C_TypeDef
*)(obj
->i2c
);
326 i2c
->OAR1
&= (uint32_t)(~I2C_OAR1_OA1EN
);
327 // Get the old register value
329 // Reset address bits
332 tmpreg
|= (uint16_t)((uint16_t)address
& (uint16_t)0x00FE); // 7-bits
333 // Store the new register value
336 i2c
->OAR1
|= I2C_OAR1_OA1EN
;
339 void i2c_slave_mode(i2c_t
*obj
, int enable_slave
)
342 I2C_TypeDef
*i2c
= (I2C_TypeDef
*)(obj
->i2c
);
345 // Get the old register value
348 // Enable / disable slave
349 if (enable_slave
== 1) {
350 tmpreg
|= I2C_OAR1_OA1EN
;
352 tmpreg
&= (uint32_t)(~I2C_OAR1_OA1EN
);
361 #define NoData 0 // the slave has not been addressed
362 #define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter)
363 #define WriteGeneral 2 // the master is writing to all slave
364 #define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
366 int i2c_slave_receive(i2c_t
*obj
)
368 I2cHandle
.Instance
= (I2C_TypeDef
*)(obj
->i2c
);
369 int retValue
= NoData
;
371 if (__HAL_I2C_GET_FLAG(&I2cHandle
, I2C_FLAG_BUSY
) == 1) {
372 if (__HAL_I2C_GET_FLAG(&I2cHandle
, I2C_FLAG_ADDR
) == 1) {
373 if (__HAL_I2C_GET_FLAG(&I2cHandle
, I2C_FLAG_DIR
) == 1)
374 retValue
= ReadAddressed
;
376 retValue
= WriteAddressed
;
377 __HAL_I2C_CLEAR_FLAG(&I2cHandle
, I2C_FLAG_ADDR
);
384 int i2c_slave_read(i2c_t
*obj
, char *data
, int length
)
388 while (size
< length
) data
[size
++] = (char)i2c_byte_read(obj
, 0);
393 int i2c_slave_write(i2c_t
*obj
, const char *data
, int length
)
396 I2cHandle
.Instance
= (I2C_TypeDef
*)(obj
->i2c
);
399 i2c_byte_write(obj
, data
[size
]);
401 } while (size
< length
);
407 #endif // DEVICE_I2CSLAVE