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

USB HID and BLE working together ,

When bluetooth only is enabled, the program has no problems.After adding USB, the program crashes。How am I supposed to initialize it, is it a matter of order

<info> app: log_init

int main(void)
{
	log_init();
    ret_code_t err_code;
    timers_init();
    usb_keyboard_init();
    ble_init();

    err_code = app_timer_create(&m_keyboard_scan_timer, APP_TIMER_MODE_REPEATED, keyboard_scan_handler);
    APP_ERROR_CHECK(err_code);   
    err_code = app_timer_start(m_keyboard_scan_timer, APP_TIMER_TICKS(KEYBOARD_SCAN_INTERVAL), NULL);
    APP_ERROR_CHECK(err_code);
    for (;;)
    {
        while (app_usbd_event_queue_process())
        {
            /* Nothing to do */
        }
        idle_state_handle();
    }
}
void ble_init(void)
{
	power_management_init();
	ble_stack_init();
	gap_params_init();
	gatt_init();
	advertising_init();
	services_init();
    conn_params_init();
	advertising_start();
}

<info> app: timers init

<info> CLOCK: Function: nrfx_clock_init, error code: NRF_SUCCESS.

<info> CLOCK: Module enabled.

<info> clock: Function: nrf_drv_clock_init, error code: NRF_SUCCESS.

<info> app: USBD HID composite example started.

<info> app: USB power detected

<error> app: Fatal error

Parents
  • Hello,

    Can you please try to define DEBUG in your preprocessor defines? Please let me know what IDE you are using if you are not sure how to add this to your preprocessor defines.

    After doing so, the log should give some more info on what the error is. What function does it point to, and what is the return value?

    Best regards,

    Edvin

    • I compiled in VScode and configured the GCC compilation environment

  • Ok. Open your makefile in a text editor and look for the lines starting with CFLAGS += ...

    Add the following line somewhere in between there:

    CFLAGS += -DDEBUG

    Then recompile and flash it again. What does the log say?

  • <info> CLOCK: Function: nrfx_clock_init, error code: NRF_SUCCESS.
    
    <info> CLOCK: Module enabled.
    
    <info> clock: Function: nrf_drv_clock_init, error code: NRF_SUCCESS.
    
    <info> app: USBD HID composite example started.
    
    <debug> nrf_sdh: State request: 0x00000000
    
    <debug> nrf_sdh: State change: 0x00000000
    
    <debug> nrf_sdh: State change: 0x00000001
    
    <debug> nrf_sdh_ble: RAM starts at 0x20002AE8
    
    <info> app: keyboard_scan_handler
    
    <info> app: Fast advertising.
    
    <info> app: keyboard_scan_handler
    
    <info> app: keyboard_scan_handler
    
    <info> app: keyboard_scan_handler
    
    <info> app: keyboard_scan_handler
    
    <info> app: keyboard_scan_handler
    
    <info> app: keyboard_scan_handler
    
    <info> app: keyboard_scan_handler
    
    <info> app: keyboard_scan_handler
    
    <info> app: keyboard_scan_handler
    
    <debug> nrf_sdh_soc: SoC event: 0xA.
    
    <info> app: USB power detected
    
    <debug> nrf_sdh_soc: SoC event: 0x9.
    
    <info> app: USB ready
    
    <debug> USBD: USBD event: SUSPEND
    
    <debug> USBD: USBD event: RESUME
    
    <debug> nrf_sdh_soc: SoC event: 0x0.
    
    <debug> USBD: USBD event: Setup (rt:80 r:06 v:0100 i:0000 l:64 )
    
    <debug> app_usbd_core: SETUP: t: 0x80 r: 0x06
    
    <debug> USBD: Transfer called on endpoint 80, size: 18, mode: FLASH
    
    <debug> USBD: USB DMA process: Starting transfer on EP: 80, size: 18
    
    <debug> USBD: USB DMA process - finishing
    
    <debug> USBD: USB event: DMA ready IN0
    
    <debug> USBD: USBD event: EndpointData: 80
    
    <debug> USBD: USBD event: EndpointData: In finished
    
    <debug> USBD: >> ep0status >>
    
    <debug> USBD: USBD event: Setup (rt:00 r:05 v:000B i:0000 l:0 )
    
    <debug> USBD: USBD event: Setup (rt:80 r:06 v:0100 i:0000 l:18 )
    
    <debug> app_usbd_core: SETUP: t: 0x80 r: 0x06
    
    <debug> USBD: Transfer called on endpoint 80, size: 18, mode: FLASH
    
    <debug> USBD: USB DMA process: Starting transfer on EP: 80, size: 18
    
    <debug> USBD: USB DMA process - finishing
    
    <debug> USBD: USB event: DMA ready IN0
    
    <debug> USBD: USBD event: EndpointData: 80
    
    <debug> USBD: USBD event: EndpointData: In finished
    
    )
    
    <debug> USBD: USB event: DMA ready IN0
    
    <debug> USBD: USBD event: EndpointData: 80
    
    <debug> USBD: USBD event: EndpointData: In finished
    
    <debug> USBD: >> ep0status >>
    
    <debug> USBD: USBD event: Setup (rt:80 r:06 v:0600 i:0000 l:10 )
    
    <debug> app_usbd_core: SETUP: t: 0x80 r: 0x06
    
    <debug> USBD: Setup stalled.
    
    <debug> USBD: USBD event: Setup (rt:80 r:06 v:0100 i:0000 l:18 )
    
    <debug> app_usbd_core: SETUP: t: 0x80 r: 0x06
    
    <debug> USBD: Transfer called on endpoint 80, size: 18, mode: FLASH
    
    : 18
    
    <debug> USBD: USB event: DMA ready IN0
    
    <debug> USBD: USBD event: EndpointData: 80
    
    <debug> USBD: USBD event: EndpointData: In finished
    
    <debug> USBD: >> ep0status >>
    
    <debug> USBD: USBD event: Setup (rt:21 r:09 v:0200 i:0000 l:1 )
    
    <debug> app_usbd_core: SETUP: t: 0x21 r: 0x09
    
    <debug> USBD: USBD event: EndpointData: 0
    
    <debug> USBD: USB DMA process: Starting transfer on EP: 0, size: 1
    
    <debug> USBD: USB DMA process - finishing
    
    <debug> USBD: USB event: DMA ready OUT0
    
    Keyboard_scan_handler, this is one of my timer callback functions, if it's normal, it should be displayed once for 5ms, obviously it's not working properly, and the bluetooth is also dead with the timer

    • 我上传了固件,我真的不知道为什么,

     

Reply Children
  • I am struggling a bit to navigate in your project, and it seems like you have changed your error handling, but I can't find where it is printing. But the end of the log looks like this, after I did some changes:

     0> <info> app: USBD HID composite example started.
     0> <debug> nrf_sdh: State request: 0x00000000
     0> <debug> nrf_sdh: State change: 0x00000000
     0> <debug> nrf_sdh: State change: 0x00000001
     0> <debug> nrf_sdh_ble: RAM starts at 0x20002AE8
     0> <info> app: keyboard_scan_handler 00
     0> <info> app: keyboard_scan_handler 01
     0> usb.c:98
     0> PC at: 0x00031D63
    

    So your usb.c file catches an error on line 98 in usb.c. After debugging, I found that it was 8 (NRF_ERROR_INVALID_STATE). This only occurs when the USBD is not connected. When it is connected, it looks like your USB scheduler is filled up:

     ...
     0> <info> app: keyboard_scan_handler 25
     0> <info> app: keyboard_scan_handler 26
     0> <info> app: keyboard_scan_handler 27
     0> <info> app: keyboard_scan_handler 28
     0> <info> app: keyboard_scan_handler 29
     0> <info> app: keyboard_scan_handler 2A
     0> <info> app: keyboard_scan_handler 2B
     0> <info> app: keyboard_scan_handler 2C
     0> <info> app: keyboard_scan_handler 2D
     0> <info> app: keyboard_scan_handler 2E
     0> <info> app: keyboard_scan_handler 2F
     0> <info> app: keyboard_scan_handler 30
     0> <info> app: keyboard_scan_handler 31
     0> <info> app: keyboard_scan_handler 32
     0> <info> app: keyboard_scan_handler 33
     0> <info> app: keyboard_scan_handler 34
     0> <info> app: keyboard_scan_handler 35
     0> <info> app: keyboard_scan_handler 36
     0> <info> app: keyboard_scan_handler 37
     0> <info> app: keyboard_scan_handler 38
     0> <info> app: keyboard_scan_handler 39
     0> <info> app: keyboard_scan_handler 3A
     0> <info> app: keyboard_scan_handler 3B
     0> <info> app: keyboard_scan_handler 3C
     0> <info> app: keyboard_scan_handler 3D
     0> <info> app: keyboard_scan_handler 3E
     0> <info> app: keyboard_scan_handler 3F
     0> <info> app: keyboard_scan_handler 40
     0> <info> app: keyboard_scan_handler 41
     0> <info> app: keyboard_scan_handler 42
     0> <info> app: keyboard_scan_handler 43
     0> <warning> app_usbd: Stacked over 500 SOF events.
     0> <debug> USBD: USBD event: Setup (rt:80 r:06 v:0100 i:0000 l:64 )
     0> <warning> app_usbd: Stacked over 500 SOF events.
     0> <debug> USBD: USBD event: Setup (rt:80 r:06 v:0100 i:0000 l:64 )
     0> <warning> app_usbd: Stacked over 500 SOF events.
     0> <debug> USBD: USBD event: Setup (rt:80 r:06 v:0100 i:0000 l:64 )
     0> <warning> app_usbd: Stacked over 500 SOF events.
     0> <debug> USBD: USBD event: SUSPEND
    

    I haven't looked thoroughly through your USB state machine, but perhaps you lack some event handling in one of the usb events.

    The other issue I found, and probably the reason you don't see the app_timer after a while, is that your idle_state_handle() is calling __WFE() instead of sd_app_evt_wait(), which is the call you should use when you are using the softdevice.

    Did you start with the BLE example, or the USB example? Just curious whether you added the softdevice to the project yourself or not. 

    I suggest that you use this as your idle_state_handle():

    //空闲状态处理函数。如果没有挂起的日志操作,则睡眠直到下一个事件发生后唤醒系统
    static void idle_state_handle(void)
    {
        while (NRF_LOG_PROCESS())
        {
            //wait
        }
        //__WFE();
        sd_app_evt_wait();
    }

  • I've blocked matrix_init(), which can broadcast and recognize to USB, but I don't know what caused it. This function initializes some pins, but it causes the bluetooth broadcast to fail and the timer to crash. I also changed idle_state_handle(void) according to your suggestion

Related