I have a FRDM-KL-27Z eval board where we changed the CPU to a KL17. We hooked up an ST h3lis331dl accelerometer that has a SPI interface and wired it to the KL27 board. On the KL27 board, we have the SPI from the ST h3lis331dl hooked up as follows.
PTD0 is the chip select. PTD0-1 On J1 connector
PTD1 is the SPI0 clock PTD1-3 On J1 connector
PTD2 is the SPI0 MOSI PTD2-5 On J1 connector
PTD3 is the SPI0 MISO PTD3-7 On J1 connector
I am using KDS 3.2.0 as a dev environment am I am starting from an example SPI_polling_transfer.
On the ST h3lis331dl, there is a register I can read called the WHO_AM_I that will reply a fixed reply of 0x32. Based on some sample code and the documentation for the Kinetis, I can confirm that the SPI is working properly by looking at the waveforms on a scope. The scope shows is the following:
1 The chip select asserted
2 The SPI clock starts and the MOSI has the correct waveform for the address for the WHO_AM_I register. This is an 8 bit burst on the bus.
3 there is a slight pause in the SPI clock
4 The SPI clock starts again and the correct reply is seen on the MISO pin. The scope shows that MISO had the data 0x32.
5 The chip select is de asserted
6 the problem I have is when I get the data from the SPI, I do not get the data value I expect. I expect to see the value 0x32 but I see 0xff.
My Pin mux is:
CLOCK_EnableClock(kCLOCK_PortD);
//PORT_SetPinMux(PORTD, 0U, kPORT_MuxAlt2); // SPI chip select //automatic CS
PORT_SetPinMux(PORTD, 1U, kPORT_MuxAlt2); // SPI0-SCK PTD1
PORT_SetPinMux(PORTD, 2U, kPORT_MuxAlt2); // SPI0-MOSI PTD2
PORT_SetPinMux(PORTD, 3U, kPORT_MuxAlt2); // SPI0-MISO PTD3
My SPI config is:
SPI_MasterGetDefaultConfig(&masterConfig);
sourceClock = CLOCK_GetFreq(EXAMPLE_SPI_MASTER_SOURCE_CLOCK);
SPI_MasterInit(SPI_0_MASTER, &masterConfig, sourceClock);
My code to read the SPI is the following:
#define BUFFER_SIZE 64U
uint8_t spi_data_IN[BUFFER_SIZE]; // <<< this is a global.
spi_transfer_t xfer = {0};
<SNIP>
memset(spi_data_IN, 0x55, sizeof(spi_data_IN)); // init the data to a known value
/* SPI master start transfer */
xfer.txData = (uint8_t *) SPI_data.sendDataSPI;
//xfer.rxData = (uint8_t *) &SPI_data.receivedDataSPI[0];
xfer.rxData = spi_data_IN;
xfer.dataSize = SPI_data.totalDataBytes;
spi_accel_chipselect(TRUE); // select the device to read/write (chip select)
reuturnValue = SPI_MasterTransferBlocking(SPI_MASTER, &xfer); // note the return value is OK
spi_accel_chipselect(FALSE); // select the device to read/write (chip select)
PRINTF("post read Data rx = %x %x %x %x %x\r\n",
spi_data_IN[0],
spi_data_IN[1],
spi_data_IN[2],
spi_data_IN[3],
spi_data_IN[4]);
OUTPUT data from above print statement: post read Data rx = ff ff 55 55 55
My question is why do I read in the buffer a value of 0xff and not 0x32 when the scope shows I should be reading 0x32?
- I can confirm that the MISO pin has a good connection between the PTD3-7 pin on the eval board and the physical pin 60 on the CPU.
- If I use masterConfig.pinMode = kSPI_PinModeOutput;, the waveforms on the scope are still good but I read 0x8f. The 0x8f is what I write on the first byte.