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

issue about fds_record_write( ) nRF5SDK160098a08e2

Hi

   I had got some problem with fds_record_write(). I found that I couldn't get the correct data from flash without waiting fds_evt_handler( ) that was called after calling fds_record_write( ).

This is the wrong data below.

So I change my code.I had waited  fds_evt_handler( )  that was called after calling fds_record_write( ).And I got the correct data.There is below.

And there is my code with waitting  fds_evt_handler( ). It must wait for the fds_evt_handler( ) after calling fds_record_write( )?






/*********************************************************************
 * 										Logcal	Varialbe
 */

#ifdef FLASH_STORE

static bool 	data_write_flash_flag;

#endif		//FLASH_STORE



/*********************************************************************
 * 										Logcal	Function
 */

#ifdef FLASH_STORE

static void fds_evt_handler(fds_evt_t const * p_evt)
{
    switch (p_evt->id)
    {
        case FDS_EVT_INIT:
            break;

        case FDS_EVT_WRITE:
				case FDS_EVT_UPDATE:
					if( ( p_evt->write.file_id == DATA_FILE_ID )&&
							( p_evt->write.record_key == DATA_RECORD_KEY ) )
					 {
						  data_write_flash_flag = false;
					 }
					break;
			
        case FDS_EVT_DEL_RECORD:
					if( ( p_evt->write.file_id == DATA_FILE_ID )&&
							( p_evt->write.record_key == DATA_RECORD_KEY ) )
					 {
						  //data_delete_flash_flag = false;
					 }
					break;

				case FDS_EVT_DEL_FILE:
					break;
				
				case FDS_EVT_GC:
					break;
				
        default:
          break;
    }
}

#endif		//FLASH_STORE


/*********************************************************************
 * 										Gobal	Function
 */



/*********************************************************************
 * @fn      data_store_init
 *
 * @brief   initializing data store
 *
 *
 * @param   none
 *
 * @return  none
 */

void data_store_init( )
{
#ifdef FLASH_STORE
	uint32_t init_err;
	
 	data_write_flash_flag = false;
 	//data_delete_flash_flag = false;
	
	init_err = fds_register( fds_evt_handler );
	APP_ERROR_CHECK( init_err );
	
	//init_err = fds_init( );					//had done in peer_manager_init( )
	//APP_ERROR_CHECK( init_err );
#endif		//FLASH_STORE
}


/*********************************************************************
 * @fn      data_store_write
 *
 * @brief   write data to flash
 *
 *
 * @param   none
 *
 * @return  none
 */


void data_store_write( )
{
#ifdef FLASH_STORE
	 if( !data_write_flash_flag )
	  {
			 uint32_t write_err = 0;
			
			 fds_record_desc_t desc = {0};
			 fds_find_token_t  tok  = {0};
			 
			 fds_record_t data_record;
			 uint8_t writeData[DATA_RECORD_LENGTH];		//__align(4)
			 uint8_t temp;
			 
			 writeData[0] = RECORD_HEAD;
			 
			 if( bondPeer.bondStatus )
				{
					 writeData[1] = RECORD_BOND;
				}
			 else
				{
					 writeData[1] = 0;
				}
			 
			 temp = 0;
			 for( uint8_t loopCnt=0; loopCnt<6; loopCnt++ )
				{
					 writeData[loopCnt+2] = Addr[loopCnt];
					 temp += Addr[loopCnt];
				}
			 
			 writeData[8] = temp;
			 writeData[9] = RECORD_END;
			 
			 data_record.file_id = DATA_FILE_ID;
			 data_record.key = DATA_RECORD_KEY;
			 data_record.data.p_data = writeData;
			 data_record.data.length_words = ( sizeof(writeData)+3 )/4;

			 write_err = fds_record_find( DATA_FILE_ID, DATA_RECORD_KEY, &desc, &tok );
			 if( write_err == NRF_SUCCESS )
				{
					 data_write_flash_flag = true;
					 
					 write_err = fds_record_update( &desc, &data_record );
					 if( write_err != NRF_SUCCESS )
					  {
							 data_write_flash_flag = false;
							 APP_ERROR_CHECK(write_err);
						}
					 else
					  {
							 while( data_write_flash_flag !=0 );
						}
						
				}
			 else
				{
					 data_write_flash_flag = true;
					
					 write_err = fds_record_write( &desc, &data_record );
					 if( write_err != NRF_SUCCESS )
					  {
							 data_write_flash_flag = false;
							 APP_ERROR_CHECK(write_err);
						}
					 else
					  {
							 while( data_write_flash_flag !=0 );
						}
				}
	  }
#endif		//FLASH_STORE
}

/*********************************************************************
 * @fn      data_store_read
 *
 * @brief   get data from store
 *
 *
 * @param   none
 *
 * @return  none
 */

void data_store_read( )
{
	 Status = false;
	
#ifdef FLASH_STORE
	 uint32_t read_err;
	 
	 fds_record_desc_t desc = {0};
   fds_find_token_t  tok  = {0};

   read_err = fds_record_find( DATA_FILE_ID, DATA_RECORD_KEY, &desc, &tok );
	 if( read_err == NRF_SUCCESS )
	  {
				fds_flash_record_t config = {0};
				uint8_t *readDataPointer;
				uint8_t readData[DATA_RECORD_LENGTH];
				uint8_t	dataTemp;
				
        /* Open the record and read its contents. */
        read_err = fds_record_open(&desc, &config);
        APP_ERROR_CHECK(read_err);
				
				readDataPointer = (uint8_t *)config.p_data;
				for( uint8_t rLoopCnt=0; rLoopCnt<DATA_RECORD_LENGTH; rLoopCnt++ )
				 {
					  readData[rLoopCnt] = readDataPointer[rLoopCnt];
				 }
				
				read_err = fds_record_close(&desc);
        APP_ERROR_CHECK(read_err);
				
				if( ( readData[0] == RECORD_HEAD )
						&&( readData[9] == RECORD_END ) )
				 {
					  
					  if( readData[1] == RECORD_BOND )
						 {
							  Status = true;
						 }
						 
						dataTemp = 0;
						for( uint8_t loopCnt=0; loopCnt<6; loopCnt++ )
						 {
								Addr[loopCnt] = readData[loopCnt+2];
								dataTemp += Addr[loopCnt];
						 }
						
						if( dataTemp != readData[8] )
						 {
							  Status = false;
						 }
				 }
		}
#endif		//FLASH_STORE
		
}

Unfortunately, My code was stuck at while( data_write_flash_flag !=0 ). And then  I check the assembler, I found the " while( data_write_flash_flag !=0 ) " was compiled to CMP r0, #0x00. It compared to zero ,so my code was stuck at here.I have no idea for that. please tell me what do I miss.

Parents Reply Children
  • Hi,

    james said:
    And what abuout "It must wait for the fds_evt_handler( ) after calling fds_record_write( )?"

    I don't know if this is still a question, but if so, the answer is that all FDS operations are asynchronous. So the writer will take some time to complete, and therefore you need to wait for the FDS event indicating that it has completed. That does not mean that you cannot do other things, but the write buffer needs to stay valid, and you cannot read it etc. before it has actually been written.

Related