#include <led.h>
#include <stm32f411.h>

void
led_init (void)
{
  /* GPIOCEN: IO port C clock enable  */
  *RCC_AHB1ENR |= (1u << 2); /* 1: IO port C clock enabled  */

  /* Default to LED being turned off  */
  *GPIOC_ODR |= (1u << 13);

  /* MODERy[1:0]: Port x configuration bits (y = 0..15)  */
  *GPIOC_MODER &= ~(0b11u << 26); /* Clear PC13 bits  */
  *GPIOC_MODER |= (1u << 26); /* 01: General purpose output mode  */

  /* PC13: 0 = LED on
   *       1 = LED off  */
}

void
led_on (void)
{
  *GPIOC_ODR &= ~(1u << 13);
}

void
led_off (void)
{
  *GPIOC_ODR |= (1u << 13);
}

void
led_toggle (void)
{
  *GPIOC_ODR ^= (1u << 13);
}
