m-mcgowan / spark-flashee-eeprom

Eeprom emulation using external flash on Particle devices. Includes unit tests cross compiled to regular gcc, and on-device integration tests.
GNU Affero General Public License v3.0
34 stars 8 forks source link

should be possible to test external flash sectors #15

Open m-mcgowan opened 9 years ago

m-mcgowan commented 9 years ago

For now, here's some code that will test each sector.

#pragma SPARK_NO_PREPROCESSOR
#include "application.h"
// This #include statement was automatically added by the Spark IDE.
#include "flashee-eeprom/flashee-eeprom.h"

using namespace Flashee;

bool testPage(FlashDevice& device, page_count_t page)
{
    uint8_t buf[128];
    bool success = true;
    page_size_t pageSize = device.pageSize();
    device.erasePage(pageSize*page);
    for (page_size_t i=0; i<pageSize; i+=sizeof(buf)) {
       device.read(buf, page*pageSize+i, sizeof(buf));
       for (unsigned j=0; j<sizeof(buf); j++) {
           if (buf[j]!=0xFF)
                success = false;
       }
    }
    return success;
}

void setup()
{
    Serial.begin(9600);

    FlashDevice& device = Devices::userFlash();
    page_size_t pageSize = device.pageSize();
    page_count_t pageCount = device.pageCount();

    int errors = 0;
    for (page_count_t i=0; i<pageCount; i++) {
        if (!testPage(device, i)) {
            errors++;
            Serial.print("!!Errors on page ");
            Serial.println(i);
        }
    }

    if (errors) {
        Serial.print("There were ERRORS! count=");
        Serial.println(errors);
    }
    else {
        Serial.println("All sectors checked out fine!");
    }

}