#include <stdint.h>
#include <stm32f4xx.h>

int main(void)
{
    // GPIO Port D Clock Enable
    RCC->AHB1ENR = RCC_AHB1ENR_GPIODEN;
    // GPIO Port D Pin 15 down to 12 Push/Pull Output
    GPIOD->MODER = GPIO_MODER_MODER12_0
                   | GPIO_MODER_MODER13_0
                   | GPIO_MODER_MODER14_0
                   | GPIO_MODER_MODER15_0;
    // Set green and red LEDs
    GPIOD->ODR = GPIO_ODR_OD12 | GPIO_ODR_OD14;

    // SysTick enable with clk source to AHB/8
    SysTick->CTRL = SysTick_CTRL_ENABLE_Msk;
    SysTick->LOAD = 1000000; // 16 MHz / 8 / 2 Hz

    // Do forever:
    while (1)
    {
        // Wait a moment
        while ((SysTick->CTRL & (1 << 16)) == 0);
        // Flip all LEDs
        GPIOD->ODR ^= GPIO_ODR_OD12
                      | GPIO_ODR_OD13
                      | GPIO_ODR_OD14
                      | GPIO_ODR_OD15;
    }
}
