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

Debugging ble_nus_data_send

Hello,

I'm using the following code to loop through the flash memory sequentially and send records created by the flash_fds example:

void SendTime(void)
{

  fds_flash_record_t  flash_record = {0};
  fds_record_desc_t   desc1 = {0};
  fds_find_token_t    tok1 = {0};
  ret_code_t rc;

  /* It is required to zero the token before first use. */
  memset(&tok1, 0x00, sizeof(fds_find_token_t));

  while (fds_record_find(FILE_ID, RECORD_KEY_1, &desc1, &tok1) == NRF_SUCCESS) //loop thru all records with this address
  {
    /* Open the record and read its contents. */
    rc = fds_record_open(&desc1, &flash_record);
    APP_ERROR_CHECK(rc);

    uint8_t hold[20] = {0}; 

    for(int i = 0; i < 20; i++) //get the data from flash memory 
    {
      hold[i] = *(volatile uint8_t *)flash_record.p_data;
      flash_record.p_data++;
    }

    uint16_t length1 = sizeof(hold);
    rc = ble_nus_data_send(&m_nus, hold, &length1, m_conn_handle);
        

    /* Close the record when done reading. */
    rc = fds_record_close(&desc1);
    APP_ERROR_CHECK(rc);

  }
}

The data stored in the records are strings generated with the following code where rx_data holds info from a real time clock.  The clock gets the start time for when a button is pressed then generates a string such as: "S: 12:33:53 7/18/21" (s for start) and then gets the time when the same button is released which generates a string such as: "E: 12:34:08 7/18/21".  (e for end)

snprintf(intermediate, sizeof(intermediate), "S: %x:%x:%d %x/%x/%x", rx_data[5], rx_data[4], rx_data[3], rx_data[8], rx_data[6], rx_data[9]);
snprintf(intermediate, sizeof(intermediate), "E: %x:%x:%d %x/%x/%x", rx_data[5], rx_data[4], rx_data[3], rx_data[8], rx_data[6], rx_data[9]);

I'm using the nRF  toolbox to receive the data sent over bluetooth and it works fine for up to 5 start/end pairs, but if there is more than 5 pairs it only sends 11 strings (5 pairs + 1 extra start time without an end time).  I've looked at the device memory and the times are still correctly writing so it seems to be an issue with the amount of data being sent. 

I was wondering if there were any constraints on the amount of data that can be sent using ble_nus_data_send (I don't think this is an issue because I'm just calling it once for each statement) or the amount of data which can be received with the nRF toolbox app.  My first idea is that there is an issue with the while loop that loops through the files in memory, but I used debugging statements to determine that the while loop that goes through the memory is working properly.  This seems to narrow the problem down to the ble_nus_data_send being called multiple times or the nRF toolbox app.

Parents Reply Children
  • Ahh I didn't realize I had overwritten it good catch.  It did indeed return 0x13 as the error code.  To fix it I changed my code to this: (change commented at line 30) based on the reply from Sigurd in this devzone question: devzone.nordicsemi.com/.../how-to-deal-with-nrf_error_resources

    void SendTime(void)
    {
      fds_flash_record_t  flash_record = {0};
      fds_record_desc_t   desc1 = {0};
      fds_find_token_t    tok1 = {0};
      ret_code_t rc;
      uint8_t countEntries = 0;
      uint32_t       err_code;
    
      /* It is required to zero the token before first use. */
      memset(&tok1, 0x00, sizeof(fds_find_token_t));
    
      while (fds_record_find(FILE_ID, RECORD_KEY_1, &desc1, &tok1) == NRF_SUCCESS) //loop thru all records with this address
      {
        /* Open the record and read its contents. */
        rc = fds_record_open(&desc1, &flash_record);
        APP_ERROR_CHECK(rc);
    
        uint8_t hold[20] = {0}; 
    
        //flash_record.p_data is a pointer to the physical memory location
        for(int i = 0; i < 20; i++) //get the string from flash memory
        {
          hold[i] = *(volatile uint8_t *)flash_record.p_data;
          flash_record.p_data++;
        }
    
        uint16_t length1 = sizeof(hold);
        
        //****UPDATED CODE****
        //loop to handle soft device buffer full 
        do
        {
          err_code = ble_nus_data_send(&m_nus, hold, &length1, m_conn_handle);
          if ( (err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_RESOURCES) &&
              (err_code != NRF_ERROR_NOT_FOUND) )
          {
            APP_ERROR_CHECK(err_code);
          }
    
        } while (err_code == NRF_ERROR_RESOURCES);
    
    
        /* old code not working
    
        rc = ble_nus_data_send(&m_nus, hold, &length1, m_conn_handle);
       
        */
    
        /* Close the record when done reading. */
        rc = fds_record_close(&desc1);
        APP_ERROR_CHECK(rc);
    
      } //end while loop
    }

    Thanks for the help!

Related