Hi,
I am using LPC55S06 to communicate with another microcontroller by using SPI. My microcontroller is the master, the other one is the slave. The configuration is CPOL=0 and CPHA=0, and the datarate is 500kbps. The protocol is 2 byte of register address (with 1 bit that indicate read/write) and 4 byte of data.
When I write a register no problem on my way: the data sniffed with a logic analyzer is perfect, and the slave read data well. When I try to read a register, the slave send out the data, on the logic analyzer the data is perfect, but on microcontroller the last bit will be lost.
In example, if the data that I see on logic analyzer is 0x00000064, on micro I read 0x00000032, and so on. The code that read is the following:
void writeReadRegister (DeviceHandle_t handle,
bool readWrite,
Register_t addr,
uint32_t* message)
{
uint8_t txData[6];
uint8_t rxData[6];
uint32_t tmpMessage = *message;
//readWrite = 1 write
if (readWrite == 1)
{
memset(txData, 0, sizeof(txData));
txData[0] = 0x01;
txData[1] = (uint8_t)addr;
txData[2] = (uint8_t)((tmpMessage >> 24) & 0x000000FF);
txData[3] = (uint8_t)((tmpMessage >> 16) & 0x000000FF);
txData[4] = (uint8_t)((tmpMessage >> & 0x000000FF);
txData[5] = (uint8_t)(tmpMessage & 0x000000FF);
}
//readWrite = 0 read
else if (readWrite == 0)
{
memset(txData, 0, sizeof(txData));
txData[0] = 0x00;
txData[1] = (uint8_t)addr;
memset(rxData, 0, sizeof(rxData));
rxData[0] = 0x00;
rxData[1] = (uint8_t)addr;
}
handle->xferDefault.txData = txData;
handle->xferDefault.rxData = rxData;
GPIO_PinWrite(handle->gpioPeripheral, handle->port, handle->pin, 0);
App_mDelay(1);
SPI_MasterTransferBlocking(handle->peripheralNum, &handle->xferDefault);
App_mDelay(1);
GPIO_PinWrite(handle->gpioPeripheral, handle->port, handle->pin, 1);
if (readWrite == 0)
{
uint32_t temp = 0;
temp = (((uint32_t)rxData[5]) & 0x000000FF) |
(((uint32_t)rxData[4] << & 0x0000FF00) |
(((uint32_t)rxData[3] << 16) & 0x00FF0000) |
(((uint32_t)rxData[2] << 24) & 0xFF000000);
*message = temp;
}
}
Where is the error? I can't see!
Thanks
Marco
Hi, I found the problem: the slave device, an FPGA programmed by ourself, generate a wrong MISO message... so the microcontroller can't read well the data.
Thanks all.
Try CPHA=1