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

using Pstorage while BLE scanning

Hi everyone,

I am using the following: *nRF51422 *SDK11 *keil uVision 5.17 *s130

I have been working with nRF51 that scans beacons and stores it into persistent storage.

I tweaked an example code, called ble_app_uart_c in ble_central of SDK11, and was able to tweak that code to scan beacon and to read data packets.

The next step is to store that information into persistent storage.

Using pstorage_store, pstorage_load and pstorage_update work fine before BLE scan start. However, pstorage_store and pstorage_update fall into while loop because of pstorage_wait_flag, since BLE scanning started. The weird thing is that pstorage_load work fine whenever I call.

Here is some code snippets.

      //Code Includes all necessary initiate functions, handlers, dispatches

      void pstorage_init_block() {...}
      void pstorage_load_block() {...}
      void pstorage_store_block() {...}

      void pstorage_update_block(uint32_t source_data)
      {
           uint8_t 	source[4] = {(source_data >> 24) & 0xFF, 
					  (source_data >> 16) & 0xFF, 
					  (source_data >> 8) & 0xFF, 
					  (source_data >> 0) & 0xFF};

           pstorage_block_identifier_get(&handle, 0, &block_0_handle);
           pstorage_wait_handle = block_0_handle.block_id;
           pstorage_wait_flag = 1;
           pstorage_update(&block_0_handle, source, 4, 4);
           while(pstorage_wait_flag) { power_manage(); }
       }

       
      static bool is_uuid_present(const ble_uuid_t *p_target_uuid, const ble_gap_evt_adv_report_t *p_adv_report)
      {
          uint32_t 	index 	= 0;
          uint8_t 	*p_data = (uint8_t *)p_adv_report->data;

          while (index < p_adv_report->dlen)
          {
              ...

                  if (p_adv_report->rssi > -50) 
                  {
                       pstorage_load_block();          // work fine
                       pstorage_update_block(10);      // break here (fall into while loop in the pstorage_update_block function)
                       pstorage_load_block();
                  }
             ...
          }
      }

       ...

       main()
       {
               ...
               ...
            pstorage_initialization();           // work fine
            pstorage_store_block();              // work fine
            pstorage_load_block();               // work fine
            pstorage_update_block(4294967295);   // work fine
            pstorage_load_block();               // work fine

            err_code = ble_db_discovery_init();
            APP_ERROR_CHECK(err_code);
	
            scan_start();
      }

I would appreciate to someone who post any idea or share a solution.

Thank you so much.

Parents
  • Not sure which S130 version you are working with. But when you are using pstorage the Softdevice will calculate how much time flash operations need before granting it time in its scheduler. Update block means creating a swap block and erasing the main block before updating the block again. So I assume this is blocked as the softdevice is giving scanning priority. Consider updating the block after you are done scanning or increase SD_CMD_MAX_TRIES.

  • Yes, you are right. I am using sd_ble_gap_scan_start.

    Here is the code for the scan parameters:

    #define SCAN_INTERVAL           0x00A0                          
    #define SCAN_WINDOW             0x0050                         
    #define SCAN_ACTIVE               1                              
    #define SCAN_SELECTIVE          0                              
    #define SCAN_TIMEOUT            0x01A4
    
    static const ble_gap_scan_params_t m_scan_params = 
      {
        .active      = SCAN_ACTIVE,
        .selective   = SCAN_SELECTIVE,
        .p_whitelist = NULL,
        .interval    = SCAN_INTERVAL,
        .window      = SCAN_WINDOW,
        .timeout     = SCAN_TIMEOUT
      };
    

    One thing that I tried is to call sd_ble_gap_scan_stop() before using pstorage. And, it did not work that I expected.

Reply
  • Yes, you are right. I am using sd_ble_gap_scan_start.

    Here is the code for the scan parameters:

    #define SCAN_INTERVAL           0x00A0                          
    #define SCAN_WINDOW             0x0050                         
    #define SCAN_ACTIVE               1                              
    #define SCAN_SELECTIVE          0                              
    #define SCAN_TIMEOUT            0x01A4
    
    static const ble_gap_scan_params_t m_scan_params = 
      {
        .active      = SCAN_ACTIVE,
        .selective   = SCAN_SELECTIVE,
        .p_whitelist = NULL,
        .interval    = SCAN_INTERVAL,
        .window      = SCAN_WINDOW,
        .timeout     = SCAN_TIMEOUT
      };
    

    One thing that I tried is to call sd_ble_gap_scan_stop() before using pstorage. And, it did not work that I expected.

Children
No Data
Related