summaryrefslogtreecommitdiffstats
path: root/pressure_control/firmware
diff options
context:
space:
mode:
Diffstat (limited to 'pressure_control/firmware')
-rw-r--r--pressure_control/firmware/main.c9
-rw-r--r--pressure_control/firmware/valves.c47
-rw-r--r--pressure_control/firmware/valves.h75
3 files changed, 90 insertions, 41 deletions
diff --git a/pressure_control/firmware/main.c b/pressure_control/firmware/main.c
index d39008d..b6f8220 100644
--- a/pressure_control/firmware/main.c
+++ b/pressure_control/firmware/main.c
@@ -49,8 +49,13 @@ static uint8_t sensor_cycle;
/* The hardware definitions. */
-DEFINE_VALVE(z_control_valves, VALVES_2MAG, D, 6, 7, 4, 5, 0);
-DEFINE_VALVE(xy_control_valves, VALVES_1MAG, C, 2, -1, 3, -1, 400);
+DEFINE_VALVE_2MAG(z_control_valves,
+ D,6, D,7,
+ D,4, D,5,
+ 0);
+DEFINE_VALVE_1MAG(xy_control_valves,
+ C,2, C,3,
+ 400);
static DEFINE_SENSOR(z_control_sensor, 0, 245, 4400, 10000);
static DEFINE_SENSOR(xy_control_sensor, (1<<MUX0), 245, 4400, 10000);
diff --git a/pressure_control/firmware/valves.c b/pressure_control/firmware/valves.c
index 191842d..4860246 100644
--- a/pressure_control/firmware/valves.c
+++ b/pressure_control/firmware/valves.c
@@ -29,46 +29,44 @@
void valve0_switch(struct valves *v, uint8_t state)
{
- uint8_t p = MMIO8(v->port);
-
switch (v->type) {
case VALVES_1MAG:
if (state == VALVE_STATE_OPEN)
- p |= BITMASK8(v->bit_0_open);
+ MMIO8(v->open0.port) |= v->open0.bitmask;
else if (state == VALVE_STATE_CLOSE)
- p &= ~BITMASK8(v->bit_0_open);
+ MMIO8(v->open0.port) &= ~(v->open0.bitmask);
break;
case VALVES_2MAG:
- p &= ~(BITMASK8(v->bit_0_close) | BITMASK8(v->bit_0_open));
- if (state == VALVE_STATE_CLOSE)
- p |= BITMASK8(v->bit_0_close);
- else if (state == VALVE_STATE_OPEN)
- p |= BITMASK8(v->bit_0_open);
+ if (state == VALVE_STATE_CLOSE) {
+ MMIO8(v->open0.port) &= ~(v->open0.bitmask);
+ MMIO8(v->close0.port) |= v->close0.bitmask;
+ } else if (state == VALVE_STATE_OPEN) {
+ MMIO8(v->close0.port) &= ~(v->close0.bitmask);
+ MMIO8(v->open0.port) |= v->open0.bitmask;
+ }
break;
}
- MMIO8(v->port) = p;
}
void valve1_switch(struct valves *v, uint8_t state)
{
- uint8_t p = MMIO8(v->port);
-
switch (v->type) {
case VALVES_1MAG:
if (state == VALVE_STATE_OPEN)
- p |= BITMASK8(v->bit_1_open);
+ MMIO8(v->open1.port) |= v->open1.bitmask;
else if (state == VALVE_STATE_CLOSE)
- p &= ~BITMASK8(v->bit_1_open);
+ MMIO8(v->open1.port) &= ~(v->open1.bitmask);
break;
case VALVES_2MAG:
- p &= ~(BITMASK8(v->bit_1_close) | BITMASK8(v->bit_1_open));
- if (state == VALVE_STATE_CLOSE)
- p |= BITMASK8(v->bit_1_close);
- else if (state == VALVE_STATE_OPEN)
- p |= BITMASK8(v->bit_1_open);
+ if (state == VALVE_STATE_CLOSE) {
+ MMIO8(v->open1.port) &= ~(v->open1.bitmask);
+ MMIO8(v->close1.port) |= v->close1.bitmask;
+ } else if (state == VALVE_STATE_OPEN) {
+ MMIO8(v->close1.port) &= ~(v->close1.bitmask);
+ MMIO8(v->open1.port) |= v->open1.bitmask;
+ }
break;
}
- MMIO8(v->port) = p;
}
void valves_global_switch(struct valves *v, uint8_t state)
@@ -112,11 +110,14 @@ static inline void valves_ddr_setup(struct valves *v)
{
switch (v->type) {
case VALVES_1MAG:
- MMIO8(v->ddr) |= BITMASK8(v->bit_0_open) | BITMASK8(v->bit_1_open);
+ MMIO8(v->open0.ddr) |= v->open0.bitmask;
+ MMIO8(v->open1.ddr) |= v->open1.bitmask;
break;
case VALVES_2MAG:
- MMIO8(v->ddr) |= BITMASK8(v->bit_0_close) | BITMASK8(v->bit_0_open) |
- BITMASK8(v->bit_1_close) | BITMASK8(v->bit_1_open);
+ MMIO8(v->open0.ddr) |= v->open0.bitmask;
+ MMIO8(v->open1.ddr) |= v->open1.bitmask;
+ MMIO8(v->close0.ddr) |= v->close0.bitmask;
+ MMIO8(v->close1.ddr) |= v->close1.bitmask;
break;
}
}
diff --git a/pressure_control/firmware/valves.h b/pressure_control/firmware/valves.h
index ddc043e..977120e 100644
--- a/pressure_control/firmware/valves.h
+++ b/pressure_control/firmware/valves.h
@@ -26,14 +26,19 @@ enum valves_type {
VALVES_2MAG, /* One magnet for opening; one magnet for closing the valve. */
};
+struct valve_coil {
+ uint16_t ddr;
+ uint16_t port;
+ uint8_t bitmask;
+};
+
struct valves {
uint8_t type; /* enum valves_type */
- uint16_t ddr; /* DDRx */
- uint16_t port; /* PORTx */
- uint8_t bit_0_open; /* Pin for opening valve 0. */
- uint8_t bit_0_close; /* Pin for closing valve 0. */
- uint8_t bit_1_open; /* Pin for opening valve 1. */
- uint8_t bit_1_close; /* Pin for closing valve 1. */
+
+ struct valve_coil open0; /* Coil for opening valve 0 */
+ struct valve_coil close0; /* Coil for closing valve 0 */
+ struct valve_coil open1; /* Coil for opening valve 1 */
+ struct valve_coil close1; /* Coil for closing valve 1 */
uint8_t current_global_state; /* enum valves_global_state */
bool need_switch_to_idle; /* Need transition to VALVE_STATE_IDLE. */
@@ -42,16 +47,54 @@ struct valves {
uint16_t state_force_threshold; /* Threshold for forcing the valve state */
};
-#define DEFINE_VALVE(name, _type, portid, bit0_open, bit0_close, bit1_open, bit1_close, force_thres) \
- struct valves name = { \
- .type = _type, \
- .ddr = _SFR_ADDR(DDR##portid), \
- .port = _SFR_ADDR(PORT##portid), \
- .bit_0_open = bit0_open, \
- .bit_0_close = bit0_close, \
- .bit_1_open = bit1_open, \
- .bit_1_close = bit1_close, \
- .state_force_threshold = force_thres, \
+#define DEFINE_VALVE_1MAG(name, \
+ port_0open, bit_0open, \
+ port_1open, bit_1open, \
+ force_thres) \
+ struct valves name = { \
+ .type = VALVES_2MAG, \
+ .state_force_threshold = force_thres, \
+ .open0 = { \
+ .ddr = _SFR_ADDR(DDR##port_0open), \
+ .port = _SFR_ADDR(PORT##port_0open), \
+ .bitmask = (1 << (bit_0open)), \
+ }, \
+ .open1 = { \
+ .ddr = _SFR_ADDR(DDR##port_1open), \
+ .port = _SFR_ADDR(PORT##port_1open), \
+ .bitmask = (1 << (bit_1open)), \
+ }, \
+ }
+
+#define DEFINE_VALVE_2MAG(name, \
+ port_0open, bit_0open, \
+ port_0close, bit_0close, \
+ port_1open, bit_1open, \
+ port_1close, bit_1close, \
+ force_thres) \
+ struct valves name = { \
+ .type = VALVES_2MAG, \
+ .state_force_threshold = force_thres, \
+ .open0 = { \
+ .ddr = _SFR_ADDR(DDR##port_0open), \
+ .port = _SFR_ADDR(PORT##port_0open), \
+ .bitmask = (1 << (bit_0open)), \
+ }, \
+ .close0 = { \
+ .ddr = _SFR_ADDR(DDR##port_0close), \
+ .port = _SFR_ADDR(PORT##port_0close), \
+ .bitmask = (1 << (bit_0close)), \
+ }, \
+ .open1 = { \
+ .ddr = _SFR_ADDR(DDR##port_1open), \
+ .port = _SFR_ADDR(PORT##port_1open), \
+ .bitmask = (1 << (bit_1open)), \
+ }, \
+ .close1 = { \
+ .ddr = _SFR_ADDR(DDR##port_1close), \
+ .port = _SFR_ADDR(PORT##port_1close), \
+ .bitmask = (1 << (bit_1close)), \
+ }, \
}
#define VALVE_TOGGLE_MSEC 10
bues.ch cgit interface