Skip to content

Commit 1fcf0cc

Browse files
committed
gpio: adds basic digital i/o support
* Map gpios as D1,2,etc to their gpionum and pinNum as per arduino_nano_r3_connector.dtsi * pulldown i/p mode gpio by default * implements pinMode, digitalRead, Write, delay and delayMicroseconds * Includes examples for push button LED and blink LED and hello world * For a comprehensive commit history on GPIO refer: DhruvaG2000/Arduino-Core-Zephyr#4 Signed-off-by: Dhruva Gole <[email protected]>
1 parent 8f8544f commit 1fcf0cc

24 files changed

+458
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(cores)

Kconfig

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#
2+
# Copyright (c) 2020 Toita, Hiroshi
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
7+
config ARDUINO_API
8+
bool "ARDUINO_API"
9+
depends on CPLUSPLUS
10+
default n
11+
12+
if ARDUINO_API
13+
14+
config ARDUINO_MAIN_LOOP
15+
bool "ARDUINO_MAIN_LOOP"
16+
default n
17+
18+
config ARDUINO_GPIO
19+
bool "ARDUINO_API"
20+
depends on GPIO
21+
default y
22+
23+
config ARDUINO_QUERY_PIN_CONFIG
24+
bool
25+
depends on ARDUINO_GPIO
26+
default n
27+
28+
config ARDUINO_UART
29+
bool "ARDUINO_API"
30+
depends on SERIAL
31+
depends on UART_INTERRUPT_DRIVEN
32+
default y
33+
34+
config QEMU_ICOUNT
35+
bool "QEMU icount mode"
36+
default n
37+
depends on QEMU_TARGET
38+
39+
endif

cores/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(arduino)

cores/arduino/Arduino.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "api/ArduinoAPI.h"
2+
3+
/* Zephyr libraries */
4+
#include "arduino_nano_ble_sense_pinmap.h"
5+
#include <zephyr/drivers/gpio.h>
6+
#include <zephyr/zephyr.h>

cores/arduino/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
zephyr_include_directories(.)
3+
4+
if(NOT DEFINED ARDUINO_BUILD_PATH)
5+
6+
zephyr_sources(zephyrCommon.cpp)
7+
zephyr_sources(main.cpp)
8+
9+
endif()
10+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
All the pins that are 100 + x are gpio1 pins and < 100 are in gpio0
3+
*/
4+
#define LED_BUILTIN 13
5+
6+
enum digitalPins {
7+
D0 = 103,
8+
D1 = 110,
9+
D2 = 111,
10+
D3 = 112,
11+
D4 = 115,
12+
D5 = 113,
13+
D6 = 114,
14+
D7 = 9,
15+
D8 = 10,
16+
D9 = 27,
17+
D10 = 102,
18+
D11 = 101,
19+
D12 = 108,
20+
D13 = 13,
21+
D14 = 4,
22+
D15 = 5,
23+
D16 = 30,
24+
D17 = 29,
25+
D18 = 14,
26+
D19 = 15,
27+
D20 = 28,
28+
D21 = 103
29+
};

cores/arduino/main.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "Arduino.h"
2+
3+
int main(void) {
4+
setup();
5+
6+
for (;;) {
7+
loop();
8+
}
9+
10+
return 0;
11+
}

cores/arduino/zephyrCommon.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include "Arduino.h"
2+
3+
void pinMode(pin_size_t pinNumber, PinMode pinMode) {
4+
if (pinNumber >= 100) {
5+
pinNumber -= 100;
6+
if (pinMode == INPUT || pinMode == INPUT_PULLDOWN) {
7+
gpio_pin_configure(DEVICE_DT_GET(DT_NODELABEL(gpio1)), pinNumber,
8+
GPIO_INPUT | GPIO_ACTIVE_LOW);
9+
} else {
10+
gpio_pin_configure(DEVICE_DT_GET(DT_NODELABEL(gpio1)), pinNumber,
11+
GPIO_OUTPUT);
12+
}
13+
} else if (pinNumber < 100) {
14+
if (pinMode == INPUT || pinMode == INPUT_PULLDOWN) {
15+
gpio_pin_configure(DEVICE_DT_GET(DT_NODELABEL(gpio0)), pinNumber,
16+
GPIO_INPUT | GPIO_ACTIVE_LOW);
17+
} else {
18+
gpio_pin_configure(DEVICE_DT_GET(DT_NODELABEL(gpio0)), pinNumber,
19+
GPIO_OUTPUT);
20+
}
21+
}
22+
}
23+
24+
void digitalWrite(pin_size_t pinNumber, PinStatus status) {
25+
if (pinNumber >= 100) {
26+
pinNumber -= 100;
27+
if (status == HIGH) {
28+
gpio_pin_set(DEVICE_DT_GET(DT_NODELABEL(gpio1)), pinNumber,
29+
GPIO_ACTIVE_HIGH);
30+
} else if (status == LOW) {
31+
gpio_pin_set(DEVICE_DT_GET(DT_NODELABEL(gpio1)), pinNumber,
32+
GPIO_ACTIVE_LOW);
33+
}
34+
} else if (pinNumber < 100) {
35+
if (status == HIGH) {
36+
gpio_pin_set(DEVICE_DT_GET(DT_NODELABEL(gpio0)), pinNumber,
37+
GPIO_ACTIVE_HIGH);
38+
} else if (status == LOW) {
39+
gpio_pin_set(DEVICE_DT_GET(DT_NODELABEL(gpio0)), pinNumber,
40+
GPIO_ACTIVE_LOW);
41+
}
42+
}
43+
}
44+
45+
PinStatus digitalRead(pin_size_t pinNumber) {
46+
if (pinNumber >= 100) {
47+
pinNumber -= 100;
48+
return (gpio_pin_get(DEVICE_DT_GET(DT_NODELABEL(gpio1)), pinNumber) == 1)
49+
? HIGH
50+
: LOW;
51+
}
52+
return (gpio_pin_get(DEVICE_DT_GET(DT_NODELABEL(gpio0)), pinNumber) == 1)
53+
? HIGH
54+
: LOW;
55+
}
56+
57+
void delay(unsigned long ms) { k_sleep(K_MSEC(ms)); }
58+
59+
void delayMicroseconds(unsigned int us) { k_sleep(K_USEC(us)); }
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(blinky)
6+
7+
target_sources(app PRIVATE src/main.cpp)

examples/blinky_arduino/README.rst

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
.. _blinky-sample:
2+
3+
Blinky
4+
######
5+
6+
Overview
7+
********
8+
9+
This Arduino Blinky sample blinks an LED forever using the `ArduinoAPI`.
10+
11+
Requirements
12+
************
13+
14+
Your board must:
15+
16+
#. Have an LED connected via a GPIO pin (these are called "User LEDs" on many of
17+
Zephyr's :ref:`boards`).
18+
#. Have the LED configured using the ``led0`` devicetree alias.
19+
20+
Building and Running
21+
********************
22+
23+
Build and flash Blinky as follows,
24+
25+
```sh
26+
$> west build -p -b arduino_nano_33_ble samples/basic/arduino-blinky/ -DZEPHYR_EXTRA_MODULES=/home/$USER/zephyrproject/modules/lib/Arduino-Core-Zephyr
27+
28+
$> west flash --bossac=/home/$USER/.arduino15/packages/arduino/tools/bossac/1.9.1-arduino2/bossac
29+
```
30+
31+
After flashing, the LED starts to blink. If a runtime error occurs, the sample
32+
exits without printing to the console.
33+
34+
Adding board support
35+
********************
36+
37+
To add support for your board, add something like this to your devicetree:
38+
39+
.. code-block:: DTS
40+
41+
/ {
42+
aliases {
43+
led0 = &myled0;
44+
};
45+
46+
leds {
47+
compatible = "gpio-leds";
48+
myled0: led_0 {
49+
gpios = <&gpio0 13 GPIO_ACTIVE_LOW>;
50+
};
51+
};
52+
};
53+
54+
The above sets your board's ``led0`` alias to use pin 13 on GPIO controller
55+
``gpio0``. The pin flags :c:macro:`GPIO_ACTIVE_HIGH` mean the LED is on when
56+
the pin is set to its high state, and off when the pin is in its low state.
57+
58+
Tips:
59+
60+
- See :dtcompatible:`gpio-leds` for more information on defining GPIO-based LEDs
61+
in devicetree.
62+
63+
- If you're not sure what to do, check the devicetrees for supported boards which
64+
use the same SoC as your target. See :ref:`get-devicetree-outputs` for details.
65+
66+
- See :zephyr_file:`include/zephyr/dt-bindings/gpio/gpio.h` for the flags you can use
67+
in devicetree.
68+
69+
- If the LED is built in to your board hardware, the alias should be defined in
70+
your :ref:`BOARD.dts file <devicetree-in-out-files>`. Otherwise, you can
71+
define one in a :ref:`devicetree overlay <set-devicetree-overlays>`.

examples/blinky_arduino/prj.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_GPIO=y
2+
CONFIG_CPLUSPLUS=y
3+
CONFIG_ARDUINO_API=y

examples/blinky_arduino/sample.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
sample:
2+
name: Blinky Sample
3+
tests:
4+
sample.basic.blinky:
5+
tags: LED gpio
6+
filter: dt_enabled_alias_with_parent_compat("led0", "gpio-leds")
7+
depends_on: gpio
8+
harness: led
9+
integration_platforms:
10+
- frdm_k64f

examples/blinky_arduino/src/main.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* Blink inbuilt LED example */
2+
3+
#include <Arduino.h>
4+
5+
/* 1000 msec = 1 sec */
6+
#define SLEEP_TIME_MS 1000
7+
8+
void setup() { pinMode(LED_BUILTIN, OUTPUT); }
9+
10+
void loop() {
11+
digitalWrite(LED_BUILTIN, HIGH);
12+
delay(SLEEP_TIME_MS);
13+
digitalWrite(LED_BUILTIN, LOW);
14+
delay(SLEEP_TIME_MS);
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(blinky)
6+
7+
target_sources(app PRIVATE src/main.cpp)

examples/button_press_led/README.rst

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
Button press LED
2+
######
3+
4+
Overview
5+
********
6+
7+
This Arduino sample turns ON an LED if button pressed using the `ArduinoAPI`.
8+
9+
Requirements
10+
************
11+
12+
Your board must:
13+
14+
#. Have an LED connected via a GPIO pin (these are called "User LEDs" on many of
15+
Zephyr's :ref:`boards`).
16+
#. Have the LED configured using the ``led0`` devicetree alias.
17+
#. Have a button connected to pin `D9` of the arduino externally (pulled down by default)
18+
19+
Building and Running
20+
********************
21+
22+
Build and flash as follows,
23+
24+
```sh
25+
$> west build -p -b arduino_nano_33_ble samples/button_press_led -DZEPHYR_EXTRA_MODULES=/home/$USER/zephyrproject/modules/lib/Arduino-Core-Zephyr
26+
27+
$> west flash --bossac=/home/$USER/.arduino15/packages/arduino/tools/bossac/1.9.1-arduino2/bossac
28+
```
29+
30+
Adding board support
31+
********************
32+
33+
To add support for your board, add something like this to your devicetree:
34+
35+
.. code-block:: DTS
36+
37+
/ {
38+
aliases {
39+
led0 = &myled0;
40+
};
41+
42+
leds {
43+
compatible = "gpio-leds";
44+
myled0: led_0 {
45+
gpios = <&gpio0 13 GPIO_ACTIVE_LOW>;
46+
};
47+
};
48+
};
49+
50+
The above sets your board's ``led0`` alias to use pin 13 on GPIO controller
51+
``gpio0``. The pin flags :c:macro:`GPIO_ACTIVE_HIGH` mean the LED is on when
52+
the pin is set to its high state, and off when the pin is in its low state.
53+
54+
Tips:
55+
56+
- See :dtcompatible:`gpio-leds` for more information on defining GPIO-based LEDs
57+
in devicetree.
58+
59+
- If you're not sure what to do, check the devicetrees for supported boards which
60+
use the same SoC as your target. See :ref:`get-devicetree-outputs` for details.
61+
62+
- See :zephyr_file:`include/zephyr/dt-bindings/gpio/gpio.h` for the flags you can use
63+
in devicetree.
64+
65+
- If the LED is built in to your board hardware, the alias should be defined in
66+
your :ref:`BOARD.dts file <devicetree-in-out-files>`. Otherwise, you can
67+
define one in a :ref:`devicetree overlay <set-devicetree-overlays>`.

examples/button_press_led/prj.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_GPIO=y
2+
CONFIG_CPLUSPLUS=y
3+
CONFIG_ARDUINO_API=y

examples/button_press_led/sample.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
sample:
2+
name: Blinky Sample
3+
tests:
4+
sample.basic.blinky:
5+
tags: LED gpio
6+
filter: dt_enabled_alias_with_parent_compat("led0", "gpio-leds")
7+
depends_on: gpio
8+
harness: led
9+
integration_platforms:
10+
- frdm_k64f

0 commit comments

Comments
 (0)