Skip to content

variants: Introduce the builtin-led-gpios node #61

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

Merged
merged 2 commits into from
Nov 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions documentation/variants.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,34 @@ The following example instantiates `Wire` and `Wire2` with each `i2c0` and `i2c1
};
```

### Configure Builtin-LED

The `builtin-led-gpios` node defines the Builtin-LED.
This node defines the `LED_BUILTIN` value by looking up the `digital-pin-gpios`
array to find the index of the pin.

The node is phandle-array, which uses the format same as `digital-pin-gpios`.

It set the digital pin number to the `LED_BUILTIN` if found the pin
that defined in `builtin-led-gpios` from `digital-pin-gpios`.

If the `builtin-led-gpios` is not defined, Use the node aliased as `led0`
to define `LED_BUILTIN`.

The `LED_BUILTIN` does not define here if it has not found both nodes or
defined `LED_BUILTIN` already.

For example, in the case of the 13th digital pins connected to the onboard LED,
define `builtin-led-gpios` as follows.

```
/ {
zephyr,user {
builtin-led-gpios = <&arduino_nano_header 13 0>;
};
};
```

### Overlays from scratch

You can see in the example above that there is no mapping for `LED0` in the
Expand Down
3 changes: 2 additions & 1 deletion variants/arduino_mkrzero/arduino_mkrzero.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
<&arduino_mkr_header 18 0>, /* D18 / A4 / I2C-SDA */
<&arduino_mkr_header 19 0>, /* D19 / A5 / I2C-SCL */
<&arduino_mkr_header 20 0>,
<&arduino_mkr_header 21 0>;
<&arduino_mkr_header 21 0>,
<&portb 8 0>;

pwms = <&tcc0 2 255>,
<&tcc0 3 255>;
Expand Down
1 change: 0 additions & 1 deletion variants/arduino_mkrzero/arduino_mkrzero_pinmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@
#include <zephyr/kernel.h>
#include <zephyr/drivers/i2c.h>

#define LED_BUILTIN 22
2 changes: 2 additions & 0 deletions variants/arduino_nano_33_ble/arduino_nano_33_ble.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@

serials = <&uart0>;
i2cs = <&arduino_nano_i2c>;

builtin-led-gpios = <&arduino_nano_header 13 0>;
};
};

Expand Down
1 change: 0 additions & 1 deletion variants/arduino_nano_33_ble/arduino_nano_33_ble_pinmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@
#include <zephyr/drivers/i2c.h>
#include <zephyr/device.h>

#define LED_BUILTIN 13
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@

serials = <&uart0>;
i2cs = <&arduino_nano_i2c>;

builtin-led-gpios = <&arduino_nano_header 13 0>;
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@
#include <zephyr/drivers/i2c.h>
#include <zephyr/device.h>

#define LED_BUILTIN 13
1 change: 0 additions & 1 deletion variants/arduino_nano_33_iot/arduino_nano_33_iot_pinmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@
#include <zephyr/drivers/i2c.h>
#include <zephyr/device.h>

#define LED_BUILTIN 13
2 changes: 0 additions & 2 deletions variants/nrf52840dk_nrf52840/nrf52840dk_nrf52840_pinmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@
#include <zephyr/drivers/i2c.h>
#include <zephyr/kernel.h>

#define LED_BUILTIN 22

#endif
43 changes: 43 additions & 0 deletions variants/variants.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,49 @@

#undef DIGITAL_PIN_CHECK_UNIQUE

#ifndef LED_BUILTIN

/* Return the index of it if matched, oterwise return 0 */
#define LED_BUILTIN_INDEX_BY_REG_AND_PINNUM(n, p, i, dev, num) \
(DIGITAL_PIN_EXISTS(n, p, i, dev, num) ? i : 0)

/* Only matched pin returns non-zero value, so the sum is matched pin's index */
#define LED_BUILTIN_FIND_DIGITAL_PIN(dev, pin) \
DT_FOREACH_PROP_ELEM_SEP_VARGS(DT_PATH(zephyr_user), digital_pin_gpios, \
LED_BUILTIN_INDEX_BY_REG_AND_PINNUM, (+), dev, pin)

#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), builtin_led_gpios) && \
(DT_PROP_LEN(DT_PATH(zephyr_user), builtin_led_gpios) > 0)

#if !(DT_FOREACH_PROP_ELEM_SEP_VARGS( \
DT_PATH(zephyr_user), digital_pin_gpios, DIGITAL_PIN_EXISTS, (+), \
DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), builtin_led_gpios, 0)), \
DT_PHA_BY_IDX(DT_PATH(zephyr_user), builtin_led_gpios, 0, pin)) > 0)
#warning "pin not found in digital_pin_gpios"
#else
#define LED_BUILTIN \
LED_BUILTIN_FIND_DIGITAL_PIN( \
DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), builtin_led_gpios, 0)), \
DT_PHA_BY_IDX(DT_PATH(zephyr_user), builtin_led_gpios, 0, pin))
#endif

/* If digital-pin-gpios is not defined, tries to use the led0 alias */
#elif DT_NODE_EXISTS(DT_ALIAS(led0))

#if !(DT_FOREACH_PROP_ELEM_SEP_VARGS(DT_PATH(zephyr_user), digital_pin_gpios, DIGITAL_PIN_EXISTS, \
(+), DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_ALIAS(led0), gpios, 0)), \
DT_PHA_BY_IDX(DT_ALIAS(led0), gpios, 0, pin)) > 0)
#warning "pin not found in digital_pin_gpios"
#else
#define LED_BUILTIN \
LED_BUILTIN_FIND_DIGITAL_PIN(DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_ALIAS(led0), gpios, 0)), \
DT_PHA_BY_IDX(DT_ALIAS(led0), gpios, 0, pin))
#endif

#endif // builtin_led_gpios

#endif // LED_BUILTIN

#define DN_ENUMS(n, p, i) D##i = i

/*
Expand Down