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
|
#include "project.h"
#define NCS (GPIO7)
#define NCS_PORT GPIOG
#define SCK (GPIO3)
#define SCK_PORT GPIOB
#define MOSI (GPIO5)
#define MOSI_PORT GPIOB
static void
set (int sck, int ncs, int mosi)
{
if (sck)
SET (SCK);
else
CLEAR (SCK);
if (ncs)
SET (NCS);
else
CLEAR (NCS);
if (mosi)
SET (MOSI);
else
CLEAR (MOSI);
// delay_us(1);
//delay_us(10);
}
static void
spip_send_8 (uint8_t wot)
{
int i;
for (i = 0; i < 8; ++i) {
set (0, 0, wot & 0x80);
set (1, 0, wot & 0x80);
set (0, 0, wot & 0x80);
wot <<= 1;
}
}
static int mutex = 0;
static int
lock (void)
{
if (__sync_add_and_fetch (&mutex, 1) != 1) {
__sync_sub_and_fetch (&mutex, 1);
return -1;
}
return 0;
}
static void
unlock (void)
{
__sync_sub_and_fetch (&mutex, 1);
}
static void
write_reg (uint8_t reg, uint8_t data)
{
while (lock());
set (0, 1, 0);
set (0, 0, 0);
spip_send_8 (reg);
spip_send_8 (data);
spip_send_8 (reg);
spip_send_8 (data);
spip_send_8 (reg);
spip_send_8 (data);
set (0, 0, 0);
set (0, 1, 0);
unlock();
}
static void
_write_regs (uint8_t reg, uint8_t *data)
{
while (lock());
set (0, 1, 0);
set (0, 0, 0);
spip_send_8 (reg);
spip_send_8 (data[2]);
spip_send_8 (reg);
spip_send_8 (data[1]);
spip_send_8 (reg);
spip_send_8 (data[0]);
set (0, 0, 0);
set (0, 1, 0);
unlock();
}
static void write_regs (uint8_t reg, uint8_t d1, uint8_t d2, uint8_t d3)
{
uint8_t d[3] = {d1, d2, d3};
_write_regs (reg, d);
}
#define SDP 0x80
#define SA 0x40
#define SB 0x20
#define SC 0x10
#define SD 0x08
#define SE 0x04
#define SF 0x02
#define SG 0x01
static uint8_t hex (unsigned v)
{
switch (v) {
case 0:
case '0':
return SA | SF | SB | SE | SC | SD;
case 1:
case '1':
return SB | SC;
case 2:
case '2':
case 'z':
return SA | SB | SG | SE | SD;
case 3:
case '3':
return SA | SB | SG | SC | SD;
case 4:
case '4':
return SF | SG | SB | SC;
case 5:
case '5':
case 's':
return SA | SF | SG | SC | SD;
case 6:
case '6':
return SA | SF | SG | SE | SC | SD;
case 7:
case '7':
return SA | SB | SC;
case 8:
case '8':
return SA | SF | SB | SG | SE | SC | SD;
case 9:
case '9':
case 'g':
return SA | SF | SB | SG | SC | SD;
case 0xa:
case 'a':
return SA | SF | SB | SG | SE | SC;
case 0xb:
case 'b':
return SF | SG | SE | SC | SD;
case 0xc:
case 'c':
return SG | SE | SD;
case 0xd:
case 'd':
return SB | SG | SE | SC | SD;
case 0xe:
case 'e':
return SA | SF | SG | SE | SD;
case 0xf:
case 'f':
return SA | SF | SG | SE;
case 'h':
return SF | SG | SE | SC;
case 'i':
return SE;
case 'j':
return SB | SC | SD;
case 'k':
case 'x':
return SF | SB | SG | SE | SC;
case 'l':
return SF | SE | SD;
case 'm':
case 'n':
return SG | SE | SC;
case 'o':
return SG | SE | SC | SD;
case 'p':
return SA | SF | SB | SG | SE;
case 'q':
return SA | SF | SB | SG | SC;
case 'r':
return SG | SE;
case 't':
return SF | SG | SE | SD;
case 'u':
case 'w':
return SE | SC | SD;
case 'y':
return SF | SB | SG | SC | SD;
case '-':
return SG;
case '.':
return SDP;
}
return 0;
}
static void _write_triad (uint8_t reg, int *d, int *dp)
{
uint8_t regs0[3] = {0x0, 0x0, 0x0};
uint8_t regs1[3] = {0x0, 0x0, 0x0};
unsigned i;
for (i = 0; i < 3; ++i) {
if (d[i] < 0) continue;
if (d[i] > 99) continue;
regs0[i] = hex (d[i] % 10);
regs1[i] = hex (d[i] / 10);
}
for (i = 0; i < 3; ++i)
if (dp[i]) regs0[i] |= SDP;
_write_regs (reg++, regs0);
_write_regs (reg, regs1);
}
static void write_triad (uint8_t reg, int d1, int d2, int d3, int dp1, int dp2, int dp3)
{
int d[3] = {d1, d2, d3};
int dp[3] = {dp1, dp2, dp3};
_write_triad (reg, d, dp);
}
static void write_string_over_numbers (char *str, int d1, int d2, int d3, int d4)
{
unsigned reg;
uint8_t digits[9] = {
0,
hex (d4 % 10),
hex (d4 / 10),
hex (d3 % 10) | SDP,
hex (d3 / 10),
hex (d2 % 10) | SDP,
hex (d2 / 10),
hex (d1 % 10) | SDP,
hex (d1 / 10)
};
for (reg = 8; reg && *str; reg--, str++)
write_regs (reg, hex (*str) | (!str[1] ? SDP : 0), digits[reg], 0);
for (; reg; reg--)
write_regs (reg, 0, digits[reg], 0);
}
void
max7219_init (int on)
{
uint8_t d[3] = {0xf, 0xf, 0xf};
unsigned i;
MAP_OUTPUT_PP (SCK);
MAP_OUTPUT_PP (NCS);
MAP_OUTPUT_PP (MOSI);
set (0, 1, 0);
if (on) {
write_reg (0xc, 0x1); //Power up
write_reg (0xf, 0x0); //normal mode
write_reg (0x9, 0x0); //no decode
write_reg (0xb, 0x7); //8 digits
write_reg (0xa, pot_brightness);
for (i = 1; i <= 8; ++i)
_write_regs (i, d);
} else {
write_reg (0xc, 0x0); //Power up
}
}
static int have_lock, have_dgps, have_time_lock, time_lock_enabled;
void max7219_report_fix (char fix, char fix2)
{
have_lock = 0;
if (fix == 'L')
have_lock = 1;
if (fix2 == 'D') have_dgps = 1;
else have_dgps = 0;
if (fix == 'T') {
have_lock = 1;
have_time_lock = 1;
}
}
void max7219_report_svin (int valid, int active)
{
time_lock_enabled = 0;
if (active || valid) time_lock_enabled = 1;
}
void max7219_dispatch (void)
{
uint64_t abs = ref_get();
static uint8_t last_brightness = 255;
int wday;
EPOCH e;
UTC u;
UTC gu;
ST l;
if (last_brightness != pot_brightness) {
last_brightness = pot_brightness;
write_reg (0xa, last_brightness);
}
if (gps_initting)
write_string_over_numbers ("gps init", 0, 0, 0, 0);
else if (!ref_valid)
write_string_over_numbers ("gps acq", gps_sats_searching, gps_sats_inop, gps_sats_locked, gps_sats_with_e);
else {
static unsigned m;
e = ref_decompose (abs);
u = time_epoch_to_utc (e);
l = time_utc_to_lst (u, gps_lon);
if (have_lock) {
e.s += 86400;
e.s += gps_utc_diff;
gu = time_epoch_to_utc (e);
wday = gps_wday;
} else {
wday = 100;
gu.hour = 100;
gu.minute = 100;
gu.second = 100;
}
write_triad (1, u.nanosecond / 10000000, l.nanosecond / 10000000, gu.second, time_lock_enabled, have_time_lock, have_dgps);
write_triad (3, u.second, l.second, gu.minute, 1, 1, 1);
write_triad (5, u.minute, l.minute, gu.hour, 1, 1, 1);
write_triad (7, u.hour, l.hour, wday, 1, 1, 1);
if (u.minute == m) return;
m = u.minute;
printf ("LEDS: %02d.%02d.%02d.%02d %02d.%02d.%02d.%02d %02d.%02d.%02d.%02d lon %.6f\r\n",
u.hour, u.minute, u.second,
u.nanosecond / 10000000,
l.hour,
l.minute,
l.second,
l.nanosecond / 10000000,
gps_wday,
gu.hour,
gu.minute,
gu.second,
gps_lon);
}
}
|