Untitled

 avatar
unknown
plain_text
a year ago
1.6 kB
13
Indexable
#include "stm32f7xx.h"   // or "stm32f4xx.h" for F4

// ---- LED selection: Nucleo-144 blue LED is PB7 ----
#define LED_GPIO        GPIOB
#define LED_PIN         7u

// Enable GPIO clock (F4/F7 use AHB1ENR for GPIO)
static inline void enable_gpio_clk(void) {
    RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN;   // enable GPIOB clock
    (void)RCC->AHB1ENR;                    // dummy read to ensure write completes
}

// Crude busy-wait (works before SysTick/PLL)
static void delay_cycles(volatile uint32_t cycles) {
    while (cycles--) { __NOP(); }
}

// 3 short blinks pattern (easy to recognize)
static void blink_pattern(GPIO_TypeDef *gpio, uint32_t pin) {
    for (int i = 0; i < 3; ++i) {
        gpio->BSRR = (1u << pin);          // set PB7 (LED on, active-high)
        delay_cycles(200000);
        gpio->BSRR = (1u << (pin + 16u));  // reset PB7 (LED off)
        delay_cycles(200000);
    }
    delay_cycles(1200000);                 // pause between groups
}

void Error_Handler(void)
{
    __disable_irq();

    // 1) Enable GPIO clock
    enable_gpio_clk();

    // 2) Configure PB7 as push-pull output, high speed, no pull
    const uint32_t pos2 = LED_PIN * 2u;
    LED_GPIO->MODER   = (LED_GPIO->MODER   & ~(0x3u << pos2)) | (0x1u << pos2); // output
    LED_GPIO->OTYPER &= ~(1u << LED_PIN);                                       // push-pull
    LED_GPIO->OSPEEDR = (LED_GPIO->OSPEEDR & ~(0x3u << pos2)) | (0x3u << pos2); // high speed
    LED_GPIO->PUPDR  &= ~(0x3u << pos2);                                        // no pull

    // 3) Blink forever
    for (;;) {
        blink_pattern(LED_GPIO, LED_PIN);
    }
}
Editor is loading...
Leave a Comment