
Library for the SST25VF016B serial flash from Microchip (16Mbit Serial Flash).
Revision 0:1633dfa4b768, committed 2012-01-15
- Comitter:
- emmibed
- Date:
- Sun Jan 15 13:51:16 2012 +0000
- Commit message:
Changed in this revision
diff -r 000000000000 -r 1633dfa4b768 SST25VF.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SST25VF.cpp Sun Jan 15 13:51:16 2012 +0000 @@ -0,0 +1,131 @@ +/* SST25VF - drive the Microchip SST25VF Serial Flash using SPI +*/ + +#include "mbed.h" +#include "SST25VF.h" + +SST25VF::SST25VF(SPI& spi, PinName ncs) : _spi(spi), _ncs(ncs) { + + _spi.format(8,0); + _spi.frequency(SPI_FREQ); + deselect(); + unlock(); + chipErase(); + +} + +void SST25VF::select() { + _ncs = 0; +} + +void SST25VF::deselect() { + _ncs = 1; +} + +void SST25VF::wren() { + select(); + _spi.write(WREN); + deselect(); + waitShort(); +} + +void SST25VF::wrdi() { + select(); + _spi.write(WRDI); + deselect(); +} + +void SST25VF::chipErase() { + wren(); + select(); + _spi.write(CHIP_ERASE); + deselect(); + waitErase(); +} + +void SST25VF::unlock() { + select(); + _spi.write(WRITE_STATUS); + _spi.write(0); + deselect(); + waitShort(); +} + +void SST25VF::writeStatus(char status) { + select(); + _spi.write(WRITE_STATUS); + _spi.write(status); + deselect(); + waitShort(); +} + +char SST25VF::readStatus() { + select(); + _spi.write(READ_STATUS); + char result = (char) _spi.write(0); + deselect(); + return result; +} + +void SST25VF::prepareCommand(char command, long int address) { + select(); + _spi.write(command); + _spi.write((address & 0xFFFFFF) >> 16); + _spi.write((address & 0xFFFF)>> 8); + _spi.write(address & 0xFF); +} + +// write or read a single byte + +void SST25VF::write(long int address, char byte) { + wren(); + prepareCommand(WRITE, address); + _spi.write(byte); + deselect(); + waitShort(); +} + +char SST25VF::read(long int address) { + prepareCommand(READ, address); + int result = _spi.write(0); + deselect(); + return (char) result; +} + +// continuous write and read + +void SST25VF::write(long int address, char * buffer, int count) { + wren(); + // write frist two bytes + for (int i = 0; i < count-1; i=i+2) { + if (i==0) { + prepareCommand(AAIWRITE, address); + } else { + select(); + _spi.write(AAIWRITE); + } + _spi.write(buffer[i]); + _spi.write(buffer[i+1]); + deselect(); + waitShort(); + } + if ((count % 2) == 1) { // odd number of bytes + select(); + _spi.write(AAIWRITE); + _spi.write(buffer[count-1]); + _spi.write(0); // odd number of bytes, so write dummy byte to flash + deselect(); + waitShort(); + } + wrdi(); + waitShort(); +} + +void SST25VF::read(long int address, char * buffer, int count) { + prepareCommand(READ, address); + // _spi.write(0); // dummy cycle, only necessary if READ is replaced by HSREAD + for (int i = 0; i < count; i++) { + buffer[i] = _spi.write(0); + } + deselect(); +} \ No newline at end of file
diff -r 000000000000 -r 1633dfa4b768 SST25VF.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SST25VF.h Sun Jan 15 13:51:16 2012 +0000 @@ -0,0 +1,88 @@ +/** SST25VF - drive the Microchip SST25VF Serial Flash using SPI +* +* Assumes spi mode is default (8,0). +* +* You can clock the SST25VF at up to >20MHz, so it supports the mbed's maximum SPI frequency of 12MHz. +*/ + +#include "mbed.h" + +/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +* Caution: Do not write to adresses where data != 0xFF +* Data in selected adress range MUST be 0xFF (See datasheet) +* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +*/ + +#ifndef SST25VF_H +#define SST25VF_H + +#define SPI_FREQ 12000000 // 12Mhz is mbed max (one eight of periph. clock) + +#define waitShort() wait_us(10) +#define waitErase() wait_ms(50) + +// command codes for SST25VF +#define WRITE_STATUS 0x01 // called WRSR in datasheet +#define WRITE 0x02 // Byte-Program +#define READ 0x03 +#define WRDI 0x04 +#define READ_STATUS 0x05 // called RDSR +#define WREN 0x06 +//#define HSREAD 0x0B // High-Speed-Read +#define EWSR 0x50 // Enable-Write-Status-Register +#define CHIP_ERASE 0x60 +#define AAIWRITE 0xAD // word based write + +class SST25VF { +public: + /** Create an interface + * + * + * @param spi An SPI object + * @param ncs Not chip select pin - any free Digital pin will do + */ + SST25VF(SPI& spi, PinName ncs); + /** read a byte from SRAM + * @param address The address to read from + * @return the character at that address + */ + char read(long int address); + + /** read multiple bytes from SRAM into a buffer + * @param address The SRAM address to read from + * @param buffer The buffer to read into (must be big enough!) + * @param count The number of bytes to read + */ + void read(long int address, char * buffer, int count); + /** write a byte to SRAM + * @param address The address SRAM to write to + * @param byte The byte to write there + */ + void write(long int address, char byte); + /** write multiple bytes to SRAM from a buffer + * @param address The SRAM address write to + * @param buffer The buffer to write from + * @param count The number of bytes to write + */ + void write(long int address, char * buffer, int count); + + + void chipErase(); + char readStatus(); + +private: + SPI& _spi; + DigitalOut _ncs; + + void writeStatus(char status); + void prepareCommand(char command, long int address); + void select(); + void deselect(); + void wren(); + void unlock(); + void wrdi(); +}; + +#endif
diff -r 000000000000 -r 1633dfa4b768 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Jan 15 13:51:16 2012 +0000 @@ -0,0 +1,35 @@ +#include "mbed.h" +#include "SST25VF.h" + +#define w() wait_us(7) +DigitalOut sr(p21); + +SPI spi(p5,p6,p7); + +SST25VF sram(spi,p21); + +int main() { + + char buff[128]; + + wait(0.2); + + + sram.write((long) 0, 'o'); + sram.write((long)1, 'n'); + sram.write((long)2, 'e'); + sram.write((long)3, '\0'); + for (long int address = 0; address < 4; address++) { + buff[address] = sram.read(address); + } + printf("\noutput1 = %s\n", buff); + + sram.write((long)128, "Hello world!",12); // need separate address space here + + sram.read((long)128, buff, 12); + buff[12]='\0'; + + printf("output2 = %s\n", buff); + + +} \ No newline at end of file
diff -r 000000000000 -r 1633dfa4b768 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sun Jan 15 13:51:16 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/9114680c05da