View detail for Atmel AT07890: SAM4 Serial Peripheral Interface (SPI)

APPLICATION NOTE
AT07890: SAM4 Serial Peripheral Interface (SPI)
ASF PROGRAMMERS MANUAL
SAM4 Serial Peripheral Interface (SPI)
This driver for SAM4 devices provides an interface for the configuration and
management of the device's Serial Peripheral Interface functionality.
The Serial Peripheral Interface is a synchronous serial data link that provides
communication with external devices in master or slave mode. It also enables
communication between processors if an external processor is connected to the
system.
The following peripherals are used by this module:
●
SPI (Serial Peripheral Interface)
The outline of this documentation is as follows:
●
Prerequisites
●
Module Overview
●
Special Considerations
●
Extra Information
●
Examples
●
API Overview
42290A-MCU-05/2014
Table of Contents
SAM4 Serial Peripheral Interface (SPI) ............................................. 1
Software License ................................................................................ 4
1. Prerequisites ................................................................................. 5
2. Module Overview .......................................................................... 6
2.1.
2.2.
2.3.
2.4.
Bus Topology .............................................................................
Master to Slave Device Interface ...................................................
Data Transfer .............................................................................
Application .................................................................................
6
6
6
7
3. Special Considerations ................................................................. 8
3.1.
3.2.
3.3.
3.4.
I/O Lines ...................................................................................
Power Management ....................................................................
Interrupt ....................................................................................
Peripheral DMA Controller (PDC/PDCA) ..........................................
8
8
8
8
4. Extra Information .......................................................................... 9
5. Examples .................................................................................... 10
6. API Overview .............................................................................. 11
6.1.
6.2.
6.3.
Variable and Type Definitions ...................................................... 11
6.1.1.
Type spi_cs_behavior_t ................................................. 11
Macro Definitions ...................................................................... 11
6.2.1.
Macro spi_get_pcs ....................................................... 11
Function Definitions ................................................................... 11
6.3.1.
Function spi_calc_baudrate_div() .................................... 11
6.3.2.
Function spi_configure_cs_behavior() .............................. 12
6.3.3.
Function spi_disable() ................................................... 12
6.3.4.
Function spi_disable_clock() ........................................... 12
6.3.5.
Function spi_disable_interrupt() ...................................... 13
6.3.6.
Function spi_disable_loopback() ..................................... 13
6.3.7.
Function spi_disable_mode_fault_detect() ......................... 13
6.3.8.
Function spi_disable_peripheral_select_decode() ............... 14
6.3.9.
Function spi_disable_tx_on_rx_empty() ............................ 14
6.3.10. Function spi_enable() ................................................... 14
6.3.11. Function spi_enable_clock() ........................................... 14
6.3.12. Function spi_enable_interrupt() ....................................... 15
6.3.13. Function spi_enable_loopback() ...................................... 15
6.3.14. Function spi_enable_mode_fault_detect() ......................... 16
6.3.15. Function spi_enable_peripheral_select_decode() ................ 16
6.3.16. Function spi_enable_tx_on_rx_empty() ............................. 16
6.3.17. Function spi_get() ........................................................ 16
6.3.18. Function spi_get_mode() ............................................... 17
6.3.19. Function spi_get_mode_fault_detect_setting() .................... 17
6.3.20. Function spi_get_pdc_base() .......................................... 17
6.3.21. Function spi_get_peripheral_select_decode_setting() .......... 18
6.3.22. Function spi_get_peripheral_select_mode() ....................... 18
6.3.23. Function spi_get_rx_access() ......................................... 19
6.3.24. Function spi_get_tx_access() ......................................... 19
6.3.25. Function spi_get_tx_on_rx_empty_setting() ....................... 20
6.3.26. Function spi_get_writeprotect_status() .............................. 20
6.3.27. Function spi_is_enabled() .............................................. 20
6.3.28. Function spi_is_rx_full() ................................................. 21
6.3.29. Function spi_is_rx_ready() ............................................. 21
6.3.30. Function spi_is_tx_empty() ............................................. 22
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
2
6.4.
6.3.31. Function spi_is_tx_ready() ............................................. 22
6.3.32. Function spi_put() ........................................................ 22
6.3.33. Function spi_read() ...................................................... 23
6.3.34. Function spi_read_interrupt_mask() ................................. 23
6.3.35. Function spi_read_status() ............................................. 24
6.3.36. Function spi_reset() ...................................................... 24
6.3.37. Function spi_set_baudrate_div() ...................................... 24
6.3.38. Function spi_set_bits_per_transfer() ................................ 25
6.3.39. Function spi_set_clock_phase() ...................................... 25
6.3.40. Function spi_set_clock_polarity() ..................................... 26
6.3.41. Function spi_set_delay_between_chip_select() .................. 26
6.3.42. Function spi_set_fixed_peripheral_select() ........................ 27
6.3.43. Function spi_set_lastxfer() ............................................. 27
6.3.44. Function spi_set_master_mode() ..................................... 27
6.3.45. Function spi_set_peripheral_chip_select_value() ................ 27
6.3.46. Function spi_set_slave_mode() ....................................... 28
6.3.47. Function spi_set_transfer_delay() .................................... 28
6.3.48. Function spi_set_variable_peripheral_select() .................... 29
6.3.49. Function spi_set_writeprotect() ....................................... 29
6.3.50. Function spi_write() ...................................................... 29
Enumeration Definitions .............................................................. 30
6.4.1.
Enum spi_cs_behavior .................................................. 30
6.4.2.
Enum spi_status_t ........................................................ 30
7. Extra Information for Serial Peripheral Interface Driver .............. 31
7.1.
7.2.
7.3.
7.4.
Acronyms ................................................................................
Dependencies ...........................................................................
Errata ......................................................................................
Module History .........................................................................
31
31
31
31
8. Examples for SPI Driver ............................................................. 32
8.1.
8.2.
8.3.
8.4.
Quick
8.1.1.
8.1.2.
Serial
8.2.1.
8.2.2.
8.2.3.
8.2.4.
8.2.5.
8.2.6.
Serial
8.3.1.
8.3.2.
8.3.3.
8.3.4.
8.3.5.
8.3.6.
Serial
8.4.1.
8.4.2.
8.4.3.
8.4.4.
8.4.5.
8.4.6.
Start Guide for SPI driver ...................................................
Basic Use Case ............................................................
Setup Steps .................................................................
Peripheral Interface (SPI) Master/Slave example .....................
Purpose ......................................................................
Requirements ...............................................................
Description ...................................................................
Main Files ....................................................................
Compilation Information ..................................................
Usage .........................................................................
Peripheral Interface (SPI) DMA slave example .......................
Purpose ......................................................................
Requirements ...............................................................
Description ...................................................................
Main Files ....................................................................
Compilation Information ..................................................
Usage .........................................................................
Peripheral Interface (SPI) PDC example ...............................
Purpose ......................................................................
Requirements ...............................................................
Description ...................................................................
Main Files ....................................................................
Compilation Information ..................................................
Usage .........................................................................
32
32
32
33
33
33
34
34
34
34
35
35
35
35
35
35
35
36
36
36
37
37
37
37
Index ................................................................................................. 39
Document Revision History .............................................................. 40
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
3
Software License
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided with the distribution.
3. The name of Atmel may not be used to endorse or promote products derived from this software without specific
prior written permission.
4. This software may only be redistributed and used in connection with an Atmel microcontroller product.
THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN
NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
4
1.
Prerequisites
There are no prerequisites for this module.
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
5
2.
Module Overview
The Serial Peripheral Interface (SPI) is a synchronous serial data link that provides communication with external
devices in master or slave mode.
The SPI is essentially a shift register that serially transmits data bits to other SPIs. During a data transfer, one SPI
device acts as the master which controls the data flow, while the other devices act as slaves. Data is clocked into
and out of the slave(s) by the master.
2.1
Bus Topology
SPI systems can be configured in two topologies:
●
Single Master Protocol: Contains a single CPU that is always the master while other devices in the system are
always slaves.
●
Multiple Master Protocol: Contains different CPUs and they take turns being the master.
Note
2.2
One master may simultaneously shift data into multiple slaves. However, only one slave may drive its
output to write data back to the master at any given time.
Master to Slave Device Interface
A slave device is selected when the master asserts its NSS signal. If multiple slave devices exist, the master
generates a separate slave select signal for each slave (NPCS). The SPI system consists of two data lines and two
control lines:
2.3
●
Master Out Slave In (MOSI): This data line supplies the output data from the master shifted into the input(s) of
the slave(s).
●
Master In Slave Out (MISO): This data line supplies the output data from a slave to the input of the master.
There may be no more than one slave transmitting data during any particular transfer.
●
Serial Clock (SPCK): This control line is driven by the master and regulates the flow of the data bits. The
master may transmit data at a variety of baud rates; the SPCK line cycles once for each bit that is transmitted.
●
Slave Select (NSS): As long as this control line is high, nothing will be shifted out/in from the slave's shift
register when the SPI module is configured in slave mode.
Data Transfer
Four combinations of polarity (CPOL) and phase (NCPHA) are available for data transfers. The clock polarity and
phase can be controlled by user applications. These two parameters determine the edges of the clock signal on
which data is driven and sampled. Each of the two parameters has two possible states, resulting in four possible
combinations that are incompatible with one another:
SPI Mode
CPOL
NCPHA
Shift SPCK
Edge
Capture SPCK
Edge
SPCK Inactive
Level
0
0
1
Falling
Rising
Low
1
0
0
Rising
Falling
Low
2
1
1
Rising
Falling
High
3
1
0
Falling
Rising
High
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
6
Note
2.4
A pair of master/slave devices must use the same clock polarity and phase values to communicate. If
multiple slaves are used and fixed in different configurations, the master must reconfigure itself each
time it needs to communicate with a slave of a different type.
Application
The SPI module can be used to communicate with external memories such as DataFlash® and 3-wire EEPROMs
and a wide variety of external peripherals e.g. ADCs, DACs, LCD Controllers, CAN Controllers, sensors, and coprocessors.
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
7
3.
Special Considerations
3.1
I/O Lines
The pins used for interfacing to external devices may be multiplexed with GPIO lines. The user application must
first program the GPIO controller to assign these pins to the SPI module.
3.2
Power Management
The SPI module may be clocked through the Power Management Controller (PMC), thus the user application must
first configure the PMC to enable the SPI clock.
3.3
Interrupt
The SPI module has an interrupt line connected to the Nested Vectored Interrupt Controller (NVIC). Handling the
SPI interrupt requires that the NVIC is configured before configuring the SPI.
3.4
Peripheral DMA Controller (PDC/PDCA)
The SPI module can be used in conjunction with the PDC in order to reduce processor overhead. For a full
description of the PDC/PDCA, refer to the corresponding section in the device-specific datasheet.
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
8
4.
Extra Information
For extra information, see Extra Information for Serial Peripheral Interface Driver. This includes:
●
Acronyms
●
Dependencies
●
Errata
●
Module History
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
9
5.
Examples
For a list of examples related to this driver, see Examples for SPI Driver.
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
10
6.
API Overview
6.1
Variable and Type Definitions
6.1.1
Type spi_cs_behavior_t
typedef enum spi_cs_behavior spi_cs_behavior_t
SPI Peripheral Chip Select (NPCSx) behavior modes while transferring data in Master mode.
6.2
Macro Definitions
6.2.1
Macro spi_get_pcs
#define spi_get_pcs(chip_sel_id) \
Note
When chip select n is asserted, NPCSn is set to a low level.
Table 6-1. Parameters
Data direction
Parameter name
Description
[in]
chip_sel_id
The chip select number used
6.3
Function Definitions
6.3.1
Function spi_calc_baudrate_div()
Calculate the baudrate divider.
int16_t spi_calc_baudrate_div(
const uint32_t baudrate,
uint32_t mck)
Table 6-2. Parameters
Data direction
Parameter name
Description
[in]
baudrate
Baudrate value
[in]
mck
SPI module input clock frequency
(MCK clock in Hz)
Returns
The baudrate divider or an error code.
Table 6-3. Return Values
Return value
Description
>=1
Valid baudrate divisor in the range 1 to 255
-1
The desired baudrate cannot be achieved with the
supplied parameters
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
11
6.3.2
Function spi_configure_cs_behavior()
Configure the Peripheral Chip Select (NPCSx) behavior for SPI data transfer.
void spi_configure_cs_behavior(
Spi * p_spi,
uint32_t ul_pcs_ch,
uint32_t ul_cs_behavior)
Table 6-4. Parameters
6.3.3
Data direction
Parameter name
Description
[in, out]
p_spi
Module hardware register base
address pointer
[in]
ul_pcs_ch
Peripheral Chip Select channel
(range 0 to 3)
[in]
ul_cs_behavior
Behavior of the Chip Select after
transfer
Function spi_disable()
Disable the SPI module.
void spi_disable(
Spi * p_spi)
Note
Peripheral Chip Select (NPCSx) is de-asserted, which indicates that the last data is done, and the
user application should check TX_EMPTY before disabling SPI.
Table 6-5. Parameters
6.3.4
Data direction
Parameter name
Description
[out]
p_spi
Module hardware register base
address pointer
Data direction
Parameter name
Description
[in]
p_spi
Module hardware register base
address pointer
Function spi_disable_clock()
Disable the SPI module clock.
void spi_disable_clock(
Spi * p_spi)
Table 6-6. Parameters
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
12
6.3.5
Function spi_disable_interrupt()
Disable SPI interrupts.
void spi_disable_interrupt(
Spi * p_spi,
uint32_t ul_sources)
Table 6-7. Parameters
Data direction
Parameter name
Description
[out]
p_spi
Module hardware register base
address pointer
[in]
ul_sources
A bitmask of interrupts to be
disabled
Where input parameter ul_sources is a bitmask containing one or more of the following:
6.3.6
Parameter Value
Description
SPI_IDR_RDRF
Receive Data Register Full interrupt disable
SPI_IDR_TDRE
Transmit Data Register Empty interrupt disable
SPI_IDR_MODF
Mode Fault Error interrupt disable
SPI_IDR_OVRES
Overrun Error interrupt disable
SPI_IDR_ENDRX
End of Receive Buffer interrupt disable
SPI_IDR_ENDTX
End of Transmit Buffer interrupt disable
SPI_IDR_RXBUFF
Receive Buffer Full interrupt disable
SPI_IDR_TXBUFE
Transmit Buffer Empty interrupt disable
SPI_IDR_NSSR
NSS Rising interrupt disable
SPI_IDR_TXEMPTY
Transmission Registers Empty interrupt disable
SPI_IDR_UNDES
Underrun Error interrupt disable
Function spi_disable_loopback()
Disable SPI internal loopback mode.
void spi_disable_loopback(
Spi * p_spi)
Table 6-8. Parameters
6.3.7
Data direction
Parameter name
Description
[in, out]
p_spi
Module hardware register base
address pointer
Function spi_disable_mode_fault_detect()
Disable Mode Fault Detection.
void spi_disable_mode_fault_detect(
Spi * p_spi)
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
13
Table 6-9. Parameters
6.3.8
Data direction
Parameter name
Description
[in, out]
p_spi
Module hardware register base
address pointer
Function spi_disable_peripheral_select_decode()
Disable Peripheral Select Decode.
void spi_disable_peripheral_select_decode(
Spi * p_spi)
Table 6-10. Parameters
6.3.9
Data direction
Parameter name
Description
[in, out]
p_spi
Module hardware register base
address pointer
Function spi_disable_tx_on_rx_empty()
Disable the SPI module starting data transfers only when the Receive Data Register is empty.
void spi_disable_tx_on_rx_empty(
Spi * p_spi)
Table 6-11. Parameters
6.3.10
Data direction
Parameter name
Description
[in, out]
p_spi
Module hardware register base
address pointer
Data direction
Parameter name
Description
[out]
p_spi
Module hardware register base
address pointer
Function spi_enable()
Enable the SPI module.
void spi_enable(
Spi * p_spi)
Table 6-12. Parameters
6.3.11
Function spi_enable_clock()
Enable the SPI module clock.
void spi_enable_clock(
Spi * p_spi)
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
14
Table 6-13. Parameters
6.3.12
Data direction
Parameter name
Description
[in]
p_spi
Module hardware register base
address pointer
Data direction
Parameter name
Description
[out]
p_spi
Module hardware register base
address pointer
[in]
ul_sources
A bitmask of interrupts to be
enabled
Function spi_enable_interrupt()
Enable SPI interrupts.
void spi_enable_interrupt(
Spi * p_spi,
uint32_t ul_sources)
Table 6-14. Parameters
Where input parameter ul_sources is a bitmask containing one or more of the following:
6.3.13
Parameter Value
Description
SPI_IER_RDRF
Receive Data Register Full interrupt enable
SPI_IER_TDRE
Transmit Data Register Empty interrupt enable
SPI_IER_MODF
Mode Fault Error interrupt enable
SPI_IER_OVRES
Overrun Error interrupt enable
SPI_IER_ENDRX
End of Receive Buffer interrupt enable
SPI_IER_ENDTX
End of Transmit Buffer interrupt enable
SPI_IER_RXBUFF
Receive Buffer Full interrupt enable
SPI_IER_TXBUFE
Transmit Buffer Empty interrupt enable
SPI_IER_NSSR
NSS Rising interrupt enable
SPI_IER_TXEMPTY
Transmission Registers Empty interrupt enable
SPI_IER_UNDES
Underrun Error interrupt enable
Function spi_enable_loopback()
Enable SPI internal loopback mode.
void spi_enable_loopback(
Spi * p_spi)
Table 6-15. Parameters
Data direction
Parameter name
Description
[in, out]
p_spi
Module hardware register base
address pointer
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
15
6.3.14
Function spi_enable_mode_fault_detect()
Enable Mode Fault Detection.
void spi_enable_mode_fault_detect(
Spi * p_spi)
Table 6-16. Parameters
6.3.15
Data direction
Parameter name
Description
[in, out]
p_spi
Module hardware register base
address pointer
Function spi_enable_peripheral_select_decode()
Enable Peripheral Select Decode.
void spi_enable_peripheral_select_decode(
Spi * p_spi)
Table 6-17. Parameters
6.3.16
Data direction
Parameter name
Description
[in, out]
p_spi
Module hardware register base
address pointer
Function spi_enable_tx_on_rx_empty()
Enable the SPI module to start data transfers only when the Receive Data Register is empty.
void spi_enable_tx_on_rx_empty(
Spi * p_spi)
Table 6-18. Parameters
6.3.17
Data direction
Parameter name
Description
[in, out]
p_spi
Module hardware register base
address pointer
Data direction
Parameter name
Description
[in]
p_spi
Module hardware register base
address pointer
Function spi_get()
Read SPI data.
uint16_t spi_get(
Spi * p_spi)
Table 6-19. Parameters
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
16
Returns
6.3.18
The data value.
Function spi_get_mode()
Get SPI Master/Slave operating mode.
uint32_t spi_get_mode(
Spi * p_spi)
Table 6-20. Parameters
Data direction
Parameter name
Description
[in]
p_spi
Module hardware register base
address pointer
Returns
The SPI module operating mode.
Table 6-21. Return Values
6.3.19
Return value
Description
0
Operating in Slave Mode
1
Operating in Master Mode
Function spi_get_mode_fault_detect_setting()
Check if mode fault detection is enabled.
uint32_t spi_get_mode_fault_detect_setting(
Spi * p_spi)
Table 6-22. Parameters
Data direction
Parameter name
Description
[in]
p_spi
Module hardware register base
address pointer
Returns
The mode fault detection status.
Table 6-23. Return Values
6.3.20
Return value
Description
0
Mode fault detection enabled
1
Mode fault detection disabled
Function spi_get_pdc_base()
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
17
Get PDC registers base address.
Pdc * spi_get_pdc_base(
Spi * p_spi)
Note
This function is not available on SAM4L devices.
Table 6-24. Parameters
Data direction
Parameter name
Description
[in]
p_spi
Module hardware register base
address pointer
Returns
6.3.21
PDC registers base for PDC driver to access.
Function spi_get_peripheral_select_decode_setting()
Get Peripheral Select Decode mode.
uint32_t spi_get_peripheral_select_decode_setting(
Spi * p_spi)
Table 6-25. Parameters
Data direction
Parameter name
Description
[in]
p_spi
Module hardware register base
address pointer
Returns
The Peripheral Select Decode mode.
Table 6-26. Return Values
6.3.22
Return value
Description
0
Direct mode is enabled
1
Decode mode is enabled
Function spi_get_peripheral_select_mode()
Get Peripheral Select mode.
uint32_t spi_get_peripheral_select_mode(
Spi * p_spi)
Table 6-27. Parameters
Data direction
Parameter name
Description
[in]
p_spi
Module hardware register base
address pointer
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
18
Returns
The Peripheral Select mode.
Table 6-28. Return Values
6.3.23
Return value
Description
0
Configured in Fixed Peripheral Select mode
1
Configured in Variable Peripheral Select mode
Function spi_get_rx_access()
Get receive data register address for DMA operation.
void * spi_get_rx_access(
Spi * p_spi)
Note
This function is only available on SAM3U and SAM3XA devices.
Table 6-29. Parameters
Data direction
Parameter name
Description
[in]
p_spi
Module hardware register base
address pointer
Returns
6.3.24
Receive address for DMA access.
Function spi_get_tx_access()
Get transmit data register address for DMA operation.
void * spi_get_tx_access(
Spi * p_spi)
Note
This function is only available on SAM3U and SAM3XA devices.
Table 6-30. Parameters
Returns
Data direction
Parameter name
Description
[in]
p_spi
Module hardware register base
address pointer
Transmit address for DMA access.
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
19
6.3.25
Function spi_get_tx_on_rx_empty_setting()
Check if SPI nodule is performing data transfers that start only when the Receive Data Register is empty.
uint32_t spi_get_tx_on_rx_empty_setting(
Spi * p_spi)
Table 6-31. Parameters
Data direction
Parameter name
Description
[in]
p_spi
Module hardware register base
address pointer
Returns
The transmit on receive data empty behavior.
Table 6-32. Return Values
6.3.26
Return value
Description
0
Transmit does not wait for receive data to be empty
before starting transmission.
1
Transmit waits for receive data to be empty
Function spi_get_writeprotect_status()
Indicate write protect status.
uint32_t spi_get_writeprotect_status(
Spi * p_spi)
Table 6-33. Parameters
Data direction
Parameter name
Description
[in]
p_spi
Module hardware register base
address pointer
Data direction
Parameter name
Description
[in]
p_spi
Module hardware register base
address pointer
Returns
6.3.27
SPI_WPSR value.
Function spi_is_enabled()
Check if the SPI module is enabled.
uint32_t spi_is_enabled(
Spi * p_spi)
Table 6-34. Parameters
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
20
Returns
The SPI module status.
Table 6-35. Return Values
6.3.28
Return value
Description
0
SPI module is disabled
1
SPI module is enabled
Function spi_is_rx_full()
Check if the SPI module contains received data.
uint32_t spi_is_rx_full(
Spi * p_spi)
Table 6-36. Parameters
Data direction
Parameter name
Description
[in]
p_spi
Module hardware register base
address pointer
Returns
The Receive Data Holding Register status.
Table 6-37. Return Values
6.3.29
Return value
Description
0
No data has been received
1
Data has been received
Function spi_is_rx_ready()
Check if all the SPI data receivers are ready.
uint32_t spi_is_rx_ready(
Spi * p_spi)
Table 6-38. Parameters
Data direction
Parameter name
Description
[in]
p_spi
Module hardware register base
address pointer
Returns
The SPI data receiver status.
Table 6-39. Return Values
Return value
Description
0
Data receivers are not ready
1
Data receivers are ready
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
21
6.3.30
Function spi_is_tx_empty()
Check if all the SPI data transmissions are complete.
uint32_t spi_is_tx_empty(
Spi * p_spi)
Table 6-40. Parameters
Data direction
Parameter name
Description
[in]
p_spi
Module hardware register base
address pointer
Returns
The SPI data transmission complete status.
Table 6-41. Return Values
6.3.31
Return value
Description
0
All data transmissions are not complete
1
All data transmissions are complete
Function spi_is_tx_ready()
Check if the SPI data transmitter is ready.
uint32_t spi_is_tx_ready(
Spi * p_spi)
Table 6-42. Parameters
Data direction
Parameter name
Description
[in]
p_spi
Module hardware register base
address pointer
Returns
The SPI data transmitter ready status.
Table 6-43. Return Values
6.3.32
Return value
Description
0
Data transmitter is not ready
1
Data transmitter is ready
Function spi_put()
Write SPI data.
void spi_put(
Spi * p_spi,
uint16_t data)
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
22
Table 6-44. Parameters
6.3.33
Data direction
Parameter name
Description
[out]
p_spi
Module hardware register base
address pointer
[in]
data
The data value to be transmitted
Function spi_read()
Read SPI data and it's peripheral chip select value.
spi_status_t spi_read(
Spi * p_spi,
uint16_t * us_data,
uint8_t * p_pcs)
Table 6-45. Parameters
Data direction
Parameter name
Description
[in]
p_spi
Module hardware register base
address pointer
[out]
us_data
Pointer to the location where to
store the received data word
[out]
p_pcs
Pointer to fill Peripheral Chip
Select value
Note
The Peripheral Chip Select value is only valid when the SPI module is configured to operate in Master
Mode.
Returns
SPI operation error code.
Table 6-46. Return Values
6.3.34
Return value
Description
SPI_OK
SPI Receive Data Register read successfully
SPI_ERROR_TIMEOUT
A Software time-out occurred while waiting for
incoming data
Function spi_read_interrupt_mask()
Read SPI interrupt mask.
uint32_t spi_read_interrupt_mask(
Spi * p_spi)
Table 6-47. Parameters
Data direction
Parameter name
Description
[in]
p_spi
Module hardware register base
address pointer
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
23
Returns
6.3.35
The interrupt mask value.
Function spi_read_status()
Read SPI status register.
uint32_t spi_read_status(
Spi * p_spi)
Table 6-48. Parameters
Data direction
Parameter name
Description
[in]
p_spi
Module hardware register base
address pointer
Returns
6.3.36
SPI status register value.
Function spi_reset()
Perform a software-triggered hardware reset of the SPI interface and set it into Slave Mode.
void spi_reset(
Spi * p_spi)
Table 6-49. Parameters
Note
6.3.37
Data direction
Parameter name
Description
[out]
p_spi
Module hardware register base
address pointer
PDC channels are not affected by a software reset.
Function spi_set_baudrate_div()
Set the Serial Clock Baud Rate divider value (SCBR).
void spi_set_baudrate_div(
Spi * p_spi,
uint32_t ul_pcs_ch,
uint8_t uc_baudrate_divider)
Table 6-50. Parameters
Data direction
Parameter name
Description
[in, out]
p_spi
Module hardware register base
address pointer
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
24
6.3.38
Data direction
Parameter name
Description
[in]
ul_pcs_ch
Peripheral Chip Select channel
(range 0 to 3)
[in]
uc_baudrate_divider
Baudrate divider from MCK
Function spi_set_bits_per_transfer()
Set the number of bits per SPI data transfer.
void spi_set_bits_per_transfer(
Spi * p_spi,
uint32_t ul_pcs_ch,
uint32_t ul_bits)
Table 6-51. Parameters
Data direction
Parameter name
Description
[in, out]
p_spi
Pointer to an SPI instance
[in]
ul_pcs_ch
Peripheral Chip Select channel
(range 0 to 3)
[in]
ul_bits
Number of bits (range 8 to 16)
Where the input parameter ul_bits can be one of the following :
6.3.39
Parameter Value
Description
SPI_CSR_BITS_8_BIT
8bit data transfers
SPI_CSR_BITS_9_BIT
9bit data transfers
SPI_CSR_BITS_10_BIT
10bit data transfers
SPI_CSR_BITS_11_BIT
11bit data transfers
SPI_CSR_BITS_12_BIT
12bit data transfers
SPI_CSR_BITS_13_BIT
13bit data transfers
SPI_CSR_BITS_14_BIT
14bit data transfers
SPI_CSR_BITS_15_BIT
15bit data transfers
SPI_CSR_BITS_16_BIT
16bit data transfers
Function spi_set_clock_phase()
Set the SPI data capture/change phase.
void spi_set_clock_phase(
Spi * p_spi,
uint32_t ul_pcs_ch,
uint32_t ul_phase)
Table 6-52. Parameters
Data direction
Parameter name
Description
[in, out]
p_spi
Module hardware register base
address pointer
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
25
6.3.40
Data direction
Parameter name
Description
[in]
ul_pcs_ch
Peripheral Chip Select channel
(range 0 to 3)
[in]
ul_phase
Data change/capture on SPCK
edge:
●
0 for data to be changed on
the leading edge and captured
on the following edge.
●
1 for data captured on the
leading edge and changed on
the following edge.
Function spi_set_clock_polarity()
Set the SPI clock (SPCK) inactive state.
void spi_set_clock_polarity(
Spi * p_spi,
uint32_t ul_pcs_ch,
uint32_t ul_polarity)
Table 6-53. Parameters
6.3.41
Data direction
Parameter name
Description
[in, out]
p_spi
Module hardware register base
address pointer
[in]
ul_pcs_ch
Peripheral Chip Select channel
(range 0 to 3)
[in]
ul_polarity
Inactive clock state of SPCK:
●
0 for logic level zero
●
1 for logic level one
Function spi_set_delay_between_chip_select()
Set the delay between chip selects (in MCK clocks).
void spi_set_delay_between_chip_select(
Spi * p_spi,
uint32_t ul_delay)
Note
If DLYBCS <= 6, 6 MCK clocks will be inserted by default.
Table 6-54. Parameters
Data direction
Parameter name
Description
[in, out]
p_spi
Module hardware register base
address pointer
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
26
6.3.42
Data direction
Parameter name
Description
[in]
ul_delay
Delay between chip selects (in
number of MCK clocks)
Function spi_set_fixed_peripheral_select()
Set Fixed Peripheral Select. Peripheral Chip Select is controlled by SPI_MR.
void spi_set_fixed_peripheral_select(
Spi * p_spi)
Table 6-55. Parameters
6.3.43
Data direction
Parameter name
Description
[in, out]
p_spi
Module hardware register base
address pointer
Function spi_set_lastxfer()
Issue a LASTXFER command. The next transfer is the last transfer and after that CS is de-asserted.
void spi_set_lastxfer(
Spi * p_spi)
Table 6-56. Parameters
6.3.44
Data direction
Parameter name
Description
[out]
p_spi
Module hardware register base
address pointer
Function spi_set_master_mode()
Configure the SPI module to operate in Master Mode.
void spi_set_master_mode(
Spi * p_spi)
Table 6-57. Parameters
6.3.45
Data direction
Parameter name
Description
[in, out]
p_spi
Module hardware register base
address pointer
Function spi_set_peripheral_chip_select_value()
Set the Peripheral Chip Select (NPCSx) value in 4 to 16 line decoder mode.
void spi_set_peripheral_chip_select_value(
Spi * p_spi,
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
27
uint32_t ul_value)
Table 6-58. Parameters
6.3.46
Data direction
Parameter name
Description
[in, out]
p_spi
Module hardware register base
address pointer
[in]
ul_value
Peripheral Chip Select value
If PCS decode mode is not
used, use spi_get_pcs to build
the value to use. On reset the
decode mode is not enabled. The
decode mode can be enabled/
disabled by the following functions:
spi_enable_peripheral_select_decode,
spi_disable_peripheral_select_decode.
Function spi_set_slave_mode()
Configure the SPI module to operate in Slave Mode.
void spi_set_slave_mode(
Spi * p_spi)
Table 6-59. Parameters
6.3.47
Data direction
Parameter name
Description
[in, out]
p_spi
Module hardware register base
address pointer
Function spi_set_transfer_delay()
Configure the timing for SPI data transfer.
void spi_set_transfer_delay(
Spi * p_spi,
uint32_t ul_pcs_ch,
uint8_t uc_dlybs,
uint8_t uc_dlybct)
Table 6-60. Parameters
Data direction
Parameter name
Description
[in, out]
p_spi
Pointer to an SPI instance
[in]
ul_pcs_ch
Peripheral Chip Select channel
(range 0 to 3)
[in]
uc_dlybs
Delay before SPCK (in MCK
clocks)
[in]
uc_dlybct
Delay between consecutive
transfers (in MCK clocks)
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
28
6.3.48
Function spi_set_variable_peripheral_select()
Set Variable Peripheral Select. Peripheral Chip Select can be controlled by SPI_TDR.
void spi_set_variable_peripheral_select(
Spi * p_spi)
Table 6-61. Parameters
6.3.49
Data direction
Parameter name
Description
[in, out]
p_spi
Module hardware register base
address pointer
Function spi_set_writeprotect()
Enable or disable write protection of the SPI registers.
void spi_set_writeprotect(
Spi * p_spi,
uint32_t ul_enable)
Table 6-62. Parameters
6.3.50
Data direction
Parameter name
Description
[out]
p_spi
Module hardware register base
address pointer
[in]
ul_enable
1 to enable, 0 to disable
Function spi_write()
Write SPI data with specified peripheral chip select value.
spi_status_t spi_write(
Spi * p_spi,
uint16_t us_data,
uint8_t uc_pcs,
uint8_t uc_lspi)
Table 6-63. Parameters
Data direction
Parameter name
Description
[in, out]
p_spi
Module hardware register base
address pointer
[in]
us_data
The data to transmit
[in]
uc_pcs
Peripheral Chip Select value. Only
valid when Variable Peripheral
Select is enabled, set to 0
otherwise.
[in]
uc_last
Indicate whether this data item is
the last. Only valid when Variable
Peripheral Select is enabled, set to
0 otherwise.
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
29
Returns
SPI operation error code.
Table 6-64. Return Values
Return value
Description
SPI_OK
SPI data transmitted successfully
SPI_ERROR_TIMEOUT
A Software time-out occurred while waiting to transmit
6.4
Enumeration Definitions
6.4.1
Enum spi_cs_behavior
SPI Peripheral Chip Select (NPCSx) behavior modes while transferring data in Master mode.
Table 6-65. Members
6.4.2
Enum value
Description
SPI_CS_KEEP_LOW
CS does not rise until a new transfer is
requested on different chip select.
SPI_CS_RISE_NO_TX
CS rises if there is no more data to transfer.
SPI_CS_RISE_FORCED
CS is de-asserted systematically during a time
DLYBCS.
Enum spi_status_t
Status codes used by the SPI driver.
Table 6-66. Members
Enum value
Description
SPI_ERROR
A non specific error occurred.
SPI_OK
No error encountered.
SPI_ERROR_TIMEOUT
An activity timeout occurred.
SPI_ERROR_ARGUMENT
Invalid argument specified.
SPI_ERROR_OVERRUN
Data overrun occurred.
SPI_ERROR_MODE_FAULT
Invalid master/slave mode specified.
SPI_ERROR_OVERRUN_AND_MODE_FAULT
Data overrun and and invalid master/slave
mode specified.
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
30
7.
Extra Information for Serial Peripheral Interface Driver
7.1
Acronyms
Below is a table listing the acronyms used in this module, along with their intended meanings.
7.2
Acronym
Definition
ADC
Analog to Digital Converter
CAN
Controller Area Network
CPU
Central Processor Unit
CS
Chip Select
DAC
Digital to Analog Converter
DMA
Direct Memory Access
DMAC
Direct Memory Access Controller
GPIO
General Purpose Input Output
LCD
Liquid Crystal Display
MCK
Master Clock
PDC
Peripheral DMA Controller
PDCA
Peripheral DMA Controller (SAM4L devices)
QSG
Quick Start Guide
UART
Universal Asynchronous Receiver Transmitter
Dependencies
This driver has the following dependencies:
7.3
●
General Purpose I/O (GPIO) Driver
●
System Clock Management (sysclk)
Errata
There are no errata related to this driver.
7.4
Module History
An overview of the module history is presented in the table below, with details on the enhancements and fixes
made to the module since its first release. The current version of this corresponds to the newest version in the
table.
Changelog
Initial document release
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
31
8.
Examples for SPI Driver
This is a list of the available Quick Start Guides (QSGs) and example applications for SAM4 Serial Peripheral
Interface (SPI). QSGs are simple examples with step-by-step instructions to configure and use this driver in a
selection of use cases. Note that QSGs can be compiled as a standalone application or be added to the user
application.
8.1
●
Quick Start Guide for SPI driver
●
Serial Peripheral Interface (SPI) Master/Slave example
●
Serial Peripheral Interface (SPI) DMA slave example
●
Serial Peripheral Interface (SPI) PDC example
Quick Start Guide for SPI driver
This is the quick start guide for the SAM4 Serial Peripheral Interface (SPI), with step-by-step instructions on how to
configure and use the driver in a selection of use cases.
The use cases contain several code fragments. The code fragments in the steps for setup can be copied into a
custom initialization function, while the steps for usage can be copied into, e.g. the main application function.
8.1.1
Basic Use Case
In this basic use case, the SPI module are configured for:
8.1.1.1
●
Master mode
●
Interrupt-based handling
Prerequisites
●
System Clock Management (sysclock)
8.1.2
Setup Steps
8.1.2.1
Example Code
Add the following into your application C-file:
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
void spi_master_init(Spi *p_spi)
{
spi_enable_clock(p_spi);
spi_reset(p_spi);
spi_set_master_mode(p_spi);
spi_disable_mode_fault_detect(p_spi);
spi_disable_loopback(p_spi);
spi_set_peripheral_chip_select_value(p_spi,
spi_get_pcs(DEFAULT_CHIP_ID));
spi_set_fixed_peripheral_select(p_spi);
spi_disable_peripheral_select_decode(p_spi);
spi_set_delay_between_chip_select(p_spi, CONFIG_SPI_MASTER_DELAY_BCS);
}
void spi_master_setup_device(Spi *p_spi, struct spi_device *device,
spi_flags_t flags, uint32_t baud_rate, board_spi_select_id_t sel_id)
{
spi_set_transfer_delay(p_spi, device->id, CONFIG_SPI_MASTER_DELAY_BS,
CONFIG_SPI_MASTER_DELAY_BCT);
spi_set_bits_per_transfer(p_spi, device->id, CONFIG_SPI_MASTER_BITS_PER_TRANSFER);
spi_set_baudrate_div(p_spi, device->id,
spi_calc_baudrate_div(baud_rate, sysclk_get_cpu_hz()));
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
32
*
*
*
*
*
*
8.1.2.2
spi_configure_cs_behavior(p_spi, device->id, SPI_CS_KEEP_LOW);
spi_set_clock_polarity(p_spi, device->id, flags >> 1);
spi_set_clock_phase(p_spi, device->id, ((flags & 0x1) ^ 0x1));
}
Workflow
1.
Initialize the SPI in Master Mode:
void spi_master_init(SPI_EXAMPLE);
2.
Set up an SPI device:
void spi_master_setup_device(SPI_EXAMPLE, &SPI_DEVICE_EXAMPLE,
*
SPI_MODE_0, SPI_EXAMPLE_BAUDRATE, 0);
Note
The returned device descriptor structure must be passed to the driver whenever that device
should be used as the current slave device.
3.
Enable SPI module:
spi_enable(SPI_EXAMPLE);
8.2
Serial Peripheral Interface (SPI) Master/Slave example
8.2.1
Purpose
This example uses the Serial Peripheral Interface (SPI) of one EK board in Slave Mode to communicate with
another EK board's SPI in Master Mode.
8.2.2
Requirements
This example can be used with two SAM4 evaluation kits such as the SAM4S Xplained, the SAM4S EK2, and other
evaluation kits. Refer to the list of kits available for the actual device on http://www.atmel.com.
8.2.2.1
Pin Connections
Connect the spi pins from one board to another as shown in Figure 8-1: Pin Connections on page 33.
Figure 8-1. Pin Connections
SAM4 Evaluation Kit
Master
SAM4 Evaluation Kit
Slave
MISO
MISO
MOSI
MOSI
SCLK
SCLK
NPCS0
NPCS0
GND
GND
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
33
8.2.3
Description
This example shows how to configure and transfer data using SPI. By default, example runs in SPI slave mode,
waiting for both SPI slave data input and UART user input.
8.2.4
8.2.5
Main Files
●
spi.c: Serial Peripheral Interface driver
●
spi.h: Serial Peripheral Interface driver header file
●
spi_example.c: Serial Peripheral Interface example application
Compilation Information
This software is written for GNU GCC and IAR Embedded Workbench for Atmel. Other compilers may or may not
work.
8.2.6
Usage
1.
Build the program and download it into the evaluation board
2.
On the computer, open, and configure a terminal application (e.g., HyperTerminal on Microsoft Windows) with
these settings:
●
115200 baud
●
8 bits of data
●
No parity
●
1 stop bit
●
No flow control
3.
Start the application
4.
In the terminal window, the following text should appear:
*
*
*
*
*
*
*
*
*
*
*
*
-- Spi Example --- xxxxxx-xx
-- Compiled: xxx xx xxxx xx:xx:xx -Menu :
-----0: Set SPCK = 500000 Hz
1: Set SPCK = 1000000 Hz
2: Set SPCK = 2000000 Hz
3: Set SPCK = 5000000 Hz
t: Perform SPI master
h: Display menu again
Initially the SPI module will be configured to operate in Slave Mode.
5.
Selecting menu option 't' will start the SPI transfer test:
●
Configure the SPI module as master, and set up the SPI clock
●
Send 4-byte CMD_TEST to indicate the start of a test
●
Send several 64-byte blocks, and after transmitting the next block, the content of the last block is returned
and checked.
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
34
6.
●
Send CMD_STATUS command and wait for the status reports from the slave
●
Send CMD_END command to indicate the end of test
The resulting terminal messages detail operations carried out in this SPI example, displaying success and/or
error messages depending on the results of the commands.
8.3
Serial Peripheral Interface (SPI) DMA slave example
8.3.1
Purpose
This example uses the Serial Peripheral Interface (SPI) of one EK board in Slave Mode to communicate with
another EK board's SPI in Master Mode using the DMAC module.
8.3.2
Requirements
This example can be used with SAM4 evaluation kits that support DMAC operation such as the SAM4E EK, and
other evaluation kits. Refer to the list of kits available for the actual device on http://www.atmel.com.
8.3.2.1
Pin Connections
Connect the spi pins from one board to another as shown in Figure 8-2: Pin Connections on page 35.
Figure 8-2. Pin Connections
8.3.3
SAM4 Evaluation Kit
Master
SAM4 Evaluation Kit
Slave
MISO
MISO
MOSI
MOSI
SCLK
SCLK
NPCS0
NPCS0
GND
GND
Description
This example shows how to configure and transfer data using SPI. By default, example runs in SPI slave mode,
waiting for both SPI slave data input and UART user input.
8.3.4
8.3.5
Main Files
●
spi.c: Serial Peripheral Interface driver
●
spi.h: Serial Peripheral Interface driver header file
●
spi_dmac_slave_example.c: Serial Peripheral Interface DMAC example application
Compilation Information
®
®
This software is written for GNU GCC and IAR Embedded Workbench for Atmel . Other compilers may or may
not work.
8.3.6
Usage
1.
Build the program and download it into the evaluation board
2.
On the computer, open, and configure a terminal application (e.g., HyperTerminal on Microsoft Windows )
with these settings:
®
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
®
35
●
115200 baud
●
8 bits of data
●
No parity
●
1 stop bit
●
No flow control
3.
Start the application
4.
In the terminal window, the following text should appear:
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
-- Spi DMA Slave Example --- xxxxxx-xx
-- Compiled: xxx xx xxxx xx:xx:xx -SPI works in slave mode.
Menu :
-----0: Set SPCK = 500000 Hz
1: Set SPCK = 1000000 Hz
2: Set SPCK = 2000000 Hz
3: Set SPCK = 5000000 Hz
s: Configure SPI as slave
m: Configure SPI as master
t: Perform communication sequence
h: Display menu again
Initially the SPI module will be configured to operate in Slave Mode.
5.
6.
Selecting menu option 't' will start the SPI transfer test:
●
Configure the SPI module as master, and set up the SPI clock
●
Send 4-byte CMD_TEST to indicate the start of a test
●
Send several 64-byte blocks, and after transmitting the next block, the content of the last block is returned
and checked.
●
Send CMD_STATUS command and wait for the status reports from the slave
●
Send CMD_END command to indicate the end of test
The resulting terminal messages detail operations carried out in this SPI example, displaying success and/or
error messages depending on the results of the commands.
8.4
Serial Peripheral Interface (SPI) PDC example
8.4.1
Purpose
This example uses the Serial Peripheral Interface (SPI) of one EK board in Slave Mode to communicate with
another EK board's SPI in Master Mode using the PDC functionality.
8.4.2
Requirements
This example can be used with two SAM4 evaluation kits such as the SAM4N Xplained Pro, SAM4S EK and other
evaluation kits. Refer to the list of kits available for the actual device on http://www.atmel.com.
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
36
8.4.2.1
Pin Connections
Connect the spi pins from one board to another as shown in Figure 8-3: Pin Connections on page 37.
Figure 8-3. Pin Connections
8.4.3
SAM4 Evaluation Kit
master
SAM4 Evaluation Kit
Slave
MISO
MISO
MOSI
MOSI
SCLK
SCLK
NPCS0
NPCS0
GND
GND
Description
This example shows how to configure and transfer data using SPI. By default, example runs in SPI slave mode,
waiting for both SPI slave data input and UART user input.
8.4.4
8.4.5
Main Files
●
spi.c: Serial Peripheral Interface driver
●
spi.h: Serial Peripheral Interface driver header file
●
spi_pdc_example.c: Serial Peripheral Interface PDC example application
Compilation Information
This software is written for GNU GCC and IAR Embedded Workbench for Atmel. Other compilers may or may not
work.
8.4.6
Usage
1.
Build the program and download it into the evaluation board
2.
On the computer, open, and configure a terminal application (e.g., HyperTerminal on Microsoft Windows) with
these settings:
●
115200 baud
●
8 bits of data
●
No parity
●
1 stop bit
●
No flow control
3.
Start the application
4.
In the terminal window, the following text should appear:
*
*
-- Spi Pdc Example
-- xxxxxx-xx
--
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
37
*
*
*
*
*
*
*
*
*
*
-- Compiled: xxx xx xxxx xx:xx:xx -Menu :
-----0: Set SPCK = 500000 Hz
1: Set SPCK = 1000000 Hz
2: Set SPCK = 2000000 Hz
3: Set SPCK = 5000000 Hz
t: Perform SPI master
h: Display menu again
Initially the SPI module will be configured to operate in Slave Mode.
5.
6.
Selecting menu option 't' will start the SPI transfer test:
●
Configure the SPI module as master, and set up the SPI clock
●
Send a 64-byte data block and check that it is received correctly
The resulting terminal messages detail operations carried out in this SPI example, displaying success and/or
error messages depending on the results of the commands.
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
38
Index
spi_set_variable_peripheral_select, 29
spi_set_writeprotect, 29
spi_write, 29
E
Enumeration Definitions
spi_cs_behavior, 30
spi_status_t, 30
M
F
T
Macro Definitions
spi_get_pcs, 11
Function Definitions
spi_calc_baudrate_div, 11
spi_configure_cs_behavior, 12
spi_disable, 12
spi_disable_clock, 12
spi_disable_interrupt, 13
spi_disable_loopback, 13
spi_disable_mode_fault_detect, 13
spi_disable_peripheral_select_decode, 14
spi_disable_tx_on_rx_empty, 14
spi_enable, 14
spi_enable_clock, 14
spi_enable_interrupt, 15
spi_enable_loopback, 15
spi_enable_mode_fault_detect, 16
spi_enable_peripheral_select_decode, 16
spi_enable_tx_on_rx_empty, 16
spi_get, 16
spi_get_mode, 17
spi_get_mode_fault_detect_setting, 17
spi_get_pdc_base, 17
spi_get_peripheral_select_decode_setting, 18
spi_get_peripheral_select_mode, 18
spi_get_rx_access, 19
spi_get_tx_access, 19
spi_get_tx_on_rx_empty_setting, 20
spi_get_writeprotect_status, 20
spi_is_enabled, 20
spi_is_rx_full, 21
spi_is_rx_ready, 21
spi_is_tx_empty, 22
spi_is_tx_ready, 22
spi_put, 22
spi_read, 23
spi_read_interrupt_mask, 23
spi_read_status, 24
spi_reset, 24
spi_set_baudrate_div, 24
spi_set_bits_per_transfer, 25
spi_set_clock_phase, 25
spi_set_clock_polarity, 26
spi_set_delay_between_chip_select, 26
spi_set_fixed_peripheral_select, 27
spi_set_lastxfer, 27
spi_set_master_mode, 27
spi_set_peripheral_chip_select_value, 27
spi_set_slave_mode, 28
spi_set_transfer_delay, 28
Type Definitions
spi_cs_behavior_t, 11
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
39
Document Revision History
Doc. Rev.
Date
Comments
42290A
05/2014
Initial document release
AT07890: SAM4 Serial Peripheral Interface (SPI) [APPLICATION NOTE]
42290A-MCU-05/2014
40
Atmel Corporation
1600 Technology Drive, San Jose, CA 95110 USA
T: (+1)(408) 441.0311
F: (+1)(408) 436.4200
|
www.atmel.com
© 2014 Atmel Corporation. All rights reserved. / Rev.: 42290A-MCU-05/2014
®
®
Atmel , Atmel logo and combinations thereof, Enabling Unlimited Possibilities , and others are registered trademarks or trademarks of Atmel Corporation or its
®
subsidiaries. Windows is a registered trademark of Microsoft Corporation in U.S. and or other countries. Other terms and product names may be trademarks
of others.
Disclaimer: The information in this document is provided in connection with Atmel products. No license, express or implied, by estoppel or otherwise, to any intellectual property right is granted by
this document or in connection with the sale of Atmel products. EXCEPT AS SET FORTH IN THE ATMEL TERMS AND CONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE, ATMEL
ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS, IMPLIED OR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
CONSEQUENTIAL, PUNITIVE, SPECIAL OR INCIDENTAL DAMAGES (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS AND PROFITS, BUSINESS INTERRUPTION, OR LOSS OF
INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT, EVEN IF ATMEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Atmel makes no
representations or warranties with respect to the accuracy or completeness of the contents of this document and reserves the right to make changes to specifications and products descriptions at
any time without notice. Atmel does not make any commitment to update the information contained herein. Unless specifically provided otherwise, Atmel products are not suitable for, and shall not be
used in, automotive applications. Atmel products are not intended, authorized, or warranted for use as components in applications intended to support or sustain life.