Hackscribble FRAM lib
/* | |
Hackscribble_Ferro Library | |
========================== | |
Connects Fujitsu Ferroelectric RAM (MB85RS range) to your | |
Arduino to add up to 32KB of fast, non-volatile storage. | |
For information on how to install and use the library, | |
read "Hackscribble_Ferro user guide.md". | |
Created on 18 April 2014 | |
By Ray Benitez | |
Last modified on 10 June 2014 | |
By Ray Benitez | |
Change history in "README.md" | |
This software is licensed by Ray Benitez under the MIT License. | |
git@hackscribble.com | http://www.hackscribble.com | http://www.twitter.com/hackscribble | |
*/ | |
#include "Hackscribble_Ferro.h" | |
void Hackscribble_Ferro::_initialiseChipSelect() | |
{ | |
#if !defined (SPARK) | |
uint8_t timer = digitalPinToTimer(_chipSelect); | |
_bit = digitalPinToBitMask(_chipSelect); | |
uint8_t port = digitalPinToPort(_chipSelect); | |
if (port == NOT_A_PIN) return; | |
// If the pin that support PWM output, we need to turn it off | |
// before doing a digital write. | |
if (timer != NOT_ON_TIMER) turnOffPWM(timer); | |
_out = portOutputRegister(port); | |
#endif | |
} | |
void Hackscribble_Ferro::_select() | |
{ | |
#if defined (SPARK) | |
PIN_MAP[_chipSelect].gpio_peripheral->BRR = PIN_MAP[_chipSelect].gpio_pin; //FRAM CS LOW | |
#else | |
digitalWrite(_chipSelect, LOW); | |
uint8_t oldSREG = SREG; | |
cli(); | |
*_out &= ~_bit; | |
SREG = oldSREG; | |
#endif | |
} | |
void Hackscribble_Ferro::_deselect() | |
{ | |
#if defined (SPARK) | |
PIN_MAP[_chipSelect].gpio_peripheral->BSRR = PIN_MAP[_chipSelect].gpio_pin; //FRAM CS HIGH | |
#else | |
digitalWrite(_chipSelect, HIGH); | |
uint8_t oldSREG = SREG; | |
cli(); | |
*_out |= _bit; | |
SREG = oldSREG; | |
#endif | |
} | |
Hackscribble_Ferro::Hackscribble_Ferro(ferroPartNumber partNumber, byte chipSelect): _partNumber(partNumber), _chipSelect(chipSelect) | |
{ | |
_topAddressForPartNumber[MB85RS16] = 0x07FF; | |
_topAddressForPartNumber[MB85RS64] = 0x1FFF; | |
_topAddressForPartNumber[MB85RS128A] = 0x3FFF; | |
_topAddressForPartNumber[MB85RS128B] = 0x3FFF; | |
_topAddressForPartNumber[MB85RS256A] = 0x7FFF; | |
_topAddressForPartNumber[MB85RS256B] = 0x7FFF; | |
_baseAddress = 0x0000; | |
_bottomAddress = _baseAddress + _maxBufferSize; | |
_topAddress = _topAddressForPartNumber[_partNumber]; | |
_numberOfBuffers = (_topAddress - _bottomAddress + 1) / _maxBufferSize; | |
_chipSelect = chipSelect; | |
// Set the standard SS pin as an output to keep Arduino SPI happy | |
pinMode (SS, OUTPUT); | |
// Set CS to inactive | |
_initialiseChipSelect(); | |
pinMode (_chipSelect, OUTPUT); | |
_deselect(); | |
_nextFreeByte = _bottomAddress; | |
} | |
ferroResult Hackscribble_Ferro::begin() | |
{ | |
// Initialize SPI | |
SPI.begin(); | |
SPI.setBitOrder(MSBFIRST); | |
SPI.setDataMode (SPI_MODE0); | |
SPI.setClockDivider(SPI_CLOCK_DIV2); | |
// Check that the FRAM is reachable | |
return checkForFRAM(); | |
} | |
ferroPartNumber Hackscribble_Ferro::getPartNumber() | |
{ | |
return _partNumber; | |
} | |
unsigned int Hackscribble_Ferro::getMaxBufferSize() | |
{ | |
return _maxBufferSize; | |
} | |
unsigned int Hackscribble_Ferro::getBottomAddress() | |
{ | |
return _bottomAddress; | |
} | |
unsigned int Hackscribble_Ferro::getTopAddress() | |
{ | |
return _topAddress; | |
} | |
ferroResult Hackscribble_Ferro::checkForFRAM() | |
{ | |
// Tests that the unused status register bits can be read, inverted, written back and read again | |
const byte srMask = 0x70; // Unused bits are bits 6..4 | |
byte registerValue = 0; | |
byte newValue = 0; | |
boolean isPresent = true; | |
// Read current value | |
_select(); | |
SPI.transfer(_RDSR); | |
registerValue = SPI.transfer(_dummy); | |
_deselect(); | |
// Invert current value | |
newValue = registerValue ^ srMask; | |
// Write new value | |
_select(); | |
SPI.transfer(_WREN); | |
_deselect(); | |
_select(); | |
SPI.transfer(_WRSR); | |
SPI.transfer(newValue); | |
_deselect(); | |
// Read again | |
_select(); | |
SPI.transfer(_RDSR); | |
registerValue = SPI.transfer(_dummy); | |
_deselect(); | |
if (((registerValue & srMask) == (newValue & srMask))) | |
{ | |
return ferroOK; | |
} | |
else | |
{ | |
return ferroBadResponse; | |
} | |
} | |
unsigned int Hackscribble_Ferro::getControlBlockSize() | |
{ | |
return _maxBufferSize; | |
} | |
void Hackscribble_Ferro::writeControlBlock(byte *buffer) | |
{ | |
_select(); | |
SPI.transfer(_WREN); | |
_deselect(); | |
_select(); | |
SPI.transfer(_WRITE); | |
SPI.transfer(_baseAddress / 256); | |
SPI.transfer(_baseAddress % 256); | |
for (byte i = 0; i < _maxBufferSize; i++) | |
{ | |
SPI.transfer(buffer[i]); | |
} | |
_deselect(); | |
} | |
void Hackscribble_Ferro::readControlBlock(byte *buffer) | |
{ | |
_select(); | |
SPI.transfer(_READ); | |
SPI.transfer(_baseAddress / 256); | |
SPI.transfer(_baseAddress % 256); | |
for (byte i = 0; i < _maxBufferSize; i++) | |
{ | |
buffer[i] = SPI.transfer(_dummy); | |
} | |
_deselect(); | |
} | |
ferroResult Hackscribble_Ferro::read(unsigned int startAddress, byte numberOfBytes, byte *buffer) | |
{ | |
// Copies numberOfBytes bytes from FRAM (starting at startAddress) into buffer (starting at 0) | |
// Returns result code | |
// Validations: | |
// _bottomAddress <= startAddress <= _topAddress | |
// 0 < numberOfBytes <= maxBuffer | |
// startAddress + numberOfBytes <= _topAddress | |
if ((startAddress < _bottomAddress) || (startAddress > _topAddress)) | |
{ | |
return ferroBadStartAddress; | |
} | |
if ((numberOfBytes > _maxBufferSize) || (numberOfBytes == 0)) | |
{ | |
return ferroBadNumberOfBytes; | |
} | |
if ((startAddress + numberOfBytes - 1) > _topAddress) | |
{ | |
return ferroBadFinishAddress; | |
} | |
_select(); | |
SPI.transfer(_READ); | |
SPI.transfer(startAddress / 256); | |
SPI.transfer(startAddress % 256); | |
for (byte i = 0; i < numberOfBytes; i++) | |
{ | |
buffer[i] = SPI.transfer(_dummy); | |
} | |
_deselect(); | |
return ferroOK; | |
} | |
ferroResult Hackscribble_Ferro::write(unsigned int startAddress, byte numberOfBytes, byte *buffer) | |
{ | |
// Copies numberOfBytes bytes from buffer (starting at 0) into FRAM (starting at startAddress) | |
// Returns result code | |
// Validations: | |
// _bottomAddress <= startAddress <= _topAddress | |
// 0 < numberOfBytes <= maxBuffer | |
// startAddress + numberOfBytes - 1 <= _topAddress | |
if ((startAddress < _bottomAddress) || (startAddress > _topAddress)) | |
{ | |
return ferroBadStartAddress; | |
} | |
if ((numberOfBytes > _maxBufferSize) || (numberOfBytes == 0)) | |
{ | |
return ferroBadNumberOfBytes; | |
} | |
if ((startAddress + numberOfBytes - 1) > _topAddress) | |
{ | |
return ferroBadFinishAddress; | |
} | |
_select(); | |
SPI.transfer(_WREN); | |
_deselect(); | |
_select(); | |
SPI.transfer(_WRITE); | |
SPI.transfer(startAddress / 256); | |
SPI.transfer(startAddress % 256); | |
for (byte i = 0; i < numberOfBytes; i++) | |
{ | |
SPI.transfer(buffer[i]); | |
} | |
_deselect(); | |
return ferroOK; | |
} | |
unsigned int Hackscribble_Ferro::allocateMemory(unsigned int numberOfBytes, ferroResult& result) | |
{ | |
if ((_nextFreeByte + numberOfBytes) < _topAddress) | |
{ | |
unsigned int base = _nextFreeByte; | |
_nextFreeByte += numberOfBytes; | |
result = ferroOK; | |
return base; | |
} | |
else | |
{ | |
result = ferroBadFinishAddress; | |
return 0; | |
} | |
} | |
ferroResult Hackscribble_Ferro::format() | |
{ | |
// Fills FRAM with 0 but does NOT overwrite control block | |
// Returns result code from ferroWrite function, or ferroOK if format is successful | |
byte buffer[_maxBufferSize]; | |
for (byte i = 0; i < _maxBufferSize; i++) | |
{ | |
buffer[i] = 0; | |
} | |
ferroResult result = ferroOK; | |
unsigned int i = _bottomAddress; | |
while ((i < _topAddress) && (result == ferroOK)) | |
{ | |
result = write(i, _maxBufferSize, buffer); | |
i += _maxBufferSize; | |
} | |
return result; | |
} | |
Hackscribble_FerroArray::Hackscribble_FerroArray(Hackscribble_Ferro& f, unsigned int numberOfElements, byte sizeOfElement, ferroResult &result): _f(f), _numberOfElements(numberOfElements), _sizeOfElement(sizeOfElement) | |
{ | |
// Creates array in FRAM | |
// Calculates and allocates required memory | |
// Returns result code | |
// Validations: | |
// _sizeOfElement <= _bufferSize | |
if (_sizeOfElement < _f.getMaxBufferSize()) | |
{ | |
_startAddress = _f.allocateMemory(_numberOfElements * _sizeOfElement, result); | |
} | |
else | |
{ | |
result = ferroArrayElementTooBig; | |
_startAddress = 0; | |
} | |
} | |
void Hackscribble_FerroArray::readElement(unsigned int index, byte *buffer, ferroResult &result) | |
{ | |
// Reads element from array in FRAM | |
// Returns result code | |
// Validations: | |
// _startAddress > 0 (otherwise array has probably not been created) | |
// index < _numberOfElements | |
if (_startAddress == 0) | |
{ | |
result = ferroBadArrayStartAddress; | |
} | |
else if (index >= _numberOfElements) | |
{ | |
result = ferroBadArrayIndex; | |
} | |
else | |
{ | |
result = _f.read(_startAddress + (index * _sizeOfElement), _sizeOfElement, buffer); | |
} | |
} | |
void Hackscribble_FerroArray::writeElement(unsigned int index, byte *buffer, ferroResult &result) | |
{ | |
// Writes element to array in FRAM | |
// Returns result code | |
// Validations: | |
// _startAddress > 0 (otherwise array has probably not been created) | |
// index < _numberOfElements | |
if (_startAddress == 0) | |
{ | |
result = ferroBadArrayStartAddress; | |
} | |
else if (index >= _numberOfElements) | |
{ | |
result = ferroBadArrayIndex; | |
} | |
else | |
{ | |
result = _f.write(_startAddress + (index * _sizeOfElement), _sizeOfElement, buffer); | |
} | |
} | |
unsigned int Hackscribble_FerroArray::getStartAddress() | |
{ | |
return _startAddress; | |
} |
/* | |
Hackscribble_Ferro Library | |
========================== | |
Connects Fujitsu Ferroelectric RAM (MB85RS range) to your | |
Arduino to add up to 32KB of fast, non-volatile storage. | |
For information on how to install and use the library, | |
read "Hackscribble_Ferro user guide.md". | |
Created on 18 April 2014 | |
By Ray Benitez | |
Last modified on 10 June 2014 | |
By Ray Benitez | |
Change history in "README.md" | |
This software is licensed by Ray Benitez under the MIT License. | |
git@hackscribble.com | http://www.hackscribble.com | http://www.twitter.com/hackscribble | |
*/ | |
#ifndef HACKSCRIBBLE_FERRO_H | |
#define HACKSCRIBBLE_FERRO_H | |
#include "application.h" | |
// MB85RS part numbers | |
enum ferroPartNumber | |
{ | |
MB85RS16 = 0, | |
MB85RS64, | |
MB85RS128A, | |
MB85RS128B, | |
MB85RS256A, | |
MB85RS256B, | |
numberOfPartNumbers | |
}; | |
// Result codes | |
enum ferroResult | |
{ | |
ferroOK = 0, | |
ferroBadStartAddress, | |
ferroBadNumberOfBytes, | |
ferroBadFinishAddress, | |
ferroArrayElementTooBig, | |
ferroBadArrayIndex, | |
ferroBadArrayStartAddress, | |
ferroBadResponse, | |
ferroUnknownError = 99 | |
}; | |
class Hackscribble_Ferro | |
{ | |
private: | |
ferroPartNumber _partNumber; | |
byte _chipSelect; | |
#if !defined (SPARK) | |
volatile uint8_t *_out; | |
uint8_t _bit; | |
#endif | |
// FRAM opcodes | |
static const byte _WREN = 0x06; | |
static const byte _WRDI = 0x04; | |
static const byte _WRITE = 0x02; | |
static const byte _READ = 0x03; | |
static const byte _RDSR = 0x05; | |
static const byte _WRSR = 0x01; | |
// Dummy write value for SPI read | |
static const byte _dummy = 0x00; | |
//static const byte _dummy = 0x7E; | |
// Set maximum size of buffer used to write to and read from FRAM | |
// Do not exceed 0x80 to prevent problems with maximum size structs in FerroArray | |
static const unsigned int _maxBufferSize = 0x40; | |
// Used in constructor to set size of usable FRAM memory, reserving some bytes as a control block | |
/* static const*/ unsigned int _topAddressForPartNumber[numberOfPartNumbers]; | |
unsigned int _baseAddress; | |
unsigned int _bottomAddress; | |
unsigned int _topAddress; | |
unsigned int _numberOfBuffers; | |
// FRAM current next byte to allocate | |
unsigned int _nextFreeByte; | |
void _initialiseChipSelect(); | |
void _select(); | |
void _deselect(); | |
public: | |
Hackscribble_Ferro(ferroPartNumber partNumber = MB85RS128A, byte chipSelect = SS); | |
ferroResult begin(); | |
ferroPartNumber getPartNumber(); | |
unsigned int getMaxBufferSize(); | |
unsigned int getBottomAddress(); | |
unsigned int getTopAddress(); | |
ferroResult checkForFRAM(); | |
unsigned int getControlBlockSize(); | |
void writeControlBlock(byte *buffer); | |
void readControlBlock(byte *buffer); | |
ferroResult read(unsigned int startAddress, byte numberOfBytes, byte *buffer); | |
ferroResult write(unsigned int startAddress, byte numberOfBytes, byte *buffer); | |
unsigned int allocateMemory(unsigned int numberOfBytes, ferroResult& result); | |
ferroResult format(); | |
}; | |
class Hackscribble_FerroArray | |
{ | |
private: | |
unsigned int _numberOfElements; | |
byte _sizeOfElement; | |
unsigned int _startAddress; | |
Hackscribble_Ferro& _f; | |
public: | |
Hackscribble_FerroArray(Hackscribble_Ferro& f, unsigned int numberOfElements, byte sizeOfElement, ferroResult &result); | |
void readElement(unsigned int index, byte *buffer, ferroResult &result); | |
void writeElement(unsigned int index, byte *buffer, ferroResult &result); | |
unsigned int getStartAddress(); | |
}; | |
#endif |
/* | |
test | |
==== | |
Example sketch for Hackscribble_Ferro Library. | |
For information on how to install and use the library, | |
read "Hackscribble_Ferro user guide.md". | |
Created on 18 April 2014 | |
By Ray Benitez | |
Last modified on --- | |
By --- | |
Change history in "README.md" | |
This software is licensed by Ray Benitez under the MIT License. | |
git@hackscribble.com | http://www.hackscribble.com | http://www.twitter.com/hackscribble | |
*/ | |
////////////////////////////////////////////////////////////////////////////////////////////////////// | |
// Prerequisites for Hackscribble_Ferro | |
////////////////////////////////////////////////////////////////////////////////////////////////////// | |
#if defined (SPARK) | |
#include "Hackscribble_Ferro.h" | |
void printPartNumber(ferroPartNumber pn); | |
#else | |
#include <SPI.h> | |
#include <Hackscribble_Ferro.h> | |
#endif | |
////////////////////////////////////////////////////////////////////////////////////////////////////// | |
// Free memory utility from www.arduino.cc | |
////////////////////////////////////////////////////////////////////////////////////////////////////// | |
#if !defined (SPARK) | |
int freeRam () { | |
extern int __heap_start, *__brkval; | |
int v; | |
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); | |
} | |
#endif | |
void printPartNumber(ferroPartNumber pn) | |
{ | |
switch(pn) | |
{ | |
case MB85RS16: | |
Serial.print("MB85RS16"); | |
break; | |
case MB85RS64: | |
Serial.print("MB85RS64"); | |
break; | |
case MB85RS128A: | |
Serial.print("MB85RS128A"); | |
break; | |
case MB85RS128B: | |
Serial.print("MB85RS128B"); | |
break; | |
case MB85RS256A: | |
Serial.print("MB85RS256A"); | |
break; | |
case MB85RS256B: | |
Serial.print("MB85RS256B"); | |
break; | |
default : | |
Serial.print("unknown part number"); | |
} | |
} | |
void setup() | |
{ | |
Serial.begin(115200); | |
Serial.println("Press any key to begin..."); | |
while(!Serial.available()) SPARK_WLAN_Loop(); | |
Serial.println(F("\n\n\n\n\nHACKSCRIBBLE_FERRO TESTS")); | |
Serial.println(F("========================\n")); | |
// Hackscribble_Ferro library uses standard Arduino SPI pin definitions: MOSI, MISO, SCK. | |
// Next statement creates an instance of Ferro using the standard Arduino SS pin and | |
// default FRAM part number, MB85RS128A. | |
Hackscribble_Ferro myFerro(MB85RS256A, D3); | |
// You specify a different FRAM part number like this ... | |
// Hackscribble_Ferro myFerro(MB85RS256B); | |
// If required, you can use a different chip select pin instead of SS, for example | |
// when you have multiple devices on the SPI bus. You specify the pin like this ... | |
// Hackscribble_Ferro myFerro(MB85RS128A, 9); | |
Serial.println("Press any key to begin..."); | |
while(!Serial.available()) SPARK_WLAN_Loop(); | |
// FRAM TESTS | |
// TEST 1 | |
Serial.println(F("\nTest: check for correct response from FRAM")); | |
Serial.println(F("Expected result: FRAM response OK; part number = MB85RS128A")); | |
Serial.println(F("Result ...")); | |
if (myFerro.begin() == ferroBadResponse) | |
{ | |
Serial.println(F("FRAM response not OK")); | |
} | |
else | |
{ | |
Serial.println(F("FRAM response OK")); | |
printPartNumber(myFerro.getPartNumber()); | |
Serial.println(); | |
ferroResult myResult = ferroUnknownError; | |
unsigned int myBufferSize = myFerro.getMaxBufferSize(); | |
unsigned int myBottom = myFerro.getBottomAddress(); | |
unsigned int myTop = myFerro.getTopAddress(); | |
// TEST 2 | |
byte ferroBuffer[myBufferSize]; | |
for (byte i = 0; i < myBufferSize; i++) | |
{ | |
ferroBuffer[i] = 0xFF; | |
} | |
Serial.println(F("\nTest: write zero-filled buffer to all available space in FRAM")); | |
Serial.println(F("Expected result: incrementing start address for each write; all result codes = 0")); | |
Serial.println(F("Result ...")); | |
for (unsigned int i = myBottom; i < myTop; i += myBufferSize) | |
{ | |
Serial.print(i, HEX); | |
Serial.print(F(" ")); | |
Serial.print(myFerro.write(i, myBufferSize, ferroBuffer)); | |
Serial.print(F(" ")); | |
} | |
Serial.println(); | |
// TEST 3 | |
const byte nRead = 20; | |
Serial.println(F("\nTest: read subset of data back from FRAM")); | |
Serial.println(F("Expected result: result code = 0; each value = 0")); | |
Serial.println(F("Result ...")); | |
// Read nRead bytes from an arbitrary address in FRAM to buffer | |
Serial.println(myFerro.read(5150, nRead, ferroBuffer)); | |
for (byte i = 0; i < nRead; i++) | |
{ | |
Serial.print(ferroBuffer[i]); | |
Serial.print(F(" ")); | |
} | |
Serial.println(); | |
// TEST 4 | |
for (byte i = 0; i < myBufferSize; i++) | |
{ | |
ferroBuffer[i] = (42 + i); | |
} | |
Serial.println(F("\nTest: fill buffer with test data")); | |
Serial.println(F("Expected result: incrementing data starting at 42")); | |
Serial.println(F("Result ...")); | |
for (byte i = 0; i < myBufferSize; i++) | |
{ | |
Serial.print(ferroBuffer[i]); | |
Serial.print(F(" ")); | |
} | |
Serial.println(); | |
// TEST 5 | |
Serial.println(F("\nTest: write buffer to FRAM")); | |
Serial.println(F("Expected result: result code = 0")); | |
Serial.println(F("Result ...")); | |
// Write 60 bytes from buffer to an arbitrary address in FRAM | |
Serial.println(myFerro.write(5100, 60, ferroBuffer)); | |
for (byte i = 0; i < myBufferSize; i++) | |
{ | |
ferroBuffer[i] = 0; | |
} | |
// TEST 6 | |
Serial.println(F("\nTest: read subset of data back from FRAM")); | |
Serial.println(F("Expected result: result code = 0; twenty incrementing values starting at 72")); | |
Serial.println(F("Result ...")); | |
Serial.println(myFerro.read(5130, nRead, ferroBuffer)); | |
for (byte i = 0; i < nRead; i++) | |
{ | |
Serial.print(ferroBuffer[i]); | |
Serial.print(F(" ")); | |
} | |
Serial.println(); | |
// FRAM CONTROL BLOCK TESTS | |
// TEST 7 | |
byte myControlBufferSize = myFerro.getControlBlockSize(); | |
byte ferroControlBuffer[myControlBufferSize]; | |
for (byte i = 0; i < myControlBufferSize; i++) | |
{ | |
ferroControlBuffer[i] = 123; | |
} | |
myFerro.writeControlBlock(ferroControlBuffer); | |
for (byte i = 0; i < myControlBufferSize; i++) | |
{ | |
ferroControlBuffer[i] = 255; | |
} | |
myFerro.readControlBlock(ferroControlBuffer); | |
Serial.println(F("\nTest: read test data from control block")); | |
Serial.println(F("Expected result: each value = 123")); | |
Serial.println(F("Result ...")); | |
for (byte i = 0; i < myControlBufferSize; i++) | |
{ | |
Serial.print(ferroControlBuffer[i]); | |
Serial.print(F(" ")); | |
} | |
Serial.println(); | |
// TEST 8 | |
for (byte i = 0; i < myControlBufferSize; i++) | |
{ | |
ferroControlBuffer[i] = 43; | |
} | |
myFerro.writeControlBlock(ferroControlBuffer); | |
for (byte i = 0; i < myControlBufferSize; i++) | |
{ | |
ferroControlBuffer[i] = 255; | |
} | |
myFerro.readControlBlock(ferroControlBuffer); | |
Serial.println(F("\nTest: read test data from control block")); | |
Serial.println(F("Expected result: each value = 43")); | |
Serial.println(F("Result ...")); | |
for (byte i = 0; i < myControlBufferSize; i++) | |
{ | |
Serial.print(ferroControlBuffer[i]); | |
Serial.print(F(" ")); | |
} | |
Serial.println(); | |
// FRAM ARRAY TESTS | |
// TEST 9 | |
struct testStruct | |
{ | |
unsigned long frequency; | |
unsigned int v1; | |
unsigned int v2; | |
}; | |
myResult = ferroUnknownError; | |
Serial.println(F("\nTest: create a FerroArray of struct")); | |
Serial.println(F("Expected result: result code = 0; start address of array = 64")); | |
Serial.println(F("Result ...")); | |
Hackscribble_FerroArray myArray(myFerro, 20, sizeof(testStruct), myResult); | |
Serial.println(myResult); | |
Serial.println(myArray.getStartAddress()); | |
// TEST 10 | |
Serial.println(F("\nTest: create a FerroArray of float")); | |
Serial.println(F("Expected result: result code = 0; start address of array = 224")); | |
Serial.println(F("Result ...")); | |
Hackscribble_FerroArray anotherArray(myFerro, 200, sizeof(float), myResult); | |
Serial.println(myResult); | |
Serial.println(anotherArray.getStartAddress()); | |
// TEST 11 | |
// Create this dummy array to check the size of the previous float array | |
Serial.println(F("\nTest: create a FerroArray of int")); | |
Serial.println(F("Expected result: start address of array = 1024")); | |
Serial.println(F("Result ...")); | |
Hackscribble_FerroArray dummyArray(myFerro, 300, sizeof(int), myResult); | |
Serial.println(dummyArray.getStartAddress()); | |
#if !defined (SPARK) | |
Serial.print(F("\n\n\nFREE MEMORY: ")); | |
Serial.println(freeRam()); | |
#endif | |
// TEST 12 | |
// Write arbitrary test data to struct array | |
testStruct myStruct = {14123456, 1234, 4320}; | |
testStruct newStruct = {0, 0.00, 0.00}; | |
Serial.println(F("\nTest: write struct with test data to array")); | |
Serial.println(F("Expected result: 14123456, 1234, 4320; result code = 0")); | |
Serial.println(F("Result ...")); | |
Serial.print(myStruct.frequency); | |
Serial.print(F("\t")); | |
Serial.print(myStruct.v1); | |
Serial.print(F("\t")); | |
Serial.println(myStruct.v2); | |
myArray.writeElement(12, (byte*)&myStruct, myResult); | |
Serial.println(myResult); | |
// TEST 13 | |
Serial.println(F("\nTest: read test data from array into a new struct")); | |
Serial.println(F("Expected result: result code = 0; 14123456, 1234, 4320")); | |
Serial.println(F("Result ...")); | |
myArray.readElement(12, (byte*)&newStruct, myResult); | |
Serial.println(myResult); | |
Serial.print(newStruct.frequency); | |
Serial.print(F("\t")); | |
Serial.print(newStruct.v1); | |
Serial.print(F("\t")); | |
Serial.println(newStruct.v2); | |
// TEST 14 | |
// Write arbitrary test data to float array | |
float myFloat = 3.14; | |
float newFloat = 0.0; | |
Serial.println(F("\nTest: write float with test data to array")); | |
Serial.println(F("Expected result: 3.14; result code = 0")); | |
Serial.println(F("Result ...")); | |
Serial.println(myFloat); | |
anotherArray.writeElement(12, (uint8_t*)&myFloat, myResult); | |
Serial.println(myResult); | |
// TEST 15 | |
Serial.println(F("\nTest: read test data from array into a new struct")); | |
Serial.println(F("Expected result: result code = 0; 3.14")); | |
Serial.println(F("Result ...")); | |
anotherArray.readElement(12, (uint8_t*)&newFloat, myResult); | |
Serial.println(myResult); | |
Serial.println(newFloat); | |
// TEST 16 | |
// Write arbitrary test data to int array | |
int16_t myInt = -123; | |
int16_t newInt = 0; | |
Serial.println(F("\nTest: write int with test data to array")); | |
Serial.println(F("Expected result: -123; result code = 0")); | |
Serial.println(F("Result ...")); | |
Serial.println(myInt); | |
dummyArray.writeElement(212, (uint8_t*)&myInt, myResult); | |
Serial.println(myResult); | |
// TEST 17 | |
Serial.println(F("\nTest: read test data from array into a new int")); | |
Serial.println(F("Expected result: result code = 0; -123")); | |
Serial.println(F("Result ...")); | |
dummyArray.readElement(212, (uint8_t*)&newInt, myResult); | |
Serial.println(myResult); | |
Serial.println(newInt); | |
// FRAM FORMAT TESTS | |
// TEST 18 | |
myResult = ferroUnknownError; | |
Serial.println(F("\nTest: format the FRAM and read subset of data")); | |
Serial.println(F("Expected result: result codes = 0 and 0; each value = 0")); | |
Serial.println(F("Result ...")); | |
Serial.println(myFerro.format()); | |
Serial.println(myFerro.read(5130, nRead, ferroBuffer)); | |
for (byte i = 0; i < nRead; i++) | |
{ | |
Serial.print(ferroBuffer[i]); | |
Serial.print(F(" ")); | |
} | |
Serial.println(); | |
// TEST 19 | |
// Control block should not have been erased by format | |
myFerro.readControlBlock(ferroControlBuffer); | |
Serial.println(F("\nTest: read test data from control block")); | |
Serial.println(F("Expected result: each value = 43")); | |
Serial.println(F("Result ...")); | |
for (byte i = 0; i < myControlBufferSize; i++) | |
{ | |
Serial.print(ferroControlBuffer[i]); | |
Serial.print(F(" ")); | |
} | |
Serial.println(); | |
#if !defined (SPARK) | |
Serial.print(F("\n\n\nFREE MEMORY: ")); | |
Serial.println(freeRam()); | |
#endif | |
Serial.println(F("\n\n\nEND OF TESTS")); | |
Serial.println(F("============\n")); | |
} | |
} | |
void loop() | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment