Use SPI_TRANSACTIONS when supported

This commit is contained in:
LowPowerLab 2015-06-02 14:59:21 -04:00
parent 46b3d40a0c
commit 7ab6179f4e
1 changed files with 51 additions and 28 deletions

View File

@ -1,16 +1,36 @@
/* // Copyright (c) 2013-2015 by Felix Rusu, LowPowerLab.com
* Copyright (c) 2013 by Felix Rusu <felix@lowpowerlab.com> // SPI Flash memory library for arduino/moteino.
* SPI Flash memory library for arduino/moteino. // This works with 256byte/page SPI flash memory
* This works with 256byte/page SPI flash memory // For instance a 4MBit (512Kbyte) flash chip will have 2048 pages: 256*2048 = 524288 bytes (512Kbytes)
* For instance a 4MBit (512Kbyte) flash chip will have 2048 pages: 256*2048 = 524288 bytes (512Kbytes) // Minimal modifications should allow chips that have different page size but modifications
* Minimal modifications should allow chips that have different page size but modifications // DEPENDS ON: Arduino SPI library
* DEPENDS ON: Arduino SPI library // > Updated Jan. 5, 2015, TomWS1, modified writeBytes to allow blocks > 256 bytes and handle page misalignment.
* // > Updated Feb. 26, 2015 TomWS1, added support for SPI Transactions (Arduino 1.5.8 and above)
* This file is free software; you can redistribute it and/or modify // > Selective merge by Felix after testing in IDE 1.0.6, 1.6.4
* it under the terms of either the GNU General Public License version 2 // **********************************************************************************
* or the GNU Lesser General Public License version 2.1, both as // License
* published by the Free Software Foundation. // **********************************************************************************
*/ // This program is free software; you can redistribute it
// and/or modify it under the terms of the GNU General
// Public License as published by the Free Software
// Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will
// be useful, but WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General
// Public License along with this program.
// If not, see <http://www.gnu.org/licenses/>.
//
// Licence can be viewed at
// http://www.gnu.org/licenses/gpl-3.0.txt
//
// Please maintain this license information along with authorship
// and copyright notices in any redistribution of this code
#ifndef _SPIFLASH_H_ #ifndef _SPIFLASH_H_
#define _SPIFLASH_H_ #define _SPIFLASH_H_
@ -69,21 +89,21 @@
class SPIFlash { class SPIFlash {
public: public:
static byte UNIQUEID[8]; static uint8_t UNIQUEID[8];
SPIFlash(byte slaveSelectPin, uint16_t jedecID=0); SPIFlash(uint8_t slaveSelectPin, uint16_t jedecID=0);
boolean initialize(); boolean initialize();
void command(byte cmd, boolean isWrite=false); void command(uint8_t cmd, boolean isWrite=false);
byte readStatus(); uint8_t readStatus();
byte readByte(long addr); uint8_t readByte(uint32_t addr);
void readBytes(long addr, void* buf, word len); void readBytes(uint32_t addr, void* buf, uint16_t len);
void writeByte(long addr, byte byt); void writeByte(uint32_t addr, uint8_t byt);
void writeBytes(long addr, const void* buf, uint16_t len); void writeBytes(uint32_t addr, const void* buf, uint16_t len);
boolean busy(); boolean busy();
void chipErase(); void chipErase();
void blockErase4K(long address); void blockErase4K(uint32_t address);
void blockErase32K(long address); void blockErase32K(uint32_t address);
word readDeviceId(); uint16_t readDeviceId();
byte* readUniqueId(); uint8_t* readUniqueId();
void sleep(); void sleep();
void wakeup(); void wakeup();
@ -91,10 +111,13 @@ public:
protected: protected:
void select(); void select();
void unselect(); void unselect();
byte _slaveSelectPin; uint8_t _slaveSelectPin;
uint16_t _jedecID; uint16_t _jedecID;
byte _SPCR; uint8_t _SPCR;
byte _SPSR; uint8_t _SPSR;
#ifdef SPI_HAS_TRANSACTION
SPISettings _settings;
#endif
}; };
#endif #endif