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 (https://community.nxp.com/docs/DOC-103944) and also read the KSDK manual. Everything seems to checkout on paper.
#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 */
CLOCK_SYS_EnablePortClock(PORTA_IDX);
CLOCK_SYS_EnablePortClock(PORTB_IDX);
CLOCK_SYS_EnablePortClock(PORTC_IDX);
CLOCK_SYS_EnablePortClock(PORTE_IDX);
/* Init board clock */
BOARD_ClockInit();
/*Debug UART init */
dbg_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
configure_spi_pins(SPI0_IDX);
// 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
while(1)
{
GPIO_SW_DELAY;
//printf("IN\n\r");
//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
printf("0x%X\n\r",count++);
GPIO_SW_DELAY;
}
/*EOF*/