SMILEY’S WORKSHOP ☺
to read a Flash page into a buffer.
Talking to DataFlash
Much of the DataFlash low level software
was adapted from the Martin Thomas gcc port of
the original Butterfly software available at www.
siwawi.arubi.uni-kl.de/avr_projects/. However, a
lot has been changed to fit into my evolving
coding style and we will use our SPI library.
Read the Status Register
The simplest thing we can do is read the
DataFlash Status register. This is a relatively
straightforward use of SPI. We send the opcode 0x57 for
‘read the status register’ to the DataFlash; then we send a
dummy byte while reading the byte returned which is the
status byte. Figure 6 shows the bits in the status register. Bit
7: RDY/BUSY reads 1 if the device is ready to write a buffer
page to a Flash page and 0 if it is busy. Bit 6: COMP is for
the most recent Main Memory Page to Buffer Compare
operation; it is 0 if they are the same and 1 if there is any
difference. Bits 5–2 provide device memory size
information. The lowest two bits aren’t used.
Our df_read_status function reads the status register
and uses the device information bits with a lookup table
to determine the value of the df_page_bits and df_page_size
variables that may be used in other functions.
■ FIGURE 5. DataFlash block diagram.
void show_status()
{
char status = 0;
char array[] = {0,0,0,0,0,0,0,0,0,0,0};
// Get the status
status = df_read_status();
uart_send_string(“\rStatus:\r”);
show_bits(status);
uart_send_string(“\rdf_page_bits: “);
itoa((int16_t)df_page_bits,array,10);
uart_send_string(array);
uart_send_string(“\rdf_page_size: “);
itoa((int16_t)df_page_size,array,10);
uart_send_string(array);
uart_send_byte(‘\r’);
}
uint8_t df_read_status (void)
{
uint8_t result,index_copy;
// Toggle SS to reset DataFlash command
// decoder
spi0_hard_SS();
// Send opcode to read the status
// register
result = spi0_master_rw8(StatusReg);
// Send a dummy byte to receive the
// results
result = spi0_master_rw8(0x00);
// Get the size information
index_copy = ((result & 0x38) >> 3);
// Get the number of page address bits
// from the lookup table
df_page_bits = pgm_read_byte(&df_
page_bits_array[index_copy]);
// Get the size of the page (in bytes)
df_page_size = pgm_read_word(&df_
page_size_array[index_copy]);
// Return the status register value
return result;
}
Test Read/Write Status Register
and SRAM Buffers
For our first test, we will read the status register and
show the bits in the status register, along with the value of
the page bits and size variables.
■ FIGURE 6. The DataFlash status register.
■ FIGURE 7. Output on Bray’s Terminal.
December 2010 65