-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.c
121 lines (95 loc) · 2.71 KB
/
clock.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
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
#include <stdbool.h>
#include "stm32f405.h"
#include "clock.h"
static void delay_1s_loop(void);
static bool clock_initialized = false;
void delay1s(void)
{
if (!clock_initialized)
{
delay_1s_loop();
while (true)
;
}
else
{
//TODO: use a proper timer
uint32_t end_time = get_time_us() + 1000000;
while (((int32_t)(get_time_us() - end_time)) < 0)
;
}
}
static void delay_1s_loop(void)
{
volatile long x;
for (x = 0; x < 1200000; x++)
x;
}
void initialize_clock(void)
{
//Set flash to 4 wait states, to allow for faster CPU clock. Also enable all flash caching and prefetch.
FLASH_ACR = 0x00000704;
//set APB2 prescaler to 16, relative to AHB (9MHz)
RCC_CFGR |= 0x0000E000;
//set APB1 prescaler to 16, relative to AHB (9MHz)
RCC_CFGR |= 0x00001C00;
//set AHB prescaler to 1, relative to system clock (144MHz)
RCC_CFGR = (RCC_CFGR & 0xFFFFFF0F);
//enable external crystal oscillator (12MHz)
RCC_CR |= 0x00010000;
//wait for the external crystal oscillator
while ((RCC_CR & 0x00020000) == 0)
;
//switch system clock to the external crystal oscillator (12MHz)
RCC_CFGR = (RCC_CFGR & 0xFFFFFFFC) | 0x00000001;
//wait for the system clock to change
while ((RCC_CFGR & 0x0000000C) != 0x00000004)
;
//set PLL USB post-divider to 6 (48MHz)
RCC_PLLCFGR = (RCC_PLLCFGR & 0xF0FFFFFF) | 0x06000000;
//set PLL input clock to HSE (12MHz crystal oscillator)
RCC_PLLCFGR |= 0x00400000;
//set PLL main system clock post divider to 2 (144MHz)
RCC_PLLCFGR = (RCC_PLLCFGR & 0xFFFCFFFF);
//set PLL multiplier to 144 (288MHz)
RCC_PLLCFGR = (RCC_PLLCFGR & 0xFFFF803F) | (((uint32_t)144) << 6);
//set PLL pre-divider to 6 (2MHz)
RCC_PLLCFGR = (RCC_PLLCFGR & 0xFFFFFFC0) | ((uint32_t)6);
//wait a bit
RCC_CR;
RCC_CR;
RCC_CR;
RCC_CR;
RCC_CR;
RCC_CR;
RCC_CR;
//enable the PLL (72MHz/144MHz/36MHz)
RCC_CR |= 0x01000000;
//wait for the PLL
while ((RCC_CR & 0x02000000) == 0)
;
//switch system clock to the PLL
RCC_CFGR = (RCC_CFGR & 0xFFFFFFFC) | 0x00000002;
//wait for the system clock to change
while ((RCC_CFGR & 0x0000000C) != 0x00000008)
;
/*
//shut off the HSI 16MHz internal oscillator
RCC_CR = (RCC_CR & 0xFFFFFFFE);
//wait for HSI to turn off
while ((RCC_CR & 0xFFFFFFFD) != 0)
;
*/
//enable TIM2
RCC_APB1ENR |= 0x00000001;
//divide APB1*2 by 18 to get 1MHz
TIM2_PSC = 17;
//start TIM2
TIM2_CR1 |= 0x00000001;
TIM2_EGR = 0x00000001;
clock_initialized = true;
}
uint32_t get_time_us(void)
{
return TIM2_CNT;
}