#include "stm32c0xx.h"  // Ensure this is the correct header file

void timer3_init(void) {
    // Enable clock for TIM3
    RCC->APBENR1 |= RCC_APBENR1_TIM3EN; // TIM3 is in APBENR1 for STM32C0

    // Configure TIM3
    TIM3->CR1 = 0;                // Disable TIM3
    TIM3->PSC = 15999;            // Prescaler (assuming 16 MHz clock)
    TIM3->ARR = 999;              // Auto-reload value for 1 ms delay
    TIM3->EGR = TIM_EGR_UG;       // Generate an update event
    TIM3->SR &= ~TIM_SR_UIF;      // Clear any previous update interrupt flag
    TIM3->CR1 |= TIM_CR1_CEN;     // Enable TIM3
}

void timer3_delay(uint32_t milliseconds) {
    for (uint32_t i = 0; i < milliseconds; i++) {
        while (!(TIM3->SR & TIM_SR_UIF)) {} // Wait for update interrupt flag
        TIM3->SR &= ~TIM_SR_UIF;           // Clear update interrupt flag
    }
    TIM3->CR1 &= ~TIM_CR1_CEN;            // Disable TIM3 after delay
}

int main(void) {
    // Enable clock for GPIOA
    RCC->IOPENR |= RCC_IOPENR_GPIOAEN;   // Enable GPIOA clock

    // Set PA5 as output
    GPIOA->MODER &= ~(3 << (5 * 2));  // Clear mode bits for PA5
    GPIOA->MODER |= (1 << (5 * 2));   // Set PA5 as output mode (01)

    while (1) {
        GPIOA->ODR ^= (1 << 5);   // Toggle PA5 (LED on/off)
        timer3_init();            // Initialize TIM3
        timer3_delay(500);        // 500 ms delay
        GPIOA->ODR ^= (1 << 5);  // Toggle PA5 (LED off)
        timer3_init();            // Reinitialize TIM3 for next delay
        timer3_delay(500);        // 500 ms delay
    }
}
nucleo:PD0
nucleo:PD2
nucleo:VDD
nucleo:PA14
nucleo:PA13
nucleo:PC6
nucleo:GND.1
nucleo:PC13
nucleo:PC14
nucleo:PF0
nucleo:PF1
nucleo:PB11
nucleo:PA2
nucleo:PD1
nucleo:PD2.2
nucleo:E5V
nucleo:GND.2
nucleo:IOREF.1
nucleo:NRST.1
nucleo:3V3.1
nucleo:5V.1
nucleo:GND.3
nucleo:GND.4
nucleo:VIN.1
nucleo:PA0
nucleo:PA1
nucleo:PA4
nucleo:PB1
nucleo:PA11
nucleo:PA12
nucleo:IOREF.2
nucleo:NRST.2
nucleo:3V3.2
nucleo:5V.2
nucleo:GND.5
nucleo:GND.6
nucleo:VIN.2
nucleo:A0
nucleo:A1
nucleo:A2
nucleo:A3
nucleo:A4
nucleo:A5
nucleo:5V.3
nucleo:PA3
nucleo:PC15
nucleo:PB12.1
nucleo:PB12.2
nucleo:PB2
nucleo:GND.7
nucleo:PF3
nucleo:PA8
nucleo:PB15
nucleo:PB14
nucleo:PB13
nucleo:AGND
nucleo:PB0.1
nucleo:PD3
nucleo:PB8
nucleo:PB9
nucleo:AVDD.1
nucleo:GND.8
nucleo:PA5
nucleo:PA6
nucleo:PA7
nucleo:PB0.2
nucleo:PC7
nucleo:PA9
nucleo:PA15
nucleo:PB5
nucleo:PB4
nucleo:PB10
nucleo:PB3
nucleo:PA10
nucleo:PB6
nucleo:PB7
nucleo:D15
nucleo:D14
nucleo:AVDD.2
nucleo:GND.9
nucleo:D13
nucleo:D12
nucleo:D11
nucleo:D10
nucleo:D9
nucleo:D8
nucleo:D7
nucleo:D6
nucleo:D5
nucleo:D4
nucleo:D3
nucleo:D2
nucleo:D1
nucleo:D0
pot1:GND
pot1:SIG
pot1:VCC