SPI driver for M25P* flash devices.

Dependents:   flash-fs-example Dragonfly_Filesystem_Example Dragonfly_Low_Power_Example STM32F407VET6_SPIFlash ... more

Files at this revision

API Documentation at this revision

Comitter:
Leon Lindenfelser
Date:
Fri Jun 26 15:05:25 2020 -0500
Parent:
4:751745dd637f
Commit message:
Update to latest from Multitech git repo

1. Add timeout when waiting for WIP
2. Check 'write in progress bit' after writing the status register
3. Add ability to write the status register
4. Read identification function actually reads identification
5. Hardcode memory size as parameter in constructor
6. Move wakeup() call after CS, WP, and HOLD setup so it wakes the flash if it was sleeping

Changed in this revision

SpiFlash25.cpp Show annotated file Show diff for this revision Revisions of this file
SpiFlash25.h Show annotated file Show diff for this revision Revisions of this file
--- a/SpiFlash25.cpp	Mon Jun 08 15:52:34 2015 -0500
+++ b/SpiFlash25.cpp	Fri Jun 26 15:05:25 2020 -0500
@@ -22,14 +22,13 @@
 
 #include "SpiFlash25.h"
 
-SpiFlash25::SpiFlash25(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName W, PinName HOLD, int page_size)
+SpiFlash25::SpiFlash25(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName W, PinName HOLD, int page_size, int mem_size)
 :   _spi(mosi, miso, sclk),
     _cs(cs),
+    _mem_size(mem_size),
     _page_size(page_size)
 {
 
-    wakeup();
-
     _cs.write(1);
     _spi.format(8, 3);
     _spi.frequency(75000000);
@@ -43,14 +42,7 @@
         _hold->write(1);
     }
 
-    _cs.write(0);
-    _spi.write(READ_IDENTIFICATION);
-    _id[ID_MANUFACTURER] = _spi.write(0x00);
-    _id[ID_MEM_TYPE] = _spi.write(0x00);
-    _id[ID_MEM_SIZE] = _spi.write(0x00);
-    _cs.write(1);
-
-    _mem_size = 1 << _id[ID_MEM_SIZE];
+    wakeup();
 }
 
 void SpiFlash25::format(int bits, int mode) {
@@ -108,9 +100,25 @@
 }
 
 char* SpiFlash25::read_id() {
+    _cs.write(0);
+    _spi.write(READ_IDENTIFICATION);
+    _id[ID_MANUFACTURER] = _spi.write(0x00);
+    _id[ID_MEM_TYPE] = _spi.write(0x00);
+    _id[ID_MEM_SIZE] = _spi.write(0x00);
+    _cs.write(1);
+
     return _id;
 }
 
+void SpiFlash25::write_status(char data) {
+    enable_write();	
+    _cs.write(0);
+    _spi.write(WRITE_STATUS);
+    _spi.write(data);	
+    _cs.write(1);
+    wait_for_write(15);    
+}
+
 char SpiFlash25::read_status() {
     char status;
 
@@ -132,7 +140,7 @@
     _spi.write(low_byte(addr));
     _cs.write(1);
 
-    wait_for_write();
+    wait_for_write(3000);
 }
 
 void SpiFlash25::clear_mem() {
@@ -142,7 +150,7 @@
     _spi.write(BULK_ERASE);
     _cs.write(1);
 
-    wait_for_write();
+    wait_for_write(20000);
 }
 
 bool SpiFlash25::write_page(int addr, int len, const char* data) {
@@ -159,9 +167,8 @@
     }
 
     _cs.write(1);
-    wait_for_write();
 
-    return true;
+    return wait_for_write(5);
 }
 
 void SpiFlash25::enable_write() {
@@ -170,10 +177,12 @@
     _cs.write(1);
 }
 
-void SpiFlash25::wait_for_write() {
-    while (read_status() & STATUS_WIP) {
+bool SpiFlash25::wait_for_write(int32_t timeout_ms) {
+    timeout_ms *= 100;
+    while ((read_status() & STATUS_WIP) && (timeout_ms-- > 0)) {
         wait_us(10);
     }
+    return timeout_ms > 0;
 }
 
 void SpiFlash25::deep_power_down() {
@@ -187,3 +196,4 @@
     _spi.write(DEEP_POWER_DOWN_RELEASE);
     _cs.write(1);
 }
+
--- a/SpiFlash25.h	Mon Jun 08 15:52:34 2015 -0500
+++ b/SpiFlash25.h	Fri Jun 26 15:05:25 2020 -0500
@@ -27,7 +27,7 @@
 
 class SpiFlash25 {
     public:
-        SpiFlash25(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName W = NC, PinName HOLD = NC, int page_size = 256);
+        SpiFlash25(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName W = NC, PinName HOLD = NC, int page_size = 256, int mem_size = 2097152);
 
         /* Set the page size (default 256) */
         void set_page_size(int size);
@@ -42,6 +42,7 @@
 
         /* Read ID and status registers */
         char* read_id();
+		void write_status(char data);
         char read_status();
 
         /* Erase methods */
@@ -84,7 +85,7 @@
 
         bool write_page(int addr, int len, const char* data);
         void enable_write();
-        void wait_for_write();
+        bool wait_for_write(int32_t timeout_ms);
 
         static inline char high_byte(int addr) {
             return ((addr & 0xff0000) >> 16);