test/spi_loop_back/main.c

/*******************************************************************************
* # License
* Copyright 2019 Silicon Laboratories Inc. www.silabs.com
*******************************************************************************
*
* The licensor of this software is Silicon Laboratories Inc. Your use of this
* software is governed by the terms of Silicon Labs Master Software License
* Agreement (MSLA) available at
* www.silabs.com/about-us/legal/master-software-license-agreement. This
* software is distributed to you in Source Code format and is governed by the
* sections of the MSLA applicable to Source Code.
*
******************************************************************************/
/* Documentation for this app is available online.
* See https://docs.silabs.com/gecko-os/4/standard/latest/sdk/examples/wifi/scan
*/
#include "gos.h"
#define INTERVAL_BTN_SCANS (150) // ms
#define INITIAL_DELAY_MS (1500) //ms
#define APPLICATION_START_LINE "\r\nSPI Loop Test ..."
#define SPI_PORT GOS_SPI_0
#define SPI_BITS 8
#define SPI_CS 5
#define SPI_CS_GPIO GOS_GPIO_5
#define SPI_CLOCK_SPEED 150000 //15MHz
#define SPI_LCD_BIT8 false
#define SPI_FLAGS GOS_SPI_MSB_FIRST | GOS_SPI_CLOCK_RISING_EDGE | GOS_SPI_READ_AFTER_WRITE
#define SPI_TX_BUFFER_SIZE 1024
#define SPI_RX_BUFFER_SIZE SPI_TX_BUFFER_SIZE
#define SPI_DATA_FRAME_LEN 16
static uint8_t spi_tx_buffer[SPI_TX_BUFFER_SIZE];
static uint8_t spi_rx_buffer[SPI_RX_BUFFER_SIZE+1];
static const uint8_t spi_data_frame[SPI_DATA_FRAME_LEN] = "1234567890123456";
/*************************************************************************************************/
void gos_app_init(void)
{
gos_spi_message_t spi_message;
uint16_t count;
GOS_LOG(APPLICATION_START_LINE);
gos_rtos_delay_milliseconds(INITIAL_DELAY_MS);
//Setup the spi port configuration structure
const gos_spi_device_t spi_device =
{
.port = SPI_PORT,
.bits = SPI_BITS,
.chip_select = SPI_CS,
.speed = SPI_CLOCK_SPEED,
.lcd_bit8 = SPI_LCD_BIT8,
.flags = SPI_FLAGS,
};
//Since CS is software controlled, the designated CS is configured as output
//Set up the spi tx buffer with multiple copies of the data frame
for(count = 0; count < SPI_TX_BUFFER_SIZE; count += SPI_DATA_FRAME_LEN)
{
memcpy(&spi_tx_buffer[count],spi_data_frame,SPI_DATA_FRAME_LEN);
}
//Setup the spi message structure
spi_message.tx_buffer = spi_tx_buffer;
spi_message.rx_buffer = spi_rx_buffer;
spi_message.length = 0;
GOS_LOG( "SPI LOOP TEST STATUS:Starting" );
// Send multiples of the SPI_DATA_FRAME_LEN on the Tx Line
for(count = 1; spi_message.length < SPI_TX_BUFFER_SIZE; count++)
{
spi_message.length = count * SPI_DATA_FRAME_LEN;
memset(spi_message.rx_buffer,0,SPI_RX_BUFFER_SIZE);
GOS_LOG("SPI LOOP TEST STATUS:Testing with %d bytes",spi_message.length);
result = gos_spi_master_transfer(&spi_device,&spi_message,1);
if(result != GOS_SUCCESS)
{
GOS_LOG("SPI LOOP TEST RESULT:Write Fail");
break;
}
/*Since we are looping the MOSI and MISO the data sent on the Tx line should be
received on the Rx line without any corruption.
If the Tx and Rx data do not match, we dump the tx buffer, rx buffer with the
length of data that the was used for the test case.
*/
if(memcmp(spi_message.tx_buffer,spi_message.rx_buffer,spi_message.length))
{
gos_dump_buffer(spi_message.tx_buffer, spi_message.length, "Transmitted Data",
GOS_DUMP_FLAGS(16, 1, LITTLE, ADD_SPACE, NO_ADDRESSES, PRINT_ASCII ));
gos_dump_buffer(spi_message.rx_buffer, spi_message.length, "Received Data",
GOS_DUMP_FLAGS(16, 1, LITTLE, ADD_SPACE, NO_ADDRESSES, PRINT_ASCII ));
result = GOS_ERROR;
break;
}
}
if(result == GOS_SUCCESS)
{
GOS_LOG("SPI LOOP TEST RESULT:Success");
}
else
{
GOS_LOG("SPI LOOP TEST RESULT:Fail");
}
GOS_LOG("SPI LOOP TEST STATUS:Complete");
}