Debian Codegraph @

examples/emcu-firmware/blinky-main.c-src

raw
1 /* Includes ------------------------------------------------------------------*/
2 #include "gw1ns4c.h"
3 /*----------------------------------------------------------------------------*/
4
5 /* Declarations*/
6 void initializeGPIO();
7 void initializeTimer();
8 void delayMillis(uint32_t ms);
9
10
11 int main(void)
12 {
13 SystemInit(); //Configures CPU for the defined system clock
14 initializeGPIO();
15 initializeTimer();
16
17 /* Infinite loop */
18 while(1)
19 {
20 GPIO_ResetBit(GPIO0, GPIO_Pin_0);
21 delayMillis(500);
22 GPIO_SetBit(GPIO0, GPIO_Pin_0);
23 delayMillis(500);
24 }
25 }
26
27
28 void initializeGPIO() {
29 GPIO_InitTypeDef gpioInitStruct;
30
31 // --------- output
32 //Select pin0, you can OR pins together to initialize them at the same time
33 gpioInitStruct.GPIO_Pin = GPIO_Pin_0;
34
35 //Set selected pins as output (see GPIOMode_TypeDef in gw1ns4c_gpio.h)
36 gpioInitStruct.GPIO_Mode = GPIO_Mode_OUT;
37
38 //Disable interrupts on selected pins (see GPIOInt_TypeDef)
39 gpioInitStruct.GPIO_Int = GPIO_Int_Disable;
40
41 //Initialize the GPIO using the configured init struct
42 GPIO_Init(GPIO0, &gpioInitStruct);
43 }
44
45 void initializeTimer() {
46 TIMER_InitTypeDef timerInitStruct;
47
48 timerInitStruct.Reload = 0;
49
50 //Disable interrupt requests from timer for now
51 timerInitStruct.TIMER_Int = DISABLE;
52
53 //Disable timer enabling/clocking from external pins (GPIO)
54 timerInitStruct.TIMER_Exti = TIMER_DISABLE;
55
56 TIMER_Init(TIMER0, &timerInitStruct);
57 TIMER_StopTimer(TIMER0);
58 }
59
60 #define CYCLES_PER_MILLISEC (SystemCoreClock / 1000)
61 void delayMillis(uint32_t ms) {
62 TIMER_StopTimer(TIMER0);
63 //Reset timer just in case it was modified elsewhere
64 TIMER_SetValue(TIMER0, 0);
65 TIMER_EnableIRQ(TIMER0);
66
67 uint32_t reloadVal = CYCLES_PER_MILLISEC * ms;
68 //Timer interrupt will trigger when it reaches the reload value
69 TIMER_SetReload(TIMER0, reloadVal);
70
71 TIMER_StartTimer(TIMER0);
72 //Block execution until timer wastes the calculated amount of cycles
73 while (TIMER_GetIRQStatus(TIMER0) != SET);
74
75 TIMER_StopTimer(TIMER0);
76 TIMER_ClearIRQ(TIMER0);
77 TIMER_SetValue(TIMER0, 0);
78 }
79