Microchip® Advanced Software Framework

spi_example.c File Reference

Serial Peripheral Interface (SPI) example for SAM.

Copyright (c) 2011-2018 Microchip Technology Inc. and its subsidiaries.

#include "asf.h"
#include "stdio_serial.h"
#include "conf_board.h"
#include "conf_clock.h"
#include "conf_spi_example.h"
#include "flexcom.h"

Data Structures

struct  status_block_t
 

Macros

#define CMD_DATA   0x29380000
 
#define CMD_DATA_MSK   0xFFFF0000
 
#define CMD_END   0x68390484
 
#define CMD_STATUS   0x68390384
 
#define CMD_TEST   0x10101010
 
#define COMM_BUFFER_SIZE   64
 
#define DATA_BLOCK_MSK   0x0000FFFF
 
#define MAX_DATA_BLOCK_NUMBER   4
 
#define MAX_RETRY   4
 
#define NB_STATUS_CMD   20
 
#define NUM_SPCK_CONFIGURATIONS   4
 
#define RC_RDY   0x12345678
 
#define RC_SYN   0x55AA55AA
 
#define SLAVE_STATE_DATA   2
 
#define SLAVE_STATE_END   5
 
#define SLAVE_STATE_IDLE   0
 
#define SLAVE_STATE_STATUS   4
 
#define SLAVE_STATE_STATUS_ENTRY   3
 
#define SLAVE_STATE_TEST   1
 
#define SPI_CHIP_PCS   spi_get_pcs(SPI_CHIP_SEL)
 
#define SPI_CHIP_SEL   0
 
#define SPI_CLK_PHASE   0
 
#define SPI_CLK_POLARITY   0
 
#define SPI_DLYBCT   0x10
 
#define SPI_DLYBS   0x40
 
#define STRING_EOL   "\r"
 
#define STRING_HEADER
 
#define UART_BAUDRATE   115200
 

Functions

static void configure_console (void)
 Configure the Console UART. More...
 
static void display_menu (void)
 Display the user menu on the terminal. More...
 
int main (void)
 Application entry point for SPI example. More...
 
void SPI_Handler (void)
 Interrupt handler for the SPI slave. More...
 
static void spi_master_go (void)
 Start SPI transfer test. More...
 
static void spi_master_initialize (void)
 Initialize SPI as master. More...
 
static void spi_master_transfer (void *p_buf, uint32_t size)
 Perform SPI master transfer. More...
 
static void spi_set_clock_configuration (uint8_t configuration)
 Set the specified SPI clock configuration. More...
 
static void spi_slave_command_process (void)
 SPI command block process. More...
 
static void spi_slave_initialize (void)
 Initialize SPI as slave. More...
 
static void spi_slave_new_command (void)
 Start waiting new command. More...
 
static void spi_slave_transfer (void *p_buf, uint32_t size)
 Set SPI slave transfer. More...
 

Variables

static uint8_t * gs_puc_transfer_buffer
 
static struct status_block_t gs_spi_status
 
static uint8_t gs_uc_spi_buffer [COMM_BUFFER_SIZE]
 
static const uint32_t gs_ul_clock_configurations []
 
static uint32_t gs_ul_spi_clock = 500000
 
static uint32_t gs_ul_spi_cmd = RC_SYN
 
static uint32_t gs_ul_spi_state = 0
 
static uint32_t gs_ul_test_block_number
 
static uint32_t gs_ul_transfer_index
 
static uint32_t gs_ul_transfer_length
 

#define CMD_DATA   0x29380000
#define CMD_DATA_MSK   0xFFFF0000
#define CMD_END   0x68390484
#define CMD_STATUS   0x68390384

Referenced by spi_master_go().

#define CMD_TEST   0x10101010
#define COMM_BUFFER_SIZE   64
#define DATA_BLOCK_MSK   0x0000FFFF
#define MAX_DATA_BLOCK_NUMBER   4

Referenced by spi_master_go().

#define MAX_RETRY   4

Referenced by spi_master_go().

#define NB_STATUS_CMD   20

Referenced by spi_slave_initialize().

#define NUM_SPCK_CONFIGURATIONS   4

Referenced by display_menu(), and main().

#define RC_RDY   0x12345678
#define RC_SYN   0x55AA55AA
#define SLAVE_STATE_DATA   2
#define SLAVE_STATE_END   5
#define SLAVE_STATE_IDLE   0
#define SLAVE_STATE_STATUS   4
#define SLAVE_STATE_STATUS_ENTRY   3
#define SLAVE_STATE_TEST   1
#define SPI_CHIP_PCS   spi_get_pcs(SPI_CHIP_SEL)
#define SPI_CHIP_SEL   0
#define SPI_CLK_PHASE   0
#define SPI_CLK_POLARITY   0
#define SPI_DLYBCT   0x10

Referenced by spi_master_initialize().

#define SPI_DLYBS   0x40

Referenced by spi_master_initialize().

#define STRING_EOL   "\r"
#define STRING_HEADER
Value:
"--Spi Example --\r\n" \
"-- "BOARD_NAME" --\r\n" \
"-- Compiled: "__DATE__" "__TIME__" --"STRING_EOL
#define STRING_EOL
Definition: spi_example.c:174
#define BOARD_NAME
Name string macro.
Definition: sam4l8_xplained_pro.h:86

Referenced by main().

#define UART_BAUDRATE   115200

static void configure_console ( void  )
static
static void display_menu ( void  )
static

Display the user menu on the terminal.

References NUM_SPCK_CONFIGURATIONS.

Referenced by main().

int main ( void  )

Application entry point for SPI example.

Returns
Unused (ANSI-C compatibility).

References board_init(), configure_console(), display_menu(), NUM_SPCK_CONFIGURATIONS, SPI_IRQn, spi_master_go(), spi_set_clock_configuration(), spi_slave_initialize(), STRING_HEADER, and sysclk_init().

static void spi_master_transfer ( void *  p_buf,
uint32_t  size 
)
static

Perform SPI master transfer.

Parameters
pbufPointer to buffer to transfer.
sizeSize of the buffer.

References SPI_MASTER_BASE, spi_read(), spi_read_status(), and spi_write().

Referenced by spi_master_go().

static void spi_set_clock_configuration ( uint8_t  configuration)
static

Set the specified SPI clock configuration.

Parameters
configurationIndex of the configuration to set.

References spi_master_initialize().

Referenced by main().

static void spi_slave_transfer ( void *  p_buf,
uint32_t  size 
)
static

Set SPI slave transfer.

Parameters
p_bufPointer to buffer to transfer.
sizeSize of the buffer.

References SPI_SLAVE_BASE, and spi_write().

Referenced by spi_slave_initialize(), and spi_slave_new_command().

uint8_t* gs_puc_transfer_buffer
static
uint8_t gs_uc_spi_buffer[COMM_BUFFER_SIZE]
static
const uint32_t gs_ul_clock_configurations[]
static
Initial value:
=
{ 500000, 1000000, 2000000, 5000000 }
uint32_t gs_ul_spi_clock = 500000
static
uint32_t gs_ul_spi_cmd = RC_SYN
static

Referenced by SPI_Handler().

uint32_t gs_ul_spi_state = 0
static
uint32_t gs_ul_test_block_number
static
uint32_t gs_ul_transfer_index
static

Referenced by SPI_Handler().

uint32_t gs_ul_transfer_length
static