//###########################################################################
//
// FILE:    flash_kernel_c28x_dual_ex1_erase_cpu1.c
//
// TITLE:   Erase functions
//!  <h1> Erase functions</h1>
//
//###########################################################################
// 
// C2000Ware v26.01.00.00
//
// Copyright (C) 2024 Texas Instruments Incorporated - http://www.ti.com
//
// 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.
// $
//###########################################################################

//
// Included Files
//

#include <flash_kernel_c28x_dual_ex1_erase_cpu1.h>
#include "F021_F2838x_C28x.h"
#include "flash_programming_f2838x_c28x.h" // Flash API example header file
#include "Types.h"
#include "f2838x_kernel_commands_cpu1.h"



//
// Globals
//
typedef struct
{
   uint16_t status;
   uint32_t address;
   uint16_t flashAPIError;
   uint32_t flashAPIFsmStatus;
}  StatusCode;
extern StatusCode statusCode;

const uint32_t sectAddress[14] = {  Bzero_Sector0_start,
                                  Bzero_Sector1_start,
                                  Bzero_Sector2_start,
                                  Bzero_Sector3_start,
                                  Bzero_Sector4_start,
                                  Bzero_Sector5_start,
                                  Bzero_Sector6_start,   //FLASH_SIZE 0x6
                                  Bzero_Sector7_start,
                                  Bzero_Sector8_start,
                                  Bzero_Sector9_start,
                                  Bzero_Sector10_start,
                                  Bzero_Sector11_start,
                                  Bzero_Sector12_start,
                                  Bzero_Sector13_start };  //FLASH_SIZE 0x7

const uint16_t sectSize[14] = {   Sector16KB_u32length,
                                Sector16KB_u32length,
                                Sector16KB_u32length,
                                Sector16KB_u32length,
                                Sector64KB_u32length,
                                Sector64KB_u32length,
                                Sector64KB_u32length,  //FLASH_SIZE 0x6
                                Sector64KB_u32length,
                                Sector64KB_u32length,
                                Sector64KB_u32length,
                                Sector16KB_u32length,
                                Sector16KB_u32length,
                                Sector16KB_u32length,
                                Sector16KB_u32length }; //FLASH_SIZE 0x7
void setFlashAPIError(Fapi_StatusType status);

void setFlashAPIError(Fapi_StatusType status)
{
    if(status == Fapi_Error_AsyncIncorrectDataBufferLength)
    {
        statusCode.flashAPIError = INCORRECT_DATA_BUFFER_LENGTH;
    }
    else if(status == Fapi_Error_AsyncIncorrectEccBufferLength)
    {
        statusCode.flashAPIError = INCORRECT_ECC_BUFFER_LENGTH;
    }
    else if(status == Fapi_Error_AsyncDataEccBufferLengthMismatch)
    {
        statusCode.flashAPIError = DATA_ECC_BUFFER_LENGTH_MISMATCH;
    }
    else if(status == Fapi_Error_FlashRegsNotWritable)
    {
        statusCode.flashAPIError = FLASH_REGS_NOT_WRITABLE;
    }
    else if(status == Fapi_Error_FeatureNotAvailable)
    {
        statusCode.flashAPIError = FEATURE_NOT_AVAILABLE;
    }
    else if(status == Fapi_Error_InvalidAddress)
    {
        statusCode.flashAPIError = INVALID_ADDRESS;
    }
    else if(status == Fapi_Error_InvalidCPUID)
    {
        statusCode.flashAPIError = INVALID_CPUID;
    }
    else if(status == Fapi_Error_Fail)
    {
        statusCode.flashAPIError = FAILURE;
    }
    else
    {
        statusCode.status = NOT_RECOGNIZED;
    }
}

//
// sharedErase - This routine takes the 32-bit sectors variable as
//                a parameter.  Each bit corresponds to a sector,
//                starting with bit 0 and sector A.  This routine
//                attempts to erase the sectors specified.
//
void sharedErase(uint32_t sectors)
{
    statusCode.status = NO_ERROR;
    statusCode.address = 0x12345678;
    int i = 0;
    Fapi_StatusType oReturnCheck;
    Fapi_FlashStatusType oFlashStatus;
    Fapi_FlashStatusWordType oFlashStatusWord;

    int fail = 0;
    EALLOW;
    for(i = 0; i < 14; i++)
    {
        if((sectors & 0x00000001) == 0x00000001 && fail == 0)
        {
            oReturnCheck = Fapi_issueAsyncCommandWithAddress(Fapi_EraseSector,
                    (uint32_t *)(sectAddress[i]));
            //
            // wait until AsyncCommand is done.
            //
            while (Fapi_checkFsmForReady() != Fapi_Status_FsmReady)
            {
            }
            oReturnCheck = Fapi_doBlankCheck((uint32_t *)(sectAddress[i]),
                                             sectSize[i],
                                             &oFlashStatusWord);
            oFlashStatus = Fapi_getFsmStatus();
            if(oReturnCheck != Fapi_Status_Success)
            {
                if(fail == 0) //first fail
                {
                    statusCode.status = BLANK_ERROR;
                    statusCode.address = oFlashStatusWord.au32StatusWord[0];
                    setFlashAPIError(oReturnCheck);
                    statusCode.flashAPIFsmStatus = (uint32_t)oFlashStatus;
                }
                fail++;
            }

        }
        sectors = sectors >> 1;
    }
    EDIS;
    return;
}

//
// End of file
//
