-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
87 lines (68 loc) · 2.18 KB
/
main.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
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/cm3/common.h>
#include <libopencm3/stm32/dbgmcu.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/l0/rcc.h>
// hack!
#define LIBOPENCM3_RTC_H
#include <libopencm3/stm32/f0/crs.h>
#define PWR_CR MMIO32(POWER_CONTROL_BASE + 0x00)
#define SYSCFG_CFGR3 MMIO32(0x40010000 + 0x20)
#include "usb_cdc.h"
extern "C" {
void usb_rx(char *data, int len) {
usb_tx("OK!\n", 4);
gpio_toggle(GPIOA, GPIO10);
if(len == 1 && *data == '!')
while(true);
}
}
int main() {
rcc_periph_clock_enable(RCC_GPIOA);
rcc_periph_clock_enable(RCC_GPIOB);
rcc_periph_clock_enable(RCC_SYSCFG);
// LEDs at PA9,10; USB at PA11,12, others input with pulldown
gpio_mode_setup(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_PULLDOWN, GPIO_ALL);
gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO9 | GPIO10);
gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO13 | GPIO14);
gpio_mode_setup(GPIOB, GPIO_MODE_INPUT, GPIO_PUPD_PULLDOWN, GPIO_ALL);
// enable debugging during wfi
DBGMCU_CR = DBGMCU_CR_STANDBY | DBGMCU_CR_STOP | DBGMCU_CR_SLEEP;
//PWR_CR = (1<<14) | (3<<11) | (1<<9) | (1<<8) | (1<<1) | (1<<0);
//PWR_CR = (3<<11) | (1<<8) | (1<<1) | (1<<0);
rcc_osc_on(HSI16);
rcc_wait_for_osc_ready(HSI16);
RCC_CFGR = RCC_CFGR_SW_SYSCLKSEL_HSI16CLK;
// Enable VREFINT for HSI48.
SYSCFG_CFGR3 |= (1 << 13) | (1 << 0); // ENREF_HSI48, EN_VREFINT
while(!(SYSCFG_CFGR3 & (1 << 26))); // REF_HSI48_RDYF
// Enable HSI48.
RCC_CRRCR |= 1 << 0; // HSI48ON
while(!(RCC_CRRCR & (1 << 1))); // HSI48RDY
// TODO figure out if the CRS actually works?
//rcc_periph_clock_enable(RCC_CRS);
//CRS_CR = CRS_CR_AUTOTRIMEN | CRS_CR_CEN;
RCC_CCIPR = RCC_CCIPR_HSI48SEL;
/*
RTC_WPR = 0xCA;
RTC_WPR = 0x53;
RTC_CR = RTC_CR_WUCLKSEL_RTC_DIV16;
while(!((RTC_ISR) & (RTC_ISR_WUTWF)))
;
RTC_WUTR = 2047;
RTC_CR = RTC_CR_WUCLKSEL_RTC_DIV16 | RTC_CR_WUTE;
RTC_WPR = 0xFF;
*/
gpio_set(GPIOA, GPIO10);
// make sure we never get interrupts
asm volatile("cpsid i");
// prepare wakeup events
nvic_enable_irq(NVIC_USB_IRQ);
usb_init();
while(true) {
asm volatile("wfi");
usb_poll();
nvic_clear_pending_irq(NVIC_USB_IRQ);
gpio_toggle(GPIOA, GPIO9);
}
}