/*
 * tmrs.h
 *
 * Created: 24/09/2016 18:41:39
 *  Author: Ledo
 */ 


#ifndef TMRS_H_
#define TMRS_H_

#include <avr32/io.h>
#include "tc.h"
#include <stdbool.h>
#include "gpio.h"
#include "sysclk.h"
#include "globales.h"


//! \note TC0 module is used in this example.
#define TMR                 (&AVR32_TC0)
//! \note TC Channel 0 is used.
#define TMR_CHANNEL0         0
//! \note TC Channel 0 is used.
#define TMR_CHANNEL1         1

//! \note IRQ0 line of TC0 module channel 0 is used.
#define TMR_IRQ             AVR32_TC0_IRQ0
//! \note IRQ Group of TC0 module
#define TMR_IRQ_GROUP       AVR32_TC0_IRQ_GROUP
//! \note Interrupt priority 0 is used for TC in this example.
#define TMR_IRQ_PRIORITY    AVR32_INTC_INT0


volatile bool adc_sample;
volatile bool emitter_toogle;
volatile bool sample_start;
volatile bool sample_end;
volatile uint8_t led_counter;
// Options for waveform generation.
	static const tc_waveform_opt_t waveform_opt0 = {
		// Channel selection.
		.channel  = TMR_CHANNEL0,
		// Software trigger effect on TIOB.
		.bswtrg   = TC_EVT_EFFECT_NOOP,
		// External event effect on TIOB.
		.beevt    = TC_EVT_EFFECT_NOOP,
		// RC compare effect on TIOB.
		.bcpc     = TC_EVT_EFFECT_NOOP,
		// RB compare effect on TIOB.
		.bcpb     = TC_EVT_EFFECT_NOOP,
		// Software trigger effect on TIOA.
		.aswtrg   = TC_EVT_EFFECT_NOOP,
		// External event effect on TIOA.
		.aeevt    = TC_EVT_EFFECT_NOOP,
		// RC compare effect on TIOA.
		.acpc     = TC_EVT_EFFECT_NOOP,
		/*
		 * RA compare effect on TIOA.
		 * (other possibilities are none, set and clear).
		 */
		.acpa     = TC_EVT_EFFECT_NOOP,
		/*
		 * Waveform selection: Up mode with automatic trigger(reset)
		 * on RC compare.
		 */
		.wavsel   = TC_WAVEFORM_SEL_UP_MODE_RC_TRIGGER,
		// External event trigger enable.
		.enetrg   = false,
		// External event selection.
		.eevt     = 0,
		// External event edge selection.
		.eevtedg  = TC_SEL_NO_EDGE,
		// Counter disable when RC compare.
		.cpcdis   = false,
		// Counter clock stopped with RC compare.
		.cpcstop  = false,
		// Burst signal selection.
		.burst    = false,
		// Clock inversion.
		.clki     = false,
		// Internal source clock 3, connected to fPBA / 8.
		.tcclks   = TC_CLOCK_SOURCE_TC5
	};


// Options for waveform generation.
	static const tc_waveform_opt_t waveform_opt1 = {
		// Channel selection.
		.channel  = TMR_CHANNEL1,
		// Software trigger effect on TIOB.
		.bswtrg   = TC_EVT_EFFECT_NOOP,
		// External event effect on TIOB.
		.beevt    = TC_EVT_EFFECT_NOOP,
		// RC compare effect on TIOB.
		.bcpc     = TC_EVT_EFFECT_NOOP,
		// RB compare effect on TIOB.
		.bcpb     = TC_EVT_EFFECT_NOOP,
		// Software trigger effect on TIOA.
		.aswtrg   = TC_EVT_EFFECT_NOOP,
		// External event effect on TIOA.
		.aeevt    = TC_EVT_EFFECT_NOOP,
		// RC compare effect on TIOA.
		.acpc     = TC_EVT_EFFECT_NOOP,
		/*
		 * RA compare effect on TIOA.
		 * (other possibilities are none, set and clear).
		 */
		.acpa     = TC_EVT_EFFECT_NOOP,
		/*
		 * Waveform selection: Up mode with automatic trigger(reset)
		 * on RC compare.
		 */
		.wavsel   = TC_WAVEFORM_SEL_UP_MODE_RC_TRIGGER,
		// External event trigger enable.
		.enetrg   = false,
		// External event selection.
		.eevt     = 0,
		// External event edge selection.
		.eevtedg  = TC_SEL_NO_EDGE,
		// Counter disable when RC compare.
		.cpcdis   = false,
		// Counter clock stopped with RC compare.
		.cpcstop  = false,
		// Burst signal selection.
		.burst    = false,
		// Clock inversion.
		.clki     = false,
		// Internal source clock 3, connected to fPBA / 8.
		.tcclks   = TC_CLOCK_SOURCE_TC2
	};


// Options for enabling TC interrupts
static const tc_interrupt_t tc_interrupt0 = {
	.etrgs = 0,
	.ldrbs = 0,
	.ldras = 0,
	.cpcs  = 1, // Enable interrupt on RC compare alone
	.cpbs  = 0,
	.cpas  = 0,
	.lovrs = 0,
	.covfs = 0 // Enable interrupt on overflow
};

// Options for enabling TC interrupts
static const tc_interrupt_t tc_interrupt1 = {
	.etrgs = 0,
	.ldrbs = 0,
	.ldras = 0,
	.cpcs  = 1, // Enable interrupt on RC compare alone
	.cpbs  = 0,
	.cpas  = 0,
	.lovrs = 0,
	.covfs = 0 // Enable interrupt on overflow
};




/**
 * \brief TC Initialization
 *
 * Initializes and start the TC module with the following:
 * - Counter in Up mode with automatic reset on RC compare match.
 * - fPBA/8 is used as clock source for TC
 * - Enables RC compare match interrupt
 * \param tc Base address of the TC module
 */
 void tc0_init(volatile avr32_tc_t *tc, uint16_t per);
 void tc1_init(volatile avr32_tc_t *tc, uint16_t per);
 
__attribute__((__interrupt__))
static void tc0_irq(void);

__attribute__((__interrupt__))
static void tc1_irq(void);




#endif /* TMRS_H_ */