You are a highly proficient firmware engineer with expertise in firmware development and coding.


#include <stdint.h>
#include <stdio.h>

// Base addresses for memory-mapped I/O (assumed)
#define GPIO_BASE_ADDR 0x40020000
#define TIMER_BASE_ADDR 0x40010000

// Pointers to memory-mapped I/O registers (GPIO and TIMER)
volatile uint32_t* GPIO_MODER = (uint32_t*)(GPIO_BASE_ADDR + 0x00);  // GPIO Mode register
volatile uint32_t* GPIO_ODR = (uint32_t*)(GPIO_BASE_ADDR + 0x14);    // GPIO Output Data Register

volatile uint32_t* TIMER_CTRL = (uint32_t*)(TIMER_BASE_ADDR + 0x00);  // Timer Control Register
volatile uint32_t* TIMER_CNT = (uint32_t*)(TIMER_BASE_ADDR + 0x24);   // Timer Counter Register

// Function Prototypes
void GPIO_init();
void Timer_init();
void set_GPIO_pin(uint8_t pin, uint8_t state);
void delay(uint32_t ms);
void TIM2_IRQHandler(void);


uint32_t TIM2_IRQHandler(void) {
    *TIMER_CTRL &= ~(1 << 0); 
    printf("Interrupt Handler Called!\n");
    return 1;  
}

// GPIO initialization (subtle error in setting the correct mode)
void GPIO_init() {
    // Configure GPIO pin 5 as output
    *GPIO_MODER |= (1 << (5 * 2));  
}

// Timer initialization
void Timer_init() {
    *TIMER_CTRL |= (1 << 0);  
}

// Set GPIO pin state (High/Low)
void set_GPIO_pin(uint8_t pin, uint8_t state) {
    if (state) {
        *GPIO_ODR |= (1 << pin);  // Set pin high
    } else {
        *GPIO_ODR &= ~(1 << pin);  // Set pin low
    }
}

// Blocking delay loop
void delay(uint32_t ms) {
    for (uint32_t i = 0; i < ms * 1000; i++) {
        asm volatile("nop");
    }
}

int main() {
    // Initialize GPIO and Timer
    GPIO_init();
    Timer_init();

    // Toggle GPIO pin 5 in a loop
    while (1) {
        set_GPIO_pin(5, 1); 
        delay(1000);         

        set_GPIO_pin(5, 0);  
        delay(1000);         

       
    }

    return 0;  
}



User: 
#include <stdint.h>
#include <stdio.h>

// Base addresses for memory-mapped I/O (assumed)
#define GPIO_BASE_ADDR 0x40020000
#define TIMER_BASE_ADDR 0x40010000

// Pointers to memory-mapped I/O registers (GPIO and TIMER)
volatile uint32_t* GPIO_MODER = (uint32_t*)(GPIO_BASE_ADDR + 0x00);  // GPIO Mode register
volatile uint32_t* GPIO_ODR = (uint32_t*)(GPIO_BASE_ADDR + 0x14);    // GPIO Output Data Register

volatile uint32_t* TIMER_CTRL = (uint32_t*)(TIMER_BASE_ADDR + 0x00);  // Timer Control Register
volatile uint32_t* TIMER_CNT = (uint32_t*)(TIMER_BASE_ADDR + 0x24);   // Timer Counter Register

// Function Prototypes
void GPIO_init();
void Timer_init();
void set_GPIO_pin(uint8_t pin, uint8_t state);
void delay(uint32_t ms);
void TIM2_IRQHandler(void);


uint32_t TIM2_IRQHandler(void) {
    *TIMER_CTRL &= ~(1 << 0); 
    printf("Interrupt Handler Called!\n");
    return 1;  
}

// GPIO initialization (subtle error in setting the correct mode)
void GPIO_init() {
    // Configure GPIO pin 5 as output
    *GPIO_MODER |= (1 << (5 * 2));  
}

// Timer initialization
void Timer_init() {
    *TIMER_CTRL |= (1 << 0);  
}

// Set GPIO pin state (High/Low)
void set_GPIO_pin(uint8_t pin, uint8_t state) {
    if (state) {
        *GPIO_ODR |= (1 << pin);  // Set pin high
    } else {
        *GPIO_ODR &= ~(1 << pin);  // Set pin low
    }
}

// Blocking delay loop
void delay(uint32_t ms) {
    for (uint32_t i = 0; i < ms * 1000; i++) {
        asm volatile("nop");
    }
}

int main() {
    // Initialize GPIO and Timer
    GPIO_init();
    Timer_init();

    // Toggle GPIO pin 5 in a loop
    while (1) {
        set_GPIO_pin(5, 1); 
        delay(1000);         

        set_GPIO_pin(5, 0);  
        delay(1000);         

       
    }

    return 0;  
}