//
// This file is part of the GNU ARM Eclipse distribution.
// Copyright (c) 2014 Liviu Ionescu.
//

// ----------------------------------------------------------------------------

#include <stdio.h>
#include "stm32f10x.h"
#include "stm32f10x_conf.h"
#include "stm32f10x_tim.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/

TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;
__IO uint16_t CCR1Val = 32768;
__IO uint16_t CCR2Val = 16384;
uint16_t PrescalerValue = 0;

/* Private function prototypes -----------------------------------------------*/

void GPIO_Configuration(void);
void NVIC_Configuration(void);

// ----- main() ---------------------------------------------------------------

// Sample pragmas to cope with warnings. Please note the related line at
// the end of this function, used to pop the compiler diagnostics status.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wmissing-declarations"
#pragma GCC diagnostic ignored "-Wreturn-type"



int main(void)
{

	/* Configure TIM9 pins */
	GPIO_Configuration();

	/* NVIC Configuration */
	NVIC_Configuration();

	/* ---------------------------------------------------------------------------
	  TIM9 Configuration: Output Compare Toggle Mode:
	  TIM9CLK = SystemCoreClock (72MHz),
	  The objective is to get TIM9 counter clock at 24 MHz:
	   - Prescaler = (TIM9CLK / TIM9 counter clock) - 1
	  CC1 update rate = TIM9 counter clock / CCR1Val = 732.4 Hz
	  CC2 update rate = TIM9 counter clock / CCR2Val = 1464.8 Hz
	----------------------------------------------------------------------------*/
	/* Compute the prescaler value */
	PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;

	/* Time base configuration */
	TIM_TimeBaseStructure.TIM_Period = 65535;
	TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

	TIM_TimeBaseInit(TIM9, &TIM_TimeBaseStructure);

	/* Output Compare Toggle Mode configuration: Channel1 */
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitStructure.TIM_Pulse = CCR1Val;
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
//	TIM_OC1Init(TIM9, &TIM_OCInitStructure);

//	TIM_OC1PreloadConfig(TIM9, TIM_OCPreload_Disable);

	/* Output Compare Toggle Mode configuration: Channel2 */
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitStructure.TIM_Pulse = CCR2Val;

//	TIM_OC2Init(TIM9, &TIM_OCInitStructure);

//	TIM_OC2PreloadConfig(TIM9, TIM_OCPreload_Disable);

	/* TIM enable counter */
//	TIM_Cmd(TIM9, ENABLE);

	/* TIM IT enable */
//	TIM_ITConfig(TIM9, TIM_IT_CC1 | TIM_IT_CC2, ENABLE);



  // Enable GPIO Peripheral clock
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

  GPIO_InitTypeDef GPIO_InitStructure;

  // Configure pin in output push/pull mode
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);


  // Infinite loop
  while (1)
    {
      GPIO_SetBits(GPIOB, GPIO_Pin_5);

      GPIO_ResetBits(GPIOB, GPIO_Pin_5);


      // Count seconds on the trace device.
      //trace_printf("Second %u\n", seconds);
    }
  // Infinite loop, never return.
}



/**
  * @brief  Configure TIM9 pins.
  * @param  None
  * @retval None
  */
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /* Enable TIM9 and GPIOA clock  */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9 | RCC_APB2Periph_GPIOA, ENABLE);

  /* GPIOA Configuration:TIM9 Channel1 and 2 as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOA, &GPIO_InitStructure);
}

/**
  * @brief  Configure the nested vectored interrupt controller.
  * @param  None
  * @retval None
  */
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Enable the TIM9 global Interrupt */
  //NVIC_InitStructure.NVIC_IRQChannel =  TIM1_BRK_TIM9_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}



#pragma GCC diagnostic pop

// ----------------------------------------------------------------------------
