/*
 * main.c
 */
#include <std.h>
#include <tsk.h>
#include <log.h>
#include <stdio.h>
#include <bcache.h>

#include "quadracfg.h"

#include "SigProc/SigProcFFT.h"
#include "core/DspUpp.h"

#define BUFF_SIZE 4096

/* Variables */
unsigned short flag;

#pragma DATA_ALIGN( 64 );
volatile int16_t buff1[BUFF_SIZE];

using namespace std;
using namespace MityDSP;

/* Definizione struttura di configurazione UPP */
tcDspUpp::tsDspUppConfig gsUppConfig =
	{
	4, // Interrupt level. Must be between 4 and 15.
	9, // Chan A DMA thread handling priority
	9, // Chan B DMA thread handling priority
	tcDspUpp::eeReceive, // Directionality of Channel A.
	tcDspUpp::eeDisabled, // Directionality of Channel B.
	tcDspUpp::ee16Bit, // Chan A data bit width.
	tcDspUpp::ee16Bit, // Chan B data bit width.
	false, // Use XData[7:0] for ChanA[15:8] even if ChanB is
	// disabled. See Table 3 in uPP User's Guide for details.
	4, // Size of MBX for Channel A.
	4, // Size of MBX for Channel B.
	0, // Clock divider for Channel A
	// (only in transmit mode). See Section 2.1.1 in uPP User's
	// Guide for details. Value must be between 1 and 16.
	0, // Clock divider for Channel B
	// (only in transmit mode). See Section 2.1.1 in uPP User's
	// Guide for details. Value must be between 1 and 16.
	tcDspUpp::ee256Bytes, // Chan A Transmit Thresh
	tcDspUpp::ee256Bytes, // Chan A Receive Thresh
	tcDspUpp::ee256Bytes, // Chan B Transmit Thresh
	tcDspUpp::ee256Bytes, // Chan B Receive Thresh
	true, // Do not use Chan A start signal
	false, // Do not use Chan B start signal
	//tcDspUpp::eeUPP_2xTXCLK, // Using external 2xTxClk for Transmit
	tcDspUpp::eePLL0_SYSCLK2,
	0 // uPP DMA Master Priority
	};

// Constant for receive channel.
tcDspUpp::teUppChan meRecvChan = tcDspUpp::eeChanA;

/* Puntatore all'oggetto per uPP */
tcDspUpp* mpDspUpp;

// Handle to receive mailbox.
MBX_Handle mhRecvMbx = NULL;
tcDspUpp::tsMbxMsg lsRecvMbxMsg;

#pragma DATA_ALIGN( 64 );
uint16_t nBytePerLine;
uint16_t nLineCount;

void TestTask();

void main()
{
	TSK_Handle tsk;
	TSK_Attrs tsk_attrs;
	tsk_attrs = TSK_ATTRS;
	tsk_attrs.priority = 5;
	tsk_attrs.name = "Test";
	tsk  = TSK_create((Fxn)TestTask, &tsk_attrs);
	return;
}

void TestTask()
{

	short int i;

	nBytePerLine = 2;
	nLineCount = BUFF_SIZE;
	
	mpDspUpp = tcDspUpp::getInstance();
	if (!mpDspUpp->initialize(&gsUppConfig))
	{	flag = 1;
		mhRecvMbx = mpDspUpp->getMBX(meRecvChan);
	}
	else
		flag = 0;

	if (!mpDspUpp->receive(meRecvChan, (uint8_t*)buff1, (uint16_t)nBytePerLine, (uint16_t)nLineCount) )
	{	flag = 1;
		if(MBX_pend(mhRecvMbx, &lsRecvMbxMsg, 10)==true)
			BCACHE_inv(lsRecvMbxMsg.pBufPtr, nLineCount, true);
	}
	else
		flag = 0;

	while(1)
	{
		i = 1;
	}
}

