📄 at45db642d.c
字号:
status = ext_flash_getByte(); // Get the status
FLASH_SELECT=1; // Disable select line
return status; // Return the status
}
// Purpose: Write data to a buffer
// Inputs: 1) A buffer number (0 or 1)
// 2) An index into the buffer
// 3) A pointer to the data to write
// 4) The number of bytes of data to write
// Outputs: None
// Dependencies: ext_flash_sendData(), ext_flash_waitUntilReady(), ext_flash_sendBytes()
void ext_flash_writeToBuffer(unsigned char bufferNumber, int16 bufferAddress, BYTE* data, int16 size)
{
BYTE opcode;
FLASH_SELECT=0; // Enable select line
if(bufferNumber)
opcode = 0x87; // Opcode for second buffer
else
opcode = 0x84; // Opcode for first buffer
ext_flash_sendData(opcode, 8); // Send opcode
ext_flash_sendData(0, 13); // Send 13 don't care bits
ext_flash_sendData(bufferAddress, 11); // Send buffer address
ext_flash_sendBytes(data, size); // Write data to the buffer
FLASH_SELECT=1; // Disable select line
}
// Purpose: Write the data in a buffer to a page
// Inputs: 1) A page address
// 2) A buffer number (0 or 1)
// 3) The writing mode to use
// - Use ERASE to first erase a page then write
// - Use NO_ERASE to write to a previously erased page
// Outputs: None
// Dependencies: ext_flash_sendData(), ext_flash_waitUntilReady()
void ext_flash_BufferToPage(unsigned char bufferNumber, int16 pageAddress, unsigned char mode)
{
BYTE opcode;
ext_flash_waitUntilReady(); // Wait until ready
FLASH_SELECT=0; // Enable select line
if(mode)
{
if(bufferNumber)
opcode = 0x86; // Opcode for second buffer
else
opcode = 0x83; // Opcode for first buffer
}
else
{
if(bufferNumber)
opcode = 0x89; // Opcode for second buffer
else
opcode = 0x88; // Opcode for first buffer
}
ext_flash_sendData(opcode, 8); // Send opcode
ext_flash_sendData(pageAddress, 13); // Send page address
ext_flash_sendData(0, 11); // Send 9 don't care bits
FLASH_SELECT=1; // Disable select line
}
// Purpose: Erase a page
// Inputs: A page address
// Outputs: None
// Dependencies: ext_flash_sendData(), ext_flash_waitUntilReady()
void ext_flash_erasePage(int16 pageAddress)
{
ext_flash_waitUntilReady();
FLASH_SELECT=0; // Enable select line
ext_flash_sendData(0x81, 8); // Send opcode
ext_flash_sendData(pageAddress, 13); // Send page address
ext_flash_sendData(0, 11); // Send 11 don't care bits
FLASH_SELECT=1; // Disable select line
}
// Purpose: Erase a block of 8 pages
// Inputs: A block address
// Outputs: None
// Dependencies: ext_flash_sendData(), ext_flash_waitUntilReady()
void ext_flash_eraseBlock(int8 blockAddress)
{
ext_flash_waitUntilReady();
FLASH_SELECT=0; // Enable select line
ext_flash_sendData(0x50, 8); // Send opcode
ext_flash_sendData(blockAddress, 10); // Send block address
ext_flash_sendData(0, 13); // Send 14 don't care bits
FLASH_SELECT=1; // Disable select line
}
// Purpose: Erase the Chip
// Inputs: None
// Outputs: None
// Dependencies: ext_flash_sendData(), ext_flash_waitUntilReady()
void ext_flash_eraseChip()
{
ext_flash_waitUntilReady();
FLASH_SELECT=0; // Enable select line
ext_flash_sendData(0xC7, 8); // Send opcode
ext_flash_sendData(0x94, 8); // Send opcode
ext_flash_sendData(0x80, 8); // Send opcode
ext_flash_sendData(0x9A, 8); // Send opcode
FLASH_SELECT=1; // Disable select line
}
// Purpose: Write data to a page through a buffer
// Inputs: 1) The address of the page to write to
// 2) The number of the buffer to use (0 or 1)
// 3) The index into the buffer to start writing at
// 4) A pointer to the data to write
// 5) The number of bytes of data to write
// Outputs: None
// Dependencies: ext_flash_sendData(), ext_flash_waitUntilReady(), ext_flash_sendBytes()
void ext_flash_writePageThroughBuffer(int16 pageAddress, unsigned char bufferNumber, int16 bufferAddress,
BYTE* data, int16 size)
{
BYTE opcode;
ext_flash_waitUntilReady();
FLASH_SELECT=0; // Enable select line
if(bufferNumber)
opcode = 0x85; // Opcode for second buffer
else
opcode = 0x82; // Opcode for first buffer
ext_flash_sendData(opcode, 8); // Send opcode
ext_flash_sendData(pageAddress, 13); // Send page address
ext_flash_sendData(bufferAddress, 11); // Send buffer address
ext_flash_sendBytes(data, size); // Write data to the buffer
FLASH_SELECT=1; // Disable select line
}
// Purpose: Get the data from a page and put it in a buffer
// Inputs: 1) A page address
// 2) A buffer number (0 or 1)
// Outputs: None
// Dependencies: ext_flash_sendData(), ext_flash_waitUntilReady()
void ext_flash_PageToBuffer(int16 pageAddress, unsigned char bufferNumber)
{
BYTE opcode;
ext_flash_waitUntilReady();
FLASH_SELECT=0; // Enable select line
if(bufferNumber)
opcode = 0x55; // Opcode for second buffer
else
opcode = 0x53; // Opcode for first buffer
ext_flash_sendData(opcode, 8); // Send opcode
ext_flash_sendData(pageAddress, 13); // Send page address
ext_flash_sendData(0, 11); // Send 11 don't care bits
FLASH_SELECT=1; // Disable select line
}
// Purpose: Compare the data in a page to the data in a buffer
// Inputs: 1) A page address
// 2) A buffer number (0 or 1)
// Outputs: 1 if the data is the same, 0 if the data is not the same
// Dependencies: ext_flash_sendData(), ext_flash_waitUntilReady()
unsigned char ext_flash_comparePageToBuffer(int16 pageAddress, unsigned char bufferNumber)
{
unsigned char CompareFlag;
BYTE opcode;
ext_flash_waitUntilReady();
FLASH_SELECT=0; // Enable select line
if(bufferNumber)
opcode = 0x61; // Opcode for second buffer
else
opcode = 0x60; // Opcode for first buffer
ext_flash_sendData(opcode, 8); // Send opcode
ext_flash_sendData(pageAddress, 13); // Send page address
ext_flash_sendData(0, 11); // Send 11 don't care bits
FLASH_SELECT=1; // Disable select line
FLASH_SELECT=0; // Enable select line
ext_flash_sendData(0xD7, 8); // Send status command
while(!FLASH_DO); // Wait until ready
FLASH_CLOCK=1; // Pulse the clock
FLASH_CLOCK=0;
CompareFlag = FLASH_DO; // Get the compare flag
FLASH_SELECT=1; // Disable select line
return CompareFlag;
}
// Purpose: Rewrite the data in a page.
// The flash device does this by transfering the data to a
// buffer, then writing the data back to the page.
// Inputs: 1) A page address
// 2) A buffer number (0 or 1)
// Outputs: None
// Dependencies: ext_flash_sendData(), ext_flash_waitUntilReady()
void ext_flash_rewritePage(int16 pageAddress, int bufferNumber)
{
BYTE opcode;
ext_flash_waitUntilReady();
FLASH_SELECT=1; // Enable select line
if(bufferNumber == 1)
opcode = 0x58;
else
opcode = 0x59;
ext_flash_sendData(opcode, 8); // Send opcode
ext_flash_sendData(pageAddress, 13); // Send page address
ext_flash_sendData(0, 11); // Send 9 don't care bits
FLASH_SELECT=1; // Disable select line
}
// Purpose: Send some data to the flash device
// Inputs: 1) Up to 16 bits of data
// 2) The number of bits to send
// Outputs: None
// Dependencies: None
void ext_flash_sendData(unsigned int data, int8 size)
{
int8 i;
// unsigned char x;
data <<= (16-size);
for(i=0; i<size; ++i)
{
FLASH_DI=((data & (1<<15)) != 0);
data<<=1;
FLASH_CLOCK=1; // Pulse the clock
FLASH_CLOCK=0;
}
}
// Purpose: Send some bytes of data to the flash device
// Inputs: 1) A pointer to an array of data to send
// 2) The number of bytes to send
// Outputs: None
// Dependencies: None
void ext_flash_sendBytes(BYTE* data, int16 size)
{
int16 i;
char j;
for(i=0; i<size; ++i)
{
for(j=7; j>=0; --j)
{
FLASH_DI=BitTest(data[i],j);
// output_bit(FLASH_DI, bit_test(data[i], j)); // Send a data bit
FLASH_CLOCK=1; // Pulse the clock
FLASH_CLOCK=0;
}
}
}
// Purpose: Wait until the flash device is ready to accept commands
// Inputs: None
// Outputs: None
// Dependencies: ext_flash_sendData()
void ext_flash_waitUntilReady()
{
FLASH_SELECT=0; // Enable select line
ext_flash_sendData(0xD7, 8); // Send status command
while(!FLASH_DO); // Wait until ready
FLASH_SELECT=1; // Disable select line
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -