/**
  ******************************************************************************
  * @file     TIMER_CFG.c
  * @author   SINOMCU-AE
  * @brief    Timer basic config
  *    
  *          This file provides functions to Timer basic config:
  *             Timer3 basic function of UIF interrupt set 100ms.
  *         
  ******************************************************************************
  * @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  Timer Basic Initialization Function
  * @param  None
  * @retval None
  */
void timer3_basic_init(void) {
  MS32_TIM_InitTypeDef TimerInitStruct = {0};
  MS32_APB1_GRP1_EnableClock(MS32_APB1_GRP1_PERIPH_TIM3);

  // count up,1KHz
  // Prescaler 16bit,CK_CNT=TIMclk/(PSD+1)
  // UIF:16bit,Autoreload,CK_CNT*(USER_TIMER_AUTORELOAD+1),(0 ~ USER_TIMER_AUTORELOAD)
  MS32_TIM_StructInit(&TimerInitStruct);
  TimerInitStruct.Prescaler = __MS32_TIM_CALC_PSC(SystemCoreClock,10000);                          // unit Hz,div sysclk to  CNT clk:10Khz           
  TimerInitStruct.CounterMode = MS32_TIM_COUNTERMODE_UP;
  TimerInitStruct.Autoreload = __MS32_TIM_CALC_ARR(SystemCoreClock,TimerInitStruct.Prescaler,10);  // unit Hz,10Hz
  TimerInitStruct.ClockDivision = MS32_TIM_CLOCKDIVISION_DIV1;            // for tDTS
  MS32_TIM_Init(TIM3, &TimerInitStruct);

  MS32_TIM_EnableARRPreload(TIM3);
  MS32_TIM_SetClockSource(TIM3, MS32_TIM_CLOCKSOURCE_INTERNAL);           // clk form RCC:PCLK <-- AHP_RRE=1,sysclk <-- HSI8M or PLL
  MS32_TIM_SetTriggerOutput(TIM3, MS32_TIM_TRGO_RESET);                   // for Master/Slave mode synchronization
  MS32_TIM_DisableMasterSlaveMode(TIM3);                                  // disable the Master/Slave mode.

  // UIF interrupt enable
  MS32_TIM_ITConfig(TIM3, MS32_TIM_DIER_UIE, 0);                          // Cortex-M0,Priority:0-3

  MS32_TIM_EnableCounter(TIM3);                                           // start timer
  //MS32_TIM_GenerateEvent_UPDATE(TIM3);                                    // Force update
}

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