// Press button to light the built-in LED
#include "ES28.h"           // All the port definitions are here

#define GPIOAEN     (1U<<0)     // Clock access to GPIOA (LED)
#define GPIOBEN     (1U<<1)     // Clock access to GPIOB (button)

#define LED_PIN     (1U<<5)     // LED on PA5    (D13)
#define BUTTON_PIN  (1U<<4)     // Button on PB4 (D5)

int main(void) {
    RCC->IOPENR |= (GPIOAEN | GPIOBEN); // Enable clock access to GPIOA,B

    GPIOA->MODER |= (1U<<10);   // Configure PA5 as output pin
    GPIOA->MODER &= ~(1U<<11);

    GPIOB->MODER &= ~(1U<<8);   // Configure PB4 as input pin
    GPIOB->MODER &= ~(1U<<9);   // (default is 0b11)

    GPIOB->PUPDR |= (1U<<8);    // enable pull-up for PB4
    GPIOB->PUPDR &= ~(1U<<9);

    delay_ms(50);               // give pull-ups time to pull up the pins

    while(1) {
        // Read the pin and control the LED
        if ((GPIOB->IDR & BUTTON_PIN) == 0) {   // button input is low-true
            GPIOA->ODR |= LED_PIN;              // turn LED on
        } else {
            GPIOA->ODR &= ~LED_PIN;             // turn LED off
        }
    }
    return 0;                   // never reached

}
