Pseudo-Code Example - NXP Semiconductors MKL27Z128VFM4 Reference Manual

Table of Contents

Advertisement

Initialization/application information
1. Update the Control Register 1 (SPIx_C1) to enable the SPI and to control interrupt
enables. This register also sets the SPI as master or slave, determines clock phase and
polarity, and configures the main SPI options.
2. Update the Control Register 2 (SPIx_C2) to enable additional SPI functions such as
the SPI match interrupt feature, the master mode-fault function, and bidirectional
mode output as well as to control 8- or 16-bit mode selection and other optional
features.
3. Update the Baud Rate Register (SPIx_BR) to set the prescaler and bit rate divisor for
an SPI master.
4. Update the Hardware Match Register (SPIx_MH:SPIx_ML) with the value to be
compared to the receive data register for triggering an interrupt if hardware match
interrupts are enabled.
5. In the master, read SPIx_S while S[SPTEF] = 1, and then write to the transmit data
register (SPIx_DH:SPIx_DL) to begin transfer.

35.6.2 Pseudo-Code Example

In this example, the SPI module is set up for master mode with only hardware match
interrupts enabled. The SPI runs in 16-bit mode at a maximum baud rate of SPI module
clock divided by 2. Clock phase and polarity are set for an active-high SPI clock where
the first edge on SPSCK occurs at the start of the first cycle of a data transfer.
SPIx_C1=0x54(%01010100)
Bit 7
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
SPIx_C2 = 0xC0(%11000000)
Bit 7
Bit 6
Bit 5
Bit 4
606
SPIE
=
0
SPE
=
1
SPTIE
=
0
MSTR
=
1
CPOL
=
0
CPHA
=
1
SSOE
=
0
LSBFE
=
0
SPMIE
=
1
SPIMODE
=
1
TXDMAE
=
0
MODFEN
=
0
Table continues on the next page...
KL27 Sub-Family Reference Manual , Rev. 5, 01/2016
Disables receive and mode fault interrupts
Enables the SPI system
Disables SPI transmit interrupts
Sets the SPI module as a master SPI device
Configures SPI clock as active-high
First edge on SPSCK at start of first data transfer cycle
Determines SS pin function when mode fault enabled
SPI serial data transfers start with most significant bit
SPI hardware match interrupt enabled
Configures SPI for 16-bit mode
DMA request disabled
Disables mode fault function
Freescale Semiconductor, Inc.

Advertisement

Table of Contents
loading

Table of Contents