2323#include " unity.h"
2424#include " utest.h"
2525#include " ci_test_common.h"
26- // #include "rtos.h"
26+
27+ #include < atomic>
28+ #include < cinttypes>
2729
2830using namespace utest ::v1;
2931
30- volatile bool result = false ;
32+ std::atomic< uint32_t > callbackCounts ;
3133
3234// Callback for all InterruptInput functions
3335void cbfn (void )
3436{
35- result = true ;
37+ ++callbackCounts ;
3638}
3739
3840// Template to check Falling edge and Rising edge interrupts.
3941template <PinName int_pin, PinName dout_pin>
4042void InterruptInTest ()
4143{
42- result = false ;
44+ callbackCounts = 0 ;
4345 InterruptIn intin (int_pin);
44- DigitalOut dout (dout_pin);
46+ DigitalOut dout (dout_pin, 0 );
4547
4648 // Test Rising Edge InterruptIn
4749 DEBUG_PRINTF (" ***** Rising Edge Test \n " );
48- dout = 0 ;
49- result = false ;
5050 intin.rise (cbfn);
5151 dout = 1 ;
5252 wait_us (GPIO_PROPAGATION_TIME);
53- DEBUG_PRINTF (" Value of result is : %d\n " ,result);
54- TEST_ASSERT_MESSAGE (result," cbfn was not triggered on rising edge of pin" );
55-
56- result = false ;
53+ DEBUG_PRINTF (" Value of callbackCounts is: %" PRIu32 " \n " , callbackCounts.load ());
54+ TEST_ASSERT_MESSAGE (callbackCounts == 1 , " cbfn was not triggered on rising edge of pin" );
5755
5856 // Check that callback is not triggered again
5957 for (size_t checkCounter = 0 ; checkCounter < 10 ; ++checkCounter)
6058 {
61- TEST_ASSERT_MESSAGE (!result , " Interrupt was triggered again!" )
59+ TEST_ASSERT_MESSAGE (callbackCounts == 1 , " Interrupt was triggered again!" )
6260 }
6361
62+ dout = 0 ;
63+ wait_us (GPIO_PROPAGATION_TIME);
64+ TEST_ASSERT_MESSAGE (callbackCounts == 1 , " cbfn was triggered on falling edge of pin" );
65+
66+ // Clear rising edge interrupt
67+ intin.rise (nullptr );
68+
6469 // Test Falling Edge InterruptIn
6570 DEBUG_PRINTF (" ***** Falling Edge Test \n " );
6671 dout = 1 ;
67- result = false ;
72+ callbackCounts = 0 ;
6873 intin.fall (cbfn);
6974 dout = 0 ;
7075 wait_us (GPIO_PROPAGATION_TIME);
71- DEBUG_PRINTF (" Value of result is : %d\n " ,result);
72- TEST_ASSERT_MESSAGE (result," cbfn was not triggered on falling edge of pin" );
73-
74- result = false ;
76+ DEBUG_PRINTF (" Value of callbackCounts is: %" PRIu32 " \n " , callbackCounts.load ());
77+ TEST_ASSERT_MESSAGE (callbackCounts == 1 , " cbfn was not triggered on falling edge of pin" );
7578
7679 // Check that callback is not triggered again
7780 for (size_t checkCounter = 0 ; checkCounter < 10 ; ++checkCounter)
7881 {
79- TEST_ASSERT_MESSAGE (!result , " Interrupt was triggered again!" )
82+ TEST_ASSERT_MESSAGE (callbackCounts == 1 , " Interrupt was triggered again!" )
8083 }
84+
85+ dout = 1 ;
86+ wait_us (GPIO_PROPAGATION_TIME);
87+ TEST_ASSERT_MESSAGE (callbackCounts == 1 , " cbfn was triggered on rising edge of pin" );
88+
89+ // Clear falling edge interrupt
90+ intin.fall (nullptr );
91+ }
92+
93+ // Test that a rising and falling edge callback can both be set at the same time,
94+ // and the correct one will be executed depending on the edge type
95+ template <PinName int_pin, PinName dout_pin>
96+ void InterruptInBothEdgesTest ()
97+ {
98+ std::atomic<bool > gotRisingEdge = false ;
99+ std::atomic<bool > gotFallingEdge = false ;
100+
101+ auto risingEdgeLambda = [&](){ gotRisingEdge = true ; };
102+ auto fallingEdgeLambda = [&](){ gotFallingEdge = true ; };
103+
104+ InterruptIn intin (int_pin);
105+ DigitalOut dout (dout_pin, 0 );
106+
107+ intin.fall (fallingEdgeLambda);
108+ intin.rise (risingEdgeLambda);
109+
110+ DEBUG_PRINTF (" ***** Rising Edge Test \n " );
111+ dout = 1 ;
112+ wait_us (GPIO_PROPAGATION_TIME);
113+
114+ TEST_ASSERT_MESSAGE (gotRisingEdge, " rising edge callback was not triggered on rising edge of pin" );
115+ TEST_ASSERT_MESSAGE (!gotFallingEdge, " falling edge callback was triggered on rising edge of pin!" );
116+
117+ gotRisingEdge = false ;
118+
119+ DEBUG_PRINTF (" ***** Falling Edge Test \n " );
120+ dout = 0 ;
121+ wait_us (GPIO_PROPAGATION_TIME);
122+
123+ TEST_ASSERT_MESSAGE (gotFallingEdge, " falling edge callback was not triggered on falling edge of pin" );
124+ TEST_ASSERT_MESSAGE (!gotRisingEdge, " rising edge callback was triggered on falling edge of pin!" );
125+ }
126+
127+ template <PinName int_pin, PinName dout_pin>
128+ void InterruptInReadValueTest ()
129+ {
130+ InterruptIn intin (int_pin);
131+ DigitalOut dout (dout_pin, 0 );
132+
133+ dout = 1 ;
134+ wait_us (GPIO_PROPAGATION_TIME);
135+ TEST_ASSERT_MESSAGE (intin.read (), " InterruptIn read failed with no callback!" );
136+
137+ dout = 0 ;
138+ wait_us (GPIO_PROPAGATION_TIME);
139+ TEST_ASSERT_MESSAGE (!intin.read (), " InterruptIn read failed with no callback!" );
140+
141+ intin.rise (cbfn);
142+
143+ dout = 1 ;
144+ wait_us (GPIO_PROPAGATION_TIME);
145+ TEST_ASSERT_MESSAGE (intin.read (), " InterruptIn read failed with callback!" );
146+
147+ dout = 0 ;
148+ wait_us (GPIO_PROPAGATION_TIME);
149+ TEST_ASSERT_MESSAGE (!intin.read (), " InterruptIn read failed with callback!" );
81150}
82151
83152utest::v1::status_t test_setup (const size_t number_of_cases)
@@ -102,12 +171,15 @@ utest::v1::status_t greentea_failure_handler(const Case *const source, const fai
102171
103172// Test cases
104173Case cases[] = {
105- Case (" Interrupt from GPOUT_2 -> GPIN_2" , InterruptInTest<PIN_GPIN_2,PIN_GPOUT_2>,greentea_failure_handler),
106- Case (" Interrupt from GPIN_2 -> GPOUT_2" , InterruptInTest<PIN_GPOUT_2,PIN_GPIN_2>,greentea_failure_handler),
107- Case (" Interrupt from GPOUT_1 -> GPIN_1" , InterruptInTest<PIN_GPIN_1,PIN_GPOUT_1_PWM>,greentea_failure_handler),
174+ // Case("Interrupt from GPOUT_2 -> GPIN_2", InterruptInTest<PIN_GPIN_2,PIN_GPOUT_2>,greentea_failure_handler),
175+ // Case("Interrupt from GPIN_2 -> GPOUT_2", InterruptInTest<PIN_GPOUT_2,PIN_GPIN_2>,greentea_failure_handler),
176+ // Case("Interrupt from GPOUT_1 -> GPIN_1", InterruptInTest<PIN_GPIN_1,PIN_GPOUT_1_PWM>,greentea_failure_handler),
108177 Case (" Interrupt from GPIN_1 -> GPOUT_1" , InterruptInTest<PIN_GPOUT_1_PWM,PIN_GPIN_1>,greentea_failure_handler),
109- Case (" Interrupt from GPOUT_0 -> GPIN_0" , InterruptInTest<PIN_GPIN_0,PIN_GPOUT_0>,greentea_failure_handler),
110- Case (" Interrupt from GPIN_0 -> GPOUT_0" , InterruptInTest<PIN_GPOUT_0,PIN_GPIN_0>,greentea_failure_handler),
178+ // Case("Interrupt from GPOUT_0 -> GPIN_0", InterruptInTest<PIN_GPIN_0,PIN_GPOUT_0>,greentea_failure_handler),
179+ // Case("Interrupt from GPIN_0 -> GPOUT_0", InterruptInTest<PIN_GPOUT_0,PIN_GPIN_0>,greentea_failure_handler),
180+
181+ Case (" InterruptIn Both Edges" , InterruptInBothEdgesTest<PIN_GPIN_0,PIN_GPOUT_0>, greentea_failure_handler),
182+ Case (" InterruptIn Read Value" , InterruptInReadValueTest<PIN_GPIN_0,PIN_GPOUT_0>, greentea_failure_handler),
111183};
112184
113185Specification specification (test_setup, cases);
0 commit comments