cancel
Showing results for 
Search instead for 
Did you mean: 

ModusToolbox Blog

Moderator
Moderator

How to generate BD address randomly and save in flash for CYW20xx devices?

There are different options to set BD address in the make file of SDK. However, in this method, customer have to manually set the BD address for each chip download. As we provide random number generator APIs, customers can make use of those and generate a BD address inside the application itself and save it to NVRAM. Below is an example code snippet for the same.

Use this code in BTM_ENABLED_EVT before starting any other BLE activities.

uint8_t bda[8];

#define NVRAM_ID_RANDOM_ADDRESS              ( WICED_NVRAM_VSID_START + x ) // where x depends on other custom NVRAM data to be saved. If nothing else, x can be 0

case BTM_ENABLED_EVT:

if (wiced_hal_read_nvram(NVRAM_ID_RANDOM_ADDRESS, 6, bda, &result) != 6)

                         {

                             for (int i = 0; i < 2000; i++)

                             {

                                   // random number generator may need to warm up. Wait for some seconds.

                                 //wiced_rtos_delay_microseconds(2000); // add this if the chip supports rtos delay                                     //function

                                 wiced_hal_wdog_restart();

                             }

                             *(uint32_t*)&bda[0] = wiced_hal_rand_gen_num();

                                                       *(uint32_t*)&bda[4] = wiced_hal_rand_gen_num();

    if (wiced_hal_write_nvram(NVRAM_ID_RANDOM_ADDRESS, 6, bda, &result) != 6)

                             {

                                 WICED_BT_TRACE("FAILED to SAVE BDADDR", event );

                             }

                         }

                         wiced_bt_set_local_bdaddr(bda, BLE_ADDR_PUBLIC);

0 Likes