Skip to content
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

KSDK2 fixes #1817

Merged
merged 3 commits into from
May 31, 2016
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 @@ -108,7 +108,7 @@ int spi_master_write(spi_t *obj, int value) {
// wait rx buffer full
while (!spi_readable(obj));
rx_data = DSPI_ReadData(spi_address[obj->instance]);
DSPI_ClearStatusFlags(spi_address[obj->instance], kDSPI_RxFifoDrainRequestFlag);
DSPI_ClearStatusFlags(spi_address[obj->instance], kDSPI_RxFifoDrainRequestFlag | kDSPI_EndOfQueueFlag);
return rx_data & 0xffff;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ int spi_master_write(spi_t *obj, int value) {
// wait rx buffer full
while (!spi_readable(obj));
rx_data = DSPI_ReadData(spi_address[obj->instance]);
DSPI_ClearStatusFlags(spi_address[obj->instance], kDSPI_RxFifoDrainRequestFlag);
DSPI_ClearStatusFlags(spi_address[obj->instance], kDSPI_RxFifoDrainRequestFlag | kDSPI_EndOfQueueFlag);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fix should be applied to K22F as well, they share the same peripheral

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I'll update it

return rx_data & 0xffff;
}

Expand Down
25 changes: 20 additions & 5 deletions hal/targets/hal/TARGET_Freescale/TARGET_KSDK2_MCUS/api/i2c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "peripheral_clock_defines.h"
#include "PeripheralPins.h"

/* 7 bit IIC addr - R/W flag not included */
static int i2c_address = 0;
/* Array of I2C peripheral base address. */
static I2C_Type *const i2c_addrs[] = I2C_BASE_PTRS;
Expand All @@ -35,6 +36,7 @@ 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);
obj->instance = pinmap_merge(i2c_sda, i2c_scl);
obj->next_repeated_start = 0;
MBED_ASSERT((int)obj->instance != NC);

i2c_master_config_t master_config;
Expand Down Expand Up @@ -77,6 +79,7 @@ int i2c_start(i2c_t *obj) {

int i2c_stop(i2c_t *obj) {
if (I2C_MasterStop(i2c_addrs[obj->instance]) != kStatus_Success) {
obj->next_repeated_start = 0;
return 1;
}

Expand All @@ -94,12 +97,19 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
I2C_Type *base = i2c_addrs[obj->instance];
i2c_master_transfer_t master_xfer;

i2c_address = address;
i2c_address = address >> 1;
memset(&master_xfer, 0, sizeof(master_xfer));
master_xfer.slaveAddress = address;
master_xfer.slaveAddress = address >> 1;
master_xfer.direction = kI2C_Read;
master_xfer.data = (uint8_t *)data;
master_xfer.dataSize = length;
if (obj->next_repeated_start) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this repeated start flag needed ? Because the default master_xfer.flags (=0) is start + stop. The stop argument changes stop behavior. Therefore if stop is true, the i2c_read() here would do start, no stop. What am I missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Posting a summary of our discussion so others can see:
KSDK 2 treats a start as an error if the bus is in use rather than sending a repeated start. This can be seen here:
https://github.com/mbedmicro/mbed/blob/34ea175b95bdf3a39c53deb5f80dc8df6d4f7692/hal/targets/hal/TARGET_Freescale/TARGET_KSDK2_MCUS/TARGET_K64F/drivers/fsl_i2c.c#L587

master_xfer.flags |= kI2C_TransferRepeatedStartFlag;
}
if (!stop) {
master_xfer.flags |= kI2C_TransferNoStopFlag;
}
obj->next_repeated_start = master_xfer.flags & kI2C_TransferNoStopFlag ? 1 : 0;

/* The below function will issue a STOP signal at the end of the transfer.
* This is required by the hardware in order to receive the last byte
Expand All @@ -116,12 +126,17 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
i2c_master_transfer_t master_xfer;

memset(&master_xfer, 0, sizeof(master_xfer));
master_xfer.slaveAddress = address;
master_xfer.slaveAddress = address >> 1;
master_xfer.direction = kI2C_Write;
master_xfer.data = (uint8_t *)data;
master_xfer.dataSize = length;
if (!stop)
master_xfer.flags = kI2C_TransferNoStopFlag;
if (obj->next_repeated_start) {
master_xfer.flags |= kI2C_TransferRepeatedStartFlag;
}
if (!stop) {
master_xfer.flags |= kI2C_TransferNoStopFlag;
}
obj->next_repeated_start = master_xfer.flags & kI2C_TransferNoStopFlag ? 1 : 0;

if (I2C_MasterTransferBlocking(base, &master_xfer) != kStatus_Success) {
return I2C_ERROR_NO_SLAVE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct analogin_s {

struct i2c_s {
uint32_t instance;
uint8_t next_repeated_start;
};

struct spi_s {
Expand Down