mpardinho
Joined: 16 Jul 2008 Posts: 8 Location: Mexico
|
at45dbxxx |
Posted: Fri Apr 27, 2012 8:01 am |
|
|
I can write and read buffer, but I can't read page.
Could anybody Point me in the right direction to get the data ?
Thank You
Code: |
#define FLASH_SELECT PIN_C3 // 4
#define FLASH_CLOCK PIN_C1 // 2
#define FLASH_DI PIN_C0 // 1
#define FLASH_DO PIN_C2 // 8
int16 QTDE_ROWS, QTDE_COLS;
byte ext_flash_readStatus();
void ext_flash_waitUntilReady();
byte ext_flash_getByte();
void ext_flash_sendData(int16 data, int8 size);
void ext_flash_sendBytes(byte* data, int16 size);
#define DTFSelect()\
output_low(FLASH_SELECT);\
delay_cycles(3) // 300 ns PIC18F452 at 40Mhz
#define DTFUnSelect()\
output_high(FLASH_SELECT);\
delay_cycles(3) // 300 ns PIC18F452 at 40Mhz
//------------------------------------------------------------------------------
void init_ext_flash() {
int8 r;
ext_flash_waitUntilReady();
r = ext_flash_readStatus();
r = r & 0b00111000;
r = r >> 3;
QTDE_ROWS = 0;
switch (r) {
case 1: QTDE_ROWS = 512; QTDE_COLS = 256; break;
case 2: QTDE_ROWS = 1024; QTDE_COLS = 256; break;
case 3: QTDE_ROWS = 2048; QTDE_COLS = 256; break;
case 4: QTDE_ROWS = 4096; QTDE_COLS = 256; break;
}
}
//------------------------------------------------------------------------------
void ext_flash_waitUntilReady() {
int8 i, r;
DTFSelect();
i = 20;
do {
ext_flash_sendData(0x57, 8);
r = ext_flash_readStatus();
} while (!(r & 0b10000000) && --i);
DTFUnSelect();
}
//------------------------------------------------------------------------------
byte ext_flash_readStatus() {
byte status;
DTFSelect();
ext_flash_sendData(0x57, 8);
status = ext_flash_getByte();
DTFUnSelect();
return status;
}
//------------------------------------------------------------------------------
byte ext_flash_getByte() {
byte flashData;
int i;
for (i = 0; i < 8; ++i) {
output_high(FLASH_CLOCK);
shift_left(&flashData, 1, input(FLASH_DO));
output_low(FLASH_CLOCK);
}
return flashData;
}
//------------------------------------------------------------------------------
void ext_flash_getBytes(byte* data, int16 size) {
int16 i;
signed int8 j;
for (i = 0; i < size; ++i) {
for (j = 0; j < 8; ++j) {
output_high(FLASH_CLOCK);
shift_left(data + i, 1, input(FLASH_DO));
output_low(FLASH_CLOCK);
}
}
}
//------------------------------------------------------------------------------
void ext_flash_readPage(int16 pageAddress, int16 pageIndex, byte* data, int16 size) {
ext_flash_waitUntilReady();
DTFSelect();
ext_flash_sendData(0xD2, 8); // D2
ext_flash_sendData(pageAddress, 15);
ext_flash_sendData(pageIndex, 9);
ext_flash_sendData(0, 32);
ext_flash_getBytes(data, size);
DTFUnSelect();
}
//------------------------------------------------------------------------------
#inline
void ext_flash_readBuffer(int1 bufferNumber, int16 bufferAddress, byte* data, int16 size) {
byte opcode;
DTFSelect(); // Enable select line
if (bufferNumber)
opcode = 0xD6; // Opcode for second buffer
else
opcode = 0xD4; // Opcode for first buffer
opcode = 0xD4;
ext_flash_sendData(opcode, 8); // Send opcode
ext_flash_sendData(0, 15); // Send 15 don't care bits
ext_flash_sendData(bufferAddress, 9); // Send buffer address
ext_flash_sendData(0, 8); // Send 8 don't care bits
ext_flash_getBytes(data, size); // Get data from the flash device
DTFUnSelect(); // Disable select line
}
//------------------------------------------------------------------------------
void ext_flash_writeToBuffer(int1 bufferNumber, int16 bufferAddress, byte* data, int16 size) {
byte opcode;
DTFSelect(); // Enable select line
if (bufferNumber)
opcode = 0x87; // Opcode for second buffer
else
opcode = 0x84; // Opcode for first buffer
opcode = 0x84;
ext_flash_sendData(opcode, 8); // Send opcode
ext_flash_sendData(0, 15); // Send 15 don't care bits
ext_flash_sendData(bufferAddress, 9); // Send buffer address
ext_flash_sendBytes(data, size); // Write data to the buffer
DTFUnSelect(); // Disable select line
}
//------------------------------------------------------------------------------
void ext_flash_BufferToPage(int1 bufferNumber, int16 pageAddress, int1 mode) {
byte opcode;
ext_flash_waitUntilReady(); // Wait until ready
DTFSelect(); // 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
}
opcode = 0x83;
ext_flash_sendData(opcode, 8); // Send opcode
ext_flash_sendData(pageAddress, 15); // Send page address
ext_flash_sendData(0, 9); // Send 9 don't care bits
DTFUnSelect(); // Disable select line
}
//------------------------------------------------------------------------------
void ext_flash_erasePage(int16 pageAddress) {
ext_flash_waitUntilReady();
DTFSelect(); // Enable select line
ext_flash_sendData(0x81, 8); // Send opcode
ext_flash_sendData(pageAddress, 15); // Send page address
ext_flash_sendData(0, 9); // Send 9 don't care bits
DTFUnSelect(); // Disable select line
}
//------------------------------------------------------------------------------
void ext_flash_eraseBlock(int8 blockAddress) {
ext_flash_waitUntilReady();
DTFSelect(); // Enable select line
ext_flash_sendData(0x50, 8); // Send opcode
ext_flash_sendData(blockAddress, 12); // Send block address
ext_flash_sendData(0, 12); // Send 12 don't care bits
DTFUnSelect(); // Disable select line
}
//------------------------------------------------------------------------------
void ext_flash_writePageThroughBuffer(int16 pageAddress, int1 bufferNumber, int16 bufferAddress, byte* data, int16 size) {
byte opcode;
ext_flash_waitUntilReady();
DTFSelect(); // Enable select line
if (bufferNumber)
opcode = 0x85; // Opcode for second buffer
else
opcode = 0x82; // Opcode for first buffer
opcode = 0x82;
ext_flash_sendData(opcode, 8); // Send opcode
ext_flash_sendData(pageAddress, 15); // Send page address
ext_flash_sendData(bufferAddress, 9); // Send buffer address
ext_flash_sendBytes(data, size); // Write data to the buffer
DTFUnSelect(); // Disable select line
}
//------------------------------------------------------------------------------
#inline
void ext_flash_PageToBuffer(int16 pageAddress, int1 bufferNumber) {
byte opcode;
ext_flash_waitUntilReady();
DTFSelect(); // Enable select line
if (bufferNumber)
opcode = 0x55; // Opcode for second buffer
else
opcode = 0x53; // Opcode for first buffer
opcode = 0x53;
ext_flash_sendData(opcode, 8); // Send opcode
ext_flash_sendData(pageAddress, 15); // Send page address
ext_flash_sendData(0, 9); // Send 9 don't care bits
DTFUnSelect(); // Disable select line
}
//------------------------------------------------------------------------------
int1 ext_flash_comparePageToBuffer(int16 pageAddress, int1 bufferNumber) {
int1 CompareFlag;
byte opcode;
ext_flash_waitUntilReady();
DTFSelect(); // 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, 15); // Send page address
ext_flash_sendData(0, 9); // Send 9 don't care bits
DTFUnSelect(); // Disable select line
DTFSelect(); // Enable select line
ext_flash_sendData(0xD7, 8); // Send status command
while(!input(FLASH_DO)); // Wait until ready
output_high(FLASH_CLOCK); // Pulse the clock
output_low(FLASH_CLOCK);
CompareFlag = !input(FLASH_DO); // Get the compare flag
DTFUnSelect(); // Disable select line
return CompareFlag;
}
//------------------------------------------------------------------------------
void ext_flash_rewritePage(int16 pageAddress, int bufferNumber) {
byte opcode;
ext_flash_waitUntilReady();
DTFSelect();
if (bufferNumber)
opcode = 0x58;
else
opcode = 0x59;
ext_flash_sendData(opcode, 8);
ext_flash_sendData(pageAddress, 15);
ext_flash_sendData(0, 9);
DTFUnSelect();
}
//------------------------------------------------------------------------------
void ext_flash_sendData(int16 data, int8 size) {
int8 i;
data <<= (16-size);
for (i = 0; i < size; ++i) {
output_bit(FLASH_DI, shift_left(&data, 2, 0));
output_high(FLASH_CLOCK);
output_low(FLASH_CLOCK);
}
}
//------------------------------------------------------------------------------
void ext_flash_sendBytes(byte* data, int16 size) {
int16 i;
signed int8 j;
for (i = 0; i < size; ++i) {
for (j = 7; j >= 0; --j) {
output_bit(FLASH_DI, bit_test(data[i], j));
output_high(FLASH_CLOCK);
output_low(FLASH_CLOCK);
}
}
}
|
Code: |
init_ext_flash();
if (QTDE_ROWS == 0) {
fprintf(SERIAL, "MEMORIA OFF\n");
} else {
fprintf(SERIAL, "%Lu\n", QTDE_ROWS);
j = 0;
for (i = 0; i < QTDE_ROWS; i++) {
buffer[i] = 65 + j;
j++;
if (j == 26) j = 0;
fprintf(SERIAL, "%c", buffer[i]);
}
fprintf(SERIAL, "\n");
ext_flash_writeToBuffer(0, 0, buffer, QTDE_ROWS);
for (i = 0; i < QTDE_ROWS; i++) buffer[i] = 0;
ext_flash_readBuffer(0, 0, buffer, QTDE_ROWS);
for (i = 0; i < QTDE_ROWS; i++) fprintf(SERIAL, "%c", buffer[i]);
fprintf(SERIAL, "\n");
ext_flash_BufferToPage(0, 0, 0);
fprintf(SERIAL, "Linha\n");
for (i = 0; i < QTDE_ROWS; i++) buffer[i] = 0;
ext_flash_readPage(0, 0, buffer, QTDE_ROWS);
for (i = 0; i < QTDE_ROWS; i++) fprintf(SERIAL, "%c", buffer[i]);
fprintf(SERIAL, "\n");
}
|
|
|