Rong,
Below is the code that I tested based on your recommendations. I do like your implementation because it avoids the need for an IRQ handler, but it has the same problem as the code I submitted at first in void FLEXCOMM9_IRQHandler(void). Spi_WR() works but spi_RD() does not. My oscilloscope shows the correct MOSI and MISO signals for both functions, including the correct read data as the last byte of the MISO signal.
The following illustrates the problem I am seeing:
ReadTest1 = spi_RD(MAX31865_CONFIG_REG); // ReadTest1 = 0xff
spi_WR(MAX31865_CONFIG_REG, 0xaa); // Write 0xaa to the address just read
ReadTest1 = spi_RD(MAX31865_CONFIG_REG); // ReadTest1 = 0xd0 (correct value)
For some reason, spi_RD() returns the correct results only if I insert a spi_WR() after the first call.
It may be helpful to realize that my code works when it is implemented in a masterCallback which looks like the SDK example
static void masterCallback(SPI_Type *base, spi_master_handle_t *handle, status_t status, void *userData)
{
masterFinished = true;
}
My masterCallback implementation has the same phase, polarity, sselNum & sselPol configuration settings as does this code, which I prefer. The only configuration differences are that the masterCallback implementation has dataWidth of 8 bits verses 16 bits. The variable ConfigFlags is 0x100000 in the Callback.
// from LPC54018.h
// #define SPI9_BASE (0x4009A000u)
// #define SPI9 ((SPI_Type *)SPI9_BASE)
#define SPI9_FIFOWR_ADDR 0x4009AE20 // Offset 0xE20
#define SPI9_FIFORD_ADDR 0x4009AE30 // Offset 0xE30
#define SPI9_FIFOSTAT_ADDR 0x4009AE04 // Offset 0xE04
void MAX31865::spi_WR(uint8_t RegAddr, uint8_t writeData)
{
WRData = 0x0F1E0000 | ((RegAddr | 0x80) << | writeData;
//while(!(SPI9_FIFOSTAT_ADDR * 0x10)) {}; // While TX is empty (has no effect on result)
*(uint32_t *)SPI9_FIFOWR_ADDR=WRData;
}
uint8_t MAX31865::spi_RD(uint8_t RegAddr)
{
uint8_t temp_var = 0;
WRData = 0x0F1E0000 | ((RegAddr & 0x7f) << 8);
*(uint32_t *)SPI9_FIFOWR_ADDR = WRData;
//while(!(SPI9_FIFOSTAT_ADDR * 0x10 )) {}; // While TX is empty (has no effect on result)
temp_var= *(uint32_t *)SPI9_FIFORD_ADDR & 0xFF;
return temp_var;
}