Skip to content

Commit 28961db

Browse files
author
Bogdan Marinescu
committed
Added timeout to i2c_stop operations
Fixes this issue: http://mbed.org/users/mbed_official/code/mbed/issues/3
1 parent 4a98d0e commit 28961db

File tree

8 files changed

+50
-13
lines changed

8 files changed

+50
-13
lines changed

libraries/mbed/hal/i2c_api.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ enum {
3434
void i2c_init (i2c_t *obj, PinName sda, PinName scl);
3535
void i2c_frequency (i2c_t *obj, int hz);
3636
int i2c_start (i2c_t *obj);
37-
void i2c_stop (i2c_t *obj);
37+
int i2c_stop (i2c_t *obj);
3838
int i2c_read (i2c_t *obj, int address, char *data, int length, int stop);
3939
int i2c_write (i2c_t *obj, int address, const char *data, int length, int stop);
4040
void i2c_reset (i2c_t *obj);

libraries/mbed/targets/hal/Freescale/TARGET_KL25Z/i2c_api.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ int i2c_start(i2c_t *obj) {
9696
return 0;
9797
}
9898

99-
void i2c_stop(i2c_t *obj) {
99+
int i2c_stop(i2c_t *obj) {
100100
volatile uint32_t n = 0;
101101
obj->i2c->C1 &= ~I2C_C1_MST_MASK;
102102
obj->i2c->C1 &= ~I2C_C1_TX_MASK;
@@ -107,6 +107,7 @@ void i2c_stop(i2c_t *obj) {
107107
// code provided with the freedom board
108108
for (n = 0; n < 100; n++) __NOP();
109109
first_read = 1;
110+
return 0;
110111
}
111112

112113
static int timeout_status_poll(i2c_t *obj, uint32_t mask) {

libraries/mbed/targets/hal/NXP/TARGET_LPC11UXX/i2c_api.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,20 @@ inline int i2c_start(i2c_t *obj) {
124124
return status;
125125
}
126126

127-
inline void i2c_stop(i2c_t *obj) {
127+
inline int i2c_stop(i2c_t *obj) {
128+
int timeout = 0;
129+
128130
// write the stop bit
129131
i2c_conset(obj, 0, 1, 0, 0);
130132
i2c_clear_SI(obj);
131133

132134
// wait for STO bit to reset
133-
while(I2C_CONSET(obj) & (1 << 4));
135+
while(I2C_CONSET(obj) & (1 << 4)) {
136+
timeout ++;
137+
if (timeout > 100000) return 1;
138+
}
139+
140+
return 0;
134141
}
135142

136143

libraries/mbed/targets/hal/NXP/TARGET_LPC176X/i2c_api.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,20 @@ inline int i2c_start(i2c_t *obj) {
133133
return status;
134134
}
135135

136-
inline void i2c_stop(i2c_t *obj) {
136+
inline int i2c_stop(i2c_t *obj) {
137+
int timeout = 0;
138+
137139
// write the stop bit
138140
i2c_conset(obj, 0, 1, 0, 0);
139141
i2c_clear_SI(obj);
140142

141143
// wait for STO bit to reset
142-
while(I2C_CONSET(obj) & (1 << 4));
144+
while(I2C_CONSET(obj) & (1 << 4)) {
145+
timeout ++;
146+
if (timeout > 100000) return 1;
147+
}
148+
149+
return 0;
143150
}
144151

145152
static inline int i2c_do_write(i2c_t *obj, int value, uint8_t addr) {

libraries/mbed/targets/hal/NXP/TARGET_LPC23XX/i2c_api.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,20 @@ inline int i2c_start(i2c_t *obj) {
133133
return status;
134134
}
135135

136-
inline void i2c_stop(i2c_t *obj) {
136+
inline int i2c_stop(i2c_t *obj) {
137+
int timeout = 0;
138+
137139
// write the stop bit
138140
i2c_conset(obj, 0, 1, 0, 0);
139141
i2c_clear_SI(obj);
140142

141143
// wait for STO bit to reset
142-
while (I2C_CONSET(obj) & (1 << 4));
144+
while (I2C_CONSET(obj) & (1 << 4)) {
145+
timeout ++;
146+
if (timeout > 100000) return 1;
147+
}
148+
149+
return 0;
143150
}
144151

145152
static inline int i2c_do_write(i2c_t *obj, int value, uint8_t addr) {

libraries/mbed/targets/hal/NXP/TARGET_LPC408X/i2c_api.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,20 @@ inline int i2c_start(i2c_t *obj) {
144144
return status;
145145
}
146146

147-
inline void i2c_stop(i2c_t *obj) {
147+
inline int i2c_stop(i2c_t *obj) {
148+
int timeout = 0;
149+
148150
// write the stop bit
149151
i2c_conset(obj, 0, 1, 0, 0);
150152
i2c_clear_SI(obj);
151153

152154
// wait for STO bit to reset
153-
while(I2C_CONSET(obj) & (1 << 4));
155+
while(I2C_CONSET(obj) & (1 << 4)) {
156+
timeout ++;
157+
if (timeout > 100000) return 1;
158+
}
159+
160+
return 0;
154161
}
155162

156163

libraries/mbed/targets/hal/NXP/TARGET_LPC81X/i2c_api.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,16 @@ inline int i2c_start(i2c_t *obj) {
8787
return status;
8888
}
8989

90-
inline void i2c_stop(i2c_t *obj) {
90+
inline int i2c_stop(i2c_t *obj) {
91+
int timeout = 0;
92+
9193
obj->i2c->MSTCTL = (1 << 2) | (1 << 0);
92-
while ((obj->i2c->STAT & ((1 << 0) | (7 << 1))) != ((1 << 0) | (0 << 1)));
94+
while ((obj->i2c->STAT & ((1 << 0) | (7 << 1))) != ((1 << 0) | (0 << 1))) {
95+
timeout ++;
96+
if (timeout > 100000) return 1;
97+
}
98+
99+
return 0;
93100
}
94101

95102

libraries/mbed/targets/hal/STM/TARGET_STM32F4XX/i2c_api.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,10 @@ inline int i2c_start(i2c_t *obj) {
163163
return 0;
164164
}
165165

166-
inline void i2c_stop(i2c_t *obj) {
166+
inline int i2c_stop(i2c_t *obj) {
167167
// Generate the stop condition
168168
obj->i2c->CR1 |= I2C_CR1_STOP;
169+
return 0;
169170
}
170171

171172

0 commit comments

Comments
 (0)