at90usbkey and at45DB642D help!!!!!

Go To Last Post
2 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I want to save data to data-flash at45db642 present on at90usbkey...

After reading the datasheet of dataflash I am trying to write 3 bytes in buffer1 and trying to read the same...

I am providing details of my program that I have written to achieve this...but still not working....
please correct it if something is wrong...

**Program

#My SPCR register= 01011000
Now 3 bytes to write in buffer1 and starting address=0

#spiwrite
Reset Porte.0 -----------making cs low

Spdr = 132----------- opcode 84H for buffer1
Spdr = 0 -------- first 8 bits don't care
Spdr = 0 -------- 9 to 14 bits don't care & 2 add. bit
Spdr = 0 ------ last 8 address bits as 0
Spdr = 10----- first data byte
Spdr = 11----- second data byte
Spdr = 12---- third data byte

Set Porte.0-----------making cs high

#spiread Reading the 3 bytes from buffer1

Reset Porte.0 -----------making cs low

Spdr = 212 ----------- opcode D4H for buffer1

Spdr = 0 -------- first 8 bits don't care
Spdr = 0 ------- 9 to 14 bits don't care & 2 add. bit
Spdr = 0 ------ last 8 address bits as 0
Spdr = 5 -----Additional don't care byte as 5
R(1) = Spdr --- Reading data byte 1
R(2) = Spdr --- Reading data byte 2
R(3) = Spdr--- Reading data byte 3

Set Porte.0-----------making cs high

end

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you search hard you'll find my AT45 routines here. But here's a short excerpt illustrating commands 0x84 and 0xD4:

//===============================================================================================
void
dflash_start_write_active(
	void
){
	// this send the command that prepares the DF to receive the next 264 bytes to the active buffer
	// assumes chip select and SPI_enable already set
	
	if (BIT_active == 0) {
		write_SPI(0x84); // make buffer 1 active
	}
	else {
		write_SPI(0x87); // make buffer 2 active
	}
	write_SPI(0); // always start writing at beginning of the 264 / 528 bytes
	write_SPI(0);
	write_SPI(0);
	gl.buf_ptr = 0; //this will be used to count the bytes output to the buffer
}

void
dflash_write_byte(
	uint8_t byte
){
	write_SPI(byte); // byte value out to active buffer
	gl.buf_ptr++;

#ifdef FLASH_528
	if (gl.buf_ptr == 528) { // the current buffer is full - force it to be written
#else
	if (gl.buf_ptr == 264) { // the current buffer is full - force it to be written
#endif
	    dflash_flush_buffer();

	    dflash_chip_select(); //and then get ready for the next command
	    if (BIT_active == 0) { // toggle the active buffer for the receipt of the next 264 bytes
	        BIT_active = 1;
	    }
	    else {
	        BIT_active = 0;
	    }
	    dflash_start_write_active(); // make the "other" buffer the active receive buffer and reset the 264 byte counter
	}
}

void
dflash_flush_buffer(
	void
){
	uint32_t addr = gl.df_page;

#ifdef FLASH_528
	addr = addr << 10; // turn page address into ppp+bbb address with bbb=0
#else
	addr = addr << 9; // turn page address into ppp+bbb address with bbb=0
#endif

	dflash_chip_unselect(); // stop current buffer write sequence
	dflash_chip_select(); //start new command write
	if (BIT_active == 0) {
	    write_SPI(0x83); // write buffer 1 to main memory with erase
	}
	else {
	    write_SPI(0x86); // write buffer 2 to main memory with erase
	}
#ifdef FLASH_528
	write_SPI((addr >> 16) & 0x3F);
#else
	write_SPI((addr >> 16) & 0x07);
#endif
	write_SPI((addr >> 8) & 0xFF); // only the page bits here relevant - byte offset bits ignored (but sent to pad)
	write_SPI( addr & 0xFF);
	
	gl.df_page++; //step on for the next buffer write

	dflash_chip_unselect(); // kick off the commit of buffer to main memory array
}
void
dump_flash(
	void
){
	uint8_t    i,j;
	uint8_t byte;
	uint16_t  page = gl.df_page;
	uint32_t disp_addr;
	uint8_t out_buf[10];
	uint8_t char_buf[17];

#ifdef FLASH_528
	disp_addr = page * 528L;
#else
	disp_addr = page * 264L;
#endif

   	SPI_enable();

	dflash_chip_select();

	write_SPI(0xE8); //continuous array read command
#ifdef FLASH_528
	write_SPI((page >> 6) & 0x3F); //send page number then byte offset = 0
	write_SPI((page &0x3F) << 2);
#else
	write_SPI((page >> 7) & 0x07); //send page number then byte offset = 0
	write_SPI((page &0x7F) << 1);
#endif
	write_SPI(0);

	write_SPI( 0 ); //four don't care bytes to start read process
	write_SPI( 0 );
	write_SPI( 0 );
	write_SPI( 0 );

	send_str_P(PSTR ("Page: "));
	n_to_asc(out_buf, page, 4);
	send_str(out_buf);
	send_crlf();

#ifdef FLASH_528
	for (j = 0; j < 33; j++) { // output 33 lines
#else
	for (j = 0; j < 17; j++) { // output 16 + 0.5 lines
#endif
	    n_to_asc(out_buf, disp_addr, 8);
	    send_str(out_buf);
	    send_str_P(PSTR (": "));
	    for (i=0; i<16; i++) { // 16 bytes per line
			write_SPI( 0xF5 ); // write dummy byte to device so it clocks one byte into SPDR
			byte = SPDR; // and collect it
	        n_to_asc(out_buf, byte, 2);
	        if ((byte >= ' ') && (byte < 0x7f)) {
	            char_buf[i] = byte;
	        }
	        else {
	            char_buf[i] = '.';
	        }
	        send_str(out_buf);
	        send_space();
#ifndef FLASH_528
		// this stuff makes the last line just 8 bytes - only needed for 264 pages, not 528
	        if ((j == 16) && (i == 7)) {
	            unsigned char k;

	            for (k=0; k<24; k++) {
	                send_space();
	            }
	            char_buf[8] = '\0';
	            break;
	        }
#endif
	    }
	    char_buf[16] = '\0';
	    send_str(char_buf);
	    send_crlf();
	    disp_addr += 16;
	}

 	dflash_chip_unselect();

    SPI_disable();
}