summaryrefslogtreecommitdiffstats
path: root/pressure_control/firmware
diff options
context:
space:
mode:
Diffstat (limited to 'pressure_control/firmware')
-rw-r--r--pressure_control/firmware/main.c37
-rw-r--r--pressure_control/firmware/main.h1
-rw-r--r--pressure_control/firmware/remote.c79
-rw-r--r--pressure_control/firmware/remote.h20
4 files changed, 106 insertions, 31 deletions
diff --git a/pressure_control/firmware/main.c b/pressure_control/firmware/main.c
index 3dab38e..ef4c81e 100644
--- a/pressure_control/firmware/main.c
+++ b/pressure_control/firmware/main.c
@@ -58,6 +58,15 @@ void get_pressure_config(struct pressure_config *ret)
irq_restore(sreg);
}
+void set_pressure_config(struct pressure_config *new_cfg)
+{
+ uint8_t sreg;
+
+ sreg = irq_disable_save();
+ memcpy(&cfg, new_cfg, sizeof(cfg));
+ irq_restore(sreg);
+}
+
void get_pressure_state(struct pressure_state *ret)
{
uint8_t sreg;
@@ -140,21 +149,21 @@ static void check_pressure(void)
uint16_t abs_offset;
bool is_too_big;
- if (!cfg.autoadjust_enable)
- return;
-
- offset = (int32_t)state.mbar - (int32_t)cfg.desired;
- abs_offset = abs(offset);
- is_too_big = (offset >= 0);
-
- if (abs_offset > cfg.hysteresis) {
- /* Adjust the pressure */
- adjust_pressure(abs_offset, !is_too_big);
- } else {
- /* The pressure is OK. Make sure the valves are
- * all idle. */
- valves_force_state(VALVES_IDLE);
+ if (cfg.autoadjust_enable) {
+ offset = (int32_t)state.mbar - (int32_t)cfg.desired;
+ abs_offset = abs(offset);
+ is_too_big = (offset >= 0);
+
+ if (abs_offset > cfg.hysteresis) {
+ /* Adjust the pressure */
+ adjust_pressure(abs_offset, !is_too_big);
+ } else {
+ /* The pressure is OK. Make sure the valves are
+ * all idle. */
+ valves_force_state(VALVES_IDLE);
+ }
}
+//FIXME remote_pressure_change_notification(state.mbar, cfg.hysteresis);
}
int main(void)
diff --git a/pressure_control/firmware/main.h b/pressure_control/firmware/main.h
index abdfa8f..9f5cef1 100644
--- a/pressure_control/firmware/main.h
+++ b/pressure_control/firmware/main.h
@@ -31,6 +31,7 @@ struct pressure_state {
};
void get_pressure_config(struct pressure_config *cfg);
+void set_pressure_config(struct pressure_config *cfg);
void get_pressure_state(struct pressure_state *state);
#endif /* MAIN_H_ */
diff --git a/pressure_control/firmware/remote.c b/pressure_control/firmware/remote.c
index d759e7a..b012b4a 100644
--- a/pressure_control/firmware/remote.c
+++ b/pressure_control/firmware/remote.c
@@ -89,30 +89,21 @@ static void send_message(struct remote_message *msg)
usart_tx_buf(msg, sizeof(*msg));
}
-static void send_message_error(uint8_t error_code)
-{
- struct remote_message msg;
-
- memset(&msg, 0, sizeof(msg));
- msg.id = MSG_ERROR;
- msg.error.code = error_code;
-
- send_message(&msg);
-}
-
static void handle_received_message(void)
{
struct remote_message reply;
uint16_t calc_crc;
+ uint8_t err = MSG_ERR_NONE;
calc_crc = crc16_block_update(0xFFFF, &rx_msg,
sizeof(rx_msg) - sizeof(rx_msg.crc));
calc_crc ^= 0xFFFF;
if (calc_crc != rx_msg.crc) {
/* CRC mismatch. */
- send_message_error(MSG_ERR_CHKSUM);
- return;
+ err = MSG_ERR_CHKSUM;
+ goto out;
}
+
memset(&reply, 0, sizeof(reply));
switch (rx_msg.id) {
@@ -156,7 +147,49 @@ static void handle_received_message(void)
reply.config.flags |= (1 << CFG_FLAG_AUTOADJUST_ENABLE);
send_message(&reply);
break;
- } }
+ }
+ case MSG_SET_DESIRED_PRESSURE: {
+ struct pressure_config conf;
+
+ cli();
+ get_pressure_config(&conf);
+ conf.desired = rx_msg.pressure.mbar;
+ set_pressure_config(&conf);
+ sei();
+ break;
+ }
+ case MSG_SET_HYSTERESIS: {
+ struct pressure_config conf;
+
+ cli();
+ get_pressure_config(&conf);
+ conf.hysteresis = rx_msg.pressure.mbar;
+ set_pressure_config(&conf);
+ sei();
+ break;
+ }
+ case MSG_SET_CONFIG_FLAGS: {
+ struct pressure_config conf;
+
+ cli();
+ get_pressure_config(&conf);
+ conf.autoadjust_enable = !!(rx_msg.config.flags & (1 << CFG_FLAG_AUTOADJUST_ENABLE));
+ set_pressure_config(&conf);
+ sei();
+ break;
+ }
+ default:
+ err = MSG_ERR_NOCMD;
+ break;
+ }
+
+out:
+ if (rx_msg.flags & (1 << MSG_FLAG_REQ_ERRCODE)) {
+ memset(&reply, 0, sizeof(reply));
+ reply.id = MSG_ERROR;
+ reply.error.code = err;
+ send_message(&reply);
+ }
}
/* RX interrupt */
@@ -182,6 +215,7 @@ ISR(USART_RXC_vect)
rx_msg_count = 0;
mb();
rx_msg_valid = 1;
+ break;
}
}
}
@@ -217,6 +251,23 @@ void remote_work(void)
}
}
+void remote_pressure_change_notification(uint16_t mbar,
+ uint16_t hysteresis)
+{
+ struct remote_message msg;
+
+ static uint16_t prev_value;
+
+ if (abs((int32_t)mbar - (int32_t)prev_value) <= hysteresis)
+ return;
+ prev_value = mbar;
+
+ memset(&msg, 0, sizeof(msg));
+ msg.id = MSG_CURRENT_PRESSURE;
+ msg.pressure.mbar = mbar;
+ send_message(&msg);
+}
+
static void usart_init(void)
{
uint8_t dummy;
diff --git a/pressure_control/firmware/remote.h b/pressure_control/firmware/remote.h
index af18abf..46d35ff 100644
--- a/pressure_control/firmware/remote.h
+++ b/pressure_control/firmware/remote.h
@@ -23,11 +23,17 @@ enum remote_message_id {
MSG_GET_CONFIG_FLAGS,
MSG_CONFIG_FLAGS,
MSG_SET_CONFIG_FLAGS,
+ MSG_SET_VALVE,
};
enum remote_message_error {
- MSG_ERR_NONE = 0,
- MSG_ERR_CHKSUM,
+ MSG_ERR_NONE = 0, /* No error */
+ MSG_ERR_CHKSUM, /* Checksum error */
+ MSG_ERR_NOCMD, /* Unknown command */
+};
+
+enum remote_message_flags {
+ MSG_FLAG_REQ_ERRCODE = 0,
};
enum remote_message_config_flags {
@@ -36,7 +42,8 @@ enum remote_message_config_flags {
struct remote_message {
uint8_t id;
- uint8_t __padding0[3];
+ uint8_t flags;
+ uint8_t __padding0[2];
union {
struct {
@@ -51,6 +58,10 @@ struct remote_message {
struct {
uint32_t flags;
} __attribute__((packed)) config;
+ struct {
+ uint8_t nr; /* Valve ID */
+ uint8_t state;
+ } __attribute__((packed)) valve;
uint8_t __padding1[32];
} __attribute__((packed));
@@ -62,6 +73,9 @@ struct remote_message {
void print_pgm(const prog_char *msg);
#define print(string_literal) print_pgm(PSTR(string_literal))
+void remote_pressure_change_notification(uint16_t mbar,
+ uint16_t hysteresis);
+
void remote_work(void);
void remote_init(void);
bues.ch cgit interface