]> begriffs open source - cmsis-driver-bare/blob - demo/gpio.c
Demo builds, flashes, and works
[cmsis-driver-bare] / demo / gpio.c
1 #include "Driver_GPIO.h"
2 #include "stm32f4xx.h"
3
4 #include "sram_vtor.h"
5 #include "debounce.h"
6
7 extern ARM_DRIVER_GPIO Driver_GPIOA, Driver_GPIOC;
8
9 volatile long gTicks;
10
11 void on_tick(void)
12 {
13         gTicks++;
14 }
15
16 void delay(uint32_t t)
17 {
18         uint32_t start = gTicks;
19         // modular subtraction is resillient against
20         // up to a single overflow in gTicks
21         while (gTicks - start < t)
22                 __WFI();
23 }
24
25 void init_clock(void)
26 {
27         SystemCoreClockUpdate();
28         SysTick_Config(SystemCoreClock / 1000);
29         NVIC_SetVector(SysTick_IRQn, (uintptr_t)on_tick);
30         NVIC_EnableIRQ(SysTick_IRQn);
31
32         RCC->AHB1ENR |=
33                 RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOCEN;
34         (void)RCC->AHB1ENR;
35         (void)RCC->AHB1ENR;
36 }
37
38 int main(void)
39 {
40         ARM_DRIVER_GPIO *ioa = &Driver_GPIOA, *ioc = &Driver_GPIOC;
41
42         NVIC_Relocate_VTOR();
43         init_clock();
44
45         ioa->Setup(5, NULL);
46         ioa->SetDirection(5, ARM_GPIO_OUTPUT);
47         ioc->Setup(13, NULL);
48
49         sample_state button_samples;
50         debounce_init(&button_samples, true, 5);
51         bool led = true;
52         ioa->SetOutput(5, led);
53
54         while (1)
55         {
56                 bool *stable;
57                 stable = debounce(
58                         &button_samples, ioc->GetInput(13));
59                 if (stable && !*stable)
60                 {
61                         led = !led;
62                         ioa->SetOutput(5, led);
63                 }
64                 delay(10);
65         }
66 }