Skip to content

Update ESP32 GPIO driver errors to match other MCU platforms #1408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

UncleGrumpy
Copy link
Collaborator

@UncleGrumpy UncleGrumpy commented Dec 21, 2024

These changes move the GPIO specific atoms out of the ESP32 platform default atoms (created at boot) into the GPIO driver where they are only created as needed.

Breaking changes update the error returns to match the spec. Nifs raise Error, and port functions return {error, Reason} when errors are encountered.

Adds gpio driver tests to the Wokwi sim to test driver functionality and error return correctness for invalid parameters.

With #1448 closes #894

These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).

SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later

@petermm
Copy link
Contributor

petermm commented Dec 21, 2024

do we need to remove RISING_ATOM_INDEX et al in platform_defaultatoms.h ?

#define RISING_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 2)

@UncleGrumpy
Copy link
Collaborator Author

do we need to remove RISING_ATOM_INDEX et al in platform_defaultatoms.h ?

#define RISING_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 2)

This is done in commit bede918

@UncleGrumpy
Copy link
Collaborator Author

My mistake, you are correct! Those changes should have been included in the commit I mentioned, but were not added. Correction has been pushed.

@UncleGrumpy UncleGrumpy marked this pull request as ready for review December 21, 2024 23:32
@UncleGrumpy UncleGrumpy force-pushed the esp32_gpio_spec branch 2 times, most recently from c4c5343 to 24fdbf9 Compare December 22, 2024 03:23
@petermm
Copy link
Contributor

petermm commented Dec 27, 2024

LGTM, but assume it needs rebasing to new defaultatoms setup.

@UncleGrumpy
Copy link
Collaborator Author

Indeed. I also realized that I should be using port_create_error_tuple, and remove the unnecessary error_tuple_maybe_gc and update/remove the usage of create_pair as well.

@UncleGrumpy
Copy link
Collaborator Author

UncleGrumpy commented Dec 27, 2024

All fixed. This should be ready now. We needed to keep create_pair, because we need to preserve a root, if gc is necessary.

@UncleGrumpy UncleGrumpy force-pushed the esp32_gpio_spec branch 3 times, most recently from f3a4ad8 to 8f81b88 Compare December 27, 2024 21:21
@bettio
Copy link
Collaborator

bettio commented Dec 29, 2024

Please, see also #1442

@UncleGrumpy
Copy link
Collaborator Author

UncleGrumpy commented Dec 30, 2024

Please, see also #1442

I believe I have followed the guidelines you laid out in #1442, and the comments you made above about atom creation and fixed all of the atoms used by the driver to insure they are created in a safe way. I also used ATOM_STR() inline, removing the const char* declarations.

UncleGrumpy added a commit to UncleGrumpy/AtomVM that referenced this pull request Jan 3, 2025
Standardizes STM32 error returns. Nif functions raise `Error`, port functions return
`{error, Reason}` on errors.

Addresses concerns raised in issue atomvm#1442.

With atomvm#1408 this closes issue atomvm#894

Signed-off-by: Winford <[email protected]>
UncleGrumpy added a commit to UncleGrumpy/AtomVM that referenced this pull request Jan 3, 2025
Standardizes stm32 gpio driver error returns. Nif functions raise `Error`, port functions return
`{error, Reason}` on errors.

Addresses some concerns raised in issue atomvm#1442.

With atomvm#1408 this closes issue atomvm#894

Signed-off-by: Winford <[email protected]>
UncleGrumpy added a commit to UncleGrumpy/AtomVM that referenced this pull request Jan 3, 2025
Standardizes stm32 gpio driver error returns. Nif functions raise `Error`, port functions return
`{error, Reason}` on errors.

Addresses some concerns raised in issue atomvm#1442.

With atomvm#1408 this closes issue atomvm#894

Signed-off-by: Winford <[email protected]>
UncleGrumpy added a commit to UncleGrumpy/AtomVM that referenced this pull request Jan 3, 2025
Standardizes stm32 gpio driver error returns. Nif functions raise `Error`, port functions return
`{error, Reason}` on errors.

Addresses some concerns raised in issue atomvm#1442.

With atomvm#1408 this closes issue atomvm#894

Signed-off-by: Winford <[email protected]>
UncleGrumpy added a commit to UncleGrumpy/AtomVM that referenced this pull request Jan 3, 2025
Standardizes stm32 gpio driver error returns. Nif functions raise `Error`, port functions return
`{error, Reason}` on errors.

Addresses some concerns raised in issue atomvm#1442.

With atomvm#1408 this closes issue atomvm#894

Signed-off-by: Winford <[email protected]>
@UncleGrumpy UncleGrumpy force-pushed the esp32_gpio_spec branch 2 times, most recently from cb6e30e to 35b5bb9 Compare January 3, 2025 08:54
@UncleGrumpy UncleGrumpy force-pushed the esp32_gpio_spec branch 2 times, most recently from 8400531 to 71e1443 Compare January 9, 2025 04:59
@UncleGrumpy UncleGrumpy force-pushed the esp32_gpio_spec branch 2 times, most recently from fd4ebdc to cffd8ff Compare January 9, 2025 05:17
@UncleGrumpy UncleGrumpy changed the title Updates to ESP32 GPIO driver to match other MCU platforms Update ESP32 GPIO driver errors to match other MCU platforms Mar 11, 2025
Reduce the number of GPIO related atoms created at boot by using more efficient matching to atom
string where possible.

Removes `static const char *const` declarations for atoms, replacing with `ATOM_STR()` in place
for better readability.

Handles the creations of atoms used by the driver in a safer way, checking their validity where
necessary.

Signed-off-by: Winford <[email protected]>
Adds the `internal_error` atom from OTP for returning errors for "thing that shouldn't happen".
There are rare occasions where user inputs may be valid, but an internal operation fails for some
reason (potentially an internal bug to the VM, not the users application code). For example, this
may be an invalid internal state when setting the direction for a gpio pin. If the users inputs are
valid `internal_error` should be returned rather than `badarg`, which could mislead application
developers into chasing a bug in their application that isn't there.

Signed-off-by: Winford <[email protected]>
Changes error returns to match the spec and ofther platforms. Nifs now raise any errors, and port
function errors return `{error, Reason}`.

Updates gpio.erl to reflect the correct returns, and make it more clear that errors for nifs will be
raised.

Closes atomvm#894

Signed-off-by: Winford <[email protected]>
Adds a test to run on hardware with a jumper wire, or in the wokwi simulator to test the basic
esp32 gpio driver funtionality and error handling, with tests for all boards when full_sim_test
is run.

The sim-test for P4 depends on PR atomvm#1438 being merged to be able to select the correct pins for the
device for the the GPIO tests.

Signed-off-by: Winford <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Return {error, Reason} from gpio operations in error conditions
3 participants