From 9c370209cda82efd3017cb86a9eb95f8fb7e874f Mon Sep 17 00:00:00 2001 From: Lucas Saavedra Vaz Date: Mon, 4 Dec 2023 10:45:37 -0300 Subject: [PATCH] CI: Add peripheral manager test --- tests/periman/periman.ino | 265 ++++++++++++++++++++++++++++++++++ tests/periman/test_periman.py | 13 ++ 2 files changed, 278 insertions(+) create mode 100644 tests/periman/periman.ino create mode 100644 tests/periman/test_periman.py diff --git a/tests/periman/periman.ino b/tests/periman/periman.ino new file mode 100644 index 00000000000..f36b14c5f35 --- /dev/null +++ b/tests/periman/periman.ino @@ -0,0 +1,265 @@ +/* Peripheral Manager test + * + * This test is using Serial to check if the peripheral manager is able to + * attach and detach peripherals correctly on shared pins. + * + * This test skips the following peripherals: + * - USB: USB is not able to be detached + * - SDMMC: SDMMC requires a card to be mounted before the pins are attached + */ + +#include + +#if SOC_I2S_SUPPORTED +#include "ESP_I2S.h" +#endif + +#if SOC_I2C_SUPPORTED +#include "Wire.h" +#endif + +#if SOC_GPSPI_SUPPORTED +#include "SPI.h" +#endif + +#include "ETH.h" + +/* Definitions */ + +#define UART1_RX_DEFAULT 4 +#define UART1_TX_DEFAULT 5 + +/* Global variables */ + +bool test_executed = false; +String last_test = ""; +int8_t uart1_rx_pin = UART1_RX_DEFAULT; +int8_t uart1_tx_pin = UART1_TX_DEFAULT; + +/* Callback functions */ + +void onReceive_cb(void) { + // This is a callback function that will be activated on UART RX events + size_t available = Serial1.available(); + while (available --) { + Serial.print((char)Serial1.read()); + } +} + +/* Unity functions */ + +// This function is automatically called by unity before each test is run +void setUp(void) { + log_v("Setting up next test"); + Serial1.end(); + Serial1.begin(115200, SERIAL_8N1, uart1_rx_pin, uart1_tx_pin); + uart_internal_loopback(1, uart1_rx_pin); + delay(100); + log_v("Running test"); +} + +// This function is automatically called by unity after each test is run +void tearDown(void) { + log_v("Tearing down last test"); + if (test_executed) { + Serial1.print(last_test); + Serial1.println(" test: This should not be printed"); + Serial1.flush(); + + Serial1.begin(115200, SERIAL_8N1, uart1_rx_pin, uart1_tx_pin); + uart_internal_loopback(1, uart1_rx_pin); + delay(100); + test_executed = false; + } + + Serial1.print(last_test); + Serial1.println(" test: This should be printed"); + Serial1.flush(); +} + +/* Test functions */ +/* These functions must only init the peripheral on the same pins and update "last_test" */ + +void gpio_test(void) { + last_test = "GPIO"; + test_executed = true; + pinMode(uart1_rx_pin, INPUT); + pinMode(uart1_tx_pin, OUTPUT); +} + +void sigmadelta_test(void) { + last_test = "SigmaDelta"; +#if SOC_SDM_SUPPORTED + test_executed = true; + if (!sigmaDeltaAttach(uart1_rx_pin, 312500)) { + TEST_FAIL_MESSAGE("SigmaDelta init failed"); + } + if (!sigmaDeltaAttach(uart1_tx_pin, 312500)) { + TEST_FAIL_MESSAGE("SigmaDelta init failed"); + } +#endif +} + +void adc_oneshot_test(void) { + last_test = "ADC Oneshot"; +#if SOC_ADC_SUPPORTED + test_executed = true; + analogReadResolution(12); + pinMode(A3, INPUT); + pinMode(A4, INPUT); + analogRead(A3); + analogRead(A4); +#endif +} + +#if SOC_ADC_SUPPORTED +volatile bool adc_coversion_done = false; +void ARDUINO_ISR_ATTR adcComplete() { + adc_coversion_done = true; +} +#endif + +void adc_continuous_test(void) { + last_test = "ADC Continuous"; +#if SOC_ADC_SUPPORTED + test_executed = true; + uint8_t adc_pins[] = {A3, A4}; + uint8_t adc_pins_count = 2; + adc_continuos_data_t * result = NULL; + + analogContinuousSetWidth(12); + analogContinuousSetAtten(ADC_11db); + + analogContinuous(adc_pins, adc_pins_count, 6, 20000, &adcComplete); + analogContinuousStart(); + + while (adc_coversion_done == false) { + delay(1); + } + + if (!analogContinuousRead(&result, 0)) { + TEST_FAIL_MESSAGE("ADC continuous read failed"); + } + + analogContinuousStop(); +#endif +} + +void dac_test(void) { + last_test = "DAC"; +#if SOC_DAC_SUPPORTED + test_executed = true; + dacWrite(DAC1, 255); + dacWrite(DAC2, 255); +#endif +} + +void ledc_test(void) { + last_test = "LEDC"; +#if SOC_LEDC_SUPPORTED + test_executed = true; + if (!ledcAttach(uart1_rx_pin, 5000, 12)) { + TEST_FAIL_MESSAGE("LEDC init failed"); + } + if (!ledcAttach(uart1_tx_pin, 5000, 12)) { + TEST_FAIL_MESSAGE("LEDC init failed"); + } +#endif +} + +void rmt_test(void) { + last_test = "RMT"; +#if SOC_RMT_SUPPORTED + test_executed = true; + if (!rmtInit(uart1_rx_pin, RMT_TX_MODE, RMT_MEM_NUM_BLOCKS_1, 10000000)) { + TEST_FAIL_MESSAGE("RMT init failed"); + } + if (!rmtInit(uart1_tx_pin, RMT_RX_MODE, RMT_MEM_NUM_BLOCKS_1, 10000000)) { + TEST_FAIL_MESSAGE("RMT init failed"); + } +#endif +} + +void i2s_test(void) { + last_test = "I2S"; +#if SOC_I2S_SUPPORTED + test_executed = true; + I2SClass i2s; + + i2s.setPins(uart1_rx_pin, uart1_tx_pin, -1); + i2s.setTimeout(1000); + if (!i2s.begin(I2S_MODE_STD, 16000, I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO)) { + TEST_FAIL_MESSAGE("I2S init failed"); + } +#endif +} + +void i2c_test(void) { + last_test = "I2C"; +#if SOC_I2C_SUPPORTED + test_executed = true; + if (!Wire.begin(uart1_rx_pin, uart1_tx_pin)) { + TEST_FAIL_MESSAGE("I2C init failed"); + } +#endif +} + +void spi_test(void) { + last_test = "SPI"; +#if SOC_GPSPI_SUPPORTED + test_executed = true; + SPI.begin(uart1_rx_pin, uart1_tx_pin, -1, -1); +#endif +} + +void touch_test(void) { + last_test = "Touch"; +#if SOC_TOUCH_SENSOR_SUPPORTED + test_executed = true; + touchRead(T1); + touchRead(T2); +#endif +} + +void eth_test(void) { + last_test = "ETH"; + test_executed = true; + ETH.begin(); +} + +/* Main functions */ + +void setup() { + Serial.begin(115200); + while(!Serial) { delay(10); } + + Serial1.begin(115200, SERIAL_8N1, uart1_rx_pin, uart1_tx_pin); + while(!Serial1) { delay(10); } + Serial1.onReceive(onReceive_cb); + uart_internal_loopback(1, uart1_rx_pin); + + UNITY_BEGIN(); + + RUN_TEST(gpio_test); + RUN_TEST(sigmadelta_test); + RUN_TEST(ledc_test); + RUN_TEST(rmt_test); + RUN_TEST(i2s_test); + RUN_TEST(i2c_test); + RUN_TEST(spi_test); + uart1_tx_pin = A3; + uart1_rx_pin = A4; + RUN_TEST(adc_oneshot_test); + RUN_TEST(adc_continuous_test); + uart1_tx_pin = DAC1; + uart1_rx_pin = DAC2; + RUN_TEST(dac_test); + uart1_tx_pin = T1; + uart1_rx_pin = T2; + RUN_TEST(touch_test); + RUN_TEST(eth_test); + + UNITY_END(); +} + +void loop() {} diff --git a/tests/periman/test_periman.py b/tests/periman/test_periman.py new file mode 100644 index 00000000000..1a92c61ea9f --- /dev/null +++ b/tests/periman/test_periman.py @@ -0,0 +1,13 @@ +def test_periman(dut): + dut.expect("GPIO test: This should be printed") + dut.expect("SigmaDelta test: This should be printed") + dut.expect("LEDC test: This should be printed") + dut.expect("RMT test: This should be printed") + dut.expect("I2S test: This should be printed") + dut.expect("I2C test: This should be printed") + dut.expect("SPI test: This should be printed") + dut.expect("ADC Oneshot test: This should be printed") + dut.expect("ADC Continuous test: This should be printed") + dut.expect("DAC test: This should be printed") + dut.expect("Touch test: This should be printed") + dut.expect("ETH test: This should be printed")