Skip to content

Commit 4551b70

Browse files
committed
fix(tests): Fix GPIO and PeriMan tests
1 parent ff3dd5b commit 4551b70

File tree

4 files changed

+50
-22
lines changed

4 files changed

+50
-22
lines changed

libraries/ESP_I2S/src/ESP_I2S.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ static esp_err_t i2s_channel_read_16_stereo_to_mono(i2s_chan_handle_t handle, ch
192192

193193
I2SClass::I2SClass() {
194194
last_error = ESP_OK;
195+
_mode = I2S_MODE_MAX; // Initialize to invalid mode to indicate I2S not started
195196

196197
tx_chan = NULL;
197198
tx_sample_rate = 0;
@@ -239,7 +240,9 @@ I2SClass::~I2SClass() {
239240

240241
bool I2SClass::i2sDetachBus(void *bus_pointer) {
241242
I2SClass *bus = (I2SClass *)bus_pointer;
242-
if (bus->tx_chan != NULL || bus->tx_chan != NULL) {
243+
// Only call end() if I2S has been initialized (begin() was called)
244+
// _mode is set to I2S_MODE_MAX in constructor and to a valid mode in begin()
245+
if (bus->_mode < I2S_MODE_MAX) {
243246
bus->end();
244247
}
245248
return true;
@@ -703,6 +706,16 @@ bool I2SClass::begin(i2s_mode_t mode, uint32_t rate, i2s_data_bit_width_t bits_c
703706
}
704707

705708
bool I2SClass::end() {
709+
// Check if already ended to prevent recursion
710+
if (_mode >= I2S_MODE_MAX) {
711+
return true;
712+
}
713+
714+
// Save mode and reset it BEFORE clearing pins to prevent recursive calls
715+
// When perimanClearPinBus() is called, it may trigger i2sDetachBus() again
716+
i2s_mode_t mode = _mode;
717+
_mode = I2S_MODE_MAX;
718+
706719
if (tx_chan != NULL) {
707720
I2S_ERROR_CHECK_RETURN_FALSE(i2s_channel_disable(tx_chan));
708721
I2S_ERROR_CHECK_RETURN_FALSE(i2s_del_channel(tx_chan));
@@ -720,7 +733,7 @@ bool I2SClass::end() {
720733
}
721734

722735
//Peripheral manager deinit used pins
723-
switch (_mode) {
736+
switch (mode) {
724737
case I2S_MODE_STD:
725738
#if SOC_I2S_SUPPORTS_TDM
726739
case I2S_MODE_TDM:

tests/validation/gpio/gpio.ino

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
volatile int interruptCounter = 0;
1515
volatile bool interruptFlag = false;
16-
volatile unsigned long lastInterruptTime = 0;
1716

1817
// Variables for interrupt with argument test
1918
volatile int argInterruptCounter = 0;
@@ -34,7 +33,6 @@ void waitForSyncAck(const String &token = "OK") {
3433
void setUp(void) {
3534
interruptCounter = 0;
3635
interruptFlag = false;
37-
lastInterruptTime = 0;
3836
argInterruptCounter = 0;
3937
argInterruptFlag = false;
4038
receivedArg = 0;
@@ -43,22 +41,14 @@ void setUp(void) {
4341
void tearDown(void) {}
4442

4543
void IRAM_ATTR buttonISR() {
46-
unsigned long currentTime = millis();
47-
if (currentTime - lastInterruptTime > 50) {
48-
interruptCounter = interruptCounter + 1;
49-
interruptFlag = true;
50-
lastInterruptTime = currentTime;
51-
}
44+
interruptCounter = interruptCounter + 1;
45+
interruptFlag = true;
5246
}
5347

5448
void IRAM_ATTR buttonISRWithArg(void *arg) {
55-
unsigned long currentTime = millis();
56-
if (currentTime - lastInterruptTime > 50) {
57-
argInterruptCounter = argInterruptCounter + 1;
58-
argInterruptFlag = true;
59-
receivedArg = *(int *)arg;
60-
lastInterruptTime = currentTime;
61-
}
49+
argInterruptCounter = argInterruptCounter + 1;
50+
argInterruptFlag = true;
51+
receivedArg = *(int *)arg;
6252
}
6353

6454
void test_read_basic(void) {

tests/validation/i2c_master/i2c_master.ino

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void ds1307_get_time(uint8_t *sec, uint8_t *min, uint8_t *hour, uint8_t *day, ui
108108
void ds1307_set_time(uint8_t sec, uint8_t min, uint8_t hour, uint8_t day, uint8_t month, uint16_t year) {
109109
Wire.beginTransmission(DS1307_ADDR);
110110
Wire.write(0x00);
111-
Wire.write(DEC2BCD(sec));
111+
Wire.write(DEC2BCD(sec) | 0x80); //Set halt bit to stop clock
112112
Wire.write(DEC2BCD(min));
113113
Wire.write(DEC2BCD(hour));
114114
Wire.write(DEC2BCD(0)); //Ignore day of week
@@ -212,6 +212,22 @@ void change_clock() {
212212
TEST_ASSERT_EQUAL(start_day, read_day);
213213
TEST_ASSERT_EQUAL(start_month, read_month);
214214
TEST_ASSERT_EQUAL(start_year, read_year);
215+
216+
//Run clock for 5 seconds to check that we can write
217+
ds1307_start();
218+
delay(5000);
219+
ds1307_stop();
220+
221+
//Get time
222+
ds1307_get_time(&read_sec, &read_min, &read_hour, &read_day, &read_month, &read_year);
223+
224+
//Check time
225+
TEST_ASSERT_NOT_EQUAL(start_sec, read_sec); //Seconds should have changed
226+
TEST_ASSERT_EQUAL(start_min, read_min);
227+
TEST_ASSERT_EQUAL(start_hour, read_hour);
228+
TEST_ASSERT_EQUAL(start_day, read_day);
229+
TEST_ASSERT_EQUAL(start_month, read_month);
230+
TEST_ASSERT_EQUAL(start_year, read_year);
215231
}
216232

217233
void swap_pins() {

tests/validation/periman/periman.ino

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
* - ETH: ETH requires a ethernet port to be connected before the pins are attached
1111
*/
1212

13+
#include <Arduino.h>
14+
1315
#if SOC_I2S_SUPPORTED
1416
#include "ESP_I2S.h"
1517
#endif
@@ -71,7 +73,10 @@ void setup_test(String test_name, int8_t rx_pin = UART1_RX_DEFAULT, int8_t tx_pi
7173

7274
pinMode(uart1_rx_pin, INPUT_PULLUP);
7375
pinMode(uart1_tx_pin, OUTPUT);
76+
// Ensure Serial1 is initialized and callback is set (in case it was terminated previously)
7477
Serial1.setPins(uart1_rx_pin, uart1_tx_pin);
78+
Serial1.begin(115200);
79+
Serial1.onReceive(onReceive_cb);
7580
uart_internal_loopback(1, uart1_rx_pin);
7681
delay(100);
7782
log_v("Running %s test", test_name.c_str());
@@ -86,12 +91,16 @@ void teardown_test(void) {
8691
Serial1.print(current_test);
8792
Serial1.println(" test: This should not be printed");
8893
Serial1.flush();
89-
90-
Serial1.setPins(uart1_rx_pin, uart1_tx_pin);
91-
uart_internal_loopback(1, uart1_rx_pin);
92-
delay(100);
9394
}
9495

96+
// Even if test didn't execute, ensure Serial1 is initialized
97+
// (in case it was terminated by a previous test or setup issue)
98+
Serial1.setPins(uart1_rx_pin, uart1_tx_pin);
99+
Serial1.begin(115200);
100+
Serial1.onReceive(onReceive_cb);
101+
uart_internal_loopback(1, uart1_rx_pin);
102+
delay(100);
103+
95104
Serial1.print(current_test);
96105
Serial1.println(" test: This should be printed");
97106
Serial1.flush();

0 commit comments

Comments
 (0)