/**
  ******************************************************************************
  * @file    system_ys32md211.c
  * @author  YSPRING Application Team
  * @brief   CMSIS Cortex-M0 Device Peripheral Access Layer System Source File.
  *
  * 1. This file provides two functions and one global variable to be called from
  *    user application:
  *      - SystemInit(): This function is called at startup just after reset and 
  *                      before branch to main program. This call is made inside
  *                      the "startup_ys32md211.s" file.
  *
  *      - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
  *                                  by the user application to setup the SysTick
  *                                  timer or configure other parameters.
  *
  *      - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
  *                                 be called whenever the core clock is changed
  *                                 during program execution.
  *
  *
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "ys32md211.h"
#include "system_ys32md211.h"

/**
 ******************************************************************************
 ** System Clock Frequency (Core Clock) Variable according CMSIS
 ******************************************************************************/
uint32_t SystemCoreClock = 16000000;

const uint8_t AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U};
const uint8_t APBPrescTable[8] =  {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U};
const uint32_t HSIFreqTable[4] =  {48000000U, 36000000U, 24000000U, 16000000U};

/**
 ******************************************************************************
 ** \brief  Setup the microcontroller system. Initialize the System and update
 ** the SystemCoreClock variable.
 **
 ** \param  none
 ** \return none
 ******************************************************************************/
void SystemInit(void)
{
  /* NOTE :SystemInit(): This function is called at startup just after reset and 
                         before branch to main program. This call is made inside
                         the "startup_ys32md211.s" file.
  */
  uint32_t reg = RCC->ICSCR;
  reg &= ~(RCC_ICSCR_HSI_FS | RCC_ICSCR_HSI_TRIM);
  reg |= ((RCC_ICSCR_HSI_FS_16Mhz) | ((*(uint32_t *)(0x1FFF7A0C)) & 0x7FF));
  RCC->ICSCR = reg;

  reg = RCC->CR;
  reg &= ~(RCC_CR_HSIDIV);
  reg |= RCC_CR_HSIDIV_1;
  RCC->CR = reg;
}

void SystemCoreClockUpdate(void)
{
  RCC_ClocksTypeDef RCC_CLK;
  
  RCC_GetClocksFreq(&RCC_CLK);
  SystemCoreClock = RCC_CLK.HCLK_Frequency;
}

#if defined(__CC_ARM)

#endif

