Calling BLE functions from outside main

HI,

We are working with nRF52832 chip for a custom product with OLED (ss1306) , external flash chip and QRcode sensor in it.

Successfully integrated the code which works as expected by properly configuring sdkconfig.h and related files.

But now our main.c is bulky and we are trying to separate the code to different functional blocks and managed to create custom components and code works fine.

Now we are converting the BLE functions as a separate block as we use ble_nus_data_send() functions in other blocks also.

but now we are able to send and receive nus data from the custom made ble function source code( same functions as different .c file), but these functions is not sending data when it is called in other .c files.

Application in a glance

QR code datas are scanned and the scanned data is saved in the external flash

We receive certain commands from BLE and we need to send responses accordingly.

On scanning QR codes , for some codes BLE data transfer is needed.

We are using SES v5.42a

SDK version 17.0.2.

Is it a good approach to change BLE functionalities from main.c ? Or is there any alternative methods to meet the requirement ?

Any help would be appreciated

thanks & regards

Parents Reply Children
  • Hi Vidar,

    Thanx for the reply .

    First of all im new to nRF chips and BLE operations , suggestions/corrections are appreciated in my approach

    This is my custom file for BLEoperations, i declared BLE_NUS_DEF(m_nus, NRF_SDH_BLE_TOTAL_LINK_COUNT);  here only and i added corresponding header files in main.c.

    Fig below shows my main.c

    but  "m_conn_handle 0 ErrorCode =8" is the debug msg received , means error code 8

    #define NRF_ERROR_INVALID_STATE               (NRF_ERROR_BASE_NUM + 8)  ///< Invalid state, operation disallowed in this state . I feel the issue is something related with m_conn_handle

    at the same time i can use ble_nus_data_send() function succesfully inside the nus_handler to acknowledge the data reception

  • Thanks for the additional information. Can you also show where main.c get the 'm_nus' instance from? Is it defined in main too? The important thing here is that main.c and BLEapplication.c are accessing the same variable.

    EDIT: After reviewing the code more closely, I noticed that you are defining the variable in your BLEapplications.h header file. This will result in a new instance of 'm_nus' being defined in every source file that includes this header. To avoid this, you should define the variable in one of the source files and then share it with others, for example, via a pointer reference.

  • Hi Vidar,

    Sorry for the late reply.

    As you suggested my code changes are as follows

    Inside BLEApplications.h

    extern ble_nus_t *p_m_nus;

    extern ble_nus_t* get_nus_instance();


    Inside BLEApplications.c

     static ble_nus_t m_nus;
     BLE_NUS_DEF(m_nus, NRF_SDH_BLE_TOTAL_LINK_COUNT);       

    ble_nus_t* get_nus_instance()

      {      
        return &m_nus;
       }

     Inside main.c

        ble_nus_t *p_m_nus;
        p_m_nus = get_nus_instance();

        if(ConnectionStatus)
         {
            ErrorCode=ble_nus_data_send(p_m_nus, data_array, &length3, m_conn_handle);
            printf("ErrorCode =%d  m_conn_handle %x\n",ErrorCode,m_conn_handle);
         }

    Now the BLE data transfer works fine from main.c, Thanx Vidar Relaxed

     Inside Process.c

          ble_nus_t *p_m_nus;

           printf("Inside EraseDataFromFlash\n");
           p_m_nus = get_nus_instance();
           ResponseCode=ble_nus_data_send(p_m_nus, DATA_ERASED, ERASE_DATA_LEN,            m_conn_handle);//Send BLE acknowledgement
          
           memset(TempData,0,sizeof(TempData));
           sprintf(TempData, "\nResponseCode =%d\n",ResponseCode);
           printf(TempData);

    But debug msg says ResponseCode =5

    Is this the correct implementation of your suggestion?

    I think something is missing here, but couldnt find it.


    Actually i didnt get the purpose and implementation of 

    BLE_NUS_DEF(m_nus, NRF_SDH_BLE_TOTAL_LINK_COUNT); macro

    thanks & regards

  • Hi,

    ble_nus_data_send() will return NRF_ERROR_NOT_FOUND (5) if m_conn_handle == 0, or p_m_nus == NULL.

  • Hi,

    That was a mistake from the application code writen by me.

     ble_nus_t *p_m_nus3;
     p_m_nus3 = get_nus_instance();

     length=sizeof(RecvBuff);
     ResponseCode=ble_nus_data_send(p_m_nus3, RecvBuff, &length, m_conn_handle);
                   
    Now code works as expected , i can send BLE data using ble_nus_data_send() function from main.c as well as process.c

    Thanx Vidar for the support so far.

    Now i just started learning about nrfconnect sdk as we need to port the code to get maximum available options in development.

    Further support is anticipated. Thanks once again

    thanks & regards

Related