Kinetis SDK v.1.3 API Reference Manual  Rev. 0
Freescale Semiconductor, Inc.
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages

Overview

This section describes the programming interface of the SPI HAL driver.

Files

file  fsl_spi_hal.h
 

Enumerations

enum  spi_status_t { ,
  kStatus_SPI_SlaveTxUnderrun,
  kStatus_SPI_SlaveRxOverrun,
  kStatus_SPI_Timeout,
  kStatus_SPI_Busy,
  kStatus_SPI_NoTransferInProgress,
  kStatus_SPI_OutOfRange,
  kStatus_SPI_TxBufferNotEmpty,
  kStatus_SPI_InvalidParameter,
  kStatus_SPI_NonInit,
  kStatus_SPI_AlreadyInitialized,
  kStatus_SPI_DMAChannelInvalid,
  kStatus_SPI_Error
}
 Error codes for the SPI driver. More...
 
enum  spi_master_slave_mode_t {
  kSpiMaster = 1,
  kSpiSlave = 0
}
 SPI master or slave configuration. More...
 
enum  spi_clock_polarity_t {
  kSpiClockPolarity_ActiveHigh = 0,
  kSpiClockPolarity_ActiveLow = 1
}
 SPI clock polarity configuration. More...
 
enum  spi_clock_phase_t {
  kSpiClockPhase_FirstEdge = 0,
  kSpiClockPhase_SecondEdge = 1
}
 SPI clock phase configuration. More...
 
enum  spi_shift_direction_t {
  kSpiMsbFirst = 0,
  kSpiLsbFirst = 1
}
 SPI data shifter direction options. More...
 
enum  spi_ss_output_mode_t {
  kSpiSlaveSelect_AsGpio = 0,
  kSpiSlaveSelect_FaultInput = 2,
  kSpiSlaveSelect_AutomaticOutput = 3
}
 SPI slave select output mode options. More...
 
enum  spi_pin_mode_t {
  kSpiPinMode_Normal = 0,
  kSpiPinMode_Input = 1,
  kSpiPinMode_Output = 3
}
 SPI pin mode options. More...
 
enum  spi_data_bitcount_mode_t {
  kSpi8BitMode = 0,
  kSpi16BitMode = 1
}
 SPI data length mode options. More...
 
enum  spi_interrupt_source_t {
  kSpiRxFullAndModfInt = 1,
  kSpiTxEmptyInt = 2,
  kSpiMatchInt = 3
}
 SPI interrupt sources. More...
 
enum  spi_int_status_flag_t {
  kSpiRxBufferFullFlag = SPI_S_SPRF_SHIFT,
  kSpiMatchFlag = SPI_S_SPMF_SHIFT,
  kSpiTxBufferEmptyFlag = SPI_S_SPTEF_SHIFT,
  kSpiModeFaultFlag = SPI_S_MODF_SHIFT
}
 SPI interrupt status flags. More...
 
enum  spi_fifo_interrupt_source_t {
  kSpiRxFifoNearFullInt = 1,
  kSpiTxFifoNearEmptyInt = 2
}
 SPI FIFO interrupt sources. More...
 
enum  spi_w1c_interrupt_t {
  kSpiRxFifoFullClearInt = 0,
  kSpiTxFifoEmptyClearInt = 1,
  kSpiRxNearFullClearInt = 2,
  kSpiTxNearEmptyClearInt = 3
}
 SPI FIFO write-1-to-clear interrupt flags. More...
 
enum  spi_txfifo_watermark_t
 SPI TX FIFO watermark settings. More...
 
enum  spi_rxfifo_watermark_t
 SPI RX FIFO watermark settings. More...
 
enum  spi_fifo_status_flag_t
 SPI status flags. More...
 
enum  spi_fifo_error_flag_t {
  kSpiNoFifoError = 0,
  kSpiRxfof = 1,
  kSpiTxfof = 2,
  kSpiRxfofTxfof = 3,
  kSpiRxferr = 4,
  kSpiRxfofRxferr = 5,
  kSpiTxfofRxferr = 6,
  kSpiRxfofTxfofRxferr = 7,
  kSpiTxferr = 8,
  kSpiRxfofTxferr = 9,
  kSpiTxfofTxferr = 10,
  kSpiRxfofTxfofTxferr = 11,
  kSpiRxferrTxferr = 12,
  kSpiRxfofRxferrTxferr = 13,
  kSpiTxfofRxferrTxferr = 14,
  kSpiRxfofTxfofRxferrTxferr =15
}
 SPI error flags. More...
 

Configuration

void SPI_HAL_Init (SPI_Type *base)
 Restores the SPI to the reset configuration. More...
 
static void SPI_HAL_Enable (SPI_Type *base)
 Enables the SPI peripheral. More...
 
static void SPI_HAL_Disable (SPI_Type *base)
 Disables the SPI peripheral. More...
 
uint32_t SPI_HAL_SetBaud (SPI_Type *base, uint32_t bitsPerSec, uint32_t sourceClockInHz)
 Sets the SPI baud rate in bits per second. More...
 
static void SPI_HAL_SetBaudDivisors (SPI_Type *base, uint32_t prescaleDivisor, uint32_t rateDivisor)
 Configures the baud rate divisors manually. More...
 
static void SPI_HAL_SetMasterSlave (SPI_Type *base, spi_master_slave_mode_t mode)
 Configures the SPI for master or slave. More...
 
static bool SPI_HAL_IsMaster (SPI_Type *base)
 Returns whether the SPI module is in master mode. More...
 
void SPI_HAL_SetSlaveSelectOutputMode (SPI_Type *base, spi_ss_output_mode_t mode)
 Sets how the slave select output operates. More...
 
void SPI_HAL_SetDataFormat (SPI_Type *base, spi_clock_polarity_t polarity, spi_clock_phase_t phase, spi_shift_direction_t direction)
 Sets the polarity, phase, and shift direction. More...
 
static uint32_t SPI_HAL_GetDataRegAddr (SPI_Type *base)
 Gets the SPI data register address for DMA operation. More...
 
void SPI_HAL_SetPinMode (SPI_Type *base, spi_pin_mode_t mode)
 Sets the SPI pin mode. More...
 

Interrupts

void SPI_HAL_SetIntMode (SPI_Type *base, spi_interrupt_source_t interrupt, bool enable)
 Enables or disables the SPI interrupts. More...
 
static void SPI_HAL_SetReceiveAndFaultIntCmd (SPI_Type *base, bool enable)
 Enables or disables the SPI receive buffer/FIFO full and mode fault interrupt. More...
 
static void SPI_HAL_SetTransmitIntCmd (SPI_Type *base, bool enable)
 Enables or disables the SPI transmit buffer/FIFO empty interrupt. More...
 
static void SPI_HAL_SetMatchIntCmd (SPI_Type *base, bool enable)
 Enables or disables the SPI match interrupt. More...
 

Status

static bool SPI_HAL_GetIntStatusFlag (SPI_Type *base, spi_int_status_flag_t flag)
 Gets the SPI interrupt status flag state. More...
 
static bool SPI_HAL_IsReadBuffFullPending (SPI_Type *base)
 Checks whether the read buffer/FIFO is full. More...
 
static bool SPI_HAL_IsTxBuffEmptyPending (SPI_Type *base)
 Checks whether the transmit buffer/FIFO is empty. More...
 
static bool SPI_HAL_IsModeFaultPending (SPI_Type *base)
 Checks whether a mode fault occurred. More...
 
void SPI_HAL_ClearModeFaultFlag (SPI_Type *base)
 Clears the mode fault flag. More...
 
static bool SPI_HAL_IsMatchPending (SPI_Type *base)
 Checks whether the data received matches the previously-set match value. More...
 
void SPI_HAL_ClearMatchFlag (SPI_Type *base)
 Clears the match flag. More...
 

Data transfer

static uint8_t SPI_HAL_ReadData (SPI_Type *base)
 Reads a byte from the data buffer. More...
 
static void SPI_HAL_WriteData (SPI_Type *base, uint8_t data)
 Writes a byte into the data buffer. More...
 
void SPI_HAL_WriteDataBlocking (SPI_Type *base, uint8_t data)
 Writes a byte into the data buffer and waits till complete to return. More...
 

Match byte

static void SPI_HAL_SetMatchValue (SPI_Type *base, uint8_t matchByte)
 Sets the value which triggers the match interrupt. More...
 

Enumeration Type Documentation

Enumerator
kStatus_SPI_SlaveTxUnderrun 

SPI Slave TX Underrun error.

kStatus_SPI_SlaveRxOverrun 

SPI Slave RX Overrun error.

kStatus_SPI_Timeout 

SPI transfer timed out.

kStatus_SPI_Busy 

SPI instance is already busy performing a transfer.

kStatus_SPI_NoTransferInProgress 

Attempt to abort a transfer when no transfer was in progress.

kStatus_SPI_OutOfRange 

SPI out-of-range error used in slave callback.

kStatus_SPI_TxBufferNotEmpty 

SPI TX buffer register is not empty.

kStatus_SPI_InvalidParameter 

Parameter is invalid.

kStatus_SPI_NonInit 

SPI driver is not initialized.

kStatus_SPI_AlreadyInitialized 

SPI driver already initialized.

kStatus_SPI_DMAChannelInvalid 

SPI driver cannot requests DMA channel.

kStatus_SPI_Error 

SPI driver error.

Enumerator
kSpiMaster 

SPI peripheral operates in master mode.

kSpiSlave 

SPI peripheral operates in slave mode.

Enumerator
kSpiClockPolarity_ActiveHigh 

Active-high SPI clock (idles low).

kSpiClockPolarity_ActiveLow 

Active-low SPI clock (idles high).

Enumerator
kSpiClockPhase_FirstEdge 

First edge on SPSCK occurs at the middle of the first cycle of a data transfer.

kSpiClockPhase_SecondEdge 

First edge on SPSCK occurs at the start of the first cycle of a data transfer.

Enumerator
kSpiMsbFirst 

Data transfers start with most significant bit.

kSpiLsbFirst 

Data transfers start with least significant bit.

Enumerator
kSpiSlaveSelect_AsGpio 

Slave select pin configured as GPIO.

kSpiSlaveSelect_FaultInput 

Slave select pin configured for fault detection.

kSpiSlaveSelect_AutomaticOutput 

Slave select pin configured for automatic SPI output.

Enumerator
kSpiPinMode_Normal 

Pins operate in normal, single-direction mode.

kSpiPinMode_Input 

Bidirectional mode.

Master: MOSI pin is input; Slave: MISO pin is input

kSpiPinMode_Output 

Bidirectional mode.

Master: MOSI pin is output; Slave: MISO pin is output

Enumerator
kSpi8BitMode 

8-bit data transmission mode

kSpi16BitMode 

16-bit data transmission mode

Enumerator
kSpiRxFullAndModfInt 

Receive buffer full (SPRF) and mode fault (MODF) interrupt.

kSpiTxEmptyInt 

Transmit buffer empty interrupt.

kSpiMatchInt 

Match interrupt.

Enumerator
kSpiRxBufferFullFlag 

Read buffer full flag.

kSpiMatchFlag 

Match flag.

kSpiTxBufferEmptyFlag 

Transmit buffer empty flag.

kSpiModeFaultFlag 

Mode fault flag.

Enumerator
kSpiRxFifoNearFullInt 

Receive FIFO nearly full interrupt.

kSpiTxFifoNearEmptyInt 

Transmit FIFO nearly empty interrupt.

Enumerator
kSpiRxFifoFullClearInt 

Receive FIFO full interrupt.

kSpiTxFifoEmptyClearInt 

Transmit FIFO empty interrupt.

kSpiRxNearFullClearInt 

Receive FIFO nearly full interrupt.

kSpiTxNearEmptyClearInt 

Transmit FIFO nearly empty interrupt.

Enumerator
kSpiNoFifoError 

No error is detected.

kSpiRxfof 

Receive FIFO Overflow.

kSpiTxfof 

Transmit FIFO Overflow.

kSpiRxfofTxfof 

Receive FIFO Overflow, Transmit FIFO Overflow.

kSpiRxferr 

Receive FIFO Error.

kSpiRxfofRxferr 

Receive FIFO Overflow, Receive FIFO Error.

kSpiTxfofRxferr 

Transmit FIFO Overflow, Receive FIFO Error.

kSpiRxfofTxfofRxferr 

Receive FIFO Overflow, Transmit FIFO Overflow, Receive FIFO Error.

kSpiTxferr 

Transmit FIFO Error.

kSpiRxfofTxferr 

Receive FIFO Overflow, Transmit FIFO Error.

kSpiTxfofTxferr 

Transmit FIFO Overflow, Transmit FIFO Error.

kSpiRxfofTxfofTxferr 

Receive FIFO Overflow, Transmit FIFO Overflow, Transmit FIFO Error.

kSpiRxferrTxferr 

Receive FIFO Error, Transmit FIFO Error.

kSpiRxfofRxferrTxferr 

Receive FIFO Overflow, Receive FIFO Error, Transmit FIFO Error.

kSpiTxfofRxferrTxferr 

Transmit FIFO Overflow, Receive FIFO Error, Transmit FIFO Error.

kSpiRxfofTxfofRxferrTxferr 

Receive FIFO Overflow, Transmit FIFO Overflow Receive FIFO Error, Transmit FIFO Error.

Function Documentation

void SPI_HAL_Init ( SPI_Type *  base)

This function basically resets all of the SPI registers to their default setting including disabling the module.

Parameters
baseModule base pointer of type SPI_Type.
static void SPI_HAL_Enable ( SPI_Type *  base)
inlinestatic
Parameters
baseModule base pointer of type SPI_Type.
static void SPI_HAL_Disable ( SPI_Type *  base)
inlinestatic
Parameters
baseModule base pointer of type SPI_Type.
uint32_t SPI_HAL_SetBaud ( SPI_Type *  base,
uint32_t  bitsPerSec,
uint32_t  sourceClockInHz 
)

This function takes in the desired bitsPerSec (baud rate) and calculates the nearest possible baud rate without exceeding the desired baud rate unless the baud rate requested is less than the absolute minimum in which case the minimum baud rate is returned. The returned baud rate is in bits-per-second. It requires that the caller also provide the frequency of the module source clock (in Hertz).

Parameters
baseModule base pointer of type SPI_Type.
bitsPerSecThe desired baud rate in bits per second.
sourceClockInHzModule source input clock in Hertz.
Returns
The actual calculated baud rate in Hz.
static void SPI_HAL_SetBaudDivisors ( SPI_Type *  base,
uint32_t  prescaleDivisor,
uint32_t  rateDivisor 
)
inlinestatic

This function allows the caller to manually set the baud rate divisors in the event that these dividers are known and the caller does not wish to call the SPI_HAL_SetBaudRate function.

Parameters
baseModule base pointer of type SPI_Type.
prescaleDivisorbaud rate prescale divisor setting.
rateDivisorbaud rate divisor setting.
static void SPI_HAL_SetMasterSlave ( SPI_Type *  base,
spi_master_slave_mode_t  mode 
)
inlinestatic
Parameters
baseModule base pointer of type SPI_Type.
modeMode setting (master or slave) of type dspi_master_slave_mode_t.
static bool SPI_HAL_IsMaster ( SPI_Type *  base)
inlinestatic
Parameters
baseModule base pointer of type SPI_Type.
Returns
true The module is in master mode. false The module is in slave mode.
void SPI_HAL_SetSlaveSelectOutputMode ( SPI_Type *  base,
spi_ss_output_mode_t  mode 
)

This function allows the user to configure the slave select in one of the three operational modes: as GPIO, as a fault input, or as an automatic output for standard SPI modes.

Parameters
baseModule base pointer of type SPI_Type.
modeSelection input of one of three modes of type spi_ss_output_mode_t.
void SPI_HAL_SetDataFormat ( SPI_Type *  base,
spi_clock_polarity_t  polarity,
spi_clock_phase_t  phase,
spi_shift_direction_t  direction 
)

This function configures the clock polarity, clock phase, and data shift direction.

Parameters
baseModule base pointer of type SPI_Type.
polarityClock polarity setting of type spi_clock_polarity_t.
phaseClock phase setting of type spi_clock_phase_t.
directionData shift direction (MSB or LSB) of type spi_shift_direction_t.
static uint32_t SPI_HAL_GetDataRegAddr ( SPI_Type *  base)
inlinestatic

This function gets the SPI data register address as this value is needed for DMA operation.

Parameters
baseModule base pointer of type SPI_Type.
Returns
The SPI data register address.
void SPI_HAL_SetPinMode ( SPI_Type *  base,
spi_pin_mode_t  mode 
)

This function configures the SPI data pins to one of three modes (of type spi_pin_mode_t): Single direction mode: MOSI and MISO pins operate in normal, single direction mode. Bidirectional mode: Master: MOSI configured as input, Slave: MISO configured as input. Bidirectional mode: Master: MOSI configured as output, Slave: MISO configured as output.

Parameters
baseModule base pointer of type SPI_Type.
modeOperational of SPI pins of type spi_pin_mode_t.
void SPI_HAL_SetIntMode ( SPI_Type *  base,
spi_interrupt_source_t  interrupt,
bool  enable 
)

This function enables or disables the SPI receive buffer (or FIFO if the module supports a FIFO) full and mode fault interrupt, SPI transmit buffer (or FIFO if the module supports a FIFO) empty interrupt, and the SPI match interrupt.

Example, to set the receive and mode fault interrupt: SPI_HAL_SetIntMode(base, kSpiRxFullAndModfInt, true);

Parameters
baseModule base pointer of type SPI_Type.
interruptSPI interrupt source to configure of type spi_interrupt_source_t.
enableEnable (true) or disable (false) the receive buffer full and mode fault interrupt.
static void SPI_HAL_SetReceiveAndFaultIntCmd ( SPI_Type *  base,
bool  enable 
)
inlinestatic

This function enables or disables the SPI receive buffer (or FIFO if the module supports a FIFO) full and mode fault interrupt.

Parameters
baseModule base pointer of type SPI_Type.
enableEnable (true) or disable (false) the receive buffer full and mode fault interrupt.
static void SPI_HAL_SetTransmitIntCmd ( SPI_Type *  base,
bool  enable 
)
inlinestatic

This function enables or disables the SPI transmit buffer (or FIFO if the module supports a FIFO) empty interrupt.

Parameters
baseModule base pointer of type SPI_Type.
enableEnable (true) or disable (false) the transmit buffer empty interrupt.
static void SPI_HAL_SetMatchIntCmd ( SPI_Type *  base,
bool  enable 
)
inlinestatic

This function enables or disables the SPI match interrupt.

Parameters
baseModule base pointer of type SPI_Type.
enableEnable (true) or disable (false) the match interrupt.
static bool SPI_HAL_GetIntStatusFlag ( SPI_Type *  base,
spi_int_status_flag_t  flag 
)
inlinestatic

This function returns the state (set or cleared) of the SPI interrupt status flag.

Parameters
baseModule base pointer of type SPI_Type.
flagThe requested interrupt status flag of type spi_int_status_flag_t.
Returns
Current setting of the requested interrupt status flag.
static bool SPI_HAL_IsReadBuffFullPending ( SPI_Type *  base)
inlinestatic

The read buffer (or FIFO if the module supports a FIFO) full flag is only cleared by reading it when it is set, then reading the data register by calling the SPI_HAL_ReadData(). This example code demonstrates how to check the flag, read data, and clear the flag.

// Check read buffer flag.
{
// Read the data in the buffer, which also clears the flag.
byte = SPI_HAL_ReadData(base);
}
Parameters
baseModule base pointer of type SPI_Type.
Returns
Current setting of the read buffer full flag.
static bool SPI_HAL_IsTxBuffEmptyPending ( SPI_Type *  base)
inlinestatic

To clear the transmit buffer (or FIFO if the module supports a FIFO) empty flag, read the flag when it is set. Then, write a new data value into the transmit buffer with a call to the SPI_HAL_WriteData(). The example code shows how to do this.

// Check if transmit buffer is empty.
{
// Buffer has room, so write the next data value.
SPI_HAL_WriteData(base, byte);
}
Parameters
baseModule base pointer of type SPI_Type.
Returns
Current setting of the transmit buffer empty flag.
static bool SPI_HAL_IsModeFaultPending ( SPI_Type *  base)
inlinestatic
Parameters
baseModule base pointer of type SPI_Type.
Returns
Current setting of the mode fault flag.
void SPI_HAL_ClearModeFaultFlag ( SPI_Type *  base)
Parameters
baseModule base pointer of type SPI_Type
static bool SPI_HAL_IsMatchPending ( SPI_Type *  base)
inlinestatic
Parameters
baseModule base pointer of type SPI_Type.
Returns
Current setting of the match flag.
void SPI_HAL_ClearMatchFlag ( SPI_Type *  base)
Parameters
baseModule base pointer of type SPI_Type.
static uint8_t SPI_HAL_ReadData ( SPI_Type *  base)
inlinestatic
Parameters
baseModule base pointer of type SPI_Type.
Returns
The data read from the data buffer.
static void SPI_HAL_WriteData ( SPI_Type *  base,
uint8_t  data 
)
inlinestatic
Parameters
baseModule base pointer of type SPI_Type.
dataThe data to send.
void SPI_HAL_WriteDataBlocking ( SPI_Type *  base,
uint8_t  data 
)

This function writes data to the SPI data register and waits until the TX is empty to return.

Parameters
baseModule base pointer of type SPI_Type.
dataThe data to send.
static void SPI_HAL_SetMatchValue ( SPI_Type *  base,
uint8_t  matchByte 
)
inlinestatic
Parameters
baseModule base pointer of type SPI_Type.
matchByteThe value which triggers the match interrupt.