Skip to content
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

tests(cpu): Add deep sleep and misc tests for CPU #10112

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions tests/validation/cpu_misc/ci.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"platforms": {
"qemu": false
}
}
35 changes: 35 additions & 0 deletions tests/validation/cpu_misc/cpu_misc.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* CPU test
*
* Tests miscellaneous CPU functions like changing the CPU frequency, checking the CPU frequency,
* reading the CPU temperature, etc.
*/

#include <Arduino.h>
#include <unity.h>

/* Utility global variables */

/* Test functions */

void get_cpu_temperature() {
#if SOC_TEMP_SENSOR_SUPPORTED
float temp = temperatureRead();
log_d("CPU temperature: %f", temp);
TEST_ASSERT_FLOAT_IS_NOT_NAN(temp);
TEST_ASSERT_FLOAT_WITHIN(40.0, 30.0, temp);
#else
log_d("CPU temperature not supported");
#endif
}

/* Main functions */

void setup() {
UNITY_BEGIN();

RUN_TEST(get_cpu_temperature);

UNITY_END();
}

void loop() {}
2 changes: 2 additions & 0 deletions tests/validation/cpu_misc/test_cpu_misc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def test_cpu_misc(dut):
dut.expect_unity_test_output(timeout=120)
6 changes: 6 additions & 0 deletions tests/validation/sleep/ci.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"platforms": {
"qemu": false,
"wokwi": false
}
}
120 changes: 120 additions & 0 deletions tests/validation/sleep/sleep.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* This sketch tests the deep sleep functionality of the ESP32
*/

#include <Arduino.h>
#include "driver/rtc_io.h"
#include "driver/uart.h"

// Timer
#define uS_TO_S_FACTOR 1000000ULL /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 1 /* Time ESP32 will go to sleep (in seconds) */

// Touchpad
#if CONFIG_IDF_TARGET_ESP32
#define THRESHOLD 1000 /* Greater the value, more the sensitivity */
#else
#define THRESHOLD 0 /* Lower the value, more the sensitivity */
#endif

#define TARGET_FREQ CONFIG_XTAL_FREQ

RTC_DATA_ATTR int boot_count = 0;
uint32_t orig_freq = 0;

void print_wakeup_reason() {
esp_sleep_wakeup_cause_t wakeup_reason;

wakeup_reason = esp_sleep_get_wakeup_cause();

Serial.print("Wakeup reason: ");

switch (wakeup_reason) {
case ESP_SLEEP_WAKEUP_EXT0: Serial.println("rtc_io"); break;
case ESP_SLEEP_WAKEUP_EXT1: Serial.println("rtc_cntl"); break;
case ESP_SLEEP_WAKEUP_TIMER: Serial.println("timer"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD: Serial.println("touchpad"); break;
case ESP_SLEEP_WAKEUP_ULP: Serial.println("ulp"); break;
case ESP_SLEEP_WAKEUP_GPIO: Serial.println("gpio"); break;
case ESP_SLEEP_WAKEUP_UART: Serial.println("uart"); break;
default: Serial.println("power_up"); break;
}
}

void setup_timer() {
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
}

void setup_touchpad() {
#if SOC_TOUCH_SENSOR_SUPPORTED
touchSleepWakeUpEnable(T1, THRESHOLD);
#endif
}

void setup_uart() {
uart_set_wakeup_threshold(UART_NUM_0, 9); // wake up with "aaa" string (9 positive edges)
esp_sleep_enable_uart_wakeup(UART_NUM_0);
}

void setup() {
Serial.begin(115200);
while (!Serial) {
delay(10);
}

orig_freq = getCpuFrequencyMhz();

//Increment boot number and print it every reboot
boot_count++;
Serial.println("Boot number: " + String(boot_count));

//Print the wakeup reason for ESP32
print_wakeup_reason();
Serial.flush();
}

void loop() {
// Disable all configured wakeup sources
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);

if(Serial.available() > 0) {
String command = Serial.readString();
command.trim();

if (command == "timer_deep") {
// Test timer wakeup from deep sleep
setup_timer();
esp_deep_sleep_start();
Serial.println("FAIL");
while(1);
} else if (command == "touchpad_deep") {
// Test touchpad wakeup from deep sleep
setup_touchpad();
esp_deep_sleep_start();
Serial.println("FAIL");
while(1);
} else if (command == "timer_light") {
// Test timer wakeup from light sleep
setup_timer();
} else if (command == "timer_freq_light") {
// Test timer wakeup from light sleep while changing CPU frequency
setCpuFrequencyMhz(TARGET_FREQ);
setup_timer();
} else if (command == "touchpad_light") {
// Test touchpad wakeup from light sleep
setup_touchpad();
} else if (command == "uart_light") {
// Test external wakeup from light sleep using UART
setup_uart();
} else {
Serial.println("FAIL");
while(1);
}

esp_light_sleep_start();
Serial.println("Woke up from light sleep");
print_wakeup_reason();
Serial.flush();
setCpuFrequencyMhz(orig_freq);
}
}
41 changes: 41 additions & 0 deletions tests/validation/sleep/test_sleep.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import logging
import time

capabilities = {
"timer": ["esp32", "esp32s2", "esp32s3", "esp32c3", "esp32c6", "esp32h2"],
"touchpad": ["esp32", "esp32s2", "esp32s3"],
"uart": ["esp32", "esp32s2", "esp32s3", "esp32c3", "esp32c6", "esp32h2"]
}

def test_sleep(dut):
LOGGER = logging.getLogger(__name__)

# Deep Sleep
boot_count = 1
dut.expect_exact("Boot number: {}".format(boot_count))
dut.expect_exact("Wakeup reason: power_up")

for capability, devices in capabilities.items():
if dut.app.target in devices and capability != "uart":
LOGGER.info("Testing {} deep sleep capability".format(capability))
boot_count += 1
dut.write("{}_deep".format(capability))
dut.expect_exact("Boot number: {}".format(boot_count))
dut.expect_exact("Wakeup reason: {}".format(capability))

# Light Sleep
for capability, devices in capabilities.items():
if dut.app.target in devices:
LOGGER.info("Testing {} light sleep capability".format(capability))
dut.write("{}_light".format(capability))
if capability == "uart":
time.sleep(5)
LOGGER.info("Sending 9 positive edges")
dut.write("aaa") # Send 9 positive edges
dut.expect_exact("Woke up from light sleep")
dut.expect_exact("Wakeup reason: {}".format(capability))
if capability == "timer":
LOGGER.info("Testing timer light sleep capability with low frequency")
dut.write("timer_freq_light")
dut.expect_exact("Woke up from light sleep")
dut.expect_exact("Wakeup reason: timer")
Loading