Skip to content

Commit 429a72b

Browse files
authored
CI: Add peripheral manager test (#8811)
* CI: Add peripheral manager test * Add note * Update comment
1 parent 60395ed commit 429a72b

File tree

2 files changed

+323
-0
lines changed

2 files changed

+323
-0
lines changed

Diff for: tests/periman/periman.ino

+297
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,297 @@
1+
/* Peripheral Manager test
2+
*
3+
* This test is using Serial to check if the peripheral manager is able to
4+
* attach and detach peripherals correctly on shared pins.
5+
* Make sure that the peripheral names contain only letters, numbers and underscores.
6+
*
7+
* This test skips the following peripherals:
8+
* - USB: USB is not able to be detached
9+
* - SDMMC: SDMMC requires a card to be mounted before the pins are attached
10+
* - ETH: ETH requires a ethernet port to be connected before the pins are attached
11+
*/
12+
13+
#if SOC_I2S_SUPPORTED
14+
#include "ESP_I2S.h"
15+
#endif
16+
17+
#if SOC_I2C_SUPPORTED
18+
#include "Wire.h"
19+
#endif
20+
21+
#if SOC_GPSPI_SUPPORTED
22+
#include "SPI.h"
23+
#endif
24+
25+
/* Definitions */
26+
27+
#define UART1_RX_DEFAULT 4
28+
#define UART1_TX_DEFAULT 5
29+
30+
#define ADC1_DEFAULT A4
31+
32+
#if CONFIG_IDF_TARGET_ESP32
33+
# define ADC2_DEFAULT A5
34+
#else
35+
# define ADC2_DEFAULT A3
36+
#endif
37+
38+
#if CONFIG_IDF_TARGET_ESP32
39+
# define TOUCH1_DEFAULT T0
40+
# define TOUCH2_DEFAULT T2
41+
#else
42+
# define TOUCH1_DEFAULT T4
43+
# define TOUCH2_DEFAULT T5
44+
#endif
45+
46+
/* Global variables */
47+
48+
bool test_executed = false;
49+
String current_test;
50+
int8_t uart1_rx_pin;
51+
int8_t uart1_tx_pin;
52+
53+
/* Callback functions */
54+
55+
void onReceive_cb(void) {
56+
// This is a callback function that will be activated on UART RX events
57+
size_t available = Serial1.available();
58+
while (available --) {
59+
Serial.print((char)Serial1.read());
60+
}
61+
}
62+
63+
// This function is called by before each test is run
64+
void setup_test(String test_name, int8_t rx_pin = UART1_RX_DEFAULT, int8_t tx_pin = UART1_TX_DEFAULT) {
65+
log_v("Setting up %s test", test_name.c_str());
66+
67+
current_test = test_name;
68+
uart1_rx_pin = rx_pin;
69+
uart1_tx_pin = tx_pin;
70+
test_executed = false;
71+
72+
pinMode(uart1_rx_pin, INPUT_PULLUP);
73+
pinMode(uart1_tx_pin, OUTPUT);
74+
Serial1.setPins(uart1_rx_pin, uart1_tx_pin);
75+
uart_internal_loopback(1, uart1_rx_pin);
76+
delay(100);
77+
log_v("Running %s test", test_name.c_str());
78+
}
79+
80+
// This function is called after each test is run
81+
void teardown_test(void) {
82+
log_v("Tearing down %s test", current_test.c_str());
83+
if (test_executed) {
84+
pinMode(uart1_rx_pin, INPUT_PULLUP);
85+
pinMode(uart1_tx_pin, OUTPUT);
86+
Serial1.print(current_test);
87+
Serial1.println(" test: This should not be printed");
88+
Serial1.flush();
89+
90+
Serial1.setPins(uart1_rx_pin, uart1_tx_pin);
91+
uart_internal_loopback(1, uart1_rx_pin);
92+
delay(100);
93+
}
94+
95+
Serial1.print(current_test);
96+
Serial1.println(" test: This should be printed");
97+
Serial1.flush();
98+
99+
log_v("Finished %s test", current_test.c_str());
100+
}
101+
102+
/* Test functions */
103+
/* These functions must call "setup_test" and "teardown_test" and set "test_executed" to true
104+
* if the test is executed
105+
*/
106+
107+
void gpio_test(void) {
108+
setup_test("GPIO");
109+
test_executed = true;
110+
pinMode(uart1_rx_pin, INPUT);
111+
pinMode(uart1_tx_pin, OUTPUT);
112+
digitalRead(uart1_rx_pin);
113+
digitalWrite(uart1_tx_pin, HIGH);
114+
teardown_test();
115+
}
116+
117+
void sigmadelta_test(void) {
118+
setup_test("SigmaDelta");
119+
#if SOC_SDM_SUPPORTED
120+
test_executed = true;
121+
if (!sigmaDeltaAttach(uart1_rx_pin, 312500)) {
122+
Serial.println("SigmaDelta init failed");
123+
}
124+
if (!sigmaDeltaAttach(uart1_tx_pin, 312500)) {
125+
Serial.println("SigmaDelta init failed");
126+
}
127+
#endif
128+
teardown_test();
129+
}
130+
131+
void adc_oneshot_test(void) {
132+
#if !SOC_ADC_SUPPORTED
133+
setup_test("ADC_Oneshot");
134+
#else
135+
setup_test("ADC_Oneshot", ADC1_DEFAULT, ADC2_DEFAULT);
136+
test_executed = true;
137+
analogReadResolution(12);
138+
pinMode(ADC1_DEFAULT, INPUT);
139+
pinMode(ADC2_DEFAULT, INPUT);
140+
analogRead(ADC1_DEFAULT);
141+
analogRead(ADC2_DEFAULT);
142+
#endif
143+
teardown_test();
144+
}
145+
146+
#if SOC_ADC_SUPPORTED
147+
volatile bool adc_coversion_done = false;
148+
void ARDUINO_ISR_ATTR adcComplete() {
149+
adc_coversion_done = true;
150+
}
151+
#endif
152+
153+
void adc_continuous_test(void) {
154+
#if !SOC_ADC_SUPPORTED
155+
setup_test("ADC_Continuous");
156+
#else
157+
setup_test("ADC_Continuous", ADC1_DEFAULT, ADC2_DEFAULT);
158+
test_executed = true;
159+
uint8_t adc_pins[] = {ADC1_DEFAULT, ADC2_DEFAULT};
160+
uint8_t adc_pins_count = 2;
161+
adc_continuos_data_t * result = NULL;
162+
163+
analogContinuousSetWidth(12);
164+
analogContinuousSetAtten(ADC_11db);
165+
166+
analogContinuous(adc_pins, adc_pins_count, 6, 20000, &adcComplete);
167+
analogContinuousStart();
168+
169+
while (adc_coversion_done == false) {
170+
delay(1);
171+
}
172+
173+
if (!analogContinuousRead(&result, 0)) {
174+
Serial.println("ADC continuous read failed");
175+
}
176+
177+
analogContinuousStop();
178+
#endif
179+
teardown_test();
180+
}
181+
182+
void dac_test(void) {
183+
#if !SOC_DAC_SUPPORTED
184+
setup_test("DAC");
185+
#else
186+
setup_test("DAC", DAC1, DAC2);
187+
test_executed = true;
188+
dacWrite(DAC1, 255);
189+
dacWrite(DAC2, 255);
190+
#endif
191+
teardown_test();
192+
}
193+
194+
void ledc_test(void) {
195+
setup_test("LEDC");
196+
#if SOC_LEDC_SUPPORTED
197+
test_executed = true;
198+
if (!ledcAttach(uart1_rx_pin, 5000, 12)) {
199+
Serial.println("LEDC init failed");
200+
}
201+
if (!ledcAttach(uart1_tx_pin, 5000, 12)) {
202+
Serial.println("LEDC init failed");
203+
}
204+
#endif
205+
teardown_test();
206+
}
207+
208+
void rmt_test(void) {
209+
setup_test("RMT");
210+
#if SOC_RMT_SUPPORTED
211+
test_executed = true;
212+
if (!rmtInit(uart1_rx_pin, RMT_TX_MODE, RMT_MEM_NUM_BLOCKS_1, 10000000)) {
213+
Serial.println("RMT init failed");
214+
}
215+
if (!rmtInit(uart1_tx_pin, RMT_RX_MODE, RMT_MEM_NUM_BLOCKS_1, 10000000)) {
216+
Serial.println("RMT init failed");
217+
}
218+
#endif
219+
teardown_test();
220+
}
221+
222+
void i2s_test(void) {
223+
setup_test("I2S");
224+
#if SOC_I2S_SUPPORTED
225+
test_executed = true;
226+
I2SClass i2s;
227+
228+
i2s.setPins(uart1_rx_pin, uart1_tx_pin, -1);
229+
i2s.setTimeout(1000);
230+
if (!i2s.begin(I2S_MODE_STD, 16000, I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO)) {
231+
Serial.println("I2S init failed");
232+
}
233+
#endif
234+
teardown_test();
235+
}
236+
237+
void i2c_test(void) {
238+
setup_test("I2C");
239+
#if SOC_I2C_SUPPORTED
240+
test_executed = true;
241+
if (!Wire.begin(uart1_rx_pin, uart1_tx_pin)) {
242+
Serial.println("I2C init failed");
243+
}
244+
#endif
245+
teardown_test();
246+
}
247+
248+
void spi_test(void) {
249+
setup_test("SPI");
250+
#if SOC_GPSPI_SUPPORTED
251+
test_executed = true;
252+
SPI.begin(uart1_rx_pin, uart1_tx_pin, -1, -1);
253+
#endif
254+
teardown_test();
255+
}
256+
257+
void touch_test(void) {
258+
#if !SOC_TOUCH_SENSOR_SUPPORTED
259+
setup_test("Touch");
260+
#else
261+
setup_test("Touch", TOUCH1_DEFAULT, TOUCH2_DEFAULT);
262+
test_executed = true;
263+
touchRead(TOUCH1_DEFAULT);
264+
touchRead(TOUCH2_DEFAULT);
265+
#endif
266+
teardown_test();
267+
}
268+
269+
/* Main functions */
270+
271+
void setup() {
272+
Serial.begin(115200);
273+
while(!Serial) { delay(10); }
274+
275+
Serial1.setPins(UART1_RX_DEFAULT, UART1_TX_DEFAULT);
276+
Serial1.begin(115200);
277+
while(!Serial1) { delay(10); }
278+
Serial1.onReceive(onReceive_cb);
279+
uart_internal_loopback(1, uart1_rx_pin);
280+
281+
gpio_test();
282+
sigmadelta_test();
283+
ledc_test();
284+
rmt_test();
285+
i2s_test();
286+
i2c_test();
287+
spi_test();
288+
adc_oneshot_test();
289+
adc_continuous_test();
290+
dac_test();
291+
touch_test();
292+
293+
// Print to Serial1 to avoid buffering issues
294+
Serial1.println("Peripheral Manager test done");
295+
}
296+
297+
void loop() {}

Diff for: tests/periman/test_periman.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def test_periman(dut):
2+
peripherals = ["GPIO", "SigmaDelta", "LEDC", "RMT", "I2S", "I2C", "SPI",
3+
"ADC_Oneshot", "ADC_Continuous", "DAC", "Touch"]
4+
5+
pattern = rb"(?:\b\w+\b test: This should(?: not)? be printed|Peripheral Manager test done)"
6+
7+
while True:
8+
try:
9+
res = dut.expect(pattern, timeout=10)
10+
except:
11+
assert False, f"Could not detect end of test"
12+
13+
console_output = res.group(0).decode("utf-8")
14+
peripheral = console_output.split()[0]
15+
16+
if "Peripheral Manager test done" in console_output:
17+
break
18+
19+
if peripheral in peripherals:
20+
if "not" in console_output:
21+
assert False, f"Peripheral {peripheral} printed when it should not"
22+
peripherals.remove(peripheral)
23+
else:
24+
assert False, f"Unknown peripheral: {peripheral}"
25+
26+
assert peripherals == [], f"Missing peripherals output: {peripherals}"

0 commit comments

Comments
 (0)