]> begriffs open source - cmsis-driver-bare/blob - demo/gpio.c
Pass cmsis validator test
[cmsis-driver-bare] / demo / gpio.c
1 #include CMSIS_device_header
2 #include "Driver_GPIO.h"
3
4 #include "cmsis_os2.h"
5
6 #include "sram_vtor.h"
7 #include "debounce.h"
8
9 extern ARM_DRIVER_GPIO Driver_GPIOA, Driver_GPIOC;
10 ARM_DRIVER_GPIO *ioa = &Driver_GPIOA, *ioc = &Driver_GPIOC;
11
12 // CMSIS RTOS2 error handler
13 void osRtxErrorNotify(uint32_t code)
14 {
15         (void)code;
16         // Handle RTOS errors
17         while(1);
18 }
19
20 void control_led_task(void *args)
21 {
22         (void)args;
23
24         sample_state button_samples;
25         debounce_init(&button_samples, true, 5);
26         bool led = true;
27         ioa->SetOutput(5, led);
28
29         while (1)
30         {
31                 bool *stable;
32                 stable = debounce(
33                         &button_samples, ioc->GetInput(13));
34                 if (stable && !*stable)
35                 {
36                         led = !led;
37                         ioa->SetOutput(5, led);
38                 }
39                 vTaskDelay(pdMS_TO_TICKS(1000));
40         }
41 }
42
43 int main(void)
44 {
45         NVIC_Relocate_VTOR();
46
47         SystemCoreClockUpdate();
48
49         RCC->AHB1ENR |=
50                 RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOCEN;
51         (void)RCC->AHB1ENR;
52         (void)RCC->AHB1ENR;
53
54         ioa->Setup(5, NULL);
55         ioa->SetDirection(5, ARM_GPIO_OUTPUT);
56         ioc->Setup(13, NULL);
57
58         osKernelInitialize();
59         
60         const osThreadAttr_t thread_attr = {
61                 .name = "LED Control",
62                 .stack_size = 512  // 512 words = 2048 bytes
63         };
64         osThreadNew(control_led_task, NULL, &thread_attr);
65         
66         osKernelStart();
67         
68         // Should never reach here
69         while(1);
70 }