#include <vect.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
//#include <ti/dsplib/dsplib.h>
//#include "gen_twiddle_fft32x32.h"
/* BIOS Header files */
#include <ti/sysbios/BIOS.h>
#include "resource_mgr.h"
#include "platform.h"
#include "evmc66x_spi.h"
#include <cslr_spi.h>
#include "csl_psc.h"
#include "csl_pscAux.h"
#include "cslr_sem.h"
/* SPI Header files */
#include <ti/drv/spi/SPI.h>
#if defined(SOC_K2H) || defined(SOC_K2K) || defined(SOC_K2E) || defined(SOC_K2L) || defined(SOC_K2G) || defined(SOC_C6678) || defined(SOC_C6657) || defined(SOC_OMAPL137) || defined(SOC_OMAPL138)
#include <ti/drv/spi/src/v0/SPI_v0.h>
#endif
#include <ti/drv/spi/soc/SPI_soc.h>
#include <ti/drv/spi/test/src/SPI_log.h>
#include <ti/drv/spi/test/src/SPI_test.h>

/* Board Header files */
#include <ti/board/board.h>
#include <ti/board/src/flash/include/board_flash.h>

#define nbytes 0x00000002

/* Buffer containing the received data */
uint16_t rxBuf[NUM_OF_SAMPLES];
uint16_t txBuf[NUM_OF_SAMPLES];
uint16_t *ptr_tx = txBuf;
//ptr_tx = &txBuf;
uint16_t *ptr_rx = rxBuf;
//ptr_rx = &rxBuf;

uint32_t status;
uint32_t status1;

uint8_t *in1 = 0;
uint8_t *in2 = 0;

/* OSAL functions for Platform Library */
uint8_t *Osal_platformMalloc (uint32_t num_bytes, uint32_t alignment)
{
    return malloc(num_bytes);
}

void Osal_platformFree (uint8_t *dataPtr, uint32_t num_bytes)
{
    /* Free up the memory */
    if (dataPtr)
    {
        free(dataPtr);
    }
}
void Osal_platformSpiCsEnter(void)
{
    /* Get the hardware semaphore.
     *
     * Acquire Multi core CPPI synchronization lock
     */
    while ((CSL_semAcquireDirect (PLATFORM_SPI_HW_SEM)) == 0);

    return;
}

void Osal_platformSpiCsExit (void)
{
    /* Release the hardware semaphore
     *
     * Release multi-core lock.
     */
    CSL_semReleaseSemaphore (PLATFORM_SPI_HW_SEM);

    return;
}
void PowerUpDomains (void);
int main(void)
{
    int i;
    CSL_PSC_setModuleNextState (CSL_PSC_LPSC_EMIF25_SPI,  PSC_MODSTATE_ENABLE);
    PowerUpDomains();
    spi_claim(1,1000000);

    for(i=0;i>-1;i++)
    {
        status = spi_xfer(nbytes,NULL,in1,FALSE);
        status1 = spi_xfer(nbytes,NULL,in2,TRUE);

        rxBuf[i%100] = (int)in1 + ((int)in2)*256;
    }

    spi_release();
//    BIOS_start();
//    return(0);
}

