-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathholly.c
More file actions
74 lines (61 loc) · 1.85 KB
/
holly.c
File metadata and controls
74 lines (61 loc) · 1.85 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
// Copyright 2022 Jay Greco
// SPDX-License-Identifier: GPL-2.0-or-later
#include "holly.h"
#include "flash.h"
#include "usb_util.h"
#include "analog.h"
static void disable_interrupts(void)
{
SysTick_CTRL &= ~1;
NVIC_ICER = 0xFFFFFFFF;
NVIC_ICPR = 0xFFFFFFFF;
}
static void reset_peripherals(void)
{
RESET = ~(
RESETS_RESET_IO_QSPI_BITS |
RESETS_RESET_PADS_QSPI_BITS |
RESETS_RESET_SYSCFG_BITS |
RESETS_RESET_PLL_SYS_BITS
);
}
static void jump_to_vtor(uint32_t vtor)
{
uint32_t reset_vector = *(volatile uint32_t *)(vtor + 0x04);
SCB_VTOR = (volatile uint32_t)(vtor);
asm volatile("msr msp, %0"::"g" (*(volatile uint32_t *)vtor));
asm volatile("bx %0"::"r" (reset_vector));
}
void keyboard_post_init_kb(void) {
#ifdef CONSOLE_ENABLE
debug_enable = true;
debug_matrix = true;
#endif
keyboard_post_init_user();
#define SAMPLE_PERIODS 10
#define MIN_READINGS 800
int rolling_sum = 0;
for (int i=0; i<SAMPLE_PERIODS; i++) {
uint16_t reading = analogReadPin(VIN_PIN);
rolling_sum += (int)reading;
dprintf("adc reading: %u\n", reading);
wait_ms(100);
}
rolling_sum /= SAMPLE_PERIODS;
dprintf("final avg: %d\n", rolling_sum);
// If either of the following are true, assume that we're running on battery power
// Jump into the low-power optimized firmware, which will last much longer
if (!usb_connected_state() || rolling_sum < MIN_READINGS) {
dprintf("jumping to: %lu\n", (uint32_t)&bindata);
wait_ms(100);
disable_interrupts();
reset_peripherals();
jump_to_vtor((uint32_t)&bindata);
} else {
// Turn on the RGB LED power supplies
setPinOutput(LED_VCC1);
setPinOutput(LED_VCC2);
writePinHigh(LED_VCC1);
writePinHigh(LED_VCC2);
}
}