Skip to content
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

[RP2xxx] Update pins helper to create pins as comptime-available data instead of zero-sized-types #303

Merged
merged 5 commits into from
Dec 19, 2024

Conversation

MatthiasPortzel
Copy link
Contributor

TL;DR: Replaces pins = pin_config.apply() with pins = comptime pin_config.pins(); pin_config.apply().

Long version:
This PR updates the "pin configuration helper," pins.GlobalConfiguration, to return a pins type which is more suitable for normal usage than the ad-hoc zero-sized-types previously created. For example, a pins.led_a could be saved into a runtime struct or passed to a struct initializer at comptime, just like a primitive Zig value. In order to make this possible, there are a couple of distinct changes being made:

  1. For .sio-function pins, the resulting pin is now a gpio.Pin. This removes the previous ZSTs and unifies the API. As well as making these data that can be used at runtime.
  2. For pwm-function pins, the Pwm type has been rewritten to be a runtime-compatible struct, instead of a zero-sized type. (Previously interacting with runtime-known Pwm pin numbers was impossible.)
  3. Instead of having pin_config.apply() return the pins type, as was previously the case, .apply() now returns nothing and pin_config.pins() returns the pins. This was necessary in order to allow pins to exist as normal Zig data. (Since apply has to be called at runtime, only the type of the return value is known at comptime.)

The only breaking change for users is splitting calls to apply() into a new call to pins(). (See the change to the examples for an example of what that looks like.)

This represents a regression in that you will no longer get compile-errors for e.g. reading from an output pin. This is acceptable to me for the time being because it matches our philosophy for the rest of the HAL—configuration is the user's responsibility. However, I'm not opposed to changing the GPIO interface to be stricter, and I have a couple of ideas on how to do that. But fundamentally I think that change would need to happen to gpio.Pin (e.g. maybe it splits into gpio.ReadPin and gpio.WritePin)—I don't think it's tenable to have a split in the type system between a configured pin and an unconfigured pin.

I'm 99% sure that accessing an ADC-type pin on main right now doesn't work. Since the ADC code is old, it could really use a rewrite, so I've left it broken in this PR (replaced it with a TODO). I'm happy to update the pin configuration helper to support ADC if it's clear what we want to do with the HAL.

@MatthiasPortzel MatthiasPortzel changed the title [RP2xxx] Update pins helper to create pins as comptime-avaiable data instead of zero-sized-types [RP2xxx] Update pins helper to create pins as comptime-available data instead of zero-sized-types Dec 3, 2024
This refactor maintains one of the very nice properties of the original approach: `pins.led_a` (for example) is comptime known. In order to preserve this property while also having `pins.led_a` be data (i.e. the existing gpio.Pin type) we needed to make pins accessible at comptime—before .apply has been called.

This refactor has a couple of parts
* Move `Pins(comptime config: GlobalConfiguration) type` to `PinsType(self: GlobalConfiguration) type`
* Move the return value from `GlobalConfiguration.apply` to `GlobalConfiguration.pins` (so `.apply` returns `void`. This allows `GlobalConfiguration.pins()` to be called at comptime, which allows sub-values of `pins` to be read at comptime.
* Change pins to be data instead of a type--replace Pwm with the new RuntimePwm and replace pins.GPIO with the existing gpio.Pin. The value of this is set in GlobalConfiguration.pins.

ADC is left broken, since I believe it's broken on main. Will be fixed in a later commit.
@mattnite mattnite merged commit b07430e into ZigEmbeddedGroup:main Dec 19, 2024
3 checks passed
mattnite added a commit that referenced this pull request Dec 19, 2024
…ble data instead of zero-sized-types (#303)"

This reverts commit b07430e.
@MatthiasPortzel MatthiasPortzel deleted the pins-as-data branch January 9, 2025 03:09
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.

2 participants