Overview
The SPI Example project is a demonstration program that uses the KSDK software. This example provides 5 examples SPI board to board with 5 modes: SPI polling, non-blocking, blocking, DMA-blocking, DMA-non blocking and SPI loopback
- SPI board to board:
- Transfer data through instance 0 of SPI interface. SPI0 pins of master board are connected with SPI0 pins of slave board.
- It is important to ensure all SPI board-to-board connections are kept as short as possible and that a solid ground wire is connected between the boards. Preferably this ground connection should be as close as possible to the SPI signals on each board. A poor board-to-board connection compromises data signal integrity causing failures in the example.
- Master send an array to slave and receive the array back from slave, compare whether the two buffers are the same. Slave send back received buffer from master (Setup slave first)
- SPI master loopback:
- Transfer data through instance 0 of SPI interface, MISO pin and MOSI pin are connected
- Send an array out through MISO pin and compare it with the received buffer from MOSI pin to see whether they are the same.
Supported Platforms
These Freescale Freedom development platforms and Tower System modules are supported by the SPI example.
- FRDM-KL02Z (*) (DMA not supported)
- FRDM-KL03Z (*) (DMA not supported)
- FRDM-KL25Z (*) (DMA not supported)
- FRDM-KL26Z
- FRDM-KL27Z (*) (DMA not supported)
- FRDM-KL43Z
- FRDM-KL46Z
- MRB-KW01
- TWR-KL25Z48M (*) (DMA not supported)
- TWR-KL43Z48M
System Requirement
Hardware requirements
- J-Link ARM
- P&E Micro Multi-link universal
- Mini/micro USB cable
- USB A to micro AB cable
- Hardware (tower/base board, ...) for specific device
- Personal Computer
Toolchain requirements
- IAR embedded Workbench version 7.30.4
- ARM GCC 4.8.3 2014q3
- Keil MDK 5.13
- Kinetis Design Studio IDE v.2.5.0
- Atollic TrueSTUDIO for ARM win32 v5.2.1
Software requirements
- The project files are in: <SDK_Install>/examples/<board>/driver_examples/spi/<use_case>/<toolchain>.
- Library dependencies: ksdk_platform_lib
Getting Started
Hardware settings
- SPI master loopback:
- Transfer data through instance 0 of SPI interface, MISO pin and MOSI pin are connected
- SPI board to board:
- Transfer data through instance 0 of SPI interface, SPI0 pins of master board are connected with SPI0 pins of slave board
This example requires two separate boards. Connect the instance SPI0 master to SPI0 slave:
FRDM-KL02Z:
Master Board | Connects To | Slave Board |
Pin Name | Board Location | | Pin Name | Board Location |
MISO | J7 pin 5 | -> | MISO | J7 pin 5 |
MOSI | J7 pin 4 | -> | MOSI | J7 pin 4 |
SCK | J7 pin 6 | -> | SCK | J7 pin 6 |
PCS0 | J7 pin 3 | -> | PCS0 | J7 pin 3 |
GND | J7 pin 7 | -> | GND | J7 pin 7 |
FRDM-KL03Z:
Master Board | Connects To | Slave Board |
Pin Name | Board Location | | Pin Name | Board Location |
MISO | J2 pin 5 | -> | MISO | J2 pin 5 |
MOSI | J2 pin 4 | -> | MOSI | J2 pin 4 |
SCK | J2 pin 6 | -> | SCK | J2 pin 6 |
PCS0 | J2 pin 3 | -> | PCS0 | J2 pin 3 |
GND | J2 pin 7 | -> | GND | J2 pin 7 |
FRDM-KL25Z:
Master Board | Connects To | Slave Board |
Pin Name | Board Location | | Pin Name | Board Location |
MISO | J1 pin 11 | -> | MISO | J1 pin 11 |
MOSI | J1 pin 1 | -> | MOSI | J1 pin 1 |
SCK | J1 pin 9 | -> | SCK | J1 pin 9 |
PCS0 | J1 pin 7 | -> | PCS0 | J1 pin 7 |
GND | J2 pin 14 | -> | GND | J2 pin 14 |
FRDM-KL26Z:
Master Board | Connects To | Slave Board |
Pin Name | Board Location | | Pin Name | Board Location |
MISO | J1 pin 9 | -> | MISO | J1 pin 9 |
MOSI | J1 pin 11 | -> | MOSI | J1 pin 11 |
SCK | J4 pin 9 | -> | SCK | J4 pin 9 |
PCS0 | J1 pin 7 | -> | PCS0 | J1 pin 7 |
GND | J2 pin 14 | -> | GND | J2 pin 14 |
FRDM-KL27Z:
Master Board | Connects To | Slave Board |
Pin Name | Board Location | | Pin Name | Board Location |
MISO | J2 pin 10 | -> | MISO | J2 pin 10 |
MOSI | J2 pin 8 | -> | MOSI | J2 pin 8 |
SCK | J2 pin 12 | -> | SCK | J2 pin 12 |
PCS0 | J2 pin 6 | -> | PCS0 | J2 pin 6 |
GND | J2 pin 14 | -> | GND | J2 pin 14 |
FRDM-KL43Z:
Master Board | Connects To | Slave Board |
Pin Name | Board Location | | Pin Name | Board Location |
MISO | J2 pin 10 | -> | MISO | J2 pin 10 |
MOSI | J2 pin 8 | -> | MOSI | J2 pin 8 |
SCK | J2 pin 12 | -> | SCK | J2 pin 12 |
PCS0 | J2 pin 6 | -> | PCS0 | J2 pin 6 |
GND | J2 pin 14 | -> | GND | J2 pin 14 |
FRDM-KL46Z:
Master Board | Connects To | Slave Board |
Pin Name | Board Location | | Pin Name | Board Location |
MISO | J3 pin 3 | -> | MISO | J3 pin 3 |
MOSI | J3 pin 5 | -> | MOSI | J3 pin 5 |
SCK | J3 pin 7 | -> | SCK | J3 pin 7 |
PCS0 | J3 pin 9 | -> | PCS0 | J3 pin 9 |
GND | J3 pin 14 | -> | GND | J3 pin 14 |
MRB-KW01:
Master Board | Connects To | Slave Board |
Pin Name | Board Location | | Pin Name | Board Location |
MISO | J15 pin 20 | -> | MISO | J15 pin 20 |
MOSI | J15 pin 19 | -> | MOSI | J15 pin 19 |
SCK | J15 pin 18 | -> | SCK | J15 pin 18 |
PCS0 | J14 pin 16 | -> | PCS0 | J14 pin 16 |
GND | J15 pin 15 | -> | GND | J15 pin 15 |
TWR-KL43Z48M:
Master Board | Connects To | Slave Board |
Pin Name | Board Location | | Pin Name | Board Location |
MISO | Primary Elevator B44 | -> | MISO | Primary Elevator B44 |
MOSI | Primary Elevator B45 | -> | MOSI | Primary Elevator B45 |
SCK | Primary Elevator B48 | -> | SCK | Primary Elevator B48 |
PCS0 | Primary Elevator B46 | -> | PCS0 | Primary Elevator B46 |
GND | Primary Elevator B2 | -> | GND | Primary Elevator B2 |
Prepare the example
- Connect a USB cable between the PC host and the OpenSDA USB port on the board.
- Open a serial terminal with these settings:
- 115200 baud rate
- 8 data bits
- No parity
- One stop bit
- No flow control
- Download the program to the target board.
- Either press the reset button on your board or launch the debugger in your IDE to begin running the example.
Run the example
SPI blocking Master - Slave
Setup the Slave first. The Slave board has to be powered up first) and then the slave board echoes to the terminal:
SPI board to board blocking example
Be sure
master's SPI0 and slave's SPI0 are connected
Slave example is running...
SPI is waiting to receive data
Master sends an array to slave and receives the array back from slave and compares whether the two buffers are the same. The master board prints to the terminal:
SPI board to board blocking example
Ensure that the
master's SPI0 and slave's SPI0 are connected
Baud rate in Hz is: 500000
Master transmit:
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F
20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F
30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F
Master receive:
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F
20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F
30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F
SPI
master blocking transfer succeed!
Press any key to run again
The slave board receives and prints to the terminal:
Slave receive:
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F
20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F
30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F
Slave example is running...
SPI is waiting to receive data
SPI non-blocking Master - Slave
Set up the Slave first and then the slave board echoes to the terminal:
SPI board to board non-blocking example
Be sure
master's SPI0 and slave's SPI0 are connected
Slave example is running...
SPI is waiting to receive data
Master sends an array to slave and receives the array back from slave and compares whether the two buffers are the same. The master board prints to the terminal:
SPI board to board non-blocking example
Ensure that the
master's SPI0 and slave's SPI0 are connected
Baud rate in Hz is: 500000
Master transmit:
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F
20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F
30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F
Master receive:
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F
20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F
30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F
SPI
master blocking transfer succeed!
Press any key to run again
The slave board receives and prints to the terminal:
Slave receive:
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F
20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F
30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F
Slave example is running...
SPI is waiting to receive data
SPI EDMA blocking Master - Slave
Set up the Slave first and then the slave board echoes to the terminal:
SPI board to board DMA blocking example
Be sure
master's SPI0 and slave's SPI0 are connected
Slave example is running...
SPI is waiting to receive data
Master sends an array to slave and receives the array back from slave and compares whether the two buffers are the same. The master board prints to the terminal:
SPI board to board DMA-blocking example
Ensure that the
master's SPI0 and slave's SPI0 are connected
Baud rate in Hz is: 500000
Master transmit:
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F
20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F
30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F
Master receive:
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F
20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F
30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F
SPI
master blocking transfer succeed!
Press any key to run again
The slave board receives and prints to the terminal:
Slave receive:
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F
20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F
30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F
Slave example is running...
SPI is waiting to receive data
SPI EDMA non-blocking Master - Slave
Set up the Slave first and then the slave board echoes to the terminal:
SPI board to board DMA non-blocking example
Be sure
master's SPI0 and slave's SPI0 are connected
Slave example is running...
SPI is waiting to receive data
Master sends an array to slave and receives the array back from slave and compares whether the two buffers are the same. The master board prints to the terminal:
SPI board to board DMA-non-blocking example
Ensure
master's SPI0 and slave's SPI0 are connected
Baud rate in Hz is: 500000
Master transmit:
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F
20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F
30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F
Master receive:
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F
20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F
30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F
SPI
master blocking transfer succeed!
Press any key to run again
The slave board receives and prints to the terminal:
Slave receive:
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F
20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F
30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F
Slave example is running...
SPI is waiting to receive data
SPI loopback
These instructions are displayed/shown on the terminal window:
SPI loopback example
Be sure MISO-to-MOSI are connected
Baud rate in Hz is: 500000
Master transmit:
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F
20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F
30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F
Master receive:
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F
20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F
30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F
SPI
master blocking transfer succeed!
Press any key to run again