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
|
#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(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);
set (0, 0, 0);
set (0, 1, 0);
unlock();
}
static void write_pair (uint8_t reg, int d)
{
write_reg (reg, d / 10);
write_reg (reg - 1, (d % 10) | 0x80);
}
void max7219_write (int d, int h, int m, int s)
{
write_pair (8, d);
write_pair (6, h);
write_pair (4, m);
write_pair (2, s);
}
void max7219_dispatch (void)
{
uint32_t now = HW_CLOCK_REG;
uint64_t abs = abs_extend (now);
EPOCH e = pll_decompose (abs);
UTC u = time_epoch_to_utc (e);
max7219_write (u.mday, u.hour, u.minute, u.second);
}
void
max7219_init (int on)
{
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, 0xff); //BCD decode
write_reg (0xb, 0x7); //8 digits
write_reg (0xa, 0x8); //intensity
} else {
write_reg (0xc, 0x0); //Power up
}
}
|