AnsweredAssumed Answered

Problem reading that SPI data in Slave Mode - FRDM-K22F

Question asked by on Jun 21, 2018
Latest reply on Jun 25, 2018 by

Hi All,

I recently started to play with the FRDM-K22F devboard. I used the KSDK v1.3 documentation and managed to print data through the UART, set interruptions through one of the push buttons, and toggle the on board LED, but I cannot get the SPI bus to read data as a Slave. I based the code on this sample from the K64 board ( and also read the KSDK manual. Everything seems to checkout on paper.


Experimental setup:

- KSDK v1.3

- FRDM-K22F devboard

- Arduino UNO (set as Master, sending 0x55 in 1 second intervals. Clock phase/polarity checked with scope)

- Kinetis code (see main.c below) 


I tried, both SPI instances, and they hang at the DSPI_DRV_SlaveTransferBlocking() function. Then switched to the non-blocking variant and printed the Error output which prints "4" or "busy". 

This should work "out of the box", I have no idea where to go from here.


#include "fsl_device_registers.h"
#include "board.h"
#include "pin_mux.h"
#include "fsl_clock_manager.h"
#include "fsl_debug_console.h"

#include "fsl_dspi_slave_driver.h"
#include "fsl_dspi_hal.h"


#include <stdio.h>

//SPI input buffer
uint8_t spiSinkBuffer;

int main(void)

/* enable clock for PORTs */

/* Init board clock */


/*Debug UART init */

printf("\nPress SW2 to Toggle LED. \r\n");


// Configure pin (sw2) interrupt
switchPins[0].config.interrupt = kPortIntFallingEdge;

// Initialize GPIO driver
GPIO_DRV_Init(switchPins, ledPins);


//SPI Config


// Interrupt driven
dspi_slave_state_t dspiSlaveState;


// update configs
dspi_slave_user_config_t slaveUserConfig;
slaveUserConfig.dataConfig.clkPhase = kDspiClockPhase_SecondEdge;
slaveUserConfig.dataConfig.clkPolarity = kDspiClockPolarity_ActiveHigh;
slaveUserConfig.dataConfig.bitsPerFrame = 8;
slaveUserConfig.dataConfig.direction = kDspiMsbFirst;
slaveUserConfig.dummyPattern = DSPI_DEFAULT_DUMMY_PATTERN;

// init the slave (interrupt driven)
DSPI_DRV_SlaveInit(SPI0_IDX, &dspiSlaveState, &slaveUserConfig);


// Main loop

  //Blocking Transfer
  dspi_status_t Error = DSPI_DRV_SlaveTransferBlocking(SPI0_IDX, //spi instance
                                                                                             NULL, //send buffer address
                                                                                             &spiSinkBuffer, //received buffer address
                                                                                             1, //number of bytes to send and receive
                                                                                             OSA_WAIT_FOREVER); //timeout                    OSA_WAIT_FOREVER = blocking

  //Non-blocking Transfer
  dspi_status_t Error = DSPI_DRV_SlaveTransfer(SPI0_IDX, //spi instance
                                                                                NULL, //send buffer address
                                                                                &spiSinkBuffer, //received buffer address
                                                                                1); //number of bytes to send and receive


  if (Error == kStatus_DSPI_Success)
    //Send over UART
    //dspiSlaveState.isTransferInProgress = 0;
    printf("SPI Buffer: 0x%X\n\r",spiSinkBuffer);

  printf("Error Code %d\n\r",Error);



  return 0;


//Define PORTC_IRQHandler (sw2)
void PORTC_IRQHandler()
  static uint8_t count = 0;
  GPIO_DRV_ClearPinIntFlag (kGpioSW2); // Clear IRQ flag
  GPIO_DRV_TogglePinOutput(BOARD_GPIO_LED_RED); // Toggle blue LED