forked from ArduPilot/ardupilot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAP_OSD_MAX7456.cpp
483 lines (419 loc) · 14.3 KB
/
AP_OSD_MAX7456.cpp
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
* This file is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
* MAX7456 driver partially based on betaflight and inav max7456.c implemention.
* Many thanks to their authors.
*/
#include <AP_OSD/AP_OSD_MAX7456.h>
#if HAL_WITH_OSD_BITMAP
#include <AP_HAL/Util.h>
#include <AP_HAL/Semaphores.h>
#include <AP_HAL/Scheduler.h>
#include <AP_Filesystem/AP_Filesystem.h>
#include <GCS_MAVLink/GCS.h>
#include <utility>
#define VIDEO_COLUMNS 30
#define NVM_RAM_SIZE 54
//MAX7456 registers
#define MAX7456ADD_READ 0x80
#define MAX7456ADD_VM0 0x00
#define MAX7456ADD_VM1 0x01
#define MAX7456ADD_HOS 0x02
#define MAX7456ADD_VOS 0x03
#define MAX7456ADD_DMM 0x04
#define MAX7456ADD_DMAH 0x05
#define MAX7456ADD_DMAL 0x06
#define MAX7456ADD_DMDI 0x07
#define MAX7456ADD_CMM 0x08
#define MAX7456ADD_CMAH 0x09
#define MAX7456ADD_CMAL 0x0a
#define MAX7456ADD_CMDI 0x0b
#define MAX7456ADD_OSDM 0x0c
#define MAX7456ADD_RB0 0x10
#define MAX7456ADD_OSDBL 0x6c
#define MAX7456ADD_STAT 0xA0
#define MAX7456ADD_CMDO 0xC0
// VM0 register bits
#define VIDEO_BUFFER_DISABLE 0x01
#define MAX7456_RESET 0x02
#define VERTICAL_SYNC_NEXT_VSYNC 0x04
#define OSD_ENABLE 0x08
#define VIDEO_MODE_PAL 0x40
#define VIDEO_MODE_NTSC 0x00
#define VIDEO_MODE_MASK 0x40
#define VIDEO_MODE_IS_PAL(val) (((val) & VIDEO_MODE_MASK) == VIDEO_MODE_PAL)
#define VIDEO_MODE_IS_NTSC(val) (((val) & VIDEO_MODE_MASK) == VIDEO_MODE_NTSC)
// VM1 register bits
// duty cycle is on_off
#define BLINK_DUTY_CYCLE_50_50 0x00
#define BLINK_DUTY_CYCLE_33_66 0x01
#define BLINK_DUTY_CYCLE_25_75 0x02
#define BLINK_DUTY_CYCLE_75_25 0x03
// blinking time
#define BLINK_TIME_0 0x00
#define BLINK_TIME_1 0x04
#define BLINK_TIME_2 0x08
#define BLINK_TIME_3 0x0C
// background mode brightness (percent)
#define BACKGROUND_BRIGHTNESS_0 (0x00 << 4)
#define BACKGROUND_BRIGHTNESS_7 (0x01 << 4)
#define BACKGROUND_BRIGHTNESS_14 (0x02 << 4)
#define BACKGROUND_BRIGHTNESS_21 (0x03 << 4)
#define BACKGROUND_BRIGHTNESS_28 (0x04 << 4)
#define BACKGROUND_BRIGHTNESS_35 (0x05 << 4)
#define BACKGROUND_BRIGHTNESS_42 (0x06 << 4)
#define BACKGROUND_BRIGHTNESS_49 (0x07 << 4)
// STAT register bits
#define STAT_PAL 0x01
#define STAT_NTSC 0x02
#define STAT_LOS 0x04
#define STAT_NVR_BUSY 0x20
#define STAT_IS_PAL(val) ((val) & STAT_PAL)
#define STAT_IS_NTSC(val) ((val) & STAT_NTSC)
#define STAT_IS_LOS(val) ((val) & STAT_LOS)
#define VIN_IS_PAL(val) (!STAT_IS_LOS(val) && STAT_IS_PAL(val))
#define VIN_IS_NTSC(val) (!STAT_IS_LOS(val) && STAT_IS_NTSC(val))
// There are occasions that NTSC is not detected even with !LOS (AB7456 specific?)
// When this happens, lower 3 bits of STAT register is read as zero.
// To cope with this case, this macro defines !LOS && !PAL as NTSC.
// Should be compatible with MAX7456 and non-problematic case.
#define VIN_IS_NTSC_ALT(val) (!STAT_IS_LOS(val) && !STAT_IS_PAL(val))
//CMM register bits
#define WRITE_NVR 0xA0
#define READ_NVR 0x50
// DMM special bits
#define DMM_BLINK (1 << 4)
#define DMM_INVERT_PIXEL_COLOR (1 << 3)
#define DMM_CLEAR_DISPLAY (1 << 2)
#define DMM_CLEAR_DISPLAY_VERT (DMM_CLEAR_DISPLAY | 1 << 1)
#define DMM_AUTOINCREMENT (1 << 0)
// time to check video signal format
#define VIDEO_SIGNAL_CHECK_INTERVAL_MS 1000
//time to wait for input to stabilize
#define VIDEO_SIGNAL_DEBOUNCE_MS 100
//time to wait nvm flash complete
#define MAX_NVM_WAIT 10000
//black and white level
#ifndef WHITEBRIGHTNESS
#define WHITEBRIGHTNESS 0x01
#endif
#ifndef BLACKBRIGHTNESS
#define BLACKBRIGHTNESS 0x00
#endif
#define BWBRIGHTNESS ((BLACKBRIGHTNESS << 2) | WHITEBRIGHTNESS)
extern const AP_HAL::HAL &hal;
AP_OSD_MAX7456::AP_OSD_MAX7456(AP_OSD &osd, AP_HAL::OwnPtr<AP_HAL::Device> dev):
AP_OSD_Backend(osd), _dev(std::move(dev))
{
video_signal_reg = VIDEO_MODE_PAL | OSD_ENABLE;
video_lines = video_lines_pal;
}
bool AP_OSD_MAX7456::init()
{
uint8_t status = 0xFF;
_dev->get_semaphore()->take_blocking();
_dev->write_register(MAX7456ADD_VM0, MAX7456_RESET);
hal.scheduler->delay(1);
_dev->read_registers(MAX7456ADD_VM0|MAX7456ADD_READ, &status, 1);
_dev->get_semaphore()->give();
if (status != 0) {
return false;
}
return update_font();
}
bool AP_OSD_MAX7456::update_font()
{
uint8_t updated_chars = 0;
last_font = get_font_num();
FileData *fd = load_font_data(last_font);
if (fd == nullptr) {
return false;
}
const uint8_t *font_data = fd->data;
uint32_t font_size = fd->length;
if (font_size != NVM_RAM_SIZE * 256) {
GCS_SEND_TEXT(MAV_SEVERITY_ERROR, "AP_OSD: bad font size %u\n", unsigned(font_size));
delete fd;
return false;
}
for (uint16_t chr=0; chr < 256; chr++) {
const uint8_t* chr_font_data = font_data + chr*NVM_RAM_SIZE;
//check if char already up to date
if (!check_font_char(chr, chr_font_data)) {
//update char inside max7456 NVM
if (!update_font_char(chr, chr_font_data)) {
delete fd;
return false;
}
updated_chars++;
}
}
delete fd;
return true;
}
//compare char chr inside MAX7456 NVM with font_data
bool AP_OSD_MAX7456::check_font_char(uint8_t chr, const uint8_t* font_data)
{
buffer_offset = 0;
//send request to read data from NVM
buffer_add_cmd(MAX7456ADD_VM0, 0);
buffer_add_cmd(MAX7456ADD_CMAH, chr);
buffer_add_cmd(MAX7456ADD_CMM, READ_NVR);
for (uint16_t x = 0; x < NVM_RAM_SIZE; x++) {
buffer_add_cmd(MAX7456ADD_CMAL, x);
buffer_add_cmd(MAX7456ADD_CMDO, 0xFF);
}
_dev->get_semaphore()->take_blocking();
_dev->transfer(buffer, buffer_offset, buffer, buffer_offset);
_dev->get_semaphore()->give();
//skip response from MAX7456ADD_VM0/MAX7456ADD_CMAH...
buffer_offset = 9;
for (uint16_t x = 0; x < NVM_RAM_SIZE; x++) {
if (buffer[buffer_offset] != font_data[x]) {
return false;
}
//one byte per MAX7456ADD_CMAL/MAX7456ADD_CMDO pair
buffer_offset += 4;
}
return true;
}
bool AP_OSD_MAX7456::update_font_char(uint8_t chr, const uint8_t* font_data)
{
uint16_t retry;
buffer_offset = 0;
buffer_add_cmd(MAX7456ADD_VM0, 0);
buffer_add_cmd(MAX7456ADD_CMAH, chr);
for (uint16_t x = 0; x < NVM_RAM_SIZE; x++) {
buffer_add_cmd(MAX7456ADD_CMAL, x);
buffer_add_cmd(MAX7456ADD_CMDI, font_data[x]);
}
buffer_add_cmd(MAX7456ADD_CMM, WRITE_NVR);
_dev->get_semaphore()->take_blocking();
_dev->transfer(buffer, buffer_offset, nullptr, 0);
_dev->get_semaphore()->give();
for (retry = 0; retry < MAX_NVM_WAIT; retry++) {
uint8_t status = 0xFF;
hal.scheduler->delay(15);
_dev->get_semaphore()->take_blocking();
_dev->read_registers(MAX7456ADD_STAT, &status, 1);
_dev->get_semaphore()->give();
if ((status & STAT_NVR_BUSY) == 0x00) {
break;
}
}
return retry != MAX_NVM_WAIT;
}
AP_OSD_Backend *AP_OSD_MAX7456::probe(AP_OSD &osd, AP_HAL::OwnPtr<AP_HAL::Device> dev)
{
if (!dev) {
return nullptr;
}
AP_OSD_MAX7456 *backend = new AP_OSD_MAX7456(osd, std::move(dev));
if (!backend) {
return nullptr;
}
if (!backend->init()) {
delete backend;
return nullptr;
}
return backend;
}
void AP_OSD_MAX7456::buffer_add_cmd(uint8_t reg, uint8_t arg)
{
if (buffer_offset < spi_buffer_size - 1) {
buffer[buffer_offset++] = reg;
buffer[buffer_offset++] = arg;
}
}
//Thanks to betaflight for the max stall/reboot detection approach and ntsc/pal autodetection
void AP_OSD_MAX7456::check_reinit()
{
uint8_t check = 0xFF;
_dev->get_semaphore()->take_blocking();
_dev->read_registers(MAX7456ADD_VM0|MAX7456ADD_READ, &check, 1);
uint32_t now = AP_HAL::millis();
// Stall check
if (check != video_signal_reg) {
reinit();
} else if ((now - last_signal_check) > VIDEO_SIGNAL_CHECK_INTERVAL_MS) {
uint8_t sense;
// Adjust output format based on the current input format
_dev->read_registers(MAX7456ADD_STAT, &sense, 1);
if (sense & STAT_LOS) {
video_detect_time = 0;
} else {
if ((VIN_IS_PAL(sense) && VIDEO_MODE_IS_NTSC(video_signal_reg))
|| (VIN_IS_NTSC_ALT(sense) && VIDEO_MODE_IS_PAL(video_signal_reg))) {
if (video_detect_time) {
if (AP_HAL::millis() - video_detect_time > VIDEO_SIGNAL_DEBOUNCE_MS) {
reinit();
}
} else {
// Wait for signal to stabilize
video_detect_time = AP_HAL::millis();
}
}
}
last_signal_check = now;
}
_dev->get_semaphore()->give();
}
void AP_OSD_MAX7456::reinit()
{
uint8_t sense;
//do not init MAX before camera power up correctly
if (AP_HAL::millis() < 1500) {
return;
}
//check input signal format
_dev->read_registers(MAX7456ADD_STAT, &sense, 1);
if (VIN_IS_PAL(sense)) {
video_signal_reg = VIDEO_MODE_PAL | OSD_ENABLE;
video_lines = video_lines_pal;
_format = FORMAT_PAL;
} else {
video_signal_reg = VIDEO_MODE_NTSC | OSD_ENABLE;
video_lines = video_lines_ntsc;
_format = FORMAT_NTSC;
}
// set all rows to same character black/white level
for (uint8_t x = 0; x < video_lines_pal; x++) {
_dev->write_register(MAX7456ADD_RB0 + x, BWBRIGHTNESS);
}
// make sure the Max7456 is enabled
_dev->write_register(MAX7456ADD_VM0, video_signal_reg);
_dev->write_register(MAX7456ADD_VM1, BLINK_DUTY_CYCLE_50_50 | BLINK_TIME_3 | BACKGROUND_BRIGHTNESS_28);
_dev->write_register(MAX7456ADD_DMM, DMM_CLEAR_DISPLAY);
//write osd position
int8_t hos = constrain_int16(_osd.h_offset, 0, 63);
int8_t vos = constrain_int16(_osd.v_offset, 0, 31);
_dev->write_register(MAX7456ADD_HOS, hos);
_dev->write_register(MAX7456ADD_VOS, vos);
last_v_offset = _osd.v_offset;
last_h_offset = _osd.h_offset;
// force redrawing all screen
memset(shadow_frame, 0xFF, sizeof(shadow_frame));
initialized = true;
}
void AP_OSD_MAX7456::flush()
{
if (last_font != get_font_num()) {
update_font();
}
// check for offset changes
if (last_v_offset != _osd.v_offset) {
int8_t vos = constrain_int16(_osd.v_offset, 0, 31);
_dev->get_semaphore()->take_blocking();
_dev->write_register(MAX7456ADD_VOS, vos);
_dev->get_semaphore()->give();
last_v_offset = _osd.v_offset;
}
if (last_h_offset != _osd.h_offset) {
int8_t hos = constrain_int16(_osd.h_offset, 0, 63);
_dev->get_semaphore()->take_blocking();
_dev->write_register(MAX7456ADD_HOS, hos);
_dev->get_semaphore()->give();
last_h_offset = _osd.h_offset;
}
check_reinit();
transfer_frame();
}
void AP_OSD_MAX7456::transfer_frame()
{
uint16_t previous_pos = UINT16_MAX - 1;
bool autoincrement = false;
if (!initialized) {
return;
}
buffer_offset = 0;
for (uint8_t y=0; y<video_lines; y++) {
for (uint8_t x=0; x<video_columns; x++) {
if (!is_dirty(x, y)) {
continue;
}
//ensure space for 1 char and escape sequence
if (buffer_offset >= spi_buffer_size - 32) {
break;
}
shadow_frame[y][x] = frame[y][x];
uint8_t chr = frame[y][x];
uint16_t pos = y * video_columns + x;
bool position_changed = ((previous_pos + 1) != pos);
if (position_changed && autoincrement) {
//it is impossible to write to MAX7456ADD_DMAH/MAX7456ADD_DMAL in autoincrement mode
//so, exit autoincrement mode
buffer_add_cmd(MAX7456ADD_DMDI, 0xFF);
buffer_add_cmd(MAX7456ADD_DMM, 0);
autoincrement = false;
}
if (!autoincrement) {
buffer_add_cmd(MAX7456ADD_DMAH, pos >> 8);
buffer_add_cmd(MAX7456ADD_DMAL, pos & 0xFF);
}
if (!autoincrement && is_dirty(x+1, y)) {
//(re)enter autoincrement mode
buffer_add_cmd(MAX7456ADD_DMM, DMM_AUTOINCREMENT);
autoincrement = true;
}
buffer_add_cmd(MAX7456ADD_DMDI, chr);
previous_pos = pos;
}
}
if (autoincrement) {
buffer_add_cmd(MAX7456ADD_DMDI, 0xFF);
buffer_add_cmd(MAX7456ADD_DMM, 0);
autoincrement = false;
}
if (buffer_offset > 0) {
_dev->get_semaphore()->take_blocking();
_dev->transfer(buffer, buffer_offset, nullptr, 0);
_dev->get_semaphore()->give();
}
}
bool AP_OSD_MAX7456::is_dirty(uint8_t x, uint8_t y)
{
if (y>=video_lines || x>=video_columns) {
return false;
}
return frame[y][x] != shadow_frame[y][x];
}
void AP_OSD_MAX7456::clear()
{
AP_OSD_Backend::clear();
memset(frame, ' ', sizeof(frame));
}
void AP_OSD_MAX7456::write(uint8_t x, uint8_t y, const char* text)
{
if (y >= video_lines_pal || text == nullptr) {
return;
}
while ((x < VIDEO_COLUMNS) && (*text != 0)) {
frame[y][x] = *text;
++text;
++x;
}
}
// return a correction factor used to display angles correctly
float AP_OSD_MAX7456::get_aspect_ratio_correction() const
{
switch (_format) {
case FORMAT_NTSC:
return 12.0f/18.46f;
case FORMAT_PAL:
return 12.0f/15.0f;
default:
return 1.0f;
};
}
#endif // HAL_WITH_OSD_BITMAP