summaryrefslogtreecommitdiffstats
path: root/app/dcf77.c
blob: 361e4ca180e04429bf132bbd844e1c80cc139d87 (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
#include "project.h"

#define P1 (GPIO11)
#define P1_PORT GPIOD

#define T (GPIO12)
#define T_PORT GPIOD


static Event_ring dcf77_ring;


uint64_t dcf77_last_second;

void exti15_10_isr (void)
{
  uint32_t now = SCS_DWT_CYCCNT;
  int v;

  v = !!gpio_get (T_PORT, T);

  nvic_disable_irq (NVIC_EXTI15_10_IRQ);
  exti_reset_request (EXTI12);

  dcf77_ring.events[dcf77_ring.tx_ptr].when = now;
  dcf77_ring.events[dcf77_ring.tx_ptr].value = v;
  dcf77_ring.tx_ptr = (dcf77_ring.tx_ptr + 1) & ERING_MASK;

  nvic_enable_irq (NVIC_EXTI15_10_IRQ);
}




static uint8_t bits[60];


static void process_bits (uint64_t abs)
{
  UTC u;
  EPOCH dcf77_time;


  if (bits[0]) return;

  if (!bits[20]) return;

  if (!check_parity (bits, 21, 27, bits[28])) return;

  if (!check_parity (bits, 29, 34, bits[35])) return;

  if (!check_parity (bits, 36, 57, bits[58])) return;

  u.jday = 0;
  u.year = le_bcd (bits, 50, 57);
  u.month = le_bcd (bits, 45, 49);
  u.mday = bcd (bits, 36, 41);
  u.hour = bcd (bits, 29, 34);
  u.minute = bcd (bits, 21, 27);
  u.second = 58;
  u.nanosecond = 0;


  dcf77_time = time_utc_to_epoch (u);
  dcf77_time.s -= 58;

#if 0
  pll_set_offset (dcf77_time, abs);
#endif

  printf ("DCF77: Next minute is: %02d-%02d-%02d %02d:%02d\r\n", u.year, u.month, u.mday, u.hour, u.minute);
  time_print_epoch ("DCF77: ", dcf77_time);

  dump_bits ("dcf77", bits);
}


static void report_bit (uint64_t abs, int second, int b)
{
  if ((second < 0) || (second > 58)) return;

  bits[second] = b;

  //  printf("DCF77: bits[%d]=%d\r\n",second,b);

  if (second == 58) process_bits (abs);

}

static void report_time (uint64_t abs)
{
#if 1
  EPOCH e = pll_decompose (abs);
  time_print_epoch ("DCF77: ", e);
#endif
}


void dcf77_dispatch (void)
{
  static uint32_t last_0, last_1, last_s;
  static int second, bit, had_m;
  uint32_t pulse_w, offset;
  static uint64_t abs;
  int is_s = 0;

  uint32_t now;
  int v;




  if (dcf77_ring.rx_ptr == dcf77_ring.tx_ptr) return;

  v = dcf77_ring.events[dcf77_ring.rx_ptr].value;
  now = dcf77_ring.events[dcf77_ring.rx_ptr].when;

  dcf77_ring.rx_ptr = (dcf77_ring.rx_ptr + 1) & ERING_MASK;

  if (v) {
    pulse_w = now - last_0;
    pulse_w /= (HZ / 1000);

    last_1 = now;

    if (pulse_w > 300)  {
      last_s = now;
      is_s = 1;
      second++;
    }

    if (pulse_w > 1300) {
      had_m = 1;
      second = 0;
    }

  } else {
    pulse_w = now - last_1;
    pulse_w /= (HZ / 1000);

    if (pulse_w > 150)
      bit = 1;
    else
      bit = 0;

    if (had_m)
      report_bit (abs, second, bit);

    last_0 = now;
  }

  offset = now - last_s;
  offset /= (HZ / 1000);

  if (is_s) {
    abs = abs_extend (now);
    dcf77_last_second = abs;

    //pll_dispatch (abs);

    if (time_known)
      report_time (abs);
  }


}



void
dcf77_init (void)
{
  MAP_INPUT (T);
  MAP_OUTPUT_PP (P1);

  gpio_clear (P1_PORT, P1);

  exti_select_source (EXTI12, T_PORT);
  exti_set_trigger (EXTI12, EXTI_TRIGGER_BOTH);
  exti_enable_request (EXTI12);
  nvic_enable_irq (NVIC_EXTI15_10_IRQ);
}