barafael / g4-dual-bank-example

MIT License
22 stars 3 forks source link

BFB2 flag, vs REMAP flag #2

Open EitanTal opened 1 year ago

EitanTal commented 1 year ago

The decision on which bank to load the new firmware to, is based off the REMAP flag, which is correct. The remap flag tells you which flash bank you're running from.

However, the decision to set BFB2, is based on the BFB2 flag. Unlike Remap, BFB2 flag does not necessarily mean you're executing from the bank you think you are. What this flag actually means is whether or not the rom bootloader will attempt to boot from bank2. If bank2 contains no valid firmware, then the bootloader will boot from bank1. In this situation, Remap will be zero, but BFB2 will be one.

https://github.com/barafael/g4-dual-bank-example/blob/a4f098da267920da0e715af3c7edce189d82f62b/Core/Src/main.c#L107

barafael commented 1 year ago

Again, thanks for sharing your insight. Would you mind explaining further what needs to be done here or if you want PR'ing the needed changes?

EitanTal commented 1 year ago

This is my suggestion:

    if (GetActiveBank() == FLASH_BANK_2) {
        // Switch over boot from Bank2 to Bank1, by disabling BFB2 flag
        OBInit.USERConfig = OB_BFB2_DISABLE;
    } else {
        // Switch over boot from Bank1 to Bank2, by enabling BFB2 flag
        OBInit.USERConfig = OB_BFB2_ENABLE;
    }
EitanTal commented 1 year ago

I'm changing the condition inside the if(). I don't want to blindly toggle BFB2.

From (((OBInit.USERConfig) & (OB_BFB2_ENABLE)) == OB_BFB2_ENABLE) to (GetActiveBank() == FLASH_BANK_2)