Last active
November 22, 2020 22:32
-
-
Save bit-hack/69d08013ddf7180356ff8e3bc4056e9d to your computer and use it in GitHub Desktop.
Arduino 23LC1024 SRAM object
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
#pragma once | |
#include <Arduino.h> | |
#include <SPI.h> | |
// 23lc1024 pin mapping | |
// | |
// 1 CS | |
// 2 MISO | |
// 3 (tie gnd) | |
// 4 GND | |
// 5 MOSI | |
// 6 SCK | |
// 7 (tie vcc) | |
// 8 VCC | |
struct sram_23lc1024_t { | |
sram_23lc1024_t(SPIClass &spi, uint8_t cs) | |
: spi(spi) | |
, cs(cs) | |
{} | |
void write(uint32_t addr, uint8_t data) { | |
digitalWrite(cs, LOW); | |
spi.beginTransaction( | |
CS_PIN_CONTROLLED_BY_USER, | |
SPISettings{SPI_SPEED_CLOCK_DEFAULT, MSBFIRST, SPI_MODE0}); | |
// write command | |
spi.transfer(WRITE); | |
// address | |
spi.transfer((addr >> 16) & 0xff); | |
spi.transfer((addr >> 8 ) & 0xff); | |
spi.transfer((addr ) & 0xff); | |
// data | |
spi.transfer(data); | |
spi.endTransaction(); | |
digitalWrite(cs, HIGH); | |
} | |
uint8_t read(uint32_t addr) { | |
digitalWrite(cs, LOW); | |
uint8_t out = 0; | |
spi.beginTransaction( | |
CS_PIN_CONTROLLED_BY_USER, | |
SPISettings{SPI_SPEED_CLOCK_DEFAULT, MSBFIRST, SPI_MODE0}); | |
// read command | |
spi.transfer(READ); | |
// address | |
spi.transfer((addr >> 16) & 0xff); | |
spi.transfer((addr >> 8 ) & 0xff); | |
spi.transfer((addr ) & 0xff); | |
// data | |
out = spi.transfer(0); | |
spi.endTransaction(); | |
digitalWrite(cs, HIGH); | |
return out; | |
} | |
protected: | |
enum { | |
WRITE = 0x02, | |
READ = 0x03, | |
}; | |
SPIClass &spi; | |
const uint8_t cs; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment