Library to drive the Microchip 23K256 SRAM over SPI.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
romilly
Date:
Sun Aug 15 10:10:05 2010 +0000
Parent:
0:318f4f480b1b
Child:
2:f96c3c85aa3b
Commit message:
Tidied up code ready for creating a library.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sat Aug 14 20:15:08 2010 +0000
+++ b/main.cpp	Sun Aug 15 10:10:05 2010 +0000
@@ -1,59 +1,103 @@
 #include "mbed.h"
 
 DigitalOut ncs(p14);
-
 SPI spi(p5,p6,p7);
 
-void on() {
+// 23K256 data sheet at http://ww1.microchip.com/downloads/en/DeviceDoc/22100B.pdf
+
+// Page-mode commands have not been implemented; I have found no need for them yet.
+
+// Assumes spi mode is default (8,0).
+
+// The 23K256 supports the mbed's maximum spi frequency of 12MHz.
+
+// mode codes for 23K256
+#define BYTE_MODE       0x00
+#define SEQUENTIAL_MODE 0x40
+
+// command codes for 23K256
+#define READ            0x03
+#define WRITE           0x02
+#define READ_STATUS     0x05 // called RDSR in datasheet
+#define WRITE_STATUS    0x01 // called WRSR in datasheet
+
+inline void on() {
     ncs = 0;
 }
 
-void off() {
+inline void off() {
     ncs = 1;
 }
 
-void write(int address, char byte) {
+void writeStatus(char status) {
+    on();
+    spi.write(WRITE_STATUS);
+    spi.write(status);
+    off();
+}
+
+int readStatus() {
     on();
-    spi.write(0x02); // write data
+    spi.write(READ_STATUS);
+    int result = spi.write(0);
+    off();
+    return result;
+}
+
+inline void prepareCommand(char command, int address) {
+    on();
+    spi.write(command);
     spi.write(address >> 8);
     spi.write(address & 0xFF);
+}
+
+// write or read a single byte
+
+void write(int address, char byte) {
+    prepareCommand(WRITE, address);
     spi.write(byte);
     off();
 }
 
-
-void write(int address, char * buffer, int count) {
-    on();
-    spi.write(0x01); // write status
-    spi.write(0x40); // sequential mode
-    off();
-    on();
-    spi.write(0x02);
-    spi.write(address >> 8);
-    spi.write(address & 0xFF);
-    for (int i = 0; i < count; i++) {
-        spi.write(buffer[i]);
-    }
-    off();
-    on();
-    spi.write(0x01); // write status
-    spi.write(0x0); //  byte mode
-    off();
-}
-
-
 char read(int address) {
-    on();
-    spi.write(0x03);
-    spi.write(address >> 8);
-    spi.write(address & 0xFF);
+    prepareCommand(READ, address);
     int result = spi.write(0);
     off();
     return (char) result;
 }
 
+// buffered write and read
+
+/*
+* the single-byte read and write assume the 23K256 is in its default byte-mode
+* so sequential-model commands must switch the chip into sequential mode
+* at the start and return it to byte mode at the end.
+*/
+
+void write(int address, char * buffer, int count) {
+    writeStatus(SEQUENTIAL_MODE);
+    prepareCommand(WRITE, address);
+    for (int i = 0; i < count; i++) {
+        spi.write(buffer[i]);
+    }
+    off();
+    writeStatus(BYTE_MODE);
+}
+
+void read(int address, char * buffer, int count) {
+    writeStatus(SEQUENTIAL_MODE);
+    prepareCommand(READ, address);
+    for (int i = 0; i < count; i++) {
+        buffer[i] = spi.write(0);
+    }
+    off();
+    writeStatus(BYTE_MODE);
+}
+
 int main() {
     off();
+    printf("Status codes: 0 = byte mode (default), 0x40 = sequential mode. "); 
+    printf("Status is currently %i\r\n", readStatus());
     char buff[50];
     write(0, 'h');
     write(1, 'i');
@@ -64,9 +108,7 @@
     }
     printf("mem = %s\r\n", buff);
     write(0, "Hello world!",12);
-    for (int address = 0; address < 12; address++) {
-        buff[address] = read(address);
-    }
+    read(0, buff, 12);
     buff[12]='\0';
     printf("now = %s\r\n", buff);
 }
\ No newline at end of file