Background
- New to NXP microcontrollers, unfamiliar with KSDK.
- Non-expert in C.
Documentation Referenced:
- Kinetis SDK 2.0 API Reference Manual - DSPI Driver
- K66 Sub-Family Reference Manual with Addendum [PDF]
- KSDK SPI Master-Slave with FRDM-K64F [PDF]
Hardware:
Software:
- MCU Xpresso v10.2.1 [2018-7-25]
- KSDK 2.x_FRDM-K64F, Version 2.4.2, Manifest Version 3.3.0
Problem:
Unable to get SPI peripheral to function with SPI device.
Aforementioned documents have been read. Unable to decipher the SDK API reference manual in part.
- srcClock_Hz = CLOCK_GetFreq(xxx); Does not appear to function with SPI0_CLK_SRC, etc. Unable to determine how to get this to function. Documentation I have read appears to state that the only clock source available to SPI is the bus clock.
- DSPI is the only driver included. The SPI documentation does not compile on the device. Is this intentional? e.g. not sure if I am using the correct driver.
Note: Hardware is functional using mbed program, so hardware has been ruled out.
Goal:
- Perform basic SPI communication operation. Read first 4 bytes of device register (send 0x00 4 times, print newline for each result)
- Write to register, should be simple once basic read is done (sent register address, read output)
Code:
#include <stdio.h>
#include "board.h"
#include "peripherals.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "MK64F12.h"
#include "fsl_debug_console.h"
#include "fsl_dspi.h"
#include "fsl_clock.h"
#define baudrate 115200
int main(void) {
BOARD_InitBootPins();
BOARD_InitBootClocks();
BOARD_InitBootPeripherals();
BOARD_InitDebugConsole();
dspi_master_handle_t g_m_handle;
dspi_master_config_t masterConfig;
dspi_transfer_t masterXfer = {0};
masterConfig.whichCtar = kDSPI_Ctar0;
masterConfig.ctarConfig.baudRate = baudrate;
masterConfig.ctarConfig.bitsPerFrame = 8;
masterConfig.ctarConfig.cpol = kDSPI_ClockPolarityActiveHigh;
masterConfig.ctarConfig.cpha = kDSPI_ClockPhaseFirstEdge;
masterConfig.ctarConfig.direction = kDSPI_MsbFirst;
masterConfig.ctarConfig.pcsToSckDelayInNanoSec = 1000000000 / baudrate;
masterConfig.ctarConfig.lastSckToPcsDelayInNanoSec = 1000000000 / baudrate;
masterConfig.ctarConfig.betweenTransferDelayInNanoSec = 1000000000 / baudrate;
masterConfig.whichPcs = kDSPI_Pcs0;
masterConfig.pcsActiveHighOrLow = kDSPI_PcsActiveLow;
masterConfig.enableContinuousSCK = false;
masterConfig.enableRxFifoOverWrite = false;
masterConfig.enableModifiedTimingFormat = false;
masterConfig.samplePoint = kDSPI_SckToSin0Clock;
int srcClock_Hz = CLOCK_GetFreq(kCLOCK_BusClk);
DSPI_MasterInit(SPI0, &masterConfig, srcClock_Hz);
DSPI_MasterTransferCreateHandle(SPI0, &g_m_handle, NULL, NULL);
masterXfer.txData = 0;
masterXfer.rxData = 0;
masterXfer.dataSize = 0;
masterXfer.configFlags = kDSPI_MasterCtar0 | kDSPI_MasterPcs0;
masterXfer.txData = 0x00;
DSPI_MasterTransferBlocking(SPI0, &masterXfer);
PRINTF("Data Received: %d\n",masterXfer.rxData);
DSPI_MasterTransferBlocking(SPI0, &masterXfer);
PRINTF("Data Received: %d\n",masterXfer.rxData);
PRINTF("Hello World\n");
volatile static int i = 0 ;
while(1) {
i++ ;
}
return 0 ;
}