This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How can I save some data to flash before the nrf51822 chip reset?

I use nrf51822qfaa,s110 v8.00,keil MDK IDE. If the program is wrong,The chip will reset, I want to save some data to the FLASH area before the chip reset. What I need to do? Use pstorage or other? Who can offer an example.

  • Hi, we don't have an example for this, but I'd suggest to try using the NVMC directly in your assert handler after disabling the interrupts. The flash write example shows how you can use the NVMC module.

  • Hi,Vidar Berg now I add custom handle at below,is OK?

    /*lint -save -e14 */
    __WEAK void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name)
    
    {
        // On assert, the system can only recover with a reset.
    
    #ifndef DEBUG
          NVIC_SystemReset();
    #else
    
    #ifdef BSP_DEFINES_ONLY 
          LEDS_ON(LEDS_MASK);
     #else
        UNUSED_VARIABLE(bsp_indication_set(BSP_INDICATE_FATAL_ERROR));
       
       // This call can be used for debug purposes during application development.
       // @note CAUTION: Activating this code will write the stack to flash on an error.
       //                This function should NOT be used in a final product.
       //                It is intended STRICTLY for development/debugging purposes.
       //                The flash write will happen EVEN if the radio is active, thus interrupting
       //                any communication.
       //                Use with care. Uncomment the line below to use.
       //ble_debug_assert_handler(error_code, line_num, p_file_name);
     #endif // BSP_DEFINES_ONLY
    
    // The following variable helps Keil keep the call stack visible, in addition, it can be set to
    // 0 in the debugger to continue executing code after the error check.
    volatile bool loop = true;
    UNUSED_VARIABLE(loop);
    
    m_error_code = error_code;
    m_line_num = line_num;
    m_p_file_name = p_file_name;
    
    UNUSED_VARIABLE(m_error_code);
    UNUSED_VARIABLE(m_line_num);
    UNUSED_VARIABLE(m_p_file_name);
    __disable_irq();
    
    Custom_Error_Handle();//use NVMC save data to flash.
    
    while(loop);
    #endif // DEBUG
    }
    
  • Yes, this is what I meant. You may have to disable the softdevice as well to access the NVMC peripheral (before interrupt disable). That can potentially be a problem depending on what assert you got.

Related