Daisuke Yanagihara / Mbed 2 deprecated p_fhash_specified_sector

Dependencies:   mbed

Revision:
1:534cb807eeff
Parent:
0:29e55001d8e4
Child:
2:dd1c560b4906
--- a/main.cpp	Sun Sep 12 14:15:01 2021 +0000
+++ b/main.cpp	Wed Sep 29 05:23:28 2021 +0000
@@ -2,13 +2,17 @@
 
 SPI spi(p5,p6,p7);  //mosi,miso,sclk
 DigitalOut cs(p8);
-Serial pc(USBTX,USBRX); //tx,rx
+Serial pc(USBTX,USBRX);
+Timer t;
 
 /*Flashのコマンドを発行*/
 #define CMD_RDID 0x9f   // 
-//#define CMD_READ 0x03 // Address Read Data
+#define CMD_READ 0x03 // Address Read Data
+#define CMD_WREN 0x06 // Write Enable
+#define CMD_SE 0x20  // Sector Erase
+#define CMD_PP 0x02  // Page Programming
+#define CMD_RDSR1 0x05  // Read Status Resistor (WIP check)
 
-uint8_t d;
 
  void spi_init(){   /*spi通信設定*/
      spi.format(8,0);
@@ -16,37 +20,165 @@
      cs = 1; //初期化
      }
 
+void spi_transfer(const uint8_t* tx_buf, uint8_t* rx_buf, uint16_t length) {   // 送受信両方判断し通信実行する関数
+    pc.printf("#spi_transfer length= 0x%X\r\n",length);
+    for(uint16_t i = 0 ; i<length ; i++){
+        uint8_t data = spi.write(tx_buf != NULL ? tx_buf[i] : 0x0);  // txDataが有るならtxDataを送る,無いなら0を送る
+        if(rx_buf != NULL){                                         // もし受信データを表示したい場合
+            rx_buf[i] = data;
+            }
+        }
+    }
+ void spi_read_data(uint8_t* rx_buf, uint16_t length){  /*受信*/
+    spi_transfer(NULL, rx_buf, length);
+    }
+ void spi_write_data(const uint8_t* tx_buf, uint16_t length){  /*受信*/
+    spi_transfer(tx_buf, NULL, length);
+    }
+    
+/*Flash関数*/ 
+void flash_send_addr(uint32_t addr) {
+    const uint8_t data[] = {
+         (addr >> 16) & 0xff,   // 1のところだけ残す
+         (addr >> 8) & 0xff,
+         (addr >> 0) & 0xff,
+         };
+    for(int i=0; i<3; i++){ // addr : 3byte
+        spi.write(data[i]);
+        }
+     }
+     
 void flash_rdid(uint8_t* rx_buf, uint16_t length){  /*RDID読み出し*/   
     cs = 0;
     spi.write(CMD_RDID);
-    for(int i=0 ; i<length ; i++){
-        d = spi.write(0x00);
-        if(rx_buf != NULL){
-            rx_buf[i] = d;
-            }
-        }
+    spi_read_data(rx_buf, length);
+    cs = 1;
+    }
+
+void flash_read(uint32_t addr, uint8_t* rx_buf, uint16_t length) {  /*読み出し*/
+    cs = 0;
+    spi.write(CMD_READ);
+    flash_send_addr(addr);
+    spi_read_data(rx_buf, length);
+    cs = 1;
+    }
+
+
+void flash_wren(){  // write enable
+    cs = 0;
+    spi.write(CMD_WREN);
+    cs = 1;
+    }    
+
+void flash_se(uint32_t addr) {  // sector erase
+    cs = 0;
+    spi.write(CMD_SE);
+    flash_send_addr(addr);
+    cs = 1;
+    }
+void flash_pp(uint32_t addr, const uint8_t* wr_data, uint16_t length) {   // Page Program
+    cs = 0;
+    spi.write(CMD_PP);
+    flash_send_addr(addr);
+    spi_write_data(wr_data, length);
     cs = 1;
     }
 
-void data_debug_printf(const uint8_t* data, uint16_t bytes){ //配列を表示
+void flash_rdsr(uint8_t* rx_buf, uint16_t length) {
+    cs = 0;
+    spi.write(CMD_RDSR1);
+    spi_read_data(rx_buf, length);  // rx_bufにRDSRの値が入っている
+    cs = 1;    
+    
+    }
+
+uint8_t flash_is_wip() { // (WIP)Write In Progress(書込み中)かどうか確認
+    uint8_t data;   //❓
+    flash_rdsr(&data, 0x01);    // 1byte(StatusRegister情報)
+    return(data & 0x01);    //WIP // ビット演算_&
+    }
+    
+    
+void data_debug_print(const uint8_t* data, uint16_t bytes){ //配列を表示
+    pc.printf("+0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F\r\n");
     pc.printf("-----------------------------------------------\r\n");
-    for(int i=0; i<bytes; i++){
-        pc.printf("[%d]:%d\r\n",i,data[i]);
+    uint16_t depth = ((bytes - 1) >> 4) + 1;
+    for(uint16_t j = 0 ; j < depth ; ++j){      //depthは何行❓
+        for(uint16_t i = 0 ; i < 16 ; ++i){
+            uint8_t d = data[i + (j << 4)];
+            pc.printf("%X", (d >> 4) & 0x0f);
+            pc.printf("%X", (d >> 0) & 0x0f);
+            pc.printf(" ");
+            }
+        pc.printf("\r\n");
         }
+    pc.printf("\r\n");
     }
 
+/********************  main  *********************/
 int main() {
+    uint8_t tx_buf[256];
     uint8_t rx_buf[256];    // 受信バッファ用意
- 
+    uint32_t addr = 0x0;    // 3byte (or 4byte)
+    uint16_t length = 0x100;
+    
     spi_init(); // spi初期設定
-    printf("RDID\r\n"); 
-    flash_rdid(rx_buf, 4);  // RDID読み出し関数
-    data_debug_printf(rx_buf, 4);   // 取得データ表示
+    
+    pc.printf("-- verification of Flash memory --\r\n");
+    pc.printf("\r\n");
+    
+    /*RDID Phase*/
+    pc.printf("<<RDID start>>\r\n"); 
+    flash_rdid(rx_buf, 3);  // RDID読み出し関数
+    data_debug_print(rx_buf, 3);   // 取得データ表示
+
+    pc.printf("<<Read start>>\r\n"); 
+    flash_read(addr, rx_buf, length);  // 読み出し関数
+    data_debug_print(rx_buf, length);   // 取得データ表示
+    
+    /*Erase Phase*/
+    pc.printf("<<Erase start>>\r\n"); 
+    t.start();  // 消去時間計測
+    flash_wren();   // 書込み・消去の前に必ず必要
+    flash_se(addr);
+    
+    pc.printf("*SPI For WIP\r\n");
+    while(flash_is_wip()){  // 消去待ち(WIPが 1 の間)  // While文が1周回る毎にflash_is_wipを実行している?
+        }
+    t.stop();
+    pc.printf("\r\n");
+    pc.printf("Erase complete! \r\n");
+    printf("The Erase time taken was %f μs\r\n", t.read_us());
+    pc.printf("\r\n");
+    
+    pc.printf("Erase check (Read the erased address.)\r\n");
+    flash_read(addr, rx_buf, length);
+    data_debug_print(rx_buf, length);
+    
+    
+    /*Write Phase*/
+    pc.printf("Make Sequential Data...\r\n\r\n");
+    for(uint16_t i = 0; i < length; ++i) {
+        tx_buf[i] = i & 0xff;   // 配列に1~256(0xFF)まで入れる
+        }
+    
+    pc.printf("<<Write start>>\r\n"); 
+    t.start();  // 書込時間計測
+    flash_wren();   // 書込み・消去の前に必ず必要
+    flash_pp(addr, tx_buf, length);
+    
+    pc.printf("*SPI For WIP\r\n");
+    while(flash_is_wip()){  // 消去待ち(WIPが 1 の間)  // While文が1周回る毎にflash_is_wipを実行している?
     }
+    pc.printf("\r\n");
+    t.stop();
     
-
-// 読み出し用メモ//
-//    const uint32_t address = 0000000;   // 0000000h(hは16進数の証)
-//    /* フラッシュから構造体にデータを読み込む */
-//    loadFlash(address, (uint8_t*)&chimame, sizeof(Chimame));
-//    test = readData;
\ No newline at end of file
+    pc.printf("Write complete! \r\n");
+    printf("The Write time taken was %f μs\r\n", t.read_us());
+    pc.printf("\r\n");
+    
+    pc.printf("Write Data check (Read the Written address.)\r\n");
+    flash_read(addr, rx_buf, length);
+    data_debug_print(rx_buf, length);
+    
+    }
\ No newline at end of file