|
| 1 | +#include <Arduino.h> |
| 2 | +#include "zephyrInternal.h" |
| 3 | +#include "PinNames.h" |
| 4 | + |
| 5 | +// duplicate of one in zephyr common |
| 6 | +static const struct gpio_dt_spec arduino_pins[] = { DT_FOREACH_PROP_ELEM_SEP( |
| 7 | + DT_PATH(zephyr_user), digital_pin_gpios, GPIO_DT_SPEC_GET_BY_IDX, (, )) }; |
| 8 | + |
| 9 | +static const struct gpio_dt_spec arduino_ports[] = { DT_FOREACH_PROP_ELEM_SEP( |
| 10 | + DT_PATH(zephyr_user), port_pin_gpios, GPIO_DT_SPEC_GET_BY_IDX, (, )) }; |
| 11 | + |
| 12 | + |
| 13 | +void pinMode(PinName pinNumber, PinMode mode) { |
| 14 | + // See if the pinName maps to a real pin number if so use the pin number version |
| 15 | + int pin_index = PinNameToIndex(pinNumber); |
| 16 | + if (pin_index != -1) { |
| 17 | + // this will grab any settings out of the device tree. |
| 18 | + pinMode((pin_size_t)pin_index, mode); |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + if (pinNumber & DUAL_PAD) return; |
| 23 | + |
| 24 | + if (mode == INPUT) { // input mode |
| 25 | + gpio_pin_configure(arduino_ports[pinNumber >> 4].port, pinNumber & 0xf, |
| 26 | + GPIO_INPUT | GPIO_ACTIVE_HIGH); |
| 27 | + } else if (mode == INPUT_PULLUP) { // input with internal pull-up |
| 28 | + gpio_pin_configure(arduino_ports[pinNumber >> 4].port, pinNumber & 0xf, |
| 29 | + GPIO_INPUT | GPIO_PULL_UP | GPIO_ACTIVE_HIGH); |
| 30 | + } else if (mode == INPUT_PULLDOWN) { // input with internal pull-down |
| 31 | + gpio_pin_configure(arduino_ports[pinNumber >> 4].port, pinNumber & 0xf, |
| 32 | + GPIO_INPUT | GPIO_PULL_DOWN | GPIO_ACTIVE_HIGH); |
| 33 | + } else if (mode == OUTPUT) { // output mode |
| 34 | + gpio_pin_configure(arduino_ports[pinNumber >> 4].port, pinNumber & 0xf, |
| 35 | + GPIO_OUTPUT_LOW | GPIO_ACTIVE_HIGH); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +void digitalWrite(PinName pinNumber, PinStatus status) { |
| 40 | + if (pinNumber & DUAL_PAD) return; |
| 41 | + gpio_pin_set(arduino_ports[pinNumber >> 4].port, pinNumber & 0xf, status); |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +PinStatus digitalRead(PinName pinNumber) { |
| 46 | + if (pinNumber & DUAL_PAD) return LOW; |
| 47 | + return (gpio_pin_get(arduino_ports[pinNumber >> 4].port, pinNumber & 0xf) == 1) ? HIGH : LOW; |
| 48 | +} |
| 49 | + |
| 50 | +int analogRead(PinName pinNumber) { |
| 51 | + // Not sure what to do here, does anyone do something like: |
| 52 | + // analogRead(PA_0c); or analogRead(PC2_ALT0)? |
| 53 | + // first pass only support ones on pins. |
| 54 | + if (pinNumber & DUAL_PAD) { |
| 55 | + switch (pinNumber & 0xf) { |
| 56 | + case 0: return analogRead(A0); |
| 57 | + case 1: return analogRead(A1); |
| 58 | + case 2: return analogRead(A2); |
| 59 | + case 3: return analogRead(A3); |
| 60 | + default: return -1; |
| 61 | + } |
| 62 | + |
| 63 | + } |
| 64 | + int pin_index = PinNameToIndex(pinNumber); |
| 65 | + if (pin_index != -1) { |
| 66 | + return analogRead(pin_index); |
| 67 | + } |
| 68 | + return -1; |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | +void analogWrite(PinName pinNumber, int value) { |
| 73 | + // first pass only support ones on pins. |
| 74 | + if (pinNumber & DUAL_PAD) return; |
| 75 | + int pin_index = PinNameToIndex(pinNumber); |
| 76 | + if (pin_index != -1) { |
| 77 | + analogWrite(pin_index, value); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +int PinNameToIndex(PinName P) { |
| 84 | + if (P & DUAL_PAD) return -1; |
| 85 | + for (size_t i = 0; i < ARRAY_SIZE(arduino_pins); i++) { |
| 86 | + if ((arduino_ports[P >> 4].port == arduino_pins[i].port) && ((P & 0xf) == arduino_pins[i].pin)) { |
| 87 | + return i; |
| 88 | + } |
| 89 | + } |
| 90 | + return -1; |
| 91 | +} |
| 92 | + |
| 93 | +PinName digitalPinToPinName(pin_size_t P) { |
| 94 | + if (P < ARRAY_SIZE(arduino_pins)) { |
| 95 | + // Convert the pins port into an index into our port's list |
| 96 | + for (uint8_t port_index = 0; port_index < ARRAY_SIZE(arduino_ports); port_index++) { |
| 97 | + if (arduino_pins[P].port == arduino_ports[port_index].port) { |
| 98 | + return (PinName)((port_index << 4) | arduino_pins[P].pin); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + return PinName::NC; |
| 103 | +} |
0 commit comments