Microchip® Advanced Software Framework

drivers/sercom/spi/unit_test/unit_test.c File Reference

SAM SPI Unit test.

Copyright (c) 2013-2020 Microchip Technology Inc. and its subsidiaries.

#include <asf.h>
#include <stdio_serial.h>
#include <string.h>
#include "conf_test.h"

Macros

#define BUFFER_LENGTH   256
 
#define TEST_SPI_BAUDRATE   1000000UL
 

Functions

static void cdc_uart_init (void)
 Initialize the USART for unit test. More...
 
static void cleanup_buffer_polled_write_interrupt_read_test (const struct test_case *test)
 Cleanup function: Send data by polling & receive with interrupt. More...
 
int main (void)
 Run SPI unit tests. More...
 
static void run_baud_test (const struct test_case *test)
 Test: Sends data at different baud rates. More...
 
static void run_buffer_polled_write_interrupt_read_test (const struct test_case *test)
 Test: Send data by polling & receive with interrupt. More...
 
static void run_single_byte_polled_test (const struct test_case *test)
 Test sending and receiving a byte by polling. More...
 
static void run_spi_init_test (const struct test_case *test)
 Test initializing SPI master & slave. More...
 
static void run_transceive_buffer_test (const struct test_case *test)
 Test: Send & receive data using transceive functions. More...
 
static void run_transfer_9bit_test (const struct test_case *test)
 Test sending and receiving 9-bit data by polling. More...
 
static void setup_buffer_polled_write_interrupt_read_test (const struct test_case *test)
 Setup function: Send data by polling & receive with interrupt. More...
 
static void setup_transfer_9bit_test (const struct test_case *test)
 Setup function: Send & receive 9-bit data by polling. More...
 
static void user_spi_callback (struct spi_module *const module)
 SPI interrupt callback function. More...
 

Variables

struct usart_module cdc_uart_module
 
struct spi_module master
 
uint8_t rx_buf [BUFFER_LENGTH]
 
struct spi_module slave
 
struct spi_slave_inst slave_inst
 
uint8_t slave_rx_buf [BUFFER_LENGTH]
 
uint8_t slave_tx_buf [BUFFER_LENGTH]
 
volatile bool spi_init_success = false
 
volatile bool transfer_complete = false
 
uint8_t tx_buf [BUFFER_LENGTH]
 

#define TEST_SPI_BAUDRATE   1000000UL

static void cdc_uart_init ( void  )
static
static void cleanup_buffer_polled_write_interrupt_read_test ( const struct test_case test)
static

Cleanup function: Send data by polling & receive with interrupt.

This function unregisters and disables callback for buffer receive completed operation. It also clears the receive buffer rx_buf.

Parameters
testCurrent test case.

References BUFFER_LENGTH, i, rx_buf, slave, SPI_CALLBACK_BUFFER_RECEIVED, spi_disable_callback(), and spi_unregister_callback().

Referenced by main().

static void run_baud_test ( const struct test_case test)
static

Test: Sends data at different baud rates.

This test sends (writes) a byte to the slave and receives the data at different baudrate testing up to the maximum allowed level.

Transmission and reception are carried out by polling.

Parameters
testCurrent test case.

References spi_master_config::baudrate, master, spi_config::master, spi_config::mode_specific, spi_config::mux_setting, spi_config::pinmux_pad0, spi_config::pinmux_pad1, spi_config::pinmux_pad2, spi_config::pinmux_pad3, PINMUX_UNUSED, slave, slave_inst, spi_disable(), spi_enable(), spi_get_config_defaults(), spi_init(), spi_init_success, spi_read_buffer_wait(), spi_select_slave(), spi_write_buffer_wait(), and test_assert_true.

Referenced by main().

static void run_buffer_polled_write_interrupt_read_test ( const struct test_case test)
static

Test: Send data by polling & receive with interrupt.

This test sends (writes) an array of data to the slave by polling and receives (reads) the buffer back with interrupt and compares.

Parameters
testCurrent test case.

References BUFFER_LENGTH, i, master, rx_buf, slave, slave_inst, spi_init_success, spi_read_buffer_job(), spi_select_slave(), spi_write_buffer_wait(), test_assert_true, transfer_complete, and tx_buf.

Referenced by main().

static void run_single_byte_polled_test ( const struct test_case test)
static

Test sending and receiving a byte by polling.

This test sends (writes) one byte of data to the slave and receives (reads) the data back and compares.

Writing and reading are carried out by polling.

Parameters
testCurrent test case.

References master, slave, slave_inst, spi_init_success, spi_is_ready_to_read(), spi_is_ready_to_write(), spi_is_write_complete(), spi_read(), spi_select_slave(), spi_write(), and test_assert_true.

Referenced by main().

static void run_transceive_buffer_test ( const struct test_case test)
static

Test: Send & receive data using transceive functions.

This test sends (writes) an array of data to the slave and receives (reads) the buffer back using transceive functions and compares.

Parameters
testCurrent test case.

References BUFFER_LENGTH, i, master, rx_buf, slave, slave_inst, slave_rx_buf, slave_tx_buf, spi_init_success, spi_select_slave(), spi_transceive_buffer_job(), spi_transceive_buffer_wait(), STATUS_ERR_IO, STATUS_OK, test_assert_true, and tx_buf.

Referenced by main().

static void run_transfer_9bit_test ( const struct test_case test)
static

Test sending and receiving 9-bit data by polling.

This test sends (writes) one 9-bit data to the slave and receives (reads) the data back and compares.

Writing and reading are carried out by polling.

Parameters
testCurrent test case.

References master, slave, slave_inst, spi_init_success, spi_is_ready_to_read(), spi_is_ready_to_write(), spi_is_write_complete(), spi_read(), spi_select_slave(), spi_write(), and test_assert_true.

Referenced by main().

static void setup_buffer_polled_write_interrupt_read_test ( const struct test_case test)
static

Setup function: Send data by polling & receive with interrupt.

This function registers and enables callback for buffer receive completed operation.

Parameters
testCurrent test case.

References slave, SPI_CALLBACK_BUFFER_RECEIVED, spi_enable_callback(), spi_register_callback(), and user_spi_callback().

Referenced by main().

static void user_spi_callback ( struct spi_module *const  module)
static

SPI interrupt callback function.

Called by SPI driver when transmission/reception is complete.

Parameters
moduleSPI module causing the interrupt (not used)

References transfer_complete.

Referenced by setup_buffer_polled_write_interrupt_read_test().

struct usart_module cdc_uart_module
struct spi_module slave
uint8_t slave_rx_buf[BUFFER_LENGTH]
uint8_t slave_tx_buf[BUFFER_LENGTH]

Referenced by main(), and run_transceive_buffer_test().