summaryrefslogtreecommitdiffstats
path: root/boiler-monster/stm32/app/ot_phy_tx.c
blob: 36fbcc57968d1da882a5222b4064a01cb72de645 (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
#include "project.h"

#define OT_THM_OUT        GPIO9
#define OT_THM_OUT_PORT   GPIOB

#define OT_BLR_OUT        GPIO8
#define OT_BLR_OUT_PORT   GPIOB


typedef struct tx_phy  {
  int busy;
  unsigned  half_bit;
  uint8_t data[4];
} TX_Phy;


static TX_Phy p_thm, p_blr;



#if 0
static uint32_t
cycle_diff (uint32_t a, uint32_t b)
{
  return b - a;
}
#endif


static int ot_phy_tx_worker (TX_Phy *p)
{
  int ret = p->half_bit & 1;
  unsigned bit;


  if (p->half_bit < 2)
    ret ^= 1;

  else if (p->half_bit < 66) {
    bit = (p->half_bit >> 1) - 1;

    if (p->data[bit >> 3] & (0x80 >> (bit & 7)))
      ret ^= 1;
  } else if (p->half_bit < 68)

    ret ^= 1;

  p->half_bit++;

  if (p->half_bit == 68) {
    p->half_bit = 0;
    p->busy = 0;
  }

  return ret;
}


void ot_phy_tx_tick (void)
{
  int v;

  if (p_thm.busy) {
    v = ot_phy_tx_worker (&p_thm);

    if (v)
      CLEAR (OT_THM_OUT);
    else
      SET (OT_THM_OUT);
  }


  if (p_blr.busy) {
    v = ot_phy_tx_worker (&p_blr);

    if (v)
      CLEAR (OT_BLR_OUT);
    else
      SET (OT_BLR_OUT);

  }
}


int ot_tx_thm (uint8_t *data)
{
  if (p_thm.busy) return -1;

  if (!data) return 0;

  led_blink();

  memcpy (p_thm.data, data, sizeof (p_thm.data));
  p_thm.busy = 1;

  return 0;
}


int ot_tx_blr (uint8_t *data)
{

  if (p_blr.busy) return -1;

  if (!data) return 0;

  led_blink();

  memcpy (p_blr.data, data, sizeof (p_blr.data));
  p_blr.busy = 1;

  return 0;
}



void ot_phy_tx_init (void)
{
  MAP_OUTPUT_PP (OT_THM_OUT);
  MAP_OUTPUT_PP (OT_BLR_OUT);
}