Skip to content

Commit 708ee4d

Browse files
committed
Implementation of valve.c
1 parent 29d24d8 commit 708ee4d

3 files changed

Lines changed: 110 additions & 9 deletions

File tree

src/app/utils/valve.c

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
}

src/app/utils/valve.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* This file is part of the Titan Flight Computer Project
3-
* Copyright (c) 2025 UW SARP
3+
* Copyright (c) 2026 UW SARP
44
*
55
* This program is free software: you can redistribute it and/or modify
66
* it under the terms of the GNU General Public License as published by
@@ -15,25 +15,29 @@
1515
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
*
1717
* @file app/utils/valve.h
18-
* @authors Joshua Beard
18+
* @authors Joshua Beard, Mahir Emran
1919
* @brief Utility functions to actuate valves by name
2020
*/
2121

2222

23-
#include "src/util/error.h"
23+
#include "src/peripheral/errc.h"
24+
#include <stdbool.h>
25+
#include <stdint.h>
2426

25-
enum Valve {
26-
27+
struct valve_t {
28+
int32_t pin_1; // pwm pin if PWM, MOSI if spi
29+
int32_t pin_2; // nothing if PWM, MISO if spi
30+
int32_t pin_3; // CS if SPI, nothing if PWM
31+
bool is_spi; // boolean if spi or pwm
2732
};
2833

2934
/**
3035
* @param valve Valve name to actuate as defined in the Valve enum
3136
* @param actuated True if providing power to valve, False if unpowering valve
32-
*
37+
* @param errc Error code pointer to store any error that occurs
38+
*
3339
* @note The actuated parameter is done this way (instead of open/closed) because some
3440
* valves may have a different default state and it would become confusing in software
3541
* to need to track which ones need power or a lack of power to reach a commanded state
3642
*/
37-
tal_err_t set_valve(enum Valve valve, bool actuated){
38-
39-
}
43+
void set_valve(struct valve_t* valve, bool actuated, enum ti_errc_t* errc);
File renamed without changes.

0 commit comments

Comments
 (0)