|
4 | 4 | * SPDX-License-Identifier: Apache-2.0
|
5 | 5 | */
|
6 | 6 |
|
| 7 | +#pragma once |
| 8 | + |
7 | 9 | #include "api/ArduinoAPI.h"
|
8 | 10 |
|
| 11 | +#include <zephyr/kernel.h> |
9 | 12 | #include <zephyr/drivers/gpio.h>
|
10 | 13 | #include <zephyr/drivers/pwm.h>
|
11 | 14 | #include <zephyr/drivers/adc.h>
|
12 |
| -#include <zephyr/kernel.h> |
| 15 | +#include <zephyr/drivers/i2c.h> |
| 16 | + |
| 17 | +#define DIGITAL_PIN_EXISTS(n, p, i, dev, num) \ |
| 18 | + (((dev == DT_REG_ADDR(DT_PHANDLE_BY_IDX(n, p, i))) && \ |
| 19 | + (num == DT_PHA_BY_IDX(n, p, i, pin))) \ |
| 20 | + ? 1 \ |
| 21 | + : 0) |
| 22 | + |
| 23 | +/* Check all pins are defined only once */ |
| 24 | +#define DIGITAL_PIN_CHECK_UNIQUE(i, _) \ |
| 25 | + ((DT_FOREACH_PROP_ELEM_SEP_VARGS( \ |
| 26 | + DT_PATH(zephyr_user), digital_pin_gpios, DIGITAL_PIN_EXISTS, (+), \ |
| 27 | + DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), digital_pin_gpios, i)), \ |
| 28 | + DT_PHA_BY_IDX(DT_PATH(zephyr_user), digital_pin_gpios, i, pin))) == 1) |
| 29 | + |
| 30 | +#if !LISTIFY(DT_PROP_LEN(DT_PATH(zephyr_user), digital_pin_gpios), DIGITAL_PIN_CHECK_UNIQUE, (&&)) |
| 31 | +#error "digital_pin_gpios has duplicate definition" |
| 32 | +#endif |
| 33 | + |
| 34 | +#undef DIGITAL_PIN_CHECK_UNIQUE |
| 35 | + |
| 36 | +#ifndef LED_BUILTIN |
| 37 | + |
| 38 | +/* Return the index of it if matched, oterwise return 0 */ |
| 39 | +#define LED_BUILTIN_INDEX_BY_REG_AND_PINNUM(n, p, i, dev, num) \ |
| 40 | + (DIGITAL_PIN_EXISTS(n, p, i, dev, num) ? i : 0) |
| 41 | + |
| 42 | +/* Only matched pin returns non-zero value, so the sum is matched pin's index */ |
| 43 | +#define DIGITAL_PIN_GPIOS_FIND_PIN(dev, pin) \ |
| 44 | + DT_FOREACH_PROP_ELEM_SEP_VARGS(DT_PATH(zephyr_user), digital_pin_gpios, \ |
| 45 | + LED_BUILTIN_INDEX_BY_REG_AND_PINNUM, (+), dev, pin) |
| 46 | + |
| 47 | +#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), builtin_led_gpios) && \ |
| 48 | + (DT_PROP_LEN(DT_PATH(zephyr_user), builtin_led_gpios) > 0) |
| 49 | + |
| 50 | +#if !(DT_FOREACH_PROP_ELEM_SEP_VARGS( \ |
| 51 | + DT_PATH(zephyr_user), digital_pin_gpios, DIGITAL_PIN_EXISTS, (+), \ |
| 52 | + DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), builtin_led_gpios, 0)), \ |
| 53 | + DT_PHA_BY_IDX(DT_PATH(zephyr_user), builtin_led_gpios, 0, pin)) > 0) |
| 54 | +#warning "pin not found in digital_pin_gpios" |
| 55 | +#else |
| 56 | +#define LED_BUILTIN \ |
| 57 | + DIGITAL_PIN_GPIOS_FIND_PIN( \ |
| 58 | + DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), builtin_led_gpios, 0)), \ |
| 59 | + DT_PHA_BY_IDX(DT_PATH(zephyr_user), builtin_led_gpios, 0, pin)) |
| 60 | +#endif |
| 61 | + |
| 62 | +/* If digital-pin-gpios is not defined, tries to use the led0 alias */ |
| 63 | +#elif DT_NODE_EXISTS(DT_ALIAS(led0)) |
| 64 | + |
| 65 | +#if !(DT_FOREACH_PROP_ELEM_SEP_VARGS(DT_PATH(zephyr_user), digital_pin_gpios, DIGITAL_PIN_EXISTS, \ |
| 66 | + (+), DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_ALIAS(led0), gpios, 0)), \ |
| 67 | + DT_PHA_BY_IDX(DT_ALIAS(led0), gpios, 0, pin)) > 0) |
| 68 | +#warning "pin not found in digital_pin_gpios" |
| 69 | +#else |
| 70 | +#define LED_BUILTIN \ |
| 71 | + DIGITAL_PIN_GPIOS_FIND_PIN(DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_ALIAS(led0), gpios, 0)), \ |
| 72 | + DT_PHA_BY_IDX(DT_ALIAS(led0), gpios, 0, pin)) |
| 73 | +#endif |
| 74 | + |
| 75 | +#endif // builtin_led_gpios |
| 76 | + |
| 77 | +#endif // LED_BUILTIN |
| 78 | + |
| 79 | +#define DN_ENUMS(n, p, i) D##i = i |
| 80 | + |
| 81 | +/* |
| 82 | + * expand as |
| 83 | + * enum digitalPins { D0, D1, ... LED... NUM_OF_DIGITAL_PINS }; |
| 84 | + */ |
| 85 | +enum digitalPins { |
| 86 | + DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), digital_pin_gpios, DN_ENUMS, (, )), |
| 87 | + NUM_OF_DIGITAL_PINS |
| 88 | +}; |
| 89 | + |
| 90 | +const struct gpio_dt_spec arduino_pins[] = {DT_FOREACH_PROP_ELEM_SEP( |
| 91 | + DT_PATH(zephyr_user), digital_pin_gpios, GPIO_DT_SPEC_GET_BY_IDX, (, ))}; |
| 92 | + |
| 93 | +#ifdef CONFIG_ADC |
| 94 | + |
| 95 | +#define AN_ENUMS(n, p, i) A ## i = DIGITAL_PIN_GPIOS_FIND_PIN( \ |
| 96 | + DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), p, i)), \ |
| 97 | + DT_PHA_BY_IDX(DT_PATH(zephyr_user), p, i, pin)), |
| 98 | +enum analogPins { DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), |
| 99 | + adc_pin_gpios, AN_ENUMS) }; |
| 100 | + |
| 101 | +#endif |
13 | 102 |
|
14 | 103 | #include <variants.h>
|
15 | 104 | #include <zephyrPrint.h>
|
|
0 commit comments