Hi all,
I have a "S25FL128LAGNFI010" SPI flash memory in my circuit and I'm using a custom board with "ATMEGA32U4".
I have tried Marzogh's Flash memory library and used the code below to read and write Strings. His code is as follows:
/*
readWriteString.ino |
SPIMemory library |
v 3.0.0 |
Marzogh
29.05.2017
This program shows the method of reading a string from the console and saving it to flash memory
*/
#include"SPIMemory.h"
uint32_t strAddr;
#if defined(ARDUINO_SAMD_ZERO) && defined(SERIAL_PORT_USBVIRTUAL)
// Required for Serial on Zero based boards
#define Serial SERIAL_PORT_USBVIRTUAL
#endif
#if defined (SIMBLEE)
#define BAUD_RATE 250000
#define RANDPIN 1
#else
#define BAUD_RATE 115200
#define RANDPIN A0
#endif
//SPIFlash flash(SS1, &SPI1); //Use this constructor if using an SPI bus other than the default SPI. Only works with chips with more than one hardware SPI bus
SPIFlash flash;
bool readSerialStr(String &inputStr);
void setup() {
Serial.begin(BAUD_RATE);
#if defined (ARDUINO_SAMD_ZERO) || (__AVR_ATmega32U4__)
while (!Serial) ; // Wait for Serial monitor to open
#endif
flash.begin();
randomSeed(analogRead(RANDPIN));
strAddr = random(0, flash.getCapacity());
String inputString = "THis jansa";
flash.writeStr(strAddr, inputString);
Serial.print(F("Written string: "));
Serial.println(inputString);
Serial.print(F("To address: "));
Serial.println(strAddr);
String outputString = "";
if (flash.readStr(strAddr, outputString)) {
Serial.print(F("Read string: "));
Serial.println(outputString);
Serial.print(F("From address: "));
Serial.println(strAddr);
}
while (!flash.eraseSector(strAddr));
}
void loop() {
}
//Reads a string from Serial
bool readSerialStr(String &inputStr) {
if (!Serial)
Serial.begin(115200);
while (Serial.available()) {
inputStr = Serial.readStringUntil('\n');
Serial.println(inputStr);
return true;
}
return false;
}
His code works well, I'm able to read and write to my flash memory.
However, When I copy all his lines and put them in the right spot in my code, my COM port prints nothing on the screen and the my main code doesn't run anymore. Every 5th time that I re-program it, it just shows the output string from Marzogh's code but not my main code and it freezes. And when I PowerOFF/ON the board, the COM port will have nothing to show :o . I want it not only to keep the results when I restart the board, but to also not interfere with my main code.
Here's my main code:
#include <arduino.h>
#include "HopeDuino_LoRa.h"
#include <avr/sleep.h>
#include "ArduinoUniqueID.h"
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <EEPROM.h>
#include <SPIMemory.h>
#define RX_MODE 1
#define TX_MODE (!RX_MODE)
#define LED_PIN 7
#define LED_PORT() pinMode(LED_PIN,OUTPUT)
#define LED_HIGH() digitalWrite(LED_PIN,HIGH)
#define LED_LOW() digitalWrite(LED_PIN,LOW)
#define LED_PIN1 12
#define LED_PORT1() pinMode(LED_PIN1,OUTPUT)
#define LED_HIGH1() digitalWrite(LED_PIN1,HIGH)
#define LED_LOW1() digitalWrite(LED_PIN1,LOW)
#define LED_PIN2 6
#define LED_PORT2() pinMode(LED_PIN2,OUTPUT)
#define LED_HIGH2() digitalWrite(LED_PIN2,HIGH)
#define LED_LOW2() digitalWrite(LED_PIN2,LOW)
byte str0[]={"stud1"};
byte str1[]={"stud2"};
byte str2[]={"stud3"};
byte str3[]={"stud4"};
byte str4[]={"stud5"};
byte str5[]="acknl";
byte health[]="syschek";
byte getstr[str];
byte mode = RX_MODE;
const byte app_syncword[] = { 0x2D, 0xD4, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0 } ;
int count=0; // to count slave EEPROM Slave ID
int counter=0;
/*
** Variables for Flash-Memory
*/
uint32_t strAddr;
SPIFlash flash(17);
/************flash memory parameter********/
String outputString;
void setup(void)
{
byte tmp;
byte val;
LED_PORT();
LED_PORT1();
LED_PORT2();
Modulation = LORA; ///Was FSK
COB = RFM98; // Was RFM95
Frequency = 434000; // was 866000
OutputPower = 10; //17dBm OutputPower
PreambleLength = 8; //8Byte preamble
FixedPktLength = true; //explicit header mode for LoRa
PayloadLength = 21;
CrcDisable = true ;
//for LORA parameter
SFSel = SF9;
BWSel = BW125K;
CRSel = CR4_5;
vInitialize();
vGoRx();
Serial.begin(115200);
_delay_ms(100);
Serial.println("Device Initializing...");
/*
* Flash Memory Start-up
*/
int address;
Serial.begin(115200);
while (!Serial);
flash.begin(); //start the Flash memory
//strAddr = random(0, flash.getCapacity());
strAddr =0; // determine initial address
//Remember to increment address to prevent from overwriting
}
void loop(void)
{
static byte last_mode = RX_MODE;
byte tmp;
int x;// counter for the LED blinks
/**********RX mode**********/
//LED_HIGH();
//LED_HIGH1();
last_mode = mode;
vGoRx();
tmp = bGetMessage(getstr);
if (tmp != 0) // if(tmp!=4) The number of messages received were less than 4, flash the led on Master
{
/*
* Get First Unique ID
*/
byte val,readVal;
/*EEPROM TEST*/
if (count<1){
int c;
_delay_ms(10);
Serial.println("***Received a Message***");
for (c=0;c< 1;c++){
LED_HIGH();
_delay_ms(50);
LED_LOW();
_delay_ms(50);
}
//Serial.print(getstr[i],HEX);
/**************************************
* Read stored values in EEPROM
**************************************/
for (size_t i = 0; i < UniqueIDsize-1; i++){
EEPROM.write (i,getstr[i]);
readVal=EEPROM.read(i);
_delay_ms(10);
Serial.println(readVal,HEX);
_delay_ms(25);
}Serial.println("Slave 1 ID is saved for the first time");
count++;
}
if(EEPROM[0]==getstr[0] && EEPROM[1]==getstr[1] && EEPROM[2]==getstr[2] && EEPROM[3]==getstr[3]
&& EEPROM[4]==getstr[4] && EEPROM[5]==getstr[5] && EEPROM[6]==getstr[6] && EEPROM[7]==getstr[7]){
if (counter<1){
Serial.println("XXXXXXXXX Slave 1 XXXXXXXXXXX");
for (int x=0;x<1;x++){
LED_HIGH1();
_delay_ms(40);
LED_LOW1();
}counter++;
Serial.println("Slave 1 is live!!!");
for (strAddr;strAddr< 1;strAddr++){
String inputString = "Slave 1 is live!!!"; //what to write in this address
flash.writeStr(strAddr, inputString); //Read whatever is stored on flash memory on COM port
}
previousMillis1 = 0;// makes Slave1 timer zero
}}
if (getstr[0]== str0[0] && getstr[1]== str0[1]&& getstr[2]== str0[2]&& getstr[3]== str0[3]&& getstr[4]== str0[4]){
for (int x=0;x<1;x++){
LED_HIGH();
LED_HIGH1();
LED_HIGH2();
_delay_ms(40);// This time is necessary for syncronisation between the Stud message and Slave message prints
Serial.println("Stud 1 is loose");
LED_LOW();
LED_LOW1();
LED_LOW2();}
counter=0;
}
}
else{
}
}
delay (250);
String outputString = "";
if (flash.readStr(strAddr, outputString)) {
Serial.println(outputString);
Serial.println(strAddr);
}
while (!flash.eraseSector(strAddr));
}
//Reads a string from Serial
bool readSerialStr(String &inputStr) {
if (!Serial)
Serial.begin(115200);
while (Serial.available()) {
inputStr = Serial.readStringUntil('\n');
Serial.println(inputStr);
return true;
}
return false;
}
My code has many header files, So I don't think it might compile for you. but it's driving me crazy that it's not working.
I'd be grateful if anyone could help me with a hint. Thanks