/*
 * maincpp.cpp
 *
 *  Created on: 22 Jun 2022
 *      Author: garth
 */

#include "main.h"
#include "gpio.h"
#include "tim.h"
#include "extra.h"

//#include "STM_ENC28_J60.h"
volatile uint16_t pwm1freq = 0;
volatile uint16_t pwm1freqnew = 100;
volatile uint16_t pwm2freq = 0;
volatile uint16_t pwm2freqnew = 10000;
volatile uint16_t val;
volatile uint32_t pwm1counter;
volatile uint32_t pwm1period;
void maincpp()
{

	HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);

//	HAL_TIM_Base_Start_IT(&htim17);

	while(1)
	{

		if(pwm1freq != pwm1freqnew)
		{
			pwm1freq = pwm1freqnew;
//			val =  (map(pwm1freq, 1, 10000, 10000, 1))-1;
			htim1.Instance->CCR1 = (pwm1freq/2);
			htim1.Instance->ARR = pwm1freq;

		}
		if(pwm2freq != pwm2freqnew)
		{
			val = (360000/pwm2freqnew)-1;
			pwm1freqnew = val;
			pwm2freq = pwm2freqnew;

//			val =  (map(pwm1freq, 1, 10000, 10000, 1))-1;
//			htim17.Instance->PSC = pwm2freq;
//			htim17.Instance->ARR = pwm2freq;

		}
	}
}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  /* Prevent unused argument(s) compilation warning */
  if(htim->Instance == TIM17)
  {
	  pwm1counter++;
	  if(pwm1counter >= pwm1period)
	  {
		  HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
		  pwm1counter = 0;
	  }

  }


  /* NOTE : This function should not be modified, when the callback is needed,
            the HAL_TIM_PeriodElapsedCallback could be implemented in the user file
   */
}
