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

Flash Storage Write API has issues

Hello,

I am trying to use the flash API of fstorage in my code. I have called below function at the start of my code. I want to save some integer values every 1 min interval.

Flash reading is working fine, but when writing was tried my whole code gets problem. When write api called debug messaged also doesn’t get print. I have included my c code for flash below. Kindly suggest what should be done.

Thank You.

NRF_FSTORAGE_DEF(nrf_fstorage_t fstorage) =
{
    /* Set a handler for fstorage events. */
    .evt_handler = fstorage_evt_handler,

    /* These below are the boundaries of the flash space assigned to this instance of fstorage.
     * You must set these manually, even at runtime, before nrf_fstorage_init() is called.
     * The function nrf5_flash_end_addr_get() can be used to retrieve the last address on the
     * last page of flash available to write data. */
    .start_addr = FLASH_START_ADDR,         //    0x70ff0
    .end_addr   = FLASH_END_ADDR,          //     0x80000
};


void Flash_init(void)
{
 uint32_t data_1=5;
      ret_code_t rc;

      #ifdef  FLASH_12
      nrf_fstorage_api_t * p_fs_api;

     /* Initialize an fstorage instance using the nrf_fstorage_sd backend.
     * nrf_fstorage_sd uses the SoftDevice to write to flash. This implementation can safely be
     * used whenever there is a SoftDevice, regardless of its status (enabled/disabled). */
      p_fs_api = &nrf_fstorage_sd;



      rc = nrf_fstorage_init(&fstorage, p_fs_api, NULL);
      APP_ERROR_CHECK(rc);

      /* It is possible to set the start and end addresses of an fstorage instance at runtime.
     * They can be set multiple times, should it be needed. The helper function below can
     * be used to determine the last address on the last page of flash memory available to
     * store data. */
//     (void) nrf5_flash_end_addr_get();


//       print_flash_info(&fstorage);

      #ifdef  UART_Flash
      printf("Flash Init=%d\n",rc);
      #endif
//
    
            wait_for_flash_ready(&fstorage);


    #endif

}


void Flash_Operation(void)
{
 ret_code_t rc;
 uint32_t local_var=0, data=5;
 uint8_t  len=0;
  
  /***********************************Flash Read *****************************************/
    #ifdef  FLASH_12
      wait_for_flash_ready(&fstorage);
//    printf("Done.");

      rc = nrf_fstorage_read(&fstorage, FLASH_START_ADDR, &local_var, sizeof(local_var));
       
       APP_ERROR_CHECK(rc);
        wait_for_flash_ready(&fstorage);
                   
      #ifdef  UART_Flash
        printf("local_var=%x , RC=%x \n",local_var,rc); 
        printf("Flash read Done.\n");
      #endif


      #endif
     /***********************************Flash read end *****************************************/

      /***********************************Flash write *****************************************/
          
           rc = nrf_fstorage_erase(&fstorage, FLASH_START_ADDR , 1, NULL);
        
            APP_ERROR_CHECK(rc);
            wait_for_flash_ready(&fstorage);
            rc = nrf_fstorage_write(&fstorage, FLASH_START_ADDR , &data, sizeof(data), NULL);
    
            APP_ERROR_CHECK(rc);
            wait_for_flash_ready(&fstorage);
            
                #ifdef  UART_Flash
                printf("ERR code=%d,Flash write %x\n",rc,data);
                #endif
        /***********************************Flash write end *****************************************/

}

Parents
  • Hi,

    Is it so that the call to nrf_fstorage_write() on line 86 in the snippet returns but not the call to wait_for_flash_ready() on line 89? If so, a typical reason for such a behavior would be if this code is running with the same or higher interrupt priority than fstorage. If so the scheduled write would never happen, and there would be a deadlock.

  • Hi Einar,

                Can you please explain more on this I am not clear on what you said "Is it so that the call to nrf_fstorage_write() on line 86 in the snippet returns but not the call to wait_for_flash_ready() on line 89? "

    I have changed the priority of other peripherals but the issue is still there. As you said, fstorage priority higher or same but I couldn't able to find in the sdk_config.h file

  • Hi,

    GeekAD said:
    Can you please explain more on this I am not clear on what you said "Is it so that the call to nrf_fstorage_write() on line 86 in the snippet returns but not the call to wait_for_flash_ready() on line 89? "

    I was just writing down how I understood your description of the behaviour to try to be sure I got it right. To rephrase: Is it so that execution reaches wait_for_flash_ready() after the call to wait_for_flash_ready(), but it never returns? I assume so.

    GeekAD said:
    I have changed the priority of other peripherals but the issue is still there. As you said, fstorage priority higher or same but I couldn't able to find in the sdk_config.h file

    If my initial theory is relevant, then what matters is the priority where you are waiting for the flash operation to finish relative to the fstorage priority, which is essentially the SoftDevice interrupt priority, if you are using a SoftDevice. Can you confirm that you are using a SoftDevice?

    If yes,

    • How is your Flash_Operation() called? where is it called from? Is it from an event handler or similar running (interrupt), or from the main loop?

    If this is not the issue or you do not make progress perhaps you can show more or all of your project so that I get a proper context to understand what is happening?

  • Hi,

    I am calling this function at the beginning of the main. and yes I am using softdevice.

        // Hardware initialization 
        Hardware_INIT();
        #ifdef  FLASH_12
        // Flash initialization 
          Flash_init();
      
        // Flash Read & write function to check if it is actually working
          Flash_Operation();
        #endif
        
         

  • I see. I do not see anything obvious, then. Can you upload code that reproduce this on a DK so that I can get a full understanding and test on my side? Which SDK version are you using?

  • Hi,

    I have attached the main file code. I am using SDK 15.2 with SoftDevice 6.1.0.

    I have not updated the definition of SOFTDEVICE_PRESENT  in the linker. When added more issue is coming.

    #include <stdbool.h>
    #include <stdint.h>
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #include "stddef.h"
    //#include "config.h"
    #include "nrf_drv_twi.h"
    #include "nrf_gpio.h"
    #include "app_error.h"
    #include "nrf.h"
    #include "bsp.h"
    #include "app_util_platform.h"
    #include "app_timer.h"
    #include "nrf_drv_clock.h"
    #include "boards.h"
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    #include "nrf_cli.h"
    #include "nrf_cli_uart.h"
    #include "nrf_fstorage.h"
    #include "nrf_fstorage_sd.h"
    #include "nrf_sdh_ble.h"
    #include "nrf_sdh.h"
    
    
    #include "nrf_soc.h"
    /*****************************************************************************
    * Include section
    * User Prepared Include Files
    *****************************************************************************/
    #include "GPIOTE_driver.h"
    #include "Relay_driver.h"
    
    #include "relay.h"
    #include"Timer.h"
    #include "nrf_delay.h"
    
    #include "PIR.h"
    #include "LED.h"
    #include "config_relay.h"
    #include "main.h"
    
    #include "Definations.h"
    
    
    
    //----------------------------------------------------------------------------
    // Constants section
    //----------------------------------------------------------------------------
    #define FLASH_START_ADDR   0x60000
    #define FLASH_END_ADDR     0x61000
    #define BUTTON_DETECTION_DELAY  APP_TIMER_TICKS(50)
    #define APP_BLE_CONN_CFG_TAG    1
    
    //----------------------------------------------------------------------------
    // Boolean variables section
    //----------------------------------------------------------------------------
    
    bool gb_PIR_Operation_Start=0;
    
    //-----------------------------------------------------------------------------
    // Character variables section
    //----------------------------------------------------------------------------
    
    uint8_t gb_Power_ON_flag=0;
    //----------------------------------------------------------------------------
    // unsigned integer variables section
    //----------------------------------------------------------------------------
    
    
    uint16_t  gb_OPT_RAW_DATA, gb_OPT_CONFIGURATION_REG_DATA;
    //----------------------------------------------------------------------------
    // Signed integer variables section
    //----------------------------------------------------------------------------
    
    
    //----------------------------------------------------------------------------
    // unsigned long integer variables section
    //----------------------------------------------------------------------------
    
    
    //----------------------------------------------------------------------------
    // signed long integer variables section
    //----------------------------------------------------------------------------
    
    
    //----------------------------------------------------------------------------
    // unsigned long long integer variables section
    //----------------------------------------------------------------------------
    
    
    //----------------------------------------------------------------------------
    // signed long long integer variables section
    //----------------------------------------------------------------------------
    
    
    //----------------------------------------------------------------------------
    // Float variables section
    //----------------------------------------------------------------------------
    
    
    //----------------------------------------------------------------------------
    // Double variables section
    //----------------------------------------------------------------------------
    
    
    //----------------------------------------------------------------------------
    // Structure or Union variables section
    //----------------------------------------------------------------------------
    
    
    
    
    //----------------------------------------------------------------------------
    // Local Functions Prototypes
    //----------------------------------------------------------------------------
    
    void Flash_init(void);
    //void Flash_read(void);
    void Flash_Operation(void);
    
    #ifdef  FLASH_12
    void wait_for_flash_ready(nrf_fstorage_t const * p_fstorage);
    
    
    // flash 
    static void fstorage_evt_handler(nrf_fstorage_evt_t * p_evt);
    
    
    
    NRF_FSTORAGE_DEF(nrf_fstorage_t fstorage) =
    {
        /* Set a handler for fstorage events. */
        .evt_handler = fstorage_evt_handler,
    
        /* These below are the boundaries of the flash space assigned to this instance of fstorage.
         * You must set these manually, even at runtime, before nrf_fstorage_init() is called.
         * The function nrf5_flash_end_addr_get() can be used to retrieve the last address on the
         * last page of flash available to write data. */
        .start_addr = FLASH_START_ADDR,         //    0x70ff0
        .end_addr   = FLASH_END_ADDR,          //     0x80000
    };
    
    
    /* Dummy data to write to flash. */
    static uint32_t m_data          = 0xBADC0FFE;
    static uint32_t gb_m_data=0;
    static char     m_hello_world[] = "hello world";
    static char     gb_restored_string[50];
    
    
    /**@brief   Helper function to obtain the last address on the last page of the on-chip flash that
     *          can be used to write user data.
     */
    static uint32_t nrf5_flash_end_addr_get()
    {
        uint32_t const bootloader_addr = NRF_UICR->NRFFW[0];
        uint32_t const page_sz         = NRF_FICR->CODEPAGESIZE;
        uint32_t const code_sz         = NRF_FICR->CODESIZE;
    
        return (bootloader_addr != 0xFFFFFFFF ?
                bootloader_addr : (code_sz * page_sz));
    }
    
    
    
    /**@brief   Initialize the timer. */
    static void timer_init_1(void)
    {
        ret_code_t err_code = app_timer_init();
        APP_ERROR_CHECK(err_code);
    }
    
    /**@brief   Sleep until an event is received. */
    static void power_manage(void)
    {
    //#ifdef SOFTDEVICE_PRESENT
        (void) sd_app_evt_wait();
    //#else
    //    __WFE();
    //#endif
    }
    
    
    
    static void ble_stack_init(void)
    {
        ret_code_t rc;
        uint32_t   ram_start;
    
        /* Enable the SoftDevice. */
        rc = nrf_sdh_enable_request();
        APP_ERROR_CHECK(rc);
    
        rc = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
        APP_ERROR_CHECK(rc);
    
        rc = nrf_sdh_ble_enable(&ram_start);
        APP_ERROR_CHECK(rc);
    }
    
    static void print_flash_info(nrf_fstorage_t * p_fstorage)
    {
        printf("========| flash info |========");
        printf("erase unit: \t%d bytes",      p_fstorage->p_flash_info->erase_unit);
        printf("program unit: \t%d bytes",    p_fstorage->p_flash_info->program_unit);
        printf("==============================");
    }
    
    static void fstorage_evt_handler(nrf_fstorage_evt_t * p_evt)
    {
        if (p_evt->result != NRF_SUCCESS)
        {
            NRF_LOG_INFO("--> Event received: ERROR while executing an fstorage operation.");
            return;
        }
    
        switch (p_evt->id)
        {
            case NRF_FSTORAGE_EVT_WRITE_RESULT:
            {
                NRF_LOG_INFO("--> Event received: wrote %d bytes at address 0x%x.",
                             p_evt->len, p_evt->addr);
            } break;
    
            case NRF_FSTORAGE_EVT_ERASE_RESULT:
            {
                NRF_LOG_INFO("--> Event received: erased %d page from address 0x%x.",
                             p_evt->len, p_evt->addr);
            } break;
    
            case  NRF_FSTORAGE_EVT_READ_RESULT :
                          printf("Flash read sucess\n");
                          break;
    
    
            default:
                break;
        }
    }
    
    void wait_for_flash_ready(nrf_fstorage_t const * p_fstorage)
    {
        /* While fstorage is busy, sleep and wait for an event. */
        while (nrf_fstorage_is_busy(p_fstorage))
        {
            power_manage();
        }
    }
    
    #endif
    
    //----------------------------------------------------------------------------
    // Global Functions
    //----------------------------------------------------------------------------
    
    void main(void)
    {
     ret_code_t rc=0;
     uint32_t data_1=5;
    
        // Hardware initialization 
        Hardware_INIT();
        #ifdef  FLASH_12
        // Flash initialization 
          Flash_init();
      
        // Flash Read function
          Flash_Operation();
        #endif
        
         
       
        printf("\r Initialization completed \r\n ");
        
    
      
    
        //Power ON Flag set
        gb_Power_ON_flag=1;
    
        // config all function 
        // Config_all();
    
        //nrf_delay_ms(2000);
        gb_Calibration_Time_flag_for_OPT=0;
    
    
    
          // relay made off 
          //x`(1);
    
       
    
          // load 30 sec to timer for PIR to configure 
          gb_PIR_config_time=35;
          
          // wait time 30 sec for PIR sensor to calibrate itself 
          while(gb_PIR_config_time!=0){   /* do nothing */}
          
          printf("delay\n");
          // nrf_drv_gpiote_out_set(const_LED);   // LED can be made ON to indicate PIR is active now
         
          // operation start flag is cleared
          gb_PIR_Operation_Start=0;
       
        
    /* Main loop */
    
    
        while (1)
        {
        // operation 
        }
    }
    
    void Flash_init(void)
    {
     uint32_t data_1=5;
          ret_code_t rc;
    
          #ifdef  FLASH_12
          nrf_fstorage_api_t * p_fs_api;
    
         /* Initialize an fstorage instance using the nrf_fstorage_sd backend.
         * nrf_fstorage_sd uses the SoftDevice to write to flash. This implementation can safely be
         * used whenever there is a SoftDevice, regardless of its status (enabled/disabled). */
          p_fs_api = &nrf_fstorage_sd;
    
    
    
          rc = nrf_fstorage_init(&fstorage, p_fs_api, NULL);
          APP_ERROR_CHECK(rc);
    
          /* It is possible to set the start and end addresses of an fstorage instance at runtime.
         * They can be set multiple times, should it be needed. The helper function below can
         * be used to determine the last address on the last page of flash memory available to
         * store data. */
    //     (void) nrf5_flash_end_addr_get();
    
    
    //       print_flash_info(&fstorage);
    
          #ifdef  UART_Flash
          printf("Flash Init=%d\n",rc);
          #endif
    //
        
    //            wait_for_flash_ready(&fstorage);
    
    
        #endif
    
    }
    
    void Flash_Operation(void)
    {
     ret_code_t rc;
     uint32_t local_var=0, data=5;
     uint8_t  len=0;
      
      /***********************************Flash Read *****************************************/
        #ifdef  FLASH_12
    //      wait_for_flash_ready(&fstorage);
    //    printf("Done.");
    
          rc = nrf_fstorage_read(&fstorage, FLASH_START_ADDR, &local_var, sizeof(local_var));
           
           APP_ERROR_CHECK(rc);
    //        wait_for_flash_ready(&fstorage);
                     
                     NRF_LOG_INFO("Enabling the SoftDevice.");
                      ble_stack_init();
                       
          #ifdef  UART_Flash
            printf("local_var=%x , RC=%x \n",local_var,rc); 
            printf("Flash read Done.\n");
          #endif
    
    
          #endif
         /***********************************Flash read end *****************************************/
    
          /***********************************Flash write *****************************************/
              
    //           rc = nrf_fstorage_erase(&fstorage, FLASH_START_ADDR , 1, NULL);
            
                APP_ERROR_CHECK(rc);
                wait_for_flash_ready(&fstorage);
                rc = nrf_fstorage_write(&fstorage, FLASH_START_ADDR , &data, sizeof(data), NULL);
        
                APP_ERROR_CHECK(rc);
                wait_for_flash_ready(&fstorage);
                
                    #ifdef  UART_Flash
                    printf("ERR code=%d,Flash write %x\n",rc,data);
                    #endif
            /***********************************Flash write end *****************************************/
    
    }
    
    
    
    /*****************************************************************************
    * End of file.
    *****************************************************************************/
    
    

Reply
  • Hi,

    I have attached the main file code. I am using SDK 15.2 with SoftDevice 6.1.0.

    I have not updated the definition of SOFTDEVICE_PRESENT  in the linker. When added more issue is coming.

    #include <stdbool.h>
    #include <stdint.h>
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #include "stddef.h"
    //#include "config.h"
    #include "nrf_drv_twi.h"
    #include "nrf_gpio.h"
    #include "app_error.h"
    #include "nrf.h"
    #include "bsp.h"
    #include "app_util_platform.h"
    #include "app_timer.h"
    #include "nrf_drv_clock.h"
    #include "boards.h"
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    #include "nrf_cli.h"
    #include "nrf_cli_uart.h"
    #include "nrf_fstorage.h"
    #include "nrf_fstorage_sd.h"
    #include "nrf_sdh_ble.h"
    #include "nrf_sdh.h"
    
    
    #include "nrf_soc.h"
    /*****************************************************************************
    * Include section
    * User Prepared Include Files
    *****************************************************************************/
    #include "GPIOTE_driver.h"
    #include "Relay_driver.h"
    
    #include "relay.h"
    #include"Timer.h"
    #include "nrf_delay.h"
    
    #include "PIR.h"
    #include "LED.h"
    #include "config_relay.h"
    #include "main.h"
    
    #include "Definations.h"
    
    
    
    //----------------------------------------------------------------------------
    // Constants section
    //----------------------------------------------------------------------------
    #define FLASH_START_ADDR   0x60000
    #define FLASH_END_ADDR     0x61000
    #define BUTTON_DETECTION_DELAY  APP_TIMER_TICKS(50)
    #define APP_BLE_CONN_CFG_TAG    1
    
    //----------------------------------------------------------------------------
    // Boolean variables section
    //----------------------------------------------------------------------------
    
    bool gb_PIR_Operation_Start=0;
    
    //-----------------------------------------------------------------------------
    // Character variables section
    //----------------------------------------------------------------------------
    
    uint8_t gb_Power_ON_flag=0;
    //----------------------------------------------------------------------------
    // unsigned integer variables section
    //----------------------------------------------------------------------------
    
    
    uint16_t  gb_OPT_RAW_DATA, gb_OPT_CONFIGURATION_REG_DATA;
    //----------------------------------------------------------------------------
    // Signed integer variables section
    //----------------------------------------------------------------------------
    
    
    //----------------------------------------------------------------------------
    // unsigned long integer variables section
    //----------------------------------------------------------------------------
    
    
    //----------------------------------------------------------------------------
    // signed long integer variables section
    //----------------------------------------------------------------------------
    
    
    //----------------------------------------------------------------------------
    // unsigned long long integer variables section
    //----------------------------------------------------------------------------
    
    
    //----------------------------------------------------------------------------
    // signed long long integer variables section
    //----------------------------------------------------------------------------
    
    
    //----------------------------------------------------------------------------
    // Float variables section
    //----------------------------------------------------------------------------
    
    
    //----------------------------------------------------------------------------
    // Double variables section
    //----------------------------------------------------------------------------
    
    
    //----------------------------------------------------------------------------
    // Structure or Union variables section
    //----------------------------------------------------------------------------
    
    
    
    
    //----------------------------------------------------------------------------
    // Local Functions Prototypes
    //----------------------------------------------------------------------------
    
    void Flash_init(void);
    //void Flash_read(void);
    void Flash_Operation(void);
    
    #ifdef  FLASH_12
    void wait_for_flash_ready(nrf_fstorage_t const * p_fstorage);
    
    
    // flash 
    static void fstorage_evt_handler(nrf_fstorage_evt_t * p_evt);
    
    
    
    NRF_FSTORAGE_DEF(nrf_fstorage_t fstorage) =
    {
        /* Set a handler for fstorage events. */
        .evt_handler = fstorage_evt_handler,
    
        /* These below are the boundaries of the flash space assigned to this instance of fstorage.
         * You must set these manually, even at runtime, before nrf_fstorage_init() is called.
         * The function nrf5_flash_end_addr_get() can be used to retrieve the last address on the
         * last page of flash available to write data. */
        .start_addr = FLASH_START_ADDR,         //    0x70ff0
        .end_addr   = FLASH_END_ADDR,          //     0x80000
    };
    
    
    /* Dummy data to write to flash. */
    static uint32_t m_data          = 0xBADC0FFE;
    static uint32_t gb_m_data=0;
    static char     m_hello_world[] = "hello world";
    static char     gb_restored_string[50];
    
    
    /**@brief   Helper function to obtain the last address on the last page of the on-chip flash that
     *          can be used to write user data.
     */
    static uint32_t nrf5_flash_end_addr_get()
    {
        uint32_t const bootloader_addr = NRF_UICR->NRFFW[0];
        uint32_t const page_sz         = NRF_FICR->CODEPAGESIZE;
        uint32_t const code_sz         = NRF_FICR->CODESIZE;
    
        return (bootloader_addr != 0xFFFFFFFF ?
                bootloader_addr : (code_sz * page_sz));
    }
    
    
    
    /**@brief   Initialize the timer. */
    static void timer_init_1(void)
    {
        ret_code_t err_code = app_timer_init();
        APP_ERROR_CHECK(err_code);
    }
    
    /**@brief   Sleep until an event is received. */
    static void power_manage(void)
    {
    //#ifdef SOFTDEVICE_PRESENT
        (void) sd_app_evt_wait();
    //#else
    //    __WFE();
    //#endif
    }
    
    
    
    static void ble_stack_init(void)
    {
        ret_code_t rc;
        uint32_t   ram_start;
    
        /* Enable the SoftDevice. */
        rc = nrf_sdh_enable_request();
        APP_ERROR_CHECK(rc);
    
        rc = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
        APP_ERROR_CHECK(rc);
    
        rc = nrf_sdh_ble_enable(&ram_start);
        APP_ERROR_CHECK(rc);
    }
    
    static void print_flash_info(nrf_fstorage_t * p_fstorage)
    {
        printf("========| flash info |========");
        printf("erase unit: \t%d bytes",      p_fstorage->p_flash_info->erase_unit);
        printf("program unit: \t%d bytes",    p_fstorage->p_flash_info->program_unit);
        printf("==============================");
    }
    
    static void fstorage_evt_handler(nrf_fstorage_evt_t * p_evt)
    {
        if (p_evt->result != NRF_SUCCESS)
        {
            NRF_LOG_INFO("--> Event received: ERROR while executing an fstorage operation.");
            return;
        }
    
        switch (p_evt->id)
        {
            case NRF_FSTORAGE_EVT_WRITE_RESULT:
            {
                NRF_LOG_INFO("--> Event received: wrote %d bytes at address 0x%x.",
                             p_evt->len, p_evt->addr);
            } break;
    
            case NRF_FSTORAGE_EVT_ERASE_RESULT:
            {
                NRF_LOG_INFO("--> Event received: erased %d page from address 0x%x.",
                             p_evt->len, p_evt->addr);
            } break;
    
            case  NRF_FSTORAGE_EVT_READ_RESULT :
                          printf("Flash read sucess\n");
                          break;
    
    
            default:
                break;
        }
    }
    
    void wait_for_flash_ready(nrf_fstorage_t const * p_fstorage)
    {
        /* While fstorage is busy, sleep and wait for an event. */
        while (nrf_fstorage_is_busy(p_fstorage))
        {
            power_manage();
        }
    }
    
    #endif
    
    //----------------------------------------------------------------------------
    // Global Functions
    //----------------------------------------------------------------------------
    
    void main(void)
    {
     ret_code_t rc=0;
     uint32_t data_1=5;
    
        // Hardware initialization 
        Hardware_INIT();
        #ifdef  FLASH_12
        // Flash initialization 
          Flash_init();
      
        // Flash Read function
          Flash_Operation();
        #endif
        
         
       
        printf("\r Initialization completed \r\n ");
        
    
      
    
        //Power ON Flag set
        gb_Power_ON_flag=1;
    
        // config all function 
        // Config_all();
    
        //nrf_delay_ms(2000);
        gb_Calibration_Time_flag_for_OPT=0;
    
    
    
          // relay made off 
          //x`(1);
    
       
    
          // load 30 sec to timer for PIR to configure 
          gb_PIR_config_time=35;
          
          // wait time 30 sec for PIR sensor to calibrate itself 
          while(gb_PIR_config_time!=0){   /* do nothing */}
          
          printf("delay\n");
          // nrf_drv_gpiote_out_set(const_LED);   // LED can be made ON to indicate PIR is active now
         
          // operation start flag is cleared
          gb_PIR_Operation_Start=0;
       
        
    /* Main loop */
    
    
        while (1)
        {
        // operation 
        }
    }
    
    void Flash_init(void)
    {
     uint32_t data_1=5;
          ret_code_t rc;
    
          #ifdef  FLASH_12
          nrf_fstorage_api_t * p_fs_api;
    
         /* Initialize an fstorage instance using the nrf_fstorage_sd backend.
         * nrf_fstorage_sd uses the SoftDevice to write to flash. This implementation can safely be
         * used whenever there is a SoftDevice, regardless of its status (enabled/disabled). */
          p_fs_api = &nrf_fstorage_sd;
    
    
    
          rc = nrf_fstorage_init(&fstorage, p_fs_api, NULL);
          APP_ERROR_CHECK(rc);
    
          /* It is possible to set the start and end addresses of an fstorage instance at runtime.
         * They can be set multiple times, should it be needed. The helper function below can
         * be used to determine the last address on the last page of flash memory available to
         * store data. */
    //     (void) nrf5_flash_end_addr_get();
    
    
    //       print_flash_info(&fstorage);
    
          #ifdef  UART_Flash
          printf("Flash Init=%d\n",rc);
          #endif
    //
        
    //            wait_for_flash_ready(&fstorage);
    
    
        #endif
    
    }
    
    void Flash_Operation(void)
    {
     ret_code_t rc;
     uint32_t local_var=0, data=5;
     uint8_t  len=0;
      
      /***********************************Flash Read *****************************************/
        #ifdef  FLASH_12
    //      wait_for_flash_ready(&fstorage);
    //    printf("Done.");
    
          rc = nrf_fstorage_read(&fstorage, FLASH_START_ADDR, &local_var, sizeof(local_var));
           
           APP_ERROR_CHECK(rc);
    //        wait_for_flash_ready(&fstorage);
                     
                     NRF_LOG_INFO("Enabling the SoftDevice.");
                      ble_stack_init();
                       
          #ifdef  UART_Flash
            printf("local_var=%x , RC=%x \n",local_var,rc); 
            printf("Flash read Done.\n");
          #endif
    
    
          #endif
         /***********************************Flash read end *****************************************/
    
          /***********************************Flash write *****************************************/
              
    //           rc = nrf_fstorage_erase(&fstorage, FLASH_START_ADDR , 1, NULL);
            
                APP_ERROR_CHECK(rc);
                wait_for_flash_ready(&fstorage);
                rc = nrf_fstorage_write(&fstorage, FLASH_START_ADDR , &data, sizeof(data), NULL);
        
                APP_ERROR_CHECK(rc);
                wait_for_flash_ready(&fstorage);
                
                    #ifdef  UART_Flash
                    printf("ERR code=%d,Flash write %x\n",rc,data);
                    #endif
            /***********************************Flash write end *****************************************/
    
    }
    
    
    
    /*****************************************************************************
    * End of file.
    *****************************************************************************/
    
    

Children
No Data
Related