/*
 * waves.c
 *
 * Created: 08/11/2016 13:26:47
 *  Author: Ledo
 */ 

#include "waves.h"



waves_init(void)
{
	 avr32_pwm_ier1_t ier1_pwm;
	 avr32_pwm_ier2_t ier2_pwm;
	 avr32_pwm_cmp0v_t cmp0v_pwm;
	 avr32_pwm_cmp0m_t cmp0m_pwm;
     channel_id = PWM_CHANNEL_ID;
     gpio_enable_module_pin(PWM_L_PIN, PWM_L_FUNCTION);
     // PWM controller configuration.
     pwm_opt.diva = AVR32_PWM_DIVA_CLK_OFF;
     pwm_opt.divb = AVR32_PWM_DIVB_CLK_OFF;
     pwm_opt.prea = AVR32_PWM_PREA_CCK;
     pwm_opt.preb = AVR32_PWM_PREB_CCK;

     pwm_opt.fault_detection_activated = false;
     pwm_opt.sync_channel_activated    = false;//true;
     pwm_opt.sync_update_channel_mode  = PWM_SYNC_UPDATE_MANUAL_WRITE_MANUAL_UPDATE;
     pwm_opt.sync_channel_select[0]    = false;//true;//true;
     pwm_opt.sync_channel_select[1]    = false;//true;
     pwm_opt.sync_channel_select[2]    = false;//true;
     pwm_opt.sync_channel_select[3]    = false;
     pwm_opt.cksel                     = PWM_CKSEL_MCK;
     pwm_init(&pwm_opt);

     // Channel configuration
     pwm_channel.CMR.dte   = 0;        // Enable Deadtime for complementary Mode
     pwm_channel.CMR.dthi  = 0;        // Deadtime Inverted on PWMH
     pwm_channel.CMR.dtli  = 0;        // Deadtime Not Inverted on PWML
     pwm_channel.CMR.ces   = 0;        // 0/1 Channel Event at the End of PWM Period
     pwm_channel.CMR.calg  = PWM_MODE_LEFT_ALIGNED;       // Channel mode.
     pwm_channel.CMR.cpol  = PWM_POLARITY_LOW;            // Channel polarity.
     pwm_channel.CMR.cpre  = AVR32_PWM_CPRE_CCK;           // Channel prescaler.
	 Mosfet_gate_pulse_update();
	 cmp0m_pwm.cen = 1;
	 cmp0v_pwm.cv = pwm_channel.cdty + ( (Params.sampleDelay +300) / 3);
	 AVR32_PWM.comp[0].CMP0V = cmp0v_pwm;
	 AVR32_PWM.comp[0].CMP0M = cmp0m_pwm;
	 ier2_pwm.cmpm0 = 1 ;
	 AVR32_PWM.IER2 = ier2_pwm;
	 
	 //ier1_pwm.chid0 = 1;
	 AVR32_PWM.ier1 = 0x00000003;//ier1_pwm; era 1
     pwm_channel.CMR.cpre = AVR32_PWM_CPRE_CCK_DIV_128;

     pwm_channel_init(0, &pwm_channel); // Set channel configuration to channel 0
     pwm_start_channels((1 << 0));  // Start channel 0.
	 

     sinusInit();
	 
	 INTC_register_interrupt(&pwm_int_handler, AVR32_PWM_IRQ, AVR32_INTC_INT0);
	 
}


void Mosfet_gate_pulse_Off(void)
{
	pwm_stop_channels((1 << 0));  // Stop channel 0.
	Mosfet_Off() ;
}

void Mosfet_gate_pulse_update(void)
{
	int32_t per,duty;
	Mosfet_gate_pulse_Off();
	switch(Params.pwmFreq)
	{
		case Hz_200:
		per = 1860;  
		break;
		case Hz_400:
		per = 932;
		break;
		case Hz_600:
		per = 620;
		break;
		case Hz_800:
		per = 462;
		break;
		default:
		break;
	}
	
	switch(Params.pwmDuty)
	{
		case Duty_10:
		duty = 10;
		break;
		case Duty_20:
		duty = 20;
		break;
		case Duty_30:
		duty = 30;
		break;
		case Duty_40:
		duty = 40;
		break;
		default:
		break;
		
	}
	pwm_channel.cprd = per;
	pwm_channel.cdty = per * duty / 100;
    pwm_interrupt_update_channel(0, &pwm_channel);	
	pwm_start_channels((1 << 0));  // Start channel 0.
}


void sinusInit(void)
{
	int32_t per;
	per = 150;
	pwm_stop_channels((1 << 1));  // Stop channel 1.
	// Channel configuration
	pwm_channel1.CMR.dte   = 0;        // Enable Deadtime for complementary Mode
	pwm_channel1.CMR.dthi  = 0;        // Deadtime Inverted on PWMH
	pwm_channel1.CMR.dtli  = 0;        // Deadtime Not Inverted on PWML
	pwm_channel1.CMR.ces   = 0;        // 0/1 Channel Event at the End of PWM Period
	pwm_channel1.CMR.calg  = PWM_MODE_LEFT_ALIGNED;       // Channel mode.
	pwm_channel1.CMR.cpol  = PWM_POLARITY_LOW;            // Channel polarity.
	pwm_channel1.CMR.cpre = AVR32_PWM_CPRE_CCK_DIV_16;
    pwm_channel1.cprd = per;
    pwm_channel1.cdty = per / 2;
	pwm_channel_init(1, &pwm_channel1); // Set channel configuration to channel 1
    pwm_start_channels((1 << 1));  // Start channel 1.
	
}




__attribute__((__interrupt__))
static void pwm_int_handler(void)
{
	
	   volatile unsigned long ulISR;

	   //uint32_t sr = sysreg_read(AVR32_SR);//AVR32_SR;
	 	/*********************************************************************************************
		* Este evento ocurre al final del tiempo de pulso + el delay
		* a partir de este momento se puede comenzar el muestreo de la seņal.
		*********************************************************************************************/  
	   // read status register
	   ulISR= AVR32_PWM.isr2;
	    if ( AVR32_PWM_ISR2_CMPM0_MASK & ulISR )  
	    {
			//gpio_toggle_pin(LED_1);
			flags.muestrearV = true;
			v_buffer_index = 0;
			LeerV();
	    }
		
       //final del periodo del canal 0
	   // read status register
	   ulISR = AVR32_PWM.isr1;
	   if ( AVR32_PWM_ISR1_CHID0_MASK & ulISR  ) 
	   {
		   flags.calcularI = true;
	   }
	   
	   
	   //Final del periodo del canal 1
	   if ( AVR32_PWM_ISR1_CHID1_MASK & ulISR  )
	   {
		   //gpio_toggle_pin(LED_1);
		   sinus(Params.volume,iArea / 3);
	   }
	   
	
	  //sysreg_write(AVR32_SR, sr) ;
}







