/*********************************************************************************
**										 **
** Name			:   codeflash.c	                                         **
**										 **
** Description	:								 **
**										 **
** 										 **
** Organization	:  Sun Mobility Pvt Ltd                             		 **
**										 **
** Revision		:   1.00 						 **
**********************************************************************************
** Revision History								 **
**  Rev      Date         By                  	Reason for Change 		 **
**------------------------------------------------------------------------------**
**  1.00     27-04-2020   Mohan JR		Base Version			 **
**										 **
*********************************************************************************/

/*********************************************************************************
**                     I N C L U D E  F I L E S
*********************************************************************************/
#include	"CodeFlash.h"
#include	"DataFlash.h"
#include    "ti/driverlib/m0p/dl_core.h"
#include    "ti_msp_dl_config.h"
/*********************************************************************************
**                          F U N C T I O N S
*********************************************************************************/

/*********************************************************************************
**   I N T E R N A L   M A C R O   A N D   T Y P E   D E F I N I T I O N S
*********************************************************************************/
typedef union MemoryParam_Tag
{
	struct
	{
	    uint8_t   Byte0;
	    uint8_t   Byte1;
	    uint8_t   Byte2;
	    uint8_t   Byte3;
	}Bytes;
	uint32_t Word;   
}MemoryParam;

        
/*********************************************************************************
**      I N T E R N A L   F U N C T I O N   P R O T O T Y P E S
*********************************************************************************/

/*********************************************************************************
**                   		V A R I A B L E S
**********************************************************************************/

/*********************************************************************************
** Name			:	SM_CodeFlash_Init				**
** Description	:								**
** Inputs		:	None						**
** Outputs		:							**
** Returns		:	None						**
*********************************************************************************/
void SM_CodeFlash_Init(void)
{
}

/*********************************************************************************
** Name			:	SM_CodeFlash_Write				**
** Description	:								**
** Inputs		:	None						**
** Outputs		:							**
** Returns		:	None						**
*********************************************************************************/
uint8_t SM_CodeFlash_Write(uint32_t lUI32_AppAddress, uint32_t *lUI32_Data,uint16_t lUI16_NoOfWords)
{
    uint8_t     lUI8_WriteErrorType = NO_ERROR;
    uint8_t     lUI8_WriteCmdStatus=0U; 
    uint16_t    lUI16_ProgramIndex=0;
    volatile uint32_t  lUI32_WriteAddress=0x00;
    uint16_t    lUI16_WriteTimeout=0;

    lUI32_WriteAddress=lUI32_AppAddress;
  	 /*
        * Program single flash word (64-bit data) write to flash in main memory
        * with ECC enabled. Data must be loaded 32-bits at a time, but a single
        * word program is executed. The flash controller hardware will handle
        * generating the 8-bit ECC code.
        */

        /*
         * Ensure proper flash command execution by clearing the STATCMD
         * register before executing a flash operation
         */
        for(lUI16_ProgramIndex=lUI16_NoOfWords;lUI16_ProgramIndex>0;lUI16_ProgramIndex--)
        {
            lUI16_WriteTimeout=0;
            DL_FlashCTL_unprotectSector(
                FLASHCTL, lUI32_WriteAddress, DL_FLASHCTL_REGION_SELECT_MAIN);
            lUI8_WriteCmdStatus = DL_FlashCTL_programMemoryFromRAM64WithECCGenerated(
                FLASHCTL, (lUI32_WriteAddress), lUI32_Data);
                
            while(lUI8_WriteCmdStatus==DL_FLASHCTL_COMMAND_STATUS_IN_PROGRESS && lUI16_WriteTimeout++<6400)
            if (lUI8_WriteCmdStatus != DL_FLASHCTL_COMMAND_STATUS_PASSED) 
            {
                /* If command did not pass, set error flag */
                lUI8_WriteErrorType = ERROR_FAIL;
                break;
            }
            else
            {
                /* Read verify on the 8-bit data */
                lUI8_WriteCmdStatus = DL_FlashCTL_readVerifyFromRAM64WithECCGenerated(
                    FLASHCTL, (lUI32_WriteAddress), lUI32_Data);
                if (lUI8_WriteCmdStatus != DL_FLASHCTL_COMMAND_STATUS_PASSED) 
                {
                    /* If command did not pass, set error flag. */
                    lUI8_WriteErrorType = ERROR_FAIL;
                    break;
                }
                else
                {
                    /*Nothing*/
                }                
            }
            lUI32_WriteAddress=lUI32_WriteAddress+8;
            lUI32_Data=lUI32_Data+2;
            
        }
	return lUI8_WriteErrorType;
}
/*********************************************************************************
** Name			:	SM_CodeFlash_Erase				**
** Description	:								**
** Inputs		:	None						**
** Outputs		:							**
** Returns		:	None						**
*********************************************************************************/
uint8_t SM_CodeFlash_Erase(uint32_t lUI32_AppBaseAddress, uint16_t lUI16_NoOfSectors)
{
    uint8_t     lUI8_EraseErrorType = NO_ERROR;
    uint8_t     lUI8_EraseCmdStatus=0U; 
    uint16_t    lUI16_EraseIndex=0;
    volatile uint32_t  lUI32_EraseAddress=0x00;
    uint16_t    lUI16_EraseTimeout=0;

    lUI32_EraseAddress=lUI32_AppBaseAddress;
	/* Erase sector in main memory */    
    SM_CodeFlash_Authentication();
    for(lUI16_EraseIndex=lUI16_NoOfSectors;lUI16_EraseIndex>0;lUI16_EraseIndex--)
    { 
        lUI16_EraseTimeout=0;
        DL_FlashCTL_unprotectSector(
            FLASHCTL, lUI32_EraseAddress, DL_FLASHCTL_REGION_SELECT_MAIN);
        lUI8_EraseCmdStatus = DL_FlashCTL_eraseMemoryFromRAM(
            FLASHCTL, lUI32_EraseAddress, DL_FLASHCTL_COMMAND_SIZE_SECTOR);           
        while(lUI8_EraseCmdStatus==DL_FLASHCTL_COMMAND_STATUS_IN_PROGRESS && lUI16_EraseTimeout++<6400)
        if (lUI8_EraseCmdStatus != DL_FLASHCTL_COMMAND_STATUS_PASSED) 
        {
            /* If command did not pass, set error flag */
            lUI8_EraseErrorType = ERROR_FAIL;
            break;
        }
        else 
        {   
            /*Nothing*/
        }
        lUI32_EraseAddress=lUI32_EraseAddress+SECTOR_MEM_SIZE;
    }
    return lUI8_EraseErrorType;
}

/*********************************************************************************
** Name			:	SM_CodeFlash_Authentication			**
** Description	:								**
** Inputs		:	None						**
** Outputs		:							**
** Returns		:	None						**
*********************************************************************************/
void SM_CodeFlash_Authentication(void)
{
    /*
     * Ensure proper flash command execution by clearing the STATCMD
     * register before executing a flash operation
     */
    DL_FlashCTL_executeClearStatus(FLASHCTL);
}




