Nexonccs
Joined: 17 Sep 2008 Posts: 1
|
dvsoft lib to work with AT45DB021B |
Posted: Wed Sep 17, 2008 10:00 am |
|
|
HI everyone
I'm using PIC18 at 20M clock, my concern question, how I can write directly to page cross the buffer for example:
write( p-->page address, i---->index in the page)
i used the code of dvsoft posted for at45db041) (this code it bitbang, don't use SPI of device), another thing I used the code At45db021.c example but don't works , the only problem with dvsoft code, when I write buffer, and have 264Bytes, I use RAM page, the first page in SPI flash it write very well,
but when I write second page nothing happen, also erase page don't work it reply 0 mean that's ok but when I read it again it still inside. anyhow this the code ( this code for dvsoft, just modified it little)..
I appreciate your help if someone haves idea to write directly specific page at specific address also reading page at specific address
Code: |
void write_SPI(BYTE Shift)
{
BYTE BitCnt = 8;
//--- Shifts out
do {
//--- Clock low
output_low(SPI_SCK);
//--- Bit = 1
if(Shift & 0x80)
output_high(SPI_SO);
//--- Bit = 0
else
output_low(SPI_SO);
Shift <<= 1;
//--- Clock high
output_high(SPI_SCK);
} while (--BitCnt);
}
BYTE read_SPI()
{
BYTE Shift,BitCnt = 8;
//--- Shift in
do {
//--- Clock Low, high
output_low(SPI_SCK);
output_high(SPI_SCK);
Shift <<= 1;
//--- Bit = 0
if(!input(SPI_SI))
Shift &= ~1;
//--- Bit = 1
else
Shift |= 1;
} while (--BitCnt);
return (Shift);
}
BOOLEAN DTF_WaitReady(void)
{
BYTE Stat,iLoop;
//--- Selected
DTFSELECT();
//--- Read Status Command
write_SPI(0x57);
//--- Max wait loop
iLoop = DTF_WAIT_LOOP;
do {
Stat = read_SPI();
}while(!(Stat & 0x80) && --iLoop);
//--- Unselected
DTFUNSELECT();
if (!iLoop)
return (FALSE);
return (TRUE);
}
BYTE DTF_ReadStatus()
{
BYTE Statu;
//--- Select
DTFSELECT();
//--- Command (8bits)
write_SPI(0x57);
//--- Ready Result
Statu = read_SPI();
//--- Unselect
DTFUNSELECT();
return (Statu);
}
BYTE DTF_Init(void)
{
BYTE Statu;
//--- Status
DTF_ReadStatus();
DTF_ReadStatus();
//--- Maybe no flash device connected ???
if(Statu == 0xFF )
return (2);
//--- Control Density Code for AT45DB321B
if (Statu & 0x80) {
//--- Get density value
if (((Statu >> 3) & 0x5) != 0x06)
return (1);
}// End IF
return 0;
}
BYTE DTF_ErasePage(int16 PageNum)
{
//--- Wait DTF_Ready
DTF_WaitReady ();
//--- Check valide page address
if ((PageNum >= 0 && PageNum < DTF_MAX_PAGE)) {
//--- Page*4
PageNum <<= 2;
//--- Select
DTFSELECT();
//--- command (32bits)
write_SPI(0x81);
//--- 7 high address bits
write_SPI(*((BYTE*)&PageNum + 1));
//--- 6 low address bits
write_SPI((BYTE)PageNum);
//--- don't care 8 bits
write_SPI(0x00);
//--- Unselect
DTFUNSELECT();
//--- Wait DTF_Ready
DTF_WaitReady ();
//--- Ok
return (0);
}// ENd IF
return (2);
}
//----------------------------------------------------------------------------
BYTE DTF_EraseAllPage()
{
BYTE Status1;
int16 MaxPage = DTF_MAX_PAGE;
//--- Delete loop
do {
Status1 = DTF_ErasePage(MaxPage);
} while ((--MaxPage >= DTF_FIRST_PAGE) && !Status1);
//--- DataFlash buffer1 is Active Buffer
DTF_BUFFER_1_ENABLED = TRUE;
//--- Reset byte counter
DTF_ByteCounter = 0;
//--- Reset Page Counter
DTF_PageCounter = 0;
return Status1;
}
int8 DTF_RamToPage(int16 PageNum)
{
//--- Wait DTF_Ready
if (!DTF_WaitReady())
return 1;
//--- Active buffer to main memory page
PageNum <<= 2;
//--- Select
DTFSELECT();
//--- Active buffer 1
if (DTF_BUFFER_1_ENABLED) {
//--- Write Buffer 1 to Memory Page with built-in Erase
write_SPI(DTF_BUFFER_1_TO_MM);
//--- Active Buffer 2
DTF_BUFFER_1_ENABLED = FALSE;
}// End IF
//--- Active Buffer 2
else {
//--- Write Buffer 2 to Memory Page with built-in Erase
write_SPI(DTF_BUFFER_2_TO_MM);
//--- Active Buffer 1
DTF_BUFFER_1_ENABLED = TRUE;
}// End ELse
write_SPI(0x83);
//--- 7 high address bits
write_SPI(*((BYTE*)&PageNum + 1));
//--- 6 low address bits
write_SPI((BYTE)PageNum);
//--- don't care 8 bits
write_SPI(0x00);
//--- Unselect
DTFUNSELECT();
return (0);
}
//----------------------------------------------------------------------------
BOOLEAN DTF_PageToRam(int16 PageNum)
{
//--- Wait DTF_Ready
if (!DTF_WaitReady ())
return (FALSE);
//--- Load memory page
PageNum <<= 2;
//--- Select
DTFSELECT();
//--- Active buffer 1
if (DTF_BUFFER_1_ENABLED)
//--- Main Memory Page To Buffer 1
write_SPI(DTF_MM_TO_BUFFER_1);
//--- Active Buffer 2
else
//--- Main Memory Page To Buffer 2
write_SPI(DTF_MM_TO_BUFFER_2);
//--- 7 high address bits
write_SPI(*((BYTE*)&PageNum + 1));
//--- 6 low address bits
write_SPI((BYTE)PageNum);
//--- don't care 8 bits
write_SPI(0x00);
//--- Unselect
DTFUNSELECT();
return (TRUE);
}
void DTFWriteToFlash(int8 Data)
{
// if (DTFFull)
// return;
while (!DTF_WaitReady ());
DTFSelect();
if (DTF_BUFFER_1_ENABLED)
write_SPI(0x87);
else
write_SPI(0x84);
write_SPI(0x00);
write_SPI(DTFByteCnt_H);
//--- Low Byte address
write_SPI(DTFByteCnt_L);
//--- Write The Value
write_SPI(Data);
//--- UnSelect
DTFUnSelect();
//--- Next Word Address In DataFlash
DTFByteCnt++;
//--- Last page Byte ?
if (DTFByteCnt >= 264) {
//--- WriteRam Start Addresse
DTFByteCnt = 0;
//--- RAM To Memory page with builtin erase
DTF_RAMToPage(DTFPageCnt);
//--- Next Page
DTFPageCnt++;
//--- Store the Last Page Value in PIC eeprom
write_eeprom(DTFPageCntRomAddress,DTFPageCnt);
//--- Last Page ?
if (DTFPageCnt >= 1024)
DTFFull = TRUE;
}// End IF
}
//--------------------------
//--------------**********************************************
void DTFDumpPacket(void)
{
int8 TxBuffer;
int16 ByteCnt;
//--- Wait DTF_Ready
while (!DTF_WaitReady ());
//--- Initialisation
DTFSelect();
//--- Buffer 2 is Active ?
if (DTF_BUFFER_1_ENABLED)
write_SPI(0x56);
//--- Buffer 1 is Active
else
write_SPI(0x54);
//--- 8 Dont'care Byte
write_SPI(0x00);
//--- High Byte address
write_SPI(0x00);
//--- Low Byte address
write_SPI(0x00);
//--- 8 Dont'care Byte
write_SPI(0x00);
//--- Byte Count
ByteCnt = 264;
//--- Load In Buffer
do {
//--- Read One Byte
TxBuffer = read_SPI();
//--- Put in serial UART
putc(TxBuffer);
} while (--ByteCnt);
//--- Unselect
DTFUnSelect();
}
void DTFInitDump(void)
{
DTFPageCnt=read_eeprom(DTFPageCntRomAddress);
if(DTFPageCnt==0xff)
{
DTFPageCnt=0; //--- First page to dump
}
DTFByteCnt=0;
// DTFDumpPageCnt = 0;
//--- Load first page in memory buffer
DTF_PageToRam(DTFPageCnt);
} |
|
|