From 80f452f36f671ddb74571dc73c758d9d8783081f Mon Sep 17 00:00:00 2001 From: MCUdude Date: Tue, 30 Mar 2021 18:51:09 +0200 Subject: [PATCH] Update SerialReadWrite example #210 related --- .../SerialReadWrite/SerialReadWrite.ino | 124 +++++++++--------- 1 file changed, 60 insertions(+), 64 deletions(-) diff --git a/avr/libraries/Optiboot_flasher/examples/SerialReadWrite/SerialReadWrite.ino b/avr/libraries/Optiboot_flasher/examples/SerialReadWrite/SerialReadWrite.ino index 1904d148b..9efe89417 100644 --- a/avr/libraries/Optiboot_flasher/examples/SerialReadWrite/SerialReadWrite.ino +++ b/avr/libraries/Optiboot_flasher/examples/SerialReadWrite/SerialReadWrite.ino @@ -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]; @@ -57,7 +47,6 @@ const uint8_t flashSpace[SPM_PAGESIZE * NUMBER_OF_PAGES] __attribute__ (( aligne }; - void setup() { // Initialize serial @@ -65,7 +54,7 @@ void setup() } -void loop() +void loop() { // Print main menu Serial.println(); @@ -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 @@ -191,13 +187,13 @@ 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) @@ -205,7 +201,7 @@ void loop() Serial.write(charBuffer); // echo character back ramBuffer[counter] = charBuffer; counter++; - } + } } } charBuffer = 0; @@ -234,4 +230,4 @@ void loop() while(returnToMenu != 'm'); returnToMenu = 0; -} // End of loop \ No newline at end of file +} // End of loop