/**
  ******************************************************************************
  * @file    TMR/PWM_Output/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_PWM_Output
  * @{
  */ 

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
TMR_BASE_CONFIG_T  TMR_baseConfigStruct;
TMR_OC_CONFIG_T  TMR_OCcongigStruct;
uint16_t CCR1_Val = 333;
uint16_t CCR2_Val = 249;
uint16_t CCR3_Val = 166;
uint16_t CCR4_Val = 83;
uint16_t PrescalerValue = 0;

/* Private function prototypes -----------------------------------------------*/
void RCM_Configuration(void);
void GPIO_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();

  /* GPIO Configuration */
  GPIO_Configuration();

  /* -----------------------------------------------------------------------
    TMR3 Configuration: generate 4 PWM signals with 4 different duty cycles:
    The TMR3CLK frequency is set to SystemCoreClock (Hz), to get TMR3 counter
    clock at 24 MHz the Prescaler is computed as following:
     - Prescaler = (TMR3CLK / TMR3 counter clock) - 1
    SystemCoreClock is set to 72 MHz for Low-density, Medium-density, High-density
    and Connectivity line devices and to 24 MHz for Low-Density Value line and
    Medium-Density Value line devices

    The TMR3 is running at 36 KHz: TMR3 Frequency = TMR3 counter clock/(ARR + 1)
                                                  = 24 MHz / 666 = 36 KHz
    TMR3 Channel1 duty cycle = (TMR3_CCR1/ TMR3_ARR)* 100 = 50%
    TMR3 Channel2 duty cycle = (TMR3_CCR2/ TMR3_ARR)* 100 = 37.5%
    TMR3 Channel3 duty cycle = (TMR3_CCR3/ TMR3_ARR)* 100 = 25%
    TMR3 Channel4 duty cycle = (TMR3_CCR4/ TMR3_ARR)* 100 = 12.5%
  ----------------------------------------------------------------------- */
  /* Compute the prescaler value */
  PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
  /* Time base configuration */
  TMR_baseConfigStruct.period = 665;
  TMR_baseConfigStruct.division = PrescalerValue;
  TMR_baseConfigStruct.clockDivision = TMR_CKDR1;
  TMR_baseConfigStruct.countMode = TMR_COUNT_MODE_UP;

  TMR_TimeBaseConfig(TMR3, &TMR_baseConfigStruct);

  /* PWM1 Mode configuration: Channel1 */
  TMR_OCcongigStruct.OC_Mode = TMR_OC_MODE_PWM1;
  TMR_OCcongigStruct.OC_OutputState = TMR_OUTPUT_STATE_ENABLE;
  TMR_OCcongigStruct.Pulse = CCR1_Val;
  TMR_OCcongigStruct.OC_Polarity = TMR_OC_POLARITY_HIGH;

  TMR_OC1Config(TMR3, &TMR_OCcongigStruct);

  TMR_OC1PreloadConfig(TMR3, TMR_OC_Preload_ENABLE);

  /* PWM1 Mode configuration: Channel2 */
  TMR_OCcongigStruct.OC_OutputState = TMR_OUTPUT_STATE_ENABLE;
  TMR_OCcongigStruct.Pulse = CCR2_Val;

  TMR_OC2Config(TMR3, &TMR_OCcongigStruct);

  TMR_OC2PreloadConfig(TMR3, TMR_OC_Preload_ENABLE);

  /* PWM1 Mode configuration: Channel3 */
  TMR_OCcongigStruct.OC_OutputState = TMR_OUTPUT_STATE_ENABLE;
  TMR_OCcongigStruct.Pulse = CCR3_Val;

  TMR_OC3Config(TMR3, &TMR_OCcongigStruct);

  TMR_OC3PreloadConfig(TMR3, TMR_OC_Preload_ENABLE);

  /* PWM1 Mode configuration: Channel4 */
  TMR_OCcongigStruct.OC_OutputState = TMR_OUTPUT_STATE_ENABLE;
  TMR_OCcongigStruct.Pulse = CCR4_Val;

  TMR_OC4Config(TMR3, &TMR_OCcongigStruct);

  TMR_OC4PreloadConfig(TMR3, TMR_OC_Preload_ENABLE);

  TMR_EnableAUTOReload(TMR3);

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

  while (1)
  {}
}

/**
  * @brief  Configures the different system clocks.
  * @param  None
  * @retval None
  */
void RCM_Configuration(void)
{
  /* TMR3 clock enable */
  RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR3);

  /* GPIOA and GPIOB clock enable */
  RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOA | RCM_APB2_PERIPH_GPIOB |
                         RCM_APB2_PERIPH_GPIOC | RCM_APB2_PERIPH_AFIO);
}

/**
  * @brief  Configure the TMR3 Ouput Channels.
  * @param  None
  * @retval None
  */
void GPIO_Configuration(void)
{
  GPIO_ConfigStruct_T GPIO_configStruct;

  /* GPIOA Configuration:TMR3 Channel1, 2, 3 and 4 as alternate function push-pull */
  GPIO_configStruct.pin = GPIO_PIN_6 | GPIO_PIN_7;
  GPIO_configStruct.mode = GPIO_MODE_AF_PP;
  GPIO_configStruct.speed = GPIO_SPEED_50MHz;

  GPIO_Config(GPIOA, &GPIO_configStruct);

  GPIO_configStruct.pin = GPIO_PIN_0 | GPIO_PIN_1;
  GPIO_Config(GPIOB, &GPIO_configStruct);
}

#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

/**
  * @}
  */ 

/**
  * @}
  */ 

