Skip to content

Commit 2ec18a0

Browse files
committed
Fixed Freescale byte reads (minus k64)
See: https://mbed.org/questions/3181/Inconsistent-results-when-attempting-wri/ for the issue. Now every byte read is treated equal and ACKs/NACKs are correctly generated
1 parent 62dcd84 commit 2ec18a0

File tree

2 files changed

+11
-45
lines changed

2 files changed

+11
-45
lines changed

libraries/mbed/targets/hal/TARGET_Freescale/TARGET_K20D5M/i2c_api.c

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ static const uint16_t ICR[0x40] = {
4848
2304, 2560, 3072, 3840
4949
};
5050

51-
static uint8_t first_read;
52-
5351

5452
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
5553
// determine the I2C to use
@@ -70,8 +68,6 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
7068

7169
pinmap_pinout(sda, PinMap_I2C_SDA);
7270
pinmap_pinout(scl, PinMap_I2C_SCL);
73-
74-
first_read = 1;
7571
}
7672

7773
int i2c_start(i2c_t *obj) {
@@ -83,7 +79,6 @@ int i2c_start(i2c_t *obj) {
8379
obj->i2c->C1 |= I2C_C1_MST_MASK;
8480
obj->i2c->C1 |= I2C_C1_TX_MASK;
8581
}
86-
first_read = 1;
8782
return 0;
8883
}
8984

@@ -98,7 +93,6 @@ int i2c_stop(i2c_t *obj) {
9893
// code provided with the freedom board
9994
for (n = 0; n < 100; n++)
10095
__NOP();
101-
first_read = 1;
10296
return 0;
10397
}
10498

@@ -279,30 +273,19 @@ void i2c_reset(i2c_t *obj) {
279273

280274
int i2c_byte_read(i2c_t *obj, int last) {
281275
char data;
282-
276+
283277
// set rx mode
284278
obj->i2c->C1 &= ~I2C_C1_TX_MASK;
285-
286-
if(first_read) {
287-
// first dummy read
288-
i2c_do_read(obj, &data, 0);
289-
first_read = 0;
290-
}
291-
292-
if (last) {
293-
// set tx mode
294-
obj->i2c->C1 |= I2C_C1_TX_MASK;
295-
return obj->i2c->D;
296-
}
297-
279+
280+
// Setup read
298281
i2c_do_read(obj, &data, last);
299282

300-
return data;
283+
// set tx mode
284+
obj->i2c->C1 |= I2C_C1_TX_MASK;
285+
return obj->i2c->D;
301286
}
302287

303288
int i2c_byte_write(i2c_t *obj, int data) {
304-
first_read = 1;
305-
306289
// set tx mode
307290
obj->i2c->C1 |= I2C_C1_TX_MASK;
308291

libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KLXX/i2c_api.c

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ static const uint16_t ICR[0x40] = {
3737
2304, 2560, 3072, 3840
3838
};
3939

40-
static uint8_t first_read;
41-
4240

4341
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
4442
// determine the I2C to use
@@ -63,8 +61,6 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
6361

6462
pinmap_pinout(sda, PinMap_I2C_SDA);
6563
pinmap_pinout(scl, PinMap_I2C_SCL);
66-
67-
first_read = 1;
6864
}
6965

7066
int i2c_start(i2c_t *obj) {
@@ -84,7 +80,6 @@ int i2c_start(i2c_t *obj) {
8480
obj->i2c->C1 |= I2C_C1_MST_MASK;
8581
obj->i2c->C1 |= I2C_C1_TX_MASK;
8682
}
87-
first_read = 1;
8883
return 0;
8984
}
9085

@@ -98,7 +93,6 @@ int i2c_stop(i2c_t *obj) {
9893
// This wait is also included on the samples
9994
// code provided with the freedom board
10095
for (n = 0; n < 100; n++) __NOP();
101-
first_read = 1;
10296
return 0;
10397
}
10498

@@ -284,26 +278,15 @@ int i2c_byte_read(i2c_t *obj, int last) {
284278
// set rx mode
285279
obj->i2c->C1 &= ~I2C_C1_TX_MASK;
286280

287-
if(first_read) {
288-
// first dummy read
289-
i2c_do_read(obj, &data, 0);
290-
first_read = 0;
291-
}
292-
293-
if (last) {
294-
// set tx mode
295-
obj->i2c->C1 |= I2C_C1_TX_MASK;
296-
return obj->i2c->D;
297-
}
298-
281+
// Setup read
299282
i2c_do_read(obj, &data, last);
300-
301-
return data;
283+
284+
// set tx mode
285+
obj->i2c->C1 |= I2C_C1_TX_MASK;
286+
return obj->i2c->D;
302287
}
303288

304289
int i2c_byte_write(i2c_t *obj, int data) {
305-
first_read = 1;
306-
307290
// set tx mode
308291
obj->i2c->C1 |= I2C_C1_TX_MASK;
309292

0 commit comments

Comments
 (0)