/**
  ******************************************************************************
  * @file     TIMER_CFG.c
  * @author   SINOMCU-AE
  * @brief    Timer basic config
  *    
  *          This file provides functions to Timer17 CH1N PWM out config:
  *              PB7      --> TIM17_CH1N
  *             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  Timer17 CH1N PWM Out Pin Initialization
  * @param  None
  * @retval None
  */
void timer17_pwmn_pin_init(void) {
  MS32_GPIO_InitTypeDef GpioInitStruct = {0};
  // PB7      --> TIM17_CH1N
  MS32_GPIO_StructInit(&GpioInitStruct); 
  GpioInitStruct.Pin = MS32_GPIO_PIN_7;
  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_2;
  MS32_GPIO_Init(GPIOB, &GpioInitStruct);                  // init function had enable clock
}

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

  timer17_pwmn_pin_init();                                                     // PWM pin set

  MS32_APB1_GRP2_EnableClock(MS32_APB1_GRP2_PERIPH_TIM17);

  // 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.RepetitionCounter = 0;
  TimerInitStruct.ClockDivision     = MS32_TIM_CLOCKDIVISION_DIV1;             // fot tDTS
  MS32_TIM_Init(TIM17, &TimerInitStruct);
  MS32_TIM_EnableARRPreload(TIM17);

  MS32_TIM_OC_StructInit(&TimerOCInitStruct);
  TimerOCInitStruct.OCMode   = MS32_TIM_OCMODE_PWM1;                           // PWM1 mode
  TimerOCInitStruct.OCState  = MS32_TIM_OCSTATE_DISABLE;
  TimerOCInitStruct.OCNState = MS32_TIM_OCSTATE_DISABLE;  
  TimerOCInitStruct.CompareValue =  ((( MS32_TIM_GetAutoReload(TIM17) + 1 ) * TIMER_PWM_DUTY_PER_MIN ) / 100);    // set duty cycle
  TimerOCInitStruct.OCPolarity   = MS32_TIM_OCPOLARITY_HIGH;
  TimerOCInitStruct.OCNPolarity  = MS32_TIM_OCPOLARITY_LOW;
  TimerOCInitStruct.OCIdleState  = MS32_TIM_OCIDLESTATE_HIGH;
  TimerOCInitStruct.OCNIdleState = MS32_TIM_OCIDLESTATE_LOW;
  MS32_TIM_OC_Init(TIM17, MS32_TIM_CHANNEL_CH1, &TimerOCInitStruct);
  MS32_TIM_OC_DisableFast(TIM17, MS32_TIM_CHANNEL_CH1);
  MS32_TIM_OC_EnablePreload(TIM17, MS32_TIM_CHANNEL_CH1);

  TimerBDTRInitStruct.OSSRState = MS32_TIM_OSSR_DISABLE;
  TimerBDTRInitStruct.OSSIState = MS32_TIM_OSSI_DISABLE;
  TimerBDTRInitStruct.LockLevel = MS32_TIM_LOCKLEVEL_OFF;
  TimerBDTRInitStruct.DeadTime = 0;
  TimerBDTRInitStruct.BreakState = MS32_TIM_BREAK_DISABLE;
  TimerBDTRInitStruct.BreakPolarity = MS32_TIM_BREAK_POLARITY_HIGH;
  TimerBDTRInitStruct.AutomaticOutput = MS32_TIM_AUTOMATICOUTPUT_DISABLE;
  MS32_TIM_BDTR_Init(TIM17, &TimerBDTRInitStruct);

  MS32_TIM_CC_EnableChannel(TIM17, MS32_TIM_CHANNEL_CH1N);                     // enable channel 
  MS32_TIM_EnableAllOutputs(TIM17);                                            // enable ouput 

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

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