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


#define uint32_t unsigned int

#define	__CVD__
#define SVS_STACK_SIZE	  0x200
unsigned char SvsStack[SVS_STACK_SIZE];
//参数块定义
#define	BUFFER_SIZE	4096
#ifdef __CVD__
typedef struct  {
	unsigned int	Command;
	unsigned int	Result;
	unsigned int	Reserved1[4];
	unsigned int	Reserved2;
	unsigned int	TargetAddr;
	unsigned int	Length;
	unsigned int	Reserved3;
	unsigned int	Reserved4;
	unsigned int	Reserved5;
	unsigned int	Reserved6;
	unsigned int	Reserved7;
	unsigned int	Reserved8;

	unsigned char data[BUFFER_SIZE];
} FLASH_INFO;

static FLASH_INFO Flash_Info __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  	TargetAddr; //0x810，FLASH操作地址
    unsigned int  	Length;     //0x814，擦除、写入字节数
    unsigned int  	Reserved;   //0x818，
    unsigned int    Command;     //0x81c,in: 1=program, 2=erase, 5=chip erase, out: 0=o.k., 0x100=fail
    unsigned char	data[BUFFER_SIZE]; //0x820，缓冲区起始地址
}FLASH_INFO;

static FLASH_INFO Flash_Info __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_0x20000000"
//#pragma clang section text = ".ARM.__at_0x20000000"   //AC6


int main1 (void)
{     
    unsigned int errCnt = 65535;
    unsigned int tMs;
    
    system_init();
    while(1)
    {
        
        switch(Flash_Info.Command)
        {
            case FLASH_PROGRAM:
            case FLASH_ERASE_PAGE:
            case FLASH_CHIP_ERASE:
            case FLASH_CHIP_ID:     
                return(0);
                break;
            default:     
                return(1);
                break;
        }
        /*
        switch(Flash_Info.Command)
        {
            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--);
				Flash_Info.Result = 0;
                break;
        }
        */
        if(errCnt > 0) errCnt--;
        else           return(1);
    }
}

static inline void __set_MSP(uint32_t topOfMainStack)
{
  register uint32_t __regMainStackPointer     __asm("msp");
  __regMainStackPointer = topOfMainStack;
}


int main(void)
{        
    __set_MSP (0x20002000); 
    main1();
}
#pragma arm section
//#pragma clang section
