@@ -37,6 +37,12 @@ const uint8_t INVALID_PIN = 255;
37
37
TEST_GROUP (digitalio_single);
38
38
TEST_GROUP (digitalio_single_internal);
39
39
40
+ volatile bool interrupt_triggered = false ;
41
+
42
+ void test_pin_interrupt_handler () {
43
+ interrupt_triggered = true ;
44
+ }
45
+
40
46
/* *
41
47
* @brief Setup method called by Unity before every test in this test group.
42
48
*/
@@ -62,6 +68,7 @@ TEST_IFX(digitalio_single_internal, test_digitalio_read_write_input_normal)
62
68
63
69
digitalWrite (TEST_PIN_DIGITAL_IO_OUTPUT, HIGH);
64
70
TEST_ASSERT_EQUAL_MESSAGE (HIGH, digitalRead (TEST_PIN_DIGITAL_IO_INPUT), " Input Pin should be set to HIGH" );
71
+
65
72
digitalWrite (TEST_PIN_DIGITAL_IO_OUTPUT, LOW);
66
73
TEST_ASSERT_EQUAL_MESSAGE (LOW, digitalRead (TEST_PIN_DIGITAL_IO_INPUT), " Input Pin should be set to LOW" );
67
74
}
@@ -77,11 +84,11 @@ TEST_IFX(digitalio_single_internal, test_digitalio_read_write_input_pullup)
77
84
pinMode (TEST_PIN_DIGITAL_IO_OUTPUT, OUTPUT_OPENDRAIN);
78
85
pinMode (TEST_PIN_DIGITAL_IO_INPUT, INPUT_PULLUP);
79
86
80
- digitalWrite (TEST_PIN_DIGITAL_IO_OUTPUT, HIGH); // set output pin to HIGH ie, floating state
81
- TEST_ASSERT_EQUAL_MESSAGE (HIGH, digitalRead (TEST_PIN_DIGITAL_IO_INPUT), " Input Pin should be set to HIGH initially" );
82
-
83
87
digitalWrite (TEST_PIN_DIGITAL_IO_OUTPUT, LOW);
84
88
TEST_ASSERT_EQUAL_MESSAGE (LOW, digitalRead (TEST_PIN_DIGITAL_IO_INPUT), " Input Pin should be set to LOW" );
89
+
90
+ digitalWrite (TEST_PIN_DIGITAL_IO_OUTPUT, HIGH); // set output pin to HIGH ie, floating state
91
+ TEST_ASSERT_EQUAL_MESSAGE (HIGH, digitalRead (TEST_PIN_DIGITAL_IO_INPUT), " Input Pin should be set to HIGH when output is floating and input is pullup" );
85
92
}
86
93
87
94
/* *
@@ -97,8 +104,8 @@ TEST_IFX(digitalio_single_internal, test_digitalio_read_write_input_pulldown)
97
104
98
105
TEST_ASSERT_EQUAL_MESSAGE (LOW, digitalRead (TEST_PIN_DIGITAL_IO_INPUT), " Input Pin should be set to LOW initially" );
99
106
100
- digitalWrite (TEST_PIN_DIGITAL_IO_OUTPUT, HIGH);
101
- // Skip assert as it may not be set to HIGH due to open-drain configuration
107
+ digitalWrite (TEST_PIN_DIGITAL_IO_OUTPUT, HIGH);// set output pin to HIGH ie, floating state
108
+ TEST_ASSERT_EQUAL_MESSAGE (LOW, digitalRead (TEST_PIN_DIGITAL_IO_INPUT), " Input Pin should be LOW when output is floating and input is pulldown " );
102
109
103
110
digitalWrite (TEST_PIN_DIGITAL_IO_OUTPUT, LOW);
104
111
TEST_ASSERT_EQUAL_MESSAGE (LOW, digitalRead (TEST_PIN_DIGITAL_IO_INPUT), " Input Pin should be set to LOW" );
@@ -155,21 +162,47 @@ TEST_IFX(digitalio_single_internal, test_invalid_pinmode) {
155
162
// No assertion as pinMode doesn't return a value, but ensure no crash
156
163
}
157
164
165
+ /* *
166
+ * @brief Test switching pinmode from OUTPUT to INPUT_PULLUP.
167
+ * Here an interrupt is attached to the input pin to check if it gets triggered on state change.
168
+ * This is to ensure that when the init pin value remains unchanged, underlying reconfigure function is used and there is no state change.
169
+ *
170
+ * @note: Assumption is made that TEST_PIN_DIGITAL_IO_OUTPUT is connected to TEST_PIN_DIGITAL_IO_INPUT
171
+ * for the test cases to work as expected.
172
+ */
173
+
174
+ TEST_IFX (digitalio_single_internal, test_switch_input_output)
175
+ {
176
+ pinMode (TEST_PIN_DIGITAL_IO_OUTPUT, OUTPUT);
177
+ pinMode (TEST_PIN_DIGITAL_IO_INPUT, INPUT);
178
+ digitalWrite (TEST_PIN_DIGITAL_IO_OUTPUT, HIGH);
179
+
180
+ interrupt_triggered = false ;
181
+ attachInterrupt (digitalPinToInterrupt (TEST_PIN_DIGITAL_IO_INPUT), test_pin_interrupt_handler, CHANGE);
182
+
183
+ pinMode (TEST_PIN_DIGITAL_IO_OUTPUT, INPUT_PULLUP);
184
+
185
+ TEST_ASSERT_FALSE_MESSAGE (interrupt_triggered, " Interrupt should not have been triggered on state change" );
186
+ }
187
+
158
188
/* *
159
189
* @brief Test group runner to run all test cases in this group.
160
190
*/
161
191
static TEST_GROUP_RUNNER (digitalio_single_internal)
162
- {
192
+ {
163
193
RUN_TEST_CASE (digitalio_single_internal, test_digitalio_read_write_input_normal);
164
194
RUN_TEST_CASE (digitalio_single_internal, test_digitalio_read_write_input_pullup);
165
195
RUN_TEST_CASE (digitalio_single_internal, test_digitalio_read_write_input_pulldown);
166
196
RUN_TEST_CASE (digitalio_single_internal, test_digitalio_read_write_output_opendrain);
197
+
167
198
#if !defined(ARDUINO_ARCH_XMC)
168
199
RUN_TEST_CASE (digitalio_single_internal, test_pinMode_invalid_pin);
169
200
RUN_TEST_CASE (digitalio_single_internal, test_digitalWrite_invalid_pin);
170
201
RUN_TEST_CASE (digitalio_single_internal, test_digitalRead_invalid_pin);
171
202
RUN_TEST_CASE (digitalio_single_internal, test_invalid_pinmode);
172
203
#endif // ARDUINO_ARCH_XMC skip these tests.
204
+
205
+ RUN_TEST_CASE (digitalio_single_internal, test_switch_input_output);
173
206
}
174
207
175
208
/* *
0 commit comments