#include "sysclk.h"
#include <stm32f411.h>

void
sysclk_init (void)
{
  /* HSEBYP: HSE clock bypass  */
  *RCC_CR &= ~(1u << 18); /* 0: HSE oscillator not bypassed  */

  /* HSEON: HSE clock enable  */
  *RCC_CR |= (1u << 16); /* 1: HSE oscillator ON  */

  /* HSERDY: HSE clock ready flag  */
  while (!(*RCC_CR & (1u << 17))); /* 0: HSE oscillator not ready  */

  /* SW: System clock switch  */
  *RCC_CFGR &= ~(0b11u); /* Clear SW bits  */
  *RCC_CFGR |= (0b01u); /* 01: HSE oscillator selected as system clock  */

  /* SWS: System clock switch status  */
  while ((*RCC_CFGR & (0b11u << 2)) != (0b01u << 2)); /* 01: HSE oscillator used
                                                         as the system clock  */
  /* HSION: Internal high-speed clock enable  */
  *RCC_CR &= ~(1u); /* 0: HSI oscillator OFF  */
}
