summaryrefslogtreecommitdiff
path: root/fw/main.c
blob: 7db22ceb0270474cd8cc7744b145b135c93d3ea3 (plain)
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/* 8seg LED display driver firmware
 * Copyright (C) 2018 Sebastian Götte <code@jaseg.net>
 * 
 * This program 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 program 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/>.
 */


#include "global.h"
#include "serial.h"
#include "i2c.h"
#include "lcd1602.h"

#include "mini-printf.h"

/* Part number: STM32F030F4C6 */

volatile unsigned int comm_led_ctr, err_led_ctr;
volatile unsigned int sys_time_tick = 0;
volatile unsigned int sys_time_ms;
volatile unsigned int sys_time_s;
volatile unsigned int sys_flag_1Hz;
unsigned int frame_duration_us;
volatile uint8_t global_brightness; /* FIXME implement sending */
volatile bool display_update_flag;
volatile bool adc_trigger_flag = 0;
volatile bool backchannel_trigger_flag = 0;

void run_menu(void);
void update_display(void);
void calc_next_alarm(void);
void update_servo(void);
void servo_activate(void);
void servo_deactivate(void);

uint8_t random() {
    static uint8_t x, a, b, c;
    x++; //x is incremented every round and is not affected by any other variable
    a = (a ^ c ^ x); //note the mix of addition and XOR
    b = (b + a); //And the use of very few instructions
    c = ((c + ((b >> 1) ^ a))); // the AES S-Box Operation ensures an even distributon of entropy
    return c;
}

volatile int input_hold_total[5] = {0};
volatile int input_triggered = 0;
volatile int input_state = 0;

void rtc_adjust(int field, int adj) {
    RTC->ISR = RTC_ISR_INIT;
    while (!(RTC->ISR & RTC_ISR_INITF))
        ;

    int offx = field*8;
    int fmax = field == 2 ? 24 : 60;

    int tr = RTC->TR;
    int fval = (tr >> offx) & 0xff;

    fval = (fval&0xf) + 10*(fval>>4);
    
    if (field == 0) {
        /* seconds field special handling: set seconds to 0 when pressing down, ignore up */
        if (adj < 0)
            fval = 0;
    } else {
        fval += adj;
    }

    if (fval < 0)
        fval = fmax-1;
    if (fval >= fmax)
        fval = 0;

    fval = ((fval/10)<<4) | (fval % 10);

    RTC->TR = (tr & ~(0xff<<offx)) | (fval << offx);
    RTC->ISR = 0;
    while (!(RTC->ISR & RTC_ISR_RSF))
        ;
}

int main(void) {
    /* Startup code */
    RCC->CR |= RCC_CR_HSEON;
    while (!(RCC->CR&RCC_CR_HSERDY));
    RCC->CFGR &= ~RCC_CFGR_PLLMUL_Msk & ~RCC_CFGR_SW_Msk & ~RCC_CFGR_PPRE_Msk & ~RCC_CFGR_HPRE_Msk;
    RCC->CFGR |= ((6-2)<<RCC_CFGR_PLLMUL_Pos) | RCC_CFGR_PLLSRC_HSE_PREDIV; /* PLL x6 -> 48.0MHz */
    RCC->CR |= RCC_CR_PLLON;
    while (!(RCC->CR&RCC_CR_PLLRDY));
    RCC->CFGR |= (2<<RCC_CFGR_SW_Pos);
    RCC->AHBENR  |= RCC_AHBENR_DMAEN | RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN | RCC_AHBENR_FLITFEN;
    RCC->APB1ENR |= RCC_APB1ENR_TIM3EN | RCC_APB1ENR_PWREN | RCC_APB1ENR_I2C1EN;
    RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN | RCC_APB2ENR_ADCEN| RCC_APB2ENR_DBGMCUEN | RCC_APB2ENR_USART1EN | RCC_APB2ENR_SPI1EN | RCC_APB2ENR_TIM1EN;
    RCC->CFGR3 |= RCC_CFGR3_I2C1SW; /* Route 48MHz system clock to I2C1 */

    PWR->CR |= PWR_CR_DBP;
    RCC->BDCR = RCC_BDCR_BDRST;
    RCC->BDCR = 0;
    RCC->BDCR = RCC_BDCR_RTCEN | (3<<RCC_BDCR_RTCSEL_Pos);

    RTC->WPR = 0xca;
    RTC->WPR = 0x53;
    RTC->ISR = RTC_ISR_INIT;
    while (!(RTC->ISR & RTC_ISR_INITF))
        ;
    RTC->PRER = (99 << RTC_PRER_PREDIV_A_Pos) | (2499 << RTC_PRER_PREDIV_S_Pos);
    RTC->SSR = 0;
    RTC->TR = 0;
    RTC->DR = 0;
    RTC->TSTR = 0;
    RTC->TSDR = 0;
    RTC->CR |= RTC_CR_ALRAIE;
    EXTI->IMR |= 1<<17;
    EXTI->RTSR |= 1<<17;
    RTC->ISR = 0;
    while (!(RTC->ISR & RTC_ISR_RSF))
        ;

    SystemCoreClockUpdate();
    SysTick_Config(SystemCoreClock/(1000/TICK_MS)); /* 10ms interval */
    NVIC_EnableIRQ(SysTick_IRQn);
    NVIC_SetPriority(SysTick_IRQn, 3<<5);

    /* GPIO setup
     *
     * Note: since we have quite a bunch of pin constraints we can't actually use complementary outputs for the
     * complementary MOSFET driver control signals (CTRL_A & CTRL_B). Instead, we use two totally separate output
     * channels (1 & 4) and emulate the dead-time generator in software.
     */
    GPIOA->MODER |=
          (2<<GPIO_MODER_MODER6_Pos)  /* PA6  - servo / TIM3_CH1 */
        | (2<<GPIO_MODER_MODER9_Pos)  /* PA9  - SCL */
        | (2<<GPIO_MODER_MODER10_Pos);/* PA10 - SDA */

    GPIOA->AFR[0] =
          (1<<GPIO_AFRL_AFSEL6_Pos); /* PA6 */
    GPIOA->AFR[1] =
          (4<<GPIO_AFRH_AFSEL9_Pos)  /* PA9 */
        | (4<<GPIO_AFRH_AFSEL10_Pos);/* PA10 */

    for (int i=0; i<5; i++)
        GPIOA->PUPDR |= 1<<(2*i);

    /* I2C for LCD, temp sensor, current sensor */
    i2c_config_filters(I2C1, I2C_AF_ENABLE, 0);
    i2c_config_timing(I2C1, 0x50330309); /* Magic value for 400kHz I2C @ 48MHz CLK. From the datasheet. The other option
                                            is to have this fall out of STMCubeMX. I love downloading 120MB of software
                                            to download another 100MB of software, only this time over unsecured HTTP,
                                            to generate 3.5 bytes of configuration values using a Java(TM) GUI. */
    for (int i=0; i<2000000; i++)
        ;
    i2c_enable(I2C1);
    lcd1602_init();

    lcd_write_str(0, 0, "timer initialized \xbc" LCD_FILL);
    lcd_write_str(0, 1, LCD_FILL);

    /* servo output */
    TIM1->CR1 = TIM_CR1_CEN;
    TIM1->PSC = 48-1; /* 1us ticks */
    TIM1->ARR = 1000-1; /* 1ms interval */
    TIM1->EGR = TIM_EGR_UG;
    TIM1->DIER = TIM_DIER_UIE;

    TIM3->CR1 = TIM_CR1_CEN;
    TIM3->PSC = 48-1; /* 1us ticks */
    TIM3->ARR = 20000-1; /* 20ms interval */
    TIM3->CCMR1 = (6<<TIM_CCMR1_OC1M_Pos) | TIM_CCMR1_OC1PE;
    TIM3->CCER = TIM_CCER_CC1E;
    TIM3->EGR = TIM_EGR_UG;

    NVIC_EnableIRQ(TIM1_BRK_UP_TRG_COM_IRQn);
    NVIC_EnableIRQ(RTC_IRQn);
    calc_next_alarm();

    uint32_t last_update_ms = 0;
    const int display_update_ivl_ms = 50;
    while(23) {
        int delta = sys_time_ms - last_update_ms;
        if (delta < 0)
            delta += 1000;
        if (delta > display_update_ivl_ms)
            update_display();
    }
}

enum {
    LVL_SERVO_OFF,
    LVL_SERVO_IDLE,
    LVL_SERVO_ON,
    NUM_SERVO_LEVELS
};

int servo_lvl_set[NUM_SERVO_LEVELS] = {
    [LVL_SERVO_ON] = 1050,
    [LVL_SERVO_IDLE] = 1250,
    [LVL_SERVO_OFF] = 1390,
};

const char *servo_lvl_string[NUM_SERVO_LEVELS] = {
    [LVL_SERVO_OFF] = "OFF",
    [LVL_SERVO_IDLE] = "IDLE",
    [LVL_SERVO_ON] = "ON",
};

const int servo_reps_set = 3;
const int servo_lvl_set_ms[] = { 1000, 1000, 1500, 1500, 1000, 1000 };
int servo_lvl_ms = 0;
int servo_reps = 0;

enum {
    MENU_DEFAULT,
    MENU_SET_TIME,
    MENU_SET_ALARM,
    MENU_SET_SERVO_LVL,
} menu_state;

int menu_adjustment_done = 0;
int sel_field_idx = 0;
int menu_alarm = 0;
int menu_servo_lvl = 0;

enum {
    INPUT_LEFT,
    INPUT_DOWN,
    INPUT_UP,
    INPUT_RIGHT,
    INPUT_MENU
};

enum {
    ALR_TON,
    ALR_TOFF,
    NUM_ALARMS
};

int alarms[NUM_ALARMS] = {
    9<<16 | 0<<8 | 0,
    20<<16 | 0<<8 | 0,
};
const char *alarm_names[NUM_ALARMS] = {" ON", "OFF"};
int next_alarm = -1;

void RTC_IRQHandler() {
    RTC->ISR &= ~RTC_ISR_ALRAF;
    EXTI->PR = 1<<17;
    calc_next_alarm();
    /* TODO FIXME handle alarm */
}

void servo_activate() {
    if (next_alarm == ALR_TON)
        TIM3->CCR1 = servo_lvl_set[LVL_SERVO_ON];
    else
        TIM3->CCR1 = servo_lvl_set[LVL_SERVO_OFF];
}

void servo_deactivate() {
    TIM3->CCR1 = servo_lvl_set[LVL_SERVO_IDLE];
}

void update_servo() {
    servo_activate();
    servo_reps = servo_reps_set * 2 - 1;
    servo_lvl_ms = servo_lvl_set_ms[servo_reps_set];
}

void adj_alarm(int alarm, int sel, int adj) {
    int fields[3] = { alarms[alarm]>>16, (alarms[alarm]>>8) & 0xff, alarms[alarm] & 0xff };
    int max[3] = { 24, 60, 60 };
    sel = 2-sel;

    fields[sel] += adj;
    if (fields[sel] < 0)
        fields[sel] += max[sel];
    if (fields[sel] >= max[sel])
        fields[sel] -= max[sel];

    alarms[alarm] = (fields[0]<<16) | (fields[1]<<8) | fields[2];
}

void calc_next_alarm() {
    int tr = RTC->TR;
    int h = ((tr>>RTC_TR_HT_Pos)&3)*10 + ((tr>>RTC_TR_HU_Pos)&0xf),
        m = ((tr>>RTC_TR_MNT_Pos)&7)*10 + ((tr>>RTC_TR_MNU_Pos)&0xf),
        s = ((tr>>RTC_TR_ST_Pos)&7)*10 +  ((tr>>RTC_TR_SU_Pos)&0xf);
    int tr_al_fmt = h<<16 | m<<8 | s;

    int min_alr = 0x7fffffff;
    int min_idx = -1;
    for (int i=0; i<NUM_ALARMS; i++) {
        if (alarms[i] > tr_al_fmt && alarms[i] < min_alr) {
            min_alr = alarms[i];
            min_idx = i;
        }
    }

    if (min_idx == -1) { /* handle rollover */
        for (int i=0; i<NUM_ALARMS; i++) {
            if (alarms[i] < min_alr) {
                min_alr = alarms[i];
                min_idx = i;
            }
        }
    }

    next_alarm = min_idx;
    RTC->CR &= ~RTC_CR_ALRAE;
    while (!(RTC->ISR & RTC_ISR_ALRAWF))
        ;
    h = min_alr>>16, m = (min_alr>>8)&0xff, s = min_alr & 0xff;
    RTC->ALRMAR = RTC_ALRMAR_MSK4
        | (h/10 << RTC_ALRMAR_HT_Pos) | (h%10 << RTC_ALRMAR_HU_Pos)
        | (m/10 << RTC_ALRMAR_MNT_Pos) | (m%10 << RTC_ALRMAR_MNU_Pos)
        | (s/10 << RTC_ALRMAR_ST_Pos) | (s%10 << RTC_ALRMAR_SU_Pos);
    RTC->ISR &= ~RTC_ISR_ALRAF;
    RTC->CR |= RTC_CR_ALRAE;

    update_servo();
}

void update_display() {
    static int updates_since_init = 0;
    char buf[17];
    bool blink_flag = sys_time_ms < 300;
    int tr = RTC->TR;

    updates_since_init++;
    if (updates_since_init > 20000) {
        lcd1602_init();
        updates_since_init = 0;
    }

    switch (menu_state) {
        case MENU_DEFAULT:
            mini_snprintf(buf, sizeof(buf), "%d%d:%d%d:%d%d" LCD_FILL,
                    (tr>>RTC_TR_HT_Pos)&3,  (tr>>RTC_TR_HU_Pos)&0xf,
                    (tr>>RTC_TR_MNT_Pos)&7, (tr>>RTC_TR_MNU_Pos)&0xf,
                    (tr>>RTC_TR_ST_Pos)&7,  (tr>>RTC_TR_SU_Pos)&0xf);
            lcd_write_str(0, 0, buf);
            if (next_alarm < 0) {
                mini_snprintf(buf, sizeof(buf), LCD_FILL);
            } else {
                int next = alarms[next_alarm];
                int h = next>>16, m = (next>>8) & 0xff,  s = next & 0xff;
                mini_snprintf(buf, sizeof(buf), "%s AT %02d:%02d:%02d" LCD_FILL, alarm_names[next_alarm], h, m, s);
            }
            lcd_write_str(0, 1, buf);
            break;

        case MENU_SET_TIME:
            mini_snprintf(buf, sizeof(buf), "%d%d:%d%d:%d%d" LCD_FILL,
                    (tr>>RTC_TR_HT_Pos)&3,  (tr>>RTC_TR_HU_Pos)&0xf,
                    (tr>>RTC_TR_MNT_Pos)&7, (tr>>RTC_TR_MNU_Pos)&0xf,
                    (tr>>RTC_TR_ST_Pos)&7,  (tr>>RTC_TR_SU_Pos)&0xf);
            if (sel_field_idx == 0 && blink_flag)
                buf[6] = buf[7] = ' ';
            if (sel_field_idx == 1 && blink_flag)
                buf[3] = buf[4] = ' ';
            if (sel_field_idx == 2 && blink_flag)
                buf[0] = buf[1] = ' ';
            lcd_write_str(0, 0, buf);
            mini_snprintf(buf, sizeof(buf), "SET TIME" LCD_FILL);
            lcd_write_str(0, 1, buf);
            break;

        case MENU_SET_ALARM:
            mini_snprintf(buf, sizeof(buf), "%02d:%02d:%02d" LCD_FILL,
                    alarms[menu_alarm]>>16, (alarms[menu_alarm]>>8) & 0xff,  alarms[menu_alarm] & 0xff);
            if (sel_field_idx == 0 && blink_flag)
                buf[6] = buf[7] = ' ';
            if (sel_field_idx == 1 && blink_flag)
                buf[3] = buf[4] = ' ';
            if (sel_field_idx == 2 && blink_flag)
                buf[0] = buf[1] = ' ';
            lcd_write_str(0, 0, buf);
            mini_snprintf(buf, sizeof(buf), "SET %s" LCD_FILL, alarm_names[menu_alarm]);
            lcd_write_str(0, 1, buf);
            break;
        
        case MENU_SET_SERVO_LVL:
            mini_snprintf(buf, sizeof(buf), "%04d" LCD_FILL, servo_lvl_set[menu_servo_lvl]);
            lcd_write_str(0, 0, buf);
            mini_snprintf(buf, sizeof(buf), "SET %s POS" LCD_FILL, servo_lvl_string[menu_servo_lvl]);
            lcd_write_str(0, 1, buf);
            break;
    }
}

void run_menu() {

    const int enter_program_mode_ms = 1000;

    int btn = input_triggered;
    input_triggered = 0;

    switch (menu_state) {
        case MENU_DEFAULT:
            if (input_state == (1<<INPUT_MENU) && input_hold_total[INPUT_MENU] >= enter_program_mode_ms) {
                menu_state = MENU_SET_TIME;
                sel_field_idx = 0;
                menu_adjustment_done = 0;
            }
            break;

        case MENU_SET_TIME:
            if (btn & (1<<INPUT_MENU)) {
                if (menu_adjustment_done) {
                    menu_state = MENU_DEFAULT;
                    calc_next_alarm();
                } else {
                    menu_state = MENU_SET_ALARM;
                    sel_field_idx = 2;
                    menu_alarm = 0;
                }
                break;
            }

            if (btn & (1<<INPUT_LEFT)) {
                menu_adjustment_done = 1;
                sel_field_idx++;
                if (sel_field_idx > 2)
                    sel_field_idx = 0;
            }
            if (btn & (1<<INPUT_RIGHT)) {
                menu_adjustment_done = 1;
                sel_field_idx--;
                if (sel_field_idx < 0)
                    sel_field_idx = 2;
            }

            if (btn & (1<<INPUT_UP)) {
                menu_adjustment_done = 1;
                rtc_adjust(sel_field_idx, 1);
            }
            if (btn & (1<<INPUT_DOWN)) {
                menu_adjustment_done = 1;
                rtc_adjust(sel_field_idx, -1);
            }
            break;

        case MENU_SET_ALARM:
            if (btn & (1<<INPUT_MENU)) {
                if (menu_adjustment_done) {
                    menu_state = MENU_DEFAULT;
                    calc_next_alarm();
                } else {
                    sel_field_idx = 2;
                    menu_alarm++;
                    if (menu_alarm == NUM_ALARMS) {
                        menu_state = MENU_SET_SERVO_LVL;
                        menu_servo_lvl = 0;
                    }
                }
                break;
            }

            if (btn & (1<<INPUT_LEFT)) {
                menu_adjustment_done = 1;
                sel_field_idx++;
                if (sel_field_idx > 2)
                    sel_field_idx = 0;
            }
            if (btn & (1<<INPUT_RIGHT)) {
                menu_adjustment_done = 1;
                sel_field_idx--;
                if (sel_field_idx < 0)
                    sel_field_idx = 2;
            }

            if (btn & (1<<INPUT_UP)) {
                menu_adjustment_done = 1;
                adj_alarm(menu_alarm, sel_field_idx, 1);
            }
            if (btn & (1<<INPUT_DOWN)) {
                menu_adjustment_done = 1;
                adj_alarm(menu_alarm, sel_field_idx, -1);
            }
            break;

        case MENU_SET_SERVO_LVL:
            if (btn & (1<<INPUT_MENU)) {
                if (menu_adjustment_done) {
                    menu_state = MENU_DEFAULT;
                    update_servo();
                } else {
                    menu_servo_lvl++;
                    if (menu_servo_lvl == NUM_SERVO_LEVELS)
                        menu_state = MENU_DEFAULT;
                }
                break;
            }

            if (btn & (1<<INPUT_UP)) {
                menu_adjustment_done = 1;
                if (input_hold_total[INPUT_UP] < 3000) {
                    servo_lvl_set[menu_servo_lvl]++;
                } else {
                    servo_lvl_set[menu_servo_lvl] += 10;
                    servo_lvl_set[menu_servo_lvl] -= servo_lvl_set[menu_servo_lvl]%10;
                }
                TIM3->CCR1 = servo_lvl_set[menu_servo_lvl];
            }
            if (btn & (1<<INPUT_DOWN)) {
                menu_adjustment_done = 1;
                if (input_hold_total[INPUT_DOWN] < 3000) {
                    servo_lvl_set[menu_servo_lvl]--;
                } else {
                    servo_lvl_set[menu_servo_lvl] -= 10;
                    servo_lvl_set[menu_servo_lvl] += (10 - servo_lvl_set[menu_servo_lvl]%10) % 10;
                }
                TIM3->CCR1 = servo_lvl_set[menu_servo_lvl];
            }
            if (servo_lvl_set[menu_servo_lvl] < 500)
                servo_lvl_set[menu_servo_lvl] = 500;
            if (servo_lvl_set[menu_servo_lvl] > 2500)
                servo_lvl_set[menu_servo_lvl] = 2500;
            break;
    }
}

void TIM1_BRK_UP_TRG_COM_IRQHandler() { /* every 1ms */
    TIM1->SR &= ~TIM_SR_UIF;
    int inputs = (~GPIOA->IDR) & 0x1f;
    input_state = inputs;
    static int input_debounce[5] = {0};
    static int input_hold[5] = {0};

    const int input_debounce_time_ms = 20;
    const int input_hold_retrigger_backoff_ms = 1000;
    const int input_hold_retrigger_ms = 120;

    for (int i=0; i<5; i++) {
        int bit = inputs&1;
        inputs >>= 1;

        if (bit) {
            if (!input_debounce[i]) {
                input_triggered |= 1<<i;
            }
            input_debounce[i] = input_debounce_time_ms;

            input_hold_total[i]++;
            if (i == INPUT_UP || i == INPUT_DOWN) {
                /* retrigger for UP and DOWN buttons */
                if (input_hold_total[i] > input_hold_retrigger_backoff_ms) {
                    input_hold[i]++;
                    if (input_hold[i] == input_hold_retrigger_ms) {
                        input_hold[i] = 0;
                        input_triggered |= 1<<i;
                    }
                }
            }
        } else {
            if (input_debounce[i])
                input_debounce[i]--;
            input_hold[i] = 0;
            input_hold_total[i] = 0;
        }
    }

    run_menu();
}

void NMI_Handler(void) {
}

void HardFault_Handler(void) __attribute__((naked));
void HardFault_Handler() {
    asm volatile ("bkpt");
}

void SVC_Handler(void) {
}


void PendSV_Handler(void) {
}

void SysTick_Handler(void) {
    sys_time_tick++;
    sys_time_ms += TICK_MS;
    if (sys_time_ms++ >= 1000) {
        sys_time_ms = 0;
        sys_time_s++;
        sys_flag_1Hz = 1;
    }

    if (servo_lvl_ms > 0) {
        servo_lvl_ms -= TICK_MS;
        if (servo_lvl_ms <= 0) {
            
            if (servo_reps & 1)
                servo_activate();
            else
                servo_deactivate();

            if (servo_reps > 0) {
                servo_reps --;
                servo_lvl_ms = servo_lvl_set_ms[servo_reps_set];
            } else {
                servo_lvl_ms = 0;
            }
        }
    }
}

void _init(void) {
}

void BusFault_Handler(void) __attribute__((naked));
void BusFault_Handler() {
    asm volatile ("bkpt");
}