Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
6 years, 12 months ago.
SPI connection for FLASH MEMORY
Hi all!
I wanted to ask questions regarding SPI for a flash memory device. I have attached the code below.
What am I working with: Discovery Board STM32F407VG Offline MBED CLI compiler using GCC ARM toolchain SPI device: SST25VF NOR Flash Memory device (with 2MB storage)
What I have done: I have successfully read the whoami data from this flash memory, and i got a read back which is correct. However, when I wanted to store and read back the data, it is just messed up. You can see the results below:
Results of printouts
register = 0xBF 0x25 0x41 what to save is: 2 result = 255
I am stuck on how to have the result shown correctly! Actually I wanted to save some results from analog in, but now I'm trying with this int data first (which I am still stuck)
My whole code for this is shown below:
SPI for Flash Memory
#include "mbed.h"
#include "SWO.h"
SPI spi(PA_7, PA_6, PA_5); // mosi, miso, sclk
DigitalOut cs(PB_6);
SWO_Channel swo("channel");
Serial ardu(PA_2, PA_3); //tx rx
#define JEDEC_ID 0x9F //device ID
#define READ_N 0x03 //read bit
#define FULLERASE 0x60 //full chip erase
#define AAIWRITE 0xAD //auto address increment
#define WREN 0x06 //Write Enable! should be init first!
#define WRDI 0x04 //write disble
#define PROG_1dat 0x02 //write 1 data byte (8bits)
//initialization voids!
void flash_on(){ //enable chip
cs = 0;
}
void flash_off(){ //disable chip
cs = 1;
}
void flash_init() { //init of chip
spi.frequency(12000000);
spi.format(8,3); //this means: 8 bits for each command, and use 3rd spimode
flash_off();
}
void flash_test(){
flash_on();
spi.write(JEDEC_ID);
int whoami = spi.write(0);
int whoami2 = spi.write(0);
int whoami3 = spi.write(0);
ardu.printf("register = 0x%X 0x%X 0x%X \n", whoami, whoami2, whoami3);
flash_off();
}
void write_en(){
flash_on();
spi.write(WREN);
flash_off();
}
//main program!
int main(){
flash_init();
//testing flash condition
flash_test();
//Writing data!
//1st: WREN should be executed first!
write_en(); //write enable first!
flash_on();
spi.write(FULLERASE);
wait(0.5);
flash_off();
write_en();
flash_on();
spi.write(PROG_1dat);
spi.write(0x00);
int testings = 2;
swo.printf("what to save is: %d \n", testings);
spi.write(testings);
wait(0.1);
flash_off();
// Reading Data!
wait(1);
write_en();
flash_on();
spi.write(READ_N);
spi.write(0x00);
char result = spi.write(0);
flash_off();
swo.printf("\n result = %d \n", result);
}
I am assuming the mistake should be somewhere in the data type or the size of the data etc. Buuut, I am now clueless and I am really stuck on how to get this device to read and write. Can anyone help me with this issue? I would be really grateful for any kind of help!
Thank you! Stanley
1 Answer
6 years, 12 months ago.
Hi Stanley,
I'm not sure you were included sector or block erase function for memory on your code. I think you missed the erase sector(4KB) stuff before writing to some location of memory. Please read at below comment of SST25VF data sheet (SST25VF016B).
The Byte-Program instruction programs the bits in the selected byte to the desired data. The selected byte must be in the erased state (FFH) when initiating a Program operation. A Byte-Program instruction applied to a protected memory area will be ignored.
Daniel,
Hi Daniel! Thank you for your answer.
In my code, I have already erase chip (for the whole chip I suppose) is it enough? Or should I erase that sector as well?
posted by 29 Nov 2018Oh, I missed that you used 'chip erase' mode already. BTW, I compared your code and reference code(https://os.mbed.com/users/emmibed/code/SST25VF/). I think you need to try to change address size from 1byte to 3byte. please look and compare reference code. It just different address size. If I have a flash chip, I would test your code, but I don't have it. : (
posted by 30 Nov 2018Hi again Daniel!
Ahh yes, I guess so, I'm still trying to make sense about the address sizes and the data types that I will be using. But you really helped to make me realize! haha thank you so much for your help, I will try to figure it out again.
Cheers, and have a great weekend! Stanley
posted by 30 Nov 2018