![](/media/cache/profiles/73640de25b7d656733ce2f808a330f18.50x50_q85.jpg)
Library to drive the Microchip 23K256 SRAM over SPI.
Revision 3:d2314b1ac797, committed 2010-08-15
- Comitter:
- romilly
- Date:
- Sun Aug 15 14:28:05 2010 +0000
- Parent:
- 2:f96c3c85aa3b
- Commit message:
- Added DOXYGEN comments
Changed in this revision
Ser23K256.cpp | Show annotated file Show diff for this revision Revisions of this file |
Ser23K256.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r f96c3c85aa3b -r d2314b1ac797 Ser23K256.cpp --- a/Ser23K256.cpp Sun Aug 15 13:06:52 2010 +0000 +++ b/Ser23K256.cpp Sun Aug 15 14:28:05 2010 +0000 @@ -1,91 +1,84 @@ -// Ser23K256 - drive the Microchip 23K256 SRAM using SPI -// Copyright (c) 2010 Romilly Cocking -// Released under the MIT License: http://mbed.org/license/mit - -// 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). - -// You can clock the 23K256 at up to 20MHz, so it supports the mbed's maximum SPI frequency of 12MHz. - -#include "mbed.h" -#include "Ser23K256.h" - -Ser23K256::Ser23K256(SPI& spi, PinName ncs) : _spi(spi), _ncs(ncs) { - deselect(); -} - -void Ser23K256::select() { - _ncs = 0; -} - -void Ser23K256::deselect() { - _ncs = 1; -} - -void Ser23K256::writeStatus(char status) { - select(); - _spi.write(WRITE_STATUS); - _spi.write(status); - deselect(); -} - -char Ser23K256::readStatus() { - select(); - _spi.write(READ_STATUS); - char result = (char) _spi.write(0); - deselect(); - return result; -} - -void Ser23K256::prepareCommand(char command, int address) { - select(); - _spi.write(command); - _spi.write(address >> 8); - _spi.write(address & 0xFF); -} - -// write or read a single byte - -void Ser23K256::write(int address, char byte) { - prepareCommand(WRITE, address); - _spi.write(byte); - deselect(); -} - -char Ser23K256::read(int address) { - prepareCommand(READ, address); - int result = _spi.write(0); - deselect(); - 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 Ser23K256::write(int address, char * buffer, int count) { - writeStatus(SEQUENTIAL_MODE); - prepareCommand(WRITE, address); - for (int i = 0; i < count; i++) { - _spi.write(buffer[i]); - } - deselect(); - writeStatus(BYTE_MODE); -} - -void Ser23K256::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); - } - deselect(); - writeStatus(BYTE_MODE); +/* Ser23K256 - drive the Microchip 23K256 SRAM using SPI +* Copyright (c) 2010 Romilly Cocking +* Released under the MIT License: http://mbed.org/license/mit +*/ + +#include "mbed.h" +#include "Ser23K256.h" + +Ser23K256::Ser23K256(SPI& spi, PinName ncs) : _spi(spi), _ncs(ncs) { + deselect(); +} + +void Ser23K256::select() { + _ncs = 0; +} + +void Ser23K256::deselect() { + _ncs = 1; +} + +void Ser23K256::writeStatus(char status) { + select(); + _spi.write(WRITE_STATUS); + _spi.write(status); + deselect(); +} + +char Ser23K256::readStatus() { + select(); + _spi.write(READ_STATUS); + char result = (char) _spi.write(0); + deselect(); + return result; +} + +void Ser23K256::prepareCommand(char command, int address) { + select(); + _spi.write(command); + _spi.write(address >> 8); + _spi.write(address & 0xFF); +} + +// write or read a single byte + +void Ser23K256::write(int address, char byte) { + prepareCommand(WRITE, address); + _spi.write(byte); + deselect(); +} + +char Ser23K256::read(int address) { + prepareCommand(READ, address); + int result = _spi.write(0); + deselect(); + 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 Ser23K256::write(int address, char * buffer, int count) { + writeStatus(SEQUENTIAL_MODE); + prepareCommand(WRITE, address); + for (int i = 0; i < count; i++) { + _spi.write(buffer[i]); + } + deselect(); + writeStatus(BYTE_MODE); +} + +void Ser23K256::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); + } + deselect(); + writeStatus(BYTE_MODE); } \ No newline at end of file
diff -r f96c3c85aa3b -r d2314b1ac797 Ser23K256.h --- a/Ser23K256.h Sun Aug 15 13:06:52 2010 +0000 +++ b/Ser23K256.h Sun Aug 15 14:28:05 2010 +0000 @@ -1,6 +1,15 @@ -// Ser23K256 - drive the Microchip 23K256 SRAM using SPI -// Copyright (c) 2010 Romilly Cocking -// Released under the MIT License: http://mbed.org/license/mit +/** Ser23K256 - drive the Microchip 23K256 SRAM using SPI +* Copyright (c) 2010 Romilly Cocking +* Released under the MIT License: http://mbed.org/license/mit +* +* 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). +* +* You can clock the 23K256 at up to 20MHz, so it supports the mbed's maximum SPI frequency of 12MHz. +*/ #include "mbed.h" @@ -17,12 +26,74 @@ #define READ_STATUS 0x05 // called RDSR in datasheet #define WRITE_STATUS 0x01 // called WRSR in datasheet +/** An interface for the Microchip 32k byte 23K256 SRAM over SPI +* +* +* +* @code +* #include "mbed.h" +* #include "Ser23K256.h" +* +* +* SPI spi(p5,p6,p7); +* Ser23K256 sram(spi,p14); +* +* int main() { +* char buff[50]; +* sram.write(0, 'h'); +* sram.write(1, 'i'); +* sram.write(2, '!'); +* sram.write(3, '\0'); +* for (int address = 0; address < 4; address++) { +* buff[address] = sram.read(address); +* } +* printf("sram = %s\r\n", buff); +* sram.write(0, "Hello world!",12); +* sram.read(0, buff, 12); +* buff[12]='\0'; +* printf("now = %s\r\n", buff); +*} +* @endcode +* connections: +* chip pin 1 to mbed ncs (see below) +* chip pin 2 SO to mbed MISO +* chip pin 3 - no connection +* chip pin 4 to mbed Gnd +* chip pin 5 SI pin to mbed MOSI +* chip pin 6 SCK to mbed sck +* chip pin 7 (notHOLD) to mbed Vout +* chip pin 8 to mbed Vout +*/ class Ser23K256 { public: +/** Create an interface +* +* +* @param spi An SPI object +* @param ncs Not chip select pin - any free Digital pin will do +*/ Ser23K256(SPI& spi, PinName ncs); +/** read a byte from SRAM +* @param address The address to read from +* @return the character at that address +*/ char read(int address); +/** read multiple bytes from SRAM into a buffer +* @param address The SRAM address to read from +* @param buffer The buffer to read into (must be big enough!) +* @param count The number of bytes to read +*/ void read(int address, char * buffer, int count); +/** write a byte to SRAM +* @param address The address SRAM to write to +* @param byte The byte to write there +*/ void write(int address, char byte); + /** write multiple bytes to SRAM from a buffer +* @param address The SRAM address write to +* @param buffer The buffer to write from +* @param count The number of bytes to write +*/ void write(int address, char * buffer, int count); private: SPI& _spi;