Tuesday, May 31, 2022

How to use custom and increment token in Silicon Labs GSDK 4.x and EmberZnet 7.x

The following steps 2-4 show you how to use custom basic token and steps 5-7 show you how to use custom increment token in Silicon Labs GSDK 4.x and EmberZnet 7.x.

1. Turn on "Enable Custom Tokens" in Token Manager.


 

2.Add the following codes to sl_custom_token_header.h.

        #define NVM3KEY_BASICTOKEN1 (NVM3KEY_DOMAIN_USER | 0x4100)
        #define BASICTOKEN1_DEFAULT  0
        #ifdef DEFINETYPES
        typedef uint16_t tokTypeBasicToken1;
        #endif
        #ifdef DEFINETOKENS
        DEFINE_BASIC_TOKEN(BASICTOKEN1,
                                                  tokTypeBasicToken1,
                                                  BASICTOKEN1_DEFAULT)
        #endif

3. Implement counter incrementing and store to custom token TOKEN_BASICTOKEN1 when button is pressed in app.c. Add related code in sl_button_on_change.

void sl_button_on_change(const sl_button_t *handle)
{
  uint16_t cnt;
  if (sl_button_get_state(handle) == SL_SIMPLE_BUTTON_RELEASED) {
      sl_token_get_data(NVM3KEY_BASICTOKEN1, 0x7f, &cnt, sizeof(cnt));
      cnt++;
      sl_token_set_data(NVM3KEY_BASICTOKEN1, 0x7f, &cnt, sizeof(cnt));
  }
}

4. Build and download application into EFR32 to test counter read and set with increment in custom token.
 

5. Add the following codes to sl_custom_token_header.h.         

    #define NVM3KEY_COUNTERTOKEN1 (NVM3KEY_DOMAIN_USER | 0x4104)
    #define COUNTERTOKEN1_DEFAULT 0
    #ifdef DEFINETYPES
        typedef uint32_t tokTypeCounterToken1;
    #endif
    #ifdef DEFINETOKENS
        DEFINE_COUNTER_TOKEN(COUNTERTOKEN1,
                                                         tokTypeCounterToken1,
                                                         COUNTERTOKEN1_DEFAULT)
    #endif

6. Implement counter incrementing using increment token by adding related code in sl_button_on_change.

    void sl_button_on_change(const sl_button_t *handle)    
    {
      uint32_t buf;
      if (sl_button_get_state(handle) == SL_SIMPLE_BUTTON_RELEASED) {
          sl_token_get_data(NVM3KEY_COUNTERTOKEN1, 0x7f, &buf, sizeof(buf));
          sl_token_increment_counter(NVM3KEY_COUNTERTOKEN1);
          sl_token_get_data(NVM3KEY_COUNTERTOKEN1, 0x7f, &buf, sizeof(buf));
      }
    }
 7. Build and download application into EFR32 to test increment counter token when button is pressed

No comments:

Post a Comment