The "enable write status" command has to be sent before "write status" command to unlock memory area.

Dependencies:   mbed

Fork of SST25VF by Pierre Emmibed

Committer:
emmibed
Date:
Sun Jan 15 13:51:16 2012 +0000
Revision:
0:1633dfa4b768

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emmibed 0:1633dfa4b768 1 /** SST25VF - drive the Microchip SST25VF Serial Flash using SPI
emmibed 0:1633dfa4b768 2 *
emmibed 0:1633dfa4b768 3 * Assumes spi mode is default (8,0).
emmibed 0:1633dfa4b768 4 *
emmibed 0:1633dfa4b768 5 * You can clock the SST25VF at up to >20MHz, so it supports the mbed's maximum SPI frequency of 12MHz.
emmibed 0:1633dfa4b768 6 */
emmibed 0:1633dfa4b768 7
emmibed 0:1633dfa4b768 8 #include "mbed.h"
emmibed 0:1633dfa4b768 9
emmibed 0:1633dfa4b768 10 /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
emmibed 0:1633dfa4b768 11 * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
emmibed 0:1633dfa4b768 12 * Caution: Do not write to adresses where data != 0xFF
emmibed 0:1633dfa4b768 13 * Data in selected adress range MUST be 0xFF (See datasheet)
emmibed 0:1633dfa4b768 14 * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
emmibed 0:1633dfa4b768 15 * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
emmibed 0:1633dfa4b768 16 */
emmibed 0:1633dfa4b768 17
emmibed 0:1633dfa4b768 18 #ifndef SST25VF_H
emmibed 0:1633dfa4b768 19 #define SST25VF_H
emmibed 0:1633dfa4b768 20
emmibed 0:1633dfa4b768 21 #define SPI_FREQ 12000000 // 12Mhz is mbed max (one eight of periph. clock)
emmibed 0:1633dfa4b768 22
emmibed 0:1633dfa4b768 23 #define waitShort() wait_us(10)
emmibed 0:1633dfa4b768 24 #define waitErase() wait_ms(50)
emmibed 0:1633dfa4b768 25
emmibed 0:1633dfa4b768 26 // command codes for SST25VF
emmibed 0:1633dfa4b768 27 #define WRITE_STATUS 0x01 // called WRSR in datasheet
emmibed 0:1633dfa4b768 28 #define WRITE 0x02 // Byte-Program
emmibed 0:1633dfa4b768 29 #define READ 0x03
emmibed 0:1633dfa4b768 30 #define WRDI 0x04
emmibed 0:1633dfa4b768 31 #define READ_STATUS 0x05 // called RDSR
emmibed 0:1633dfa4b768 32 #define WREN 0x06
emmibed 0:1633dfa4b768 33 //#define HSREAD 0x0B // High-Speed-Read
emmibed 0:1633dfa4b768 34 #define EWSR 0x50 // Enable-Write-Status-Register
emmibed 0:1633dfa4b768 35 #define CHIP_ERASE 0x60
emmibed 0:1633dfa4b768 36 #define AAIWRITE 0xAD // word based write
emmibed 0:1633dfa4b768 37
emmibed 0:1633dfa4b768 38 class SST25VF {
emmibed 0:1633dfa4b768 39 public:
emmibed 0:1633dfa4b768 40 /** Create an interface
emmibed 0:1633dfa4b768 41 *
emmibed 0:1633dfa4b768 42 *
emmibed 0:1633dfa4b768 43 * @param spi An SPI object
emmibed 0:1633dfa4b768 44 * @param ncs Not chip select pin - any free Digital pin will do
emmibed 0:1633dfa4b768 45 */
emmibed 0:1633dfa4b768 46 SST25VF(SPI& spi, PinName ncs);
emmibed 0:1633dfa4b768 47 /** read a byte from SRAM
emmibed 0:1633dfa4b768 48 * @param address The address to read from
emmibed 0:1633dfa4b768 49 * @return the character at that address
emmibed 0:1633dfa4b768 50 */
emmibed 0:1633dfa4b768 51 char read(long int address);
emmibed 0:1633dfa4b768 52
emmibed 0:1633dfa4b768 53 /** read multiple bytes from SRAM into a buffer
emmibed 0:1633dfa4b768 54 * @param address The SRAM address to read from
emmibed 0:1633dfa4b768 55 * @param buffer The buffer to read into (must be big enough!)
emmibed 0:1633dfa4b768 56 * @param count The number of bytes to read
emmibed 0:1633dfa4b768 57 */
emmibed 0:1633dfa4b768 58 void read(long int address, char * buffer, int count);
emmibed 0:1633dfa4b768 59 /** write a byte to SRAM
emmibed 0:1633dfa4b768 60 * @param address The address SRAM to write to
emmibed 0:1633dfa4b768 61 * @param byte The byte to write there
emmibed 0:1633dfa4b768 62 */
emmibed 0:1633dfa4b768 63 void write(long int address, char byte);
emmibed 0:1633dfa4b768 64 /** write multiple bytes to SRAM from a buffer
emmibed 0:1633dfa4b768 65 * @param address The SRAM address write to
emmibed 0:1633dfa4b768 66 * @param buffer The buffer to write from
emmibed 0:1633dfa4b768 67 * @param count The number of bytes to write
emmibed 0:1633dfa4b768 68 */
emmibed 0:1633dfa4b768 69 void write(long int address, char * buffer, int count);
emmibed 0:1633dfa4b768 70
emmibed 0:1633dfa4b768 71
emmibed 0:1633dfa4b768 72 void chipErase();
emmibed 0:1633dfa4b768 73 char readStatus();
emmibed 0:1633dfa4b768 74
emmibed 0:1633dfa4b768 75 private:
emmibed 0:1633dfa4b768 76 SPI& _spi;
emmibed 0:1633dfa4b768 77 DigitalOut _ncs;
emmibed 0:1633dfa4b768 78
emmibed 0:1633dfa4b768 79 void writeStatus(char status);
emmibed 0:1633dfa4b768 80 void prepareCommand(char command, long int address);
emmibed 0:1633dfa4b768 81 void select();
emmibed 0:1633dfa4b768 82 void deselect();
emmibed 0:1633dfa4b768 83 void wren();
emmibed 0:1633dfa4b768 84 void unlock();
emmibed 0:1633dfa4b768 85 void wrdi();
emmibed 0:1633dfa4b768 86 };
emmibed 0:1633dfa4b768 87
emmibed 0:1633dfa4b768 88 #endif