/* STM32F405: mirror PC7 (board pin 5) onto PC1 (board pin 13).
 * No simulator hooks: this ELF also runs on the physical board.
 */
#include <stdint.h>
#define REG32(address) (*(volatile uint32_t *)(address))

void reset_handler(void);
extern uint32_t _stack_top;
__attribute__((section(".vectors"), used))
const uintptr_t vectors[] = {(uintptr_t)&_stack_top, (uintptr_t)reset_handler};

void reset_handler(void) {
    REG32(0x40023830) |= 1u << 2; /* RCC_AHB1ENR: GPIOC */
    (void)REG32(0x40023830);
    REG32(0x40020800) = (REG32(0x40020800) & ~((3u << 2) | (3u << 14))) | (1u << 2);
    REG32(0x4002080c) = (REG32(0x4002080c) & ~(3u << 14)) | (2u << 14); /* PC7 pull-down */
    for (;;) {
        const uint32_t high = (REG32(0x40020810) >> 7) & 1u;
        REG32(0x40020818) = high ? (1u << 1) : (1u << 17); /* PC1 set/reset */
    }
}
