-
Notifications
You must be signed in to change notification settings - Fork 60
/
main.c
107 lines (94 loc) · 2.93 KB
/
main.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
/*
* This example demonstrates how to configure control clock
* and shows how much FLASH might deteriorate execution speed
*/
#include "stm32f0xx_ll_rcc.h"
#include "stm32f0xx_ll_system.h"
#include "stm32f0xx_ll_bus.h"
#include "stm32f0xx_ll_gpio.h"
#define FLASH_0LAT_DELAY0LAT
//#define FLASH_0LAT_DELAY1LAT
//#define FLASH_1LAT_DELAY0LAT
//#define FLASH_1LAT_DELAY1LAT
/**
* System Clock Configuration
* The system Clock is configured as follow :
* System Clock source = PLL (HSI/2)
* SYSCLK(Hz) = 48000000
* HCLK(Hz) = 48000000
* AHB Prescaler = 1
* APB1 Prescaler = 1
* HSI Frequency(Hz) = 8000000
* PLLMUL = 12
* Flash Latency(WS) = 1
*/
static void rcc_config()
{
/* Set FLASH latency */
#if defined(FLASH_0LAT_DELAY0LAT) || defined(FLASH_0LAT_DELAY1LAT)
LL_FLASH_SetLatency(LL_FLASH_LATENCY_0);
#else
LL_FLASH_SetLatency(LL_FLASH_LATENCY_1);
#endif
/* Enable HSI and wait for activation*/
LL_RCC_HSI_Enable();
while (LL_RCC_HSI_IsReady() != 1);
/* Main PLL configuration and activation */
LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSI_DIV_2,
LL_RCC_PLL_MUL_12);
LL_RCC_PLL_Enable();
while (LL_RCC_PLL_IsReady() != 1);
/* Sysclk activation on the main PLL */
LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL);
/* Set APB1 prescaler */
LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1);
/* Update CMSIS variable (which can be updated also
* through SystemCoreClockUpdate function) */
SystemCoreClock = 48000000;
}
/*
* Clock on GPIOC and set pin with Blue led connected
*/
static void gpio_config(void)
{
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOC);
LL_GPIO_SetPinMode(GPIOC, LL_GPIO_PIN_8, LL_GPIO_MODE_OUTPUT);
return;
}
/*
* Just set of commands to waste CPU power for a second
* (basically it is a simple cycle with a predefined number
* of loops)
*/
__attribute__((naked)) static void delay(void)
{
asm ("push {r7, lr}");
asm ("ldr r6, [pc, #8]");
asm ("sub r6, #1");
asm ("cmp r6, #0");
asm ("bne delay+0x4");
asm ("pop {r7, pc}");
#if defined(FLASH_0LAT_DELAY0LAT) || defined(FLASH_1LAT_DELAY0LAT)
asm (".word 0x927c00"); //9600000
#else
asm (".word 0x5b8d80"); //6000000
#endif
}
/*
* Here we call configure all peripherals we need and
* start blinking upon current mode
*/
int main(void)
{
rcc_config();
gpio_config();
while (1) {
LL_GPIO_SetOutputPin(GPIOC, LL_GPIO_PIN_8);
delay();
LL_GPIO_ResetOutputPin(GPIOC, LL_GPIO_PIN_8);
delay();
}
return 0;
}