summaryrefslogtreecommitdiffstats
path: root/pressure_control/firmware
diff options
context:
space:
mode:
authorMichael Buesch <mb@bu3sch.de>2009-06-13 15:04:02 +0200
committerMichael Buesch <mb@bu3sch.de>2009-06-13 15:04:02 +0200
commitfc3276d57c54af5929b1f02f2c271058e025d163 (patch)
tree62d07d582db2538aaf144019370f452eaaed2119 /pressure_control/firmware
parentf78d0963af065e55a64c383d2ad8b00247f75977 (diff)
downloadcnc-fc3276d57c54af5929b1f02f2c271058e025d163.tar.xz
cnc-fc3276d57c54af5929b1f02f2c271058e025d163.zip
pressure_control: Add support for two control circuits
Signed-off-by: Michael Buesch <mb@bu3sch.de>
Diffstat (limited to 'pressure_control/firmware')
-rw-r--r--pressure_control/firmware/main.c72
-rw-r--r--pressure_control/firmware/main.h16
-rw-r--r--pressure_control/firmware/remote.c120
-rw-r--r--pressure_control/firmware/remote.h17
4 files changed, 149 insertions, 76 deletions
diff --git a/pressure_control/firmware/main.c b/pressure_control/firmware/main.c
index 65443cc..0534231 100644
--- a/pressure_control/firmware/main.c
+++ b/pressure_control/firmware/main.c
@@ -33,18 +33,20 @@
struct eeprom_data {
- struct pressure_config cfg;
+ struct pressure_config cfg_xy;
+ struct pressure_config cfg_z;
};
/* The pressure configuration data. */
-struct pressure_config cfg;
+struct pressure_config cfg_xy;
+struct pressure_config cfg_z;
/* The pressure state data. */
struct pressure_state state;
/* The 1000Hz jiffies counter */
static jiffies_t jiffies_counter;
DEFINE_VALVE(xy_control_valves, VALVES_2MAG, D, 6, 7, 4, 5);
-DEFINE_VALVE(z_control_valves, VALVES_1MAG, C, 2, 3, 4, 5);
+DEFINE_VALVE(z_control_valves, VALVES_1MAG, C, 2, -1, 3, -1);
static DEFINE_SENSOR(xy_control_sensor, 0, 245, 4400, 10000);
static DEFINE_SENSOR(z_control_sensor, (1<<MUX0), 245, 4400, 10000);
@@ -53,9 +55,14 @@ static uint8_t sensor_cycle;
/* EEPROM contents */
static struct eeprom_data EEMEM eeprom = {
- .cfg = {
+ .cfg_xy = {
.desired = 2500, /* Millibar */
- .hysteresis = 200, /* Millibar */
+ .hysteresis = 150, /* Millibar */
+ .autoadjust_enable = 1,
+ },
+ .cfg_z = {
+ .desired = 1500, /* Millibar */
+ .hysteresis = 150, /* Millibar */
.autoadjust_enable = 1,
},
};
@@ -65,7 +72,8 @@ static struct eeprom_data EEMEM eeprom = {
static void eeprom_load_config(void)
{
eeprom_busy_wait();
- eeprom_read_block(&cfg, &eeprom.cfg, sizeof(cfg));
+ eeprom_read_block(&cfg_xy, &eeprom.cfg_xy, sizeof(cfg_xy));
+ eeprom_read_block(&cfg_z, &eeprom.cfg_z, sizeof(cfg_z));
eeprom_busy_wait();
}
@@ -73,25 +81,30 @@ static void eeprom_load_config(void)
static void eeprom_store_config(void)
{
eeprom_busy_wait();
- eeprom_write_block(&cfg, &eeprom.cfg, sizeof(cfg));
+ eeprom_write_block(&cfg_xy, &eeprom.cfg_xy, sizeof(cfg_xy));
+ eeprom_write_block(&cfg_z, &eeprom.cfg_z, sizeof(cfg_z));
eeprom_busy_wait();
}
-void get_pressure_config(struct pressure_config *ret)
+void get_pressure_config(struct pressure_config *xy,
+ struct pressure_config *z)
{
uint8_t sreg;
sreg = irq_disable_save();
- memcpy(ret, &cfg, sizeof(*ret));
+ memcpy(xy, &cfg_xy, sizeof(*xy));
+ memcpy(z, &cfg_z, sizeof(*z));
irq_restore(sreg);
}
-void set_pressure_config(struct pressure_config *new_cfg)
+void set_pressure_config(struct pressure_config *new_xy,
+ struct pressure_config *new_z)
{
uint8_t sreg;
sreg = irq_disable_save();
- memcpy(&cfg, new_cfg, sizeof(cfg));
+ memcpy(&cfg_xy, new_xy, sizeof(cfg_xy));
+ memcpy(&cfg_z, new_z, sizeof(cfg_z));
eeprom_store_config();
irq_restore(sreg);
}
@@ -143,7 +156,10 @@ void system_timer_init(void)
/* Check the current pressure value against the desired value and
* adjust the pressure if needed. */
-static void do_check_pressure(struct valves *valves, uint16_t mbar)
+static void do_check_pressure(struct valves *valves,
+ struct pressure_config *cfg,
+ uint16_t mbar,
+ uint16_t *reported_mbar)
{
int32_t offset;
uint16_t abs_offset;
@@ -151,20 +167,20 @@ static void do_check_pressure(struct valves *valves, uint16_t mbar)
bool report_change = 0;
uint8_t cur_state;
- if (cfg.autoadjust_enable) {
- offset = (int32_t)mbar - (int32_t)cfg.desired;
+ if (cfg->autoadjust_enable) {
+ offset = (int32_t)mbar - (int32_t)cfg->desired;
abs_offset = abs(offset);
is_too_big = (offset >= 0);
cur_state = valves_get_global_state(valves);
- if (abs_offset > cfg.hysteresis) {
+ if (abs_offset > cfg->hysteresis) {
/* Adjust the pressure */
report_change = (cur_state == VALVES_IDLE);
if (is_too_big)
valves_global_switch(valves, VALVES_FLOW_OUT);
else
valves_global_switch(valves, VALVES_FLOW_IN);
- } else if (abs_offset > cfg.hysteresis / 4) {
+ } else if (abs_offset > cfg->hysteresis / 4) {
/* If we're idle, stay idle.
* If we're increasing or decreasing pressure,
* keep on doing this to reach the desired center value
@@ -186,11 +202,12 @@ static void do_check_pressure(struct valves *valves, uint16_t mbar)
valves_disarm_auto_idle(valves);
}
}
- if (abs((int32_t)mbar - (int32_t)state.reported_mbar) >= 100)
+ if (abs((int32_t)mbar - (int32_t)(*reported_mbar)) >= 100)
report_change = 1;
if (report_change) {
- remote_pressure_change_notification(mbar);
- state.reported_mbar = mbar;
+ remote_pressure_change_notification(state.measured_mbar_xy,
+ state.measured_mbar_z);
+ *reported_mbar = mbar;
}
}
@@ -198,10 +215,20 @@ static void check_pressure(void)
{
switch (sensor_cycle) {
case SENSOR_CYCLE_XY:
- do_check_pressure(&xy_control_valves, state.measured_mbar);
+ state.measured_mbar_xy = state.measured_mbar;
+ if (state.device_enabled) {
+ do_check_pressure(&xy_control_valves, &cfg_xy,
+ state.measured_mbar_xy,
+ &state.reported_mbar_xy);
+ }
break;
case SENSOR_CYCLE_Z:
- do_check_pressure(&z_control_valves, state.measured_mbar);
+ state.measured_mbar_z = state.measured_mbar;
+ if (state.device_enabled) {
+ do_check_pressure(&z_control_valves, &cfg_z,
+ state.measured_mbar_z,
+ &state.reported_mbar_z);
+ }
break;
default:
BUG_ON(1);
@@ -281,8 +308,7 @@ int main(void)
mb();
now = get_jiffies();
if (state.needs_checking) {
- if (state.device_enabled)
- check_pressure();
+ check_pressure();
/* Trigger another measurement in
* a few milliseconds. */
if (++sensor_cycle == __NR_SENSOR_CYCLE)
diff --git a/pressure_control/firmware/main.h b/pressure_control/firmware/main.h
index 279b7e0..28b15c1 100644
--- a/pressure_control/firmware/main.h
+++ b/pressure_control/firmware/main.h
@@ -44,10 +44,16 @@ struct pressure_config {
struct pressure_state {
/* Sensing and adjustment logic enabled? */
bool device_enabled;
- /* The last measured pressure (in mBar) */
+ /* The last measured pressure (in mBar).
+ * It depends on sensor_cycle which valves this
+ * value belongs to. */
uint16_t measured_mbar;
+ /* The current pressure for the individual valves */
+ uint16_t measured_mbar_xy;
+ uint16_t measured_mbar_z;
/* Reported pressure via RS232 */
- uint16_t reported_mbar;
+ uint16_t reported_mbar_xy;
+ uint16_t reported_mbar_z;
/* True, if the current pressure value needs checking against
* the desired pressure config. */
bool needs_checking;
@@ -58,8 +64,10 @@ struct pressure_state {
int8_t sensor_trigger_cnt;
};
-void get_pressure_config(struct pressure_config *cfg);
-void set_pressure_config(struct pressure_config *cfg);
+void get_pressure_config(struct pressure_config *xy,
+ struct pressure_config *z);
+void set_pressure_config(struct pressure_config *xy,
+ struct pressure_config *z);
void get_pressure_state(struct pressure_state *state);
void prepare_turn_on(void);
void prepare_shutdown(void);
diff --git a/pressure_control/firmware/remote.c b/pressure_control/firmware/remote.c
index b6d6141..1945ef4 100644
--- a/pressure_control/firmware/remote.c
+++ b/pressure_control/firmware/remote.c
@@ -29,8 +29,8 @@
#include <avr/io.h>
-#define BAUDRATE 38400 /* Error = 0.2% */
-#define USE_2X 0
+#define BAUDRATE 115200
+#define USE_2X 1
static struct remote_message rx_msg;
@@ -119,95 +119,123 @@ static void handle_received_message(void)
case MSG_GET_CURRENT_PRESSURE: {
struct pressure_state state;
-//FIXME
get_pressure_state(&state);
reply.id = MSG_CURRENT_PRESSURE;
- reply.pressure.mbar = state.measured_mbar;
+ reply.pressure.mbar[0] = state.measured_mbar_xy;
+ reply.pressure.mbar[1] = state.measured_mbar_z;
send_message(&reply);
break;
}
case MSG_GET_DESIRED_PRESSURE: {
- struct pressure_config conf;
+ struct pressure_config xy, z;
- get_pressure_config(&conf);
+ get_pressure_config(&xy, &z);
reply.id = MSG_DESIRED_PRESSURE;
- reply.pressure.mbar = conf.desired;
+ reply.pressure.mbar[0] = xy.desired;
+ reply.pressure.mbar[1] = z.desired;
send_message(&reply);
break;
}
case MSG_GET_HYSTERESIS: {
- struct pressure_config conf;
+ struct pressure_config xy, z;
- get_pressure_config(&conf);
+ get_pressure_config(&xy, &z);
reply.id = MSG_HYSTERESIS;
- reply.pressure.mbar = conf.hysteresis;
+ reply.pressure.mbar[0] = xy.hysteresis;
+ reply.pressure.mbar[1] = z.hysteresis;
send_message(&reply);
break;
}
case MSG_GET_CONFIG_FLAGS: {
- struct pressure_config conf;
+ struct pressure_config xy, z;
- get_pressure_config(&conf);
+ get_pressure_config(&xy, &z);
reply.id = MSG_CONFIG_FLAGS;
- if (conf.autoadjust_enable)
- reply.config.flags |= (1 << CFG_FLAG_AUTOADJUST_ENABLE);
+ if (xy.autoadjust_enable)
+ reply.config.flags[0] |= (1 << CFG_FLAG_AUTOADJUST_ENABLE);
+ if (z.autoadjust_enable)
+ reply.config.flags[1] |= (1 << CFG_FLAG_AUTOADJUST_ENABLE);
send_message(&reply);
break;
}
case MSG_SET_DESIRED_PRESSURE: {
- struct pressure_config conf;
+ struct pressure_config xy, z;
cli();
- get_pressure_config(&conf);
- conf.desired = rx_msg.pressure.mbar;
- set_pressure_config(&conf);
+ get_pressure_config(&xy, &z);
+ if (rx_msg.setpressure.island == 0)
+ xy.desired = rx_msg.setpressure.mbar;
+ else if (rx_msg.setpressure.island == 1)
+ z.desired = rx_msg.setpressure.mbar;
+ set_pressure_config(&xy, &z);
sei();
break;
}
case MSG_SET_HYSTERESIS: {
- struct pressure_config conf;
+ struct pressure_config xy, z;
cli();
- get_pressure_config(&conf);
- conf.hysteresis = rx_msg.pressure.mbar;
- set_pressure_config(&conf);
+ get_pressure_config(&xy, &z);
+ if (rx_msg.setpressure.island == 0)
+ xy.hysteresis = rx_msg.setpressure.mbar;
+ else if (rx_msg.setpressure.island == 1)
+ z.hysteresis = rx_msg.setpressure.mbar;
+ set_pressure_config(&xy, &z);
sei();
break;
}
case MSG_SET_CONFIG_FLAGS: {
- struct pressure_config conf;
+ struct pressure_config xy, z;
bool flag;
cli();
- get_pressure_config(&conf);
- flag = !!(rx_msg.config.flags & (1 << CFG_FLAG_AUTOADJUST_ENABLE));
- if (conf.autoadjust_enable != flag) {
- conf.autoadjust_enable = flag;
- /* Make sure the values are idle. */
- valves_global_switch(&xy_control_valves, VALVES_IDLE);
+ get_pressure_config(&xy, &z);
+ flag = !!(rx_msg.setconfig.flags & (1 << CFG_FLAG_AUTOADJUST_ENABLE));
+ if (rx_msg.setconfig.island == 0) {
+ if (xy.autoadjust_enable != flag) {
+ xy.autoadjust_enable = flag;
+ /* Make sure the values are idle. */
+ valves_global_switch(&xy_control_valves, VALVES_IDLE);
+ }
+ } else if (rx_msg.setconfig.island == 1) {
+ if (z.autoadjust_enable != flag) {
+ z.autoadjust_enable = flag;
+ /* Make sure the values are idle. */
+ valves_global_switch(&z_control_valves, VALVES_IDLE);
+ }
}
- set_pressure_config(&conf);
+ set_pressure_config(&xy, &z);
sei();
break;
}
case MSG_SET_VALVE: {
- struct pressure_config conf;
+ struct pressure_config xy, z;
+ struct valves *v;
- get_pressure_config(&conf);
- if (conf.autoadjust_enable) {
- err = MSG_ERR_BUSY;
- break;
+ get_pressure_config(&xy, &z);
+ if (rx_msg.valve.island == 0) {
+ if (xy.autoadjust_enable) {
+ err = MSG_ERR_BUSY;
+ break;
+ }
+ v = &xy_control_valves;
+ } else {
+ if (z.autoadjust_enable) {
+ err = MSG_ERR_BUSY;
+ break;
+ }
+ v = &z_control_valves;
}
if (rx_msg.valve.nr == 0) {
- valve0_switch(&xy_control_valves, rx_msg.valve.state == 0 ?
+ valve0_switch(v, rx_msg.valve.state == 0 ?
VALVE_STATE_CLOSE : VALVE_STATE_OPEN);
- valve_wait_toggle(&xy_control_valves);
- valve0_switch(&xy_control_valves, VALVE_STATE_IDLE);
+ valve_wait_toggle(v);
+ valve0_switch(v, VALVE_STATE_IDLE);
} else if (rx_msg.valve.nr == 1) {
- valve1_switch(&xy_control_valves, rx_msg.valve.state == 0 ?
+ valve1_switch(v, rx_msg.valve.state == 0 ?
VALVE_STATE_CLOSE : VALVE_STATE_OPEN);
- valve_wait_toggle(&xy_control_valves);
- valve0_switch(&xy_control_valves, VALVE_STATE_IDLE);
+ valve_wait_toggle(v);
+ valve0_switch(v, VALVE_STATE_IDLE);
} else
err = MSG_ERR_INVAL;
break;
@@ -215,6 +243,7 @@ static void handle_received_message(void)
case MSG_SHUTDOWN:
prepare_shutdown();
valves_shutdown(&xy_control_valves);
+ valves_shutdown(&z_control_valves);
break;
case MSG_TURNON:
prepare_turn_on();
@@ -383,13 +412,14 @@ void remote_work(void)
sei();
}
-void remote_pressure_change_notification(uint16_t mbar)
+void remote_pressure_change_notification(uint16_t xy_mbar, uint16_t z_mbar)
{
struct remote_message msg;
memset(&msg, 0, sizeof(msg));
msg.id = MSG_CURRENT_PRESSURE;
- msg.pressure.mbar = mbar;
+ msg.pressure.mbar[0] = xy_mbar;
+ msg.pressure.mbar[1] = z_mbar;
send_message(&msg);
}
@@ -418,8 +448,8 @@ static void usart_init(void)
#if USE_2X
UCSRA = (1 << U2X);
#endif
- /* 8 Data bits, 1 Stop bit, No parity */
- UCSRC = (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1);
+ /* 8 Data bits, 2 Stop bits, No parity */
+ UCSRC = (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1) | (1 << USBS);
/* Enable transceiver and RX IRQs */
UCSRB = (1 << RXEN) | (1 << TXEN) | (1 << RXCIE);
/* Drain the RX buffer */
diff --git a/pressure_control/firmware/remote.h b/pressure_control/firmware/remote.h
index 709a1f0..c587782 100644
--- a/pressure_control/firmware/remote.h
+++ b/pressure_control/firmware/remote.h
@@ -57,13 +57,22 @@ struct remote_message {
char str[8];
} __attribute__((packed)) logmessage;
struct {
- uint16_t mbar;
+ uint16_t mbar[2];
} __attribute__((packed)) pressure;
struct {
- uint32_t flags;
+ uint8_t island; /* Valve island */
+ uint16_t mbar;
+ } __attribute__((packed)) setpressure;
+ struct {
+ uint32_t flags[2];
} __attribute__((packed)) config;
struct {
- uint8_t nr; /* Valve ID */
+ uint8_t island; /* Valve island */
+ uint32_t flags;
+ } __attribute__((packed)) setconfig;
+ struct {
+ uint8_t island; /* Valve island */
+ uint8_t nr; /* Valve ID in the island */
uint8_t state;
} __attribute__((packed)) valve;
@@ -81,7 +90,7 @@ void print_dec(uint16_t number);
void print_dec_signed(int16_t number);
void print_hex(uint8_t number);
-void remote_pressure_change_notification(uint16_t mbar);
+void remote_pressure_change_notification(uint16_t xy_mbar, uint16_t z_mbar);
void remote_notify_restart(void);
void remote_work(void);
bues.ch cgit interface