This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

LP-EM-CC2745R10-Q1: The MCU does not operate (exception handler) when executing TRNG_getRandomBytes().

Part Number: LP-EM-CC2745R10-Q1
Other Parts Discussed in Thread: SYSCONFIG

Tool/software:

Hi,

While developing a project using the CC2745 LaunchPad and the simple_ble example, I created a function using TRNG.h to obtain random bytes of the desired length (lub_size input parameter) into the laub_Rnd array. The implemented code is as follows (I added TRNG manually in sysconfig, Name: CONFIG_TRNG_0, and didn't change any value of TRNG syscfg).

#define RANDOM_BYTE_SIZE 4
// Added TRNG_init() immediately after Board_init() inside the main() function in main_freertos.c.
uint8 BLE_F_IF_Get_RandomByte(uint8* laub_Rnd, uint8 lub_size)
{
    TRNG_Handle handle;
    TRNG_Params params;
    int_fast16_t result;
    uint8 randomBytesArray[RANDOM_BYTE_SIZE] = {0};
    uint8 lub_byte_cnt = 0;
    uint8 lub_size_cnt = lub_size;
    uint8 lub_copy_len;

    TRNG_Params_init(&params);
    handle = TRNG_open(CONFIG_TRNG_0, &params);
    if (!handle) {
        // Handle error
        return BLE_D_FALSE;
    }
    while(lub_size_cnt > 0)
    {
        result = TRNG_getRandomBytes(handle, randomBytesArray, RANDOM_BYTE_SIZE);
        if (result != TRNG_STATUS_SUCCESS) {
            // Handle error
            TRNG_close(handle);
            return BLE_D_FALSE;
        }

        lub_copy_len = (lub_size_cnt >= RANDOM_BYTE_SIZE) ? RANDOM_BYTE_SIZE : lub_size_cnt;
        memcpy(&laub_Rnd[lub_byte_cnt], randomBytesArray, lub_copy_len);
        lub_byte_cnt += lub_copy_len;
        lub_size_cnt -= lub_copy_len;
    }
    
    TRNG_close(handle);
    return BLE_D_TRUE;
}

Even when trying TRNG_open(0, NULL) as shown in the example in TRNG.h, the result is the same.

#define RANDOM_BYTE_SIZE 4
// Added TRNG_init() immediately after Board_init() inside the main() function in main_freertos.c.
uint8 BLE_F_IF_Get_RandomByte(uint8* laub_Rnd, uint8 lub_size)
{
    TRNG_Handle handle;
    // TRNG_Params params;
    int_fast16_t result;
    uint8 randomBytesArray[RANDOM_BYTE_SIZE] = {0};
    uint8 lub_byte_cnt = 0;
    uint8 lub_size_cnt = lub_size;
    uint8 lub_copy_len;

    // TRNG_Params_init(&params);
    // handle = TRNG_open(CONFIG_TRNG_0, &params);
    handle = TRNG_open(0, NULL);
    if (!handle) {
        // Handle error
        return BLE_D_FALSE;
    }
    while(lub_size_cnt > 0)
    {
        result = TRNG_getRandomBytes(handle, randomBytesArray, RANDOM_BYTE_SIZE);
        if (result != TRNG_STATUS_SUCCESS) {
            // Handle error
            TRNG_close(handle);
            return BLE_D_FALSE;
        }

        lub_copy_len = (lub_size_cnt >= RANDOM_BYTE_SIZE) ? RANDOM_BYTE_SIZE : lub_size_cnt;
        memcpy(&laub_Rnd[lub_byte_cnt], randomBytesArray, lub_copy_len);
        lub_byte_cnt += lub_copy_len;
        lub_size_cnt -= lub_copy_len;
    }
    
    TRNG_close(handle);
    return BLE_D_TRUE;
}

When executing this code, after calling the TRNG_getRandomBytes function, the LaunchPad falls into Exception_handlerSpin(). Upon debugging, I found that it falls into an exception at SemaphoreP_pend() during the execution of the HSMLPF3_waitForResult function.

I am curious if I missed any additional settings or if there was an error in using the API.

Could anyone help resolve this issue or provide guidance on correctly using TRNG_getRandomBytes() in the simple_ble example?

(SDK version: 8.40.2.01, SysConfig: 1.22.0)

Thank you.

  • Hello Jaeeun, 

    I will look into this question and provide a response by Thursday next week (06/12). Apologies for the delay. 

    Thanks, 
    Isaac

  • Hi Issac,

    I am leaving this here in case it helps with problem analysis.

    The above function was called within the app_main() of the basic_ble example.

    Is there any additional information that needs to be provided?

  • Can you try and call TRNG_init() API before calling TRNG_Params_init() API? The TRNG_init() API is responsible for initializing all of the RTOS-related metadata such as semaphores, interrupts and ISR callbacks. 

  • Hi,

    As you suggested, I changed the code to call TRNG_init() right before TRNG_Params_init(&params) instead of immediately after Board_init() in main_freertos.c.

    /*******************************************************
    * Changes in app_main.c of the CC2745 basic_ble example
    * No other parts were changed.
    ********************************************************/
    
    #include <ti/drivers/TRNG.h>
    #include <string.h>
    
    #define RANDOM_BYTE_SIZE 4
    uint8 BLE_F_IF_Get_RandomByte(uint8_t* laub_Rnd, uint8_t lub_size)
    {
        TRNG_Handle handle;
        TRNG_Params params;
        int_fast16_t result;
        uint8_t randomBytesArray[RANDOM_BYTE_SIZE] = {0};
        uint8_t lub_byte_cnt = 0;
        uint8_t lub_size_cnt = lub_size;
        uint8_t lub_copy_len;
    
        TRNG_init();
        TRNG_Params_init(&params);
        handle = TRNG_open(CONFIG_TRNG_0, &params);
        // handle = TRNG_open(CONFIG_TRNG_0, NULL);
        if (!handle) {
            // Handle error
            return 0;
        }
        while(lub_size_cnt > 0)
        {
            result = TRNG_getRandomBytes(handle, randomBytesArray, RANDOM_BYTE_SIZE);
            if (result != TRNG_STATUS_SUCCESS) {
                // Handle error
                TRNG_close(handle);
                return 0;
            }
    
            lub_copy_len = (lub_size_cnt >= RANDOM_BYTE_SIZE) ? RANDOM_BYTE_SIZE : lub_size_cnt;
            memcpy(&laub_Rnd[lub_byte_cnt], randomBytesArray, lub_copy_len);
            lub_byte_cnt += lub_copy_len;
            lub_size_cnt -= lub_copy_len;
        }
        
        TRNG_close(handle);
        return 0;
    }
    
    void appMain(void)
    {
        uint8_t laub_Rnd[16] = {0};
        (void)BLE_F_IF_Get_RandomByte(laub_Rnd, 16);
    
        // Call the BLEAppUtil module init function
        BLEAppUtil_init(&criticalErrorHandler, &App_StackInitDoneHandler,
                        &appMainParams, &appMainPeriCentParams);
    }
    
    

    But the LaunchPad still falls into Exception_handlerSpin().

    The same applies to the code below.

     

    /*******************************************************
    * Changes in app_main.c of the CC2745 basic_ble example
    * No other parts were changed.
    ********************************************************/
    #include <ti/drivers/TRNG.h>
    #include <string.h>
    
    void Get_RandomByte(uint8_t* laub_Rnd, uint8_t lub_size)
    {
        TRNG_Handle handle;
        int_fast16_t result;
        uint8_t randomBytesArray[16] = {0};
        TRNG_init();
    
        handle = TRNG_open(0, NULL);
        if (!handle) {
            // Handle error
            while(1);
        }
        result = TRNG_getRandomBytes(handle, randomBytesArray, 16);
        if (result != TRNG_STATUS_SUCCESS) {
            // Handle error
            while(1);
        }
        memcpy(&laub_Rnd[0], randomBytesArray, 16);
        TRNG_close(handle);
    }
    
    void appMain(void)
    {
        uint8_t laub_Rnd[16] = {0};
        
        Get_RandomByte(laub_Rnd,16);
    
        // Call the BLEAppUtil module init function
        BLEAppUtil_init(&criticalErrorHandler, &App_StackInitDoneHandler,
                        &appMainParams, &appMainPeriCentParams);
    }

    I wonder if it works well on your LaunchPad.

  • The complete code for app_main.c that I used is as follows.

    /******************************************************************************
    
    @file  app_main.c
    
    @brief This file contains the application main functionality
    
    Group: WCS, BTS
    Target Device: cc23xx
    
    ******************************************************************************
    
     Copyright (c) 2022-2024, Texas Instruments Incorporated
     All rights reserved.
    
     Redistribution and use in source and binary forms, with or without
     modification, are permitted provided that the following conditions
     are met:
    
     *  Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.
    
     *  Redistributions in binary form must reproduce the above copyright
        notice, this list of conditions and the following disclaimer in the
        documentation and/or other materials provided with the distribution.
    
     *  Neither the name of Texas Instruments Incorporated nor the names of
        its contributors may be used to endorse or promote products derived
        from this software without specific prior written permission.
    
     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    
    ******************************************************************************
    
    
    *****************************************************************************/
    
    //*****************************************************************************
    //! Includes
    //*****************************************************************************
    #include "ti_ble_config.h"
    #include <ti/bleapp/ble_app_util/inc/bleapputil_api.h>
    #include <ti/bleapp/menu_module/menu_module.h>
    #include <app_main.h>
    #include <ti/drivers/TRNG.h>
    #include <string.h>
    //*****************************************************************************
    //! Defines
    //*****************************************************************************
    
    //*****************************************************************************
    //! Globals
    //*****************************************************************************
    
    // Parameters that should be given as input to the BLEAppUtil_init function
    BLEAppUtil_GeneralParams_t appMainParams =
    {
        .taskPriority = 1,
        .taskStackSize = 1024,
        .profileRole = (BLEAppUtil_Profile_Roles_e)(HOST_CONFIG),
        .addressMode = DEFAULT_ADDRESS_MODE,
        .deviceNameAtt = attDeviceName,
        .pDeviceRandomAddress = pRandomAddress,
    };
    
    #if defined( HOST_CONFIG ) && ( HOST_CONFIG & ( PERIPHERAL_CFG | CENTRAL_CFG ) )
    BLEAppUtil_PeriCentParams_t appMainPeriCentParams =
    {
    #if defined( HOST_CONFIG ) && ( HOST_CONFIG & ( PERIPHERAL_CFG ) )
     .connParamUpdateDecision = DEFAULT_PARAM_UPDATE_REQ_DECISION,
    #endif //#if defined( HOST_CONFIG ) && ( HOST_CONFIG & ( PERIPHERAL_CFG ) )
     .gapBondParams = &gapBondParams
    };
    #else //observer || broadcaster
    BLEAppUtil_PeriCentParams_t appMainPeriCentParams;
    #endif //#if defined( HOST_CONFIG ) && ( HOST_CONFIG & ( PERIPHERAL_CFG | CENTRAL_CFG ) )
    
    
    
    //*****************************************************************************
    //! Functions
    //*****************************************************************************
    
    /*********************************************************************
     * @fn      criticalErrorHandler
     *
     * @brief   Application task entry point
     *
     * @return  none
     */
    void criticalErrorHandler(int32 errorCode , void* pInfo)
    {
    //    trace();
    //
    //#ifdef DEBUG_ERR_HANDLE
    //
    //    while (1);
    //#else
    //    SystemReset();
    //#endif
    
    }
    
    /*********************************************************************
     * @fn      App_StackInitDone
     *
     * @brief   This function will be called when the BLE stack init is
     *          done.
     *          It should call the applications modules start functions.
     *
     * @return  none
     */
    void App_StackInitDoneHandler(gapDeviceInitDoneEvent_t *deviceInitDoneData)
    {
        bStatus_t status = SUCCESS;
    
        // Menu
        Menu_start();
    
        // Print the device ID address
        MenuModule_printf(APP_MENU_DEVICE_ADDRESS, 0, "BLE ID Address: "
                          MENU_MODULE_COLOR_BOLD MENU_MODULE_COLOR_GREEN "%s" MENU_MODULE_COLOR_RESET,
                          BLEAppUtil_convertBdAddr2Str(deviceInitDoneData->devAddr));
    
        if ( appMainParams.addressMode > ADDRMODE_RANDOM)
        {
          // Print the RP address
            MenuModule_printf(APP_MENU_DEVICE_RP_ADDRESS, 0,
                         "BLE RP Address: "
                         MENU_MODULE_COLOR_BOLD MENU_MODULE_COLOR_GREEN "%s" MENU_MODULE_COLOR_RESET,
                         BLEAppUtil_convertBdAddr2Str(GAP_GetDevAddress(FALSE)));
        }
    
    #if defined( HOST_CONFIG ) && ( HOST_CONFIG & ( PERIPHERAL_CFG | CENTRAL_CFG ) )
        status = DevInfo_start();
        if(status != SUCCESS)
        {
            // TODO: Call Error Handler
        }
        status = SimpleGatt_start();
        if(status != SUCCESS)
        {
            // TODO: Call Error Handler
        }
    #endif
    
    #if defined( HOST_CONFIG ) && ( HOST_CONFIG & ( PERIPHERAL_CFG | CENTRAL_CFG ))  &&  defined(OAD_CFG)
        status =  OAD_start();
        if(status != SUCCESS)
        {
            // TODO: Call Error Handler
        }
    #endif
    
    #if defined( HOST_CONFIG ) && ( HOST_CONFIG & ( PERIPHERAL_CFG ) )
        // Any device that accepts the establishment of a link using
        // any of the connection establishment procedures referred to
        // as being in the Peripheral role.
        // A device operating in the Peripheral role will be in the
        // Peripheral role in the Link Layer Connection state.
        status = Peripheral_start();
        if(status != SUCCESS)
        {
            // TODO: Call Error Handler
        }
    #endif
    
    #if defined( HOST_CONFIG ) && ( HOST_CONFIG & ( BROADCASTER_CFG ) )
        // A device operating in the Broadcaster role is a device that
        // sends advertising events or periodic advertising events
        status = Broadcaster_start();
        if(status != SUCCESS)
        {
            // TODO: Call Error Handler
        }
    #endif
    
    #if defined( HOST_CONFIG ) && ( HOST_CONFIG & ( CENTRAL_CFG ) )
        // A device that supports the Central role initiates the establishment
        // of an active physical link. A device operating in the Central role will
        // be in the Central role in the Link Layer Connection state.
        // A device operating in the Central role is referred to as a Central.
        status = Central_start();
        if(status != SUCCESS)
        {
            // TODO: Call Error Handler
        }
    #endif
    
    #if defined( HOST_CONFIG ) && ( HOST_CONFIG & ( OBSERVER_CFG ) )
        // A device operating in the Observer role is a device that
        // receives advertising events or periodic advertising events
        status = Observer_start();
        if(status != SUCCESS)
        {
            // TODO: Call Error Handler
        }
    #endif
    
    #if defined( HOST_CONFIG ) && ( HOST_CONFIG & ( PERIPHERAL_CFG | CENTRAL_CFG ) )
        status = Connection_start();
        if(status != SUCCESS)
        {
            // TODO: Call Error Handler
        }
        status = Pairing_start();
        if(status != SUCCESS)
        {
            // TODO: Call Error Handler
        }
        status = Data_start();
        if(status != SUCCESS)
        {
            // TODO: Call Error Handler
        }
    
    #if defined (BLE_V41_FEATURES) && (BLE_V41_FEATURES & L2CAP_COC_CFG)
        /* L2CAP COC Init */
        status = L2CAPCOC_start();
        if ( status != SUCCESS )
        {
        // TODO: Call Error Handler
        }
    #endif //(BLE_V41_FEATURES) && (BLE_V41_FEATURES & L2CAP_COC_CFG)
    
    #endif
    }
    
    #define RANDOM_BYTE_SIZE 4
    uint8_t BLE_F_IF_Get_RandomByte(uint8_t* laub_Rnd, uint8_t lub_size)
    {
        TRNG_Handle handle;
        TRNG_Params params;
        int_fast16_t result;
        uint8_t randomBytesArray[RANDOM_BYTE_SIZE] = {0};
        uint8_t lub_byte_cnt = 0;
        uint8_t lub_size_cnt = lub_size;
        uint8_t lub_copy_len;
    
        TRNG_init();
        TRNG_Params_init(&params);
        handle = TRNG_open(CONFIG_TRNG_0, &params);
        // handle = TRNG_open(CONFIG_TRNG_0, NULL);
        if (!handle) {
            // Handle error
            return 0;
        }
        while(lub_size_cnt > 0)
        {
            result = TRNG_getRandomBytes(handle, randomBytesArray, RANDOM_BYTE_SIZE);
            if (result != TRNG_STATUS_SUCCESS) {
                // Handle error
                TRNG_close(handle);
                return 0;
            }
    
            lub_copy_len = (lub_size_cnt >= RANDOM_BYTE_SIZE) ? RANDOM_BYTE_SIZE : lub_size_cnt;
            memcpy(&laub_Rnd[lub_byte_cnt], randomBytesArray, lub_copy_len);
            lub_byte_cnt += lub_copy_len;
            lub_size_cnt -= lub_copy_len;
        }
        
        TRNG_close(handle);
        return 0;
    }
    
    void Get_RandomByte(uint8_t* laub_Rnd, uint8_t lub_size)
    {
        TRNG_Handle handle;
        int_fast16_t result;
        uint8_t randomBytesArray[16] = {0};
        TRNG_init();
    
        handle = TRNG_open(0, NULL);
        if (!handle) {
            // Handle error
            while(1);
        }
        result = TRNG_getRandomBytes(handle, randomBytesArray, 16);
        if (result != TRNG_STATUS_SUCCESS) {
            // Handle error
            while(1);
        }
        memcpy(&laub_Rnd[0], randomBytesArray, 16);
        TRNG_close(handle);
    }
    
    
    /*********************************************************************
     * @fn      appMain
     *
     * @brief   Application main function
     *
     * @return  none
     */
    void appMain(void)
    {
        uint8_t laub_Rnd[16] = {0};
        
        (void)BLE_F_IF_Get_RandomByte(laub_Rnd, 16);
        // Get_RandomByte(laub_Rnd,16);
    
        // Call the BLEAppUtil module init function
        BLEAppUtil_init(&criticalErrorHandler, &App_StackInitDoneHandler,
                        &appMainParams, &appMainPeriCentParams);
    }
    

  • Yes. I am able to reproduce the issue on my side. When I use the empty example project and port your code there, I do not see the issue. Now the hard fault is happening when we are trying to pend on an operation semaphore since we are in blocking mode. I did give it a try with the RNG diver and same luck. So my initial assessment is that there is some sort of conflict between the BLE stack and the crypto SW architecture in terms of interrupt priority. Hoping o provide more details and a solution by the end of the week.

    Thank you.

  • Hi,

    You mentioned last week that you would analyze it. Has the analysis been done? I'm curious about the progress.

    I would appreciate it if you could share the progress.

  • I apologize for the delay. The problem here is that we are trying to leverage RTOS features before FreeRTOS is fully setup. appMain() is being called before vTaskStartScheduler() which is responsible for acknowledging IRQs and firing up the ISR. When you are calling the TRNG driver with default params, the default return behavior is blocking which leverages the ISR but since the RTOS hasn't been initialized fully, the IRQ gets lost, never handled, and as a result we get an exception. The fix here is to move the code where you're calling your function to App_stackInitDoneHandler() fxn. This callback function gets triggered as soon as the scheduler is initialized and the RTOS is ready to handle IRQs. 

    Attached code moves the block of code previously placed under appMain() to line 187 under App_StackInitDoneHandler(). I tried this and it works. 

    Alternatively, you can keep the code where it is but change the return behavior from blocking to polling. The attached code runs both options.

    /******************************************************************************
    
    @file  app_main.c
    
    @brief This file contains the application main functionality
    
    Group: WCS, BTS
    Target Device: cc23xx
    
    ******************************************************************************
    
     Copyright (c) 2022-2024, Texas Instruments Incorporated
     All rights reserved.
    
     Redistribution and use in source and binary forms, with or without
     modification, are permitted provided that the following conditions
     are met:
    
     *  Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.
    
     *  Redistributions in binary form must reproduce the above copyright
        notice, this list of conditions and the following disclaimer in the
        documentation and/or other materials provided with the distribution.
    
     *  Neither the name of Texas Instruments Incorporated nor the names of
        its contributors may be used to endorse or promote products derived
        from this software without specific prior written permission.
    
     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    
    ******************************************************************************
    
    
    *****************************************************************************/
    
    //*****************************************************************************
    //! Includes
    //*****************************************************************************
    #include "ti_ble_config.h"
    #include <ti/bleapp/ble_app_util/inc/bleapputil_api.h>
    #include <ti/bleapp/menu_module/menu_module.h>
    #include <app_main.h>
    #include <ti/drivers/TRNG.h>
    #include <ti/drivers/RNG.h>
    #include <string.h>
    
    //*****************************************************************************
    //! Defines
    //*****************************************************************************
    
    //*****************************************************************************
    //! Globals
    //*****************************************************************************
    
    // Parameters that should be given as input to the BLEAppUtil_init function
    BLEAppUtil_GeneralParams_t appMainParams =
    {
        .taskPriority = 1,
        .taskStackSize = 1024,
        .profileRole = (BLEAppUtil_Profile_Roles_e)(HOST_CONFIG),
        .addressMode = DEFAULT_ADDRESS_MODE,
        .deviceNameAtt = attDeviceName,
        .pDeviceRandomAddress = pRandomAddress,
    };
    
    #if defined( HOST_CONFIG ) && ( HOST_CONFIG & ( PERIPHERAL_CFG | CENTRAL_CFG ) )
    BLEAppUtil_PeriCentParams_t appMainPeriCentParams =
    {
    #if defined( HOST_CONFIG ) && ( HOST_CONFIG & ( PERIPHERAL_CFG ) )
     .connParamUpdateDecision = DEFAULT_PARAM_UPDATE_REQ_DECISION,
    #endif //#if defined( HOST_CONFIG ) && ( HOST_CONFIG & ( PERIPHERAL_CFG ) )
     .gapBondParams = &gapBondParams
    };
    #else //observer || broadcaster
    BLEAppUtil_PeriCentParams_t appMainPeriCentParams;
    #endif //#if defined( HOST_CONFIG ) && ( HOST_CONFIG & ( PERIPHERAL_CFG | CENTRAL_CFG ) )
    
    #define RANDOM_BYTE_SIZE 4
    uint8_t BLE_F_IF_Get_RandomByte(uint8_t* laub_Rnd, uint8_t lub_size)
    {
        TRNG_Handle handle;
        TRNG_Params params;
        int_fast16_t result;
        uint8_t randomBytesArray[RANDOM_BYTE_SIZE] = {0};
        uint8_t lub_byte_cnt = 0;
        uint8_t lub_size_cnt = lub_size;
        uint8_t lub_copy_len;
    
        TRNG_init();
        TRNG_Params_init(&params);
    //    params.returnBehavior = TRNG_RETURN_BEHAVIOR_CALLBACK;
    //    params.randomBytesCallbackFxn = randomBytesCallback1;
    //    params.cryptoKeyCallbackFxn = cryptoKeyCallback;
        handle = TRNG_open(CONFIG_TRNG_0, &params);
        // handle = TRNG_open(CONFIG_TRNG_0, NULL);
        if (!handle) {
            // Handle error
            return 0;
        }
        while(lub_size_cnt > 0)
        {
            result = TRNG_getRandomBytes(handle, randomBytesArray, RANDOM_BYTE_SIZE);
            if (result != TRNG_STATUS_SUCCESS) {
                // Handle error
                TRNG_close(handle);
                return 0;
            }
    
            lub_copy_len = (lub_size_cnt >= RANDOM_BYTE_SIZE) ? RANDOM_BYTE_SIZE : lub_size_cnt;
            memcpy(&laub_Rnd[lub_byte_cnt], randomBytesArray, lub_copy_len);
            lub_byte_cnt += lub_copy_len;
            lub_size_cnt -= lub_copy_len;
        }
    
        TRNG_close(handle);
        return 0;
    }
    
    uint8_t BLE_F_IF_Get_RandomByte_POLLING(uint8_t* laub_Rnd, uint8_t lub_size)
    {
        TRNG_Handle handle;
        TRNG_Params params;
        int_fast16_t result;
        uint8_t randomBytesArray[RANDOM_BYTE_SIZE] = {0};
        uint8_t lub_byte_cnt = 0;
        uint8_t lub_size_cnt = lub_size;
        uint8_t lub_copy_len;
    
        TRNG_init();
        TRNG_Params_init(&params);
        params.returnBehavior = TRNG_RETURN_BEHAVIOR_POLLING;
    //    params.returnBehavior = TRNG_RETURN_BEHAVIOR_CALLBACK;
    //    params.randomBytesCallbackFxn = randomBytesCallback1;
    //    params.cryptoKeyCallbackFxn = cryptoKeyCallback;
        handle = TRNG_open(CONFIG_TRNG_0, &params);
        // handle = TRNG_open(CONFIG_TRNG_0, NULL);
        if (!handle) {
            // Handle error
            return 0;
        }
        while(lub_size_cnt > 0)
        {
            result = TRNG_getRandomBytes(handle, randomBytesArray, RANDOM_BYTE_SIZE);
            if (result != TRNG_STATUS_SUCCESS) {
                // Handle error
                TRNG_close(handle);
                return 0;
            }
    
            lub_copy_len = (lub_size_cnt >= RANDOM_BYTE_SIZE) ? RANDOM_BYTE_SIZE : lub_size_cnt;
            memcpy(&laub_Rnd[lub_byte_cnt], randomBytesArray, lub_copy_len);
            lub_byte_cnt += lub_copy_len;
            lub_size_cnt -= lub_copy_len;
        }
    
        TRNG_close(handle);
        return 0;
    }
    
    
    
    //*****************************************************************************
    //! Functions
    //*****************************************************************************
    
    /*********************************************************************
     * @fn      criticalErrorHandler
     *
     * @brief   Application task entry point
     *
     * @return  none
     */
    void criticalErrorHandler(int32 errorCode , void* pInfo)
    {
    //    trace();
    //
    //#ifdef DEBUG_ERR_HANDLE
    //
    //    while (1);
    //#else
    //    SystemReset();
    //#endif
    
    }
    
    /*********************************************************************
     * @fn      App_StackInitDone
     *
     * @brief   This function will be called when the BLE stack init is
     *          done.
     *          It should call the applications modules start functions.
     *
     * @return  none
     */
    void App_StackInitDoneHandler(gapDeviceInitDoneEvent_t *deviceInitDoneData)
    {
        bStatus_t status = SUCCESS;
    
        // Menu
        Menu_start();
    
        // Print the device ID address
        MenuModule_printf(APP_MENU_DEVICE_ADDRESS, 0, "BLE ID Address: "
                          MENU_MODULE_COLOR_BOLD MENU_MODULE_COLOR_GREEN "%s" MENU_MODULE_COLOR_RESET,
                          BLEAppUtil_convertBdAddr2Str(deviceInitDoneData->devAddr));
    
        if ( appMainParams.addressMode > ADDRMODE_RANDOM)
        {
          // Print the RP address
            MenuModule_printf(APP_MENU_DEVICE_RP_ADDRESS, 0,
                         "BLE RP Address: "
                         MENU_MODULE_COLOR_BOLD MENU_MODULE_COLOR_GREEN "%s" MENU_MODULE_COLOR_RESET,
                         BLEAppUtil_convertBdAddr2Str(GAP_GetDevAddress(FALSE)));
        }
    
        // put the code here.
        uint8_t laub_Rnd[16] = {0};
    
        (void)BLE_F_IF_Get_RandomByte(laub_Rnd, 16);
    
    //    BLE_F_IF_RNG_Get_RandomByte(laub_Rnd, 16);
    
    #if defined( HOST_CONFIG ) && ( HOST_CONFIG & ( PERIPHERAL_CFG | CENTRAL_CFG ) )
        status = DevInfo_start();
        if(status != SUCCESS)
        {
            // TODO: Call Error Handler
        }
        status = SimpleGatt_start();
        if(status != SUCCESS)
        {
            // TODO: Call Error Handler
        }
    #endif
    
    #if defined( HOST_CONFIG ) && ( HOST_CONFIG & ( PERIPHERAL_CFG | CENTRAL_CFG ))  &&  defined(OAD_CFG)
        status =  OAD_start();
        if(status != SUCCESS)
        {
            // TODO: Call Error Handler
        }
    #endif
    
    #if defined( HOST_CONFIG ) && ( HOST_CONFIG & ( PERIPHERAL_CFG ) )
        // Any device that accepts the establishment of a link using
        // any of the connection establishment procedures referred to
        // as being in the Peripheral role.
        // A device operating in the Peripheral role will be in the
        // Peripheral role in the Link Layer Connection state.
        status = Peripheral_start();
        if(status != SUCCESS)
        {
            // TODO: Call Error Handler
        }
    #endif
    
    #if defined( HOST_CONFIG ) && ( HOST_CONFIG & ( BROADCASTER_CFG ) )
        // A device operating in the Broadcaster role is a device that
        // sends advertising events or periodic advertising events
        status = Broadcaster_start();
        if(status != SUCCESS)
        {
            // TODO: Call Error Handler
        }
    #endif
    
    #if defined( HOST_CONFIG ) && ( HOST_CONFIG & ( CENTRAL_CFG ) )
        // A device that supports the Central role initiates the establishment
        // of an active physical link. A device operating in the Central role will
        // be in the Central role in the Link Layer Connection state.
        // A device operating in the Central role is referred to as a Central.
        status = Central_start();
        if(status != SUCCESS)
        {
            // TODO: Call Error Handler
        }
    #endif
    
    #if defined( HOST_CONFIG ) && ( HOST_CONFIG & ( OBSERVER_CFG ) )
        // A device operating in the Observer role is a device that
        // receives advertising events or periodic advertising events
        status = Observer_start();
        if(status != SUCCESS)
        {
            // TODO: Call Error Handler
        }
    #endif
    
    #if defined( HOST_CONFIG ) && ( HOST_CONFIG & ( PERIPHERAL_CFG | CENTRAL_CFG ) )
        status = Connection_start();
        if(status != SUCCESS)
        {
            // TODO: Call Error Handler
        }
        status = Pairing_start();
        if(status != SUCCESS)
        {
            // TODO: Call Error Handler
        }
        status = Data_start();
        if(status != SUCCESS)
        {
            // TODO: Call Error Handler
        }
    
    #if defined (BLE_V41_FEATURES) && (BLE_V41_FEATURES & L2CAP_COC_CFG)
        /* L2CAP COC Init */
        status = L2CAPCOC_start();
        if ( status != SUCCESS )
        {
        // TODO: Call Error Handler
        }
    #endif //(BLE_V41_FEATURES) && (BLE_V41_FEATURES & L2CAP_COC_CFG)
    
    #endif
    }
    
    static void cryptoKeyCallback(TRNG_Handle cbHandle, int_fast16_t cbStatus, CryptoKey *entropyKey)
    {
        return;
    }
    
    static void randomBytesCallback1(TRNG_Handle cbHandle,
                                     int_fast16_t cbStatus,
                                     uint8_t *randomBytes,
                                     size_t randomBytesSize)
    {
        return;
    }
    
    uint8_t BLE_F_IF_RNG_Get_RandomByte(uint8_t* laub_Rnd, uint8_t lub_size)
    {
        RNG_Handle handle;
        RNG_Params params;
        int_fast16_t result;
        uint8_t randomBytesArray[RANDOM_BYTE_SIZE] = {0};
        uint8_t lub_byte_cnt = 0;
        uint8_t lub_size_cnt = lub_size;
        uint8_t lub_copy_len;
    
        RNG_init();
        RNG_Params_init(&params);
    //    params.returnBehavior = TRNG_RETURN_BEHAVIOR_CALLBACK;
    //    params.randomBytesCallbackFxn = randomBytesCallback1;
    //    params.cryptoKeyCallbackFxn = cryptoKeyCallback;
        handle = RNG_open(CONFIG_TRNG_0, &params);
        // handle = TRNG_open(CONFIG_TRNG_0, NULL);
        if (!handle) {
            // Handle error
            return 0;
        }
        while(lub_size_cnt > 0)
        {
            result = RNG_getRandomBits(handle, randomBytesArray, RANDOM_BYTE_SIZE);
            if (result != TRNG_STATUS_SUCCESS) {
                // Handle error
                RNG_close(handle);
                return 0;
            }
    
            lub_copy_len = (lub_size_cnt >= RANDOM_BYTE_SIZE) ? RANDOM_BYTE_SIZE : lub_size_cnt;
            memcpy(&laub_Rnd[lub_byte_cnt], randomBytesArray, lub_copy_len);
            lub_byte_cnt += lub_copy_len;
            lub_size_cnt -= lub_copy_len;
        }
    
        RNG_close(handle);
        return 0;
    }
    
    /*********************************************************************
     * @fn      appMain
     *
     * @brief   Application main function
     *
     * @return  none
     */
    void appMain(void)
    {
        // put the code here.
        uint8_t laub_Rnd[16] = {0};
    
        (void)BLE_F_IF_Get_RandomByte_POLLING(laub_Rnd, 16);
        // Call the BLEAppUtil module init function
        BLEAppUtil_init(&criticalErrorHandler, &App_StackInitDoneHandler,
                        &appMainParams, &appMainPeriCentParams);
    }
    

    Please let me know if this works for you.

  • Hi,

    The code you provided works well.

    Thank you for your response.

    Have a great day!