Romilly Cocking / Mbed 2 deprecated Ser23K256

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Ser23K256.cpp Source File

Ser23K256.cpp

00001 /* Ser23K256 - drive the Microchip 23K256 SRAM using SPI
00002 * Copyright (c) 2010 Romilly Cocking
00003 * Released under the MIT License: http://mbed.org/license/mit
00004 */
00005 
00006 #include "mbed.h"
00007 #include "Ser23K256.h"
00008 
00009 Ser23K256::Ser23K256(SPI& spi, PinName ncs) : _spi(spi), _ncs(ncs)  {
00010     deselect();
00011 }
00012 
00013 void Ser23K256::select() {
00014     _ncs = 0;
00015 }
00016 
00017 void Ser23K256::deselect() {
00018     _ncs = 1;
00019 }
00020 
00021 void Ser23K256::writeStatus(char status) {
00022     select();
00023     _spi.write(WRITE_STATUS);
00024     _spi.write(status);
00025     deselect();
00026 }
00027 
00028 char Ser23K256::readStatus() {
00029     select();
00030     _spi.write(READ_STATUS);
00031     char result = (char) _spi.write(0);
00032     deselect();
00033     return result;
00034 }
00035 
00036 void Ser23K256::prepareCommand(char command, int address) {
00037     select();
00038     _spi.write(command);
00039     _spi.write(address >> 8);
00040     _spi.write(address & 0xFF);
00041 }
00042 
00043 // write or read a single byte
00044 
00045 void Ser23K256::write(int address, char byte) {
00046     prepareCommand(WRITE, address);
00047     _spi.write(byte);
00048     deselect();
00049 }
00050 
00051 char Ser23K256::read(int address) {
00052     prepareCommand(READ, address);
00053     int result = _spi.write(0);
00054     deselect();
00055     return (char) result;
00056 }
00057 
00058 // buffered write and read
00059 
00060 /*
00061 * the single-byte read and write assume the 23K256 is in its default byte-mode
00062 * so sequential-model commands must switch the chip into sequential mode
00063 * at the start and return it to byte mode at the end.
00064 */
00065 
00066 void Ser23K256::write(int address, char * buffer, int count) {
00067     writeStatus(SEQUENTIAL_MODE);
00068     prepareCommand(WRITE, address);
00069     for (int i = 0; i < count; i++) {
00070         _spi.write(buffer[i]);
00071     }
00072     deselect();
00073     writeStatus(BYTE_MODE);
00074 }
00075 
00076 void Ser23K256::read(int address, char * buffer, int count) {
00077     writeStatus(SEQUENTIAL_MODE);
00078     prepareCommand(READ, address);
00079     for (int i = 0; i < count; i++) {
00080         buffer[i] = _spi.write(0);
00081     }
00082     deselect();
00083     writeStatus(BYTE_MODE);
00084 }