/**
  ******************************************************************************
  * @file     TIMER_CFG.c
  * @author   SINOMCU-AE
  * @brief    Timer basic config
  *    
  *          This file provides functions to Timer basic config:
  *             USER_TIMER parameter initialization timer1 or timer2;
  *             Timer basic function of UIF interrupt.
  *             defalt timer1 UIF set 250ms,timer2 UIF set 100ms,change by modify macro 
  * define in file"TIMER_CFG.h" :
  *    USER_TIMER_PRESCALER
  *    USER_TIMER_AUTORELOAD
  *    USER_TIMER_REPETCOUNT
  *         
  ******************************************************************************
  * @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 Timer_Basic_Init(void)
{
  MS32_TIM_InitTypeDef TIM_InitStruct = {0};
  MS32_APB1_GRP2_EnableClock(USER_APB1_GRP2_PERIPH_TIMER);

  // count up
  MS32_TIM_StructInit(&TIM_InitStruct);
  TIM_InitStruct.Prescaler = USER_TIMER_PRESCALER;
  TIM_InitStruct.CounterMode = MS32_TIM_COUNTERMODE_UP;
  TIM_InitStruct.Autoreload = USER_TIMER_AUTORELOAD;
  TIM_InitStruct.ClockDivision = MS32_TIM_CLOCKDIVISION_DIV1;
#if( defined(USER_TIMER1) && USER_TIMER1)
  TIM_InitStruct.RepetitionCounter = USER_TIMER_REPETCOUNT;
#endif
  
  MS32_TIM_Init(USER_TIMER, &TIM_InitStruct);
  MS32_TIM_EnableARRPreload(USER_TIMER);
  MS32_TIM_SetClockSource(USER_TIMER, MS32_TIM_CLOCKSOURCE_INTERNAL);     // clk form RCC
  MS32_TIM_SetTriggerOutput(USER_TIMER, MS32_TIM_TRGO_RESET);
  MS32_TIM_DisableMasterSlaveMode(USER_TIMER);

  // TIM interrupt enable
  NVIC_SetPriority(USER_TIMER_IRQn, 0);
  NVIC_EnableIRQ(USER_TIMER_IRQn);

  MS32_TIM_EnableIT_UPDATE(USER_TIMER);                                   // UIF interrupt enable
  MS32_TIM_EnableCounter(USER_TIMER);                                     // start timer
  //MS32_TIM_GenerateEvent_UPDATE(USER_TIMER);                              // Force update
}

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