Reply
Posts: 29
Registered: ‎12-16-2016
Accepted Solution

Thunderboard sense non volatile memory

Dear experts 

 

I am trying to develop an application on Thunderboard sense where i am using external sensor and external ADC through SPI interface. SPI i am using through expansion header present on the board.

So far i have successfully been able to read ADC count from external ADC. Further i want to store two calibration constants in some non volatile memory. These two constants are 16 bit integer values. I am not able to figure out a proper method to do so. If anyone can shed some light on topic it will be great help.

It’s not the years in your life that count. It’s the life in your years.
Highlighted
Posts: 2,405
Registered: ‎08-25-2015

Re: Thunderboard sense non volatile memory

Hi,

 

You can use the Persistent Storage, check the API ref section 2.3.

 

Regards,

Tiago

Posts: 29
Registered: ‎12-16-2016

Re: Thunderboard sense non volatile memory

hi tmonte

 

Thanks a lot for your response.

Although it does not solve my problem, the API reference which you are talking about, as per my understanding is for saving something from a master device. That means it involves maling a bluetooth service and using that service from master device to store something in the uC flash. I have interfaced an external ADC to Thunderboard sense which samples data from another sensor. This ADC is interfaced to the SPI ports available on the expansion header. My requirement is pretty simple i just want to store two ADC sample(lowest value sample and highest value sample) somewhere in non volatile memory which can be later used for displaying actual physical value(i.e temprature in degree celcius or farenhite). I am using firmware made available by silicon labs(Thunderboard sense official app). So i want to write some routine inside this firmware itself to store two values in the Flash. I dont want to create another bluetooth service for this.

 

I hope i have been able to explain it this time.

It’s not the years in your life that count. It’s the life in your years.
Posts: 2,405
Registered: ‎08-25-2015

Re: Thunderboard sense non volatile memory

Hi,

 

The Persistent Storage is for saving whatever you want and it's decoupled from the Bluetooth stack as such. The stack also uses it to save some data (e.g. bonding information) and therefore you should use only the key indexes given in the API.

 

Regards,

Tiago

Posts: 29
Registered: ‎12-16-2016

Re: Thunderboard sense non volatile memory

hi sir

i am unable to find persistence storage file

There is no description in API ref manual as to which files to include and which files not to include. And where can i find files in directory?

It’s not the years in your life that count. It’s the life in your years.
Posts: 2,405
Registered: ‎08-25-2015

Re: Thunderboard sense non volatile memory

Hi,

 

I'm not really sure what you are talking about. The Persistent Storage is just and abstraction API to save data into the flash, it has nothing to do with files.

 

You can find the Persistent Storage APIs description in the Blue Gecko API Reference Manual section 2.6.

 

Regards,

Tiago

Posts: 29
Registered: ‎12-16-2016

Re: Thunderboard sense non volatile memory

Dear sir 

 

I know i have read that i have to use following function 

* Function */
struct gecko_msg_flash_ps_save_rsp_t *gecko_cmd_flash_ps_save(uint16 key, uint8 value_len, const uint8 *value_d
ata);
/* Response id */
gecko_rsp_flash_ps_save_id
/* Response structure */
struct gecko_msg_flash_ps_save_rsp_t
{
uint16 result;
};

but to use this function i will have to include some .C or .H file in my project folder which will contain declaration of function.

I am unable to find those files which contains the declaration.

Or can i get any reference as how to use these function, Is there any reference of it being used in thunderboard official app firmware, where i can refer and see how these can be used? 

 

Any source code example where flash storage is used (e.g iBeacon example ) will be very useful. You just have to guide me in the right direction.

 

 

 

 

 

 

It’s not the years in your life that count. It’s the life in your years.
Posts: 29
Registered: ‎12-16-2016

Re: Thunderboard sense non volatile memory

void PSwrite( uint8array *writeValue )
{

   printf("UI: LED write; %d : %02x\r\n",
          writeValue->len,
          writeValue->data[0]
      );
   gecko_cmd_flash_ps_save(0x4000,sizeof(writeValue),(const uint8 *)writeValue);
   gecko_cmd_gatt_server_send_user_write_response(
      conGetConnectionId(),
	  gattdb_write_PS,
      PS_WRITE_OK
      );

   return;

}
void PSread( void )
{
	//static struct gecko_msg_flash_ps_load_rsp_t * resp2;
//	static uint16 resultV=0;
//	uint8array valV = {0};
   //uiButtonState = BOARD_pushButtonGetState();
   printf("UI: Buttons = \r\n" );

   struct gecko_msg_flash_ps_load_rsp_t * resp = gecko_cmd_flash_ps_load(0x4000);
   /* Send response to read request */
//   resultV = resp->result;
//   valV = resp->value;
   gecko_cmd_gatt_server_send_user_read_response( conGetConnectionId(),
		   	   	   	   	   	   	   	   	   	   	   gattdb_write_PS,
                                                  0,
                                                  sizeof(resp->value),
                                                  (uint8_t *)&(resp->value ));

   return;

}

I am writing 0x050506 through iPhone APP

 

Am i missing something here because the value returned on read is always 0x04

 

image1.PNG

image2[254].png

serviceSetting.png

 

It’s not the years in your life that count. It’s the life in your years.
Posts: 2,582
Registered: ‎09-01-2015

Re: Thunderboard sense non volatile memory

[ Edited ]

You are not using the response to ps_load command correctly. See code below:

// your code:
     gecko_cmd_gatt_server_send_user_read_response( conGetConnectionId(),
  		   	   	   	   	   	   	   	   	   	   	   gattdb_write_PS,
                                                    0,
                                                    sizeof(resp->value),
                                                    (uint8_t *)&(resp->value ));

// fixed version:
     gecko_cmd_gatt_server_send_user_read_response( conGetConnectionId(),
      		   	   	   	   	   	   	   	   	   	   	   gattdb_write_PS,
                                                        0,
                                                        resp->value.len,
                                                        (uint8_t *)(resp->value.data));

note that resp->value is a struct of type uint8array that is defined as follows:

typedef struct
{
	uint8 len;
	uint8  data[];
}uint8array;

 

 

 

Posts: 29
Registered: ‎12-16-2016

Re: Thunderboard sense non volatile memory

Thank you

 

For pointing out my mistake. I have corrected it . It is saving properly but when i am trying to load the value

value.data [0] is always read as 0

 

eg when I am writing 0x11223344 through my bluetooth app i am able to read value as 

value.data [0] = 0x00

value.data [1] = 0x11

value.data[2] = 0x22

value.data [3] = 0x33

value.data [4] = 0x44

 

is this suppose to happen ?

 

It’s not the years in your life that count. It’s the life in your years.
Posts: 2,582
Registered: ‎09-01-2015

Re: Thunderboard sense non volatile memory

Did you also fix this error in your code?

 gecko_cmd_flash_ps_save(0x4000,sizeof(writeValue),(const uint8 *)writeValue);

It's basically the same error as I pointed out earlier.  THe code should look something like this:

 gecko_cmd_flash_ps_save(0x4000,writeValue->len,writeValue->data);
Posts: 29
Registered: ‎12-16-2016

Re: Thunderboard sense non volatile memory

I also have one more issue

 

I have a float variable which needs to be saved in non volatile memory or the persistent storage

 

as per the definition of

/* Function */
struct gecko_msg_flash_ps_save_rsp_t *gecko_cmd_flash_ps_save(uint16 key, uint8array value);
/* Response id */
gecko_rsp_flash_ps_save_id
/* Response structure */
struct gecko_msg_flash_ps_save_rsp_t
{
uint16 result;
};

I think only 8 bit integers are allowed

Is there any other way or work around which can help me in storing a float variable ?

 

It’s not the years in your life that count. It’s the life in your years.
Posts: 2,582
Registered: ‎09-01-2015

Re: Thunderboard sense non volatile memory

PS store allows you to save byte arrays in flash. Any variables (integers, floats, structs ..) are stored in memory as byte arrays. In case of float, four bytes are required to store the value.

 

I wrote a pair of helper functions that allow you to store basically any data in a PS key:

uint16_t ps_save_object(uint16_t key, void *pValue, uint8_t size)
{
	struct gecko_msg_flash_ps_save_rsp_t *pResp;

	pResp = gecko_cmd_flash_ps_save(key, size, pValue);

	return(pResp->result);
}

uint16_t ps_load_object(uint16_t key, void *pValue, uint8_t size)
{
	struct gecko_msg_flash_ps_load_rsp_t *pResp;

	pResp = gecko_cmd_flash_ps_load(key);

	if(pResp->result == 0)
	{
		memcpy(pValue, pResp->value.data, pResp->value.len);

		// sanity check: length of data stored in PS key must match the expected value
		if(size != pResp->value.len)
		{
			return(bg_err_unspecified);
		}
	}

	return(pResp->result);
}

First parameter is the PS key (from range 0x4000...0x407F). The second parameter is a pointer to the data object (it can be a variable of type float). Last parameter defines the size of the object. You can use the sizeof() operator when calling these.

 

Here's an example how to store and load a float

 

float F;


// store the value:
ps_save_object(0x4000, &F, sizeof(F));


// load the value of F from PS:

if(ps_load_object(0x4000, &F, sizeof(F)) == 0)
{
   // value was loaded OK
}
else
{
  // value not OK (either not stored in PS or length of the data does not match the expected value)
}
    	

Posts: 2,582
Registered: ‎09-01-2015

Re: Thunderboard sense non volatile memory

I made a knowledgebase article based on the sample code shown above, here's a direct link:

http://community.silabs.com/t5/Bluetooth-Wi-Fi-Knowledge-Base/How-to-save-float-values-or-any-arbitr...