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

Try to separate the sample of hid_keyboard into ble function and hid service, then found it could be connected at first but show pair immediately

Hi, 

As the subject description, I just remove the part of hid function from main.c then make the personal source file.

Before I removed, the project could work as usual.

And after I create the source file for hid function into the project, the project could be built without error.

Somehow, it could not work properly.

Compare the log

As you could see from the picture above, the left show fatal error compare to the right(Normal).

Not sure why it could be connected but show pair immediately without function.

Do you have any idea or instruction could share with me?

Thanks.

  • A random guess based off your description is that you are referencing memory that is no longer in scope / belonged to a returned functions stack frame.

    Could you provide some code?

  • Hi Smackenzie,

    Actually, I could not understand "referencing memory", what does it mean?

    Anyway, below is the header I make

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    #ifndef HID_FUNCTION_H__
    #define HID_FUNCTION_H__
    #include <stdint.h>
    #include <stdbool.h>
    #include "ble.h"
    #include "ble_advdata.h"
    #include "ble_advertising.h"
    #include "nrf_ble_gatt.h"
    #include "ble_hids.h"
    #ifdef __cplusplus
    extern "C" {
    #endif
    #define MAX_BUFFER_ENTRIES 5 /**< Number of elements that can be enqueued */
    /**Buffer queue access macros
    *
    * @{ */
    /** Initialization of buffer list */
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    And here is the source code:

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    #include "sdk_common.h"
    #include "ble_hids.h"
    #include <string.h>
    #include "app_error.h"
    #include "ble_srv_common.h"
    #include "nrf_ble_gatt.h"
    #include "ble_advertising.h"
    #include "hid_function.h"
    #include "nrf_delay.h"
    #define OUTPUT_REPORT_INDEX 0 /**< Index of Output Report. */
    #define OUTPUT_REPORT_MAX_LEN 1 /**< Maximum length of Output Report. */
    #define INPUT_REPORT_KEYS_INDEX 0 /**< Index of Input Report. */
    #define INPUT_REP_REF_ID 0 /**< Id of reference to Keyboard Input Report. */
    #define OUTPUT_REP_REF_ID 0 /**< Id of reference to Keyboard Output Report. */
    #define FEATURE_REP_REF_ID 0 /**< ID of reference to Keyboard Feature Report. */
    #define FEATURE_REPORT_MAX_LEN 2 /**< Maximum length of Feature Report. */
    #define FEATURE_REPORT_INDEX 0 /**< Index of Feature Report. */
    #define BASE_USB_HID_SPEC_VERSION 0x0101 /**< Version number of base USB HID Specification implemented by this application. */
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    About the main.c, I just include them and add the function as below:

    hids_init();

    buffer_init();

    Thanks.

  • Hi Smackenzie, 

    Was the information for you?

    Or you still need the main.c?

  • Hi Smackenzie,

    I just noticed that when I remove below functions from main.c to source file the issue happened.

    static void hids_init(void)

    static void on_hid_rep_char_write(ble_hids_evt_t * p_evt)

    static void on_hids_evt(ble_hids_t * p_hids, ble_hids_evt_t * p_evt)

    The symptom is a little bit like force to disconnected by windows.

    However, the device could be stable connected with mobile.

    Do you have any idea or experience with that?

    Thanks.

  • The error happened in app_error_weak.c app_error_fault_handler function. Enable DEBUG define in your project and put a breakpoint here and run your code in debugger and step to see which can of error this is. You will get more info if you enable DEBUG in your preprocessor defines.