-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathmain.c
98 lines (86 loc) · 2.68 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
/*
* This example demonstrates using software reset
*/
#include "stm32f0xx_ll_rcc.h"
#include "stm32f0xx_ll_system.h"
#include "stm32f0xx_ll_bus.h"
#include "stm32f0xx_ll_gpio.h"
/**
* 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 */
LL_FLASH_SetLatency(LL_FLASH_LATENCY_1);
/* 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 two led pins
*/
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);
LL_GPIO_SetPinMode(GPIOC, LL_GPIO_PIN_9, 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}");
asm (".word 0x5b8d80"); //6000000
}
/*
* Turn on Green led when software reset was generated
* Turn on Blue led when Reset pin was pressed
*/
int main(void)
{
gpio_config();
if (LL_RCC_IsActiveFlag_SFTRST()) {
LL_GPIO_SetOutputPin(GPIOC, LL_GPIO_PIN_9);
LL_RCC_ClearResetFlags();
}
if (LL_RCC_IsActiveFlag_PINRST())
LL_GPIO_TogglePin(GPIOC, LL_GPIO_PIN_8);
rcc_config();
while (1) {
delay();
NVIC_SystemReset();
}
return 0;
}