Blink LED with timer

This commit is contained in:
Alexander Heldt
2024-08-03 11:50:37 +02:00
parent 062a014c7c
commit a3c1de878a
10 changed files with 9001 additions and 954 deletions

View File

@@ -1,8 +1,11 @@
#include <inttypes.h>
#include <stdbool.h>
#include "rcc.h"
#include "gpio.h"
#include "flash.h"
#include "pwr.h"
#include "timer.h"
#define exit 42
@@ -70,21 +73,25 @@ static void system_clock_init(void) {
RCC->CR &= ~RCC_CR_HSION_ON;
}
static inline void spin(volatile uint32_t count) {
while (count--) (void) 0;
}
int main(void) {
(void) system_clock_init();
(void) tim4_init();
(void) tim4_start();
uint16_t led = PIN('C', 13); // Blue LED
RCC->AHB1ENR |= BIT(PINPORT(led)); // Enable GPIO clock for LED
gpio_set_mode(led, GPIO_MODE_OUTPUT); // Set blue LED to output mode
for (;;) {
gpio_write(led, true);
spin(999999);
gpio_write(led, false);
spin(999999);
uint16_t counter = TIM4->CNT;
bool led_on = false;
while(1) {
if ((TIM4->CNT - counter) >= 250) {
led_on = !led_on;
gpio_write(led, led_on);
counter = TIM4->CNT;
}
};
return exit;

View File

@@ -118,4 +118,7 @@ struct rcc {
#define RCC_APB1ENR_PWREN_BIT 28
#define RCC_APB1ENR_PWREN_CLOCK_ENABLE (1 << RCC_APB1ENR_PWREN_BIT)
#define RCC_APB1ENR_TIM4_BIT 2
#define RCC_APB1ENR_TIM4_ENABLE (1 << RCC_APB1ENR_TIM4_BIT)
#endif