Page 2 of 2

Posted: Wed Jun 05, 2019 4:29 am
by lightningrod
This probably belongs on a different thread at this point - I think we're looking at 2 bugs here: the first bug is that a lua script can cause corruption.
The second bug is that the factory reset doesn't clear the script partition.
Looking at the firmware code below and doing a little googling, it appears that flashing will fail if the flags are not cleared or if the memory is not erased prior to the "program" call. The ClearFlag op does not return a value but a check to see if the flags are actually clear might be warranted, as everything following will fail. Also the EraseSector call does return an error value which is not being checked. Knowing if this is where things fail might be useful.
Third: the ProgramByte operation can return 3 different failure values. It might also be useful to know what the cause of this call failing is.
Finally: I saw some other flash code that include this for the erase code:

Code: Select all

 while (FLASH_EraseSector(i, VoltageRange_3) != FLASH_COMPLETE)

But I think having a limit on the number of tries might be wise.

I'm not brave enough to modify this code and test it on my rcp3. Perhaps brent might be so good as to supply me with a version that adds more error checking and logging (which as been tested to work on his rcp) When I get the error next I can pass on the results from my logs.

Code: Select all

enum memory_flash_result_t memory_device_flash_region(const void *address, const void *data,
        unsigned int length)

    enum memory_flash_result_t rc = MEMORY_FLASH_SUCCESS;
    /* Erase the entire page before you can write.  This filters
     * the incoming addresses to available flash pages for the STM32F4 */
    uint32_t flashSector = selectFlashSector(address);
    if (flashSector) {
                        FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR |
                        FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
        FLASH_EraseSector(flashSector, VoltageRange_3);

        uint32_t addrTarget = (uint32_t) address;
        uint8_t *dataTarget = (uint8_t *) data;

        for &#40;unsigned int i = 0; i < length; i++&#41; &#123;
            if &#40;FLASH_ProgramByte&#40;addrTarget + i, *&#40;dataTarget + i&#41;&#41; != FLASH_COMPLETE&#41; &#123;
                rc = MEMORY_FLASH_WRITE_ERROR;
    &#125; else &#123;
    return rc;