-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathclk.c
90 lines (66 loc) · 1.8 KB
/
clk.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
/*
* Copyright (C) 2017 Obermaier Johannes
*
* This Source Code Form is subject to the terms of the MIT License.
* If a copy of the MIT License was not distributed with this file,
* you can obtain one at https://opensource.org/licenses/MIT
*/
#include "clk.h"
/* Systick is used for wait* functions. We use it as a raw timer (without interrupts) for simplicity. */
#define F_CPU (48000000u)
#define SYSTICK_MAX (0xFFFFFFu)
#define SYSTICK_TICKS_PER_US (F_CPU / 1000000u)
#define SYSTICK_CSR_ADDR ((uint32_t *) 0xE000E010u)
#define SYSTICK_CVR_ADDR ((uint32_t *) 0xE000E018u)
#define SYSTICK_CSR_ON (0x00000005u)
/* Systick config end */
static uint32_t volatile *sysTickCSR = SYSTICK_CSR_ADDR;
static uint32_t volatile *sysTickCVR = SYSTICK_CVR_ADDR;
/* Choose 48MHz system clock using the PLL */
void clkEnablePLLInt( void )
{
/* Flash Latency for >=24MHz: One wait state */
FLASH->ACR |= FLASH_ACR_LATENCY;
/* Prediv = 2 */
RCC->CFGR2 = RCC_CFGR2_PREDIV1_DIV2;
RCC->CFGR |= RCC_CFGR_PLLMUL12 | RCC_CFGR_PLLSRC_HSI_PREDIV;
RCC->CR |= RCC_CR_PLLON;
while (!(RCC->CR & RCC_CR_PLLRDY))
{
; /* Wait for clock to become stable */
}
RCC->CFGR = (RCC->CFGR & ~(RCC_CFGR_SW)) | RCC_CFGR_SW_PLL;
while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL)
{
; /* Wait for clock to become selected */
}
return ;
}
void clkEnableSystick( void )
{
/* Initialize the Systick timer (no interrupts!) */
*sysTickCSR = SYSTICK_CSR_ON;
*sysTickCVR = SYSTICK_MAX;
return ;
}
void waitus( uint16_t const us )
{
uint32_t cmpTicks = 0u;
*sysTickCVR = SYSTICK_MAX;
cmpTicks = SYSTICK_MAX - ((uint32_t) us * SYSTICK_TICKS_PER_US);
while (*sysTickCVR >= cmpTicks)
{
; /* Wait */
}
return ;
}
void waitms( uint16_t const ms )
{
uint16_t timeMs = ms;
while (timeMs)
{
waitus(1000u);
--timeMs;
}
return ;
}