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

#define OT_THM_IN       GPIO6
#define OT_THM_IN_PORT    GPIOB

#define OT_BLR_IN       GPIO7
#define OT_BLR_IN_PORT    GPIOB


#define OT_RX_IRQ     NVIC_EXTI9_5_IRQ


typedef struct rx_phy  {
  uint32_t last_cycle;
  int last_v;
  unsigned half_bits;
  unsigned data_bits;
  int frame_error;
  uint8_t data[10];
  int parity;
} RX_Phy;


static RX_Phy p_thm, p_blr;


static uint32_t
cycle_diff (uint32_t a, uint32_t b)
{
  return b - a;
}




static void ot_phy_rx_bit (RX_Phy *p)
{


  if (p->half_bits & 1)
    p->frame_error = 1;

  if (!p->half_bits) {
    if (!p->last_v)
      p->frame_error = 1;

    return;
  }

  if (p->data_bits < 32) {
    if (p->last_v) p->data[p->data_bits >> 3] |= 0x80 >> (p->data_bits & 7);

    p->parity ^= p->last_v;
  } else if (p->data_bits == 32) {
    if ((!p->last_v) || (p->parity))
      p->frame_error = 1;

    led_blink();

    if (p == &p_thm)
      ot_rx_thm (p->data, p->frame_error);
    else
      ot_rx_blr (p->data, p->frame_error);
  }


  p->data_bits++;

}


static void ot_phy_rx_worker (RX_Phy *p, int v)
{
  uint32_t now, diff;

  if (v == p->last_v) return;


  now = dwt_read_cycle_counter();
  diff = cycle_diff (p->last_cycle, now);


  if (diff < 10000) return;

  if (diff < 50000) {
    if (! (p->half_bits & 1)) ot_phy_rx_bit (p);

    p->half_bits++;
  } else if (diff < 85000) {
    p->half_bits++;
    ot_phy_rx_bit (p);
    p->half_bits++;
  } else {
    p->parity = 0;
    p->half_bits = 0;
    p->frame_error = 0;
    p->data_bits = 0;
    memset (p->data, 0, sizeof (p->data));
  }

  p->last_cycle = now;
  p->last_v = v;
}



void
exti9_5_isr (void)
{
  int v;

  if (EXTI_PR & OT_THM_IN) {
    EXTI_PR = OT_THM_IN;
    v = !GET (OT_THM_IN);

#if 0

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

#endif

    ot_phy_rx_worker (&p_thm, v);


  }

  if (EXTI_PR & OT_BLR_IN) {
    EXTI_PR = OT_BLR_IN;
    v = !GET (OT_BLR_IN);

#if 0

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

#endif

    ot_phy_rx_worker (&p_blr, v);
  }

  return;
}


void ot_phy_rx_tick (void)
{
}



void ot_phy_rx_init (void)
{
  MAP_INPUT_PU (OT_THM_IN);
  MAP_INPUT_PU (OT_BLR_IN);


  exti_select_source (OT_THM_IN, OT_THM_IN_PORT);
  exti_set_trigger (OT_THM_IN, EXTI_TRIGGER_BOTH);
  exti_enable_request (OT_THM_IN);
  exti_reset_request (OT_THM_IN);

  exti_select_source (OT_BLR_IN, OT_BLR_IN_PORT);
  exti_set_trigger (OT_BLR_IN, EXTI_TRIGGER_BOTH);
  exti_enable_request (OT_BLR_IN);
  exti_reset_request (OT_BLR_IN);

  nvic_enable_irq (OT_RX_IRQ);

}