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

Can't get a callback from FDS no matter what - help!

I am able to initialize the module and get the callback , but when I try to write I will not get the callback after the write, and no errors.

If I simply try to read after write he will not find anything to read.

Please help, I'v been trying 2 days without any clue. I guess again there is some hidden parameter that nobody wrote on the docs.

This is the output :

:INFO:STARTED
:INFO:fds_evt_handler with :  0
:INFO: init good!
:INFO:Writing Record ID = 1 
:INFO:Writing Record ID = 2 

This is the program :

 static void fds_evt_handler(fds_evt_t const * const p_fds_evt)
    {
     
       // ****this line will log ONLY on init event, not on write event
       NRF_LOG_INFO("fds_evt_handler with :  %d\r\n",p_fds_evt->id); 
    
        switch (p_fds_evt->id)
        {
    
            case FDS_EVT_INIT:
                if (p_fds_evt->result != FDS_SUCCESS)
                {
                     NRF_LOG_INFO("failed initializing\r\n"); 
                }
                 else
                 {
                   NRF_LOG_INFO(" init good!\r\n"); 
                   init_flag=1;
                 }
    
                break; 
    
           case FDS_EVT_WRITE:
    
              write_flag=1;
               if (p_fds_evt->result == FDS_SUCCESS)  
                    {  NRF_LOG_INFO("finish write\r\n"); }
               else 
                    { NRF_LOG_INFO("failed write with %d \r\n",p_fds_evt->result); }
        
    
              break;
    
    
            case FDS_EVT_UPDATE:
    
               write_flag=1;
               if (p_fds_evt->result == FDS_SUCCESS)  
                      {NRF_LOG_INFO("finish write\r\n"); }
               else 
                    { NRF_LOG_INFO("failed updated with %d \r\n",p_fds_evt->result); }
              break;
    
            default:
                break;
        }
    }
    
    
    
    void initStorage()
    {
    
            ret_code_t ret = fds_register(fds_evt_handler);
            if (ret != FDS_SUCCESS)
            {
                NRF_LOG_INFO("FAILED TO INIT STORAGE\r\n"); 
            }
            ret = fds_init();
            if (ret != FDS_SUCCESS)
            {
                NRF_LOG_INFO("ERROR INIT\r\n"); 
            }
    
           	
     
    
    }
    
    
      void  write()
    {
    	        #define FILE_ID     0x1111
    		#define REC_KEY     0x2222
    		static uint32_t const m_deadbeef[2] = {0xDEADBEEF,0xBAADF00D};
    		fds_record_t        record;
    		fds_record_desc_t   record_desc;
    		fds_record_chunk_t  record_chunk;
    		// Set up data.
    		record_chunk.p_data         = m_deadbeef;
    		record_chunk.length_words   = 2;
    		// Set up record.
    		record.file_id              = FILE_ID;
    		record.key              		= REC_KEY;
    		record.data.p_chunks       = &record_chunk;
    		record.data.num_chunks   = 1;
    				
    		ret_code_t ret = fds_record_write(&record_desc, &record);
    		if (ret != FDS_SUCCESS)
    		{
    	          NRF_LOG_INFO("failed write with %d \r\n",ret);
    		}
    		 NRF_LOG_INFO("Writing Record ID = %d \r\n",record_desc.record_id);
    	 
    
          
        
       
                    ret = fds_record_write(&record_desc, &record);
    		if (ret != FDS_SUCCESS)
    		{
    				NRF_LOG_INFO("ERROR WROTE WITH %d\r\n",ret); 
    		}
                    NRF_LOG_INFO("Writing Record ID = %d \r\n",record_desc.record_id);
    	
    
    
    }
    
    
    void read ()
    {
                     NRF_LOG_INFO("START READING\r\n"); 
    
    		#define FILE_ID     0x1111
    		#define REC_KEY     0x2222
    		fds_flash_record_t  flash_record;
    		fds_record_desc_t   record_desc;
    		fds_find_token_t    ftok ={0};//Important, make sure you zero init the ftok token
    		uint32_t *data;
    		uint32_t err_code;
    		
    		 
    		// Loop until all records with the given key and file ID have been found.
    		while (fds_record_find(FILE_ID, REC_KEY, &record_desc, &ftok) == FDS_SUCCESS)
    		{
    				err_code = fds_record_open(&record_desc, &flash_record);
    				if ( err_code != FDS_SUCCESS)
    				{
                                       NRF_LOG_INFO("error read: %d\r\n",err_code); 		
    				}
    				
    				NRF_LOG_INFO("Found Record ID = %d\r\n",record_desc.record_id);
    				NRF_LOG_INFO("Data = ");
    				data = (uint32_t *) flash_record.p_data;
    				for (uint8_t i=0;i<flash_record.p_header->tl.length_words;i++)
    				{
    					NRF_LOG_INFO("0x%8x ",data[i]);
    				}
    				NRF_LOG_INFO("\r\n");
    				// Access the record through the flash_record structure.
    				// Close the record when done.
    				err_code = fds_record_close(&record_desc);
    				if (err_code != FDS_SUCCESS)
    				{
    				      NRF_LOG_INFO("error read: %d\r\n",err_code); 
    				}
    		}
    
    
    }
    
     void find_and_delete (void)
    {
                    #define FILE_ID     0x1111
    		#define REC_KEY     0x2222
    		fds_record_desc_t   record_desc;
    		fds_find_token_t    ftok;
    	
    		ftok.page=0;
    		ftok.p_addr=NULL;
    		// Loop and find records with same ID and rec key and mark them as deleted. 
    		while (fds_record_find(FILE_ID, REC_KEY, &record_desc, &ftok) == FDS_SUCCESS)
    		{
    			fds_record_delete(&record_desc);
    			NRF_LOG_INFO("Deleted record ID: %d \r\n",record_desc.record_id);
                             
    		}
    	 
    		ret_code_t ret = fds_gc();
    		if (ret != FDS_SUCCESS)
    		{
                          NRF_LOG_INFO("error find: %d\r\n",ret); 
    		}
    		 
    }
    
    
 static void sys_evt_dispatch(uint32_t sys_evt)
{
    fs_sys_event_handler(sys_evt);
}
    
     
     
     
    int main(void)
    {
    
    
            log_init();
            NRF_LOG_INFO("STARTED\r\n"); 
     
    
    
             setupBluetoothWithDelegate(BluetoothDelegate); 
    
                // fds
                   initStorage();
    		find_and_delete();
    		 write();
    		//wait until the write is finished. 
    		while (write_flag==0);
    		read();
    	 
          
      
           advertise();
    
              
        
           
    
            for (;;)
            {
     
              power_manage();
            }
     
    
       
    }
Parents
  • FormerMember
    0 FormerMember

    What does setupBluetoothWithDelegate(..) do?

    Do you add sys_evt_dispatch() to softdevice_sys_evt_handler_set():

    static void sys_evt_dispatch(uint32_t sys_evt)
    {
    // Dispatch the system event to the fstorage module, where it will be
    // dispatched to the Flash Data Storage (FDS) module.
    fs_sys_event_handler(sys_evt);
    
    ...
    }
    
    
    static void ble_stack_init(void)
    {
        ...
        err_code = softdevice_sys_evt_handler_set(sys_evt_dispatch);
        APP_ERROR_CHECK(err_code);
    } 
    

    Update: The purpose of registering an event handler through softdevice_sys_evt_handler_set() is to have an event handler for SOC events "going through"/forwarded by the softdevice. For doing write operations, fstorage uses sd_flash_write(). When sd_flash_write() has successfully completed or upon an error, an event will be fetched. This event is handled by fs_sys_event_handler() in fstorage.c. If no event handler is registered to the softdevice, the softdevice will not have any place to forward the event.

    Therefore, softdevice_sys_evt_handler_set(sys_evt_dispatch) has to be called, with sys_evt_dispatch() containing the relevant event handlers.

Reply
  • FormerMember
    0 FormerMember

    What does setupBluetoothWithDelegate(..) do?

    Do you add sys_evt_dispatch() to softdevice_sys_evt_handler_set():

    static void sys_evt_dispatch(uint32_t sys_evt)
    {
    // Dispatch the system event to the fstorage module, where it will be
    // dispatched to the Flash Data Storage (FDS) module.
    fs_sys_event_handler(sys_evt);
    
    ...
    }
    
    
    static void ble_stack_init(void)
    {
        ...
        err_code = softdevice_sys_evt_handler_set(sys_evt_dispatch);
        APP_ERROR_CHECK(err_code);
    } 
    

    Update: The purpose of registering an event handler through softdevice_sys_evt_handler_set() is to have an event handler for SOC events "going through"/forwarded by the softdevice. For doing write operations, fstorage uses sd_flash_write(). When sd_flash_write() has successfully completed or upon an error, an event will be fetched. This event is handled by fs_sys_event_handler() in fstorage.c. If no event handler is registered to the softdevice, the softdevice will not have any place to forward the event.

    Therefore, softdevice_sys_evt_handler_set(sys_evt_dispatch) has to be called, with sys_evt_dispatch() containing the relevant event handlers.

Children
  • At the init_ble_stack function I have this :

    err_code = softdevice_ble_evt_handler_set(ble_evt_dispatch);
    APP_ERROR_CHECK(err_code);
    

    Do I have to add also your lines ? if so , why ? can you explain please ?

  • FormerMember
    0 FormerMember in reply to FormerMember

    I have updated my answer to answer your questions.

  • I love you :) Thanks Kristen. first of all adding that line solved the problem right away. Then I have questions, please if I may .

    1. There are 2 things to register and I don't understand if your explanation is regarding the first or second. The softdevice_sys pushes from the soft device to the FS, and the fs_sys from fs to the application ?

    2. As I see you are a Nordic employ, I must ask, why, why you are making things so hard? :) You built an amazing chip, and yet for every little thing I have to fight because it's not on the docs. I would never find out that I have to put this line without you, and the same happened to me with the PWM and config file where you have to enable anything you do on the config in many other modules. If you would make things super clear and friendly (put things in classes with clear setup like arduino) you would become super popular. Thanks again!

  • FormerMember
    0 FormerMember in reply to FormerMember

    Good to hear that it works now!

    1) Yes, that's correct, the flow is the following: The softdevice pushes events to fs_sys_event_handler (fstorage.c) --> fs_event_handle (fds.c) --> my_fds_event_handler (main.c)

    2) We want to make things clear, and we try to make the documentation good. But I do see that it lacks some small, but important details. I will report what you mentioned regarding PWM internally, I see that sdk_config.h always mentioned in the documentation for other library as well. Hopefully the documentation can be updated!

  • Thanks, I read the fds doc 4 times, no single word about this line.would be simple to just create classes that are encapsulated with things like saveToMemory(fileID,Key) , instead of huge messy example projects..

    Anyway - thank you again .

Related