Skip to content

Commit daefeed

Browse files
committed
[NUCLEO_L152RE] Change the check of I2C clock frequency + update stop function + cleanup
1 parent a8236d8 commit daefeed

File tree

1 file changed

+37
-43
lines changed
  • libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE

1 file changed

+37
-43
lines changed

libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/i2c_api.c

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,24 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
9090
void i2c_frequency(i2c_t *obj, int hz) {
9191
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
9292
I2C_InitTypeDef I2C_InitStructure;
93+
94+
if (hz == 0) return;
95+
if (hz > 400000) hz = 400000;
96+
97+
/* Warning: To use the I2C at 400 kHz (in fast mode), the PCLK1 frequency
98+
(I2C peripheral input clock) must be a multiple of 10 MHz.
99+
With the actual clock configuration, the max frequency is measured at 296 kHz */
93100

94-
if ((hz != 0) && (hz <= 400000)) {
95-
I2C_DeInit(i2c);
96-
97-
// I2C configuration
98-
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
99-
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
100-
I2C_InitStructure.I2C_OwnAddress1 = 0;
101-
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
102-
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
103-
I2C_InitStructure.I2C_ClockSpeed = hz;
104-
I2C_Init(i2c, &I2C_InitStructure);
105-
106-
I2C_Cmd(i2c, ENABLE);
107-
}
101+
// I2C configuration
102+
I2C_DeInit(i2c);
103+
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
104+
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
105+
I2C_InitStructure.I2C_OwnAddress1 = 0;
106+
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
107+
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
108+
I2C_InitStructure.I2C_ClockSpeed = hz;
109+
I2C_Init(i2c, &I2C_InitStructure);
110+
I2C_Cmd(i2c, ENABLE);
108111
}
109112

110113
inline int i2c_start(i2c_t *obj) {
@@ -118,7 +121,6 @@ inline int i2c_start(i2c_t *obj) {
118121

119122
// Wait the START condition has been correctly sent
120123
timeout = FLAG_TIMEOUT;
121-
//while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_MODE_SELECT) == ERROR) {
122124
while (I2C_GetFlagStatus(i2c, I2C_FLAG_SB) == RESET) {
123125
timeout--;
124126
if (timeout == 0) {
@@ -131,9 +133,25 @@ inline int i2c_start(i2c_t *obj) {
131133

132134
inline int i2c_stop(i2c_t *obj) {
133135
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
134-
135-
I2C_GenerateSTOP(i2c, ENABLE);
136-
136+
int timeout;
137+
volatile int temp;
138+
139+
if (I2C_GetFlagStatus(i2c, I2C_FLAG_MSL) == RESET) {
140+
timeout = LONG_TIMEOUT;
141+
// wait for STOP
142+
while (I2C_GetFlagStatus(i2c, I2C_FLAG_STOPF) == RESET) {
143+
timeout--;
144+
if (timeout == 0) {
145+
return 0;
146+
}
147+
}
148+
temp = i2c->SR1;
149+
I2C_Cmd(i2c, ENABLE);
150+
}
151+
else {
152+
I2C_GenerateSTOP(i2c, ENABLE);
153+
}
154+
137155
return 0;
138156
}
139157

@@ -145,17 +163,6 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
145163

146164
if (length == 0) return 0;
147165

148-
/*
149-
// Wait until the bus is not busy anymore
150-
timeout = LONG_TIMEOUT;
151-
while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) {
152-
timeout--;
153-
if (timeout == 0) {
154-
return 0;
155-
}
156-
}
157-
*/
158-
159166
i2c_start(obj);
160167

161168
// Send slave address for read
@@ -193,17 +200,6 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
193200
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
194201
int timeout;
195202
int count;
196-
197-
/*
198-
// Wait until the bus is not busy anymore
199-
timeout = LONG_TIMEOUT;
200-
while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) {
201-
timeout--;
202-
if (timeout == 0) {
203-
return 0;
204-
}
205-
}
206-
*/
207203

208204
i2c_start(obj);
209205

@@ -268,8 +264,7 @@ int i2c_byte_write(i2c_t *obj, int data) {
268264
I2C_SendData(i2c, (uint8_t)data);
269265

270266
// Wait until the byte is transmitted
271-
timeout = FLAG_TIMEOUT;
272-
//while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_BYTE_TRANSMITTED) == ERROR) {
267+
timeout = FLAG_TIMEOUT;
273268
while ((I2C_GetFlagStatus(i2c, I2C_FLAG_TXE) == RESET) &&
274269
(I2C_GetFlagStatus(i2c, I2C_FLAG_BTF) == RESET)) {
275270
timeout--;
@@ -319,7 +314,6 @@ void i2c_slave_mode(i2c_t *obj, int enable_slave) {
319314
#define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
320315

321316
int i2c_slave_receive(i2c_t *obj) {
322-
// TO BE DONE
323317
return(0);
324318
}
325319

0 commit comments

Comments
 (0)