1 | #include <stdio.h>
|
2 | #include <stdlib.h>
|
3 | #include "diag/Trace.h"
|
4 | #include "stm32f10x.h"
|
5 | #include "cortexm/ExceptionHandlers.h"
|
6 |
|
7 |
|
8 | #define GPIOB_LED GPIO_Pin_2
|
9 | #define GPIOC_BZZ GPIO_Pin_13
|
10 |
|
11 | #define TIMER_FREQUENCY_HZ (1000u)
|
12 |
|
13 |
|
14 | volatile uint32_t timer_delayCount;
|
15 |
|
16 | void Delay(uint32_t ticks);
|
17 | void TimingDelay_Decrement(void);
|
18 |
|
19 |
|
20 | void System_Clock_Setup(void);
|
21 |
|
22 | #pragma GCC diagnostic push
|
23 | #pragma GCC diagnostic ignored "-Wunused-parameter"
|
24 | #pragma GCC diagnostic ignored "-Wmissing-declarations"
|
25 | #pragma GCC diagnostic ignored "-Wreturn-type"
|
26 |
|
27 | void System_Clock_Setup(void) {
|
28 | RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_12); // 48MHz
|
29 | RCC_PLLCmd(ENABLE);
|
30 |
|
31 | while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) {
|
32 | }
|
33 | RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
|
34 |
|
35 | /* Set HCLK, PCLK1, and PCLK2 to SCLK (these are default */
|
36 | RCC_HCLKConfig(RCC_SYSCLK_Div1); // 48 MHz
|
37 | RCC_PCLK1Config(RCC_HCLK_Div2); // APB1 24MHz, max 36MHz
|
38 | RCC_PCLK2Config(RCC_HCLK_Div1); // APB2 48MHz
|
39 | RCC_ADCCLKConfig(RCC_PCLK2_Div4); // 48MHz/4 = 12MHz
|
40 |
|
41 | RCC_ClocksTypeDef clocks;
|
42 | RCC_GetClocksFreq(&clocks);
|
43 | SysTick_Config(clocks.SYSCLK_Frequency / 100000); //10us
|
44 | }
|
45 |
|
46 | int main(int argc, char* argv[]) {
|
47 |
|
48 | trace_puts("Hello ARM World!");
|
49 |
|
50 |
|
51 | //SysTick_Config(SystemCoreClock / TIMER_FREQUENCY_HZ);
|
52 | System_Clock_Setup();
|
53 |
|
54 | trace_printf("System clock: %u Hz\n", SystemCoreClock);
|
55 |
|
56 | // Enable GPIO Peripheral clock
|
57 | /* GPIOA GPIOB and GPIOC clock enable */
|
58 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
59 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
|
60 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
|
61 |
|
62 | GPIO_InitTypeDef GPIO_InitStructure;
|
63 |
|
64 | /* PB2 LED configuration */
|
65 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
|
66 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
67 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
|
68 | GPIO_Init(GPIOB, &GPIO_InitStructure);
|
69 |
|
70 | /* PC13 Buzzer configuration */
|
71 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
|
72 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
73 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
|
74 | GPIO_Init(GPIOC, &GPIO_InitStructure);
|
75 |
|
76 | uint32_t seconds = 0;
|
77 |
|
78 | GPIO_SetBits(GPIOB, GPIOB_LED);
|
79 | GPIO_SetBits(GPIOC, GPIOC_BZZ);
|
80 | // Infinite loop
|
81 | while (1) {
|
82 | Delay(100);
|
83 |
|
84 | GPIOC->ODR ^= GPIOC_BZZ; // Toggle Buzzer
|
85 | GPIOB->ODR ^= GPIOB_LED; // Toggle LED
|
86 |
|
87 | Delay(100);
|
88 |
|
89 | ++seconds;
|
90 |
|
91 | // Count seconds on the trace device.
|
92 | trace_printf("Second %u\n", seconds);
|
93 | }
|
94 | }
|
95 |
|
96 | void Delay(uint32_t ticks) {
|
97 | timer_delayCount = ticks;
|
98 | while (timer_delayCount != 0u)
|
99 | ;
|
100 | }
|
101 |
|
102 | void TimingDelay_Decrement(void) {
|
103 | if (timer_delayCount != 0u) {
|
104 | --timer_delayCount;
|
105 | }
|
106 | }
|
107 |
|
108 | void SysTick_Handler(void) {
|
109 | TimingDelay_Decrement();
|
110 | }
|
111 |
|
112 | #pragma GCC diagnostic pop
|