
#include "myflash.h"
// #include "includes.h"

//flash擦除
void Flash_RangeErase(uint32_t FlashAddress, uint32_t Size)
{
    FLASH_Unlock();
    FLASH_Erase(FlashAddress, Size);
    FLASH_Lock();
}

//flash写指定长度数据
void Flash_BufferWrite(uint32_t address, uint32_t *p_buffer, uint32_t len)
{
	uint32_t EndAddr = 0;

	if(address%4||len%4) {
		return ;
	}
    
    FLASH_Unlock();
    FLASH_FastProgram(address, (uint32_t)p_buffer, len);

    // EndAddr = address + len;
    // FLASH_EnableProgram();
    // while (address < EndAddr) {
	// 	//FLASH_ProgramWord(address, *p_buffer);
    //     *(volatile uint32_t *)address = *p_buffer;
	// 	p_buffer++;
	// 	address += 4;
	// }
    // FLASH_DisableProgram();
    FLASH_Lock();
}

//flash读指定长度的数据
void Flash_BufferRead(uint32_t address, uint32_t *buffer, uint32_t len)
{
    uint32_t EndAddr = 0;
	uint32_t *p_buffer = (uint32_t *)buffer;
    if(address%4||len%4) {
		return ;
	}
    EndAddr = address + len;
    FLASH_Unlock();
    while (address < EndAddr) {
		*p_buffer++ = RD_REG(address);
		address += 4;
	}
    FLASH_Lock();
}

//flash读指定长度的数据
void Flash_BufferRead_u8(uint32_t address, uint8_t *buffer, uint32_t len)
{
    uint32_t EndAddr = 0;
	// uint32_t *p_buffer = (uint32_t *)buffer;
    uint32_t data,i=0;
    if(address%4||len%4) {
		return ;
	}
    EndAddr = address + len;
    FLASH_Unlock();
    while (address < EndAddr) {
		data = RD_REG(address);
        buffer[i] = data&0xff;
        buffer[i+1] = (data>>8)&0xff;
        buffer[i+2] = (data>>16)&0xff;
        buffer[i+3] = (data>>24)&0xff;
        i+=4;
		address += 4;
	}
    FLASH_Lock();
}

//读取flash中有效数据的长度
uint32_t Flash_DatalenRead(uint32_t address,uint32_t len)
{
    uint32_t BeginAddr = 0;
	uint32_t addrlen = 0;
	uint32_t data;
	if(address%4||len%4) {
		return addrlen;
	}
    BeginAddr = address + len - 4;
    FLASH_Unlock();
    while(address < BeginAddr)
	{
		data = RD_REG(BeginAddr);
		if(data != 0xFFFFFFFF)
			break;
		BeginAddr -= 4;
	}
    FLASH_Lock();

    if(address <= BeginAddr)
	{
		addrlen = (BeginAddr+4) - address;
	}
	return addrlen;
}

/*
#define FLASH_TEST_ADDR 0x80010000
#define FLASH_TEST_LEN  2048 //0x800
//flash测试
void flash_test(void)
{
    static uint32_t flash_buf[FLASH_TEST_LEN/4],flash_len;
    static uint32_t flash_read_buf[FLASH_TEST_LEN/4];
    uint32_t i,num=0;
    uint32_t crc;

    // num = Flash_DatalenRead(FLASH_TEST_ADDR,FLASH_TEST_LEN);

    Flash_BufferRead(FLASH_TEST_ADDR,flash_buf,FLASH_TEST_LEN);
    data_dump("flash read",(uint8_t *)flash_buf,FLASH_TEST_LEN);
    for(i = 0; i < FLASH_TEST_LEN/4; i++)
    {
        flash_buf[i] = 0x12345678+i;
    }
    data_dump("flash write",(uint8_t *)flash_buf,FLASH_TEST_LEN);
    Flash_RangeErase(FLASH_TEST_ADDR,FLASH_TEST_LEN);
    Flash_BufferWrite(FLASH_TEST_ADDR,flash_buf,FLASH_TEST_LEN);

    //计算crc32
    SYS_EnableAHBClock(AHB_MASK_CRC0);
    CRC_Reset(CRC0);
    crc = crc_block_data_calculate((uint32_t *)FLASH_TEST_ADDR,FLASH_TEST_LEN/4);
    printf("crc = %08x\n",crc);

    // memset(flash_read_buf,0,FLASH_TEST_LEN);
    // Flash_BufferRead(FLASH_TEST_ADDR,flash_read_buf,FLASH_TEST_LEN);
    // data_dump("flash read",(uint8_t *)flash_read_buf,FLASH_TEST_LEN);

    // for(i = 0; i < FLASH_TEST_LEN/4-1; i++)
    // {
    //     if(flash_buf[i] != flash_read_buf[i])
    //         num++;
    // }
    // printf("error num = %u\n",num);

}
*/
