/* blinky.c: the Blinky program from the first day of class.
 *
 * ENGS 28, Thayer School of Engineering, Dartmouth College.
 *
 * Put this file in the Src folder of your Blinky project, which is a copy of
 * your TemplateProject (the book's appendix on setting up STM32CubeIDE walks
 * through both).  It is the same program the book goes through line by line
 * in the Blinky chapter.
 *
 * It is also the known-good copy for the debugging chapter: if your Blinky
 * project will not build, replace the contents of the blinky.c in its Src
 * folder with this file and build again.
 */

#include "stm32c0xx.h"          // All register definitions are here
#define GPIOAEN   (1U << 0)     // RCC->IOPENR bit 0: clock for GPIOA
#define LED_PIN   (1U << 5)     // GPIOA bit 5: the on-board LED

int main(void) {
    RCC->IOPENR  |= GPIOAEN;     // Step 1: enable GPIOA clock
    GPIOA->MODER |= (1U << 10);  // Step 2a: set   bit 10
    GPIOA->MODER &= ~(1U << 11); // Step 2b: clear bit 11 -> PA5 output

    while (1) {
        GPIOA->ODR |= LED_PIN;   // Step 3: bit 5 = 1 -> LED on
        for (int i = 0; i < 100000; i++);
        GPIOA->ODR &= ~LED_PIN;  // Step 4: bit 5 = 0 -> LED off
        for (int i = 0; i < 100000; i++);
    }
    return 0;                    // never reached
}
