/*
 * Copyright (c) 2017-2018, 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.
 */
/*
 *  ======== nvsinternal.c ========
 */

#include <string.h>
#include <stdlib.h>
#include <stdint.h>

#include <stdbool.h>
#include <stdio.h>
#include <xdc/runtime/System.h>

/* DriverLib Includes */
#include <ti/devices/msp432e4/driverlib/driverlib.h>

/* Driver Header files */
#include <ti/display/Display.h>
#include <ti/drivers/NVS.h>
#include <ti/drivers/UART.h>
#include <ti/drivers/GPIO.h>
#include <ti/drivers/eeprom.h>
#include <ti/drivers/types.h>

/* For usleep() */
#include <unistd.h>

#include <pthread.h>

/* Example/Board Header files */
#include "Board.h"

#define FOOTER "=================================================="

/* Buffer placed in RAM to hold bytes read from non-volatile storage. */
unsigned char buffer2[1000];
unsigned char buffer1[10];
unsigned char Rd_Buffer[1000];
/*
 * Some devices have a minimum FLASH write size of 4-bytes (1 word). Trying
 * to write a non-multiple of 4 amount of data will fail. This array is
 * rounded up (to next multiple of 4) to meet this requirement. Refer to NVS
 * driver documentation for more details.
 */
//static const char signature[52] =    {"SimpleLink SDK Non-Volatile Storage (NVS) Example."};

unsigned char Start_Bit=0,Cmd_ID,Data_Received;
unsigned char Rx_Buffer[6]    =  {'0','0','0','0','0','0'} ;
unsigned char buffer3[10];
unsigned char Tx_Buffer[1000]  =   {'6','7','8','9',
                                    '1','b','c','d',
                                    'a','b','c','d',
                                    '1','2','3','4',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    '1','x','z','z',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    '1','1','1','1',
                                    '1','1','1','1',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    'a','b','c','d',
                                    '1','2','3','4',
                                    'a','b','c','d',
                                    '1','2','3','4'
                                    } ;

int i=0,Count=0;
uint32_t ui32Index;
uint32_t ui32EEPROMInitStatus;
uint32_t ui32EEPROMReadData[16];
bool     bStatus;

unsigned char Wr_Data[5];
//bStatus = true;

UART_Handle uart;
UART_Params uartParams;

NVS_Handle nvsHandle;
NVS_Attrs regionAttrs;
NVS_Params nvsParams;

Display_Handle displayHandle;

void *Rx_Thread(void *arg0);
void *Proc_Thread(void *arg0);

const char  echoPrompt3[] = "Write Executing..........\r\n";
const char  echoPrompt4[] = "Read Executing..........\r\n";
const char  echoPrompt[] = "Uart Initialised...\r\n";
//    const char  echoPrompt1[] = "Erasing Flash sector........\r\n";
//    const char  echoPrompt2[] = "Writing signature to flash..........\r\n";
//    const char  echoPrompt3[] = "Write Executing..........\r\n";
//    const char  echoPrompt4[] = "Read Executing..........\r\n";
const char  Test1[] = "Test Passed...\r\n";
const char  Test2[] = "Test Failed...\r\n";

/*
 *  ======== mainThread ========
 */
void *mainThread(void *arg0)
{
    bStatus = true;

    Wr_Data[0] = 0x7A;
    Wr_Data[1] = 0x7B;
    Wr_Data[2] = 0x7C;
    Wr_Data[3] = 0x7D;
    Wr_Data[4] = 0x7E;

    Display_init();
    NVS_init();
    UART_init();
    GPIO_init();

    GPIO_setConfig(Board_GPIO_LED0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);

    displayHandle = Display_open(Display_Type_UART, NULL);
    if (displayHandle == NULL)
    {
        /* Display_open() failed */
        while (1);
    }

    NVS_Params_init(&nvsParams);
    nvsHandle = NVS_open(Board_NVSINTERNAL, &nvsParams);

    if (nvsHandle == NULL)
    {
        Display_printf(displayHandle, 0, 0, "NVS_open() failed.");

        return (NULL);
    }

    Display_printf(displayHandle, 0, 0, "\n");

    UART_Params_init(&uartParams);
    uartParams.writeDataMode = UART_DATA_BINARY;
    uartParams.readDataMode = UART_DATA_BINARY;
    uartParams.readReturnMode = UART_RETURN_FULL;
    uartParams.readEcho = UART_ECHO_OFF;
    uartParams.baudRate = 115200;
   // uartParams.readCallback =   UART_MODE_CALLBACK;

    uart    =   UART_open(Board_UART4, &uartParams);

    if (uart == NULL)
    {
           /* UART_open() failed */
           while (1);
    }
    else
    {
        Display_printf(displayHandle, 0, 0, "UART Initialized.......");
        UART_write(uart, echoPrompt, sizeof(echoPrompt));
    }

    /*
     * This will populate a NVS_Attrs structure with properties specific
     * to a NVS_Handle such as region base address, region size,
     * and sector size.
     */
    NVS_getAttrs(nvsHandle, &regionAttrs);

    /* Display the NVS region attributes */
    Display_printf(displayHandle, 0, 0, "Region Base Address: 0x%x",regionAttrs.regionBase);
    Display_printf(displayHandle, 0, 0, "Sector Size: 0x%x",regionAttrs.sectorSize);
    Display_printf(displayHandle, 0, 0, "Region Size: 0x%x\n",regionAttrs.regionSize);


    /*      ***********************************************************************       */
    /*                              WHILE CONDITION                                       */
    /*      ***********************************************************************       */

    while(1)
    {
        UART_read(uart, &buffer1, 1);
        //      UART_write(uart,  &buffer1, 1);

       if((buffer1[0]    ==  0x24)  &&  (Start_Bit  ==  0))
       {
          Start_Bit    =   1;
          Count =   0;
       }

       if(Start_Bit ==  1)
       {
           Rx_Buffer[Count] =   buffer1[0];
           Count++;
           if(Count ==  4)
           {
               Start_Bit    =   0;
               Data_Received    =   1;
               Cmd_ID   =   Rx_Buffer[1];
           }
       }

       if(Data_Received ==  1)
       {
           UART_write(uart,  &Rx_Buffer, 4);
           Data_Received    =   0;
       }

       switch(Cmd_ID)
       {
       case 0x61:
           NVS_erase(nvsHandle, 0, regionAttrs.regionSize);
           for(i=0;i<250;i++)
           {
              NVS_write(nvsHandle, 0+(4*i), &Tx_Buffer[i], 4,NVS_WRITE_POST_VERIFY);
              Display_printf(displayHandle, 0, 0, "i= %d and Write Data: %x,%x,%x,%x\n",i, Tx_Buffer[0+(4*i)],Tx_Buffer[(4*i)+1],Tx_Buffer[(4*i)+2],Tx_Buffer[(4*i)+3]);
           }

           UART_write(uart, &echoPrompt3, sizeof(echoPrompt3));
           Cmd_ID   =   0;
           break;

       case 0x62:
           for(i=0;i<250;i++)
           {
              NVS_read(nvsHandle, (4*i), &Rd_Buffer[i], 4);
              //sleep(1);
              Display_printf(displayHandle, 0, 0, "Read Data[%d ]:   %x,%x,%x,%x\n, ",i, Rd_Buffer[(4*i)],Rd_Buffer[(4*i)+1],Rd_Buffer[(4*i)+2],Rd_Buffer[(4*i)+3]);
           }

//           NVS_read(nvsHandle, 0, &buffer2, 1);
//           Display_printf(displayHandle, 0, 0, "Read Data:  %s\n", buffer2[0]);
           UART_write(uart, &echoPrompt4, sizeof(echoPrompt4));

           Cmd_ID   =   0;
           break;
       }
    }



