/*******************************************************************************
* Project:      Amped'RF Bluetooth
* File name:    GKI_flash.c
* Description:  Flash abstraction layer
*
* Copyright (c) 2013 Amped'RF Inc., AR Technology  All rights reserved.
*******************************************************************************/
#include "SDK_Common.h"
#include "GKI_lib.h"




/*******************************************************************************
* Function: GKI_flash_ErrorText
*
* Description:
*   Return error text from status
*
*******************************************************************************/
char *GKI_flash_ErrorText(tGKI_FlashStatus status)
{
#if defined(STM32F10x)
    switch (status) {
    case GKI_FLASH_BUSY         : return "FLASH_BUSY";
    case GKI_FLASH_ERROR_PG     : return "FLASH_ERROR_PG";
    case GKI_FLASH_ERROR_WRP    : return "FLASH_ERROR_WRP";
    case GKI_FLASH_COMPLETE     : return "FLASH_COMPLETE";
    case GKI_FLASH_TIMEOUT      : return "FLASH_TIMEOUT";
    default                     : return "FLASH_STATUS_INVALID";
    }

#elif defined(STM32F2xx)
    switch (status) {
    case GKI_FLASH_BUSY             : return "FLASH_BUSY";
    case GKI_FLASH_ERROR_PGS        : return "FLASH_ERROR_PGS";
    case GKI_FLASH_ERROR_PGP        : return "FLASH_ERROR_PGP";
    case GKI_FLASH_ERROR_PGA        : return "FLASH_ERROR_PGA";
    case GKI_FLASH_ERROR_WRP        : return "FLASH_ERROR_WRP";
    case GKI_FLASH_ERROR_PROGRAM    : return "FLASH_ERROR_PROGRAM";
    case GKI_FLASH_ERROR_OPERATION  : return "FLASH_ERROR_OPERATION";
    case GKI_FLASH_COMPLETE         : return "FLASH_COMPLETE";
    default                         : return "FLASH_STATUS_INVALID";
    }

#elif defined(STM32F4xx)
    switch (status) {
    case GKI_FLASH_BUSY             : return "FLASH_BUSY";
    case GKI_FLASH_ERROR_RD         : return "FLASH_ERROR_RD";
    case GKI_FLASH_ERROR_PGS        : return "FLASH_ERROR_PGS";
    case GKI_FLASH_ERROR_PGP        : return "FLASH_ERROR_PGP";
    case GKI_FLASH_ERROR_PGA        : return "FLASH_ERROR_PGA";
    case GKI_FLASH_ERROR_WRP        : return "FLASH_ERROR_WRP";
    case GKI_FLASH_ERROR_PROGRAM    : return "FLASH_ERROR_PROGRAM";
    case GKI_FLASH_ERROR_OPERATION  : return "FLASH_ERROR_OPERATION";
    case GKI_FLASH_COMPLETE         : return "FLASH_COMPLETE";
    default                         : return "FLASH_STATUS_INVALID";
    }
#endif
}



tGKI_FlashStatus GKI_flash_ProgramWord(u32 Address, u32 Data)
{
    return FLASH_ProgramWord(Address, Data);
}



tGKI_FlashStatus GKI_flash_ErasePage(u32 Address)
{
#if defined(STM32F10x)
    return FLASH_ErasePage(Address);
#elif defined(STM32F2xx) || defined(STM32F4xx)
    u16 Sector;
    switch (Address) {
    case 0x08000000:    Sector = FLASH_Sector_0;    break;
    case 0x08004000:    Sector = FLASH_Sector_1;    break;
    case 0x08008000:    Sector = FLASH_Sector_2;    break;
    case 0x0800C000:    Sector = FLASH_Sector_3;    break;
    case 0x08010000:    Sector = FLASH_Sector_4;    break;
    case 0x08020000:    Sector = FLASH_Sector_5;    break;
    case 0x08040000:    Sector = FLASH_Sector_6;    break;
    default:
        return FLASH_ERROR_OPERATION;
    }
    return FLASH_EraseSector(Sector, VoltageRange_1);
#endif
}



void GKI_flash_Unlock(void)
{
    FLASH_ClearFlag(
            FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR|
            FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
    FLASH_Unlock();
}


void GKI_flash_Lock(void)
{
    FLASH_Lock();
}


void GKI_wdg_PeriodValueConfig(u32 res)
{
}


void GKI_wdg_CntRefresh(void)
{
}


void GKI_uart_SetCharTimeout(u16 u, u32 tm)
{
}



//--------------------------------------------------------------
//