Skip to content

Commit

Permalink
Merge pull request #166 from Marzogh/development
Browse files Browse the repository at this point in the history
v3.3.0 code now frozen and ready for testing prior to deployment.
  • Loading branch information
Marzogh committed Apr 20, 2019
2 parents 5f3f669 + d96a87c commit 45893ff
Show file tree
Hide file tree
Showing 18 changed files with 2,105 additions and 267 deletions.
296 changes: 120 additions & 176 deletions README.md

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions extras/Changes.log
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@
// SPIMemory Library //
// Changes log //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// Version 3.3.0 //
// Author: Prajwal Bhattaram //
// 13.04.2018 //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
Library now supports FRAM memory. Refer to the RAM section of the ReadMe file for more information

Bugs squashed:
--> Fixed a major bug in how SFDP data is used to calculate chip capacity. The previous version of the library returned a wrong value. This has now been fixed. Refer to comments in _getSFDPFlashParam(void) for further details.
--> Fixes bug that causes ballooning of pagesize when SFDP is not read. This is because no default _pageSize value was set in this scenario. This has now been fixed.

Enhancements:
--> Now supports FRAM memory chips via the `SPIFram` constructor
--> Changed Diagnostics output in _chipID() to match situation better.
--> Fixed _chipID() to run more efficiently.

New flash memory chips supported:
--> GD25Q16C from Giga devices (Used on the Adafruit ItsyBitsy M0 Express)
--> W25Q64JV from Winbond

New FRAM memory chips supported:
--> FM25W256 from Cypress
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// Version 3.2.1 //
// Author: Prajwal Bhattaram //
// 13.04.2018 //
Expand Down
2 changes: 2 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#######################################

SPIFlash KEYWORD1
SPIFram KEYWORD1
SPIMemory KEYWORD1

#######################################
Expand Down Expand Up @@ -62,6 +63,7 @@ powerDown KEYWORD2
WINBOND_MANID LITERAL1
MICROCHIP_MANID LITERAL1
CYPRESS_MANID LITERAL1
RAMTRON_FRAM_MANID LITERAL1
ADESTO_MANID LITERAL1
MICRON_MANID LITERAL1
NULLBYTE LITERAL1
Expand Down
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=SPIMemory
version=3.2.1
version=3.3.0
author=Prajwal Bhattaram
maintainer=Prajwal Bhattaram <marzogh@icloud.com>
sentence=SPI Memory library for Arduino. (Formerly SPIFlash)
paragraph=This library enables read, write, erase and power functions on number of compatible flash memory chips. Refer to change log for further information about this release. To download any version of this library pre v3.2.0 (pre name-change from SPIFlash please visit https://github.com/Marzogh/SPIMemory/releases?after=v3.2.0 )
paragraph=This library enables read, write, erase and power functions on number of compatible flash and fram memory chips. Refer to change log for further information about this release. To download any version of this library pre v3.2.0 (pre name-change from SPIFlash please visit https://github.com/Marzogh/SPIMemory/releases?after=v3.2.0 )
category=Data Storage
url=https://github.com/Marzogh/SPIMemory
architectures=avr,sam,samd,esp8266,esp32,Simblee,stm32
Expand Down
9 changes: 4 additions & 5 deletions src/DMASAM.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/* Arduino SPIMemory Library v.2.6.0
/* Arduino SPIMemory Library v.3.3.0
* Copyright (C) 2017 by Prajwal Bhattaram
* Created by Prajwal Bhattaram - 30/09/2016
* Modified by Prajwal Bhattaram - 14/04/2017
* Original code from @manitou48 <https://github.com/manitou48/DUEZoo/blob/master/dmaspi.ino>
* Modified by Prajwal Bhattaram - 19/06/2018
*
* This file is part of the Arduino SPIMemory Library. This library is for
* Winbond NOR flash memory modules. In its current form it enables reading
* and writing individual data variables, structs and arrays from and to various locations;
* reading and writing pages; continuous read functions; sector, block and chip erase;
* Flash and FRAM memory modules. In its current form it enables reading,
* writing and erasing data from and to various locations;
* suspending and resuming programming/erase and powering down for low power operation.
*
* This Library is free software: you can redistribute it and/or modify
Expand Down
9 changes: 4 additions & 5 deletions src/DMASAM.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/* Arduino SPIMemory Library v.3.2.0
/* Arduino SPIMemory Library v.3.3.0
* Copyright (C) 2017 by Prajwal Bhattaram
* Created by Prajwal Bhattaram - 19/04/2018
* Modified by Prajwal Bhattaram - 20/04/2018
* Modified by Prajwal Bhattaram - 19/06/2018
*
* This file is part of the Arduino SPIMemory Library. This library is for
* Winbond NOR flash memory modules. In its current form it enables reading
* and writing individual data variables, structs and arrays from and to various locations;
* reading and writing pages; continuous read functions; sector, block and chip erase;
* Flash and FRAM memory modules. In its current form it enables reading,
* writing and erasing data from and to various locations;
* suspending and resuming programming/erase and powering down for low power operation.
*
* This Library is free software: you can redistribute it and/or modify
Expand Down
158 changes: 149 additions & 9 deletions src/SPIFlash.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/* Arduino SPIMemory Library v.3.2.1
/* Arduino SPIMemory Library v.3.3.0
* Copyright (C) 2017 by Prajwal Bhattaram
* Created by Prajwal Bhattaram - 19/05/2015
* Created by Prajwal Bhattaram - 20/05/2015
* Modified by @boseji <salearj@hotmail.com> - 02/03/2017
* Modified by Prajwal Bhattaram - 21/05/2018
* Modified by Prajwal Bhattaram - 20/04/2019
*
* This file is part of the Arduino SPIMemory Library. This library is for
* Winbond NOR flash memory modules. In its current form it enables reading
* and writing individual data variables, structs and arrays from and to various locations;
* reading and writing pages; continuous read functions; sector, block and chip erase;
* Flash and FRAM memory modules. In its current form it enables reading,
* writing and erasing data from and to various locations;
* suspending and resuming programming/erase and powering down for low power operation.
*
* This Library is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -82,6 +81,7 @@ bool SPIFlash::begin(uint32_t flashChipSize) {
bool retVal = _chipID(flashChipSize);
_endSPI();
chipPoweredDown = false;
_disableGlobalBlockProtect();
return retVal;
}

Expand Down Expand Up @@ -182,7 +182,7 @@ uint64_t SPIFlash::getUniqueID(void) {

//Gets the next available address for use.
// Takes the size of the data as an argument and returns a 32-bit address
// All addresses in the in the sketch must be obtained via this function or not at all.
// This function can be called anytime - even if there is preexisting data on the flash chip. It will simply find the next empty address block for the data.
uint32_t SPIFlash::getAddress(uint16_t size) {
if (!_addressCheck(currentAddress, size)){
return false;
Expand Down Expand Up @@ -348,7 +348,49 @@ float SPIFlash::readFloat(uint32_t _addr, bool fastRead) {
// 2. outputString --> String variable to write the output to
// 3. fastRead --> defaults to false - executes _beginFastRead() if set to true
bool SPIFlash::readStr(uint32_t _addr, String &data, bool fastRead) {
return _read(_addr, data, sizeof(data), fastRead);
//return _read(_addr, data, sizeof(data), fastRead, _STRING_);
#ifdef RUNDIAGNOSTIC
_spifuncruntime = micros();
#endif

uint32_t _sz = 0;

if (!_prep(READDATA, _addr, _sz)) {
return false;
}
else {
if (fastRead) {
_beginSPI(FASTREAD);
}
else {
_beginSPI(READDATA);
}
for (uint16_t i = 0; i < sizeof(_sz); i++) {
_sz |= (_nextByte(READ) << (8*i));
}
_endSPI();
}

char _inChar[_sz];

if (!_addressCheck((_addr + sizeof(_sz)), _sz) || !_notBusy()) {
return false;
}
if(fastRead) {
_beginSPI(FASTREAD);
}
else {
_beginSPI(READDATA);
}
_nextBuf(READDATA, (uint8_t*) &(*_inChar), _sz);
_endSPI();

data = String(_inChar);

#ifdef RUNDIAGNOSTIC
_spifuncruntime = micros() - _spifuncruntime;
#endif
return true;
}

// Writes a byte of data to a specific location in a page.
Expand Down Expand Up @@ -928,7 +970,105 @@ bool SPIFlash::writeFloat(uint32_t _addr, float data, bool errorCheck) {
// WARNING: You can only write to previously erased memory locations (see datasheet).
// Use the eraseSector()/eraseBlock32K/eraseBlock64K commands to first clear memory (write 0xFFs)
bool SPIFlash::writeStr(uint32_t _addr, String &data, bool errorCheck) {
return _write(_addr, data, sizeof(data), errorCheck, _STRING_);

#ifdef RUNDIAGNOSTIC
_spifuncruntime = micros();
#endif

uint32_t _sz = (sizeof(char)*(data.length()+1));

char _outCharArray[_sz];
data.toCharArray(_outCharArray, _sz);

if(_isChipPoweredDown() || !_addressCheck(_addr, sizeof(_sz)) || !_notPrevWritten(_addr, sizeof(_sz)+_sz) || !_notBusy() || !_writeEnable()) {
return false;
}

_beginSPI(PAGEPROG);
for (uint8_t i = 0; i < sizeof(_sz); i++) {
_nextByte(WRITE, _sz >> (8*i));
}
CHIP_DESELECT
_endSPI();

if(!_addressCheck(_addr+sizeof(_sz), _sz) || !_notBusy() || !_writeEnable()) {
return false;
}
uint16_t maxBytes = SPI_PAGESIZE-(_addr % SPI_PAGESIZE); // Force the first set of bytes to stay within the first page

if (_sz <= maxBytes) {
CHIP_SELECT
_nextByte(WRITE, PAGEPROG);
_transferAddress();
//_nextBuf(PAGEPROG, &_outCharArray[0], _sz);
for (uint16_t i = 0; i < _sz; ++i) {
_nextByte(WRITE, _outCharArray[i]);
}
CHIP_DESELECT
}
else {
uint16_t length = _sz;
uint16_t writeBufSz;
uint16_t data_offset = 0;

do {
writeBufSz = (length<=maxBytes) ? length : maxBytes;

CHIP_SELECT
_nextByte(WRITE, PAGEPROG);
_transferAddress();
for (uint16_t i = 0; i < writeBufSz; ++i) {
_nextByte(WRITE, _outCharArray[data_offset + i]);
}
CHIP_DESELECT

_currentAddress += writeBufSz;
data_offset += writeBufSz;
length -= writeBufSz;
maxBytes = 256; // Now we can do up to 256 bytes per loop

if(!_notBusy() || !_writeEnable()){
return false;
}

} while (length > 0);
}

if (!errorCheck) {
_endSPI();
#ifdef RUNDIAGNOSTIC
_spifuncruntime = micros() - _spifuncruntime;
#endif
return true;
}
else {
if (!_notBusy()) {
return false;
}
_currentAddress = (_addr + sizeof(_sz));
char _inCharArray[_sz];

CHIP_SELECT
_nextByte(WRITE, READDATA);
_transferAddress();
for (uint8_t i = 0; i < _sz; i++) {
_inCharArray[i] = _nextByte(READ);
}
_endSPI();

for (uint8_t i = 0; i < _sz; i++) {
if (_inCharArray[i] != _outCharArray[i]) {
#ifdef RUNDIAGNOSTIC
_spifuncruntime = micros() - _spifuncruntime;
#endif
return false;
}
}
#ifdef RUNDIAGNOSTIC
_spifuncruntime = micros() - _spifuncruntime;
#endif
}
return true;
}


Expand Down
37 changes: 22 additions & 15 deletions src/SPIFlash.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/* Arduino SPIMemory Library v.3.2.1
/* Arduino SPIMemory Library v.3.3.0
* Copyright (C) 2017 by Prajwal Bhattaram
* Created by Prajwal Bhattaram - 19/05/2015
* Modified by @boseji <salearj@hotmail.com> - 02/03/2017
* Modified by Prajwal Bhattaram - 21/05/2018
* Modified by Prajwal Bhattaram - 11/08/2018
*
* This file is part of the Arduino SPIMemory Library. This library is for
* Winbond NOR flash memory modules. In its current form it enables reading
* and writing individual data variables, structs and arrays from and to various locations;
* reading and writing pages; continuous read functions; sector, block and chip erase;
* Flash and FRAM memory modules. In its current form it enables reading,
* writing and erasing data from and to various locations;
* suspending and resuming programming/erase and powering down for low power operation.
*
* This Library is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -163,7 +162,8 @@ class SPIFlash {
gpio_t csPin;
#endif
volatile uint8_t *cs_port;
bool pageOverflow, SPIBusState;
bool pageOverflow;
bool SPIBusState = false;
bool chipPoweredDown = false;
bool address4ByteEnabled = false;
bool _loopedOver = false;
Expand Down Expand Up @@ -193,14 +193,14 @@ class SPIFlash {
uint32_t _addressOverflow = false;
uint32_t _BasicParamTableAddr, _SectorMapParamTableAddr, _byteFirstPrgmTime, _byteAddnlPrgmTime, _pagePrgmTime;
uint8_t _uniqueID[8];
const uint8_t _capID[16] =
{0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x43, 0x4B, 0x00, 0x01, 0x13, 0x37};
const uint8_t _capID[18] =
{0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x41, 0x42, 0x43, 0x4B, 0x00, 0x01, 0x13, 0x37};

const uint32_t _memSize[16] =
{KB(64), KB(128), KB(256), KB(512), MB(1), MB(2), MB(4), MB(8), MB(16), MB(32), MB(8), MB(8), KB(256), KB(512), MB(4), KB(512)};
const uint32_t _memSize[18] =
{KB(64), KB(128), KB(256), KB(512), MB(1), MB(2), MB(4), MB(8), MB(16), MB(32), MB(2), MB(4), MB(8), MB(8), KB(256), KB(512), MB(4), KB(512)};
// To understand the _memSize definitions check defines.h

const uint8_t _supportedManID[7] = {WINBOND_MANID, MICROCHIP_MANID, CYPRESS_MANID, ADESTO_MANID, MICRON_MANID, ON_MANID, AMIC_MANID};
const uint8_t _supportedManID[8] = {WINBOND_MANID, MICROCHIP_MANID, CYPRESS_MANID, ADESTO_MANID, MICRON_MANID, ON_MANID, GIGA_MANID, AMIC_MANID};

const uint8_t _altChipEraseReq[3] = {A25L512, M25P40, SST26};
};
Expand Down Expand Up @@ -312,7 +312,11 @@ template <class T> bool SPIFlash::_write(uint32_t _addr, const T& value, uint32_

do {
writeBufSz = (length<=maxBytes) ? length : maxBytes;

if(_currentAddress % SPI_PAGESIZE==0){
CHIP_SELECT
_nextByte(WRITE, PAGEPROG);
_transferAddress();
}
for (uint16_t i = 0; i < writeBufSz; ++i) {
_nextByte(WRITE, *p++);
}
Expand All @@ -337,6 +341,9 @@ template <class T> bool SPIFlash::_write(uint32_t _addr, const T& value, uint32_

if (!errorCheck) {
_endSPI();
#ifdef RUNDIAGNOSTIC
_spifuncruntime = micros() - _spifuncruntime;
#endif
return true;
}
else {
Expand Down Expand Up @@ -367,6 +374,7 @@ template <class T> bool SPIFlash::_read(uint32_t _addr, T& value, uint32_t _sz,
uint8_t* p = (uint8_t*)(void*)&value;

if (_dataType == _STRING_) {
_sz++;
char _inChar[_sz];
_beginSPI(READDATA);
_nextBuf(READDATA, (uint8_t*) &(*_inChar), _sz);
Expand All @@ -378,12 +386,11 @@ template <class T> bool SPIFlash::_read(uint32_t _addr, T& value, uint32_t _sz,
else {
CHIP_SELECT
if (fastRead) {
_nextByte(WRITE, FASTREAD);
_beginSPI(FASTREAD);
}
else {
_nextByte(WRITE, READDATA);
_beginSPI(READDATA);
}
_transferAddress();
for (uint16_t i = 0; i < _sz; i++) {
*p++ =_nextByte(READ);
}
Expand Down

0 comments on commit 45893ff

Please sign in to comment.