CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

driver for at45db041

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Rob Sterling



Joined: 07 Sep 2003
Posts: 1

View user's profile Send private message

driver for at45db041
PostPosted: Sat Dec 20, 2003 12:29 am     Reply with quote

Has anybody used the at45db041 and written a driver for it. I am trying to use the at45db041 but am not having much luck with the data storage.

Any help will be appriciated.

Thanks
rob Very Happy
Hans
Guest







PostPosted: Sat Dec 20, 2003 2:05 am     Reply with quote

Hi,

might not be exactly what you want but this:
http://www.atmel.com/dyn/resources/prod_documents/doc1456.pdf

is an appnote from Atmel how to use Dataflash with their AVR controllers.

You should be able to get what you need out of this.

Best regards
dvsoft



Joined: 28 Nov 2003
Posts: 46

View user's profile Send private message

PostPosted: Sat Dec 20, 2003 2:11 am     Reply with quote

Hello

I use the following code with a AT45DB321B I hope that it will help you

Alain


//----------------------------------------------------------------------------
// SPI I/O
//
#ifndef __SPI_IO__
#USE fast_io(c)
#define SPI_SO PIN_A5 // SPI Out pin
#define SPI_SI PIN_A4 // SPI In pin
#define SPI_SCK PIN_E0 // SPI Out Clock pin
#define SPI_TRIS_A 0x30 // Tris on port A
#define SPI_TRIS_E 0x00 // Tris on port E
#endif

//----------------------------------------------------------------------------
// Function :write_SPI
// Version :0.1
// Author :Alain
//
// Usage :write_SPI(Data)
//
// Arguments:BYTE Data Data to write
//
// Return :void
//
// Modify :void
//
// Shift one Byte out on the falling edge of the clock.
//----------------------------------------------------------------------------
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);
}
//----------------------------------------------------------------------------
// Function :read_SPI
// Version :0.1
// Author :Alain
//
// Usage :Data = read_SPI(void)
//
// Arguments:void
//
// Return :BYTE Data -> Read result
//
// Modify :void
//
// Read one Byte in data on the rising edge of the clock.
//----------------------------------------------------------------------------
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);
}
//----------------------------------------------------------------------------


//----------------------------------------------------------------------------
// DataFlash I/O
//
#USE fast_io(b)
#define DTF_CS PIN_E1

//----------------------------------------------------------------------------
// DataFlash Macro
//
#define DTF_SELECT() output_low(DTF_CS)
#define DTF_UNSELECT() output_high(DTF_CS)
#define DTF_WAIT_LOOP 3

//----------------------------------------------------------------------------
// DataFlash value
//
#define DTF_FIRST_PAGE 257
#define DTF_BUFFER_SIZE 512
#define DTF_SECTOR_SIZE 528
#define DTF_MAX_PAGE 8192
#define DTF_PREFIXE_SIZE (DTF_SECTOR_SIZE-DTF_BUFFER_SIZE)
#define DTF_PREFIXE_OFFSET 0x02
#define DTF_VERSION 0xC0
#define DTF_SIGNATURE 0x0A


//----------------------------------------------------------------------------
// DataFlash command
//
#define DTF_WRITE_BUFFER_1 0x84
#define DTF_WRITE_BUFFER_2 0x87
#define DTF_READ_BUFFER_1 0x54
#define DTF_READ_BUFFER_2 0x56
#define DTF_MM_TO_BUFFER_1 0x53
#define DTF_MM_TO_BUFFER_2 0x55

//----------------------------------------------------------------------------
// DataFlash Prog mode
//
#ifdef __BUILT_IN_ERASE__
#define DTF_BUFFER_1_TO_MM 0x83
#define DTF_BUFFER_2_TO_MM 0x86
#else
#define DTF_BUFFER_1_TO_MM 0x88
#define DTF_BUFFER_2_TO_MM 0x89
#endif

//----------------------------------------------------------------------------
// DataFlash data
//
int16 DTF_ByteCounter;
int16 DTF_PageCounter;
BOOLEAN DTF_BUFFER_1_ENABLED;

//----------------------------------------------------------------------------
// DataFlash prototype
//
BOOLEAN DTF_WaitReady(void);
BYTE DTF_ReadStatus(void);
BYTE DTF_Init(void);
BYTE DTF_ErasePage(int16 PageNum);
BOOLEAN DTF_WritePrefix(BOOLEAN Clear);
BOOLEAN DTF_ReadPrefix(void);
BOOLEAN DTF_RamToPage(int16 PageNum);
BOOLEAN DTF_PageToRam(int16 PageNum);

//----------------------------------------------------------------------------
BOOLEAN DTF_WaitReady(void)
{
BYTE Status,iLoop;
//--- Selected
DTF_SELECT();
//--- Read Status Command
write_SPI(0x57);
//--- Max wait loop
iLoop = DTF_WAIT_LOOP;
do {
Status = read_SPI();
}while(!(Status & 0x80) && --iLoop);
//--- Unselected
DTF_UNSELECT();
if (!iLoop)
return (FALSE);
return (TRUE);
}
//----------------------------------------------------------------------------
BYTE DTF_ReadStatus()
{
BYTE Status;
//--- Select
DTF_SELECT();
//--- Command (8bits)
write_SPI(0x57);
//--- Ready Result
Status = read_SPI();
//--- Unselect
DTF_UNSELECT();
return (Status);
}
//----------------------------------------------------------------------------
BYTE DTF_Init(void)
{
BYTE Status;
//--- Status
DTF_ReadStatus();
DTF_ReadStatus();
//--- Maybe no flash device connected ???
if(Status == 0xFF )
return (2);
//--- Control Density Code for AT45DB321B
if (Status & 0x80) {
//--- Get density value
if (((Status >> 3) & 0x7) != 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
DTF_SELECT();
//--- 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
DTF_UNSELECT();
//--- Wait DTF_Ready
DTF_WaitReady ();
//--- Ok
return (0);
}// ENd IF
return (2);
}
//----------------------------------------------------------------------------
BYTE DTF_EraseAllPage()
{
BYTE Status;
int16 MaxPage = DTF_MAX_PAGE;
//--- Delete loop
do {
Status = DTF_ErasePage(MaxPage);
} while ((--MaxPage >= DTF_FIRST_PAGE) && !Status);
//--- DataFlash buffer1 is Active Buffer
DTF_BUFFER_1_ENABLED = TRUE;
//--- Reset byte counter
DTF_ByteCounter = 0;
//--- Reset Page Counter
DTF_PageCounter = 0;
return Status;
}
//----------------------------------------------------------------------------
BOOLEAN DTF_WritePrefix(BOOLEAN Clear)
{
int16 StartAddress = DTF_PREFIXE_OFFSET;
//--- Wait DTF_Ready
if (!DTF_WaitReady ())
return (FALSE);
//--- Write the value into the active RAM buffer
DTF_SELECT();
//--- Active buffer 1
if (DTF_BUFFER_1_ENABLED)
write_SPI(DTF_WRITE_BUFFER_1);
//--- Active Buffer 2
else
write_SPI(DTF_WRITE_BUFFER_2);
//--- don't cares 8 bits
write_SPI(0x00);
//--- don't cares 8 bits + first 2 bits of address
write_SPI(*((BYTE*)&StartAddress +1));
//--- Low address bits
write_SPI((BYTE)StartAddress);
if (!Clear) {
//--- Version
write_SPI(DTF_VERSION);
//--- Sync
write_SPI(DTF_SIGNATURE);
}// End IF
else {
//--- Clear prefixe
write_SPI(0x00);
write_SPI(0x00);
}// End Else
//--- Unselect
DTF_UNSELECT();
//--- Success
return (TRUE);
}
//----------------------------------------------------------------------------
BOOLEAN DTF_ReadPrefix(void)
{
BYTE Version,SyncValue;
BOOLEAN RetValue = TRUE;
//--- Prefix byte address
int16 StartAddress = DTF_PREFIXE_OFFSET;
//--- Write prefixe to RAM buffer
DTF_SELECT();
//--- Wait DTF_Ready
if (!DTF_WaitReady ())
return (FALSE);
//--- Active buffer 1
if (DTF_BUFFER_1_ENABLED)
write_SPI(DTF_READ_BUFFER_1);
//--- Active Buffer 2
else
write_SPI(DTF_READ_BUFFER_2);
//--- don't cares 8 bits
write_SPI(0x00);
//--- don't cares 8 bits + first 2 bits of address
write_SPI(*((BYTE*)&StartAddress +1));
//--- Low address bits
write_SPI((BYTE)StartAddress);
//--- don't care 8 bits
write_SPI(0x00);
//--- Version Value
Version = read_SPI();
//--- Syncho value
SyncValue = read_SPI();
if (SyncValue != DTF_SIGNATURE || Version != DTF_VERSION)
RetValue = FALSE;
//--- Unselect
DTF_UNSELECT();
return (RetValue);
}
//----------------------------------------------------------------------------
BOOLEAN DTF_RamToPage(int16 PageNum)
{
//--- Wait DTF_Ready
if (!DTF_WaitReady())
return FALSE;
//--- Active buffer to main memory page
PageNum <<= 2;
//--- Select
DTF_SELECT();
//--- 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
DTF_UNSELECT();
return (TRUE);
}
//----------------------------------------------------------------------------
BOOLEAN DTF_PageToRam(int16 PageNum)
{
//--- Wait DTF_Ready
if (!DTF_WaitReady ())
return (FALSE);
//--- Load memory page
PageNum <<= 2;
//--- Select
DTF_SELECT();
//--- 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
DTF_UNSELECT();
return (TRUE);
}
//----------------------------------------------------------------------------
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group