#include "ti_msp_dl_config.h"

bool swap_enabled;
bool exec_upper;

static void configure_bank_swap_early(bool use_upper_bank)
{
    /* Enable bank swap policy (write-once before INITDONE) */
    DL_SYSCTL_enableFlashBankSwap();

    /* Select execution bank after reset */
    if (use_upper_bank) {
        DL_SYSCTL_executeFromUpperFlashBank();
    } else {
        DL_SYSCTL_executeFromLowerFlashBank();
    }
}

int main(void)
{

    if (!DL_SYSCTL_isINITDONEIssued())
    {
        configure_bank_swap_early(true);
        SYSCFG_DL_init();

        DL_SYSCTL_issueINITDONE();

        while (1);
    }

    SYSCFG_DL_init();

    swap_enabled = DL_SYSCTL_isFlashBankSwapEnabled();
    exec_upper   = DL_SYSCTL_isExecuteFromUpperFlashBank();

    while (1)
    {
        if (swap_enabled && exec_upper)
        {
            /* GREEN LED → running from upper bank */
            DL_GPIO_togglePins(GPIO_LEDS_USER_LED_3_PORT,
                               GPIO_LEDS_USER_LED_3_PIN);
        }
        else
        {
            /* RED LED → swap not active or running from lower */
            DL_GPIO_togglePins(GPIO_LEDS_USER_LED_2_PORT,
                               GPIO_LEDS_USER_LED_2_PIN);
        }

        delay_cycles(100000000);
    }
}
