//***********************************************************************************************************
//  Ver 1.0
//      2022.04.02  1、新建文件。
//***********************************************************************************************************
//#include    "gd32l23x_config.h"
//#include    "bat32g135_config.h"
//#include    "acm32f4_config.h"
#include    "stm32f10x_config.h"
//#include    "mm32f016x_config.h"
#include    "string.h"

#define	__CVD__
#define SVS_STACK_SIZE	  0x200
const  int iSVS_STACK_SIZE = SVS_STACK_SIZE;
static char SvsStack[SVS_STACK_SIZE] __attribute__((used)) ;
//参数块定义
#define	BUFFER_SIZE	4096
#ifdef __CVD__
typedef struct  {
	unsigned int	status;	//Command;
	unsigned int	Result;
	unsigned int	Reserved1[4];
	unsigned int	Reserved2;
	unsigned int	addr;	//TargetAddr;
	unsigned int	size;	//Length;
	unsigned int	Reserved3;
	unsigned int	Reserved4;
	unsigned int	Reserved5;
	unsigned int	Reserved6;
	unsigned int	Reserved7;
	unsigned int	Reserved8;
	unsigned char 	data[BUFFER_SIZE];
} FlashParameter_Str;

 FlashParameter_Str FlashParameter;// __attribute__((at(0x20000ce4)));

enum{
    FLASH_PROGRAM       = 0x00,
    FLASH_CHIP_ERASE    = 0x01,	
    FLASH_ERASE_PAGE    = 0x02,
    FLASH_CHIP_ID		= 0x03,
};

#else
typedef struct{
    unsigned int  flashaddr;  //0x800,芯片起始地址
    unsigned int  flashwidth; //0x804,1=8bit,2=16bit,4=32bit
    unsigned int  width;      //0x808，数据总线位宽
    unsigned int  offset;     //0x80c，
    unsigned int  addr;       //0x810，FLASH操作地址
    unsigned int  size;       //0x814，擦除、写入字节数
    unsigned int  Result;	  //reserved;   //0x818，
    unsigned int  status;     //0x81c,in: 1=program, 2=erase, 5=chip erase, out: 0=o.k., 0x100=fail
    unsigned char data[BUFFER_SIZE]; //0x820，缓冲区起始地址
}FlashParameter_Str;

static FlashParameter_Str FlashParameter __attribute__((at(0x20000800)));
//static FlashParameter_Str FlashParameter __attribute__((section(".ARM.__at_0x20000800")));

enum{
    FLASH_PROGRAM       = 0x01,
    FLASH_ERASE_PAGE    = 0x02,
    FLASH_CHIP_ERASE    = 0x05,
    FLASH_CHIP_ID		= 0x00,
};
#endif

 

#pragma arm section code = ".ARM.__at_0x20000100"

#define _DEBUG 1

#if(_DEBUG)
void _flash_program(void)
{ 
    FlashParameter.status = 1;                  
}

void _flash_erasepage(void)
{ 
    FlashParameter.status = 1;                   
}

void _flash_erasechip(void)
{ 
    FlashParameter.status = 1;                   
}

void _flash_readchip(void)
{  
    FlashParameter.status = 1;     
}

#else
void _flash_program(void)
{
    flash_program();
}

void _flash_erasepage(void)
{
    flash_erasepage();
}

void _flash_erasechip(void)
{
    flash_erasechip();
}

void _flash_readchip(void)
{
    flash_readchip();
}

#endif



unsigned int errCnt = 6000;
unsigned int tMs; 
int main(void)
{  
    system_init();
    while(1)
    {
        switch(FlashParameter.status)
        {
            case FLASH_PROGRAM:
                _flash_program();                 
                return(0);
                //break;
                    
            case FLASH_ERASE_PAGE:
                _flash_erasepage();
                return(0);
                //break; 
                    
            case FLASH_CHIP_ERASE:
                _flash_erasechip();               
                return(0);
                //break; 
                    
            case FLASH_CHIP_ID:
                _flash_readchip();               
                return(0);
                //break; 
                
            default:
                tMs = 65535;
                while(tMs--);
                FlashParameter.status = 0; 
                break;
        }
        if(errCnt > 0) 
            errCnt--;
        else           
        {
            memset(&FlashParameter,0,sizeof(FlashParameter));
            return(1);
        }
    }    
    return(0);
}

#pragma arm section
//#pragma clang section
