/*
 * Copyright (c) 2015, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *     its contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 *  ======== spiloopback.c ========
 */
/* XDCtools Header files */

#include <xdc/std.h>
#include <xdc/runtime/System.h>

/* BIOS Header files */
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Task.h>

#include <drivers/spi/ioc_spi.h>
/* TI-RTOS Header files */
#include <ti/drivers/GPIO.h>
#include <ti/drivers/SPI.h>

/* Example/Board Header files */
#include "Board.h"
#include "ioc_debug.h"
#include "spi_test_main.h"

//#include <digital_output/digital_output_spi_test.h>

#define SPI_MSG_LENGTH    26

#define TASKSTACKSIZE     768

/* Allocate buffers in .dma section of memory for concerto devices */
#ifdef MWARE
#pragma DATA_SECTION(masterRxBuffer, ".dma");
#pragma DATA_SECTION(masterTxBuffer, ".dma");
#pragma DATA_SECTION(slaveRxBuffer, ".dma");
#pragma DATA_SECTION(slaveTxBuffer, ".dma");
#endif

Task_Struct task0Struct, task1Struct;
Char task0Stack[TASKSTACKSIZE], task1Stack[TASKSTACKSIZE];

//unsigned char masterRxBuffer[SPI_MSG_LENGTH];
unsigned char masterRxBuffer[3];
//unsigned char masterTxBuffer[SPI_MSG_LENGTH] = "Hello, this is master SPI";


unsigned char slaveRxBuffer[3];
//unsigned char slaveRxBuffer[SPI_MSG_LENGTH];
//unsigned char slaveTxBuffer[SPI_MSG_LENGTH] =  "Hello, this is slave SPI";

uint16_t ui16SpiRxBuffer[2];
uint16_t ui16SpiTxBuffer[2];

uint16_t spiTx1Buffer[2], spiTx2Buffer[2];
/* *  ======== slaveTaskFxn ========
 *  Task function for slave task.
 *
 *  This task runs on a higher priority, since slave
 *  has to be ready for master. Slave SPI sends a
 *  message to master and also receives message from
 *  master. Task for this function is created
 *  statically. See the project's .cfg file.
 */

/******************************************************************************/
void ioc_DefaultDOInit(void)
{
   spiConfigbus  stSpiConfig;
   void *pPoint;
   uint8_t i;

   //stSpiConfig.vui8SpiBus = Board_SPI3;
   stSpiConfig.vui8DataCount = 1;
   stSpiConfig.pui8SRxBuffer = ui16SpiRxBuffer;

   // Enabling both half bridges
   GPIO_write(BOARD_PORTM_PIN5, HIGH);

   // Making Chip Select 1 to high impedance state for
    //  digital output from 0 to 4
   GPIO_write(BOARD_PORTP_PIN1, HIGH);

   ui16SpiTxBuffer[0] = STATUS_RESET_REG;
   stSpiConfig.vui8ChipSelect = 1;
   stSpiConfig.pui8STxbuffer = ui16SpiTxBuffer;
   ioc_DOSpiTransfer(&stSpiConfig);
}

void ioc_UpdateDOData(unsigned char *pDO_Status, uint8_t ui8Do_Source_Sink)
{
    spiConfigbus  stSpiConfig;
   // i2cConfigbus stI2cConfig;
    uint16_t aui16SpiTxBuffer[2];
    uint16_t aui16SpiRxBuffer[2][2];
    uint8_t aui8I2cTxBuffer[3];
    uint8_t aui8I2cRxBuffer[3];
    uint8_t i;

    //stSpiConfig.vui8SpiBus = Board_SPI3;
    stSpiConfig.vui8DataCount = 1;
    stSpiConfig.pui8SRxBuffer = aui16SpiRxBuffer;

    // Digital output channel number from 0 to 5 select first half bridge
    aui16SpiTxBuffer[0] = 0;

   for(i = 0; i <= 5; i++)
   {    if(*(pDO_Status + i))
        {  if(~(ui8Do_Source_Sink & 0x1)) //Source High,so sinking output
              aui16SpiTxBuffer[0] = aui16SpiTxBuffer[0] | ((HB_ENABLE << i)
                                                    | (HB_CONFIG << i));
           else//Sink
               aui16SpiTxBuffer[0] = aui16SpiTxBuffer[0] | (HB_ENABLE << i)
                                                       & (~(HB_CONFIG << i));
        }
   }
   stSpiConfig.vui8ChipSelect = 1;
   stSpiConfig.pui8STxbuffer = aui16SpiTxBuffer;

   ioc_DOSpiTransfer(&stSpiConfig);

   // Digital output channel number from 6 to 11 select second half bridge
   /*aui16SpiTxBuffer[0] = 0;

      for(i = 0; i <= 5; i++)
      {   if(*(pDO_Status + i + 6))
          { if(~(ui8Do_Source_Sink & 0x1)) //Source High,so sinking output
              aui16SpiTxBuffer[0] = aui16SpiTxBuffer[0] | ((HB_ENABLE << i)
                                                    | (HB_CONFIG << i));
            else//Sink
               aui16SpiTxBuffer[0] = aui16SpiTxBuffer[0] | (HB_ENABLE << i)
                                                       & (~(HB_CONFIG << i));
          }
      }
    stSpiConfig.vui8ChipSelect = 2;
    stSpiConfig.pui8STxbuffer = aui16SpiTxBuffer;
    ioc_DOSpiTransfer(&stSpiConfig);*/
}

    // Digital output channel number from 12 to 17 select second half bridge
       /*
        aui16SpiTxBuffer[0] = 0;

       for(i = 0; i <= 5; i++)
       {   if(*(pDO_Status + i + 12))
           { if(~((ui8Do_Source_Sink>>1) & 0x1) & 0x1) //Source High
               aui16SpiTxBuffer[0] = aui16SpiTxBuffer[0] | ((HB_ENABLE << i)
                                                     | (HB_CONFIG << i));
             else//Sink
                aui16SpiTxBuffer[0] = aui16SpiTxBuffer[0] | (HB_ENABLE << i)
                                                        & (~(HB_CONFIG << i));
           }
       }
       stSpiConfig.vui8ChipSelect = 3;//for 3rd half bridge driver
       stSpiConfig.pui8STxbuffer = aui16SpiTxBuffer;
       ioc_DOSpiTransfer(&stSpiConfig);

       // Digital output channel number from 18 to 23 select second half bridge
       aui16SpiTxBuffer[0] = 0;

       for(i = 0; i <= 5; i++)
       {   if(*(pDO_Status + i + 18))
           { if(~((ui8Do_Source_Sink>>1) & 0x1) & 0x1) //Source High
               aui16SpiTxBuffer[0] = aui16SpiTxBuffer[0] | ((HB_ENABLE << i)
                                                     | (HB_CONFIG << i));
             else//Sink
                aui16SpiTxBuffer[0] = aui16SpiTxBuffer[0] | (HB_ENABLE << i)
                                                        & (~(HB_CONFIG << i));
           }
       }
       stSpiConfig.vui8ChipSelect = 4;//for 4th half bridge driver
       stSpiConfig.pui8STxbuffer = aui16SpiTxBuffer;
       ioc_DOSpiTransfer(&stSpiConfig);*/

       /*
        aui8I2cTxBuffer[1] = 0;
    aui8I2cTxBuffer[2] = 0;

    for(i = 0; i <= 11; i++) /////check this code for LEDs
        {   if(i <= 7)
                if(*(pDO_Status + i))//if output active
                    aui8I2cTxBuffer[1] = aui8I2cTxBuffer[1] & (~(0x1 << i));//turn ON the led
                else
                    aui8I2cTxBuffer[1] = aui8I2cTxBuffer[1] | (0x1 << i);//else turn OFF
            else //if(i <= 11)
                if(*(pDO_Status + i))
                    aui8I2cTxBuffer[2] = aui8I2cTxBuffer[2] & (~(0x1 << (i - 8)));
                else
                    aui8I2cTxBuffer[2] = aui8I2cTxBuffer[2] | (0x1 << (i - 8));

        }

        //check I2C and SPI communication formats
        aui8I2cTxBuffer[0] = DIO_OUTPUTREG_PORTONE;
        stI2cConfig.ui8regaddr = DIO_OUTPUTREG_PORTONE;
        stI2cConfig.vui8slaveAddress =  LED_SLAVEADDRESS1;//IO expander IC address
        stI2cConfig.vui8I2cBus = Board_I2C2;
        stI2cConfig.pui8txbuffer = aui8I2cTxBuffer;
        stI2cConfig.pui8RxBuffer = aui8I2cRxBuffer;
        stI2cConfig.vui8writeCount = 3;
        stI2cConfig.vui8readCount = 0;
        ioc_DigitalLEDI2CTransfer(&stI2cConfig);

        for(i = 12; i < 24; i++) /////check this code for LEDs
        {   if(i <= 19)
                if(*(pDO_Status + i))
                    aui8I2cTxBuffer[1] = aui8I2cTxBuffer[1] & (~(0x1 << (i - 12)));
                else
                    aui8I2cTxBuffer[1] = aui8I2cTxBuffer[1] | (0x1 << (i - 12));
            else
                if(*(pDO_Status + i))
                aui8I2cTxBuffer[2] = aui8I2cTxBuffer[2] & (~(0x1 << (i - 20)));
                else
                aui8I2cTxBuffer[2] = aui8I2cTxBuffer[2] | (0x1 << (i - 20));
        }

        aui8I2cTxBuffer[0] = DIO_OUTPUTREG_PORTONE;
        stI2cConfig.ui8regaddr = DIO_OUTPUTREG_PORTONE;
        stI2cConfig.vui8slaveAddress =  LED_SLAVEADDRESS2;//IO expander IC address
        stI2cConfig.vui8I2cBus = Board_I2C2;
        stI2cConfig.pui8txbuffer = aui8I2cTxBuffer;
        stI2cConfig.pui8RxBuffer = aui8I2cRxBuffer;
        stI2cConfig.vui8writeCount = 3;
        stI2cConfig.vui8readCount = 0;
        ioc_DigitalLEDI2CTransfer(&stI2cConfig);*/

/*void ioc_UpdateDOData(void)
{

}*/

void DigitalOutputProcessingFunc()
{
    uint8_t i;
    void *pPoint;
    uint8_t u8Do_Source_Sink;
    bool pDO_Status[DIGITAL_OUTPUT_COUNT];
    //pD0_status,ui16Do_Source_Sink
    u8Do_Source_Sink = 0x00;
    //bDoInitialized = TRUE;
    pDO_Status[0] = true;

    ioc_UpdateDOData(pDO_Status, u8Do_Source_Sink);
}


/* Void slaveTaskFxn (UArg arg0, UArg arg1)
{
    SPI_Handle slaveSpi;
    SPI_Params slaveSpiParams;
    SPI_Transaction slaveTransaction;
    bool transferOK;

    // Initialize SPI handle with slave mode
    SPI_Params_init(&slaveSpiParams);
    slaveSpiParams.mode = SPI_SLAVE;
    slaveSpi = SPI_open(Board_SPI1, &slaveSpiParams);
    if (slaveSpi == NULL) {
        System_abort("Error initializing SPI\n");
    }
    else {
        System_printf("SPI initialized\n");
    }

    // Initialize slave SPI transaction structure
    slaveTransaction.count = SPI_MSG_LENGTH;
    slaveTransaction.txBuf = (Ptr)slaveTxBuffer;
    slaveTransaction.rxBuf = (Ptr)slaveRxBuffer;

    // Initiate SPI transfer
    transferOK = SPI_transfer(slaveSpi, &slaveTransaction);

    if(transferOK) {
        // Print contents of slave receive buffer
        System_printf("Slave: %s\n", slaveRxBuffer);
    }
    else {
        System_printf("Unsuccessful slave SPI transfer");
    }

    // Deinitialize SPI
    SPI_close(slaveSpi);
}*/

/*
 *  ======== masterTaskFxn ========
 *  Task function for master task.
 *
 *  This task runs at a lower priority after the slave
 *  task to ensure it is ready for a transaction.
 *  Master SPI sends a message to slave and also
 *  receives message from slave. Task for this function
 *  is created statically. See the project's .cfg
 *  file.
 */
/*Void masterTaskFxn (UArg arg0, UArg arg1)
{
    SPI_Handle masterSpi;
    SPI_Transaction masterTransaction;
    bool transferOK;

    // Initialize SPI handle as default master
    masterSpi = SPI_open(Board_SPI0, NULL);
    if (masterSpi == NULL) {
        System_abort("Error initializing SPI\n");
    }
    else {
        System_printf("SPI initialized\n");
    }

    // Initialize master SPI transaction structure
    masterTransaction.count = SPI_MSG_LENGTH;
    masterTransaction.txBuf = (Ptr)masterTxBuffer;
    masterTransaction.rxBuf = (Ptr)masterRxBuffer;

    // Initiate SPI transfer
    transferOK = SPI_transfer(masterSpi, &masterTransaction);

    if(transferOK) {
        // Print contents of master receive buffer
        System_printf("Master: %s\n", masterRxBuffer);
    }
    else {
        System_printf("Unsuccessful master SPI transfer");
    }

    // Deinitialize SPI
    SPI_close(masterSpi);

    System_printf("Done\n");

    System_flush();
}*/


Void masterTaskFxn (UArg arg0, UArg arg1)
{
    SPI_Handle masterSpi;
    SPI_Transaction masterTransaction;
    bool transferOK;

    uint16_t aui16SpiTxBuffer[2];
    SPI_Params stSpiParams;

    SPI_Params_init(&stSpiParams);
   stSpiParams.bitRate = 1500000;
   stSpiParams.frameFormat = SPI_POL0_PHA1;
   stSpiParams.dataSize = 16;

    // Initialize SPI handle as default master
    //masterSpi = SPI_open(Board_SPI1, NULL);
       masterSpi = SPI_open(Board_SPI1, &stSpiParams);
    if (masterSpi == NULL) {
        System_abort("Error initializing SPI\n");
    }
    else {
        System_printf("SPI initialized\n");
    }

    aui16SpiTxBuffer[0] = aui16SpiTxBuffer[0] | (HB_ENABLE << 0)
                                                           & (~(HB_CONFIG << 0));
    // Initialize master SPI transaction structure
    masterTransaction.count = 1;//16;//SPI_MSG_LENGTH;
    masterTransaction.txBuf = aui16SpiTxBuffer[0];//(Ptr)masterTxBuffer;
    masterTransaction.rxBuf = (Ptr)masterRxBuffer;

    // Initiate SPI transfer
    transferOK = SPI_transfer(masterSpi, &masterTransaction);

    if(transferOK) {
        // Print contents of master receive buffer

        System_printf("Master: %s\n", masterRxBuffer);
    }
    else {
        System_printf("Unsuccessful master SPI transfer");
    }

    // Deinitialize SPI
    SPI_close(masterSpi);

    System_printf("Done\n");

    System_flush();
}
/*
 *  ======== main ========
 */
int main(void)
{
    /* Construct BIOS objects */
    Task_Params taskParams;

    /* Call board init functions. */
    Board_initGeneral();
    Board_initGPIO();
    Board_initSPI();

    GPIOPinTypeGPIOOutput(GPIO_PORTP_BASE, GPIO_PIN_1);

    GPIOPinTypeGPIOOutput(GPIO_PORTM_BASE, GPIO_PIN_5);


    /* Construct master/slave Task threads */

    Task_Params_init(&taskParams);
    taskParams.priority = 1;
    taskParams.stackSize = TASKSTACKSIZE;
    taskParams.stack = &task0Stack;
    Task_construct(&task0Struct, (Task_FuncPtr)masterTaskFxn, &taskParams, NULL);

   /* taskParams.stack = &task1Stack;
    taskParams.priority = 2;
    Task_construct(&task1Struct, (Task_FuncPtr)slaveTaskFxn, &taskParams, NULL);*/

    /*Task_Params_init(&taskParams);
    taskParams.priority = 1;
    taskParams.stackSize = TASKSTACKSIZE;
    taskParams.stack = &task0Stack;
    Task_construct(&task0Struct, (Task_FuncPtr)ioc_DefaultDOInit, &taskParams, NULL);*/

    /* Turn on user LED */
    GPIO_write(Board_LED0, Board_LED_ON);

    //GPIO_write(BOARD_PORTM_PIN5, HIGH);

    System_printf("Starting the SPI loop-back example\nSystem provider is set to"
                  " SysMin. Halt the target to view any SysMin contents in ROV.\n");
    /* SysMin will only print to the console when you call flush or exit */
    System_flush();
    System_printf("This example requires external wires to be connected to the "
                  "header pins. Please see the Getting Started Guide for "
                  "details.\n");
    /* SysMin will only print to the console when you call flush or exit */
    System_flush();

    /* Start BIOS */
    BIOS_start();

    return (0);
}
