summaryrefslogtreecommitdiffstats
path: root/radiator-plc/stm32/app/main.c
blob: 4a00a708e066eaa2cc64216efc7bc4d5188e43a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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();
  logic_init();

  printf ("D: RESET\n");

  for (;;) {
    if (!ring_empty (&rx1_ring))
      kbd_dispatch();

    logic_dispatch();

  }

  return 0;
}