/*******************************************************************************
* the includes
******************************************************************************/
#include "string.h"
#include "MemoryTask.h"
#include "fee.h"
#include "fee_internal.h"
#include "fee_extra.h"

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



/*******************************************************************************
* the typedefs
******************************************************************************/


/*******************************************************************************
* the globals
******************************************************************************/
static FlashDrv_ConfigType flashConfig;
static uint8_t WriteReq,ReadReq,WriteReqBlockID;
extern McuType mcu;
static uint8_t  feeWriteBuffer[100] = {0};
static uint8_t  feeReadBuffer[100] = {0};
static uint8_t *ReadDataPtr;
static uint8_t ReadLen,ReadBlockID;
static FEERWSTATE_t ReadState,WriteState;
/*******************************************************************************
* the const
******************************************************************************/



/*******************************************************************************
* the functions
******************************************************************************/
static void WriteNotification(Fee_NotificationType notificationNumber)
{
    if(FEE_JOBENDNOTIFICATION == notificationNumber)
    {
        WriteState = FEERWSTATE_FINISHED;
    }
    else
    {
        WriteState = FEERWSTATE_ERROR;
    }
}

static void ReadNotification(Fee_NotificationType notificationNumber)
{
    if(FEE_JOBENDNOTIFICATION == notificationNumber)
    {
        ReadState = FEERWSTATE_FINISHED;
        if (ReadDataPtr != NULL)
        {
            memcpy(ReadDataPtr,feeReadBuffer,ReadLen);
        }
    }
    else
    {
        ReadState = FEERWSTATE_ERROR;
    }
    ReadDataPtr = NULL;
    ReadLen = 0;
}




void MemoryTaskInit(void)
{
    /*! \note disable cache,because cache-enable impact the flash memory data checking on debug side */
    /*        use fee must disable cache! */
    *((volatile uint32_t *)0xE0082000u) = 0x0;

    FlashDrv_GetDefaultConfig(&flashConfig);
    FlashDrv_Configure(&mcu.flashDrv, &flashConfig);

    Fee_Configure(&fee);

    WriteReq = 0;
}


void MemoryMainTask(void)
{
    if (FEE_STATUS_IDLE == Fee_GetStatus(&fee))
    {
        //写请求
        if(WriteReq > 0)
        {
            if(FEE_RETURN_OK == Fee_Write(&fee, WriteReqBlockID, feeWriteBuffer, WriteNotification))
            {
                WriteReq = 0;
                WriteState = FEERWSTATE_START;
            }
        }
        //读请求
        if (ReadReq > 0)
        {
            if(FEE_RETURN_OK == Fee_Read(&fee, ReadBlockID, 0, feeReadBuffer, ReadNotification))
            {
                ReadReq = 0;
                ReadState = FEERWSTATE_START;
            }
        }
    }

    Fee_MainFunction(&fee);
}


void MemoryTask_ReqWrite(uint8_t id,uint8_t *dataptr,uint8_t len)
{
    WriteReq = 1;
    WriteReqBlockID = id;
    WriteState = FEERWSTATE_REQ;
    memcpy(feeWriteBuffer,dataptr,len);
}

void MemoryTask_ReqRead(uint8_t id,uint8_t *dataptr,uint8_t len)
{
    ReadReq = 1;
    ReadState = FEERWSTATE_REQ;
    ReadDataPtr = dataptr;
    ReadLen = len;
    ReadBlockID = id;
}

FEERWSTATE_t MemoryTask_getReadState(void)
{
    FEERWSTATE_t ret = ReadState;
    if (ReadState > FEERWSTATE_START)
    {
        ReadState = FEERWSTATE_IDLE;
    }
    return ret;
}

FEERWSTATE_t MemoryTask_getWriteState(void)
{
    FEERWSTATE_t ret = WriteState;
    if (WriteState > FEERWSTATE_START)
    {
        WriteState = FEERWSTATE_IDLE;
    }
    return ret;
}

