/*
===============================================================================
 Name        : PR7261.c
 Author      : $(author)
 Version     :
 Copyright   : $(copyright)
 Description : main definition
===============================================================================
*/
/*
#if defined (__USE_LPCOPEN)
#if defined(NO_BOARD_LIB)
#include "chip.h"
#else
#include "board.h"
#endif
#endif

#include <cr_section_macros.h>

// TODO: insert other include files here

// TODO: insert other definitions and declarations here

#if defined (M0_SLAVE_PAUSE_AT_MAIN)
volatile unsigned int pause_at_main;
#endif

int main(void) {

#if defined (M0_SLAVE_PAUSE_AT_MAIN)
    // Pause execution until debugger attaches and modifies variable
    while (pause_at_main == 0) {}
#endif

#if defined (__USE_LPCOPEN)
#if !defined(NO_BOARD_LIB)
    // Read clock settings and update SystemCoreClock variable
    SystemCoreClockUpdate();
    // Set up and initialize all required blocks and
    // functions related to the board hardware
    Board_Init();
    // Set the LED to the state of "On"
    Board_LED_Set(0, true);
#endif
#endif

    // TODO: insert code here

    // Force the counter to be placed into memory
    volatile static int i = 0 ;
    // Enter an infinite loop, just incrementing a counter
    while(1) {
        i++ ;
    }
    return 0 ;
}
*/
/*
===============================================================================
 Name        : PR7261.c
 Author      : $(author)
 Version     :
 Copyright   : $(copyright)
 Description : main definition
===============================================================================
*/

#include "chip.h"
#include "pe0003.h"
#include "pe0003_config.h"
#include "ftdi.h"
#include "cbus.h"
#include "gpio.h"
#include "timer.h"
#include "lpc_types.h"

#include "monitor.h"
#include "messages.h"
#include "mon_general.h"

#include "Settings.h"
#include "Registers.h"
#include "ProgTools.h"
#include "PR7261_Tools.h"

#include <stdio.h>

//To use the GUI printfs
#define printf		mprintf
#define scanf       mscanf


//Local functions
uint8_t ProgramRegisters();

uint16_t status = 0;



int main(void) {
	uint8_t res;
	uint8_t opt = 0;

	//Init the board and Gui functions
	res = BoardInit();

	//Load FI
	res = LoadFI();
	if (res == FALSE)
	{
		//FIXME DANIEL ERROR LOADING FI
	}


	opt = Menu_DefaultSelection(DEFAULT_OPT);


	//Set idle mode
	Pe0003_CbusWriteWord(CBUS1, MODE_REG, 0);

	//Flush FIFOs
	Pe0003_CbusWriteWord(CBUS1, FIFO_CONTROL, 8080);

	//Configure device for the correct xtal Freq
	res = ProgramRegisters();




	uint32_t nSamples = 20000;


	switch(opt)
	{
	case(1):break;
	case(2): G279A_Encoder_Op(nSamples); break;
	}


	mprintf("END OF PROGRAM\n");

	return 1 ;
}



/**
 * Wait for an interrupt flag to set.
 * @param timeout - timeout value in us.
 * @param val - address of status, updated by interrupt.
 * @param mask - flag to wait on.
 * @return Return false if a timeout occurs.
 */

uint8_t WaitStatusIrq(uint32_t timeout, uint16_t mask)
{
	uint8_t timed_out = 0;

	SetDelay(timeout);
	timed_out = DelayReached();
	while (!timed_out)
	{
		__WFI();
		if (((status) & mask) == mask)
		{
			status &= ~mask;
			return TRUE;
		}

		timed_out = DelayReached();
	}

	mprintf("Timeout!!\n ");
	return FALSE;
}


/**
 * IRQn1 interrupt handler
 */
void GPIO0_IRQHandler(void)
{
	status |= Pe0003_CbusReadWord(CBUSPORT, STATUS);
	//Clear interrupt
	//Needs to clear interrupts otherwise, the previous comment will cause an usb overflow
	LPC_GPIO_PIN_INT->IST |= (1<<0);
}





uint8_t ProgramRegisters()
{
	uint16_t status_mask = 0;

	Pe0003_CbusWriteWord(CBUSPORT, REG_DONE_SELECT, ST_PRG);
	status_mask = ST_REG_DONE | 0x8000;
	Pe0003_CbusWriteWord(CBUS_PORT, IRQ_ENABLE, status_mask);

	//Clear status value
	status = Pe0003_CbusReadWord(CBUSPORT, STATUS);
	status = 0;
	Pe0003_IrqnEnable();



//FIXME DANIEL CORRECT TIMES
	//Programming Block Clock	P1.X, 19.2MHz configuration selected
	Pe0003_CbusWriteWord(CBUS1, MODE_REG, 0x0210);
	Pe0003_CbusWriteWord(CBUSPORT, MODE_REG, P1_2_VAL);				//P1.2
	WaitStatusIrq(1000, 0x4000);

	Pe0003_CbusWriteWord(CBUSPORT, MODE_REG, P1_3_VAL);				//P1.3
	WaitStatusIrq(50000000, 0x4000);

	Pe0003_CbusWriteWord(CBUSPORT, MODE_REG, P1_4_VAL);				//P1.4
	WaitStatusIrq(50000, 0x4000);

	Pe0003_CbusWriteWord(CBUSPORT, MODE_REG, P1_5_VAL);				//P1.5
	WaitStatusIrq(50000, 0x4000);

}





