SPI
Feedback Loop
The simplest way to test the SPI interface is with just a jumper (wire) and looping a data transmission from the PICO GPIO pin; back into the POCI GPIO pin. For this example, users are free to utilize any method or hardware they have to connect the PICO and POCI GPIO pins together. However, we recommend some IC hooks for a temporary connection.
Optional Hardware
SPI_Loopback.ino
#include <SPI.h>
#include <cstdint>
void setup()
{
Serial.begin();
while(!Serial) {delay(100);};
Serial.println("SPI loopback test.");
SPI.begin();
SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE1));
testByteTransfer();
testWordTransfer();
testArrayTransfer();
SPI.endTransaction();
SPI.end();
}
void loop()
{
}
void testByteTransfer(void)
{
const uint8_t testByte = 0b10100011;
Serial.print("Sending: 0b");
Serial.println(testByte, BIN);
uint8_t recvByte = SPI.transfer(testByte);
Serial.print("Received: 0b");
Serial.println(recvByte, BIN);
if (recvByte == testByte)
{
Serial.println("Byte loopback successful.");
}
else
{
Serial.println("Byte loopback unsuccessful. Please check logs.");
}
}
void testWordTransfer(void)
{
const uint8_t testWord = 0b1111000010100101;
Serial.print("Sending: 0b");
Serial.println(testWord, BIN);
uint8_t recvWord = SPI.transfer(testWord);
Serial.print("Received: 0b");
Serial.println(recvWord, BIN);
if (recvWord == testWord)
{
Serial.println("Word loopback successful.");
}
else
{
Serial.println("Word loopback unsuccessful. Please check logs.");
}
}
void testArrayTransfer(void)
{
const uint8_t numel = 18;
uint8_t *data = (uint8_t*)malloc(numel);
uint8_t *data_ptr = data;
for(uint8_t i = 0; i < numel; ++i)
{
*data_ptr++ = i;
}
SPI.transfer(data, numel);
data_ptr = data;
/* 0 is inital expected results, 255 is when pin is open
so initialse result variable to 111 to make it clear the value has changed. */
uint8_t result = 111;
Serial.println();
Serial.println("Multiple byte transfer test");
Serial.println("element value result");
for(uint8_t i = 0; i< numel; ++i)
{
result = *data_ptr++;
Serial.print(i);
Serial.print("\t");
Serial.print(result);
Serial.print("\t");
if(result == i)
{
Serial.println("pass");
}
else
{
Serial.println("fail");
}
}
}
Peripheral Device
A more direct method for testing the SPI interface is with an actual SPI device.
BME688 Environmental Sensor
Users are free to utilize any hardware they already have; however, we recommend the BME688 environmental sensor, below. Its SPI pins are broken out on the edge of the board and can be easily connected to the RA6M5 Thing Plus. In addition, a hookup guide and Arduino library for the sensor are available.
Optional Hardware
Install Arduino Library
Users will need to install the Bosch BME68x Arduino library for the sensor. In the Arduino IDE, users can install it by searching for BME68x Sensor Library
, in the Library Manager:
BME68x Sensor Library
Users can find this sketch in the File > Examples > BME68x Sensor library > forced_mode drop-down menu. For more details on utilizing the BME68x breakout board, please refer to our hookup guide for the sensor.
forced_mode.ino
/**
* Copyright (C) 2021 Bosch Sensortec GmbH
*
* SPDX-License-Identifier: BSD-3-Clause
*
*/
#include "Arduino.h"
#include "bme68xLibrary.h"
#ifndef PIN_CS
#define PIN_CS SS
#endif
Bme68x bme;
/**
* @brief Initializes the sensor and hardware settings
*/
void setup(void)
{
SPI.begin();
Serial.begin(115200);
while (!Serial)
delay(10);
/* initializes the sensor based on SPI library */
bme.begin(PIN_CS, SPI);
if(bme.checkStatus())
{
if (bme.checkStatus() == BME68X_ERROR)
{
Serial.println("Sensor error:" + bme.statusString());
return;
}
else if (bme.checkStatus() == BME68X_WARNING)
{
Serial.println("Sensor Warning:" + bme.statusString());
}
}
/* Set the default configuration for temperature, pressure and humidity */
bme.setTPH();
/* Set the heater configuration to 300 deg C for 100ms for Forced mode */
bme.setHeaterProf(300, 100);
Serial.println("TimeStamp(ms), Temperature(deg C), Pressure(Pa), Humidity(%), Gas resistance(ohm), Status");
}
void loop(void)
{
bme68xData data;
bme.setOpMode(BME68X_FORCED_MODE);
delayMicroseconds(bme.getMeasDur());
if (bme.fetchData())
{
bme.getData(data);
Serial.print(String(millis()) + ", ");
Serial.print(String(data.temperature) + ", ");
Serial.print(String(data.pressure) + ", ");
Serial.print(String(data.humidity) + ", ");
Serial.print(String(data.gas_resistance) + ", ");
Serial.println(data.status, HEX);
}
}