/**
  ******************************************************************************
  * @file    TMR/TimeBase/main.c
  * @author  IOT Development Team
  * @version V1.0.0
  * @date    10-January-2020
  * @brief   Main program body
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "apm32f10x.h"

/** @addtogroup APM32F10x_StdPeriph_Examples
  * @{
  */

/** @addtogroup TMR_TimeBase
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
TMR_BASE_CONFIG_T  TMR_baseConfigStruct;
TMR_OC_CONFIG_T  TMR_OCcongigStruct;
__IO uint16_t CCR1_Val = 40961;
__IO uint16_t CCR2_Val = 27309;
__IO uint16_t CCR3_Val = 13654;
__IO uint16_t CCR4_Val = 6826;
uint16_t PrescalerValue = 0;

/* Private function prototypes -----------------------------------------------*/
void RCM_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
    /*!< At this stage the microcontroller clock setting is already configured,
         this is done through SystemInit() function which is called from startup
         file (startup_apm32f10x_xx.s) before to branch to application main.
         To reconfigure the default setting of SystemInit() function, refer to
         system_apm32f10x.c file
       */

    /* System Clocks Configuration */
    RCM_Configuration();

    /* NVIC Configuration */
    NVIC_Configuration();

    /* GPIO Configuration */
    GPIO_Configuration();

    /* ---------------------------------------------------------------
      TMR2 Configuration: Output Compare Timing Mode:
      TMR2 counter clock at 6 MHz
      CC1 update rate = TMR2 counter clock / CCR1_Val = 146.48 Hz
      CC2 update rate = TMR2 counter clock / CCR2_Val = 219.7 Hz
      CC3 update rate = TMR2 counter clock / CCR3_Val = 439.4 Hz
      CC4 update rate = TMR2 counter clock / CCR4_Val = 878.9 Hz
    --------------------------------------------------------------- */

    /* Compute the prescaler value */
    PrescalerValue = (uint16_t) (SystemCoreClock / 12000000) - 1;

    /* Time base configuration */
    TMR_baseConfigStruct.period = 65535;
    TMR_baseConfigStruct.division = 0;
    TMR_baseConfigStruct.clockDivision = 0;
    TMR_baseConfigStruct.countMode = TMR_COUNT_MODE_UP;

    TMR_TimeBaseConfig(TMR2, &TMR_baseConfigStruct);

    /* Prescaler configuration */
    TMR_PrescalerConfig(TMR2, PrescalerValue, TMR_PSCRELOAD_MODE_IMMEDIATE);

    /* Output Compare Timing Mode configuration: Channel1 */
    TMR_OCcongigStruct.OC_Mode = TMR_OC_MODE_TMRING;
    TMR_OCcongigStruct.OC_OutputState = TMR_OUTPUT_STATE_ENABLE;
    TMR_OCcongigStruct.Pulse = CCR1_Val;
    TMR_OCcongigStruct.OC_Polarity = TMR_OC_POLARITY_HIGH;

    TMR_OC1Config(TMR2, &TMR_OCcongigStruct);

    TMR_OC1PreloadConfig(TMR2, TMR_OC_Preload_DISABLE);

    /* Output Compare Timing Mode configuration: Channel2 */
    TMR_OCcongigStruct.OC_OutputState = TMR_OUTPUT_STATE_ENABLE;
    TMR_OCcongigStruct.Pulse = CCR2_Val;

    TMR_OC2Config(TMR2, &TMR_OCcongigStruct);

    TMR_OC1PreloadConfig(TMR2, TMR_OC_Preload_DISABLE);

    /* Output Compare Timing Mode configuration: Channel3 */
    TMR_OCcongigStruct.OC_OutputState = TMR_OUTPUT_STATE_ENABLE;
    TMR_OCcongigStruct.Pulse = CCR3_Val;

    TMR_OC3Config(TMR2, &TMR_OCcongigStruct);

    TMR_OC1PreloadConfig(TMR2, TMR_OC_Preload_DISABLE);

    /* Output Compare Timing Mode configuration: Channel4 */
    TMR_OCcongigStruct.OC_OutputState = TMR_OUTPUT_STATE_ENABLE;
    TMR_OCcongigStruct.Pulse = CCR4_Val;

    TMR_OC4Config(TMR2, &TMR_OCcongigStruct);

    TMR_OC1PreloadConfig(TMR2, TMR_OC_Preload_DISABLE);

    /* TMR IT enable */
    TMR_EnableINT(TMR2, TMR_TI_CH1 | TMR_TI_CH2 | TMR_TI_CH3 | TMR_TI_CH4);

    /* TMR2 enable counter */
    TMR_Enable(TMR2);

    while (1);
}

/**
  * @brief  Configures the different system clocks.
  * @param  None
  * @retval None
  */
void RCM_Configuration(void)
{
    /* PCLK1 = HCLK/4 */
    RCM_ConfigAPB1(RCM_HCLK_DIV_4);

    /* TMR2 clock enable */
    RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR2);

    /* GPIOC clock enable */
    RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOC);
}

/**
  * @brief  Configure the GPIO Pins.
  * @param  None
  * @retval None
  */
void GPIO_Configuration(void)
{
    GPIO_ConfigStruct_T GPIO_configStruct;

    /* GPIOC Configuration:Pin6, 7, 8 and 9 as alternate function push-pull */
    GPIO_configStruct.pin = GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9;
    GPIO_configStruct.mode = GPIO_MODE_OUT_PP;
    GPIO_configStruct.speed = GPIO_SPEED_50MHz;

    GPIO_Config(GPIOC, &GPIO_configStruct);
}

/**
  * @brief  Configure the nested vectored interrupt controller.
  * @param  None
  * @retval None
  */
void NVIC_Configuration(void)
{
    /* Enable the TMR2 global Interrupt */
    NVIC_EnableIRQRequest(TMR2_IRQn, 0x00, 0x01);
}

#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
    /* User can add his own implementation to report the file name and line number,
       ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

    while (1)
    {}
}

#endif

/**
  * @}
  */

/**
  * @}
  */

