/*******************************************************************************
* 51hei版權所有,未經允許,不得發表及作為它用!
* File Name : AT45DB642D.c
* Author : XiaoShm
* Version : V1.0
* Date : 8/29/2012
* Description :
* Last Modefly Date : 8/31/2012
*******************************************************************************/
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "InitDevice.h"
#include"at45db642d.h"
//AT45DB有兩個緩沖區Buffer1和Buffer2 (SRAM),此處只使用buffer1
/******************************
*opcode-操作碼
******************************/
//讀取狀態的操作碼
#define Status_Register_Opcode 0xD7
//讀取ID的操作碼
#define Device_ID_Opcode 0x9F
//Write to buffer
#define Write_Data_to_Buffer1 0x84//0x84,0x87
//Write buffer to page
#define Write_Buffer1_to_Page_whin_Erase 0x83
//Continuous Array Read
#define Continuous_Array_Read_Command 0xe8//0x03//0x0b//0xe8
#define Deep_Power_Down 0xb9
#define Resume_from_Deep_Power_Down 0xab
#define Main_MemoryPage_to_Buffer1_Transfer 0x53
#define Auto_Page_Rewrite_through_Buffer1 0x58
#define Main_Memory_Page_Pro_Through_Buff1 0x82
#define DF_FullSize 0x7fffff//8M byte
#define DF_PageSize 0x400//頁大小
#ifdef TEST_AT45DB
#define TESTBASE 0X2000
u32 TestAddr = TESTBASE;
u8 TestBuffer[256] ={0};
#endif
//======================================================================
//函數名稱:DF_SpiRxByte(void)
//輸入:
//輸出:
//函數功能:通過SPI接從AT45DB讀一字節數據
//======================================================================
u8 DF_SpiRxByte(void)
{
u8 i = 0, rdat = 0;
for(i = 0; i < 8; i ++)
{
rdat <<= 1;
GPIO_SetBits(GPIOC, FSCK);
if(GPIO_ReadInputDataBit(GPIOC, FSDO))
{
rdat |= 0x01;
}
GPIO_ResetBits(GPIOC, FSCK);
}
return rdat;
}
//======================================================================
//函數名稱:DF_SpiTxByte(u8 dat)
//輸入:data待寫入的數據
//輸出:
//函數功能:通過SPI接口向AT45DB寫入一字節數據
//======================================================================
void DF_SpiTxByte(u8 dat)
{
u8 i = 0, sdat = 0;
sdat = dat;
for(i = 0; i < 8; i ++)
{
if(sdat & 0x80)
{
GPIO_SetBits(GPIOC, FSDI);
}
else
{
GPIO_ResetBits(GPIOC, FSDI);
}
GPIO_SetBits(GPIOC, FSCK);
sdat <<= 1;
GPIO_ResetBits(GPIOC, FSCK);
}
}
//======================================================================
//函數名稱:Enable_DFLASH(void)
//輸入:
//輸出:
//函數功能:使能FLASH(低電平使能)
//======================================================================
void Enable_DFLASH(void)
{
u8 i = 5;
GPIO_ResetBits(GPIOC, FCS);
while(i --);
}
//======================================================================
//函數名稱:Disable_DFLASH(void)
//輸入:
//輸出:
//函數功能:禁止FLASH(高電平禁止)
//======================================================================
void Disable_DFLASH(void)
{
u8 i = 5;
GPIO_SetBits(GPIOC, FCS);
while(i --); //延長上一次高電平時間
}
//=========================================================================================
//*Status Register Format: *
//* -----------------------------------------------------------------------
//* bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 *
//* -------- -------- -------- -------- -------- -------- -------- -------- *
//* RDY/BUSY COMP 0 1 1 1 X X *
//* ----------------------------------------------------------------------- *
//* bit7 - 忙標記,0為忙1為不忙。 *
//* 當Status Register的位0移出之后,接下來的時鐘脈沖序列將使SPI器件繼續*
//* 將最新的狀態字節送出。 */
//* bit6 - 標記最近一次Main Memory Page和Buffer的比較結果,0相同,1不同。 *
//* bit5 *
//* bit4 *
//* bit3 *
//* bit2 - 這4位用來標記器件密度,對于AT45DB041B,這4位應該是0111,一共能標記
//* 16種不同密度的器件。
//* bit1
//* bit0 - 這2位暫時無效
//=======================================================================================
u8 DF_ReadStatus(void)
{
u8 i = 0;
Enable_DFLASH();
DF_SpiTxByte(Status_Register_Opcode);
i = DF_SpiRxByte();/////////////////****************//////////////////
Disable_DFLASH();
return i;
}
//======================================================================
//函數名稱:DF_Check_Busy_State(void)
//輸入:
//輸出:
//函數功能:讀取FLASH忙標志位(最多判斷255次,不行還是返回且返回0)
//======================================================================
u8 DF_Check_Busy_State(void)
{
u8 state = 0;
u8 i = 255;
while(i)
{
state = DF_ReadStatus();
if(state & 0x80) break; //讀取的最高位0時器件忙
--i;
DelayNInstruct(100);
}
return i;
}
//************************************************************
//*讀取FLASH的頁大小
//*返回1表示每一頁的大小為1024 bytes,否則為1056 bytes
//************************************************************
u8 DF_Check_Page_Size(void)
{
u8 Page_Size = 0;
Page_Size = DF_ReadStatus();
if(Page_Size & 0x01) return 1;
return 0;
}
//======================================================================
//函數名稱:DF_Deep_Power_Down(void)
//輸入:
//輸出:
//函數功能:使FLASH進入DeepPowerDown模式
//======================================================================
void DF_Deep_Power_Down(void)
{
Enable_DFLASH();
DF_SpiTxByte(Deep_Power_Down);
Disable_DFLASH();
}
//======================================================================
//函數名稱:DFLASH_Resume_from_Deep_Power_Down(void)
//輸入:
//輸出:
//函數功能:將FLASH從DeepPowerDown模式喚醒
//======================================================================
void DF_Resume_from_Deep_Power_Down(void)
{
Enable_DFLASH();
DF_SpiTxByte(Resume_from_Deep_Power_Down);
Disable_DFLASH();
}
//************************************************************
//*配置FLASH頁大小為1024bytes
//************************************************************
void DF_Configer_Binary_Page_Size(void)
{
u8 i = 0, j = 0;
u8 Power_of_Two_Page_Size_Command[4]={0x3d,0x2a,0x80,0xa6};
while(j < 5)
{
if(!DF_Check_Page_Size())
{//如果頁大小為1056 bytes,則配置為1024 bytes
DF_Check_Busy_State();
Enable_DFLASH();
for(i=0;i<4;i++)
{
DF_SpiTxByte(Power_of_Two_Page_Size_Command[i]);//
}
Disable_DFLASH();
DelayNmsTime(10);
DF_Check_Busy_State();
DelayNmsTime(5);
DF_Deep_Power_Down();
DelayNmsTime(5);
DF_Resume_from_Deep_Power_Down();
DelayNmsTime(1);
}
else
{//已經為1024則不用再配置
break;
}
j ++;
}
}
//************************************************************
//*使能扇區保護
//************************************************************
void DF_Enable_Sector_Protection(void)
{
u8 Enable_Sector_Protection_Command[4]={0x3D,0x2A,0x7F,0xA9}; //使能扇區保護操作碼
u8 i;
DF_Check_Busy_State();
Enable_DFLASH();
for(i=0;i<4;i++)
{
DF_SpiTxByte(Enable_Sector_Protection_Command[i]);//寫使能扇區保護操作碼
}
Disable_DFLASH();
}
//======================================================================
//函數名稱:
//輸入:
//輸出:
//函數功能:禁止扇區保護
//======================================================================
void DF_Disable_Sector_Protection(void)
{
u8 Disable_Sector_Protection_Command[4]={0x3D,0x2A,0x7F,0x9A};//禁止扇區保護操作碼
u8 i;
DF_Check_Busy_State();
Enable_DFLASH();
for(i=0;i<4;i++)
{
DF_SpiTxByte(Disable_Sector_Protection_Command[i]);//寫禁止扇區保護操作碼
}
Disable_DFLASH();
}
//======================================================================
//函數名稱:
//輸入:各扇區保護字0---unprotected 0xff---protected,32byte refer to 32 sector
//輸出:
//函數功能:設置扇區保護 注意:會改變BUFFER1中的內容
//======================================================================
void DF_Program_Sector_Protection_Register(u8 *Sector_Protection_Register)
{
u8 Program_Sector_Protection_Command[4]={0x3D,0x2A,0x7F,0xFC};//設置扇區保護操作碼
u8 i;
DF_Check_Busy_State();
Enable_DFLASH();
for(i=0;i<4;i++)
{
DF_SpiTxByte(Program_Sector_Protection_Command[i]);//寫設置扇區保護操作碼
}
for(i=0;i<32;i++)
{
DF_SpiTxByte(Sector_Protection_Register[i]);//寫設置扇區保護數據
}
Disable_DFLASH();
}
//======================================================================
//函數名稱:
//輸入:
//輸出:
//函數功能:讀取扇區保護寄存器內容(返回32個字節,對應32個扇區的情況)
//======================================================================
void DF_Read_Sector_Protection_Register(u8 *Sector_Protection_Register)
{
u8 Read_Sector_Protection_Register_Command[4]={0x32,0,0,0};
u8 i;
DF_Check_Busy_State();
Enable_DFLASH();
for(i=0;i<4;i++)//write
{
DF_SpiTxByte(Read_Sector_Protection_Register_Command[i]);
}
for(i=0;i<32;i++)//read
{
Sector_Protection_Register[i] = DF_SpiRxByte();
}
Disable_DFLASH();
}
//************************************************************
//*取消所有扇區保護
//*返回1表示成功取消扇區所以保護
//************************************************************
u8 DF_Cancel_Sector_Protection(void)
{
u8 Sector_Protection_Register_for_Write[32]={0,0,0,0,0,0,0,0};//寫入0為去保護
u8 Sector_Protection_Register_for_Read[32]={1,1,1,1,1,1,1,1};//防止默認值為0
u16 i;
u8 j=1;
//使能扇區保護
DF_Enable_Sector_Protection();
//設置扇區保護
DF_Program_Sector_Protection_Register(Sector_Protection_Register_for_Write);
//讀取扇區保護寄存器內容
DF_Read_Sector_Protection_Register(Sector_Protection_Register_for_Read);
//判斷扇區保護寄存器內容
for(i=0;i<8;i++)
{
if(Sector_Protection_Register_for_Read[i] != 0) j++;
}
//禁止扇區保護
DF_Disable_Sector_Protection();
return j;
}
//=============================================================================
//函數名稱:DF_ContinusArrayRead(UIN32 readaddr,u8 *readbuff,u16 len)
//輸入:readaddr指定數據在FLASH中的存儲地址,指定數據的首地址,指定數據的長度
//輸出:readbuf指定數據暫存區的首地址
//函數功能:從FLASH主存readaddr地址處連續讀取len字節數據
//=============================================================================
void DF_ContinusArrayRead(u32 readaddr,u8 *readbuff,u16 len)
{
u16 i;
DF_Check_Busy_State();
Enable_DFLASH();
DF_SpiTxByte(Continuous_Array_Read_Command);
DF_SpiTxByte((u8)((readaddr >> 16) & 0xff));
DF_SpiTxByte((u8)((readaddr >> 8) & 0xff));
DF_SpiTxByte((u8)(readaddr & 0xff));
for(i = 0 ; i < 4 ; i ++)
{
DF_SpiTxByte(0x00);
}
for(i = 0 ; i < len ; i ++)
{
readbuff[i] = DF_SpiRxByte();
}
Disable_DFLASH();
}
//=============================================================================
//函數名稱:DF_BufferWrite(u8 bufferaddr,u8 *writebuff,u16 len)
//輸入:BUFFER中的起始地址, 待存數據的頭指針,待存數據的長度1~1024
//輸出:
//函數功能:將指定數據寫入從某個地址(0~1023)開始的BUFFER1中
//=============================================================================
void DF_BufferWrite(u16 bufferaddr,u8 *writebuff,u16 len)
{
u16 i;
DF_Check_Busy_State();
Enable_DFLASH();
DF_SpiTxByte(Write_Data_to_Buffer1);
DF_SpiTxByte(0x00);
DF_SpiTxByte((u8)((bufferaddr >> 8) & 0x03));
DF_SpiTxByte((u8)(bufferaddr & 0xff));
for(i = 0 ; i < len ; i ++ )
{
DF_SpiTxByte(writebuff[i]);
}
Disable_DFLASH();
}
//=============================================================================
//函數名稱:DF_BufferToMainMemoryWithErase(u16 pageaddr,u8 buffaddr,u8 *writebuff,u16 len)
//輸入:BUFFER中的起始地址, 待存數據的頭指針,待存數據的長度1~1024
//輸出:
//函數功能:將指定數據寫入從某個地址(0~1023)開始的BUFFER1中:包含2個動作,首先將
//指定數據寫入到BUFFER 1中,其中可以指定BUFFER中的起始寫入地址,此寫入動作不影響
//BUFFER中其它地址中的數據,然后再將BUFFER中的整個數據寫入到某指定頁中(帶預擦除)。
//=============================================================================
void DF_BufferToMainMemoryWithErase(u16 pageaddr,u16 buffaddr,u8 *writebuff,u16 len)
{
DF_BufferWrite(buffaddr,writebuff,len);
DF_Check_Busy_State();
Enable_DFLASH();
DF_SpiTxByte(Write_Buffer1_to_Page_whin_Erase);
DF_SpiTxByte((u8)((pageaddr >> 8) & 0x7f));
DF_SpiTxByte((u8)(pageaddr & 0xfc));
DF_SpiTxByte(0x00);
DelayNmsTime(40);
Disable_DFLASH();
}
//======================================================================
//函數名稱:DF_MainMemoryPageProgramThroughBuffer(u32 writeaddr, u8 *writebuff, u16 len)
//輸入:riteaddr---待寫入地址,writebuff數據指針,len數據長度
//輸出:
//函數功能:通過BUF1寫主存,包含動作:先將1-1024字節數據寫入BUF1,然后進行
//BUF1到主存的帶擦除編程
//==================================================================
void DF_MainMemoryPageProgramThroughBuffer(u32 writeaddr, u8 *writebuff, u16 len)
{
u16 i = 0;
DF_Check_Busy_State();
Enable_DFLASH();
DF_SpiTxByte(Main_Memory_Page_Pro_Through_Buff1);
DF_SpiTxByte((u8)((writeaddr >> 16) & 0xff));
DF_SpiTxByte((u8)((writeaddr >> 8) & 0xff));
DF_SpiTxByte((u8)(writeaddr & 0xff));
for(i = 0 ; i < len ; i ++ )
{
DF_SpiTxByte(writebuff[i]);
}
DelayNmsTime(40);
Disable_DFLASH();
}
//========================================================================================
//函數名稱:DF_MainMemoryPagetoBufferTransfer(u16 pageaddr)
//輸入:pageaddr
//輸出:
//函數功能:
//==========================================================================================
void DF_MainMemoryPagetoBufferTransfer(u16 pageaddr)
{
DF_Check_Busy_State();
Enable_DFLASH();
DF_SpiTxByte(Main_MemoryPage_to_Buffer1_Transfer);
DF_SpiTxByte((u8)((pageaddr >> 8) & 0x7f));
DF_SpiTxByte((u8)(pageaddr & 0xfc));
DF_SpiTxByte(0x00);
Disable_DFLASH();
}
//========================================================================================
//函數名稱:
//輸入:
//輸出:
//函數功能:
//==========================================================================================
void DF_AutoPageRewriteThroughBuffer(u16 pageaddr)
{
DF_Check_Busy_State();
Enable_DFLASH();
DF_SpiTxByte(Auto_Page_Rewrite_through_Buffer1);
DF_SpiTxByte((u8)((pageaddr >> 8) & 0x7f));
DF_SpiTxByte((u8)(pageaddr & 0xfc));
DF_SpiTxByte(0x00);
Disable_DFLASH();
DelayNmsTime(18);
}
//=========================================================================================
//函數名稱:DF_Chip_Erase(void)
//輸入:
//輸出:
//函數功能:整片擦除FLASH全部內容
//==========================================================================================
void DF_Chip_Erase(void)
{
u8 Chip_Erase_Command[4] = {0xC7,0x94,0x80,0x9A};//整片擦除操作碼
u8 i;
DF_Check_Busy_State();
Enable_DFLASH();
for(i = 0 ; i < 4 ; i ++)
{
DF_SpiTxByte(Chip_Erase_Command[i]);
}
Disable_DFLASH();
}
//=========================================================================================
//函數名稱:
//輸入:
//輸出:
//函數功能:讀取制造ID,確認是否有DATAFLASH存在,存在返回1,否則返回0
//==========================================================================================
u8 DF_ReadManufactureIDInformation(void)
{
u8 i = 0;
u8 Idbuff[4] = {0};
DF_Check_Busy_State();
Enable_DFLASH();
DF_SpiTxByte(Device_ID_Opcode);
for(i = 0 ; i < 4 ; i ++)
{
Idbuff[i] = DF_SpiRxByte();
}
Disable_DFLASH();
if((Idbuff[0] == 0x1f) && (Idbuff[1] == 0x28))
{
return 1;
}
else
{
return 0;
}
}
//=========================================================================================
//函數名稱:
//輸入:
//輸出:
//函數功能:初始化DataFlash,檢查制造ID,配置為1024字節每page,禁止扇區保護
//==========================================================================================
void DF_Init(void)
{
u8 i = 0;
GPIO_SetBits(GPIOC, FCS);
GPIO_ResetBits(GPIOC, FSCK);//AT45DB選擇SPI方式0
GPIO_ResetBits(GPIOC, FSDI);
i = DF_Check_Busy_State();
if(DF_ReadManufactureIDInformation())
{
DF_Configer_Binary_Page_Size();
i = DF_Cancel_Sector_Protection();
}
else
{//未檢測到DF,則給出提示報警信息
i = 0;
while(i < 50)
{
i ++;
}
BeepTim = 100;
}
}
//=========================================================================================
//函數名稱:SaveDataToFlash(u32 *pBuffer, u32 cnt, u32 addr)
//輸入:pBuffer-數據緩沖區首址,cnt-寫入字節數,WriteAddr-保存地址
//輸出:保存成功與否
//函數功能:將任意字節(小于0X800000)數據保存入FLASH任意地址
//==========================================================================================
u8 SaveDataToFlash(u8 *pBuffer, u32 sizlen, u32 WriteAddr)
{
u32 NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0;
u16 paddr = 0;
if(WriteAddr + sizlen >= DF_FullSize)
{//如果 超出地址
return 0;
}
Addr = WriteAddr % DF_PageSize;
count = DF_PageSize - Addr;
NumOfPage = sizlen / DF_PageSize;
NumOfSingle = sizlen % DF_PageSize;
/* If WriteAddr is DF_PageSize aligned */
if(Addr == 0)
{//地址為整頁起始地址
/* If sizlen < DF_PageSize */
if(NumOfPage == 0)
{//不夠一頁
paddr = (u16)((WriteAddr >> 8) & 0xfffc);//查找頁地址
DF_MainMemoryPagetoBufferTransfer(paddr);
DF_MainMemoryPageProgramThroughBuffer(WriteAddr,pBuffer,NumOfSingle);
DF_AutoPageRewriteThroughBuffer(paddr);
}
/* If sizlen > DF_PageSize */
else
{//超出一頁
while(NumOfPage--)
{//先寫整頁
paddr = (u16)((WriteAddr >> 8) & 0xfffc);//查找頁地址
DF_MainMemoryPagetoBufferTransfer(paddr);
DF_MainMemoryPageProgramThroughBuffer(WriteAddr,pBuffer,DF_PageSize);
DF_AutoPageRewriteThroughBuffer(paddr);
WriteAddr += DF_PageSize;
pBuffer += DF_PageSize;
}//再寫余下部分
if(NumOfSingle!=0)
{
paddr = (u16)((WriteAddr >> 8) & 0xfffc);//查找頁地址
DF_MainMemoryPagetoBufferTransfer(paddr);
DF_MainMemoryPageProgramThroughBuffer(WriteAddr,pBuffer,NumOfSingle);
DF_AutoPageRewriteThroughBuffer(paddr);
}
}
}
/* If WriteAddr is not DF_PageSize aligned */
else
{//起始地址非整頁地址
/* If sizlen < DF_PageSize */
if(NumOfPage== 0)
{//不超過一頁
if(NumOfSingle > count)
{//產生跨頁
paddr = (u16)((WriteAddr >> 8) & 0xfffc);//查找頁地址
DF_MainMemoryPagetoBufferTransfer(paddr);
DF_MainMemoryPageProgramThroughBuffer(WriteAddr,pBuffer,count);
DF_AutoPageRewriteThroughBuffer(paddr);
NumOfSingle -= count;
WriteAddr += count;
pBuffer += count;
paddr = (u16)((WriteAddr >> 8) & 0xfffc);//查找頁地址
DF_MainMemoryPagetoBufferTransfer(paddr);
DF_MainMemoryPageProgramThroughBuffer(WriteAddr,pBuffer,NumOfSingle);
DF_AutoPageRewriteThroughBuffer(paddr);
}
else
{//不產生跨頁
paddr = (u16)((WriteAddr >> 8) & 0xfffc);//查找頁地址
DF_MainMemoryPagetoBufferTransfer(paddr);
DF_MainMemoryPageProgramThroughBuffer(WriteAddr,pBuffer,NumOfSingle);
DF_AutoPageRewriteThroughBuffer(paddr);
}
}
/* If sizlen > DF_PageSize */
else
{//超過一頁
sizlen -= count;
NumOfPage = sizlen / DF_PageSize;
NumOfSingle = sizlen % DF_PageSize;
if(count != 0)
{
paddr = (u16)((WriteAddr >> 8) & 0xfffc);//查找頁地址
DF_MainMemoryPagetoBufferTransfer(paddr);
DF_MainMemoryPageProgramThroughBuffer(WriteAddr,pBuffer,count);
DF_AutoPageRewriteThroughBuffer(paddr);
WriteAddr += count;
pBuffer += count;
}
while(NumOfPage--)
{
paddr = (u16)((WriteAddr >> 8) & 0xfffc);//查找頁地址
DF_MainMemoryPagetoBufferTransfer(paddr);
DF_MainMemoryPageProgramThroughBuffer(WriteAddr,pBuffer,DF_PageSize);
DF_AutoPageRewriteThroughBuffer(paddr);
WriteAddr += DF_PageSize;
pBuffer += DF_PageSize;
}
if(NumOfSingle != 0)
{
paddr = (u16)((WriteAddr >> 8) & 0xfffc);//查找頁地址
DF_MainMemoryPagetoBufferTransfer(paddr);
DF_MainMemoryPageProgramThroughBuffer(WriteAddr,pBuffer,NumOfSingle);
DF_AutoPageRewriteThroughBuffer(paddr);
}
}
}
return 1;
}
//=========================================================================================
//函數名稱:GetFlashData(u32 readdr, u8 *pBuffer, u16 sizlen)
//輸入:readdr-讀取地址,pBuffer-讀取數據緩存地址,sizlen-數據字節數
//輸出:
//函數功能:從FLASH任意(0-0X7FFFFF)地址讀取任意字節(0-65535)數據
//==========================================================================================
u8 GetFlashData(u32 readdr, u8 *pBuffer, u16 sizlen)
{
if(readdr + sizlen >= DF_FullSize)
{//如果 超出地址
return 0;
}
DF_ContinusArrayRead(readdr,pBuffer,sizlen);
return 1;
}
//=========================================================================================
//函數名稱:
//輸入:
//輸出:
//函數功能:測試讀寫DATAFLASH
//==========================================================================================
void Test_At45db642d(void)
{
#ifdef TEST_AT45DB
u16 paddr = 0;
u8 i = 0;
if(TestAddr < TESTBASE + 0x2000)
{
for(i = 0; i < 32; i ++)
{
TestBuffer[i] = i;
}
paddr = (u16)((TestAddr >> 8) & 0xfffc);
DF_MainMemoryPagetoBufferTransfer(paddr);
DF_MainMemoryPageProgramThroughBuffer(TestAddr,TestBuffer,32);
DF_AutoPageRewriteThroughBuffer(paddr);
for(i = 0; i < 250; i ++)
{
TestBuffer[i] = 0;
}
DF_ContinusArrayRead(TestAddr-32,TestBuffer,96);
TestAddr += 32;
}
#endif
}