Winbond W25Q32 SPI library. Tested on Winbond W74M32 embedded on Hani-IoT. Based on https://os.mbed.com/users/perezalvarezhi/code/W25Q80BV/
W25Q32.h@0:cc8118fdcc22, 2020-01-08 (annotated)
- Committer:
- Pawel Zarembski
- Date:
- Wed Jan 08 11:13:52 2020 +0100
- Revision:
- 0:cc8118fdcc22
- Child:
- 1:d3928deefb53
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Pawel Zarembski |
0:cc8118fdcc22 | 1 | #ifndef W25Q32_H |
Pawel Zarembski |
0:cc8118fdcc22 | 2 | #define W25Q32_H |
Pawel Zarembski |
0:cc8118fdcc22 | 3 | |
Pawel Zarembski |
0:cc8118fdcc22 | 4 | #include "mbed.h" |
Pawel Zarembski |
0:cc8118fdcc22 | 5 | |
Pawel Zarembski |
0:cc8118fdcc22 | 6 | #define SPI_FREQ 1000000 |
Pawel Zarembski |
0:cc8118fdcc22 | 7 | #define SPI_MODE 0 |
Pawel Zarembski |
0:cc8118fdcc22 | 8 | #define SPI_NBIT 8 |
Pawel Zarembski |
0:cc8118fdcc22 | 9 | |
Pawel Zarembski |
0:cc8118fdcc22 | 10 | #define WRITE_ENABLE 0x06 |
Pawel Zarembski |
0:cc8118fdcc22 | 11 | #define WRITE_DISABLE 0x04 |
Pawel Zarembski |
0:cc8118fdcc22 | 12 | #define READ_DATA 0x03 |
Pawel Zarembski |
0:cc8118fdcc22 | 13 | #define WRITE_DATA 0x02 |
Pawel Zarembski |
0:cc8118fdcc22 | 14 | #define CHIP_ERASE 0x60 |
Pawel Zarembski |
0:cc8118fdcc22 | 15 | #define DEV_ID 0x90 |
Pawel Zarembski |
0:cc8118fdcc22 | 16 | |
Pawel Zarembski |
0:cc8118fdcc22 | 17 | #define DUMMY_ADDR 0x00 |
Pawel Zarembski |
0:cc8118fdcc22 | 18 | #define WAIT_TIME 1000 |
Pawel Zarembski |
0:cc8118fdcc22 | 19 | |
Pawel Zarembski |
0:cc8118fdcc22 | 20 | #define ADDR_BMASK2 0x00ff0000 |
Pawel Zarembski |
0:cc8118fdcc22 | 21 | #define ADDR_BMASK1 0x0000ff00 |
Pawel Zarembski |
0:cc8118fdcc22 | 22 | #define ADDR_BMASK0 0x000000ff |
Pawel Zarembski |
0:cc8118fdcc22 | 23 | |
Pawel Zarembski |
0:cc8118fdcc22 | 24 | #define ADDR_BSHIFT2 16 |
Pawel Zarembski |
0:cc8118fdcc22 | 25 | #define ADDR_BSHIFT1 8 |
Pawel Zarembski |
0:cc8118fdcc22 | 26 | #define ADDR_BSHIFT0 0 |
Pawel Zarembski |
0:cc8118fdcc22 | 27 | |
Pawel Zarembski |
0:cc8118fdcc22 | 28 | class W25Q80BV { |
Pawel Zarembski |
0:cc8118fdcc22 | 29 | public: |
Pawel Zarembski |
0:cc8118fdcc22 | 30 | W25Q80BV(PinName mosi, PinName miso, PinName sclk, PinName cs); |
Pawel Zarembski |
0:cc8118fdcc22 | 31 | |
Pawel Zarembski |
0:cc8118fdcc22 | 32 | int readByte(int addr); // takes a 24-bit (3 bytes) address and returns the data (1 byte) at that location |
Pawel Zarembski |
0:cc8118fdcc22 | 33 | int readByte(int a2, int a1, int a0); // takes the address in 3 separate bytes A[23,16], A[15,8], A[7,0] |
Pawel Zarembski |
0:cc8118fdcc22 | 34 | void readStream(int addr, char* buf, int count); // takes a 24-bit address, reads count bytes, and stores results in buf |
Pawel Zarembski |
0:cc8118fdcc22 | 35 | |
Pawel Zarembski |
0:cc8118fdcc22 | 36 | void writeByte(int addr, int data); // takes a 24-bit (3 bytes) address and a byte of data to write at that location |
Pawel Zarembski |
0:cc8118fdcc22 | 37 | 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] |
Pawel Zarembski |
0:cc8118fdcc22 | 38 | void writeStream(int addr, char* buf, int count); // write count bytes of data from buf to memory, starting at addr |
Pawel Zarembski |
0:cc8118fdcc22 | 39 | |
Pawel Zarembski |
0:cc8118fdcc22 | 40 | void chipErase(); // erase all data on chip |
Pawel Zarembski |
0:cc8118fdcc22 | 41 | int getMID( ) ; // get the manufacturer ID |
Pawel Zarembski |
0:cc8118fdcc22 | 42 | |
Pawel Zarembski |
0:cc8118fdcc22 | 43 | private: |
Pawel Zarembski |
0:cc8118fdcc22 | 44 | void writeEnable(); // write enable |
Pawel Zarembski |
0:cc8118fdcc22 | 45 | void writeDisable(); // write disable |
Pawel Zarembski |
0:cc8118fdcc22 | 46 | void chipEnable(); // chip enable |
Pawel Zarembski |
0:cc8118fdcc22 | 47 | void chipDisable(); // chip disable |
Pawel Zarembski |
0:cc8118fdcc22 | 48 | int writeSPI(int data); // software SPI write |
Pawel Zarembski |
0:cc8118fdcc22 | 49 | |
Pawel Zarembski |
0:cc8118fdcc22 | 50 | // SPI inputs/outputs |
Pawel Zarembski |
0:cc8118fdcc22 | 51 | DigitalOut _cs; |
Pawel Zarembski |
0:cc8118fdcc22 | 52 | DigitalOut _mosi; |
Pawel Zarembski |
0:cc8118fdcc22 | 53 | DigitalIn _miso; |
Pawel Zarembski |
0:cc8118fdcc22 | 54 | DigitalOut _sclk; |
Pawel Zarembski |
0:cc8118fdcc22 | 55 | }; |
Pawel Zarembski |
0:cc8118fdcc22 | 56 | |
Pawel Zarembski |
0:cc8118fdcc22 | 57 | #endif |