Hi All...
I am trying to get my Uno to talk to a Winbond 8MB Flash chip via SPI. I checked the leading edge/falling edge stuff and the other things that need to be considered on SPI. The code below simply tries to read the status register, set write enable, then read the status register again. It then looks for a change in the status register.
The code below executes the same whether the flash chip is hooked up or not. I'm pretty sure I have it hooked up right. Its pretty simple actually. I'm hopeing someone can help me find the issue before I decide my chip is bad. The data sheet for the chip is referenced int eh code, in a comment.
Thanks!
// Some code to play with the Winbond flash
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO
#define SPICLOCK 13//SCK
#define SLAVESELECT 10//SS
#define WP 8//Write protect
// Opcodes
// Taken from Winbond datasheet:
// http://www.winbond.com/NR/rdonlyres/591A37FF-007C-4E99-956C-F7EE4A6D9A8F/0/W25Q64BV.pdf
#define WREN 0x06 // Write Enable
#define WRDI 0x04 // Write Disable
#define RDSR1 0x05 // Read Status Register 1
#define RDSR2 0x35 // Read Status Register 2
#define WRSR 0x01 // Write Status Register
#define READ 0x03 // Read Data
#define READID 0x90 // Read Manufacturer / Device ID
#define READUNID 0x4B // Read Unique ID Number (factory set 64-bit ID) - optional feature on chip
void setup()
{
Serial.begin(9600);
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(SPICLOCK, OUTPUT);
pinMode(SLAVESELECT, OUTPUT);
pinMode(WP, OUTPUT);
digitalWrite(SLAVESELECT, HIGH); //Disable device
digitalWrite(WP, HIGH); // Disable write protect
// Set CPUs SPI status register to 0101 0000
SPCR = (1 << SPE) | (1 << MSTR);
// Read any junk data from status registers
byte clr;
clr = SPSR;
clr = SPDR;
byte readA = 0;
byte readB = 0;
byte readC = 0;
delay(100);
Serial.print("Reading status register\r\n");
digitalWrite(SLAVESELECT, LOW);
readA = spiTransfer(RDSR1);
digitalWrite(SLAVESELECT, HIGH);
delay(100);
Serial.print("Sending write enable\r\n");
digitalWrite(SLAVESELECT, LOW);
byte response = spiTransfer(WREN);
digitalWrite(SLAVESELECT, HIGH);
Serial.print("Reading status register\r\n");
delay(100);
digitalWrite(SLAVESELECT, LOW);
readB = spiTransfer(RDSR1);
digitalWrite(SLAVESELECT, HIGH);
if(readA != readB)
{
Serial.print("Values differ\r\n");
}
else
{
Serial.print("Values the same\r\n");
}
}
void loop()
{
}
byte spiTransfer(volatile byte data)
{
SPDR = data;
while (!(SPSR & (1<<SPIF)))
{
};
return SPDR;
}