/***********************************************************************//**
 * @file		pwm.c
 * @brief		Contains all functions for PWM generating
 * @version		1.0
 * @date		26. February. 2014
 * @author		Michal Semrad
 **************************************************************************/

#include "pwm.h"

 /********************************************************************//*
   * @brief		HW initialization of PWM peripheral
   * @param[in]	none
   * @return 	None
   **********************************************************************/

void PWMInit(void)
{
	uint32_t temp;
	uint32_t PWMq[6];

	for (temp = 0; temp < 6; temp++)
	{
		PWMq[temp]=37500;//neutral middle
	}

	PWM_TIMERCFG_Type PWMCfgDat;
	PWM_MATCHCFG_Type PWMMatchCfgDat;
	PINSEL_CFG_Type PinCfg;

	/* PWM block section -------------------------------------------- */
	/* Initialize PWM peripheral, timer mode
	 * PWM prescale value = 1 (absolute value - tick value) */
	PWMCfgDat.PrescaleOption = PWM_TIMER_PRESCALE_TICKVAL;
	PWMCfgDat.PrescaleValue = 1;
	PWM_Init(LPC_PWM1, PWM_MODE_TIMER, (void *) &PWMCfgDat);

	/*
	 * Initialize PWM pin connect
	 */
	PinCfg.Funcnum = 1;
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
	PinCfg.Portnum = 2;
	for (temp = 0; temp <= 6; temp++){
		PinCfg.Pinnum = temp;
		PINSEL_ConfigPin(&PinCfg);
	}


	/* Set match value for PWM match channel 0 = 256, update immediately */
	PWM_MatchUpdate(LPC_PWM1, 0, 500000, PWM_MATCH_UPDATE_NOW);
	/* PWM Timer/Counter will be reset when channel 0 matching
	 * no interrupt when match
	 * no stop when match */
	PWMMatchCfgDat.IntOnMatch = DISABLE;
	PWMMatchCfgDat.MatchChannel = 0;
	PWMMatchCfgDat.ResetOnMatch = ENABLE;
	PWMMatchCfgDat.StopOnMatch = DISABLE;
	PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat);

	/* Configure each PWM channel: --------------------------------------------- */
	/* - Single edge
	 * - PWM Duty on each PWM channel determined by
	 * the match on channel 0 to the match of that match channel.
	 * Example: PWM Duty on PWM channel 1 determined by
	 * the match on channel 0 to the match of match channel 1.
	 */

	/* Configure PWM channel edge option
	 * Note: PWM Channel 1 is in single mode as default state and
	 * can not be changed to double edge mode */
	for (temp = 2; temp < 7; temp++)
	{
		PWM_ChannelConfig(LPC_PWM1, temp, PWM_CHANNEL_SINGLE_EDGE);
	}


	/* Configure match value for each match channel */
	for (temp = 1; temp < 7; temp++)
	{
		/* Set up match value */
		PWM_MatchUpdate(LPC_PWM1, temp, PWMq[temp-1], PWM_MATCH_UPDATE_NOW);
		/* Configure match option */
		PWMMatchCfgDat.IntOnMatch = DISABLE;
		PWMMatchCfgDat.MatchChannel = temp;
		PWMMatchCfgDat.ResetOnMatch = DISABLE;
		PWMMatchCfgDat.StopOnMatch = DISABLE;
		PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat);
		/* Enable PWM Channel Output */
		//PWM_ChannelCmd(LPC_PWM1, temp, ENABLE);
	}

	/* Reset and Start counter */
	PWM_ResetCounter(LPC_PWM1);

	PWMturnOff();

	PWM_CounterCmd(LPC_PWM1, ENABLE);

	/* Start PWM now */
	PWM_Cmd(LPC_PWM1, ENABLE);
}

/********************************************************************//*
  * @brief		Enable PWM generating
  * @param[in]	none
  * @return 	None
  **********************************************************************/
void PWMturnOn(){

	int temp;
	for (temp = 1; temp < 7; temp++){
		PWM_ChannelCmd(LPC_PWM1, temp, ENABLE);
	}
}


/********************************************************************//*
  * @brief		Disable PWM generating
  * @param[in]	none
  * @return 	None
  **********************************************************************/
void PWMturnOff(){

	int temp;
	for (temp = 1; temp < 7; temp++){
		PWM_ChannelCmd(LPC_PWM1, temp, DISABLE);
	}
}

/********************************************************************//*
  * @brief		Update PWM values
  * @param[in]	PWMq[6] array of PWM values
  * @return 	None
  **********************************************************************/
void PWMUpdate(int PWMq[6])
{
	int temp;
	for (temp = 1; temp < 7; temp++)
		{
			/* Set up match value */
			PWM_MatchUpdate(LPC_PWM1, temp, PWMq[temp-1], PWM_MATCH_UPDATE_NEXT_RST);
		}
}
