summaryrefslogtreecommitdiffstats
path: root/radiator-plc
diff options
context:
space:
mode:
authorfishsoupisgood <github@madingley.org>2021-01-12 16:58:31 +0000
committerfishsoupisgood <github@madingley.org>2021-01-12 16:58:31 +0000
commit7c6887eaaf812b63bab6c5e134f80a2ef36aeb31 (patch)
tree4b4b0d371107ae1b8540ca1618cb9aa796b72616 /radiator-plc
parentf4b573fe337a436d5e2b20be4be031d77376d609 (diff)
downloadheating-7c6887eaaf812b63bab6c5e134f80a2ef36aeb31.tar.gz
heating-7c6887eaaf812b63bab6c5e134f80a2ef36aeb31.tar.bz2
heating-7c6887eaaf812b63bab6c5e134f80a2ef36aeb31.zip
works
Diffstat (limited to 'radiator-plc')
-rw-r--r--radiator-plc/stm32/Makefile.rules2
-rw-r--r--radiator-plc/stm32/app/Makefile9
-rw-r--r--radiator-plc/stm32/app/control.c114
-rw-r--r--radiator-plc/stm32/app/logic.c178
-rw-r--r--radiator-plc/stm32/app/main.c4
-rw-r--r--radiator-plc/stm32/app/project.h1
-rw-r--r--radiator-plc/stm32/app/prototypes.h1
-rw-r--r--radiator-plc/stm32/app/rain.c100
-rw-r--r--radiator-plc/stm32/app/ticker.c5
9 files changed, 150 insertions, 264 deletions
diff --git a/radiator-plc/stm32/Makefile.rules b/radiator-plc/stm32/Makefile.rules
index a723e6f..cf67b1a 100644
--- a/radiator-plc/stm32/Makefile.rules
+++ b/radiator-plc/stm32/Makefile.rules
@@ -137,7 +137,7 @@ srec: $(BINARY).srec
list: $(BINARY).list
images: $(BINARY).images
-flash: $(BINARY).flash
+#flash: $(BINARY).flash
%.images: %.bin %.hex %.srec %.list %.map %.dfu
@#printf "*** $* images generated ***\n"
diff --git a/radiator-plc/stm32/app/Makefile b/radiator-plc/stm32/app/Makefile
index 8ef06f7..2ff2f51 100644
--- a/radiator-plc/stm32/app/Makefile
+++ b/radiator-plc/stm32/app/Makefile
@@ -20,7 +20,8 @@
CPROTO=cproto
PROG=radiator-plc
-D=radiator-plc0
+D=radiator-plc0 radiator-plc1
+#D=10.32.94.238
V=1
default: ${PROG}.elf
@@ -34,17 +35,19 @@ OBJS = ${CSRCS:%.c=%.o}
${OBJS}:
+
include ../Makefile.include
INCLUDES += -I..
+flash: ${PROG}.hex
+ for d in ${D}; do ssh $$d flash_stm32 < ${PROG}.hex; done
+
protos: ${CSRCS}
echo -n > prototypes.h
${CPROTO} $(INCLUDES) $(DEFINES) -e -v ${CSRCS} > prototypes.h.tmp
mv -f prototypes.h.tmp prototypes.h
-flash: ${PROG}.hex
- ssh ${D} flash_stm32 < ${PROG}.hex
local_flash: ${PROG}.hex
$(Q)$(OOCD) -f $(OOCD_INTERFACE) \
diff --git a/radiator-plc/stm32/app/control.c b/radiator-plc/stm32/app/control.c
deleted file mode 100644
index f3a35c9..0000000
--- a/radiator-plc/stm32/app/control.c
+++ /dev/null
@@ -1,114 +0,0 @@
-#include "project.h"
-
-
-static uint32_t target = 25000;
-static uint32_t value = ~0U;
-
-#define SHORT 200
-#define LONG 1200
-#define REPEAT 400
-
-#define HYST 75UL
-
-
-static void
-show_temp (char t, uint32_t v, int y)
-{
- char buf[16];
- uint32_t i, f;
-
- i = v / 1000;
- f = v / 100 - (i * 10);
-
- if (v == ~0U)
- {
- sprintf (buf, "%c:error ", t);
- }
- else
- {
- sprintf (buf, "%c:%2d.%1d%cC", t, (int) i, (int) f, 0xdf);
- }
-
- lcd_write (buf, 0, y);
-}
-
-void
-control_update (void)
-{
-
- show_temp ('P', value, 0);
- show_temp ('T', target, 1);
-
- if (value == ~0U)
- relay_off ();
- else if (value < (target - HYST))
- relay_on ();
- else if (value > (target + HYST))
- relay_off ();
-
-
-}
-
-void
-control_tick (void)
-{
- static int down, up, update;
-
- if (!gpio_get (GPIOB, GPIO4))
- down++;
- else
- down = 0;
-
- if (!gpio_get (GPIOB, GPIO6))
- up++;
- else
- up = 0;
-
- if ((down == SHORT) || (down == LONG))
- {
- if (target > 100)
- target -= 100;
- if (down == LONG)
- down = LONG - REPEAT;
- control_update ();
- }
-
- if ((up == SHORT) || (up == LONG))
- {
- if (target < 99900)
- target += 100;
- if (up == LONG)
- up = LONG - REPEAT;
- control_update ();
- }
-
- update++;
-
- if (update == 1000)
- {
- control_update ();
- update = 0;
- }
-}
-
-void
-control_report (uint32_t v)
-{
-
- value = v;
- control_update ();
-}
-
-void
-control_init (void)
-{
-
- gpio_set_mode (GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN,
- GPIO4 | GPIO6);
- gpio_set_mode (GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL,
- GPIO5);
- gpio_set (GPIOB, GPIO4 | GPIO6);
- gpio_clear (GPIOB, GPIO5);
-
-
-}
diff --git a/radiator-plc/stm32/app/logic.c b/radiator-plc/stm32/app/logic.c
index 8cab7ca..a9f845c 100644
--- a/radiator-plc/stm32/app/logic.c
+++ b/radiator-plc/stm32/app/logic.c
@@ -7,7 +7,7 @@ static volatile int semaphore = 0;
-static const char *valve_name[N_VALVES] = {
+static const char *plc0_valve_names[N_VALVES] = {
"dd_radiator1",
"dd_radiator2",
"dd_radiator3",
@@ -18,16 +18,57 @@ static const char *valve_name[N_VALVES] = {
"plc0_radiator7",
};
-static Onewire_addr valve_owa[N_VALVES] = {
- {{0x28, 0xe3, 0x5d, 0x56, 0xb5, 0x01, 0x3c, 0x96}},
- {{0x28, 0x0d, 0xaf, 0x56, 0xb5, 0x01, 0x3c, 0x89}},
- {{0x28, 0x79, 0xb1, 0x56, 0xb5, 0x01, 0x3c, 0x13}},
- {{0x28, 0x99, 0x8f, 0x56, 0xb5, 0x01, 0x3c, 0x1c}},
- {{0x28, 0xb4, 0x98, 0x6e, 0x32, 0x20, 0x01, 0xc7}},
- {{0x28, 0x0d, 0x4d, 0x56, 0xb5, 0x01, 0x3c, 0xd0}},
- {{0x28, 0xb1, 0x71, 0x75, 0x32, 0x20, 0x01, 0xa9}},
+static const Onewire_addr plc0_valve_owas[N_VALVES] = {
+ {{0x28, 0x09, 0xb3, 0xc3, 0x0b, 0x00, 0x00, 0xf9}},
+ {{0x28, 0x89, 0xda, 0xc3, 0x0b, 0x00, 0x00, 0xf1}},
+ {{0x28, 0xdf, 0xb2, 0xc6, 0x0b, 0x00, 0x00, 0xc3}},
+ {{0x28, 0x42, 0x23, 0xc4, 0x0b, 0x00, 0x00, 0x45}},
+ {{0x28, 0xf9, 0xd6, 0xc6, 0x0b, 0x00, 0x00, 0xc9}},
+ {{0x28, 0x8d, 0x0a, 0xc6, 0x0b, 0x00, 0x00, 0xf2}},
+ {{0x28, 0x5f, 0xb7, 0xc6, 0x0b, 0x00, 0x00, 0xfb}},
+ {{0x28, 0x25, 0xe5, 0xc6, 0x0b, 0x00, 0x00, 0xba}},
+};
+
+
+
+
+
+
+
+
+
+
+
+
+static const char *plc1_valve_names[N_VALVES] = {
+ "2fl_main_radiator",
+ "2fl_stair_radiator",
+ "plc1_radiator2",
+ "plc1_radiator3",
+ "plc1_radiator4",
+ "plc1_radiator5",
+ "plc1_radiator6",
+ "plc1_radiator7",
+};
+
+
+static const Onewire_addr plc1_valve_owas[N_VALVES] = {
+ {{0x28, 0x03, 0x01, 0xc5, 0x0b, 0x00, 0x00, 0xf3}},
+ {{0x28, 0xea, 0x79, 0xc5, 0x0b, 0x00, 0x00, 0x1f}},
+ {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
+ {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
+ {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
+ {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
+ {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
};
+
+
+Onewire_addr owa_zero = {0};
+
+
+static const char **valve_name;
+static const Onewire_addr *valve_owa;
volatile int temp[N_VALVES];
#define BAD_TEMP 0x7fffffff
@@ -56,80 +97,98 @@ int high_limit[N_VALVES] = {
};
int valve_state[N_VALVES];
+int failed_reads[N_VALVES];
+
+
+
+static float scale (int i)
+{
+ float ret;
+ ret = i;
+ return ret / 1000.;
+}
void mqtt_dispatch (char *type, char *who, char *what, char *msg)
{
unsigned i;
+ static char msg_buf[80];
if (strcmp (type, "cmnd")) return;
for (i = 0; i < N_VALVES; ++i) {
if (!strcmp (who, valve_name[i])) {
- if (!strcmp (what, "var1"))
+ if (!strcmp (what, "var1")) {
low_limit[i] = atoi (msg) * 1000;
+ usart1_queue_buf (msg_buf, sprintf (msg_buf, "\r\nMQTTR stat/%s/var1 %.2f\r\n", valve_name[i], scale (low_limit[i])));
+ }
- if (!strcmp (what, "var2"))
+ if (!strcmp (what, "var2")) {
high_limit[i] = atoi (msg) * 1000;
- }
- }
-}
-
+ usart1_queue_buf (msg_buf, sprintf (msg_buf, "\r\nMQTTR stat/%s/var2 %.2f\r\n", valve_name[i], scale (high_limit[i])));
+ }
-static float scale(int i)
-{
-float ret;
-ret=i;
-return ret/1000.;
+ }
+ }
}
void logic_tick (void)
{
static unsigned i;
static int read_failed = 1;
+ static int not_present = 1;
static char msg_buf[80];
int t;
int o;
int v;
+ if (!not_present) {
+ if (!read_failed)
+ read_failed = ds18b20_read (&valve_owa[i], &t);
+ if (read_failed) {
+ temp[i] = BAD_TEMP;
+ valve_state[i] = 0;
+ failed_reads[i]++;
+ } else {
+ temp[i] = t;
+ failed_reads[i] = 0;
- if (!read_failed)
- read_failed = ds18b20_read (&valve_owa[i], &t);
+ if (t < low_limit[i])
+ valve_state[i] = 1;
- if (read_failed) {
- temp[i] = BAD_TEMP;
- valve_state[i] = 0;
- } else {
- temp[i] = t;
+ if (t >= high_limit[i])
+ valve_state[i] = 0;
+ }
- if (t < low_limit[i])
- valve_state[i] = 1;
+ o = valve_state[i];
- if (t >= high_limit[i])
- valve_state[i] = 0;
- }
+ output_write (i, o);
+ v = input_get (i);
- o = valve_state[i];
+ usart1_queue_buf (msg_buf, sprintf (msg_buf, "\r\nMQTTR stat/%s/POWER %s\r\n", valve_name[i], o ? "ON" : "OFF"));
+ usart1_queue_buf (msg_buf, sprintf (msg_buf, "\r\nMQTTR stat/%s/OPEN %d\r\n", valve_name[i], v));
- output_write (i, o);
- v = input_get (i);
+ usart1_queue_buf (msg_buf, sprintf (msg_buf, "\r\nMQTTR stat/%s/var1 %.2f\r\n", valve_name[i], scale (low_limit[i])));
- usart1_queue_buf (msg_buf, sprintf (msg_buf, "\r\nMQTTR stat/%s/POWER %s\r\n", valve_name[i], o ? "ON" : "OFF"));
- usart1_queue_buf (msg_buf, sprintf (msg_buf, "\r\nMQTTR stat/%s/OPEN %d\r\n", valve_name[i], v));
+ usart1_queue_buf (msg_buf, sprintf (msg_buf, "\r\nMQTTR stat/%s/var2 %.2f\r\n", valve_name[i], scale (high_limit[i])));
- usart1_queue_buf (msg_buf, sprintf (msg_buf, "\r\nMQTTR stat/%s/var1 %.2f\r\n", valve_name[i], scale(low_limit[i])));
- usart1_queue_buf (msg_buf, sprintf (msg_buf, "\r\nMQTTR stat/%s/var2 %.2f\r\n", valve_name[i], scale(high_limit[i])));
+ if (!read_failed) {
+ usart1_queue_buf (msg_buf, sprintf (msg_buf, "\r\nMQTTR stat/%s/TEMPERATURE %.2f\r\n", valve_name[i], scale (temp[i])));
+ usart1_queue_buf (msg_buf, sprintf (msg_buf, "\r\nMQTTR stat/%s/DELTA %.2f\r\n", valve_name[i], scale (low_limit[i] - temp[i])));
+ }
+ usart1_queue_buf (msg_buf, sprintf (msg_buf, "\r\nMQTTR stat/%s/failed_reads %d\r\n", valve_name[i], failed_reads[i]));
- if (!read_failed) {
- usart1_queue_buf (msg_buf, sprintf (msg_buf, "\r\nMQTTR stat/%s/TEMPERATURE %.2f\r\n", valve_name[i], scale(temp[i])));
- usart1_queue_buf (msg_buf, sprintf (msg_buf, "\r\nMQTTR stat/%s/DELTA %.2f\r\n", valve_name[i], scale(low_limit[i]-temp[i])));
+ } else {
+ valve_state[i] = 0;
+ o = valve_state[i];
+ output_write (i, o);
}
@@ -143,7 +202,12 @@ void logic_tick (void)
onewire_search();
}
- read_failed = ds18b20_set_12_convert (&valve_owa[i]);
+ if (!memcmp (&valve_owa[i], &owa_zero, sizeof (owa_zero)))
+ not_present = 1;
+ else {
+ not_present = 0;
+ read_failed = ds18b20_set_12_convert (&valve_owa[i]);
+ }
}
@@ -156,3 +220,31 @@ void logic_slow_tick (void)
void logic_dispatch (void)
{
}
+
+
+void logic_init (void)
+{
+ char serial[25] = {0};
+ unsigned i;
+
+ desig_get_unique_id_as_string (serial, sizeof (serial));
+
+ if (!strcmp (serial, "325715574155303632FFD305")) {
+ valve_name = plc0_valve_names;
+ valve_owa = plc0_valve_owas;
+
+ for (i = 0; i < 3; ++i) {
+ low_limit[i] = 19000;
+ high_limit[i] = 20000;
+ }
+ } else if (!strcmp (serial, "378125574155383139FFD805")) {
+ valve_name = plc1_valve_names;
+ valve_owa = plc1_valve_owas;
+ } else {
+ printf ("serial: %s\n", serial);
+ valve_name = plc0_valve_names;
+ valve_owa = plc0_valve_owas;
+ }
+
+
+}
diff --git a/radiator-plc/stm32/app/main.c b/radiator-plc/stm32/app/main.c
index 57b6752..4a00a70 100644
--- a/radiator-plc/stm32/app/main.c
+++ b/radiator-plc/stm32/app/main.c
@@ -19,6 +19,7 @@ static void mqtt_cmd (char *type)
type++;
who = index (type, ' ');
+
if (who) *who = 0;
@@ -133,11 +134,10 @@ main (void)
onewire_init();
inputs_init();
outputs_init();
+ logic_init();
printf ("D: RESET\n");
- //onewire_search();
-
for (;;) {
if (!ring_empty (&rx1_ring))
kbd_dispatch();
diff --git a/radiator-plc/stm32/app/project.h b/radiator-plc/stm32/app/project.h
index 85f14b9..f4f308a 100644
--- a/radiator-plc/stm32/app/project.h
+++ b/radiator-plc/stm32/app/project.h
@@ -2,6 +2,7 @@
#include <string.h>
#include <strings.h>
#include <libopencm3/stm32/rcc.h>
+#include <libopencm3/stm32/desig.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/usart.h>
#include <libopencm3/stm32/iwdg.h>
diff --git a/radiator-plc/stm32/app/prototypes.h b/radiator-plc/stm32/app/prototypes.h
index 2347f01..6c6f36a 100644
--- a/radiator-plc/stm32/app/prototypes.h
+++ b/radiator-plc/stm32/app/prototypes.h
@@ -68,6 +68,7 @@ extern void mqtt_dispatch(char *type, char *who, char *what, char *msg);
extern void logic_tick(void);
extern void logic_slow_tick(void);
extern void logic_dispatch(void);
+extern void logic_init(void);
/* ds18b20.c */
extern unsigned extract_leu16(uint8_t *d);
extern int extract_les16(uint8_t *d);
diff --git a/radiator-plc/stm32/app/rain.c b/radiator-plc/stm32/app/rain.c
deleted file mode 100644
index 4188990..0000000
--- a/radiator-plc/stm32/app/rain.c
+++ /dev/null
@@ -1,100 +0,0 @@
-#include "project.h"
-
-#define DEBOUNCE 20
-
-static uint8_t r_state;
-static uint32_t r_ticks;
-static uint8_t r_current;
-static uint8_t r_filtered;
-static unsigned r_expiry;
-
-
-#define RAIN_BANK GPIOA
-#define RAIN_GPIO GPIO15
-
-static int
-raw_rain (void)
-{
- return !gpio_get (RAIN_BANK, RAIN_GPIO);
-}
-
-
-void rain_notify (void)
-{
- if (r_current)
- vents_rain_notify();
-}
-
-
-void rain_ticker (void)
-{
- unsigned s;
- static int ticker = 0;
-
-
- s = raw_rain();
-
- if (s == r_state) {
- if (r_ticks < DEBOUNCE)
- r_ticks++;
- else if (r_ticks == DEBOUNCE) {
- r_current = s;
- r_ticks++;
- printf ("D: rain state now %d\r\n", r_current);
- }
- } else {
- r_ticks = 0;
- r_state = s;
- }
-
- if (r_current) {
- r_filtered = 1;
- r_expiry = 1800;
- }
-
- rain_notify();
-
- ticker++;
-
- if (ticker < 1000) return;
-
- ticker = 0;
-
- if (r_expiry)
- r_expiry--;
- else
- r_filtered = r_current;
-
-}
-
-void rain_slow_ticker (void)
-{
- printf ("R: %d %d\r\n", r_current, r_filtered);
-}
-
-
-int rain_filtered_state (void)
-{
- return r_filtered;
-}
-
-int rain_state (void)
-{
- return r_current;
-}
-
-char rain_state_char (void)
-{
- return r_current ? 'Y' : 'N';
-}
-
-void
-rain_init (void)
-{
- gpio_set_mode (RAIN_BANK, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN,
- RAIN_GPIO);
-
- gpio_set (RAIN_BANK, RAIN_GPIO);
-}
-
-
diff --git a/radiator-plc/stm32/app/ticker.c b/radiator-plc/stm32/app/ticker.c
index bb346fb..ffe63d0 100644
--- a/radiator-plc/stm32/app/ticker.c
+++ b/radiator-plc/stm32/app/ticker.c
@@ -47,7 +47,10 @@ sys_tick_handler (void)
slow++;
- if (slow < 2) return;
+ if (slow < 5) return;
+
+ slow = 0;
+
logic_tick();