Skip to content

Commit

Permalink
tests/periph/gpio_ll: fix randomly failing test
Browse files Browse the repository at this point in the history
A test intended to ensure that a configuration toggling the direction
of a GPIO two times restores the original configuration not only
compared the configuration at the two points in time, but also the
value of the input buffer. Since a floating input reads back random
values when not externally driven, the test was actually randomly
failing. Apparently I got lucky before consistently and this never
triggered until now.

This now clears the input value from both the configuration reported
before and after toggling the direction twice and should now indeed
succeed consistently.
  • Loading branch information
maribu committed Aug 1, 2024
1 parent ed6aa33 commit 2e8027a
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions tests/periph/gpio_ll/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,11 @@ static void test_switch_dir(void)
expect(conf.state == GPIO_INPUT);
gpio_conf_t conf_orig = conf;

/* conf_orig.initial_value contains the current value in query_conf.
* Since this is a floating input connected to a floating input, it is
* random here. ==> Clear it. */
conf_orig.initial_value = false;

/* capture output state before switching from input mode to output mode, so
* that it can be restored when switching back to input mode */
uword_t out_state = gpio_ll_read_output(port_out);
Expand All @@ -935,6 +940,8 @@ static void test_switch_dir(void)
gpio_ll_write(port_out, out_state);
/* verify we are back at the old config */
conf = gpio_ll_query_conf(port_out, PIN_OUT_0);
/* again, initial_value is random due to floating input ==> clear it */
conf.initial_value = false;
test_passed = (conf.bits == conf_orig.bits);
printf_optional("Returning back to input had no side effects on config: %s\n",
noyes[test_passed]);
Expand Down

0 comments on commit 2e8027a

Please # to comment.