Microchip® Advanced Software Framework

mega/drivers/spi/unit_tests/unit_tests.c File Reference

Unit tests for SPI driver of megaRF.

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

#include <asf.h>
#include <conf_test.h>

Macros

#define DATA_BUFFER_SIZE   0x05
 
#define SPI_DEVICE_SELECT   IOPORT_CREATE_PIN(PORTB, 0)
 
#define SPI_MASTER_DUMMY   0xFF
 

Functions

static void flash_read_data (void)
 Read data from flash. More...
 
static uint8_t get_spi_divisor (void)
 Get SPI module baudrate divisor value. More...
 
int main (void)
 Run SPI driver unit tests. More...
 
static void run_baudrate_set_test (const struct test_case *test)
 Test baudrate setting. More...
 
static void run_spi_transfer_test (const struct test_case *test)
 Test SPI transfer. More...
 
static void spi_deselect_device (void)
 Deselect given device on the SPI bus. More...
 
static status_code_t spi_read (volatile void *spi, uint8_t *data, size_t len)
 Receive a sequence of bytes from a SPI device. More...
 
static void spi_select_device (void)
 Select given device on the SPI bus. More...
 
static status_code_t spi_write (volatile void *spi, const uint8_t *data, size_t len)
 Send a sequence of bytes to a SPI device. More...
 

Variables

static const uint8_t data_buffer_write [] = {0x84, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05}
 
static const uint8_t data_flash_read [] = {0xd2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
 
static const uint8_t data_flash_write [] = {0x83, 0x00, 0x00, 0x00}
 
static uint8_t data_received [DATA_BUFFER_SIZE]
 
static const uint8_t data_sent [] = {0x01, 0x02, 0x03, 0x04, 0x05}
 

#define DATA_BUFFER_SIZE   0x05
#define SPI_DEVICE_SELECT   IOPORT_CREATE_PIN(PORTB, 0)
#define SPI_MASTER_DUMMY   0xFF

Referenced by spi_read().

static void flash_read_data ( void  )
static

Read data from flash.

Read command is sent to read the data from specific page of the flash

Parameters
testCurrent test case.

References DATA_BUFFER_SIZE, data_flash_read, data_received, spi_deselect_device(), spi_read(), spi_select_device(), and spi_write().

Referenced by run_spi_transfer_test().

static uint8_t get_spi_divisor ( void  )
static

Get SPI module baudrate divisor value.

Returns
The divisor value the SPI module is configured to

References SPI_CLK2X_bm, SPI_PRESCALER_DIV128_gc, SPI_PRESCALER_DIV16_gc, SPI_PRESCALER_DIV4_gc, SPI_PRESCALER_DIV64_gc, and SPI_PRESCALER_gm.

Referenced by run_baudrate_set_test().

int main ( void  )

Run SPI driver unit tests.

Main function.

The SAM3X_EK, SAM3X Arduino board and SAM4C_EK use two bytes length internal address EEPROM.

References usart_rs232_options::baudrate, board_init(), DEFINE_TEST_ARRAY, DEFINE_TEST_CASE, DEFINE_TEST_SUITE, NULL, run_baudrate_set_test(), run_spi_transfer_test(), stdio_serial_init(), sysclk_init(), test_suite_run(), and usart_serial_options.

static void run_baudrate_set_test ( const struct test_case test)
static

Test baudrate setting.

This test sets different baudrates and test if the divisor is set as expected.

Parameters
testCurrent test case.

References get_spi_divisor(), spi_enable(), spi_set_baud_div(), sysclk_enable_peripheral_clock(), and test_assert_true.

Referenced by main().

static void run_spi_transfer_test ( const struct test_case test)
static

Test SPI transfer.

In this test SPI master sends write command to write set of data and send read command to get the data back from external flash.

Parameters
testCurrent test case.

References DATA_BUFFER_SIZE, data_buffer_write, data_flash_write, data_received, data_sent, flash_read_data(), spi_deselect_device(), spi_enable(), spi_enable_master_mode(), spi_select_device(), spi_set_baud_div(), spi_set_clock_mode(), spi_write(), sysclk_enable_peripheral_clock(), sysclk_get_cpu_hz(), and test_assert_true.

Referenced by main().

static void spi_deselect_device ( void  )
static

Deselect given device on the SPI bus.

References ioport_set_pin_high(), and SPI_DEVICE_SELECT.

Referenced by flash_read_data(), run_spi_transfer_test(), and spi_at45dbx_mem_check().

static status_code_t spi_read ( volatile void *  spi,
uint8_t *  data,
size_t  len 
)
static

Receive a sequence of bytes from a SPI device.

All bytes sent out on SPI bus are sent as value 0.

Parameters
spiBase address of the SPI instance.
datadata buffer to read
lenLength of data
Precondition
SPI device must be selected with spi_select_device() first

References spi_get(), spi_is_tx_ok(), SPI_MASTER_DUMMY, spi_put(), and STATUS_OK.

Referenced by flash_read_data().

static void spi_select_device ( void  )
static

Select given device on the SPI bus.

References ioport_set_pin_low(), and SPI_DEVICE_SELECT.

Referenced by flash_read_data(), run_spi_transfer_test(), and spi_at45dbx_mem_check().

static status_code_t spi_write ( volatile void *  spi,
const uint8_t *  data,
size_t  len 
)
static

Send a sequence of bytes to a SPI device.

Received bytes on the SPI bus are discarded.

Parameters
spiBase address of the SPI instance.
datadata buffer to write
lenLength of data
Precondition
SPI device must be selected with spi_select_device() first

References spi_is_tx_ok(), spi_put(), and STATUS_OK.

Referenced by flash_read_data(), and run_spi_transfer_test().

const uint8_t data_buffer_write[] = {0x84, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05}
static

Referenced by run_spi_transfer_test().

const uint8_t data_flash_read[] = {0xd2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
static

Referenced by flash_read_data().

const uint8_t data_flash_write[] = {0x83, 0x00, 0x00, 0x00}
static

Referenced by run_spi_transfer_test().

uint8_t data_received[DATA_BUFFER_SIZE]
static
const uint8_t data_sent[] = {0x01, 0x02, 0x03, 0x04, 0x05}
static

Referenced by run_spi_transfer_test().