Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
SV-Zanshin authored Nov 20, 2016
1 parent a9cbe7b commit b117dda
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
8 changes: 3 additions & 5 deletions MicrochipSRAM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
/*******************************************************************************************************************
** Class Constructor instantiates the class **
*******************************************************************************************************************/
MicrochipSRAM::MicrochipSRAM(const uint8_t SSPin, uint32_t &SRAMBytes) // CONSTRUCTOR - Instantiate class //
: _SSPin(SSPin) { // Privately store the pin number //
MicrochipSRAM::MicrochipSRAM(const uint8_t SSPin) : _SSPin(SSPin) { // CONSTRUCTOR - Instantiate class //
pinMode(_SSPin,OUTPUT); // Define the CS/SS pin SPI I/O //
digitalWrite(_SSPin,HIGH); // Deselect by pulling CS pin high //
SPI.begin(); // Start SPI //
Expand Down Expand Up @@ -79,7 +78,6 @@ MicrochipSRAM::MicrochipSRAM(const uint8_t SSPin, uint32_t &SRAMBytes) //
} // of if-then else we don't have a 64kbit chip // //
} // of if-then-else we have a positive 1mbit ID // //
} // of if-then the size was specified by caller // //
_SRAMBytes = SRAMBytes; // Store value in private space //
} // of class constructor //----------------------------------//
/*******************************************************************************************************************
** Class Destructor currently does nothing and is included for compatibility purposes **
Expand All @@ -91,9 +89,9 @@ MicrochipSRAM::~MicrochipSRAM() {} // of unused class destructor //
void MicrochipSRAM::clearMemory(const uint8_t clearValue = 0) { // Clear all memory to one value //
digitalWrite(_SSPin,LOW); // Select by pulling CS low //
SPI.transfer(SRAM_WRITE_CODE); // Send the command for WRITE mode //
if (_SRAMBytes==SRAM_1024) SPI.transfer(0x00); // Send 3rd address when required //
if (SRAMBytes==SRAM_1024) SPI.transfer(0x00); // Send 3rd address when required //
SPI.transfer(0x00); // Send address data 0x00 value //
SPI.transfer(0x00); // Send address data 0x00 value //
for (uint32_t i=0;i<_SRAMBytes;i++) SPI.transfer(clearValue); // Fill memory with given value //
for (uint32_t i=0;i<SRAMBytes;i++) SPI.transfer(clearValue); // Fill memory with given value //
digitalWrite(_SSPin,HIGH); // Deselect by pulling CS high //
} // of method ClearMemory //----------------------------------//
19 changes: 10 additions & 9 deletions MicrochipSRAM.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@
** **
** Vers. Date Developer Comments **
** ====== ========== =================== ======================================================================== **
** 1.0.2 2016-11-20 [email protected] Changed constructor to only use CS/SS pin, moved "SRAMBytes" to public **
** 1.0.1 2016-11-19 [email protected] Added method "clearMemory" **
** 1.0.0 2016-11-19 [email protected] Cleaned up, published https://github.com/SV-Zanshin/MicrochipSRAM **
** 1.0.b1 2016-11-19 [email protected] Created class **
** 1.0.b1 2016-11-16 [email protected] Created class **
** **
*******************************************************************************************************************/
#include "Arduino.h" // Arduino data type definitions //
#include <SPI.h> // SPI (Serial Peripheral Interface)//
#ifndef MicrochipSRAM_h // Guard code definition //
#define MicrochipSRAM_h // Define the name inside guard code//
class MicrochipSRAM { // Class definition //
/***************************************************************************************************************
** Declare constants used in the class **
***************************************************************************************************************/
Expand All @@ -63,8 +63,9 @@
const uint32_t SRAM_64 = 8192; // Equates to 64kbit of storage //
const uint8_t SRAM_WRITE_CODE = 2; // Write //
const uint8_t SRAM_READ_CODE = 3; // Read //
class MicrochipSRAM { // Class definition //
public: // Publicly visible methods //
MicrochipSRAM(const uint8_t SSPin, uint32_t &SRAMBytes); // Class constructor //
MicrochipSRAM(const uint8_t SSPin); // Class constructor //
~MicrochipSRAM(); // Class destructor //
void clearMemory(const uint8_t clearValue = 0); // Clear all memory to one value //
/*************************************************************************************************************
Expand All @@ -75,10 +76,10 @@
*************************************************************************************************************/
template< typename T > uint32_t &get(const uint32_t addr,T &value) { // method to write a structure //
uint8_t* bytePtr = (uint8_t*)&value; // Pointer to structure beginning //
uint32_t returnAddress = (addr+sizeof(T))%_SRAMBytes; // compute the return address //
uint32_t returnAddress = (addr+sizeof(T))%SRAMBytes; // compute the return address //
digitalWrite(_SSPin,LOW); // Pull CS/SS low to select device //
SPI.transfer(SRAM_READ_CODE); // Send the command for WRITE mode //
if (_SRAMBytes==SRAM_1024) SPI.transfer((uint8_t)(addr>>16)&0xFF); // Send the MSB of the 24bit address//
if (SRAMBytes==SRAM_1024) SPI.transfer((uint8_t)(addr>>16)&0xFF); // Send the MSB of the 24bit address//
SPI.transfer((uint8_t)(addr>>8) & 0xFF); // Send the 2nd byte of the address //
SPI.transfer((uint8_t)addr); // Send the LSB of the address //
for (uint32_t i=0;i<sizeof(T);i++) *bytePtr++ = SPI.transfer(0x00); // loop for each byte to be read //
Expand All @@ -87,21 +88,21 @@
} // of method get //----------------------------------//
template<typename T> uint32_t &put(const uint32_t addr,const T &value){ // method to get a structure //
const uint8_t* bytePtr = (const uint8_t*)&value; // Pointer to structure beginning //
uint32_t returnAddress = (addr+sizeof(T))%_SRAMBytes; // compute the return address //
uint32_t returnAddress = (addr+sizeof(T))%SRAMBytes; // compute the return address //
digitalWrite(_SSPin,LOW); // Pull CS/SS low to select device //
SPI.transfer(SRAM_WRITE_CODE); // Send the command for WRITE mode //
if (_SRAMBytes==SRAM_1024) SPI.transfer((uint8_t)(addr>>16)&0xFF); // Send the MSB of the 24bit address//
if (SRAMBytes==SRAM_1024) SPI.transfer((uint8_t)(addr>>16)&0xFF); // Send the MSB of the 24bit address//
SPI.transfer((uint8_t)(addr>>8) & 0xFF); // Send the 2nd byte of the address //
SPI.transfer((uint8_t)addr); // Send the LSB of the address //
for (uint32_t i=0;i<sizeof(T);i++) SPI.transfer(*bytePtr++); // loop for each byte to be written //
digitalWrite(_SSPin,HIGH); // Pull the SS/CS high to deselect //
return(returnAddress); // Return the computed new address //
} // of method put //----------------------------------//
template< typename T > &fillMemory( uint32_t addr, T &value ) { // method to fill memory with values//
while(addr<(_SRAMBytes-sizeof(T))) addr = put(addr,value); // loop until we reach end of memory//
while(addr<(SRAMBytes-sizeof(T))) addr = put(addr,value); // loop until we reach end of memory//
} // of method fillMemory //----------------------------------//
uint32_t SRAMBytes = 0; // Number of bytes available on chip//
private: // Private variables and methods //
uint8_t _SSPin = 0; // The CS/SS pin attached //
uint32_t _SRAMBytes = 0; // Number of bytes available on chip//
}; // of MicrochipSRAM class definition // //
#endif //----------------------------------//

0 comments on commit b117dda

Please sign in to comment.