MicrochipSRAM  v1.0.6
Arduino Library for an SNVRAM SPI chips from Microchip
MicrochipSRAM.h
Go to the documentation of this file.
1 // clang-format off
84 // clang-format on
85 #include "Arduino.h" // Arduino data type definitions
86 #include <SPI.h> // SPI (Serial Peripheral Interface)
87 #ifndef MicrochipSRAM_h
88 
89 #define MicrochipSRAM_h
90 /****************************************************************************************************
91 ** Declare constants used in the class **
92 ****************************************************************************************************/
93 const uint8_t SRAM_WRITE_MODE_REG{0x01};
94 const uint8_t SRAM_READ_MODE_REG{0x05};
95 const uint8_t SRAM_BYTE_MODE{B00000000};
96 const uint8_t SRAM_PAGE_MODE{B10000000};
97 const uint8_t SRAM_SEQ_MODE{B11000000};
98 const uint32_t SRAM_1024{131072};
99 const uint32_t SRAM_512{65536};
100 const uint32_t SRAM_256{32768};
101 const uint32_t SRAM_128{16384};
102 const uint32_t SRAM_64{8192};
103 const uint8_t SRAM_WRITE_CODE{2};
104 const uint8_t SRAM_READ_CODE{3};
105 
111  public:
112  MicrochipSRAM(const uint8_t SSPin);
113  ~MicrochipSRAM();
114  void clearMemory(const uint8_t clearValue = 0) const;
115  /*************************************************************************************************
116  ** Declare the get and put methods as template functions here in the header file. This allows **
117  ** any type of variable or structure to be used rather than having to make one function for **
118  ** each datatype used. Note that due to the sequential mode being active, reads and writes that **
119  ** go past the last existing address will automatically wrap back to the beginning of memory. **
120  *************************************************************************************************/
121  template <typename T>
122  uint32_t &get(const uint32_t addr, T &value) const {
130  static uint32_t returnAddress;
131  uint8_t * bytePtr = (uint8_t *)&value; // Pointer to structure beginning
132  returnAddress = (addr + sizeof(T)) % SRAMBytes; // compute the return address
133  digitalWrite(_SSPin, LOW); // Pull CS/SS low to select device
134  SPI.transfer(SRAM_READ_CODE); // Send the command for READ mode
135  if (SRAMBytes == SRAM_1024)
136  SPI.transfer((uint8_t)(addr >> 16) & 0xFF); // Send the MSB of the 24bit address
137  SPI.transfer((uint8_t)(addr >> 8) & 0xFF); // Send the 2nd byte of the address
138  SPI.transfer((uint8_t)addr); // Send the LSB of the address
139  for (uint32_t i = 0; i < sizeof(T); i++)
140  *bytePtr++ = SPI.transfer(0x00); // loop for each byte to be read
141  digitalWrite(_SSPin, HIGH); // Pull the SS/CS high to deselect
142  return (returnAddress); // Return the computed new address
143  } // of method "get()"
144  template <typename T>
145  uint32_t &put(const uint32_t addr, const T &value) const {
153  static uint32_t returnAddress;
154  const uint8_t * bytePtr = (const uint8_t *)&value; // Pointer to structure beginning
155  returnAddress = (addr + sizeof(T)) % SRAMBytes; // compute the return address
156  digitalWrite(_SSPin, LOW); // Pull CS/SS low to select device
157  SPI.transfer(SRAM_WRITE_CODE); // Send the command for WRITE mode
158  if (SRAMBytes == SRAM_1024)
159  SPI.transfer((uint8_t)(addr >> 16) & 0xFF); // Send the MSB of the 24bit address
160  SPI.transfer((uint8_t)(addr >> 8) & 0xFF); // Send the 2nd byte of the address
161  SPI.transfer((uint8_t)addr); // Send the LSB of the address
162  for (uint32_t i = 0; i < sizeof(T); i++)
163  SPI.transfer(*bytePtr++); // loop for each byte to be written
164  digitalWrite(_SSPin, HIGH); // Pull the SS/CS high to deselect
165  return (returnAddress); // Return the computed new address
166  } // of method put()
167  template <typename T>
168  uint32_t &fillMemory(uint32_t addr, T &value) const {
175  while (addr < (SRAMBytes - sizeof(T)))
176  addr = put(addr, value); // loop until we reach end of memory
177  return (addr);
178  } // of method fillMemory()
179  uint32_t SRAMBytes = 0;
180  private:
181  uint8_t _SSPin = 0;
182 }; // of MicrochipSRAM class definition
183 #endif
Access the Microchip SRAM with automated memory size detection.
Definition: MicrochipSRAM.h:106
const uint32_t SRAM_1024
Equates to 1mbit of storage.
Definition: MicrochipSRAM.h:98
uint32_t & fillMemory(uint32_t addr, T &value) const
Definition: MicrochipSRAM.h:168
~MicrochipSRAM()
Definition: MicrochipSRAM.cpp:93
const uint8_t SRAM_PAGE_MODE
2MSB 10 is page mode
Definition: MicrochipSRAM.h:96
const uint8_t SRAM_READ_MODE_REG
Read the mode register.
Definition: MicrochipSRAM.h:94
uint32_t & put(const uint32_t addr, const T &value) const
Definition: MicrochipSRAM.h:145
const uint8_t SRAM_BYTE_MODE
2MSB 00 is Byte mode
Definition: MicrochipSRAM.h:95
const uint32_t SRAM_64
Equates to 64kbit of storage.
Definition: MicrochipSRAM.h:102
const uint32_t SRAM_256
Equates to 256kbit of storage.
Definition: MicrochipSRAM.h:100
const uint32_t SRAM_128
Equates to 128kbit of storage.
Definition: MicrochipSRAM.h:101
const uint32_t SRAM_512
Equates to 512kbit of storage.
Definition: MicrochipSRAM.h:99
uint32_t SRAMBytes
Number of bytes available on chip//.
Definition: MicrochipSRAM.h:179
MicrochipSRAM(const uint8_t SSPin)
Definition: MicrochipSRAM.cpp:9
const uint8_t SRAM_WRITE_MODE_REG
Write the mode register.
Definition: MicrochipSRAM.h:93
const uint8_t SRAM_SEQ_MODE
2MSB 11 is sequential mode
Definition: MicrochipSRAM.h:97
const uint8_t SRAM_READ_CODE
Read.
Definition: MicrochipSRAM.h:104
const uint8_t SRAM_WRITE_CODE
Write.
Definition: MicrochipSRAM.h:103
void clearMemory(const uint8_t clearValue=0) const
Definition: MicrochipSRAM.cpp:99