/*
 * Copyright (c) 2022, Shenzhen CVA Innovation CO.,LTD
 * All rights reserved.
 *
 * Shenzhen CVA Innovation CO.,LTD (CVA chip) is supplying this file for use
 * exclusively with CVA's microcontroller products. This file can be freely
 * distributed within development tools that are supporting such microcontroller
 * products.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
 * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
 * CVA SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
 * OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 */

/*******************************************************************************
 * the includes
 ******************************************************************************/

#include <stddef.h>
#include "mcu.h"
#include "drivers/flash/flash_drv.h"
#include "fee_extra.h"

extern McuType mcu;

/*******************************************************************************
 * the defines
 ******************************************************************************/

#define FEE_INTERNAL_CRC_TABLE_SIZE  (256U)
#define FEE_INTERNAL_WRITE_PHASE_LEN (8)
/*******************************************************************************
 * the typedefs
 ******************************************************************************/

typedef enum
{
    FEE_INTERNAL_ERROR_WRITE_PHASE_UNEARSE = 0x00,
    FEE_INTERNAL_ERROR_WRITE_PHASE_ERROR,
    FEE_INTERNAL_ERROR_WRITE_PHASE_READBACK_ERROR,
    FEE_INTERNAL_ERROR_READ_ERROR,
    FEE_INTERNAL_ERROR_READ_UNWRITE_ERROR,
    FEE_INTERNAL_ERROR_READ_READBACK_ERROR,
    FEE_INTERNAL_ERROR_UNKNOW = 0xFF,
} Fee_InternalErrorsType;

/*******************************************************************************
 * the globals
 ******************************************************************************/

static const uint8_t Fee_CrcTable8[FEE_INTERNAL_CRC_TABLE_SIZE] =
{
  0x00U, 0x1DU, 0x3AU, 0x27U, 0x74U, 0x69U, 0x4EU, 0x53U, 0xE8U, 0xF5U, 0xD2U,
  0xCFU, 0x9CU, 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0xBCU, 0x9BU, 0x86U, 0xD5U, 0xC8U, 0xEFU, 0xF2U, 0x49U, 0x54U,
  0x73U, 0x6EU, 0x3DU, 0x20U, 0x07U, 0x1AU, 0x6CU, 0x71U, 0x56U, 0x4BU, 0x18U,
  0x05U, 0x22U, 0x3FU, 0x84U, 0x99U, 0xBEU, 0xA3U, 0xF0U, 0xEDU, 0xCAU, 0xD7U,
  0x35U, 0x28U, 0x0FU, 0x12U, 0x41U, 0x5CU, 0x7BU, 0x66U, 0xDDU, 0xC0U, 0xE7U,
  0xFAU, 0xA9U, 0xB4U, 0x93U, 0x8EU, 0xF8U, 0xE5U, 0xC2U, 0xDFU, 0x8CU, 0x91U,
  0xB6U, 0xABU, 0x10U, 0x0DU, 0x2AU, 0x37U, 0x64U, 0x79U, 0x5EU, 0x43U, 0xB2U,
  0xAFU, 0x88U, 0x95U, 0xC6U, 0xDBU, 0xFCU, 0xE1U, 0x5AU, 0x47U, 0x60U, 0x7DU,
  0x2EU, 0x33U, 0x14U, 0x09U, 0x7FU, 0x62U, 0x45U, 0x58U, 0x0BU, 0x16U, 0x31U,
  0x2CU, 0x97U, 0x8AU, 0xADU, 0xB0U, 0xE3U, 0xFEU, 0xD9U, 0xC4U
#endif
};

static const uint16_t Fee_CrcTable16[FEE_INTERNAL_CRC_TABLE_SIZE] =
{
  0x0000U, 0x1021U, 0x2042U, 0x3063U, 0x4084U, 0x50A5U, 0x60C6U, 0x70E7U,
  0x8108U, 0x9129U, 0xA14AU, 0xB16BU, 0xC18CU, 0xD1ADU, 0xE1CEU, 0xF1EFU,
#if (FEE_INTERNAL_CRC_TABLE_SIZE == 256U)
  0x1231U, 0x0210U, 0x3273U, 0x2252U, 0x52B5U, 0x4294U, 0x72F7U, 0x62D6U,
  0x9339U, 0x8318U, 0xB37BU, 0xA35AU, 0xD3BDU, 0xC39CU, 0xF3FFU, 0xE3DEU,
  0x2462U, 0x3443U, 0x0420U, 0x1401U, 0x64E6U, 0x74C7U, 0x44A4U, 0x5485U,
  0xA56AU, 0xB54BU, 0x8528U, 0x9509U, 0xE5EEU, 0xF5CFU, 0xC5ACU, 0xD58DU,
  0x3653U, 0x2672U, 0x1611U, 0x0630U, 0x76D7U, 0x66F6U, 0x5695U, 0x46B4U,
  0xB75BU, 0xA77AU, 0x9719U, 0x8738U, 0xF7DFU, 0xE7FEU, 0xD79DU, 0xC7BCU,
  0x48C4U, 0x58E5U, 0x6886U, 0x78A7U, 0x0840U, 0x1861U, 0x2802U, 0x3823U,
  0xC9CCU, 0xD9EDU, 0xE98EU, 0xF9AFU, 0x8948U, 0x9969U, 0xA90AU, 0xB92BU,
  0x5AF5U, 0x4AD4U, 0x7AB7U, 0x6A96U, 0x1A71U, 0x0A50U, 0x3A33U, 0x2A12U,
  0xDBFDU, 0xCBDCU, 0xFBBFU, 0xEB9EU, 0x9B79U, 0x8B58U, 0xBB3BU, 0xAB1AU,
  0x6CA6U, 0x7C87U, 0x4CE4U, 0x5CC5U, 0x2C22U, 0x3C03U, 0x0C60U, 0x1C41U,
  0xEDAEU, 0xFD8FU, 0xCDECU, 0xDDCDU, 0xAD2AU, 0xBD0BU, 0x8D68U, 0x9D49U,
  0x7E97U, 0x6EB6U, 0x5ED5U, 0x4EF4U, 0x3E13U, 0x2E32U, 0x1E51U, 0x0E70U,
  0xFF9FU, 0xEFBEU, 0xDFDDU, 0xCFFCU, 0xBF1BU, 0xAF3AU, 0x9F59U, 0x8F78U,
  0x9188U, 0x81A9U, 0xB1CAU, 0xA1EBU, 0xD10CU, 0xC12DU, 0xF14EU, 0xE16FU,
  0x1080U, 0x00A1U, 0x30C2U, 0x20E3U, 0x5004U, 0x4025U, 0x7046U, 0x6067U,
  0x83B9U, 0x9398U, 0xA3FBU, 0xB3DAU, 0xC33DU, 0xD31CU, 0xE37FU, 0xF35EU,
  0x02B1U, 0x1290U, 0x22F3U, 0x32D2U, 0x4235U, 0x5214U, 0x6277U, 0x7256U,
  0xB5EAU, 0xA5CBU, 0x95A8U, 0x8589U, 0xF56EU, 0xE54FU, 0xD52CU, 0xC50DU,
  0x34E2U, 0x24C3U, 0x14A0U, 0x0481U, 0x7466U, 0x6447U, 0x5424U, 0x4405U,
  0xA7DBU, 0xB7FAU, 0x8799U, 0x97B8U, 0xE75FU, 0xF77EU, 0xC71DU, 0xD73CU,
  0x26D3U, 0x36F2U, 0x0691U, 0x16B0U, 0x6657U, 0x7676U, 0x4615U, 0x5634U,
  0xD94CU, 0xC96DU, 0xF90EU, 0xE92FU, 0x99C8U, 0x89E9U, 0xB98AU, 0xA9ABU,
  0x5844U, 0x4865U, 0x7806U, 0x6827U, 0x18C0U, 0x08E1U, 0x3882U, 0x28A3U,
  0xCB7DU, 0xDB5CU, 0xEB3FU, 0xFB1EU, 0x8BF9U, 0x9BD8U, 0xABBBU, 0xBB9AU,
  0x4A75U, 0x5A54U, 0x6A37U, 0x7A16U, 0x0AF1U, 0x1AD0U, 0x2AB3U, 0x3A92U,
  0xFD2EU, 0xED0FU, 0xDD6CU, 0xCD4DU, 0xBDAAU, 0xAD8BU, 0x9DE8U, 0x8DC9U,
  0x7C26U, 0x6C07U, 0x5C64U, 0x4C45U, 0x3CA2U, 0x2C83U, 0x1CE0U, 0x0CC1U,
  0xEF1FU, 0xFF3EU, 0xCF5DU, 0xDF7CU, 0xAF9BU, 0xBFBAU, 0x8FD9U, 0x9FF8U,
  0x6E17U, 0x7E36U, 0x4E55U, 0x5E74U, 0x2E93U, 0x3EB2U, 0x0ED1U, 0x1EF0U
#endif
};

Fee_InternalErrorsType Fee_InternalErrors = FEE_INTERNAL_ERROR_UNKNOW;

/*******************************************************************************
 * the functions
 ******************************************************************************/

static FlashDrv_Status FlashDrv_ReadData(FlashDrvType *obj, uint8_t *srcaddr, uint8_t *destaddr, uint32_t rdsize);

Fls_StateType Fee_InternalIsWriteable(Fee_AddressType sAddr, uint32_t size)
{
    Fls_StateType ret = FLS_STATE_NOT_WRITABLE;

    if(FLASHDRV_STATUS_SUCCESS == FlashDrv_Read1sSection(&mcu.flashDrv, (uint32_t *)sAddr, size))
    {
        ret = FLS_STATE_OK;
    }
    return ret;
}

Fls_StateType Fee_InternalWriteCheck(Fee_AddressType sAddr, uint8_t *buff, uint32_t size)
{
    Fls_StateType ret = FLS_STATE_UNKNOW;
    int idx;
    for(idx = 0; idx < (size / FEE_INTERNAL_WRITE_PHASE_LEN); idx++)
    {
        if(FLASHDRV_STATUS_SUCCESS == FlashDrv_Read1sSection(&mcu.flashDrv, (uint32_t *)(sAddr + (idx * FEE_INTERNAL_WRITE_PHASE_LEN)), FEE_INTERNAL_WRITE_PHASE_LEN))
        {
            if(FLASHDRV_STATUS_SUCCESS == FlashDrv_ProgramPhrase(&mcu.flashDrv, (uint32_t *)(sAddr + (idx * FEE_INTERNAL_WRITE_PHASE_LEN)), buff + (idx * FEE_INTERNAL_WRITE_PHASE_LEN), true))
            {
                if(FLASHDRV_STATUS_SUCCESS == FlashDrv_CheckPhrase(&mcu.flashDrv, (uint32_t *)(sAddr + (idx * FEE_INTERNAL_WRITE_PHASE_LEN)), buff + (idx * FEE_INTERNAL_WRITE_PHASE_LEN)))
                {
                    ret = FLS_STATE_OK;
                }
                else
                {
                    ret = FLS_STATE_READ_BACK_FAIL;
                    Fee_InternalErrors = FEE_INTERNAL_ERROR_WRITE_PHASE_READBACK_ERROR;
                    break;
                }
            }
            else
            {
                ret = FLS_STATE_PROGRAM_FAIL;
                Fee_InternalErrors = FEE_INTERNAL_ERROR_WRITE_PHASE_ERROR;
                break;
            }
        }
        else
        {
            ret = FLS_STATE_NOT_WRITABLE;
            Fee_InternalErrors = FEE_INTERNAL_ERROR_WRITE_PHASE_UNEARSE;
            break;
        }
    }
    return ret;
}

Fls_StateType Fee_InternalReadCheckData(Fee_AddressType sAddr, uint8_t *buff, uint32_t size)
{
    Fls_StateType ret = FLS_STATE_UNKNOW;
    int  cnt;

    if(FLASHDRV_STATUS_SUCCESS != FlashDrv_Read1sSection(&mcu.flashDrv, (uint32_t *)sAddr, size))
    {
        if(FLASHDRV_STATUS_SUCCESS == FlashDrv_ReadData(&mcu.flashDrv, (uint8_t *)sAddr, buff, size))
        {
            for(cnt = 0; cnt < (size / 8); cnt++)
            {
                if(FLASHDRV_STATUS_SUCCESS == FlashDrv_CheckPhrase(&mcu.flashDrv, (uint32_t *)(sAddr + cnt * 8), &buff[cnt * 8]))
                {
                    ret = FLS_STATE_OK;
                }
                else
                {
                    ret = FLS_STATE_READ_BACK_FAIL;
                    Fee_InternalErrors = FEE_INTERNAL_ERROR_READ_READBACK_ERROR;
                    break;
                }
            }
        }
        else
        {
            ret = FLS_STATE_ECC_ERROR;
            Fee_InternalErrors = FEE_INTERNAL_ERROR_READ_ERROR;
        }
    }
    else
    {
        ret = FLS_STATE_NOT_WRITTEN;
        Fee_InternalErrors = FEE_INTERNAL_ERROR_READ_UNWRITE_ERROR;
    }
    return ret;
}

Fls_StateType Fee_InternalEarseCheckPhrase(Fee_AddressType earseAddr)
{
    Fls_StateType ret = FLS_STATE_UNKNOW;

    if(FLASHDRV_STATUS_SUCCESS != FlashDrv_Read1sSection(&mcu.flashDrv, (uint32_t *)earseAddr, FLASHDRV_DFLASH_SECTOR_SIZE))
    {
        if(FLASHDRV_STATUS_SUCCESS == FlashDrv_EraseSector(&mcu.flashDrv, (uint32_t *)earseAddr, FLASHDRV_SCTERS_RETRY))
        {
            if(FLASHDRV_STATUS_SUCCESS == FlashDrv_Read1sSection(&mcu.flashDrv, (uint32_t *)earseAddr, FLASHDRV_DFLASH_SECTOR_SIZE))
            {
                ret = FLS_STATE_OK;
            }
            else
            {
                ret = FLS_STATE_ERASE_FAIL;
            }
        }
        else
        {
            ret = FLS_STATE_ERASE_FAIL;
        }
    }
    else
    {
        ret = FLS_STATE_OK;
    }
    return ret;
}

uint16_t Fee_InternalCalcCrc16(void *sAddr, uint32_t size)
{
    uint8_t   crcStartValue16 = 0;
    uint16_t *dataPtr = (uint16_t *)sAddr;

    while(size != 0U)
    {
        /* Process one byte of data */
        crcStartValue16 = Fee_CrcTable16[((uint8_t)(crcStartValue16 >> 8U)) ^ *dataPtr] ^ ((uint16_t)(crcStartValue16 << 8U));
        ++dataPtr;
        --size;
    }
    return crcStartValue16;
}

uint8_t Fee_InternalCalcCrc8(void *sAddr, uint32_t size)
{
    uint8_t  crcStartValue8 = 0;
    uint8_t *dataPtr = (uint8_t *)sAddr;

    crcStartValue8 = 0xFFU;

    while(size != 0U)
    {
        crcStartValue8 = Fee_CrcTable8[crcStartValue8 ^ *dataPtr];
        ++dataPtr;
        --size;
    }

    crcStartValue8 ^= 0xFFU; /* XOR crc value */
    return crcStartValue8;
}

static FlashDrv_Status FlashDrv_ReadData(FlashDrvType *obj, uint8_t *srcaddr, uint8_t *destaddr, uint32_t rdsize)
{
    int i;
    FlashDrv_Status ret = FLASHDRV_STATUS_ERROR;

    for(i = 0; i < (rdsize / 8); i++)
    {
        if (FLASHDRV_STATUS_SUCCESS != FlashDrv_CheckEccState(obj, (uint32_t *)(srcaddr + i * 8)))
        {
            return ret;
        }
    }

    for(i = 0; i < rdsize; i++)
    {
        destaddr[i] = srcaddr[i];
    }

    return FLASHDRV_STATUS_SUCCESS;
}

