/**
  ******************************************************************************
  * @file           : main.c
  * @author         : SINOMCU-AE
  * @brief          : Main program body
  *    
  *          This file provides example code for Timer1 PWM out and printf by uart.
  * PWM period 1KHz,UIF interrupt enable;Duty adjustment 5%-99%,10ms 1% inc or dec.
  *     PA8    --> TIM1_CH1
  *     PB13   --> TIM1_CH1N 
  * LED1 Toggle uart send information of running count(redirect printf),defualt period 0.5s.
  * 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 COUNT_PERIOD    500       // unit ms

FlagStatus TIMER1_BRK_UP_TRG_COM = RESET;

/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void) 
{
  uint32_t count = 0;
  uint8_t Timer1_UIF_Count   = 0;
  uint8_t Timer1_PWM_Duty    = TIMER1_PWM_DUTY_PER_MIN;
  uint8_t Timer_PWM_Duty_Dir = DUTY_DIR_INC;
    
  //SystemClock_Config();           // sysclk 48M,HSI 8M /2*12;if not call this function,default HSI 8M
  SystemCoreClockUpdate();        // get System Core Clock
    
  SysTick_Init();
  GPIO_Init();
  Timer1_PWMwN_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*****Timer1 CH1 CH1N PWM out Example*****");
  printf("\r\n--Inf:System Core Clock %d Hz.",SystemCoreClock);
    
  Start_User_MS_SF_Timer(COUNT_PERIOD);
  while(1) 
  {
    if(TIMER1_BRK_UP_TRG_COM == SET)                                           // Timer UIF interrupt
    {
      TIMER1_BRK_UP_TRG_COM = RESET;  
      Timer1_UIF_Count++;

      if(Timer1_UIF_Count == TIMER1_DUTY_ADJ_10MS_NUM)                         // adjust duty cycle
      {
        Timer1_UIF_Count =0;
        if(Timer1_PWM_Duty >= TIMER1_PWM_DUTY_PER_MAX)                         // when duty increase to max,than to decrease
        {
          Timer_PWM_Duty_Dir = DUTY_DIR_DEC;
        }
        if(Timer1_PWM_Duty <= TIMER1_PWM_DUTY_PER_MIN)                         // when duty decrease to min,than to increase
        {
          Timer_PWM_Duty_Dir = DUTY_DIR_INC;
        }

        MS32_TIM_OC_SetCompareCH1(TIM1, (MS32_TIM_GetAutoReload(TIM1)+1) * Timer1_PWM_Duty / 100 );     // update duty cycle
        printf("\r\n--Inf:Timer1 CH1 PWM Duty %d",Timer1_PWM_Duty);

        if(Timer_PWM_Duty_Dir == DUTY_DIR_INC)                                 // increase duty
        {
          Timer1_PWM_Duty++;
        }
        else
        {
          Timer1_PWM_Duty--;
        }
      } //if(Timer1_UIF_Count == TIMER1_DUTY_ADJ_10MS_NUM)
    }    
    
    if(Get_State_User_MS_SF_Timer() == SF_TIMER_OV)
    {
      Start_User_MS_SF_Timer(COUNT_PERIOD);                                    // clear soft timer ov
      LED1_TOGGLE();
      count++;
      printf("\r\n--Inf: running count:%d",count);
    }
  }
}

/**
  * @brief  timer1 UIF interrupt callback function
  * @param  None
  * @retval None
  */
void Timer_BRK_UP_TRG_COM_Callback(void)
{
  TIMER1_BRK_UP_TRG_COM = SET;
}

/**
  * @brief  SystemClock_Config PLL 48M
  * @param  None
  * @retval None
  */
void SystemClock_Config(void)
{
  MS32_FLASH_SetLatency(MS32_FLASH_LATENCY_1);
  while(MS32_FLASH_GetLatency() != MS32_FLASH_LATENCY_1){ ; }
  MS32_RCC_HSI_Enable();

  // Wait HSI is ready
  while(MS32_RCC_HSI_IsReady() != 1){ ; }
 
  MS32_RCC_PLL_ConfigDomain_SYS(MS32_RCC_PLLSOURCE_HSI_DIV_2, MS32_RCC_PLL_MUL_12);
  MS32_RCC_PLL_Enable();

  // Wait PLL is ready 
  while(MS32_RCC_PLL_IsReady() != 1){ ; }

  MS32_RCC_SetAHBPrescaler(MS32_RCC_SYSCLK_DIV_1);
  MS32_RCC_SetAPB1Prescaler(MS32_RCC_APB1_DIV_1);
  MS32_RCC_SetSysClkSource(MS32_RCC_SYS_CLKSOURCE_PLL);

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

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