Microchip 23K256 (SPI SRAM) sample code for https://www.switch-science.com/catalog/1072/ module.

Dependencies:   mbed

Revision:
5:fb6ac7306443
Parent:
4:b1021974c3e7
--- a/main.cpp	Mon Feb 16 04:08:20 2015 +0000
+++ b/main.cpp	Sun Feb 22 04:07:28 2015 +0000
@@ -1,8 +1,5 @@
 /*
- * test for 
- *  - Microchip 23K256
- *    - SRAM
- *    - SPI 20MHz
+ * sample for Microchip 23K256 (SRAM, SPI 20MHz)
  */
 
 #include "mbed.h"
@@ -12,6 +9,12 @@
 
 DigitalIn user_button(USER_BUTTON);
 
+/** */
+SPI spi(SPI_MOSI, SPI_MISO, SPI_SCK);
+
+/** */
+DigitalOut cs(SPI_CS);
+
 /**
  * USERボタンが押されるまで待つ。
  */
@@ -24,29 +27,6 @@
     wait(0.2);
 }
 
-/** */
-//DigitalOut user_led(LED1);  // SPI1 SCK
-
-/**
- *
- * @param[in] interval_sec 点滅間隔(単位=秒)
- * @param[in] times 点滅回数
-*/
-//void
-//flash_led(const float interval_sec, const uint16_t times) {
-//    user_led = 1;
-//    for (uint16_t i = 0; i < times * 2; ++i) {
-//        wait(interval_sec);
-//        user_led = !user_led;
-//    }
-//}
-
-/** */
-SPI spi(SPI_MOSI, SPI_MISO, SPI_SCK);
-
-/** */
-DigitalOut cs(SPI_CS);
-
 /*
  * SPI COMMAND (Microchip 23K256 SRAM)
  * 1byte read/write 
@@ -61,7 +41,7 @@
     WRSR = 0x01u,
 };
 
-/** SRAM Mode */
+/** SRAM Mode (Microchip 23K256 SRAM) */
 enum Microchip23K256Mode {
     MODE_MASK = 0xc0u,
     BYTE = 0x00u,
@@ -86,7 +66,7 @@
 }
 
 /**
- * レジスタ読みだし
+ * ステータスレジスタ読みだし
  * @return レジスタ値
  */
 inline uint8_t
@@ -100,7 +80,7 @@
 }
 
 /**
- * レジスタ書き込み
+ * ステータスレジスタ書き込み
  * @param[in] status
  */
 inline void
@@ -115,7 +95,7 @@
  * モードを切り替える。
  * @param[in] mode モード
  * @return 切替前のモード
- * @invariant モード以外のフラグは変更しない。
+ * @invariant ステータスレジスタ中のモード以外のフラグは変更しない。
  */
 inline uint8_t
 change_mode(const unsigned int next_mode) {
@@ -129,7 +109,7 @@
 }
 
 /**
- * 1byte読み出し
+ * 1byte read
  * @param[in] address アドレス 16bit。容量32KBなので、MSBは無視する
  * @return データ
  * @pre byteモードに切り替えていること。
@@ -143,14 +123,14 @@
     spi.write(READ);
     spi.write(address_high);
     spi.write(address_low);
-    const uint8_t data = spi.write(0x00);
+    const uint8_t data = spi.write(0);
     negate_CS();
 
     return data;
 }
 
 /**
- * 1byte書き込み
+ * 1byte write
  * @param[in] address アドレス 16bit。容量32KBなので、MSBは無視する
  * @param[in] data データ
  * @pre byteモードに切り替えていること。
@@ -308,23 +288,31 @@
  */
 void
 try_single_byte_access() {
+    const uint8_t address_high = (0x12 >> (8 * 1)) & 0xFFu;
+    const uint8_t address_low  = (0x13 >> (8 * 0)) & 0xFFu;
+
+    const uint8_t command_write = 0x02u;
+    const uint8_t command_read = 0x03u;
+    
+    const uint8_t data = 0x55;
+
     // 1byte write
-    assert_CS();
-    spi.write(0x02u);   // write command
-    spi.write(0);       // address
-    spi.write(0);       // address
-    spi.write(0x5a);    // data
-    nagate_CS();
+    cs = 0;
+    spi.write(command_write);
+    spi.write(address_high);
+    spi.write(address_low);
+    spi.write(data);
+    cs = 1;
     
     // 1byte read
-    assert_CS();
-    spi.write(0x03u);   // read command
-    spi.write(0);       // address
-    spi.write(0);       // address
-    const uint8_t value = spi.write(0); // data
-    nagate_CS();
+    cs = 0;
+    spi.write(command_read);
+    spi.write(address_high);
+    spi.write(address_low);
+    const uint8_t value = spi.write(0);
+    cs = 1;
 
-    pc.printf("read: 0x%02x\r\n", value);
+    pc.printf("write: %02x -> read: %02x\r\n", data, value);
 }
 
 
@@ -362,7 +350,5 @@
     pc.printf("\r\nTEST END\r\n\r\n");
 
     for(;;) {
-//        flash_led(0.125, 8);
-//        flash_led(0.50, 2);
     }
 }