Implementation of reading and writing to AT25512N-SH SPI-Serial EEPROM. Use it in your own risk. Free software.

Committer:
mjm2016
Date:
Tue Aug 13 05:35:58 2019 +0000
Revision:
0:d6075c37f0a4
Simple functions for writing and reading from SPI Serial EEPROM 512 Kbits (65,536x8)-AT25512N-SH. This might help you in understanding how the communications works with the flash memory.Please read the datasheet for further informations.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mjm2016 0:d6075c37f0a4 1 #include "mbed.h"
mjm2016 0:d6075c37f0a4 2 /**
mjm2016 0:d6075c37f0a4 3 * Writing and reading from Serial Flash memory
mjm2016 0:d6075c37f0a4 4 * AT25512N-SH
mjm2016 0:d6075c37f0a4 5 * http://ww1.microchip.com/downloads/en/DeviceDoc/AT25512-SPI-Serial-EEPROM-512-Kbits-20006218A.pdf
mjm2016 0:d6075c37f0a4 6 * Author : Mariwan jalal
mjm2016 0:d6075c37f0a4 7 */
mjm2016 0:d6075c37f0a4 8
mjm2016 0:d6075c37f0a4 9 SPI device(SPI_MOSI, SPI_MISO, SPI_SCK);
mjm2016 0:d6075c37f0a4 10
mjm2016 0:d6075c37f0a4 11 DigitalOut CS(SPI_CS);
mjm2016 0:d6075c37f0a4 12
mjm2016 0:d6075c37f0a4 13 Serial pc(USBTX, USBRX);
mjm2016 0:d6075c37f0a4 14 void read()
mjm2016 0:d6075c37f0a4 15 {
mjm2016 0:d6075c37f0a4 16 CS=1;
mjm2016 0:d6075c37f0a4 17 int v=0;
mjm2016 0:d6075c37f0a4 18 for(int i=0x200; i<0x210; i++) {
mjm2016 0:d6075c37f0a4 19 wait_ms(1);
mjm2016 0:d6075c37f0a4 20 CS=0;
mjm2016 0:d6075c37f0a4 21 device.write(0x03);
mjm2016 0:d6075c37f0a4 22 device.write((i&0xff00)>>8);
mjm2016 0:d6075c37f0a4 23 device.write(i&0xff);
mjm2016 0:d6075c37f0a4 24 v=device.write(0);///dummy
mjm2016 0:d6075c37f0a4 25 CS=1;
mjm2016 0:d6075c37f0a4 26 pc.printf("Results for i=%i : %x\n",i,v);
mjm2016 0:d6075c37f0a4 27 }
mjm2016 0:d6075c37f0a4 28
mjm2016 0:d6075c37f0a4 29 }
mjm2016 0:d6075c37f0a4 30 void write()
mjm2016 0:d6075c37f0a4 31 {
mjm2016 0:d6075c37f0a4 32 for(int i=0x200; i<0x0210; i++) {
mjm2016 0:d6075c37f0a4 33 CS=1;
mjm2016 0:d6075c37f0a4 34 CS=0;
mjm2016 0:d6075c37f0a4 35 wait_ms(1);
mjm2016 0:d6075c37f0a4 36 device.write(0x01);
mjm2016 0:d6075c37f0a4 37 device.write(0x00);
mjm2016 0:d6075c37f0a4 38 CS=1;
mjm2016 0:d6075c37f0a4 39 CS=0;
mjm2016 0:d6075c37f0a4 40 device.write(0x6);
mjm2016 0:d6075c37f0a4 41 CS=1;
mjm2016 0:d6075c37f0a4 42 CS=0;
mjm2016 0:d6075c37f0a4 43 device.write(0x02);
mjm2016 0:d6075c37f0a4 44 device.write((i&0xff00)>>8);
mjm2016 0:d6075c37f0a4 45 device.write(i&0xff);
mjm2016 0:d6075c37f0a4 46 device.write((0xDA));
mjm2016 0:d6075c37f0a4 47 CS=1;
mjm2016 0:d6075c37f0a4 48 pc.printf("Address= %x %x\n",((i&0xff00)>>8), (i&0xff));
mjm2016 0:d6075c37f0a4 49 }
mjm2016 0:d6075c37f0a4 50
mjm2016 0:d6075c37f0a4 51 }
mjm2016 0:d6075c37f0a4 52 int main()
mjm2016 0:d6075c37f0a4 53 {
mjm2016 0:d6075c37f0a4 54 pc.printf("device Started\n");
mjm2016 0:d6075c37f0a4 55 device.format(8, 0);
mjm2016 0:d6075c37f0a4 56 device.frequency(1000000);
mjm2016 0:d6075c37f0a4 57 pc.printf("START Write\n");
mjm2016 0:d6075c37f0a4 58 write();
mjm2016 0:d6075c37f0a4 59 pc.printf("START Read\n");
mjm2016 0:d6075c37f0a4 60 read();
mjm2016 0:d6075c37f0a4 61 }