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

fds_record_find axes using

Good morning I use the fds function to store and read data in flash memory. FDS_SUCCES is not returned by the fds_record_find function when reading data, so the read function does not work Help me solve this part Below is my code

#include "sdk_config.h"
#include "uuid_data.h"
#include "app_error.h" 
#include <string.h >
#include "app_util.h"

#define FILE_ID 0x1111
#define REC_KEY_ID 0x2222

static void fds_evt_handler(fds_evt_t const * const p_fds_evt)
{
    switch (p_fds_evt->id)
    {
        case FDS_EVT_INIT:
            if (p_fds_evt->result == FDS_SUCCESS)
            {
                // Initialization failed.
				printf("init success \n\r");
			}
			break;
		case FDS_EVT_WRITE:
			if(p_fds_evt->result ==FDS_SUCCESS)
			{
				printf("write success \n\r");
			}
			break;
        default:
            break;
    }
}

ret_code_t beacon_uuid_storage_init(void)
{
	ret_code_t ret = fds_register(fds_evt_handler);
	if (ret != FDS_SUCCESS)
	{
		// Registering of the event handler has failed.
	}

	ret = fds_init();
	if (ret != FDS_SUCCESS)
	{
		// Handle error.
	}
	return NRF_SUCCESS;
}

ret_code_t uuid_storage_write(uint16_t file_id,uint16_t key, uint8_t const *p_data,uint16_t length)
{
	fds_record_t record;
	fds_record_desc_t record_desc;
	fds_record_chunk_t record_chunk;
	
	record_chunk.p_data = p_data;
	record_chunk.length_words = BYTES_TO_WORDS(length);
	
	record.file_id = file_id;
	record.key = key;
	record.data.num_chunks = 1;
	record.data.p_chunks = &record_chunk;
	
	ret_code_t ret;
	
	ret = fds_record_write(&record_desc,&record);
	if(ret != FDS_SUCCESS)
	{
		printf("write fail \n\r");
		return ret;
	}
	else if(ret == FDS_SUCCESS)
	{
		printf("write success \n\r");
		return ret;
	}
	
}

ret_code_t uuid_storage_read(uint16_t file_id,uint16_t key,uint8_t *p_data)
{
	ret_code_t ret;
	fds_flash_record_t flash_record;
	fds_record_desc_t record_desc;
	fds_find_token_t ftok ;
	memset(&ftok, 0x00, sizeof(fds_find_token_t));
	
	 while (fds_record_find(file_id, key, &record_desc, &ftok) == FDS_SUCCESS)
	{
		printf("read success \n\r");
		ret = fds_record_open(&record_desc,&flash_record);
	
		memcpy(p_data , flash_record.p_data , (flash_record.p_header->tl.length_words * BYTES_PER_WORD));
	
		ret = fds_record_close(&record_desc);
	
		if(ret != FDS_SUCCESS)
		{
			printf("close fail \n\r");
			return ret;
		}
		return ret;
	}
}


ret_code_t uuid_del(uint16_t file_id,uint16_t key)
{
	fds_flash_record_t flash_record;
	fds_record_desc_t descriptor;
	fds_find_token_t ftok = {0};

	ret_code_t ret = fds_record_find(file_id,key,&descriptor,&ftok);
	ret = fds_record_delete(&descriptor);
	if (ret != FDS_SUCCESS)
	{
		printf("del fail \n\r");
		// Error.
	}
}
Parents
  • In the ble_app_uart example, only the static void nus_data_handler function enter code here

      static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
    {
        static uint8_t state=1,count=0,j;
    	uint8_t d[20],resule[2]={'0','1'},save_uuid2[32];
    	switch(state)
    	{
    		case 1:
    		{
    			for (uint32_t i = 0; i < length; i++)
    			{
    					d[i]=p_data[i];
    					if( d[i]=='1' && length ==1)
    					{
    						printf("saver\n\r");
    						printf("%d  %d\n\r",d[0],length);
    						state =2;
    						break;
    					}
    					if( d[i]=='2' && length ==1)
    					{
    							printf("saver\n\r");
    						uuid_storage_read(FILE_ID,REC_KEY_ID,save_uuid2);
    							printf("saver\n\r");
    						for(j=0;j<32;j++)
    						{
    							printf("%d \n\r",save_uuid2[j]);
    						}
    							printf("saver\n\r");
    						break;
    					}
    					if( d[i]=='3' && length ==1)
    					{
    						printf("del\n\r");
    						uuid_del(FILE_ID,REC_KEY_ID);
    								printf("del\n\r");
    						break;
    					}
    			}
    			break;
    		}
    		case 2:
    		{
    			for (uint32_t i = 0; i < 32; i++)
    			{
    				save_uuid[i] = p_data[i];
    			}
    			count++;
    			uuid_storage_write(FILE_ID,REC_KEY_ID,save_uuid,16);
    			 ble_nus_string_send(&m_nus, &resule[1], 1);
    			state=1;
    			break;
    		}
    	}
    
    }
    

    I've fixed this. If ble is 1, it fds_write 16 digit value and when value of 2 is entered, it deletes when output 3 value comes in.

Reply
  • In the ble_app_uart example, only the static void nus_data_handler function enter code here

      static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
    {
        static uint8_t state=1,count=0,j;
    	uint8_t d[20],resule[2]={'0','1'},save_uuid2[32];
    	switch(state)
    	{
    		case 1:
    		{
    			for (uint32_t i = 0; i < length; i++)
    			{
    					d[i]=p_data[i];
    					if( d[i]=='1' && length ==1)
    					{
    						printf("saver\n\r");
    						printf("%d  %d\n\r",d[0],length);
    						state =2;
    						break;
    					}
    					if( d[i]=='2' && length ==1)
    					{
    							printf("saver\n\r");
    						uuid_storage_read(FILE_ID,REC_KEY_ID,save_uuid2);
    							printf("saver\n\r");
    						for(j=0;j<32;j++)
    						{
    							printf("%d \n\r",save_uuid2[j]);
    						}
    							printf("saver\n\r");
    						break;
    					}
    					if( d[i]=='3' && length ==1)
    					{
    						printf("del\n\r");
    						uuid_del(FILE_ID,REC_KEY_ID);
    								printf("del\n\r");
    						break;
    					}
    			}
    			break;
    		}
    		case 2:
    		{
    			for (uint32_t i = 0; i < 32; i++)
    			{
    				save_uuid[i] = p_data[i];
    			}
    			count++;
    			uuid_storage_write(FILE_ID,REC_KEY_ID,save_uuid,16);
    			 ble_nus_string_send(&m_nus, &resule[1], 1);
    			state=1;
    			break;
    		}
    	}
    
    }
    

    I've fixed this. If ble is 1, it fds_write 16 digit value and when value of 2 is entered, it deletes when output 3 value comes in.

Children
No Data
Related