/*
 * dac0.c
 *
 * Created: 10/08/2018 8:31:30
 *  Author: Ledo
 */ 

#include "asf.h"

#define DACC_ANALOG_CONTROL (DACC_ACR_IBCTLCH0(0x02) \
| DACC_ACR_IBCTLCH1(0x02) \
| DACC_ACR_IBCTLDACCORE(0x01))

void dac0_init(void)
{
	sysclk_enable_peripheral_clock(ID_DACC);
	
	//DAC0 Pin
	ioport_set_pin_mode(PIO_PB13_IDX, IOPORT_MODE_MUX_D);
	ioport_disable_pin(PIO_PB13_IDX);

	/* Reset DACC registers */
	dacc_reset(DACC);

	/* Half word transfer mode */
	dacc_set_transfer_mode(DACC, 0);
	dacc_set_timing(DACC,0, 0x10);
	/* Disable TAG and select output channel DACC_CHANNEL */
	dacc_set_channel_selection(DACC, 0);

	/* Enable output channel DACC_CHANNEL */
	dacc_enable_channel(DACC, 0);

	/* Set up analog current */
	dacc_set_analog_control(DACC, DACC_ANALOG_CONTROL);


}

void dac0_write(uint32_t dac_val)
{
	dacc_write_conversion_data(DACC, dac_val);
}