
#include "predef.h"
#include <startnet.h>
#include <stdio.h>
#include <sim5441X.h>
#include <ucos.h>
#include <utils.h>
#include <serial.h>
#include <iosys.h>
#include <sim.h>
#include <autoupdate.h>
#include <counters.h>
#include <ethernet.h>
#include <dhcpclient.h>
#include <pins.h>

extern "C"
{
   void UserMain( void *pd );
};

extern unsigned long CPU_CLOCK;

// Sim1 Miscellaneous Control Register
#define MISCCR2_DAC1SEL 0x0040   // Enable DAC1 drive output
#define MISCCR2_DAC0SEL 0x0020   // Enable DAC0 drive output
#define MISCCR2_ADCEN   0x0010   // Enable ADC 6-4 and 2-0
#define MISCCR2_ADC7EN  0x0008   // Enable ADC 7
#define MISCCR2_ADC3EN  0x0004   // Enable ADC 3

// ADC Calibration Register
#define ADC_CAL_DAC1  0x0002     // Selects the source of the ADCA7 input as DAC1 output.
#define ADC_CAL_DAC0  0x0001     // Selects the source of the ADCA3 input as DAC0 output.

// DAC Control Register
#define DAC_CR_HSLS	 0x0040
#define DAC_CR_RESET 0x1101
#define DAC_CR_UP    0x0020
#define DAC_CR_DOWN  0x0010
#define DAC_CR_AUTO  0x0008
#define DAC_CR_SYNC  0x0004
#define DAC_CR_FMT   0x0002
#define DAC_CR_PDN   0x0001       // Power down. 0 = power on, 1 = power down

const char *AppName = "Simple MOD5441X A/D Example";

void UserMain(void *pd)
{

	OSChangePrio(MAIN_PRIO);

	InitializeStack();
	EnableAutoUpdate();
	GetDHCPAddressIfNecessary();

	int fds0 = SimpleOpenSerial(0, 115200);

	ReplaceStdio(0, fds0);
	ReplaceStdio(1, fds0);
	ReplaceStdio(2, fds0);

	volatile WORD vw;
	unsigned short adResult;
	float voltResult;

	// ADC SINGLE-ENDED INIT
	sim2.adc.cr1 = 0;
	sim2.adc.cr2 = 0;
	sim2.adc.zccr = 0;
	sim2.adc.lst1 = 0x3210;
	sim2.adc.lst2 = 0x7654;
	sim2.adc.sdis = 0;
	sim2.adc.sr = 0xFFFF;

	for (int i = 0; i < 8; i++)
	{
		vw = sim2.adc.rslt[i];
		sim2.adc.ofs[i] = 0;
	}

	sim2.adc.lsr = 0xFFFF;
	sim2.adc.zcsr = 0xFFFF;

	sim2.adc.pwr = 0;
	sim2.adc.cal = 0x0000;
	sim2.adc.pwr2 = 0x0005;
	sim2.adc.div = 0x505;
	sim2.adc.asdiv = 0x13;

	// DAC INIT
	sim1.ccm.dactsr = 0x0;

	sim1.ccm.misccr2 &= ~(MISCCR2_ADC7EN);
	sim1.ccm.misccr2 &= ~(MISCCR2_ADC3EN);

	sim1.ccm.misccr2 |= MISCCR2_DAC1SEL;
	sim1.ccm.misccr2 |= MISCCR2_DAC0SEL;

	OSTimeDly(50);

  	printf("\r\nADC_IN5 With DACs Powered On\r\n");
  	sim2.dac[0].cr &= ~DAC_CR_PDN;
	sim2.dac[1].cr &= ~DAC_CR_PDN;

	OSTimeDly(50);

	// Start ADC
	sim2.adc.sr = 0xffff;
	sim2.adc.cr1 |= 0x2000;

	while (!(sim2.adc.sr & 0x0800))
		asm("nop");

	adResult = sim2.adc.rslt[5];

	adResult /= 8;
	voltResult = (adResult / 4096.0) * 3.34;

	printf("(%d) %f V\r\n", adResult, voltResult);

	printf("\r\nADC_IN5 With DACs Powered Off\r\n");
	sim2.dac[0].cr |= DAC_CR_PDN;
	sim2.dac[1].cr |= DAC_CR_PDN;

	OSTimeDly(50);

	// Start ADC
	sim2.adc.sr = 0xffff;
	sim2.adc.cr1 |= 0x2000;

	while (!(sim2.adc.sr & 0x0800))
		asm("nop");

	adResult = sim2.adc.rslt[5];

	adResult /= 8;
	voltResult = (adResult / 4096.0) * 3.34;

	printf("(%d) %f V\r\n", adResult, voltResult);
}

