|
| 1 | +#include "5v_ctrl.h" |
| 2 | + |
| 3 | +#include <initializer_list> |
| 4 | +#include <tuple> |
| 5 | +#include <zephyr/drivers/gpio.h> |
| 6 | +#include <zephyr/logging/log.h> |
| 7 | +LOG_MODULE_REGISTER(rail5v); |
| 8 | + |
| 9 | +static const struct gpio_dt_spec ldo_en = GPIO_DT_SPEC_GET(DT_ALIAS(ldo5v), gpios); |
| 10 | + |
| 11 | +static const struct gpio_dt_spec servo_en = GPIO_DT_SPEC_GET(DT_NODELABEL(servo_enable), gpios); |
| 12 | + |
| 13 | +static const struct gpio_dt_spec pump_enable = GPIO_DT_SPEC_GET(DT_NODELABEL(pump_enable), gpios); |
| 14 | + |
| 15 | +const struct gpio_dt_spec buzzer = GPIO_DT_SPEC_GET(DT_ALIAS(buzz), gpios); |
| 16 | + |
| 17 | +// Prepares the GPIO |
| 18 | +int five_volt_rail_init() { |
| 19 | + using Pair = std::tuple<const struct gpio_dt_spec *, const char *>; |
| 20 | + constexpr Pair pairs[4] = { |
| 21 | + Pair{&ldo_en, "ldo5v"}, |
| 22 | + Pair{&servo_en, "servo"}, |
| 23 | + Pair{&pump_enable, "pump"}, |
| 24 | + Pair{&buzzer, "buzzer"}, |
| 25 | + }; |
| 26 | + for (auto [gpio, name] : pairs) { |
| 27 | + if (!gpio_is_ready_dt(gpio)) { |
| 28 | + LOG_WRN("No %s pin :(", name); |
| 29 | + } |
| 30 | + int ret = gpio_pin_configure_dt(gpio, GPIO_OUTPUT_INACTIVE); |
| 31 | + if (ret < 0) { |
| 32 | + LOG_WRN("Failed to conf %s pin :(", name); |
| 33 | + return ret; |
| 34 | + } |
| 35 | + } |
| 36 | + LOG_INF("5V Rail all good"); |
| 37 | + return 0; |
| 38 | +} |
| 39 | + |
| 40 | +static bool state[(int) FiveVoltItem::NumItems] = {false}; |
| 41 | + |
| 42 | +static int set_ldo_accordingly() { return gpio_pin_set_dt(&ldo_en, (state[0] || state[1] || state[2])); } |
| 43 | +static int set_item(FiveVoltItem item, bool set) { |
| 44 | + if (item >= FiveVoltItem::NumItems) { |
| 45 | + LOG_WRN("Invalid 5v rail item"); |
| 46 | + return -ENODEV; |
| 47 | + } |
| 48 | + state[(int) item] = set; |
| 49 | + if (item == FiveVoltItem::Buzzer) { |
| 50 | + return gpio_pin_set_dt(&buzzer, set); |
| 51 | + } else if (item == FiveVoltItem::Servos) { |
| 52 | + return gpio_pin_set_dt(&servo_en, set); |
| 53 | + } else { |
| 54 | + return gpio_pin_set_dt(&pump_enable, set); |
| 55 | + } |
| 56 | + return 0; |
| 57 | +} |
| 58 | +// CONFIG_PM who? |
| 59 | +int rail_item_enable(FiveVoltItem item) { return rail_item_set(item, true); } |
| 60 | + |
| 61 | +int rail_item_disable(FiveVoltItem item) { return rail_item_set(item, false); } |
| 62 | + |
| 63 | +int rail_item_set(FiveVoltItem item, bool set) { |
| 64 | + set_item(item, set); |
| 65 | + return set_ldo_accordingly(); |
| 66 | +} |
0 commit comments