Skip to content

Add spi #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ test_wifi_extras: TESTS=-DTEST_WIFI_EXTRAS

## SPI tests targets
test_spi_connected1_loopback: TESTS=-DTEST_SPI_CONNECTED1_LOOPBACK
test_spi_3wire: TESTS=-DTEST_SPI_3WIRE

# Arduino-cli commands

Expand Down
2 changes: 1 addition & 1 deletion Unity
Submodule Unity updated 122 files
68 changes: 68 additions & 0 deletions src/corelibs/spi/test_spi_3wire.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* test_spi_3wire.cpp
*
* This test is used to verify if the TLE5012 sensor is responding using 3-wire SPI communication.
*/

// std includes
#include <tlx5012-arduino.hpp>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might not be easy to test the 3-wire SPI interface... But this is not a 3wire SPI test, it is a TLE5012 test. Of course, if it works, then the affected 3-wire spi functionalities are as well validated.
We should be using the 3wire spi class and functions for it to belong in this project.

#include "test_common_includes.h"

// Use the tle5012 namespace
using namespace tle5012;

// TLE5012 sensor instance and variables
static Tle5012Ino Tle5012Sensor = Tle5012Ino();
static errorTypes checkError;

// Test group definition
TEST_GROUP(spi_3wire);

TEST_SETUP(spi_3wire) {
// Initialize the TLE5012 sensor

checkError = Tle5012Sensor.begin();
}

TEST_TEAR_DOWN(spi_3wire) {
// Cleanup if necessary
}

// Test case: Verify if the sensor is responding
TEST(spi_3wire, test_sensor_response) {
TEST_ASSERT_EQUAL(tle5012::NO_ERROR, checkError); // Check if initialization was successful
}


// Test case: Request data from the sensor using 3-wire SPI
TEST(spi_3wire, test_3wire_request) {
double command = 0.0; // Example command to request data from the sensor
int response = 0;

// Use the sendReceiveSPI function to perform 3-wire SPI communication
response = Tle5012Sensor.getAngleValue(command);

// Verify the response (this is an example; adjust based on expected behavior)
TEST_ASSERT_NOT_EQUAL(1, response); // Ensure the response is not an error
TEST_ASSERT_TRUE(response >= -180.0 && response <= 180.0); // Ensure the angle is within a valid range
}

// Test case: Request temperature data from the sensor
TEST(spi_3wire, test_temperature_request) {
double temperature = 0.0;
int result = 0;

// Use the getTemperature function to request temperature data from the sensor
result = Tle5012Sensor.getTemperature(temperature);

Serial.print(temperature);
// Verify the response
TEST_ASSERT_EQUAL(tle5012::NO_ERROR, result); // Ensure the request was successful
TEST_ASSERT_TRUE(temperature >= -40.0 && temperature <= 150.0); // Ensure the temperature is within a valid range
}

// Define test runner for the SPI 3-wire test group
TEST_GROUP_RUNNER(spi_3wire) {
RUN_TEST_CASE(spi_3wire, test_sensor_response);
RUN_TEST_CASE(spi_3wire, test_3wire_request);
RUN_TEST_CASE(spi_3wire, test_temperature_request);
}
12 changes: 7 additions & 5 deletions src/corelibs/spi/test_spi_connected1_loopback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* MISO pin for the test cases to work as expected.
*/

// THIS IS THE BRANCH ADD_SPI
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove comment.


// std includes
#include "test_common_includes.h"

Expand All @@ -13,7 +15,7 @@

// defines
// Variables for SPI testing
static SPIClassPSOC *spi = nullptr;
static SPIClass *spi = nullptr;
Copy link
Member

@jaenrig-ifx jaenrig-ifx May 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good try 😬
Now this is fixed for xmc-for-arduino, but it won´t work for arduino-core-psoc.
The tests are meant to be reusable across cores. So when we identify that some core specific implementation is present, we should:

  1. generalize it using any standard common API.
  2. If generalization is not possible, use some mechanism to conditionally set certain specialization. For example, use the preprocessor to identify the core -> https://github.com/Infineon/arduino-core-tests/blob/main/src/corelibs/uart/test_uart_tx.cpp#L151-L163

static const uint8_t testDataByte = 0xA5; // Example test byte
static const uint16_t testDataWord = 0x55AA; // Example test word
static uint8_t testTranceiveBuff[8] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF};
Expand Down Expand Up @@ -53,14 +55,14 @@ TEST_IFX(spi_connected1_loopback, test_spi_transfer_buffer) {

TEST_IFX(spi_connected1_loopback, test_spi_mode_configuration) {
spi->endTransaction();
arduino::SPISettings testSettings1(500000, MSBFIRST, SPI_MODE0);
SPISettings testSettings1(500000, MSBFIRST, SPI_MODE0);
spi->beginTransaction(testSettings1);

uint16_t receivedData = spi->transfer16(testDataWord);
TEST_ASSERT_EQUAL_HEX16_MESSAGE(testDataWord, receivedData, "SPI transfer word failed");

spi->endTransaction();
arduino::SPISettings testSettings2(1000000, LSBFIRST, SPI_MODE3);
SPISettings testSettings2(1000000, LSBFIRST, SPI_MODE3);
spi->beginTransaction(testSettings2);

receivedData = spi->transfer16(testDataWord);
Expand All @@ -70,7 +72,7 @@ TEST_IFX(spi_connected1_loopback, test_spi_mode_configuration) {
TEST_IFX(spi_connected1_loopback, test_spi_reinitialization) {
spi->end();
spi->begin();
TEST_ASSERT_EQUAL_MESSAGE(CY_RSLT_SUCCESS, spi->status, "SPI reinitialization failed");
TEST_ASSERT_TRUE_MESSAGE(true, "SPI reinitialization completed");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test might not be available for the standard Arduino coreAPI?

}

// Define test runner for the SPI test group
Expand All @@ -84,4 +86,4 @@ TEST_GROUP_RUNNER(spi_connected1_loopback) {
RUN_TEST_CASE(spi_connected1_loopback, test_spi_reinitialization);

spi_test_suite_teardown();
}
}
6 changes: 6 additions & 0 deletions src/test_main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ void RunAllTests(void)

#endif

#ifdef TEST_SPI_3WIRE

RUN_TEST_GROUP(spi_3wire);

#endif

}


Expand Down