Simple read/write API for the W25X40BV SPI Flash IC wiki: http://mbed.org/cookbook/W25X40BV

Committer:
jyam
Date:
Mon Mar 26 04:30:32 2012 +0000
Revision:
3:6e3c0b23dc6e
Parent:
2:2b0daec9f8c0
now a derived class of SPI from \"mbedh.h\" (can access SPI\s public member functions; more flexible);

modified W25X40BV API read() to readStream() and write() to writeStream() to avoid overwriting SPI\s write() function (and it is now a more descriptive function name)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jyam 1:cd41b8e3fdad 1 // W25X40BV.h
jyam 1:cd41b8e3fdad 2
jyam 1:cd41b8e3fdad 3 #ifndef W25X40BV_H
jyam 1:cd41b8e3fdad 4 #define W25X40BV_H
jyam 1:cd41b8e3fdad 5
jyam 1:cd41b8e3fdad 6 #include "mbed.h"
jyam 1:cd41b8e3fdad 7
jyam 1:cd41b8e3fdad 8 #define SPI_FREQ 1000000
jyam 1:cd41b8e3fdad 9 #define SPI_MODE 0
jyam 1:cd41b8e3fdad 10 #define SPI_NBIT 8
jyam 1:cd41b8e3fdad 11
jyam 1:cd41b8e3fdad 12 #define WE_INST 0x06
jyam 1:cd41b8e3fdad 13 #define WD_INST 0x04
jyam 1:cd41b8e3fdad 14 #define R_INST 0x03
jyam 1:cd41b8e3fdad 15 #define W_INST 0x02
jyam 1:cd41b8e3fdad 16 #define C_ERASE_INST 0x60
jyam 1:cd41b8e3fdad 17
jyam 1:cd41b8e3fdad 18 #define DUMMY_ADDR 0x00
jyam 1:cd41b8e3fdad 19 #define WAIT_TIME 1
jyam 1:cd41b8e3fdad 20
jyam 1:cd41b8e3fdad 21 #define ADDR_BMASK2 0x00ff0000
jyam 1:cd41b8e3fdad 22 #define ADDR_BMASK1 0x0000ff00
jyam 1:cd41b8e3fdad 23 #define ADDR_BMASK0 0x000000ff
jyam 1:cd41b8e3fdad 24
jyam 1:cd41b8e3fdad 25 #define ADDR_BSHIFT2 16
jyam 1:cd41b8e3fdad 26 #define ADDR_BSHIFT1 8
jyam 1:cd41b8e3fdad 27 #define ADDR_BSHIFT0 0
jyam 1:cd41b8e3fdad 28
jyam 3:6e3c0b23dc6e 29 class W25X40BV: public SPI {
jyam 1:cd41b8e3fdad 30 public:
jyam 1:cd41b8e3fdad 31 W25X40BV(PinName mosi, PinName miso, PinName sclk, PinName cs);
jyam 1:cd41b8e3fdad 32
jyam 3:6e3c0b23dc6e 33 int readByte(int addr); // takes a 24-bit (3 bytes) address and returns the data (1 byte) at that location
jyam 3:6e3c0b23dc6e 34 int readByte(int a2, int a1, int a0); // takes the address in 3 separate bytes A[23,16], A[15,8], A[7,0]
jyam 3:6e3c0b23dc6e 35 void readStream(int addr, char* buf, int count); // takes a 24-bit address, reads count bytes, and stores results in buf
jyam 1:cd41b8e3fdad 36
jyam 3:6e3c0b23dc6e 37 void writeByte(int addr, int data); // takes a 24-bit (3 bytes) address and a byte of data to write at that location
jyam 3:6e3c0b23dc6e 38 void writeByte(int a2, int a1, int a0, int data); // takes the address in 3 separate bytes A[23,16], A[15,8], A[7,0]
jyam 3:6e3c0b23dc6e 39 void writeStream(int addr, char* buf, int count); // write count bytes of data from buf to memory, starting at addr
jyam 1:cd41b8e3fdad 40
jyam 3:6e3c0b23dc6e 41 void chipErase(); // erase all data on chip
jyam 1:cd41b8e3fdad 42
jyam 1:cd41b8e3fdad 43 private:
jyam 3:6e3c0b23dc6e 44 void writeEnable(); // write enable
jyam 3:6e3c0b23dc6e 45 void writeDisable(); // write disable
jyam 3:6e3c0b23dc6e 46 void chipEnable(); // chip enable
jyam 3:6e3c0b23dc6e 47 void chipDisable(); // chip disable
jyam 1:cd41b8e3fdad 48
jyam 3:6e3c0b23dc6e 49 // SPI _spi;
jyam 1:cd41b8e3fdad 50 DigitalOut _cs;
jyam 1:cd41b8e3fdad 51 };
jyam 1:cd41b8e3fdad 52
jyam 0:9de212faf980 53 #endif