#include "Head.h"
//-----------------------------------------------------------------------------
// FLASH_ByteWrite
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters :
// 1) FLADDR addr - address of the byte to write to
// valid range is 0x0000 to 0xFBFF for 64K Flash devices
// valid range is 0x0000 to 0x7FFF for 32K Flash devices
// 2) U8 byte - byte to write to Flash.
//
// This routine writes <byte> to the linear FLASH address <addr>.
//-----------------------------------------------------------------------------
U8 Flash_data[30];
void FLASH_ByteWrite (FLADDR addr, U8 byte)
{
bit EA_SAVE = EA; // Preserve EA
U8 xdata * data pwrite; // FLASH write pointer
U8 SFRPAGE_save = SFRPAGE;
SFRPAGE = ACTIVE_PAGE;
EA = 0; // Disable interrupts
VDM0CN = 0xA0; // Enable VDD monitor and high threshold
RSTSRC = 0x02; // Enable VDD monitor as a reset source
pwrite = (U8 xdata *) addr;
FLKEY = 0xA5; // Key Sequence 1
FLKEY = 0xF1; // Key Sequence 2
PSCTL |= 0x01; // PSWE = 1 which enables writes
VDM0CN = 0xA0; // Enable VDD monitor and high threshold
RSTSRC = 0x02; // Enable VDD monitor as a reset source
*pwrite = byte; // Write the byte
PSCTL &= ~0x01; // PSWE = 0 which disable writes
EA = EA_SAVE; // Restore interrupts
SFRPAGE = SFRPAGE_save;
}
//-----------------------------------------------------------------------------
// FLASH_ByteRead
//-----------------------------------------------------------------------------
//
// Return Value :
// U8 - byte read from Flash
// Parameters :
// 1) FLADDR addr - address of the byte to read to
// valid range is 0x0000 to 0xFBFF for 64K Flash devices
// valid range is 0x0000 to 0x7FFF for 32K Flash devices
//
// This routine reads a <byte> from the linear FLASH address <addr>.
//-----------------------------------------------------------------------------
U8 FLASH_ByteRead (FLADDR addr)
{
bit EA_SAVE = EA; // Preserve EA
U8 code * data pread; // FLASH read pointer
U8 byte;
EA = 0; // Disable interrupts
pread = (U8 code *) addr;
byte = *pread; // Read the byte
EA = EA_SAVE; // Restore interrupts
return byte;
}
//-----------------------------------------------------------------------------
// FLASH_PageErase
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters :
// 1) FLADDR addr - address of any byte in the page to erase
// valid range is 0x0000 to 0xF9FF for 64K Flash devices
// valid range is 0x0000 to 0x7DFF for 32K Flash devices
//
// This routine erases the FLASH page containing the linear FLASH address
// <addr>. Note that the page of Flash containing the Lock Byte cannot be
// erased from application code.
//-----------------------------------------------------------------------------
void FLASH_PageErase (FLADDR addr)
{
bit EA_SAVE = EA; // Preserve EA
U8 xdata * data pwrite; // FLASH write pointer
U8 SFRPAGE_save = SFRPAGE;
SFRPAGE = ACTIVE_PAGE;
EA = 0; // Disable interrupts
VDM0CN = 0xA0; // Enable VDD monitor and high threshold
RSTSRC = 0x02; // Enable VDD monitor as a reset source
pwrite = (U8 xdata *) addr;
FLKEY = 0xA5; // Key Sequence 1
FLKEY = 0xF1; // Key Sequence 2
PSCTL |= 0x03; // PSWE = 1; PSEE = 1
VDM0CN = 0xA0; // Enable VDD monitor and high threshold
RSTSRC = 0x02; // Enable VDD monitor as a reset source
*pwrite = 0; // Initiate page erase
PSCTL &= ~0x03; // PSWE = 0; PSEE = 0
EA = EA_SAVE; // Restore interrupts
SFRPAGE = SFRPAGE_save;
}
void FLASH_ReadData(U8 *buffer,U8 length)
{
U8 i;
for(i = 0; i < length ; i++){
buffer[i] = FLASH_ByteRead (start_address + i);
}
}
void FLASH_WriteData(U8 *buffer,U8 length)
{
U8 i;
for(i = 0; i < length; i ++){
FLASH_PageErase(start_address + i);
}
for(i = 0; i < length; i ++){
FLASH_ByteWrite (start_address + i, buffer[i]);
}
}
void Flash_data_init()
{
U8 i;
for(i = 0; i < 30; i++){
Flash_data[i] = 0x00;
}
}
相关文章
- 《DFZU2EG_4EV MPSoC之嵌入式Vitis开发指南》第十三章 QSPI Flash读写测试实验
- C8051F Flash 写和读操作
- Android_内部存储文件的读写
- 友善之臂Mini2440之嵌入式Linux下应用程序对Nand Flash的读写操作
- 基于FPGA的SD卡的数据读写实现(SD NAND FLASH)
- Serial Flash Loader读写Flash存储芯片
- SPI操作flash MX25L64读写数据
- Android文件读写操作(assets 文件、 raw文件、内部存储文件、外部存储文件)
- STM32:Flash擦除与读写操作(HAL库)
- S3C2440读写NAND FLASH(大页)