diff --git a/MicrochipSRAM.cpp b/MicrochipSRAM.cpp index 9c0f94f..e631431 100644 --- a/MicrochipSRAM.cpp +++ b/MicrochipSRAM.cpp @@ -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 // @@ -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 ** @@ -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 // 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 ** ***************************************************************************************************************/ @@ -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 // /************************************************************************************************************* @@ -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 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 &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 //----------------------------------//