/**
  ******************************************************************************
  * @file           : main.c
  * @author         : SINOMCU-AE
  * @brief          : Main program body
  *    
  *          This file provides example code for RTC Alarm sub second,printf by uart,
  * LED1 blink by sysTick soft timer,LED blink period 1S.LED2 Toggle by alarm interrupt.
  * 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"

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

FlagStatus RTC_Alarm_IRQ_Flag = RESET;

/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void) {
  uint32_t LED_blink_Time = LED_BLINK_HALF_PER;
  
  //sysclk_init();                            //sysclk 48M,HSI/2 *12;if not call this function,default HSI 8M
  SystemCoreClockUpdate();
  
  sys_tick_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*****RTC Alarm Example*****");
  printf("\r\n--Inf:System Core Clock %d Hz.",SystemCoreClock);
  
  // RTC stop when Debug 
  //MS32_DBGMCU_APB1_GRP1_FreezePeriph(MS32_DBGMCU_APB1_GRP1_RTC_STOP);

  rtc_config();
  rtc_alarm_config(); 
  
  Start_User_MS_SF_Timer(LED_blink_Time);
  while(1) {
    if(RTC_Alarm_IRQ_Flag ==SET) {                         // RTC alarm interrupt occur
      RTC_Alarm_IRQ_Flag = RESET;
      get_rtc_calendar(); 
    }
    
    if(Get_State_User_MS_SF_Timer() == SF_TIMER_OV) {
      Start_User_MS_SF_Timer(LED_blink_Time);              // clear soft timer ov,default 500ms
      LED1_TOGGLE();                                       // led blink for running
    }
  }
}

/**
  * @brief  RTC alarm IRQ callback function.
  * @param  None
  * @retval None
  */
void RTC_Alarm_Callback(void) {
  RTC_Alarm_IRQ_Flag = SET;
  LED2_TOGGLE();
}

/**
  * @brief  SystemClock_Config HSI/2 to PLL*12 sysclk 48M
  * @param  None
  * @retval None
  */
void sysclk_init(void) {
  MS32_RCC_OscInitTypeDef osc_cfg;
  MS32_RCC_PllInitTypeDef pll_cfg;
  MS32_RCC_ClkInitTypeDef clk_cfg;

  // get and clear reset flag 
  MS32_RCC_GetResetFlag(ENABLE);
  
  // osc config 
  osc_cfg.HSIState = MS32_RCC_HSI_ON;
  osc_cfg.HSEState = MS32_RCC_HSE_OFF;
  osc_cfg.LSIState = MS32_RCC_LSI_OFF;
//  osc_cfg.LSEState = MS32_RCC_LSE_ON_DRV_H;
  MS32_RCC_OscConfig(&osc_cfg);
  
  // pll config 
  pll_cfg.PLLState  = MS32_RCC_PLL_ON;
  pll_cfg.PLLSource = MS32_RCC_PLL_SOURCE_HSI_DIV_2;       // HSI/2 4M
  pll_cfg.PLLMUL    = MS32_RCC_PLL_MUL12;
  pll_cfg.PREDIV    = MS32_RCC_PLL_PREDIV_DIV1;
  MS32_RCC_PllConfig(&pll_cfg);
  
  // sysclk config 
  clk_cfg.SYSCLKSource   = MS32_RCC_SYSCLK_SOURCE_PLL;
  clk_cfg.AHBCLKDivider  = MS32_RCC_SYSCLK_DIV1;
  clk_cfg.APB1CLKDivider = MS32_RCC_HCLK_DIV1;
  MS32_RCC_ClockConfig(&clk_cfg);
}
/******************************** END OF FILE *********************************/
