Library to drive the Microchip 23K256 SRAM over SPI.

Dependencies:   mbed

Committer:
romilly
Date:
Sun Aug 15 10:10:05 2010 +0000
Revision:
1:28eb43851e6e
Parent:
0:318f4f480b1b
Tidied up code ready for creating a library.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
romilly 0:318f4f480b1b 1 #include "mbed.h"
romilly 0:318f4f480b1b 2
romilly 0:318f4f480b1b 3 DigitalOut ncs(p14);
romilly 0:318f4f480b1b 4 SPI spi(p5,p6,p7);
romilly 0:318f4f480b1b 5
romilly 1:28eb43851e6e 6 // 23K256 data sheet at http://ww1.microchip.com/downloads/en/DeviceDoc/22100B.pdf
romilly 1:28eb43851e6e 7
romilly 1:28eb43851e6e 8 // Page-mode commands have not been implemented; I have found no need for them yet.
romilly 1:28eb43851e6e 9
romilly 1:28eb43851e6e 10 // Assumes spi mode is default (8,0).
romilly 1:28eb43851e6e 11
romilly 1:28eb43851e6e 12 // The 23K256 supports the mbed's maximum spi frequency of 12MHz.
romilly 1:28eb43851e6e 13
romilly 1:28eb43851e6e 14 // mode codes for 23K256
romilly 1:28eb43851e6e 15 #define BYTE_MODE 0x00
romilly 1:28eb43851e6e 16 #define SEQUENTIAL_MODE 0x40
romilly 1:28eb43851e6e 17
romilly 1:28eb43851e6e 18 // command codes for 23K256
romilly 1:28eb43851e6e 19 #define READ 0x03
romilly 1:28eb43851e6e 20 #define WRITE 0x02
romilly 1:28eb43851e6e 21 #define READ_STATUS 0x05 // called RDSR in datasheet
romilly 1:28eb43851e6e 22 #define WRITE_STATUS 0x01 // called WRSR in datasheet
romilly 1:28eb43851e6e 23
romilly 1:28eb43851e6e 24 inline void on() {
romilly 0:318f4f480b1b 25 ncs = 0;
romilly 0:318f4f480b1b 26 }
romilly 0:318f4f480b1b 27
romilly 1:28eb43851e6e 28 inline void off() {
romilly 0:318f4f480b1b 29 ncs = 1;
romilly 0:318f4f480b1b 30 }
romilly 0:318f4f480b1b 31
romilly 1:28eb43851e6e 32 void writeStatus(char status) {
romilly 1:28eb43851e6e 33 on();
romilly 1:28eb43851e6e 34 spi.write(WRITE_STATUS);
romilly 1:28eb43851e6e 35 spi.write(status);
romilly 1:28eb43851e6e 36 off();
romilly 1:28eb43851e6e 37 }
romilly 1:28eb43851e6e 38
romilly 1:28eb43851e6e 39 int readStatus() {
romilly 0:318f4f480b1b 40 on();
romilly 1:28eb43851e6e 41 spi.write(READ_STATUS);
romilly 1:28eb43851e6e 42 int result = spi.write(0);
romilly 1:28eb43851e6e 43 off();
romilly 1:28eb43851e6e 44 return result;
romilly 1:28eb43851e6e 45 }
romilly 1:28eb43851e6e 46
romilly 1:28eb43851e6e 47 inline void prepareCommand(char command, int address) {
romilly 1:28eb43851e6e 48 on();
romilly 1:28eb43851e6e 49 spi.write(command);
romilly 0:318f4f480b1b 50 spi.write(address >> 8);
romilly 0:318f4f480b1b 51 spi.write(address & 0xFF);
romilly 1:28eb43851e6e 52 }
romilly 1:28eb43851e6e 53
romilly 1:28eb43851e6e 54 // write or read a single byte
romilly 1:28eb43851e6e 55
romilly 1:28eb43851e6e 56 void write(int address, char byte) {
romilly 1:28eb43851e6e 57 prepareCommand(WRITE, address);
romilly 0:318f4f480b1b 58 spi.write(byte);
romilly 0:318f4f480b1b 59 off();
romilly 0:318f4f480b1b 60 }
romilly 0:318f4f480b1b 61
romilly 0:318f4f480b1b 62 char read(int address) {
romilly 1:28eb43851e6e 63 prepareCommand(READ, address);
romilly 0:318f4f480b1b 64 int result = spi.write(0);
romilly 0:318f4f480b1b 65 off();
romilly 0:318f4f480b1b 66 return (char) result;
romilly 0:318f4f480b1b 67 }
romilly 0:318f4f480b1b 68
romilly 1:28eb43851e6e 69 // buffered write and read
romilly 1:28eb43851e6e 70
romilly 1:28eb43851e6e 71 /*
romilly 1:28eb43851e6e 72 * the single-byte read and write assume the 23K256 is in its default byte-mode
romilly 1:28eb43851e6e 73 * so sequential-model commands must switch the chip into sequential mode
romilly 1:28eb43851e6e 74 * at the start and return it to byte mode at the end.
romilly 1:28eb43851e6e 75 */
romilly 1:28eb43851e6e 76
romilly 1:28eb43851e6e 77 void write(int address, char * buffer, int count) {
romilly 1:28eb43851e6e 78 writeStatus(SEQUENTIAL_MODE);
romilly 1:28eb43851e6e 79 prepareCommand(WRITE, address);
romilly 1:28eb43851e6e 80 for (int i = 0; i < count; i++) {
romilly 1:28eb43851e6e 81 spi.write(buffer[i]);
romilly 1:28eb43851e6e 82 }
romilly 1:28eb43851e6e 83 off();
romilly 1:28eb43851e6e 84 writeStatus(BYTE_MODE);
romilly 1:28eb43851e6e 85 }
romilly 1:28eb43851e6e 86
romilly 1:28eb43851e6e 87 void read(int address, char * buffer, int count) {
romilly 1:28eb43851e6e 88 writeStatus(SEQUENTIAL_MODE);
romilly 1:28eb43851e6e 89 prepareCommand(READ, address);
romilly 1:28eb43851e6e 90 for (int i = 0; i < count; i++) {
romilly 1:28eb43851e6e 91 buffer[i] = spi.write(0);
romilly 1:28eb43851e6e 92 }
romilly 1:28eb43851e6e 93 off();
romilly 1:28eb43851e6e 94 writeStatus(BYTE_MODE);
romilly 1:28eb43851e6e 95 }
romilly 1:28eb43851e6e 96
romilly 0:318f4f480b1b 97 int main() {
romilly 0:318f4f480b1b 98 off();
romilly 1:28eb43851e6e 99 printf("Status codes: 0 = byte mode (default), 0x40 = sequential mode. ");
romilly 1:28eb43851e6e 100 printf("Status is currently %i\r\n", readStatus());
romilly 0:318f4f480b1b 101 char buff[50];
romilly 0:318f4f480b1b 102 write(0, 'h');
romilly 0:318f4f480b1b 103 write(1, 'i');
romilly 0:318f4f480b1b 104 write(2, '!');
romilly 0:318f4f480b1b 105 write(3, '\0');
romilly 0:318f4f480b1b 106 for (int address = 0; address < 4; address++) {
romilly 0:318f4f480b1b 107 buff[address] = read(address);
romilly 0:318f4f480b1b 108 }
romilly 0:318f4f480b1b 109 printf("mem = %s\r\n", buff);
romilly 0:318f4f480b1b 110 write(0, "Hello world!",12);
romilly 1:28eb43851e6e 111 read(0, buff, 12);
romilly 0:318f4f480b1b 112 buff[12]='\0';
romilly 0:318f4f480b1b 113 printf("now = %s\r\n", buff);
romilly 0:318f4f480b1b 114 }