-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspicmd.c
200 lines (156 loc) · 2.77 KB
/
spicmd.c
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
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "defs.h"
#include "reg.h"
#include "spicmd.h"
#include "gfx.h"
// NOTE:
// Timing between SPI bytes is forced by the code.
// It is possible that current values won't work for you.
// In such case just change values in each 'spi_wait' call.
// (or add something like 'ttt += 4' into 'spi_wait')
// Find out values that work for you but do not slow OSD too much.
__xdata spi_cmd_t spicmd;
//
// waiting
static void spi_wait(uint8_t ttt)
{
while(--ttt);
}
//
// data transfer
void spi_send(uint8_t len)
{
__xdata uint8_t *ptr = (__xdata uint8_t*)&spicmd;
OUT_CS = 0;
do
{
SPDAT = *ptr;
ptr++;
spi_wait(2);
} while(--len);
spi_wait(3);
OUT_CS = 1;
}
//
// API
void spicmd_osd_clear(uint8_t color)
{
color |= color << 4;
for(uint8_t y = 0; y < 65; y++)
{
uint8_t x = 64;
OUT_CS = 0;
SPDAT = 0x0D;
spi_wait(4);
SPDAT = y << 6;
spi_wait(4);
SPDAT = y >> 2;
spi_wait(4);
do
{
SPDAT = color;
spi_wait(4);
} while(--x);
spi_wait(3);
OUT_CS = 1;
spi_wait(3);
}
}
void spicmd_osd_line(uint8_t y, uint8_t color)
{
uint16_t addr;
if(y >= OSD_HEIGHT)
return;
color |= color << 4;
addr = (uint16_t)y * OSD_STRIDE;
#ifdef SPI_OSDADDR_OFFSET
addr += (uint16_t)SPI_OSDADDR_OFFSET;
#endif
OUT_CS = 0;
SPDAT = 0x0D;
spi_wait(4);
SPDAT = addr & 0xFF;
spi_wait(4);
SPDAT = addr >> 8;
spi_wait(5);
y = OSD_STRIDE;
do
{
SPDAT = color;
spi_wait(4);
} while(--y);
spi_wait(3);
OUT_CS = 1;
}
void spicmd_xbm_line(const __code uint8_t *data)
{
__xdata uint8_t *ptr = (__xdata uint8_t*)&spicmd;
uint8_t len = 3;
OUT_CS = 0;
// command
do
{
SPDAT = *ptr;
ptr++;
spi_wait(2);
} while(--len);
// data
len = OSD_WIDTH / 8;
do
{
register uint8_t bits = *data++;
register uint8_t out;
out = logo_pal_idx[bits & 1];
out |= logo_pal_idx[(bits >> 1) & 1] << 4;
SPDAT = out;
spi_wait(1);
out = logo_pal_idx[(bits >> 2) & 1];
out |= logo_pal_idx[(bits >> 3) & 1] << 4;
SPDAT = out;
spi_wait(1);
out = logo_pal_idx[(bits >> 4) & 1];
out |= logo_pal_idx[(bits >> 5) & 1] << 4;
SPDAT = out;
spi_wait(1);
out = logo_pal_idx[(bits >> 6) & 1];
out |= logo_pal_idx[(bits >> 7) & 1] << 4;
SPDAT = out;
spi_wait(1);
} while(--len);
spi_wait(4);
OUT_CS = 1;
}
void spicmd_img_line()
{
__xdata uint8_t *ptr = (__xdata uint8_t*)&spicmd;
uint8_t len = 3;
OUT_CS = 0;
// command
do
{
SPDAT = *ptr;
ptr++;
spi_wait(2);
} while(--len);
// data
while(1)
{
uint8_t out;
uint8_t idx;
idx = gfx_read_2bpp();
if(idx & 0x80)
break;
out = osd_pal_idx[idx];
idx = gfx_read_2bpp();
if(idx & 0x80)
out |= out << 4;
else
out |= osd_pal_idx[idx] << 4;
SPDAT = out;
// no delay here
}
spi_wait(3);
OUT_CS = 1;
}