|
| 1 | +/** |
| 2 | + * This file is part of the Titan Flight Computer Project |
| 3 | + * Copyright (c) 2026 UW SARP |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, version 3. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, but |
| 10 | + * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | + * General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + * |
| 17 | + * @file app/utils/valve.c |
| 18 | + * @authors Mahir Emran |
| 19 | + * @brief Utility functions to actuate valves by name |
| 20 | + */ |
| 21 | +#include "valve.h" |
| 22 | +#include "devices/actuator.h" |
| 23 | +#include "peripheral/gpio.h" |
| 24 | +#include "peripheral/pwm.h" |
| 25 | +#include "peripheral/errc.h" |
| 26 | + |
| 27 | +static uint8_t get_spi_inst(int32_t mosi, int32_t miso) { |
| 28 | + // TODO: update based on later stuff |
| 29 | + if (mosi == 46 && miso == 45) return 1; |
| 30 | + if (mosi == 75 && miso == 74) return 2; |
| 31 | + if (mosi == 111 && miso == 110) return 3; |
| 32 | + if (mosi == 5 && miso == 4) return 4; |
| 33 | + if (mosi == 23 && miso == 22) return 5; |
| 34 | + if (mosi == 127 && miso == 126) return 6; |
| 35 | + return 0; |
| 36 | +} |
| 37 | + |
| 38 | +static bool get_pwm_inst_chan(int32_t pin, uint8_t *inst, uint8_t *chan) { |
| 39 | + // TODO - update based on later stuff |
| 40 | + if (pin == 37) { *inst = 2; *chan = 1; return true; } |
| 41 | + if (pin == 38) { *inst = 2; *chan = 2; return true; } |
| 42 | + if (pin == 39) { *inst = 2; *chan = 3; return true; } |
| 43 | + if (pin == 40) { *inst = 2; *chan = 4; return true; } |
| 44 | + if (pin == 45) { *inst = 3; *chan = 1; return true; } |
| 45 | + if (pin == 46) { *inst = 3; *chan = 2; return true; } |
| 46 | + if (pin == 49) { *inst = 3; *chan = 3; return true; } |
| 47 | + if (pin == 50) { *inst = 3; *chan = 4; return true; } |
| 48 | + if (pin == 133) { *inst = 4; *chan = 1; return true; } |
| 49 | + if (pin == 134) { *inst = 4; *chan = 2; return true; } |
| 50 | + if (pin == 136) { *inst = 4; *chan = 3; return true; } |
| 51 | + if (pin == 137) { *inst = 4; *chan = 4; return true; } |
| 52 | + return false; |
| 53 | +} |
| 54 | + |
| 55 | +void set_valve(struct valve_t* valve, bool actuated, enum ti_errc_t* errc) { |
| 56 | + if (errc) *errc = TI_ERRC_NONE; |
| 57 | + if (!valve) { |
| 58 | + TI_SET_ERRC(errc, TI_ERRC_INVALID_ARG, "valve parameter is NULL"); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + if (valve->is_spi) { |
| 63 | + actuator_t dev = {0}; |
| 64 | + dev.spi_config.ss_pin = valve->pin_3; // CS pin |
| 65 | + dev.spi_config.spi_inst = get_spi_inst(valve->pin_1, valve->pin_2); |
| 66 | + |
| 67 | + if (dev.spi_config.spi_inst == 0) { |
| 68 | + TI_SET_ERRC(errc, TI_ERRC_INVALID_ARG, "Invalid SPI pins"); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + // TODO: channel config hardcoded? valve might need it |
| 73 | + actuator_set_channel_enable(&dev, ACTUATOR_CHANNEL_0, actuated, errc); |
| 74 | + if (errc && *errc != TI_ERRC_NONE) { |
| 75 | + TI_SET_ERRC(errc, *errc, "Propagated actuator error"); |
| 76 | + } |
| 77 | + } else { |
| 78 | + uint8_t inst = 0, chan = 0; |
| 79 | + if (!get_pwm_inst_chan(valve->pin_1, &inst, &chan)) { |
| 80 | + TI_SET_ERRC(errc, TI_ERRC_INVALID_ARG, "Invalid PWM pin"); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + struct ti_pwm_config_t config = { |
| 85 | + .channel = chan, |
| 86 | + .instance = inst, |
| 87 | + .clock_freq = 4000000, |
| 88 | + .freq = 100, |
| 89 | + .duty = actuated ? 1000 : 0 |
| 90 | + }; |
| 91 | + |
| 92 | + ti_set_pwm(config, errc); |
| 93 | + if (errc && *errc != TI_ERRC_NONE) { |
| 94 | + TI_SET_ERRC(errc, *errc, "Propagated PWM error"); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments