Created
November 7, 2014 09:08
-
-
Save DarkLotus/12522b61d77f979a150d to your computer and use it in GitHub Desktop.
23LCV512 MBED STM32 Driver code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Library to interface with the 23LCV512 SRAM microchip. | |
| * | |
| * Apache2 licensed | |
| * Written by James Kidd 2014 | |
| * */ | |
| #include "mbed.h" | |
| enum SRAM_MODE { | |
| BYTE_MODE = 0x00, | |
| SEQ_MODE = 0x40 | |
| }; | |
| enum SRAM_COMMAND { | |
| WRITE_STATUS = 0x01, | |
| WRITE = 0x02, | |
| READ = 0x03, | |
| READ_STATUS = 0x05, | |
| EDIO = 0x3B, | |
| RST_DIO = 0xFF | |
| }; | |
| class SRAM23LCV { | |
| private: | |
| SPI& _spi; | |
| DigitalOut _cs; | |
| SRAM_MODE getMode(); | |
| void setMode(SRAM_MODE mode); | |
| void sendCommand(int command, uint16_t address); | |
| public: | |
| SRAM23LCV(SPI& spi,PinName cs); | |
| ~SRAM23LCV(); | |
| uint8_t read(uint16_t address); | |
| void read(uint16_t address, char * buffer, uint16_t count); | |
| void write(uint16_t address, uint8_t data); | |
| void write(uint16_t address, char * buffer, uint16_t count); | |
| //No idea if these should be inline or not, seems like a good idea? | |
| //inline | |
| void select() { _cs = 0; } | |
| //inline | |
| void deselect() { _cs = 1; } | |
| }; | |
| //end .h | |
| #include "SPI.h" | |
| #include "DigitalOut.h" | |
| #include "SRAM23LCV.h" | |
| SRAM23LCV::SRAM23LCV(SPI& spi, PinName cs) : _spi(spi), _cs(cs) { | |
| deselect(); | |
| } | |
| void SRAM23LCV::setMode(SRAM_MODE mode) { | |
| select(); | |
| _spi.write((char)WRITE_STATUS); | |
| _spi.write((char)mode); | |
| deselect(); | |
| } | |
| SRAM_MODE SRAM23LCV::getMode() { | |
| select(); | |
| _spi.write((int)READ_STATUS); | |
| return (SRAM_MODE)_spi.write(0); | |
| deselect(); | |
| } | |
| uint8_t SRAM23LCV::read(uint16_t address) { | |
| sendCommand(READ,address); | |
| uint8_t result = 0; | |
| result = _spi.write(0); | |
| deselect(); | |
| return result; | |
| } | |
| void SRAM23LCV::read(uint16_t address, char * buffer, uint16_t count){ | |
| setMode(SEQ_MODE); | |
| sendCommand(READ,address); | |
| for(int i = 0; i < count;i++){ | |
| buffer[i] = (char)_spi.write(0); | |
| } | |
| setMode(BYTE_MODE); | |
| deselect(); | |
| } | |
| void SRAM23LCV::write(uint16_t address,uint8_t data) { | |
| sendCommand(WRITE,address); | |
| _spi.write((char)data); | |
| deselect(); | |
| } | |
| void SRAM23LCV::write(uint16_t address, char * buffer, uint16_t count) { | |
| setMode(SEQ_MODE); | |
| sendCommand(WRITE,address); | |
| for(int i = 0; i < count;i++){ | |
| _spi.write((char)buffer[i]); | |
| } | |
| setMode(BYTE_MODE); | |
| deselect(); | |
| } | |
| void SRAM23LCV::sendCommand(int command, uint16_t address){ | |
| select(); | |
| _spi.write((char)command); | |
| _spi.write(address >> 8); | |
| _spi.write(address & 0xFF); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment