Skip to content

I2C byte read fix for Freescale platforms #288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ static const uint16_t ICR[0x40] = {
2304, 2560, 3072, 3840
};

static uint8_t first_read;


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

pinmap_pinout(sda, PinMap_I2C_SDA);
pinmap_pinout(scl, PinMap_I2C_SCL);

first_read = 1;
}

int i2c_start(i2c_t *obj) {
Expand All @@ -83,7 +79,6 @@ int i2c_start(i2c_t *obj) {
obj->i2c->C1 |= I2C_C1_MST_MASK;
obj->i2c->C1 |= I2C_C1_TX_MASK;
}
first_read = 1;
return 0;
}

Expand All @@ -98,7 +93,6 @@ int i2c_stop(i2c_t *obj) {
// code provided with the freedom board
for (n = 0; n < 100; n++)
__NOP();
first_read = 1;
return 0;
}

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

int i2c_byte_read(i2c_t *obj, int last) {
char data;

// set rx mode
obj->i2c->C1 &= ~I2C_C1_TX_MASK;

if(first_read) {
// first dummy read
i2c_do_read(obj, &data, 0);
first_read = 0;
}

if (last) {
// set tx mode
obj->i2c->C1 |= I2C_C1_TX_MASK;
return obj->i2c->D;
}


// Setup read
i2c_do_read(obj, &data, last);

return data;
// set tx mode
obj->i2c->C1 |= I2C_C1_TX_MASK;
return obj->i2c->D;
}

int i2c_byte_write(i2c_t *obj, int data) {
first_read = 1;

// set tx mode
obj->i2c->C1 |= I2C_C1_TX_MASK;

Expand Down
27 changes: 5 additions & 22 deletions libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KLXX/i2c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ static const uint16_t ICR[0x40] = {
2304, 2560, 3072, 3840
};

static uint8_t first_read;


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

pinmap_pinout(sda, PinMap_I2C_SDA);
pinmap_pinout(scl, PinMap_I2C_SCL);

first_read = 1;
}

int i2c_start(i2c_t *obj) {
Expand All @@ -84,7 +80,6 @@ int i2c_start(i2c_t *obj) {
obj->i2c->C1 |= I2C_C1_MST_MASK;
obj->i2c->C1 |= I2C_C1_TX_MASK;
}
first_read = 1;
return 0;
}

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

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

if(first_read) {
// first dummy read
i2c_do_read(obj, &data, 0);
first_read = 0;
}

if (last) {
// set tx mode
obj->i2c->C1 |= I2C_C1_TX_MASK;
return obj->i2c->D;
}

// Setup read
i2c_do_read(obj, &data, last);

return data;

// set tx mode
obj->i2c->C1 |= I2C_C1_TX_MASK;
return obj->i2c->D;
}

int i2c_byte_write(i2c_t *obj, int data) {
first_read = 1;

// set tx mode
obj->i2c->C1 |= I2C_C1_TX_MASK;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ static const PinMap PinMap_I2C_SCL[] = {
{NC , NC , 0}
};

static uint8_t first_read;

void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
uint32_t i2c_sda = pinmap_peripheral(sda, PinMap_I2C_SDA);
uint32_t i2c_scl = pinmap_peripheral(scl, PinMap_I2C_SCL);
Expand All @@ -60,12 +58,10 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {

pinmap_pinout(sda, PinMap_I2C_SDA);
pinmap_pinout(scl, PinMap_I2C_SCL);
first_read = 1;
}

int i2c_start(i2c_t *obj) {
i2c_hal_send_start(obj->instance);
first_read = 1;
return 0;
}

Expand All @@ -78,7 +74,6 @@ int i2c_stop(i2c_t *obj) {
// This wait is also included on the samples
// code provided with the freedom board
for (n = 0; n < 100; n++) __NOP();
first_read = 1;
return 0;
}

Expand Down Expand Up @@ -229,26 +224,15 @@ int i2c_byte_read(i2c_t *obj, int last) {
// set rx mode
i2c_hal_set_direction(obj->instance, kI2CReceive);

if(first_read) {
// first dummy read
i2c_do_read(obj, &data, 0);
first_read = 0;
}

if (last) {
// set tx mode
i2c_hal_set_direction(obj->instance, kI2CTransmit);
return i2c_hal_read(obj->instance);
}

// Setup read
i2c_do_read(obj, &data, last);

return data;
// set tx mode
i2c_hal_set_direction(obj->instance, kI2CTransmit);
return i2c_hal_read(obj->instance);
}

int i2c_byte_write(i2c_t *obj, int data) {
first_read = 1;

// set tx mode
i2c_hal_set_direction(obj->instance, kI2CTransmit);

Expand Down