Skip to content

Commit

Permalink
Update SerialReadWrite example
Browse files Browse the repository at this point in the history
#210 related
  • Loading branch information
MCUdude committed Mar 30, 2021
1 parent eea3ec8 commit 80f452f
Showing 1 changed file with 60 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,12 @@
// and write from the flash memory
#include "optiboot.h"


// Define the number of pages you want to write to here (limited by flash size)
#define NUMBER_OF_PAGES 8

// Define your termination and blank character here
const char terminationChar = '@';

// This is the character that gets printed if the memory block doesn't contain any data
const char blankChar = '.';


uint8_t charBuffer;
uint8_t menuOption;
uint16_t pageNumber;
char returnToMenu;

// The temporary data (data that's read or is about to get written) is stored here
uint8_t ramBuffer[SPM_PAGESIZE];

Expand All @@ -57,15 +47,14 @@ const uint8_t flashSpace[SPM_PAGESIZE * NUMBER_OF_PAGES] __attribute__ (( aligne
};



void setup()
{
// Initialize serial
Serial.begin(9600);
}


void loop()
void loop()
{
// Print main menu
Serial.println();
Expand All @@ -87,101 +76,108 @@ void loop()
Serial.println(F("| 1. Show current flash content |"));
Serial.println(F("| 2. Write to flash memory |"));
Serial.println(F("|------------------------------------------------|"));
Serial.println();


// Static variables
static uint8_t charBuffer;
static char menuOption;
static uint16_t pageNumber;
static char returnToMenu;

// Get menu option from the serial monitor
do
{
while(!Serial.available());
menuOption = Serial.parseInt();
if(menuOption < 1 || menuOption > 2)
menuOption = Serial.read();
if(menuOption != '1' && menuOption != '2')
Serial.print(F("\nPlease enter a valid option! "));
}
while(menuOption < 1 || menuOption > 2);
Serial.print(F("Option "));
while(menuOption != '1' && menuOption != '2');

Serial.print(F("\nOption "));
Serial.print(menuOption);
Serial.println(F(" selected."));



// Read flash option selected
if(menuOption == 1)
if(menuOption == '1')
{
Serial.print(F("What page number do you want to read? Page: "));
Serial.print(F("Which page number do you want to read? Page 0 to "));
Serial.print(NUMBER_OF_PAGES - 1);
Serial.print(F(", Page "));
Serial.print(NUMBER_OF_PAGES);
Serial.print(F(" to show all pages: "));

//Get page number from the serial monitor
do
{
while(!Serial.available());
pageNumber = Serial.parseInt();
if(pageNumber < 1 || pageNumber > NUMBER_OF_PAGES)
pageNumber = Serial.read() - 0x30;
if(pageNumber > NUMBER_OF_PAGES)
{
Serial.print(F("\nPlease enter a valid page between 1 and "));
Serial.print(NUMBER_OF_PAGES);
Serial.print(F("\nPlease enter a valid page between 0 and "));
Serial.print(NUMBER_OF_PAGES - 1);
Serial.println(F(". The number of pages can be extended by changing NUMBER_OF_PAGES constant"));
}
}
while(pageNumber > NUMBER_OF_PAGES);
if(pageNumber > 0)

if(pageNumber <= NUMBER_OF_PAGES)
Serial.println(pageNumber);

// READ SELECTED PAGE AND STORE THE CONTENT IN THE ramBuffer ARRAY
// flash_buffer is where the data is stored (contains the memory addresses)
// ramBuffer is where the data gets stored after reading from flash
// pageNumber is the page the data is read from
// blankChar is the character that gets printed/stored if there are unused space (default '.')
// use optiboot_readPage(flashSpace, ramBuffer, pageNumber) if you don't want blank chars

if(pageNumber == 0) // Read all pages

uint8_t pageFirst = 0;
uint8_t pageLast = NUMBER_OF_PAGES;

if(pageNumber != NUMBER_OF_PAGES)
{
Serial.println(F("\nAll flash content:"));
for(uint16_t page = 1; page < NUMBER_OF_PAGES+1; page++)
{
Serial.print(F("Page "));
Serial.print(page);
Serial.print(F(": "));
optiboot_readPage(flashSpace, ramBuffer, page, blankChar);
Serial.println((char*)ramBuffer);
}
pageFirst = pageNumber;
pageLast = pageNumber;
}
else // Read selected page

for(uint8_t page = pageFirst; page < pageLast; page++)
{
optiboot_readPage(flashSpace, ramBuffer, pageNumber, blankChar);

// Print page content
Serial.print(F("\nContent of page "));
Serial.print(pageNumber);
Serial.println(F(":"));
Serial.println((char*)ramBuffer);
optiboot_readPage(flashSpace, ramBuffer, page);
Serial.print(F("Page "));
Serial.print(page);
Serial.print(F(": "));
for(uint16_t i = 0; i < sizeof(ramBuffer); i++)
{
if(ramBuffer[i] == 0x00 || ramBuffer[i] == 0xff)
Serial.write('.');
else
Serial.write(ramBuffer[i]);
}
Serial.println("");
}

} // End of flash read option



// Write flash option selected
else if(menuOption == 2)
else if(menuOption == '2')
{
// Clear pageNumber
pageNumber = 0;
pageNumber = 0xff;

//Get page number from the serial monitor
Serial.print(F("\nWhat page do you want to write to? Page: "));
Serial.print(F("\nWhich page do you want to write to? Page: "));
do
{
while(!Serial.available());
pageNumber = Serial.parseInt();
if(pageNumber < 1 || pageNumber > NUMBER_OF_PAGES)
pageNumber = Serial.read() - 0x30;
if(pageNumber > NUMBER_OF_PAGES - 1)
{
Serial.print(F("\nPlease enter a valid page between 1 and "));
Serial.print(NUMBER_OF_PAGES);
Serial.print(F("\nPlease enter a valid page between 0 and "));
Serial.print(NUMBER_OF_PAGES - 1);
Serial.println(F(". The number of pages can be extended by changing NUMBER_OF_PAGES constant"));
}
}
while(pageNumber < 1 || pageNumber > NUMBER_OF_PAGES);
while(pageNumber >= NUMBER_OF_PAGES);
Serial.println(pageNumber);

// Print prompt to enter some new characters to write to flash
Expand All @@ -191,21 +187,21 @@ void loop()
Serial.print(F("End the line by sending the '"));
Serial.write(terminationChar);
Serial.println(F("' character:"));

// Get all characters from the serial monitor and store it to the ramBuffer
memset(ramBuffer, 0, sizeof(ramBuffer));
uint16_t counter = 0;
while (counter < SPM_PAGESIZE && charBuffer != terminationChar)
while (counter < SPM_PAGESIZE && charBuffer != terminationChar)
{
if(Serial.available() > 0)
if(Serial.available() > 0)
{
charBuffer = Serial.read(); // read character from serial
if(charBuffer != terminationChar)
{
Serial.write(charBuffer); // echo character back
ramBuffer[counter] = charBuffer;
counter++;
}
}
}
}
charBuffer = 0;
Expand Down Expand Up @@ -234,4 +230,4 @@ void loop()
while(returnToMenu != 'm');
returnToMenu = 0;

} // End of loop
} // End of loop

0 comments on commit 80f452f

Please sign in to comment.