Shows how to use a display and the onboard SD Card. Requires a display module with an adapter

Dependencies:   DmTftLibrary SDFileSystem mbed

Committer:
displaymodule
Date:
Mon Sep 01 11:06:18 2014 +0000
Revision:
6:592bba211e38
Parent:
0:ee27d4c12433
Removed dependency on mbed-src for LPC1549

Who changed what in which revision?

UserRevisionLine numberNew contents of line
displaymodule 0:ee27d4c12433 1 // W25Q16BV.cpp
displaymodule 0:ee27d4c12433 2
displaymodule 0:ee27d4c12433 3 #include"W25Q16BV.h"
displaymodule 0:ee27d4c12433 4
displaymodule 0:ee27d4c12433 5 // CONSTRUCTOR
displaymodule 0:ee27d4c12433 6 W25Q16BV::W25Q16BV(PinName mosi, PinName miso, PinName sclk, PinName cs) : _spi(mosi, miso, sclk), _cs(cs) {
displaymodule 0:ee27d4c12433 7 _spi.format(SPI_NBIT, SPI_MODE);
displaymodule 0:ee27d4c12433 8 _spi.frequency(SPI_FREQ);
displaymodule 0:ee27d4c12433 9 chipDisable();
displaymodule 0:ee27d4c12433 10
displaymodule 0:ee27d4c12433 11 exitDeepPowerDown();
displaymodule 0:ee27d4c12433 12 // The SPI Flash cannot safely be accessed the first 1-10 ms after power ON
displaymodule 0:ee27d4c12433 13 // wait_us(WAIT_US_TPUW);
displaymodule 0:ee27d4c12433 14 }
displaymodule 0:ee27d4c12433 15
displaymodule 0:ee27d4c12433 16
displaymodule 0:ee27d4c12433 17 // READING
displaymodule 0:ee27d4c12433 18 int W25Q16BV::readByte(int addr) {
displaymodule 0:ee27d4c12433 19 chipEnable();
displaymodule 0:ee27d4c12433 20 _spi.write(R_INST);
displaymodule 0:ee27d4c12433 21 _spi.write((addr >> 16) & 0xff);
displaymodule 0:ee27d4c12433 22 _spi.write((addr >> 8) & 0xff);
displaymodule 0:ee27d4c12433 23 _spi.write((addr ) & 0xff);
displaymodule 0:ee27d4c12433 24 int response = _spi.write(DUMMY_ADDR);
displaymodule 0:ee27d4c12433 25 chipDisable();
displaymodule 0:ee27d4c12433 26 return response;
displaymodule 0:ee27d4c12433 27 }
displaymodule 0:ee27d4c12433 28 int W25Q16BV::readByte(int a2, int a1, int a0) {
displaymodule 0:ee27d4c12433 29 chipEnable();
displaymodule 0:ee27d4c12433 30 _spi.write(R_INST);
displaymodule 0:ee27d4c12433 31 _spi.write(a2);
displaymodule 0:ee27d4c12433 32 _spi.write(a1);
displaymodule 0:ee27d4c12433 33 _spi.write(a0);
displaymodule 0:ee27d4c12433 34 int response = _spi.write(DUMMY_ADDR);
displaymodule 0:ee27d4c12433 35 chipDisable();
displaymodule 0:ee27d4c12433 36 return response;
displaymodule 0:ee27d4c12433 37 }
displaymodule 0:ee27d4c12433 38 void W25Q16BV::readStream(int addr, char* buf, int count) {
displaymodule 0:ee27d4c12433 39 if (count < 1)
displaymodule 0:ee27d4c12433 40 return;
displaymodule 0:ee27d4c12433 41 chipEnable();
displaymodule 0:ee27d4c12433 42 _spi.write(R_INST);
displaymodule 0:ee27d4c12433 43 _spi.write((addr >> 16) & 0xff);
displaymodule 0:ee27d4c12433 44 _spi.write((addr >> 8) & 0xff);
displaymodule 0:ee27d4c12433 45 _spi.write((addr ) & 0xff);
displaymodule 0:ee27d4c12433 46 for (int i = 0; i < count; i++)
displaymodule 0:ee27d4c12433 47 buf[i] = _spi.write(DUMMY_ADDR);
displaymodule 0:ee27d4c12433 48 chipDisable();
displaymodule 0:ee27d4c12433 49 }
displaymodule 0:ee27d4c12433 50 void W25Q16BV::readJEDEC(uint8_t* manId, uint8_t* memType, uint8_t* cap)
displaymodule 0:ee27d4c12433 51 {
displaymodule 0:ee27d4c12433 52 chipEnable();
displaymodule 0:ee27d4c12433 53 _spi.write(JDEC_INST);
displaymodule 0:ee27d4c12433 54 *manId = _spi.write(DUMMY_ADDR);
displaymodule 0:ee27d4c12433 55 *memType = _spi.write(DUMMY_ADDR);
displaymodule 0:ee27d4c12433 56 *cap = _spi.write(DUMMY_ADDR);
displaymodule 0:ee27d4c12433 57 chipDisable();
displaymodule 0:ee27d4c12433 58 }
displaymodule 0:ee27d4c12433 59 uint8_t W25Q16BV::readStatus1()
displaymodule 0:ee27d4c12433 60 {
displaymodule 0:ee27d4c12433 61 uint8_t status;
displaymodule 0:ee27d4c12433 62 chipEnable();
displaymodule 0:ee27d4c12433 63 _spi.write(STATUS1_INST);
displaymodule 0:ee27d4c12433 64 status = _spi.write(DUMMY_ADDR);
displaymodule 0:ee27d4c12433 65 chipDisable();
displaymodule 0:ee27d4c12433 66 return status;
displaymodule 0:ee27d4c12433 67 }
displaymodule 0:ee27d4c12433 68 uint8_t W25Q16BV::readStatus2()
displaymodule 0:ee27d4c12433 69 {
displaymodule 0:ee27d4c12433 70 uint8_t status;
displaymodule 0:ee27d4c12433 71 chipEnable();
displaymodule 0:ee27d4c12433 72 _spi.write(STATUS2_INST);
displaymodule 0:ee27d4c12433 73 status = _spi.write(DUMMY_ADDR);
displaymodule 0:ee27d4c12433 74 chipDisable();
displaymodule 0:ee27d4c12433 75 return status;
displaymodule 0:ee27d4c12433 76 }
displaymodule 0:ee27d4c12433 77
displaymodule 0:ee27d4c12433 78 // WRITING
displaymodule 0:ee27d4c12433 79 void W25Q16BV::writeByte(int addr, int data) {
displaymodule 0:ee27d4c12433 80 writeEnable();
displaymodule 0:ee27d4c12433 81 chipEnable();
displaymodule 0:ee27d4c12433 82 _spi.write(W_INST);
displaymodule 0:ee27d4c12433 83 _spi.write((addr >> 16) & 0xff);
displaymodule 0:ee27d4c12433 84 _spi.write((addr >> 8) & 0xff);
displaymodule 0:ee27d4c12433 85 _spi.write((addr ) & 0xff);
displaymodule 0:ee27d4c12433 86 _spi.write(data);
displaymodule 0:ee27d4c12433 87 chipDisable();
displaymodule 0:ee27d4c12433 88 writeDisable();
displaymodule 0:ee27d4c12433 89 // wait_us(WAIT_US_TBP);
displaymodule 0:ee27d4c12433 90 waitWhileBusy();
displaymodule 0:ee27d4c12433 91 }
displaymodule 0:ee27d4c12433 92 void W25Q16BV::writeByte(int a2, int a1, int a0, int data) {
displaymodule 0:ee27d4c12433 93 writeEnable();
displaymodule 0:ee27d4c12433 94 chipEnable();
displaymodule 0:ee27d4c12433 95 _spi.write(W_INST);
displaymodule 0:ee27d4c12433 96 _spi.write(a2);
displaymodule 0:ee27d4c12433 97 _spi.write(a1);
displaymodule 0:ee27d4c12433 98 _spi.write(a0);
displaymodule 0:ee27d4c12433 99 _spi.write(data);
displaymodule 0:ee27d4c12433 100 chipDisable();
displaymodule 0:ee27d4c12433 101 writeDisable();
displaymodule 0:ee27d4c12433 102 // wait_us(WAIT_US_TBP);
displaymodule 0:ee27d4c12433 103 waitWhileBusy();
displaymodule 0:ee27d4c12433 104 }
displaymodule 0:ee27d4c12433 105 #if 0
displaymodule 0:ee27d4c12433 106 void W25Q16BV::writeStream(int addr, char* buf, int count) {
displaymodule 0:ee27d4c12433 107 if (count < 1)
displaymodule 0:ee27d4c12433 108 return;
displaymodule 0:ee27d4c12433 109 writeEnable();
displaymodule 0:ee27d4c12433 110 chipEnable();
displaymodule 0:ee27d4c12433 111 _spi.write(W_INST);
displaymodule 0:ee27d4c12433 112 _spi.write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
displaymodule 0:ee27d4c12433 113 _spi.write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
displaymodule 0:ee27d4c12433 114 _spi.write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
displaymodule 0:ee27d4c12433 115 for (int i = 0; i < count; i++)
displaymodule 0:ee27d4c12433 116 _spi.write(buf[i]);
displaymodule 0:ee27d4c12433 117 chipDisable();
displaymodule 0:ee27d4c12433 118 writeDisable();
displaymodule 0:ee27d4c12433 119 wait(WAIT_TIME_MS);
displaymodule 0:ee27d4c12433 120 }
displaymodule 0:ee27d4c12433 121 #else
displaymodule 0:ee27d4c12433 122 void W25Q16BV::writeStream(int addr, char* buf, int count)
displaymodule 0:ee27d4c12433 123 {
displaymodule 0:ee27d4c12433 124 int left = count;
displaymodule 0:ee27d4c12433 125 int offset = 0;
displaymodule 0:ee27d4c12433 126 int len = 0;
displaymodule 0:ee27d4c12433 127
displaymodule 0:ee27d4c12433 128 if (count < 1) {
displaymodule 0:ee27d4c12433 129 return;
displaymodule 0:ee27d4c12433 130 }
displaymodule 0:ee27d4c12433 131
displaymodule 0:ee27d4c12433 132 // find length of first page write
displaymodule 0:ee27d4c12433 133 if ((addr / PAGE_SIZE) != ((addr + count) / PAGE_SIZE)) {
displaymodule 0:ee27d4c12433 134 //spans across at least one boundary
displaymodule 0:ee27d4c12433 135 len = PAGE_SIZE - (addr % PAGE_SIZE);
displaymodule 0:ee27d4c12433 136 } else {
displaymodule 0:ee27d4c12433 137 // ends inside same page => use normal length
displaymodule 0:ee27d4c12433 138 len = count % PAGE_SIZE;
displaymodule 0:ee27d4c12433 139 }
displaymodule 0:ee27d4c12433 140
displaymodule 0:ee27d4c12433 141 //break up large write operation into several page write operations
displaymodule 0:ee27d4c12433 142 while (left > 0) {
displaymodule 0:ee27d4c12433 143 writeEnable();
displaymodule 0:ee27d4c12433 144 chipEnable();
displaymodule 0:ee27d4c12433 145 _spi.write(W_INST);
displaymodule 0:ee27d4c12433 146 _spi.write(((addr + offset) >> 16) & 0xff);
displaymodule 0:ee27d4c12433 147 _spi.write(((addr + offset) >> 8) & 0xff);
displaymodule 0:ee27d4c12433 148 _spi.write(((addr + offset) ) & 0xff);
displaymodule 0:ee27d4c12433 149 for (int i = 0; i < len; i++) {
displaymodule 0:ee27d4c12433 150 _spi.write(buf[offset + i]);
displaymodule 0:ee27d4c12433 151 }
displaymodule 0:ee27d4c12433 152 chipDisable();
displaymodule 0:ee27d4c12433 153 //writeDisable();
displaymodule 0:ee27d4c12433 154
displaymodule 0:ee27d4c12433 155 offset += len;
displaymodule 0:ee27d4c12433 156 left -= len;
displaymodule 0:ee27d4c12433 157 len = (left < PAGE_SIZE) ? left : PAGE_SIZE;
displaymodule 0:ee27d4c12433 158
displaymodule 0:ee27d4c12433 159 //wait_us(WAIT_US_TPP);
displaymodule 0:ee27d4c12433 160 waitWhileBusy();
displaymodule 0:ee27d4c12433 161 }
displaymodule 0:ee27d4c12433 162 }
displaymodule 0:ee27d4c12433 163 #endif
displaymodule 0:ee27d4c12433 164
displaymodule 0:ee27d4c12433 165 //ERASING
displaymodule 0:ee27d4c12433 166 void W25Q16BV::chipErase() {
displaymodule 0:ee27d4c12433 167 writeEnable();
displaymodule 0:ee27d4c12433 168 chipEnable();
displaymodule 0:ee27d4c12433 169 _spi.write(C_ERASE_INST);
displaymodule 0:ee27d4c12433 170 chipDisable();
displaymodule 0:ee27d4c12433 171 // writeDisable();
displaymodule 0:ee27d4c12433 172 // wait_us(WAIT_US_TCE);
displaymodule 0:ee27d4c12433 173 waitWhileBusy();
displaymodule 0:ee27d4c12433 174 }
displaymodule 0:ee27d4c12433 175 bool W25Q16BV::blockErase(int startBlock, int num) {
displaymodule 0:ee27d4c12433 176 if ((num < 1) || (startBlock < 0) || ((startBlock+num) > NUM_64KB_BLOCKS)) {
displaymodule 0:ee27d4c12433 177 return false;
displaymodule 0:ee27d4c12433 178 }
displaymodule 0:ee27d4c12433 179 for (int i = 0; i < num; i++) {
displaymodule 0:ee27d4c12433 180 writeEnable();
displaymodule 0:ee27d4c12433 181 chipEnable();
displaymodule 0:ee27d4c12433 182 _spi.write(B_ERASE_INST);
displaymodule 0:ee27d4c12433 183 _spi.write(startBlock + i);
displaymodule 0:ee27d4c12433 184 _spi.write(0);
displaymodule 0:ee27d4c12433 185 _spi.write(0);
displaymodule 0:ee27d4c12433 186 chipDisable();
displaymodule 0:ee27d4c12433 187 // writeDisable();
displaymodule 0:ee27d4c12433 188 // wait_us(WAIT_US_TBE);
displaymodule 0:ee27d4c12433 189 waitWhileBusy();
displaymodule 0:ee27d4c12433 190 }
displaymodule 0:ee27d4c12433 191 return true;
displaymodule 0:ee27d4c12433 192 }
displaymodule 0:ee27d4c12433 193 bool W25Q16BV::sectorErase(int startSector, int num) {
displaymodule 0:ee27d4c12433 194 if ((num < 1) || (startSector < 0) || ((startSector+num) > NUM_SECTORS)) {
displaymodule 0:ee27d4c12433 195 return false;
displaymodule 0:ee27d4c12433 196 }
displaymodule 0:ee27d4c12433 197 int addr = startSector * SECTOR_SIZE;
displaymodule 0:ee27d4c12433 198 for (int i = 0; i < num; i++) {
displaymodule 0:ee27d4c12433 199 writeEnable();
displaymodule 0:ee27d4c12433 200 chipEnable();
displaymodule 0:ee27d4c12433 201 _spi.write(S_ERASE_INST);
displaymodule 0:ee27d4c12433 202 _spi.write((addr >> 16) & 0xff);
displaymodule 0:ee27d4c12433 203 _spi.write((addr >> 8) & 0xff);
displaymodule 0:ee27d4c12433 204 _spi.write((addr ) & 0xff);
displaymodule 0:ee27d4c12433 205 chipDisable();
displaymodule 0:ee27d4c12433 206 // writeDisable();
displaymodule 0:ee27d4c12433 207 // wait_us(WAIT_US_TSE);
displaymodule 0:ee27d4c12433 208 waitWhileBusy();
displaymodule 0:ee27d4c12433 209
displaymodule 0:ee27d4c12433 210 addr += SECTOR_SIZE;
displaymodule 0:ee27d4c12433 211 }
displaymodule 0:ee27d4c12433 212 return true;
displaymodule 0:ee27d4c12433 213 }
displaymodule 0:ee27d4c12433 214
displaymodule 0:ee27d4c12433 215 // Wakeup from deep power down (default state)
displaymodule 0:ee27d4c12433 216 void W25Q16BV::exitDeepPowerDown() {
displaymodule 0:ee27d4c12433 217 chipEnable();
displaymodule 0:ee27d4c12433 218 _spi.write(POWERUP_INST);
displaymodule 0:ee27d4c12433 219 chipDisable();
displaymodule 0:ee27d4c12433 220 wait_us(WAIT_US_TRES1);
displaymodule 0:ee27d4c12433 221 }
displaymodule 0:ee27d4c12433 222
displaymodule 0:ee27d4c12433 223 void W25Q16BV::waitWhileBusy() {
displaymodule 0:ee27d4c12433 224 uint8_t status = 0;
displaymodule 0:ee27d4c12433 225 int i = 0;
displaymodule 0:ee27d4c12433 226
displaymodule 0:ee27d4c12433 227 do {
displaymodule 0:ee27d4c12433 228 for (i = 0; i < 0x2000; i++);
displaymodule 0:ee27d4c12433 229
displaymodule 0:ee27d4c12433 230 status = readStatus1();
displaymodule 0:ee27d4c12433 231 }
displaymodule 0:ee27d4c12433 232 while ((status & STATUS_1_BUSY) != 0);
displaymodule 0:ee27d4c12433 233 }
displaymodule 0:ee27d4c12433 234
displaymodule 0:ee27d4c12433 235 //ENABLE/DISABLE (private functions)
displaymodule 0:ee27d4c12433 236 void W25Q16BV::writeEnable() {
displaymodule 0:ee27d4c12433 237 chipEnable();
displaymodule 0:ee27d4c12433 238 _spi.write(WE_INST);
displaymodule 0:ee27d4c12433 239 chipDisable();
displaymodule 0:ee27d4c12433 240 }
displaymodule 0:ee27d4c12433 241 void W25Q16BV::writeDisable() {
displaymodule 0:ee27d4c12433 242 chipEnable();
displaymodule 0:ee27d4c12433 243 _spi.write(WD_INST);
displaymodule 0:ee27d4c12433 244 chipDisable();
displaymodule 0:ee27d4c12433 245 }
displaymodule 0:ee27d4c12433 246 void W25Q16BV::chipEnable() {
displaymodule 0:ee27d4c12433 247 _cs = 0;
displaymodule 0:ee27d4c12433 248 }
displaymodule 0:ee27d4c12433 249 void W25Q16BV::chipDisable() {
displaymodule 0:ee27d4c12433 250 _cs = 1;
displaymodule 0:ee27d4c12433 251 }
displaymodule 0:ee27d4c12433 252