Switching between two simple code with code

i want to switch between two code... the code is simple led blinking i am trying for the testing purpose...in my code 1 led1 is blinking 500 ms of delay and in my code 2 led2 is blinking 1 sec of delay...i want to archive if i press button on my board then it can able to switch between this two code...where i have put each other flash starting address in code so i can able to start from there if button is pressed...but when i try to flash my code from nrf connect for desktop -> programmer and add the code 1 there there is no problem but when i try to flash code 2 hex file...then it is showing that  some of the HEX file have overlapping data..

App2.hexApp1.hex

Parents
  • Hi Mayank,

    Please make sure that both the codes are built with a separate flash base address. Also, you need a code in the main application to jump to the required code on button press.

    -Priyanka

  • hello priyanka,

    i have not mention in my question. really sorry about that but i am doing with different flash address in my code.. where i am also updating the vector table.. and after doing some experiments now i am able to store in flash but the issue is that if its in application1 when i press the button then it is going into the hardfault in application2. so can you give me solution for that..

    #include "nrf_gpio.h"
    #include "nrf_delay.h"
    #include "nrf.h"
    
    #define LED_PIN 16       // Adjust to your board's LED pin
    #define BUTTON_PIN 11    // Adjust to your board's button pin
    #define APP1_START_ADDR 0x0  // Address for App1
    
    typedef void (*app_entry_t)(void);
    
    void app2_main() {
        nrf_gpio_cfg_output(LED_PIN);
        nrf_gpio_cfg_input(BUTTON_PIN, NRF_GPIO_PIN_PULLUP);
    
        while (1) {
            nrf_gpio_pin_toggle(LED_PIN);  // Toggle LED
            nrf_delay_ms(100);  // Wait for 100 ms
    
              if (nrf_gpio_pin_read(BUTTON_PIN) == 0) {  // Button press detected
              nrf_delay_ms(50);  // Debounce delay
    
              if (nrf_gpio_pin_read(BUTTON_PIN) == 0) {
    
          uint32_t app1_sp = *(volatile uint32_t *)APP1_START_ADDR;  // App1 stack pointer
          uint32_t app1_reset_vector = *(volatile uint32_t *)(APP1_START_ADDR + 4);  // App1's reset vector
    
          // Validate stack pointer and reset vector
          if (app1_sp >= 0x20000000 && app1_sp <= 0x20080000 && app1_reset_vector >= APP1_START_ADDR && app1_reset_vector < APP1_START_ADDR + 0x40000)
           {
           __disable_irq();  // Disable interrupts
           __set_MSP(app1_sp);  // Set the stack pointer to App1's SP
           SCB->VTOR = APP1_START_ADDR;  // Update VTOR to point to App1's vector table
           __enable_irq();  // Enable interrupts if necessary
    
              void (*app1_entry)() = (void (*)())app1_reset_vector;  // Cast to function pointer
              app1_entry();  // Jump to App1
    
              //uint32_t app1_sp = *(volatile uint32_t *)APP1_START_ADDR;  // App2 stack pointer
    
              //  __set_MSP(app1_sp);  // Set the stack pointer to App2's SP
    
              //  SCB->VTOR = APP1_START_ADDR;  // Update VTOR to point to the new app
    
              //  uint32_t app1_reset_vector = *(volatile uint32_t *)(APP1_START_ADDR + 4);  // App2's reset vector
              //  void (*app1_entry)() = (void (*)())app1_reset_vector;  // Cast to function pointer
              //          app1_entry();  // Jump to App2
    
            }
          }
        }
      }
    }
    
    int main() {
        app2_main();  // Start Code 2 (100 ms blink)
        return 0;
    }
    
    #include "nrf_gpio.h"
    #include "nrf_delay.h"
    #include "nrf.h"
    
    #define LED_PIN 16       // Adjust to your board's LED pin
    #define BUTTON_PIN 11    // Adjust to your board's button pin
    #define APP1_START_ADDR 0x0  // Address for App1
    
    typedef void (*app_entry_t)(void);
    
    void app2_main() {
        nrf_gpio_cfg_output(LED_PIN);
        nrf_gpio_cfg_input(BUTTON_PIN, NRF_GPIO_PIN_PULLUP);
    
        while (1) {
            nrf_gpio_pin_toggle(LED_PIN);  // Toggle LED
            nrf_delay_ms(100);  // Wait for 100 ms
    
              if (nrf_gpio_pin_read(BUTTON_PIN) == 0) {  // Button press detected
              nrf_delay_ms(50);  // Debounce delay
    
              if (nrf_gpio_pin_read(BUTTON_PIN) == 0) {
    
          uint32_t app1_sp = *(volatile uint32_t *)APP1_START_ADDR;  // App1 stack pointer
          uint32_t app1_reset_vector = *(volatile uint32_t *)(APP1_START_ADDR + 4);  // App1's reset vector
    
          // Validate stack pointer and reset vector
          if (app1_sp >= 0x20000000 && app1_sp <= 0x20080000 && app1_reset_vector >= APP1_START_ADDR && app1_reset_vector < APP1_START_ADDR + 0x40000)
           {
           __disable_irq();  // Disable interrupts
           __set_MSP(app1_sp);  // Set the stack pointer to App1's SP
           SCB->VTOR = APP1_START_ADDR;  // Update VTOR to point to App1's vector table
           __enable_irq();  // Enable interrupts if necessary
    
              void (*app1_entry)() = (void (*)())app1_reset_vector;  // Cast to function pointer
              app1_entry();  // Jump to App1
    
              //uint32_t app1_sp = *(volatile uint32_t *)APP1_START_ADDR;  // App2 stack pointer
    
              //  __set_MSP(app1_sp);  // Set the stack pointer to App2's SP
    
              //  SCB->VTOR = APP1_START_ADDR;  // Update VTOR to point to the new app
    
              //  uint32_t app1_reset_vector = *(volatile uint32_t *)(APP1_START_ADDR + 4);  // App2's reset vector
              //  void (*app1_entry)() = (void (*)())app1_reset_vector;  // Cast to function pointer
              //          app1_entry();  // Jump to App2
    
            }
          }
        }
      }
    }
    
    int main() {
        app2_main();  // Start Code 2 (100 ms blink)
        return 0;
    }
    
      

Reply
  • hello priyanka,

    i have not mention in my question. really sorry about that but i am doing with different flash address in my code.. where i am also updating the vector table.. and after doing some experiments now i am able to store in flash but the issue is that if its in application1 when i press the button then it is going into the hardfault in application2. so can you give me solution for that..

    #include "nrf_gpio.h"
    #include "nrf_delay.h"
    #include "nrf.h"
    
    #define LED_PIN 16       // Adjust to your board's LED pin
    #define BUTTON_PIN 11    // Adjust to your board's button pin
    #define APP1_START_ADDR 0x0  // Address for App1
    
    typedef void (*app_entry_t)(void);
    
    void app2_main() {
        nrf_gpio_cfg_output(LED_PIN);
        nrf_gpio_cfg_input(BUTTON_PIN, NRF_GPIO_PIN_PULLUP);
    
        while (1) {
            nrf_gpio_pin_toggle(LED_PIN);  // Toggle LED
            nrf_delay_ms(100);  // Wait for 100 ms
    
              if (nrf_gpio_pin_read(BUTTON_PIN) == 0) {  // Button press detected
              nrf_delay_ms(50);  // Debounce delay
    
              if (nrf_gpio_pin_read(BUTTON_PIN) == 0) {
    
          uint32_t app1_sp = *(volatile uint32_t *)APP1_START_ADDR;  // App1 stack pointer
          uint32_t app1_reset_vector = *(volatile uint32_t *)(APP1_START_ADDR + 4);  // App1's reset vector
    
          // Validate stack pointer and reset vector
          if (app1_sp >= 0x20000000 && app1_sp <= 0x20080000 && app1_reset_vector >= APP1_START_ADDR && app1_reset_vector < APP1_START_ADDR + 0x40000)
           {
           __disable_irq();  // Disable interrupts
           __set_MSP(app1_sp);  // Set the stack pointer to App1's SP
           SCB->VTOR = APP1_START_ADDR;  // Update VTOR to point to App1's vector table
           __enable_irq();  // Enable interrupts if necessary
    
              void (*app1_entry)() = (void (*)())app1_reset_vector;  // Cast to function pointer
              app1_entry();  // Jump to App1
    
              //uint32_t app1_sp = *(volatile uint32_t *)APP1_START_ADDR;  // App2 stack pointer
    
              //  __set_MSP(app1_sp);  // Set the stack pointer to App2's SP
    
              //  SCB->VTOR = APP1_START_ADDR;  // Update VTOR to point to the new app
    
              //  uint32_t app1_reset_vector = *(volatile uint32_t *)(APP1_START_ADDR + 4);  // App2's reset vector
              //  void (*app1_entry)() = (void (*)())app1_reset_vector;  // Cast to function pointer
              //          app1_entry();  // Jump to App2
    
            }
          }
        }
      }
    }
    
    int main() {
        app2_main();  // Start Code 2 (100 ms blink)
        return 0;
    }
    
    #include "nrf_gpio.h"
    #include "nrf_delay.h"
    #include "nrf.h"
    
    #define LED_PIN 16       // Adjust to your board's LED pin
    #define BUTTON_PIN 11    // Adjust to your board's button pin
    #define APP1_START_ADDR 0x0  // Address for App1
    
    typedef void (*app_entry_t)(void);
    
    void app2_main() {
        nrf_gpio_cfg_output(LED_PIN);
        nrf_gpio_cfg_input(BUTTON_PIN, NRF_GPIO_PIN_PULLUP);
    
        while (1) {
            nrf_gpio_pin_toggle(LED_PIN);  // Toggle LED
            nrf_delay_ms(100);  // Wait for 100 ms
    
              if (nrf_gpio_pin_read(BUTTON_PIN) == 0) {  // Button press detected
              nrf_delay_ms(50);  // Debounce delay
    
              if (nrf_gpio_pin_read(BUTTON_PIN) == 0) {
    
          uint32_t app1_sp = *(volatile uint32_t *)APP1_START_ADDR;  // App1 stack pointer
          uint32_t app1_reset_vector = *(volatile uint32_t *)(APP1_START_ADDR + 4);  // App1's reset vector
    
          // Validate stack pointer and reset vector
          if (app1_sp >= 0x20000000 && app1_sp <= 0x20080000 && app1_reset_vector >= APP1_START_ADDR && app1_reset_vector < APP1_START_ADDR + 0x40000)
           {
           __disable_irq();  // Disable interrupts
           __set_MSP(app1_sp);  // Set the stack pointer to App1's SP
           SCB->VTOR = APP1_START_ADDR;  // Update VTOR to point to App1's vector table
           __enable_irq();  // Enable interrupts if necessary
    
              void (*app1_entry)() = (void (*)())app1_reset_vector;  // Cast to function pointer
              app1_entry();  // Jump to App1
    
              //uint32_t app1_sp = *(volatile uint32_t *)APP1_START_ADDR;  // App2 stack pointer
    
              //  __set_MSP(app1_sp);  // Set the stack pointer to App2's SP
    
              //  SCB->VTOR = APP1_START_ADDR;  // Update VTOR to point to the new app
    
              //  uint32_t app1_reset_vector = *(volatile uint32_t *)(APP1_START_ADDR + 4);  // App2's reset vector
              //  void (*app1_entry)() = (void (*)())app1_reset_vector;  // Cast to function pointer
              //          app1_entry();  // Jump to App2
    
            }
          }
        }
      }
    }
    
    int main() {
        app2_main();  // Start Code 2 (100 ms blink)
        return 0;
    }
    
      

Children
Related