From 9d87c925a9eaa4fc256be3173c14a20d1469472d Mon Sep 17 00:00:00 2001 From: fishsoupisgood Date: Wed, 9 Sep 2020 11:53:37 +0100 Subject: everything, mostly, working --- radiator-plc/stm32/app/main.c | 150 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 radiator-plc/stm32/app/main.c (limited to 'radiator-plc/stm32/app/main.c') diff --git a/radiator-plc/stm32/app/main.c b/radiator-plc/stm32/app/main.c new file mode 100644 index 0000000..57b6752 --- /dev/null +++ b/radiator-plc/stm32/app/main.c @@ -0,0 +1,150 @@ +#include "project.h" + + +volatile int request_search; + +static void mqtt_cmd (char *type) +{ + char *m; + char *who, *what; + + m = rindex (type, ' '); + + if (!m) return; + + *m = 0; + m++; + + while (index (type, ' ') == type) + type++; + + who = index (type, ' '); + if (who) *who = 0; + + + who = index (type, '/'); + + if (!who) { + who = ""; + what = ""; + } else { + *who = 0; + who++; + + what = index (who, '/'); + + if (!what) + what = ""; + + else { + *what = 0; + what++; + } + } + + + mqtt_dispatch (type, who, what, m); +} + +static void cmd_dispatch (char *cmd) +{ + + if (!strcmp (cmd, "SEARCH")) { + request_search = 1; + return; + } + + if (!strncmp (cmd, "MQTT ", 5)) { + mqtt_cmd (cmd + 5); + return; + } + + + printf ("Unknown command %s\n", cmd); +} + + + + +static void +kbd_dispatch (void) +{ + static char cmd_buf[80]; + static unsigned cmd_ptr; + + uint8_t c; + + while (!ring_read_byte (&rx1_ring, &c)) { + + switch (c) { + case ' ': + case '\t': + if (!cmd_ptr) + break; + + /*fall through */ + default: + cmd_buf[cmd_ptr++] = c; + cmd_buf[cmd_ptr] = 0; + + + if (cmd_ptr < (sizeof (cmd_buf) - 1)) + break; + + /*fall through*/ + case '\r': + case '\n': + if (!cmd_ptr) + break; + + cmd_dispatch (cmd_buf); + cmd_ptr = 0; + break; + } + } +} + + +int +main (void) +{ + /*set up pll */ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + + /*turn on clocks which aren't done elsewhere */ + rcc_periph_clock_enable (RCC_GPIOA); + rcc_periph_clock_enable (RCC_GPIOB); + rcc_periph_clock_enable (RCC_GPIOC); + rcc_periph_clock_enable (RCC_GPIOD); + rcc_periph_clock_enable (RCC_GPIOE); + rcc_periph_clock_enable (RCC_AFIO); + + //watchdog_init (); + + /*Adjust interrupt priorities so that uarts trump timer */ + nvic_set_priority (NVIC_USART1_IRQ, 0x40); + nvic_set_priority (NVIC_USART2_IRQ, 0x40); + nvic_set_priority (NVIC_USART3_IRQ, 0x40); + nvic_set_priority (NVIC_SYSTICK_IRQ, 0xff); + + ticker_init(); + usart_init(); + led_init(); + onewire_init(); + inputs_init(); + outputs_init(); + + printf ("D: RESET\n"); + + //onewire_search(); + + for (;;) { + if (!ring_empty (&rx1_ring)) + kbd_dispatch(); + + logic_dispatch(); + + } + + return 0; +} -- cgit v1.2.3