📄 at45db041.c
字号:
/***************************************************************************/
#include "config.h"
/***************************************************************************/
uint8 Read_Flash_Page(uint8 *DestAddr, uint16 PageIndex)
{
uint8 buf = 0x00, addr[8];
uint16 i;
addr[0] = 0x52;
addr[1] = (uint8)(PageIndex >> 0x07);
addr[2] = (uint8)(PageIndex << 0x01);
addr[3] = 0x00;
addr[4] = 0x00;
addr[5] = 0x00;
addr[6] = 0x00;
addr[7] = 0x00;
IO0CLR |= SAVECS; /* 设置P0.29 CS 输出为0*/
if (SPIRW(&buf, addr[0]) != TRUE){
IO0SET |= SAVECS; /* 设置P0.29 CS 输出为1*/
return ERROR;
}
if (SPIRW(&buf, addr[1]) != TRUE){
IO0SET |= SAVECS; /* 设置P0.29 CS 输出为1*/
return ERROR;
}
if (SPIRW(&buf, addr[2]) != TRUE){
IO0SET |= SAVECS; /* 设置P0.29 CS 输出为1*/
return ERROR;
}
if (SPIRW(&buf, addr[3]) != TRUE){
IO0SET |= SAVECS; /* 设置P0.29 CS 输出为1*/
return ERROR;
}
if (SPIRW(&buf, addr[4]) != TRUE){
IO0SET |= SAVECS; /* 设置P0.29 CS 输出为1*/
return ERROR;
}
if (SPIRW(&buf, addr[5]) != TRUE){
IO0SET |= SAVECS; /* 设置P0.29 CS 输出为1*/
return ERROR;
}
if (SPIRW(&buf, addr[6]) != TRUE){
IO0SET |= SAVECS; /* 设置P0.29 CS 输出为1*/
return ERROR;
}
if (SPIRW(&buf, addr[7]) != TRUE){
IO0SET |= SAVECS; /* 设置P0.29 CS 输出为1*/
return ERROR;
}
for (i = 0x00; i < 264; i++)
if (SPIRW(DestAddr++, buf) != TRUE){
IO0SET |= SAVECS; /* 设置P0.29 CS 输出为1*/
return ERROR;
}
IO0SET |= (1 << 29); /* 设置P0.29 CS 输出为1*/
return SUCCESS;
}
/***************************************************************************/
uint8 Write_Flash_Page(uint8 *SrcAddr, uint16 PageIndex)
{
uint8 buf, addr1, addr2, addr3;
uint16 i;
addr1 = (uint8)(PageIndex >> 0x07);
addr2 = (uint8)(PageIndex << 0x01);
addr3 = 0x00;
IOCLR = 0x80000000; /* 设置P0.31输出为0*/
IO0CLR |= SAVECS; /* 设置P0.29 CS 输出为0*/
if (SPIRW(&buf, 0x84) != TRUE){
IO0SET |= SAVECS; /* 设置P0.29 CS 输出为1*/
return ERROR;
}
if (SPIRW(&buf, 0x00) != TRUE){
IO0SET |= SAVECS; /* 设置P0.29 CS 输出为1*/
return ERROR;
}
if (SPIRW(&buf, 0x00) != TRUE){
IO0SET |= SAVECS; /* 设置P0.29 CS 输出为1*/
return ERROR;
}
if (SPIRW(&buf, 0x00) != TRUE){
IO0SET |= SAVECS; /* 设置P0.29 CS 输出为1*/
return ERROR;
}
for (i = 0x00; i < 264; i++)
if (SPIRW(&buf, *SrcAddr++) != TRUE){
IO0SET |= SAVECS; /* 设置P0.29 CS 输出为1*/
return ERROR;
}
IO0SET |= SAVECS; /* 设置P0.29 CS 输出为1*/
IOSET = 0x80000000; /* 设置P0.31输出为1*/
for (i = 0x00; i < 264; i++);
IO0CLR |= SAVECS; /* 设置P0.29 CS 输出为0*/
if (SPIRW(&buf, 0x83) != TRUE){
IO0SET |= SAVECS; /* 设置P0.29 CS 输出为1*/
return ERROR;
}
if (SPIRW(&buf, addr1) != TRUE){
IO0SET |= SAVECS; /* 设置P0.29 CS 输出为1*/
return ERROR;
}
if (SPIRW(&buf, addr2) != TRUE){
IO0SET |= SAVECS; /* 设置P0.29 CS 输出为1*/
return ERROR;
}
if (SPIRW(&buf, addr3) != TRUE){
IO0SET |= SAVECS; /* 设置P0.29 CS 输出为1*/
return ERROR;
}
IO0SET |= SAVECS; /* 设置P0.29 CS 输出为1*/
return SUCCESS;
}
/***************************************************************************/
static uint8 Write_Flash_Offset(uint8 *SrcAddr, uint16 PageIndex, uint16 PageAddr, uint16 Size)
{
uint8 buf[264];
uint16 i;
if (Read_Flash_Page(buf, PageIndex) != SUCCESS)
return ERROR;
for(i =0x00; i < Size; i++)
buf[PageAddr + i] = *SrcAddr++;
OSTimeDly (OS_TICKS_PER_SEC / 100); //延时10毫秒
if (Write_Flash_Page(buf, PageIndex) != SUCCESS)
return ERROR;
OSTimeDly (OS_TICKS_PER_SEC / 10); //延时10毫秒
return SUCCESS;
}
/***************************************************************************/
uint8 ReadFlash(uint8 *DestAddr, uint32 ChipAddr, uint16 Size)
{
uint8 buf[264];
uint16 offset, i, j, page_num, page_offset;
uint16 page_index, page_addr;
page_index = ChipAddr / 264;
page_addr = ChipAddr % 264;
if (page_addr != 0x00){ // 起始地址在页中间
offset = 264 - page_addr; // 本页还可读多少字节
if (Size <= offset){
if (Read_Flash_Page(buf, page_index) != SUCCESS)
return ERROR;
for(i =0x00; i < Size; i++)
*DestAddr++ = buf[page_addr + i];
return SUCCESS;
}//if (Size <= offset)
page_num = (Size - offset) / 264;
page_offset = (Size - offset) % 264;
if (Read_Flash_Page(buf, page_index) != SUCCESS)
return ERROR;
for(i =0x00; i < offset; i++)
*DestAddr++ = buf[page_addr + i];
page_index += 1;
}//if (page_addr != 0x00)
else
{
page_num = Size / 264;
page_offset = Size % 264;
}
for(j =0x00; j < page_num; j++){
if (Read_Flash_Page(buf, (page_index + j)) != SUCCESS)
return ERROR;
for(i =0x00; i < 264; i++)
*DestAddr++ = buf[i];
OSTimeDly (OS_TICKS_PER_SEC / 100); //延时10毫秒
}
if (Read_Flash_Page(buf, (page_index + page_num)) != SUCCESS)
return ERROR;
for(i =0x00; i < page_offset; i++)
*DestAddr++ = buf[i];
return SUCCESS;
}
/***************************************************************************/
uint8 WriteFlash(uint8 *SrcAddr, uint32 ChipAddr, uint16 Size)
{
uint8 buf[264];
uint16 offset, i, j, page_num, page_offset;
uint16 page_index, page_addr;
page_index = ChipAddr / 264;
page_addr = ChipAddr % 264;
if (page_addr != 0x00){ // 起始地址在页中间
offset = 264 - page_addr; // 本页还可写多少字节
if (Size <= offset){
if (Write_Flash_Offset(SrcAddr, page_index, page_addr, Size) == SUCCESS)
return SUCCESS;
else return ERROR;
}//if (Size <= offset)
page_num = (Size - offset) / 264;
page_offset = (Size - offset) % 264;
if (Write_Flash_Offset(SrcAddr, page_index, page_addr, offset) != SUCCESS)
return ERROR;
SrcAddr += offset;
page_index += 1;
}//if (page_addr != 0x00)
else
{
page_num = Size / 264;
page_offset = Size % 264;
}
for(j =0x00; j < page_num; j++){
for(i =0x00; i < 264; i++)
buf[i] = *SrcAddr++;
if (Write_Flash_Page(buf, (page_index + j)) != SUCCESS)
return ERROR;
OSTimeDly (OS_TICKS_PER_SEC / 10); //延时100毫秒
}
if (Write_Flash_Offset(SrcAddr, (page_index + page_num), 0, page_offset) != SUCCESS)
return ERROR;
return SUCCESS;
}
/***************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -