/**
  ******************************************************************************
  * @file           : main.c
  * @author         ：SINOMCU-AE
  * @brief          : Main program body
  *    
  *          This file provides example code for EN HSE,LSE,MCO out and printf by uart:
  * En HSE input,PLL /1 *3, En LSE, MCO(PA8) out LSE/2;
  * LED1 blink by sysTick soft timer,defualt blink period 1S;
  * uart send information  of running count(redirect printf).
  * For details, see "readme.txt"   
  *         
  ******************************************************************************
   * @attention 
  *                           DISCLAIMER
  * The present firmware which is for guidance only aims at providing customers
  * with coding information regarding their products in order for them to save
  * time. As a result, SinoMCU shall not be held liable for any direct,indirect
  * or consequential damages with respect to any claims arising from the content 
  * of such firmware and/or the use made by customers of the coding information 
  * contained herein in connection with their products.
  *
  * <h2><center>&copy; Copyright (c) 2021 sinomcu All rights reserved.</center></h2>
  *
  ******************************************************************************
  */  
  
#include "system_define.h"

// LED blink half cycle in ms  
#define LED_BLINK_HALF_PER        500       // unit ms

/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void) 
{
  uint32_t count=0;

  SystemClock_Config();                     // sysclk 24M,HSE 8M /1*3;LSE en,MOC out LSE/2
  SystemCoreClockUpdate();                  // get System Core Clock
    
  SysTick_Init();
  GPIO_Init();
#if(defined(EN_PRINT_UART) && EN_PRINT_UART)  
  MS32_RCC_SetUSARTClockSource(MS32_RCC_USART1_CLKSOURCE_HSI);  // uart clk HSI 8M     
  USART1_UART_Init(115200);
#endif
  LED1_ON(); 
  
  printf("\r\n\r\n*****HSE LSE MCO Example*****");
  printf("\r\n--Inf:System Core Clock %d Hz.",SystemCoreClock);
  
  Start_User_MS_SF_Timer(LED_BLINK_HALF_PER);
  while(1) 
  {
    if(Get_State_User_MS_SF_Timer() == SF_TIMER_OV)
    {
      Start_User_MS_SF_Timer(LED_BLINK_HALF_PER);               // clear soft timer ov
      LED1_TOGGLE();
      count++;
      printf("\r\n--Inf: running count:%d",count);
    }
  }
}

/**
  * @brief System Clock Configuration
  * HSE 8M,PLL CLK 24M,EN LSE;MCO out LSE/2
  * @retval None
  */
void SystemClock_Config(void)
{
  MS32_FLASH_SetLatency(MS32_FLASH_LATENCY_1);
  while(MS32_FLASH_GetLatency() != MS32_FLASH_LATENCY_1){ ; }

  // EN HSE and wait it ready
  MS32_RCC_HSE_Enable();
  while(MS32_RCC_HSE_IsReady() != 1){ ; }
  
  // EN HSI and wait it ready for uart
  MS32_RCC_HSI_Enable();
  while(MS32_RCC_HSI_IsReady() != 1){ ; }

  // en power clk for write  PWR->CR,LSE on need 
  MS32_APB1_GRP1_EnableClock(MS32_APB1_GRP1_PERIPH_PWR);  
  MS32_PWR_EnableBkUpAccess();  

  MS32_RCC_LSE_SetDriveCapability(MS32_RCC_LSEDRIVE_HIGH);

  // EN LSE and wait it ready
  MS32_RCC_LSE_Enable();
  while(MS32_RCC_LSE_IsReady() != 1){ ; }

  // PLL HSE/1*3 ,EN and wait it ready 
  MS32_RCC_PLL_ConfigDomain_SYS(MS32_RCC_PLLSOURCE_HSE_DIV_1, MS32_RCC_PLL_MUL_3);
  MS32_RCC_PLL_Enable();
  while(MS32_RCC_PLL_IsReady() != 1){ ; }

  MS32_RCC_SetAHBPrescaler(MS32_RCC_SYSCLK_DIV_1);
  MS32_RCC_SetAPB1Prescaler(MS32_RCC_APB1_DIV_1);
  // sysclk select Pll out
  MS32_RCC_SetSysClkSource(MS32_RCC_SYS_CLKSOURCE_PLL);

  // Wait System clock ready
  while(MS32_RCC_GetSysClkSource() != MS32_RCC_SYS_CLKSOURCE_STATUS_PLL){ ; }

  MS32_Init1msTick(32000000);
  MS32_SetSystemCoreClock(32000000);
  MS32_RCC_SetUSARTClockSource(MS32_RCC_USART1_CLKSOURCE_HSI);
  //MS32_RCC_ConfigMCO(MS32_RCC_MCO1SOURCE_PLL, MS32_RCC_MCO1_DIV_16);
  MS32_RCC_ConfigMCO(MS32_RCC_MCO1SOURCE_LSE, MS32_RCC_MCO1_DIV_2);
}

/******************************** END OF FILE *********************************/
