/**
  ******************************************************************************
  * @file     TIMER_CFG.c
  * @author   SINOMCU-AE
  * @brief    Timer basic config
  *    
  *          This file provides functions to Timer3 CH1 PWM out config:
  *              PA6      --> TIM3_CH1
  *             PWM period 1KHz,UIF CC1 interrupt enable;Duty adjustment in main.c. 
  *         
  ******************************************************************************
  * @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 "TIMER_CFG.h"

/**
  * @brief  Timer3 CH1 PWM Out Pin Initialization
  * @param  None
  * @retval None
  */
void timer3_pwm_pin_init(void) {
  MS32_GPIO_InitTypeDef GpioInitStruct = {0};
  // PA6      --> TIM3_CH1
  MS32_GPIO_StructInit(&GpioInitStruct); 
  GpioInitStruct.Pin = MS32_GPIO_PIN_6;
  GpioInitStruct.Mode = MS32_GPIO_MODE_ALTERNATE;
  GpioInitStruct.Speed = MS32_GPIO_SPEED_FREQ_HIGH;
  GpioInitStruct.OutputType = MS32_GPIO_OUTPUT_PUSHPULL;
  GpioInitStruct.Pull = MS32_GPIO_PULL_UP;
  GpioInitStruct.Alternate = MS32_GPIO_AF_1;
  MS32_GPIO_Init(GPIOA, &GpioInitStruct);                  // init function had enable clock
}

/**
  * @brief  Timer3 CH1 PWM Initialization
  * @param  None
  * @retval None
  */
void timer3_pwm_init(void) {
  MS32_TIM_InitTypeDef TimerInitStruct = {0};
  MS32_TIM_OC_InitTypeDef TimerOCInitStruct = {0};

  timer3_pwm_pin_init();                                                                           // PWM pin set

  MS32_APB1_GRP1_EnableClock(MS32_APB1_GRP1_PERIPH_TIM3);
  MS32_TIM_SetClockSource(TIM3, MS32_TIM_CLOCKSOURCE_INTERNAL);                                    // CNT clk form RCC

  // count up,PWM period 1KHz
  MS32_TIM_StructInit(&TimerInitStruct);
  TimerInitStruct.CounterMode = MS32_TIM_COUNTERMODE_UP;
  TimerInitStruct.Prescaler = __MS32_TIM_CALC_PSC(SystemCoreClock,1000000);                        // unit Hz,div sysclk to  CNT clk:1Mhz
  TimerInitStruct.Autoreload = __MS32_TIM_CALC_ARR(SystemCoreClock,TimerInitStruct.Prescaler,1000);// unit Hz,1KHz
  // TimerInitStruct.ClockDivision = MS32_TIM_CLOCKDIVISION_DIV1;                                  // fot tDTS
  MS32_TIM_Init(TIM3, &TimerInitStruct);
  MS32_TIM_EnableARRPreload(TIM3);

  MS32_TIM_OC_StructInit(&TimerOCInitStruct);
  TimerOCInitStruct.OCMode = MS32_TIM_OCMODE_PWM1;                                                 // PWM1 mode
  TimerOCInitStruct.OCState = MS32_TIM_OCSTATE_DISABLE;
  TimerOCInitStruct.CompareValue =  ((( MS32_TIM_GetAutoReload(TIM3) + 1 ) * TIMER_PWM_DUTY_PER_MIN ) / 100);     // set duty cycle
  TimerOCInitStruct.OCPolarity = MS32_TIM_OCPOLARITY_HIGH;
  MS32_TIM_OC_Init(TIM3, MS32_TIM_CHANNEL_CH1, &TimerOCInitStruct);

  MS32_TIM_OC_DisableFast(TIM3, MS32_TIM_CHANNEL_CH1);
  MS32_TIM_SetTriggerOutput(TIM3, MS32_TIM_TRGO_RESET);
  MS32_TIM_DisableMasterSlaveMode(TIM3);
  MS32_TIM_OC_EnablePreload(TIM3, MS32_TIM_CHANNEL_CH1);

  MS32_TIM_CC_EnableChannel(TIM3, MS32_TIM_CHANNEL_CH1);                                           // enable channel 
  MS32_TIM_EnableAllOutputs(TIM3);                                                                 // enable ouput 

  // UIF interrupt enable
  MS32_TIM_ITConfig(TIM3, MS32_TIM_DIER_UIE |MS32_TIM_DIER_CC1IE, 0);                              // Cortex-M0,Priority:0-3
  MS32_TIM_EnableCounter(TIM3);                                                                    // start timer
  // MS32_TIM_GenerateEvent_UPDATE(TIM3);                                                          // Force update
} 

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