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

nRF52840 Eddystone connectable mode issue with FreeRTOS

We are using nRF52840 DK with SDK17, our aim is to achieve Eddystone packet advertisement and connectable mode using FreeRTOS. Implementation started with ble_app_blinky example, at this stage with Freertos, packet advertisement (Beaconing) working fine and we able to receive the Eddystone beacons in mobile application. But as soon as we try to establish a connection (put the device into connectable mode) we get the below error and the code crashes.

<debug> nrf_ble_lesc: Initialized nrf_crypto.
<debug> nrf_ble_lesc: Initialized nrf_ble_lesc.
<debug> nrf_ble_lesc: Generating ECC key pair
<info> app: Marker Application; built on Apr 22 2021 at 21:13:20 for nRF52840
<info> app: DevAddr C662550D DDD6F8DB
<error> app: ERROR 3735928559 [Unknown error code] at /home/../external/freertos/source/tasks.c:4796
PC at: 0x000357DB
<error> app: End of error report


It seems like as soon as connection thread starts, an interrupt is triggered but the firmware unable to handle and crashes.

We tried to fix we did few changes in "nrf_ble_es.c" ,


/**
 * Copyright (c) 2016 - 2020, Nordic Semiconductor ASA
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form, except as embedded into a Nordic
 *    Semiconductor ASA integrated circuit in a product or a software update for
 *    such product, 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.
 *
 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * 4. This software, with or without modification, must only be used with a
 *    Nordic Semiconductor ASA integrated circuit.
 *
 * 5. Any software provided in binary form under this license must not be reverse
 *    engineered, decompiled, modified and/or disassembled.
 *
 * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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.
 *
 */

#include <string.h>
#include "nrf_ble_es.h"
#include "app_error.h"
#include "fds.h"
#include "es_adv.h"
#include "es_battery_voltage.h"
#include "es_flash.h"
#include "es_gatts.h"
#include "es_security.h"
#include "es_slot.h"
#include "es_stopwatch.h"
#include "escs_defs.h"
#include "nrf_sdh_ble.h"
#include "nrf_sdh_freertos.h"
#include "nrf_delay.h"
#include "nrf_log.h"

static uint16_t                       m_conn_handle = BLE_CONN_HANDLE_INVALID;  //!< Connection handle.
static nrf_ble_escs_t                 m_ble_ecs;                                //!< Struct identifying the Eddystone Config Service.
static nrf_ble_es_evt_handler_t       m_evt_handler;                            //!< Event handler.

uint8_t m_adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET;                          //!< Advertising handle used to identify an advertising set.
//extern bool ble_adv_conn_mode;
/**@brief Function for invoking registered callback.
 *
 * @param[in] evt Event to issue to callback.
 */
static void handle_evt(nrf_ble_es_evt_t evt)
{
    if (m_evt_handler != NULL)
    {
        m_evt_handler(evt);
    }
}


/**@brief Function resetting MAC address. Will resume advertisement. */
static void new_address_set(void)
{

    ret_code_t     err_code;
    uint8_t        bytes_available;
    ble_gap_addr_t new_address;

    new_address.addr_type = BLE_GAP_ADDR_TYPE_PUBLIC;

    // Randomize the MAC address on every EID generation
    (void)sd_rand_application_bytes_available_get(&bytes_available);

    while (bytes_available < BLE_GAP_ADDR_LEN)
    {
        // wait for SD to acquire enough RNs
        (void)sd_rand_application_bytes_available_get(&bytes_available);
    }

    (void)sd_rand_application_vector_get(new_address.addr, BLE_GAP_ADDR_LEN);

    // Stop advertising to ensure that it is possible to change the address.
    (void)sd_ble_gap_adv_stop(m_adv_handle);

    do
    {
        err_code = sd_ble_gap_addr_set(&new_address);
    } while (err_code == NRF_ERROR_INVALID_STATE);

    APP_ERROR_CHECK(err_code);

    if (es_adv_remain_connectable_get())
    {
        es_adv_start_connectable_adv();
    }
    else
    {
        es_adv_start_non_connctable_adv();
    }
}


/**@brief Function updating MAC address if required.
 *
 * @param[in] demand_new_mac If 'true', mac address will be updated on next invocation when not connected.
 *                           If 'false', simply check if we have an outstanding demand for new MAC and update if not connected.
 */
static void check_and_update_mac_address(bool demand_new_mac)
{
    static bool reset_mac_address = false;

    if (demand_new_mac)
    {
        reset_mac_address = true;
    }

    // Not possible to update MAC address while in a connection
    if (m_conn_handle != BLE_CONN_HANDLE_INVALID)
    {
        return;
    }

    else if (reset_mac_address)
    {
        reset_mac_address = false;

        new_address_set();
    }
}


/**@brief Function to lock the beacon (change lock state characteristic to LOCKED)
 */
static void lock_beacon(void)
{
    m_ble_ecs.lock_state = NRF_BLE_ESCS_LOCK_STATE_LOCKED;
}


/**@brief Function for handling BLE event from the SoftDevice.
 *
 * @param[in] p_ble_evt Pointer to BLE event.
 */
static void on_ble_evt(ble_evt_t const * p_ble_evt)
{
    ret_code_t            err_code;
    es_flash_flags_t      flash_flag = {{0}};
    const es_slot_reg_t * p_reg      = es_slot_get_registry();

    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            m_conn_handle = p_ble_evt->evt.common_evt.conn_handle;
            *(m_ble_ecs.p_active_slot) = 0;
            break;

        case BLE_GAP_EVT_DISCONNECTED:
            m_conn_handle = BLE_CONN_HANDLE_INVALID;

            for (uint32_t i = 0; i < APP_MAX_ADV_SLOTS; ++i)
            {
                err_code = es_slot_write_to_flash(i);
                APP_ERROR_CHECK(err_code);

                flash_flag.slot_is_empty[i] = !p_reg->slots[i].configured;
            }

            err_code = es_flash_access_flags(&flash_flag, ES_FLASH_ACCESS_WRITE);
            APP_ERROR_CHECK(err_code);

            es_flash_beacon_config_t beacon_config;
            beacon_config.adv_interval       = es_adv_interval_get();
            beacon_config.remain_connectable = es_adv_remain_connectable_get();

            err_code = es_flash_access_beacon_config(&beacon_config, ES_FLASH_ACCESS_WRITE);
            APP_ERROR_CHECK(err_code);

            if (m_ble_ecs.lock_state == NRF_BLE_ESCS_LOCK_STATE_UNLOCKED)
            {
                lock_beacon();
            }

            check_and_update_mac_address(false);
            //ble_adv_conn_mode = false;
            break;

        default:
            // No implementation needed.
            break;
    }
}


/**@brief Callback function to receive messages from the security module
 *
 * @details Need to be passed in during es_security_init(). The security
 *          module will callback anytime a particular security process is completed
 *
 * @params[in]  slot_no     Index of the slot
 * @params[in]  msg_type    Message type corersponding to different security components
 */
static void nrf_ble_escs_security_cb(uint8_t slot_no, es_security_msg_t msg_type)
{
    nrf_ble_escs_eid_id_key_t      encrypted_id_key;
    nrf_ble_escs_public_ecdh_key_t pub_ecdh_key;

    ret_code_t               err_code;
    static ble_gatts_value_t value;

    switch (msg_type)
    {
        case ES_SECURITY_MSG_UNLOCKED:
            m_ble_ecs.lock_state = NRF_BLE_ESCS_LOCK_STATE_UNLOCKED;
            break;

        case ES_SECURITY_MSG_EID:
            es_slot_eid_ready(slot_no);
#ifdef MAC_RANDOMIZED
            check_and_update_mac_address(true);
#endif // MAC_RANDOMIZED
            break;

        case ES_SECURITY_MSG_IK:
            es_security_encrypted_eid_id_key_get(slot_no, (uint8_t *)encrypted_id_key.key);
            // Set the EID ID key in the slot so it can be exposed in the characteristic
            es_slot_encrypted_eid_id_key_set(slot_no, &encrypted_id_key);
            break;

        case ES_SECURITY_MSG_ECDH:
            es_security_pub_ecdh_get(slot_no, (uint8_t *)pub_ecdh_key.key);

            // Set the characteristic to the ECDH key value
            value.len     = sizeof(nrf_ble_escs_public_ecdh_key_t);
            value.offset  = 0;
            value.p_value = (uint8_t *)pub_ecdh_key.key;

            if (m_conn_handle != BLE_CONN_HANDLE_INVALID)
            {
                err_code = sd_ble_gatts_value_set(m_ble_ecs.conn_handle,
                                                  m_ble_ecs.pub_ecdh_key_handles.value_handle,
                                                  &value);
                if (err_code != NRF_SUCCESS)
                {
                    APP_ERROR_CHECK(err_code);
                }
            }
            break;

        case ES_SECURITY_MSG_STORE_TIME:
            // Every 24 hours any EID slots time is stored to flash to allow for power lock_state_handles
            // recovery. Only time needs to be stored, but just store the entire slot anyway for API simplicity.
            err_code = es_slot_write_to_flash(slot_no);
            APP_ERROR_CHECK(err_code);

            break;

        default:
            APP_ERROR_CHECK(NRF_ERROR_INVALID_PARAM); // Should never happen
            break;
    }
}


/**@brief Function for handling advertisement events from 'es_adv'.
 *
 * @param[in] evt Advertisement event to handle.
 */
static void adv_evt_handler(es_adv_evt_t evt)
{
    switch (evt)
    {
        case ES_ADV_EVT_NON_CONN_ADV:
            handle_evt(NRF_BLE_ES_EVT_ADVERTISEMENT_SENT);
            es_security_update_time();
            break;

        case ES_ADV_EVT_CONNECTABLE_ADV_STARTED:
            handle_evt(NRF_BLE_ES_EVT_CONNECTABLE_ADV_STARTED);
            break;

        case ES_ADV_EVT_CONNECTABLE_ADV_STOPPED:
            handle_evt(NRF_BLE_ES_EVT_CONNECTABLE_ADV_STOPPED);
            break;

        default:
            break;
    }
}


/**@brief Initialize the ECS with initial values for the characteristics and other necessary modules */
static void ble_escs_init(void)
{
    ret_code_t                 err_code;
    nrf_ble_escs_init_t        ecs_init;
    nrf_ble_escs_init_params_t init_params;
    int8_t                     tx_powers[ESCS_NUM_OF_SUPPORTED_TX_POWER] = ESCS_SUPPORTED_TX_POWER;

    /*Init the broadcast capabilities characteristic*/
    memset(&init_params.broadcast_cap, 0, sizeof(init_params.broadcast_cap));
    init_params.broadcast_cap.vers_byte            = ES_SPEC_VERSION_BYTE;
    init_params.broadcast_cap.max_supp_total_slots = APP_MAX_ADV_SLOTS;
    init_params.broadcast_cap.max_supp_eid_slots   = APP_MAX_EID_SLOTS;
    init_params.broadcast_cap.cap_bitfield         = ( (APP_IS_VARIABLE_ADV_SUPPORTED << ESCS_BROADCAST_VAR_ADV_SUPPORTED_Pos)
                                                   | (APP_IS_VARIABLE_TX_POWER_SUPPORTED << ESCS_BROADCAST_VAR_TX_POWER_SUPPORTED_Pos))
                                                   & (ESCS_BROADCAST_VAR_RFU_MASK);
    init_params.broadcast_cap.supp_frame_types     = ( (APP_IS_URL_SUPPORTED << ESCS_FRAME_TYPE_URL_SUPPORTED_Pos)
                                                   | (APP_IS_UID_SUPPORTED << ESCS_FRAME_TYPE_UID_SUPPORTED_Pos)
                                                   | (APP_IS_TLM_SUPPORTED << ESCS_FRAME_TYPE_TLM_SUPPORTED_Pos)
                                                   | (APP_IS_EID_SUPPORTED << ESCS_FRAME_TYPE_EID_SUPPORTED_Pos))
                                                   & (ESCS_FRAME_TYPE_RFU_MASK);
    memcpy(init_params.broadcast_cap.supp_radio_tx_power, tx_powers, ESCS_NUM_OF_SUPPORTED_TX_POWER);

    init_params.adv_interval    = APP_CFG_NON_CONN_ADV_INTERVAL_MS;
    init_params.adv_tx_pwr      = APP_CFG_DEFAULT_RADIO_TX_POWER;
    init_params.radio_tx_pwr    = 0x00;
    init_params.factory_reset   = 0;
    init_params.remain_connectable.r_is_non_connectable_supported = APP_IS_REMAIN_CONNECTABLE_SUPPORTED;

    // Initialize evt handlers and the service
    memset(&ecs_init, 0, sizeof(ecs_init));
    ecs_init.write_evt_handler = es_gatts_handle_write;
    ecs_init.read_evt_handler  = es_gatts_handle_read;
    ecs_init.p_init_vals       = &(init_params);

    err_code = nrf_ble_escs_init(&m_ble_ecs, &ecs_init);
    APP_ERROR_CHECK(err_code);

}


/**@brief Function for initializing 'es_adv' module. */
static void adv_init(void)
{
    ret_code_t               err_code;
    es_flash_beacon_config_t beacon_config;

    err_code = es_flash_access_beacon_config(&beacon_config, ES_FLASH_ACCESS_READ);

    if (err_code == FDS_ERR_NOT_FOUND)
    {
        beacon_config.adv_interval       = APP_CFG_NON_CONN_ADV_INTERVAL_MS;
        beacon_config.remain_connectable = false;
    }

    else
    {
        APP_ERROR_CHECK(err_code);
    }

    es_adv_init(m_ble_ecs.uuid_type,
                adv_evt_handler,
                beacon_config.adv_interval,
                beacon_config.remain_connectable,
                &m_adv_handle);
}


/**@brief Function for initializing es_slots module. */
static void adv_slots_init(void)
{
    uint8_t UID_Frame_Data[UID_FRAME_LENGTH] = UID_FRAME_DATA;
    //Slot 0 UID
    es_slot_t UID_adv_slot = {.slot_no                 = 0,
                                .radio_tx_pwr        = DEFAULT_FRAME_TX_POWER,
                                .adv_frame.type      = UID_FRAME_TYPE,
                                .adv_frame.length    = UID_FRAME_LENGTH,
                                .adv_custom_tx_power = false,
                                .configured          = true};

    memcpy(&UID_adv_slot.adv_frame.frame, UID_Frame_Data, UID_FRAME_LENGTH) ;

    es_slots_init(&UID_adv_slot);


}


void nrf_ble_es_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
{
    ret_code_t err_code;

    es_adv_on_ble_evt(p_ble_evt);
    err_code = nrf_ble_escs_on_ble_evt(&m_ble_ecs, p_ble_evt);
    APP_ERROR_CHECK(err_code);
    on_ble_evt(p_ble_evt);
    es_flash_on_ble_evt(p_ble_evt);
}

NRF_SDH_BLE_OBSERVER(m_es_observer, NRF_BLE_ES_BLE_OBSERVER_PRIO, nrf_ble_es_on_ble_evt, NULL);


void nrf_ble_es_on_start_connectable_advertising(void)
{
    es_adv_start_connectable_adv();
}

void nrf_ble_es_on_start_connectable_adv(void * p_context)
{
    es_adv_start_connectable_adv();
}

void nrf_ble_es_on_start_non_connectable_adv(void * p_context)
{
    es_adv_start_non_connctable_adv();
}

void nrf_ble_es_init(nrf_ble_es_evt_handler_t evt_handler)
{
    ret_code_t err_code;
    uint8_t p_context = NULL;
    m_evt_handler = evt_handler;
    m_conn_handle = BLE_CONN_HANDLE_INVALID;

    es_stopwatch_init();

    err_code = es_gatts_init(&m_ble_ecs);
    APP_ERROR_CHECK(err_code);
    

    err_code = es_flash_init();
    APP_ERROR_CHECK(err_code);

    while (es_flash_num_pending_ops() > 0)
    {
         // Busy wait while initialization of FDS module completes
    }
    
    err_code = es_security_init(nrf_ble_escs_security_cb);
    APP_ERROR_CHECK(err_code);

    es_adv_timers_init();
    ble_escs_init();
    adv_slots_init();
    adv_init();
    es_adv_remain_connectable_set(true);
    nrf_sdh_freertos_init(nrf_ble_es_on_start_connectable_adv, &p_context);
    // The task will run nrf_ble_es_on_start_non_connectable_adv() before entering its loop.
    //nrf_sdh_freertos_init(nrf_ble_es_on_start_non_connectable_adv, &p_context);
}



Doing so makes the program crash with the following logs:


<debug> nrf_ble_lesc: Initialized nrf_crypto.
<debug> nrf_ble_lesc: Initialized nrf_ble_lesc.
<debug> nrf_ble_lesc: Generating ECC key pair
<error> app: SOFTDEVICE: ASSERTION FAILED


Further debugging leads us to the following function where we get the ASSERTION error:

ret_code_t es_security_init(es_security_msg_cb_t security_callback)
{
    ret_code_t err_code;

    if (security_callback == NULL)
    {
        return NRF_ERROR_INVALID_PARAM;
    }

    // Get lock code from 'es_app_config.h', or fetch it from flash if exists.
    lock_code_init(m_aes_ecb_lk.key);

    m_security_callback = security_callback;

    memset(&m_ecdh, 0, sizeof(es_security_ecdh_t));

    for (uint32_t i = 0; i < APP_MAX_EID_SLOTS; ++i)
    {
        m_security_slot[i].timing.time_counter = APP_CONFIG_TIMING_INIT_VALUE;
    }

    err_code = es_stopwatch_create(&m_seconds_passed_sw_id, APP_TIMER_TICKS(1000));
    APP_ERROR_CHECK(err_code);

    //Already Initialized through peer manager
    //err_code = nrf_crypto_init(); 
    //APP_ERROR_CHECK(err_code);

    return NRF_SUCCESS;
}



Are we missing any configuration /settings here ?? How can we resolve this app: SOFTDEVIE ASSERTION FAILED ERROR, Is  there is any other template program/example that we can  to achieve the desired goal??

Thanks!

Memory segments and section placement macros giving as follows:

FLASH RX 0x0 0x100000;RAM RWX 0x20000000 0x40000

FLASH_PH_START=0x0
FLASH_PH_SIZE=0x100000
RAM_PH_START=0x20000000
RAM_PH_SIZE=0x40000
FLASH_START=0x27000
FLASH_SIZE=0xd9000
RAM_START=0x20005968
RAM_SIZE=0x3a698

Parents
  • What is the context in which es_security_init is called? Could you post the functional call stack snapshot when this error happened. You can put a breakpoint at the place where the error happens and view the call stack when the breakpoint hits. If this is called in FreeRTOS task, then i am suspecting that there is some stack overflow corrupting the memory which in turns make the softdevice use a pointer pointing to this corrupted memory.

    If this is a task, then i suggest you increase the stack_size for this task ( and if necessary increase the heap size in FreeRTOSConfig.h )

  • Hi Susheel,

    Thank you for the replying.

    We fixed the es_security_init() function by adding some modifications in our previous code and fixed that error.

    The scenario is that we are now able to advertise the beacons and able to see the beacons in nRF Connect application but when we try to make a connection by using "Connect" button the application crashes with the below log:

    <error> app: ERROR 3735928559 [Unknown error code] at /external/freertos/source/tasks.c:4796
    PC at: 0x00035C83
    <error> app: End of error report


    We are getting the below function call stack:



    We also tried by updating the configTOTAL_HEAP_SIZE from 20kB to 30kB but it didn't workout.

    There is one more thing that we are observing in our code that there is a function NRF_SDH_BLE_OBSERVER() which is used to register the BLE events handler but whenever we are getting a BLE event i.e. the Connect event (mentioned above), our code doesn't reach this function. We have verified this by adding various breakpoints. Whereas in the "ble_app_hrs_freertos_pca10056" exampl (which we are using for reference) the code reaches the function.
    We are assuming that maybe there is some flag due to which the NRF_SDH_BLE_OBSERVER() function is not being compiled etc. or if there is something we are missing.

    Thanks

  • The error you mentioned happens if you have SD_EVT_IRQHandler triggered before the nrf_sdh_freertos_init has been completed. 

    Why was vTaskNotifyGiveFromISR called with a NULL task handle? If you get an answer for that then you are close to finding the issue here. My suspect is that you BLE activity has started way before the FreeRTOS tasks have been initialized properly

Reply Children
No Data
Related