-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathmain.c
More file actions
459 lines (412 loc) · 11.9 KB
/
main.c
File metadata and controls
459 lines (412 loc) · 11.9 KB
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
/*
This project 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.
Deviation 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 Deviation. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common.h"
#include "protocol/interface.h"
#include "mixer.h"
#include "gui/gui.h"
#include "buttons.h"
#include "timer.h"
#include "autodimmer.h"
#include "music.h"
#include "config/model.h"
#include "config/tx.h"
#include "config/display.h"
#include "rtc.h"
#include "extended_audio.h"
void Init();
void Banner();
void EventLoop();
volatile u8 priority_ready;
void TOUCH_Handler(); // temporarily in main()
void VIDEO_Update();
void PAGE_Test();
#ifdef TEST
#define main _main
#endif
#ifndef DUMP_BOOTLOADER
int main() {
Init();
#ifndef ENABLE_MODULAR
//Banner();
#endif
if(PWR_CheckPowerSwitch()) PWR_Shutdown();
LCD_Clear(0x0000);
#ifdef TEST_ADC
ADC_ScanChannels(); while(1);
#endif
u32 buttons = ScanButtons();
if (CHAN_ButtonIsPressed(buttons, BUT_ENTER) || !FS_Init()) {
LCD_DrawUSBLogo(LCD_WIDTH, LCD_HEIGHT);
USB_Connect();
LCD_Clear(0x0000);
FS_Init();
}
CONFIG_LoadTx();
SPI_ProtoInit();
CONFIG_ReadDisplay();
CONFIG_ReadModel(CONFIG_GetCurrentModel());
CONFIG_ReadLang(Transmitter.language);
BACKLIGHT_Brightness(Transmitter.backlight);
LCD_Contrast(Transmitter.contrast);
LCD_SetFont(DEFAULT_FONT.font);
LCD_SetFontColor(DEFAULT_FONT.font_color);
GUI_HandleButtons(1);
MIXER_Init();
PAGE_Init();
CLOCK_StartWatchdog();
#if HAS_DATALOG
DATALOG_Init();
#endif
priority_ready = 0;
CLOCK_SetMsecCallback(LOW_PRIORITY, LOW_PRIORITY_MSEC);
CLOCK_SetMsecCallback(MEDIUM_PRIORITY, MEDIUM_PRIORITY_MSEC);
// We need to wait until we've actually measured the ADC before proceeding
while(! (priority_ready & (1 << LOW_PRIORITY)))
PWR_Sleep();
//Only do this after we've initialized all channel data so the saftey works
PROTOCOL_InitModules();
GUI_DrawScreen();
// Add startup delay to make sure audio player is initialized
// AUDIO_Init() has already been called by CONFIG_ReadModel()
#if HAS_EXTENDED_AUDIO
audio_queue_time = CLOCK_getms() + 1500;
num_audio=1;
next_audio=1;
#if (LCD_WIDTH == 480) || (LCD_WIDTH == 320)
if(Display.background.drawn_background)
while(CLOCK_getms() < audio_queue_time - 1200);
#endif
AUDIO_SetVolume(); // Initial setting of voice volume
#endif
MUSIC_Play(MUSIC_STARTUP);
#ifdef HAS_EVENT_LOOP
start_event_loop();
#else
while(1) {
if(priority_ready) {
EventLoop();
}
//This does not appear to have any impact on power
//and has been disabled in common/devo/power.c
//but it helps a huge amount for the emulator
PWR_Sleep();
}
#endif
}
#else //DUMP_BOOTLOADER
#define SPI_BOOTLOADER 1
int main() {
PWR_Init();
LED_Init();
CLOCK_Init();
UART_Initialize();
if(PWR_CheckPowerSwitch()) PWR_Shutdown();
#if SPI_BOOTLOADER
Initialize_ButtonMatrix();
STORAGE_Init(); // This must come before LCD_Init() for 7e
STORAGE_WriteEnable(1); // Enable writing to all banks of storage
LCD_Init();
LCD_Clear(0x0000);
BACKLIGHT_Init();
BACKLIGHT_Brightness(5);
LCD_SetFont(0);
LCD_SetFontColor(0xffff);
dump_bootloader(0);
#else
dump_bootloader(1);
#endif
}
#endif //DUMP_BOOTLOADER
void Init() {
PWR_Init();
LED_Init();
CLOCK_Init();
UART_Initialize();
printf("Start\n");
Initialize_ButtonMatrix();
STORAGE_Init(); // This must come before LCD_Init() for 7e
#ifdef MEDIA_DRIVE
MEDIA_Init();
#endif
LCD_Init();
CHAN_Init();
SPITouch_Init();
SOUND_Init();
BACKLIGHT_Init();
BACKLIGHT_Brightness(1);
AUTODIMMER_Init();
STORAGE_WriteEnable(1); // Enable writing to all banks of storage
PPMin_TIM_Init();
#ifdef ENABLE_MODULAR
//Force protocol to none to initialize RAM
Model.protocol = PROTOCOL_NONE;
PROTOCOL_Init(1);
#endif
#if HAS_RTC
RTC_Init(); // Watchdog must be running in case something goes wrong (e.g no crystal)
#endif
}
void Banner()
{
CYRF_Reset();
u8 Power = CYRF_MaxPower();
u8 mfgdata[6];
u8 tmp[12];
TxName(tmp, 12);
printf("\nDeviation\n");
/* Check CPU type */
printf("BootLoader : '%s'\n",tmp);
printf("Power : '%s'\n",Power == CYRF_PWR_100MW ? "100mW" : "10mW" );
printf("SPI Flash : '%X'\n", STORAGE_ReadID());
CYRF_GetMfgData(mfgdata);
printf("CYRF Mfg Data : '%02X %02X %02X %02X %02X %02X'\n",
mfgdata[0],
mfgdata[1],
mfgdata[2],
mfgdata[3],
mfgdata[4],
mfgdata[5]);
}
void EventLoop()
{
CLOCK_ResetWatchdog();
#ifdef HEAP_DEBUG
static int heap = 0;
int h = _sbrk_r(NULL, 0);
if(h > heap) {
printf("heap: %x\n", h);
heap = h;
}
#endif
#ifdef TIMING_DEBUG
debug_timing(0, 0);
#endif
priority_ready &= ~(1 << MEDIUM_PRIORITY);
#if !HAS_HARD_POWER_OFF
if(PWR_CheckPowerSwitch()) {
if(! (BATTERY_Check() & BATTERY_CRITICAL)) {
PAGE_Test();
CONFIG_SaveModelIfNeeded();
CONFIG_SaveTxIfNeeded();
}
if(Transmitter.music_shutdown) {
#if HAS_EXTENDED_AUDIO
MUSIC_Play(MUSIC_SHUTDOWN);
while (CLOCK_getms() < audio_queue_time) {
// Wait for voice to finished
CLOCK_ResetWatchdog();
}
#else
// We wait ~1sec for shutdown buzzer music finished
unsigned int time;
MUSIC_Play(MUSIC_SHUTDOWN);
time = CLOCK_getms()+700;
while (CLOCK_getms() < time) {
CLOCK_ResetWatchdog();
}
#endif
}
PWR_Shutdown();
}
#endif
BUTTON_Handler();
TOUCH_Handler();
INPUT_CheckChanges();
if (priority_ready & (1 << LOW_PRIORITY)) {
priority_ready &= ~(1 << LOW_PRIORITY);
PAGE_Event();
PROTOCOL_CheckDialogs();
TIMER_Update();
TELEMETRY_Alarm();
BATTERY_Check();
AUTODIMMER_Update();
#if HAS_DATALOG
DATALOG_Update();
#endif
#if HAS_VIDEO
VIDEO_Update();
#endif
#if HAS_EXTENDED_AUDIO
AUDIO_CheckQueue();
#endif
GUI_RefreshScreen();
#if HAS_HARD_POWER_OFF
if (PAGE_ModelDoneEditing())
CONFIG_SaveModelIfNeeded();
CONFIG_SaveTxIfNeeded();
#endif
}
#ifdef TIMING_DEBUG
debug_timing(0, 1);
#endif
}
void TOUCH_Handler() {
if(! HAS_TOUCH)
return;
u32 pen_down=0;
static u32 pen_down_last=0;
static u32 pen_down_long_at=0;
struct touch t;
if(SPITouch_IRQ()) {
pen_down=1;
t=SPITouch_GetCoords();
if (! pen_down_last)
pen_down_long_at=CLOCK_getms()+500;
} else {
pen_down=0;
}
if(pen_down && (!pen_down_last)) {
AUTODIMMER_Check();
GUI_CheckTouch(&t, 0);
}
if(!pen_down && pen_down_last) {
GUI_TouchRelease();
}
if(pen_down && pen_down_last) {
if(CLOCK_getms()>pen_down_long_at) {
GUI_CheckTouch(&t, 1);
pen_down_long_at += 100;
}
}
pen_down_last=pen_down;
}
#if HAS_VIDEO
void VIDEO_Update()
{
static u8 video_enable = 0;
static u32 check_standard_ms = 0;
// Check if Video is turn on
int enabled = MIXER_SourceAsBoolean(Model.videosrc);
if (enabled != video_enable) {
VIDEO_Enable(enabled);
video_enable = enabled;
if (enabled) {
VIDEO_SetChannel(Model.videoch);
VIDEO_Contrast(Model.video_contrast);
VIDEO_Brightness(Model.video_brightness);
check_standard_ms = CLOCK_getms() + 3000;
}
else
check_standard_ms = 0;
}
if(video_enable &&
check_standard_ms > 0
&& check_standard_ms < CLOCK_getms()) {
u8 video_standard_current = VIDEO_GetStandard();
if((video_standard_current > 0) &&
(video_standard_current < 8)) {
VIDEO_SetStandard(video_standard_current);
check_standard_ms = 0;
}
else {
check_standard_ms = CLOCK_getms() + 3000;
}
}
if(video_enable)
AUTODIMMER_Check();
}
#endif //HAS_VIDEO
#ifdef TIMING_DEBUG
void debug_timing(u32 type, int startend)
{
static u32 last_time[2][100];
static u32 loop_time[4][101];
static u32 loop_pos[4] = {-1, -1, -1, -1};
static u32 max_last[2];
static u32 max_loop[4];
static int save_priority;
if (type == 0) {
if (! startend)
save_priority = priority_ready;
if (save_priority & (1 << MEDIUM_PRIORITY))
debug_timing(2, startend);
if (save_priority & (1 << LOW_PRIORITY))
debug_timing(1, startend);
return;
}
type--;
if (! startend) {
u32 t = CLOCK_getms();
loop_pos[type] = (loop_pos[type] + 1) % 100;
if (type < 2) {
last_time[type][loop_pos[type]] = t;
if (t - last_time[type][(loop_pos[type] + 99) % 100] > max_last[type])
max_last[type] = t - last_time[type][(loop_pos[type] + 99) % 100];
}
loop_time[type][100] = t;
} else {
loop_time[type][loop_pos[type]] = CLOCK_getms() - loop_time[type][100];
if (loop_time[type][loop_pos[type]] > max_loop[type])
max_loop[type] = loop_time[type][loop_pos[type]];
if (type == 0 && loop_pos[0] == 99) {
unsigned avg_loop[4] = {0, 0, 0, 0};
unsigned avg_last[2] = {0, 0};
for(int i = 0; i < 99; i++) {
for(int t = 0; t < 2; t++) {
u32 delay = last_time[t][(i + loop_pos[t] + 2) % 100] - last_time[t][(i + loop_pos[t] + 1) % 100];
avg_last[t] += delay;
}
for(int t = 0; t < 4; t++)
avg_loop[t] += loop_time[t][i];
}
for(int t = 0; t < 4; t++)
avg_loop[t] /= 99;
avg_last[0] /= 99;
avg_last[1] /= 99;
printf("Avg: radio: %d mix: %d med: %d/%d low: %d/%d\n", avg_loop[3], avg_loop[2], avg_loop[1], avg_last[1], avg_loop[0], avg_last[0]);
printf("Max: radio: %d mix: %d med: %d/%d low: %d/%d\n", max_loop[3], max_loop[2], max_loop[1], max_last[1], max_loop[0], max_last[0]);
memset(max_loop, 0, sizeof(max_loop));
max_last[0] = 0;
max_last[1] = 0;
}
}
}
#endif
void debug_switches()
{
s32 data[INP_LAST];
for(int i = INP_HAS_CALIBRATION+1; i < INP_LAST; i++) {
data[i] = CHAN_ReadRawInput(i);
}
while(1) {
u32 changed = 0;
for(int i = INP_HAS_CALIBRATION+1; i < INP_LAST; i++) {
s32 val = CHAN_ReadRawInput(i);
if (val != data[i]) {
printf("%s=%d ", INPUT_SourceName(tempstring, i), val);
data[i] = val;
changed = 1;
}
}
if (changed) { printf("\n"); }
if(PWR_CheckPowerSwitch()) PWR_Shutdown();
}
}
void debug_buttons()
{
u32 data = ScanButtons();
while(1) {
u32 val = ScanButtons();
u32 delta = val ^ data;
for(int i = 1; i < BUT_LAST; i++) {
if(delta & (1 << (i-1))) {
printf("%s(%s) ", INPUT_ButtonName(i), (val &(1 << (i-1)))? "Down" : "Up");
}
}
if (delta) {
printf("\n");
data = val;
}
if(PWR_CheckPowerSwitch()) PWR_Shutdown();
}
}