Skip to content
This repository was archived by the owner on Aug 12, 2022. It is now read-only.

Commit 603d0df

Browse files
DhruvaG2000beriberikix
authored andcommitted
gpio: refactor API implementations to use DT
* Tested with basic LED Blink example. * Now users can put their board's overlay files in variants folder without needing to worry about whats inside the core implementation. * Better abstraction at core level makes porting new boards a cleaner process. Signed-off-by: Dhruva Gole <goledhruva@gmail.com>
1 parent e8e3c62 commit 603d0df

File tree

3 files changed

+96
-85
lines changed

3 files changed

+96
-85
lines changed

cores/arduino/zephyrCommon.cpp

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,57 +7,28 @@
77
#include "Arduino.h"
88

99
void pinMode(pin_size_t pinNumber, PinMode pinMode) {
10-
if (pinNumber >= 100) {
11-
pinNumber -= 100;
12-
if (pinMode == INPUT || pinMode == INPUT_PULLDOWN) {
13-
gpio_pin_configure(DEVICE_DT_GET(DT_NODELABEL(gpio1)), pinNumber,
14-
GPIO_INPUT | GPIO_ACTIVE_LOW);
15-
} else {
16-
gpio_pin_configure(DEVICE_DT_GET(DT_NODELABEL(gpio1)), pinNumber,
17-
GPIO_OUTPUT);
18-
}
19-
} else if (pinNumber < 100) {
20-
if (pinMode == INPUT || pinMode == INPUT_PULLDOWN) {
21-
gpio_pin_configure(DEVICE_DT_GET(DT_NODELABEL(gpio0)), pinNumber,
22-
GPIO_INPUT | GPIO_ACTIVE_LOW);
23-
} else {
24-
gpio_pin_configure(DEVICE_DT_GET(DT_NODELABEL(gpio0)), pinNumber,
25-
GPIO_OUTPUT);
26-
}
10+
if (pinMode == INPUT || pinMode == INPUT_PULLDOWN) { // input mode
11+
gpio_pin_configure_dt(arduino_pins[pinNumber],
12+
GPIO_INPUT | GPIO_ACTIVE_LOW);
13+
} else { // output mode
14+
gpio_pin_configure_dt(arduino_pins[pinNumber], GPIO_OUTPUT);
2715
}
2816
}
2917

3018
void digitalWrite(pin_size_t pinNumber, PinStatus status) {
31-
if (pinNumber >= 100) {
32-
pinNumber -= 100;
33-
if (status == HIGH) {
34-
gpio_pin_set(DEVICE_DT_GET(DT_NODELABEL(gpio1)), pinNumber,
35-
GPIO_ACTIVE_HIGH);
36-
} else if (status == LOW) {
37-
gpio_pin_set(DEVICE_DT_GET(DT_NODELABEL(gpio1)), pinNumber,
38-
GPIO_ACTIVE_LOW);
39-
}
40-
} else if (pinNumber < 100) {
41-
if (status == HIGH) {
42-
gpio_pin_set(DEVICE_DT_GET(DT_NODELABEL(gpio0)), pinNumber,
43-
GPIO_ACTIVE_HIGH);
44-
} else if (status == LOW) {
45-
gpio_pin_set(DEVICE_DT_GET(DT_NODELABEL(gpio0)), pinNumber,
46-
GPIO_ACTIVE_LOW);
47-
}
19+
if (status == HIGH) {
20+
gpio_pin_set_dt(arduino_pins[pinNumber], GPIO_ACTIVE_HIGH);
21+
} else if (status == LOW) {
22+
gpio_pin_set_dt(arduino_pins[pinNumber], GPIO_ACTIVE_LOW);
4823
}
4924
}
5025

5126
PinStatus digitalRead(pin_size_t pinNumber) {
5227
if (pinNumber >= 100) {
5328
pinNumber -= 100;
54-
return (gpio_pin_get(DEVICE_DT_GET(DT_NODELABEL(gpio1)), pinNumber) == 1)
55-
? HIGH
56-
: LOW;
29+
return (gpio_pin_get_dt(arduino_pins[pinNumber]) == 1) ? HIGH : LOW;
5730
}
58-
return (gpio_pin_get(DEVICE_DT_GET(DT_NODELABEL(gpio0)), pinNumber) == 1)
59-
? HIGH
60-
: LOW;
31+
return (gpio_pin_get_dt(arduino_pins[pinNumber]) == 1) ? HIGH : LOW;
6132
}
6233

6334
void delay(unsigned long ms) { k_sleep(K_MSEC(ms)); }
Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
/ {
22
zephyr,user {
3-
a0_gpios = <&arduino_nano_header 0 0>;
4-
a1_gpios = <&arduino_nano_header 1 0>;
5-
a2_gpios = <&arduino_nano_header 2 0>;
6-
a3_gpios = <&arduino_nano_header 3 0>;
7-
a4_gpios = <&arduino_nano_header 4 0>;
8-
a5_gpios = <&arduino_nano_header 5 0>;
9-
d0_gpios = <&arduino_nano_header 6 0>;
10-
d1_gpios = <&arduino_nano_header 7 0>;
11-
d2_gpios = <&arduino_nano_header 8 0>;
12-
d3_gpios = <&arduino_nano_header 9 0>;
13-
d4_gpios = <&arduino_nano_header 10 0>;
14-
d5_gpios = <&arduino_nano_header 11 0>;
15-
d6_gpios = <&arduino_nano_header 12 0>;
16-
d7_gpios = <&arduino_nano_header 13 0>;
17-
d8_gpios = <&arduino_nano_header 14 0>;
18-
d9_gpios = <&arduino_nano_header 15 0>;
19-
d10_gpios = <&arduino_nano_header 16 0>;
20-
d11_gpios = <&arduino_nano_header 17 0>;
21-
d12_gpios = <&arduino_nano_header 18 0>;
22-
d13_gpios = <&arduino_nano_header 19 0>;
23-
d14_gpios = <&arduino_nano_header 20 0>;
24-
d15_gpios = <&arduino_nano_header 21 0>;
3+
d0_gpios = <&arduino_nano_header 0 0>;
4+
d1_gpios = <&arduino_nano_header 1 0>;
5+
d2_gpios = <&arduino_nano_header 2 0>;
6+
d3_gpios = <&arduino_nano_header 3 0>;
7+
d4_gpios = <&arduino_nano_header 4 0>;
8+
d5_gpios = <&arduino_nano_header 5 0>;
9+
d6_gpios = <&arduino_nano_header 6 0>;
10+
d7_gpios = <&arduino_nano_header 7 0>;
11+
d8_gpios = <&arduino_nano_header 8 0>;
12+
d9_gpios = <&arduino_nano_header 9 0>;
13+
d10_gpios = <&arduino_nano_header 10 0>;
14+
d11_gpios = <&arduino_nano_header 11 0>;
15+
d12_gpios = <&arduino_nano_header 12 0>;
16+
d13_gpios = <&arduino_nano_header 13 0>;
17+
d14_gpios = <&arduino_nano_header 14 0>; /* D14 / A0 */
18+
d15_gpios = <&arduino_nano_header 15 0>;
19+
d16_gpios = <&arduino_nano_header 16 0>;
20+
d17_gpios = <&arduino_nano_header 17 0>;
21+
d18_gpios = <&arduino_nano_header 18 0>; /* D18 / A4 / I2C-SDA */
22+
d19_gpios = <&arduino_nano_header 19 0>; /* D19 / A5 / I2C-SCL */
23+
d20_gpios = <&arduino_nano_header 20 0>;
24+
d21_gpios = <&arduino_nano_header 21 0>;
2525
};
2626
};

variants/ARDUINO_NANO33BLE/arduino_nano_ble_sense_pinmap.h

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,70 @@
1414
static struct gpio_dt_spec d0 = GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), d0_gpios);
1515
static struct gpio_dt_spec d1 = GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), d1_gpios);
1616
static struct gpio_dt_spec d2 = GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), d2_gpios);
17+
static struct gpio_dt_spec d3 =
18+
GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), d3_gpios);
19+
static struct gpio_dt_spec d4 =
20+
GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), d4_gpios);
21+
static struct gpio_dt_spec d5 =
22+
GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), d5_gpios);
23+
static struct gpio_dt_spec d6 =
24+
GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), d6_gpios);
25+
static struct gpio_dt_spec d7 =
26+
GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), d7_gpios);
27+
static struct gpio_dt_spec d8 =
28+
GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), d8_gpios);
29+
static struct gpio_dt_spec d9 =
30+
GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), d9_gpios);
31+
static struct gpio_dt_spec d10 =
32+
GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), d10_gpios);
33+
static struct gpio_dt_spec d11 =
34+
GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), d11_gpios);
35+
static struct gpio_dt_spec d12 =
36+
GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), d12_gpios);
37+
static struct gpio_dt_spec d13 =
38+
GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), d13_gpios);
39+
static struct gpio_dt_spec d14 =
40+
GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), d13_gpios);
41+
static struct gpio_dt_spec d15 =
42+
GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), d15_gpios);
43+
static struct gpio_dt_spec d16 =
44+
GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), d16_gpios);
45+
static struct gpio_dt_spec d17 =
46+
GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), d17_gpios);
47+
static struct gpio_dt_spec d18 =
48+
GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), d18_gpios);
49+
static struct gpio_dt_spec d19 =
50+
GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), d19_gpios);
51+
static struct gpio_dt_spec d20 =
52+
GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), d20_gpios);
53+
static struct gpio_dt_spec d21 =
54+
GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), d21_gpios);
1755

18-
static struct gpio_dt_spec *arduino_pins[3] = { &d0, &d1, &d2 };
56+
static struct gpio_dt_spec *arduino_pins[22] = {
57+
&d0, &d1, &d2, &d3, &d4, &d5, &d6, &d7, &d8, &d9, &d10,
58+
&d11, &d12, &d13, &d14, &d15, &d16, &d17, &d18, &d19, &d20, &d21};
1959

2060
enum digitalPins {
21-
D0 = 3,
22-
D1 = 10,
23-
D2 = 11,
24-
D3 = 112,
25-
D4 = 115,
26-
D5 = 113,
27-
D6 = 114,
28-
D7 = 9,
29-
D8 = 10,
30-
D9 = 27,
31-
D10 = 102,
32-
D11 = 101,
33-
D12 = 108,
34-
D13 = 13,
35-
D14 = 4,
36-
D15 = 5,
37-
D16 = 30,
38-
D17 = 29,
39-
D18 = 14,
40-
D19 = 15,
41-
D20 = 28,
42-
D21 = 103
61+
D0,
62+
D1,
63+
D2,
64+
D3,
65+
D4,
66+
D5,
67+
D6,
68+
D7,
69+
D8,
70+
D9,
71+
D10,
72+
D11,
73+
D12,
74+
D13,
75+
D14,
76+
D15,
77+
D16,
78+
D17,
79+
D18,
80+
D19,
81+
D20,
82+
D21
4383
};

0 commit comments

Comments
 (0)