summaryrefslogtreecommitdiffstats
path: root/boiler-monster/stm32/app/ot.c
blob: 2a8ad362ad780c0ac069c273f7e363c1eb467e57 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#include "project.h"


typedef struct {
  uint8_t flags: 5;
  uint8_t type: 3;
  uint8_t data[2];
} OT_Msg;


#define FLAGS_PENDING_THM   1U << 0
#define FLAGS_PROCESSED_THM 1U << 1
#define FLAGS_PENDING_USR   1U << 2
#define FLAGS_PROCESSED_USR 1U << 3
#define FLAGS_PENDING_OVR   1U << 4

#define FLAGS_PENDING (FLAGS_PENDING_THM|FLAGS_PENDING_USR | FLAGS_PENDING_OVR)

#define IDX_MAX 0x300

static OT_Msg ot_thm_req[IDX_MAX];
static OT_Msg ot_blr_rsp[IDX_MAX];

#define OT_NEXT(a) (((a) < 0x2ff) ?  ((a)+1):0)
#define OT_INDEX(t,id) ((((unsigned) (t) ) <<8 ) | ((unsigned) (id)))
#define OT_IDX_TO_ID(idx) ((idx) & 0xff)
#define OT_IDX_TO_TYPE(idx) ((idx) >> 8)

static unsigned ot_req_idx;
static unsigned in_flight_req_type;
static unsigned blr_backoff;
static unsigned ot_status_wdt;


#define OT_READ_DATA    0x0
#define OT_WRITE_DATA   0x1
#define OT_INVALID_DATA 0x2
#define OT_READ_ACK     0x4
#define OT_WRITE_ACK    0x5
#define OT_DATA_INVALID 0x6
#define OT_UNKNOWN_DATAID 0x7


#define OT_IDX_STATUS 0
/*TX*/
#define OT_IDX_STATUS_BIT_ENABLE_CH (1U << 0)
#define OT_IDX_STATUS_BIT_ENABLE_DHW (1U << 1)

/*RX*/
#define OT_IDX_STATUS_BIT_FAULT (1U << 0)
#define OT_IDX_STATUS_BIT_CH_MODE (1U << 1)
#define OT_IDX_STATUS_BIT_DHW_MODE (1U << 2)
#define OT_IDX_STATUS_BIT_FLAME (1U << 3)

#define OT_IDX_CONTROL_SETPOINT 1
#define OT_IDX_DHW_SETPOINT 56


#define OT_IDX_CH_WATER_PRESSURE  18
#define OT_IDX_RETURN_WATER_TEMP  28
#define OT_IDX_SUPPLY_INLET_TEMP  80



unsigned ot_override_ch = 0;
unsigned ot_override_dhw = 0;


static inline int parity (uint8_t v)
{
  return (0x6996u >> ((v ^ (v >> 4)) & 0xf)) & 1;
}

static void ot_parity (uint8_t *data)
{
  int p;
  p = parity (data[0]);
  p ^= parity (data[1]);
  p ^= parity (data[2]);
  p ^= parity (data[3]);

  if (p) data[0] ^= 0x80;
}


static const char *type_str[8] = {
  ">Read Data",
  ">Write Data",
  ">Invalid Data",
  ">Reserved",
  "<Read ACK",
  "<Write ACK",
  "<Data Invalid",
  "<Unknown DataID"
};

static void ot_debug (char *who, uint8_t *msg, char *what)
{
  unsigned type = (msg[0] >> 4) & 7;


  printf ("%s%02x%02x%02x%02x %s %s\r\n",
          who,
          msg[0],
          msg[1],
          msg[2],
          msg[3],
          type_str[type],
          what
         );
}

static void send_reply_to_thm (unsigned idx)
{
  uint8_t reply[4];

  if (ot_tx_thm (NULL)) return;

  reply[0] = ot_blr_rsp[idx].type << 4;
  reply[1] = OT_IDX_TO_ID (idx);
  reply[2] = ot_blr_rsp[idx].data[0];
  reply[3] = ot_blr_rsp[idx].data[1];

  ot_debug ("B", reply, "");

  ot_parity (reply);
  ot_tx_thm (reply);

  ot_blr_rsp[idx].flags &= ~FLAGS_PROCESSED_THM;

}


static int send_req_to_blr (unsigned idx)
{
  uint8_t req[4];

  if (ot_tx_blr (NULL)) return -1;



  req[0] = ot_thm_req[ot_req_idx].type << 4;
  req[1] = OT_IDX_TO_ID (ot_req_idx);
  req[2] = ot_thm_req[ot_req_idx].data[0];
  req[3] = ot_thm_req[ot_req_idx].data[1];

  ot_parity (req);
  ot_debug ("          S", req, "");

  return  ot_tx_blr (req);
}




void  ot_rx_thm (uint8_t *msg, int error)
{
  unsigned type = (msg[0] >> 4) & 7;
  unsigned id = msg[1];

  unsigned idx;

  if (error) return;

  if (type > 2) {
    ot_debug ("T", msg, "message type invalid for thermostat");
    return;
  }

  if (ot_override_ch) {
    if ((id == OT_IDX_STATUS) && (type == OT_READ_DATA)) /* Turn on heating */
      msg[2] |= OT_IDX_STATUS_BIT_ENABLE_CH;

    if ((id == OT_IDX_CONTROL_SETPOINT) && (type == OT_WRITE_DATA)) /* set water temp */
      msg[2] = ot_override_ch;

  }

  if (ot_override_dhw) {
    if ((id == OT_IDX_STATUS) && (type == OT_READ_DATA)) /* Turn on hotwater */
      msg[2] |= OT_IDX_STATUS_BIT_ENABLE_DHW;

    if ((id == OT_IDX_DHW_SETPOINT) && (type == OT_WRITE_DATA)) /* set water temp */
      msg[2] = ot_override_dhw;

  }

  if ((id == OT_IDX_STATUS) && (type == OT_READ_DATA))
    ot_status_wdt = 0;

  ot_debug ("T", msg, "");

  idx = OT_INDEX (type, id);

  if (ot_blr_rsp[idx].flags & FLAGS_PROCESSED_THM)
    send_reply_to_thm (idx);

  else {
    ot_thm_req[idx].type = type;
    ot_thm_req[idx].data[0] = msg[2];;
    ot_thm_req[idx].data[1] = msg[3];;
    ot_thm_req[idx].flags |= FLAGS_PENDING_THM;
  }
}

static int  ot_fake_read_ack (unsigned id, uint8_t *msg)
{
  unsigned t;

  switch (id) {
  case OT_IDX_CH_WATER_PRESSURE:
    t = pressure_ch();

    if (!t) return -1;

    break;

  case OT_IDX_RETURN_WATER_TEMP:
    t = temp_ch_return();

    if (!t) return -1;

    break;

  case OT_IDX_SUPPLY_INLET_TEMP:
    t = temp_supply_inlet();

    if (!t) return -1;

    break;

  default:
    return -1;
  }



  msg[0] = OT_READ_ACK << 4;
  msg[1] = id;
  msg[2] = t >> 8;
  msg[3] = t & 0xff;

  return 0;
}


void  ot_rx_blr (uint8_t *msg, int error)
{
  unsigned type = (msg[0] >> 4) & 7;
  unsigned id = msg[1];

  unsigned idx;

  if (error) return;

  ot_debug ("          A", msg, "");

  idx = OT_INDEX (in_flight_req_type, id);

  if ((in_flight_req_type == OT_READ_DATA) && (type != OT_READ_ACK)) {
    if (!ot_fake_read_ack (id, msg)) {
      ot_debug ("          A", msg, " (faked)");
      type = (msg[0] >> 4) & 7;
    }
  }

  ot_blr_rsp[idx].type = type;
  ot_blr_rsp[idx].data[0] = msg[2];;
  ot_blr_rsp[idx].data[1] = msg[3];;
  ot_blr_rsp[idx].flags |= FLAGS_PROCESSED_THM | FLAGS_PROCESSED_USR;

  if (ot_thm_req[idx].flags & FLAGS_PENDING_THM) {
    ot_thm_req[idx].flags &= ~FLAGS_PENDING_THM;
    send_reply_to_thm (idx);
  }

  if (ot_thm_req[idx].flags & FLAGS_PENDING_USR) {
    ot_thm_req[idx].flags &= ~FLAGS_PENDING_USR;
    //send_reply_to_usr (idx);
  }

  if (ot_thm_req[idx].flags & FLAGS_PENDING_OVR)
    ot_thm_req[idx].flags &= ~FLAGS_PENDING_OVR;

  blr_backoff = 0;

}



static void ot_boiler_worker (void)
{
  unsigned i;

  if (blr_backoff) {
    blr_backoff--;
    return;
  }

  if (ot_tx_blr (NULL)) return;


  for (i = 0; i < IDX_MAX; ++i, ot_req_idx = OT_NEXT (ot_req_idx)) {

    if (ot_thm_req[ot_req_idx].flags & FLAGS_PENDING)  {

      if (!send_req_to_blr (ot_req_idx)) {
        ot_thm_req[ot_req_idx].flags &= ~FLAGS_PENDING;
        in_flight_req_type = OT_IDX_TO_TYPE (ot_req_idx);
        blr_backoff = 10;
        ot_req_idx = OT_NEXT (ot_req_idx);
        return;
      }
    }

  }

}


static void ot_request (unsigned flags, unsigned type, unsigned id, uint8_t *data)
{
  unsigned idx = OT_INDEX (type, id);

  ot_thm_req[idx].type = type;

  if (data) {
    ot_thm_req[idx].data[0] = data[0];
    ot_thm_req[idx].data[1] = data[1];
  } else {
    ot_thm_req[idx].data[0] = 0;
    ot_thm_req[idx].data[1] = 0;
  }

  ot_thm_req[idx].flags |= FLAGS_PENDING_USR;

}

void ot_request_usr (unsigned type, unsigned id, uint8_t *data)
{
  ot_request (FLAGS_PENDING_USR, type, id, data);
}
static void ot_request_ovr (unsigned type, unsigned id, uint8_t *data)
{
  ot_request (FLAGS_PENDING_OVR, type, id, data);
}



static void ot_force_status (void)
{
  uint8_t data[2] = { (ot_override_ch ? OT_IDX_STATUS_BIT_ENABLE_CH : 0) | (ot_override_dhw ? OT_IDX_STATUS_BIT_ENABLE_DHW : 0), 0};

  ot_request_ovr (OT_READ_DATA, OT_IDX_STATUS, data);

}

static void ot_30s_ticker (void)
{
  uint8_t data[2];

  printf ("Q ot ticker - push control flags\r\n");

  if (ot_override_ch) {
    data[0] = ot_override_ch;
    data[1] = 0;
    ot_request_ovr (OT_WRITE_DATA, OT_IDX_CONTROL_SETPOINT, data);
  }

  if (ot_override_dhw) {

    data[0] = ot_override_dhw;
    data[1] = 0;
    ot_request_ovr (OT_WRITE_DATA, OT_IDX_DHW_SETPOINT, data);
  }

}

static void ot_2s_ticker (void)
{
  uint8_t msg[4];

  if (!ot_fake_read_ack (OT_IDX_CH_WATER_PRESSURE, msg))
    ot_debug ("B", msg, " (fake request and reply)");

  if (!ot_fake_read_ack (OT_IDX_RETURN_WATER_TEMP, msg))
    ot_debug ("B", msg, " (fake request and reply)");

  if (!ot_fake_read_ack (OT_IDX_SUPPLY_INLET_TEMP, msg))
    ot_debug ("B", msg, " (fake request and reply)");
}





static void ot_4hz_ticker (void)
{
  static unsigned thirty, two;
  thirty++;
  two++;

  ot_boiler_worker();

  ot_status_wdt++;

  if (ot_status_wdt > 120) {
    printf ("Q  forcing status packet\r\n");
    ot_force_status();
    ot_status_wdt = 0;
  }


  if (two >= 8)  {
    ot_2s_ticker();
    two = 0;
  }


  if (thirty >= 120)  {
    ot_30s_ticker();
    thirty = 0;
  }


  led_yellow_set (ot_blr_rsp[OT_INDEX (OT_READ_DATA, OT_IDX_STATUS)].data[1] & OT_IDX_STATUS_BIT_FAULT);
  led_green1_set (ot_blr_rsp[OT_INDEX (OT_READ_DATA, OT_IDX_STATUS)].data[1] & OT_IDX_STATUS_BIT_CH_MODE);
  led_green2_set (ot_blr_rsp[OT_INDEX (OT_READ_DATA, OT_IDX_STATUS)].data[1] & OT_IDX_STATUS_BIT_DHW_MODE);
  led_red_set (ot_blr_rsp[OT_INDEX (OT_READ_DATA, OT_IDX_STATUS)].data[1] & OT_IDX_STATUS_BIT_FLAME);

}


void ot_tick (void)
{
  static unsigned i;
  i++;

  if (i >= 500)  {
    ot_4hz_ticker();
    i = 0;
  }


}


void ot_init (void)
{
  ot_phy_rx_init();
  ot_phy_tx_init();
}