hi guys,
ive written some code on the UNO that relies on EEPROM.read and EEPROM.write and I want to be able to do the same thing on a portenta. I have established that there is no EEPROM and following this tutorial:
I have taken the string thats in the flash memory, (which in my case only ever needs to be two digits) and used the toInt() function in order to get an INT from it and be able to manipulate that INT in my original code before saving it again.
my code is here:
#include "QSPIFBlockDevice.h"
#include "MBRBlockDevice.h"
using namespace mbed;
#define BLOCK_DEVICE_SIZE 1024 * 8 // 8 KB
#define PARTITION_TYPE 0x0B // FAT 32
int int11 = 5;
int int12 = 2;
//int intStored=0;
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println("QSPI Block Device Test");
Serial.println("------------------------");
// Feed the random number generator for later content generation
randomSeed(analogRead(0));
// Create a block device on the available space of the flash
QSPIFBlockDevice root(PD_11, PD_12, PF_7, PD_13, PF_10, PG_6, QSPIF_POLARITY_MODE_1, 40000000);
MBRBlockDevice blockDevice(&root, 1);
//MBRBlockDevice blockDevice2(&root, 21);
// Initialize the Flash IAP block device and print the memory layout
if (blockDevice.init() != 0 || blockDevice.size() != BLOCK_DEVICE_SIZE) {
Serial.println("Partitioning block device...");
blockDevice.deinit();
// Allocate a FAT 32 partition
MBRBlockDevice::partition(&root, 1, PARTITION_TYPE, 0, BLOCK_DEVICE_SIZE);
blockDevice.init();
}
/*if (blockDevice2.init() != 0 || blockDevice.size() != BLOCK_DEVICE_SIZE) {
Serial.println("Partitioning block device2...");
blockDevice2.deinit();
// Allocate a FAT 32 partition
MBRBlockDevice::partition(&root, 21, PARTITION_TYPE, 0, BLOCK_DEVICE_SIZE);
blockDevice2.init();
}*/
const auto eraseBlockSize = blockDevice.get_erase_size();
const auto programBlockSize = blockDevice.get_program_size();
Serial.println("Block device size: " + String((unsigned int) blockDevice.size() / 1024) + " KB");
Serial.println("Readable block size: " + String((unsigned int) blockDevice.get_read_size()) + " bytes");
Serial.println("Programmable block size: " + String((unsigned int) programBlockSize) + " bytes");
Serial.println("Erasable block size: " + String((unsigned int) eraseBlockSize / 1024) + " KB");
String int11Mem = String(int11);
String int12Mem = String(int12);
// Calculate the amount of bytes needed to store the message
// This has to be a multiple of the program block size
const auto messageSize = int11Mem.length() + 1; // C String takes 1 byte for NULL termination
const unsigned int requiredEraseBlocks = ceil(messageSize / (float) eraseBlockSize);
const unsigned int requiredBlocks = ceil(messageSize / (float) programBlockSize);
const auto dataSize = requiredBlocks * programBlockSize;
char buffer[dataSize] {};
// char buffer[dataSize] {};
// Read back what was stored at previous execution
Serial.println("Reading stored int11");
blockDevice.read(buffer, 0, dataSize);
Serial.println(buffer);
String int11Read = buffer; // variable to store char
// Erase a block starting at the offset 0 relative
// to the block device start address
blockDevice.erase(0, requiredEraseBlocks * eraseBlockSize);
/* Serial.println("Reading stored int12");
delay(10);
blockDevice2.read(buffer, 20, dataSize);
Serial.println(buffer);
String int12Read = buffer; // variable to store char
//Serial.print("int11 stored : ");
//Serial.println(int11Mem);
// Erase a block starting at the offset 0 relative
// to the block device start address
blockDevice2.erase(20, requiredEraseBlocks * eraseBlockSize);*/
// Write an updated message to the first block
Serial.println("int11 string to int");
int int11Stored = int11Read.toInt();
Serial.println(int11Stored);
/*Serial.println("int12 string to int");
int int12Stored = int12Read.toInt();
Serial.println(int12Stored);*/
Serial.print("new int11 incremented : ");
int11Stored = int11Stored + 1;
if (int11Stored > 9) {
int11Stored = 0;
}
Serial.println(int11Stored);
/*Serial.print("new int12 incremented : ");
int12Stored = int12Stored + 1;
if (int12Stored > 9) {
int12Stored = 0;
}
Serial.println(int12Stored);*/
Serial.println("Writing new message 11");
String messageToStore11 = String(int11Stored);
Serial.println(messageToStore11);
blockDevice.program(messageToStore11.c_str(), 0, dataSize);
/*Serial.println("Writing new message 12");
String messageToStore12 = String(int12Stored);
Serial.println(messageToStore12);
blockDevice.program(messageToStore12.c_str(), 20, dataSize);*/
// Deinitialize the device
blockDevice.deinit();
Serial.println("Done.");
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(300);
digitalWrite(LED_BUILTIN, LOW);
delay(300);
}
that works ok, or at least it seems, for one String turning into one INT. I need to do it at least two, preferably 6 times. Could anyone provide me some help and pointers as to what I need to do in order to get this running multiple times? as you can see ive tried just dumb luck duplicating code and changing variable names etc, but to no avail
thanks in advance