This repository has been archived by the owner on Oct 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.c
61 lines (47 loc) · 1.81 KB
/
button.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
#include <stdbool.h>
#include "button.h"
#include "smps.h"
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/rcc.h>
#define INT_PORT GPIOC
#define INT_PIN GPIO14
#define EXT_PORT GPIOC
#define EXT_PIN GPIO15
#define delay(x) do { for (int i = 0; i < x * 1000; i++) \
__asm__("nop"); \
} while(0)
void button_init(void) {
gpio_set(INT_PORT, INT_PIN); // Pull-up
gpio_set_mode(INT_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, INT_PIN);
gpio_set(EXT_PORT, EXT_PIN); // Pull-up
gpio_set_mode(EXT_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, EXT_PIN);
}
bool button_int_read(void) { return gpio_get(INT_PORT, INT_PIN); }
bool button_ext_read(void) { return gpio_get(EXT_PORT, EXT_PIN); }
bool force_bootloader() __attribute__((section(".bootloader")));
bool force_bootloader()
{
// Enable GPIOC
rcc_periph_clock_enable(RCC_GPIOC);
// HACK TIME: When the bootloader runs, we need to turn the 5v rail smps on.
// This is because if we stick in the bootloader, the odroid needs to be
// brought up so that it can actually flash the power board. If we didn't,
// we'd be stuck in a state where the power board is on, nothing else is,
// thus nothing can cause the power board to make progress.
smps_on_boot();
// Function to check whether we should force the bootloader.
// Specifically, do that if the start button is pressed on startup
gpio_set(INT_PORT, INT_PIN); // Pull-up
gpio_set_mode(INT_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, INT_PIN);
gpio_set(EXT_PORT, EXT_PIN); // Pull-up
gpio_set_mode(EXT_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, EXT_PIN);
// Rise time
delay(10);
if (!button_int_read() && !button_ext_read())
return true;
return false;
}
bool button_pressed(void)
{
return !button_int_read() || !button_ext_read();
}