GD32H7XX的USB-host例程求助

[复制链接]
1215|1
holicfly 发表于 2025-11-12 13:52 | 显示全部楼层 |阅读模式
在看GD32H7xx的usb官方demo(GD32H7xx_Firmware_Library_V1.4.0\Examples\USBHS\usb_host\usb_host_hid_keyboard_mouse),发现个问题,想请教各位大佬,以下是代码
<app.c>主进程入口

usbh_host usb_host_hid;
usb_core_driver hid_host_core;

int main(void)
{
   
    usb_core_driver *idev = (usb_core_driver *)usb_host_hid.data;
   
    cache_enable();


#ifdef USE_ULPI_PHY
    usb_gpio_config();
#endif /* USE_ULPI_PHY */


    usb_rcu_config();


    usb_timer_init();


    /* configure GPIO pin used for switching VBUS power and charge pump I/O */
    usb_vbus_config();


    /* register device class */
    usbh_class_register(&usb_host_hid, &usbh_hid);


#ifdef USE_USBHS0


#ifdef USE_USB_FS
    usb_para_init(&hid_host_core, USBHS0, USB_SPEED_FULL);
#endif


#ifdef USE_USB_HS
    usb_para_init(&hid_host_core, USBHS0, USB_SPEED_HIGH);
#endif


#endif /* USE_USBHS0 */


#ifdef USE_USBHS1


#ifdef USE_USB_FS
    usb_para_init(&hid_host_core, USBHS1, USB_SPEED_FULL);
#endif


#ifdef USE_USB_HS
    usb_para_init(&hid_host_core, USBHS1, USB_SPEED_HIGH);
#endif


#endif /* USE_USBHS1 */


     usb_core_driver *udev = (usb_core_driver *)usb_host_hid.data;


    usbh_init(&usb_host_hid, &hid_host_core, &usr_cb);


#ifdef USE_USB_HS
    #ifndef USE_ULPI_PHY
        #ifdef USE_USBHS0
            pllusb_rcu_config(USBHS0);
        #elif defined USE_USBHS1
            pllusb_rcu_config(USBHS1);
        #else
        #endif
    #endif /* !USE_ULPI_PHY */
#endif /* USE_USB_HS */


    usb_intr_config();


    while(1) {
        usbh_core_task(&usb_host_hid);
    }
}

进入usbh_init


/*!
    \brief      USB host stack initializations
    \param[in]  uhost: pointer to USB host
    \param[in]  udev: pointer to USB core instance
    \param[in]  user_cb: pointer to user callback
    \param[out] none
    \retval     none
*/
void usbh_init(usbh_host *uhost, usb_core_driver *udev, usbh_user_cb *user_cb)
{
    /* host de-initializations */
    usbh_deinit(uhost);

    uhost->usr_cb = user_cb;

    udev->host.connect_status = 0U;

    for(uint8_t i = 0U; i < USBHS_MAX_TX_FIFOS; i++) {
        udev->host.pipe.err_count = 0U;
        udev->host.pipe.pp_status = PIPE_IDLE;
        udev->host.backup_xfercount = 0U;
    }

    udev->host.pipe[0].ep.mps = 8U;

    usb_basic_init(&udev->bp, &udev->regs);

#ifndef DUAL_ROLE_MODE_ENABLED
    usb_globalint_disable(&udev->regs);

    usb_core_init(udev->bp, &udev->regs);

#ifndef USE_OTG_MODE
    usb_curmode_set(&udev->regs, HOST_MODE);
#endif /* USE_OTG_MODE */

    usb_host_init(udev);

    usb_globalint_enable(&udev->regs);
#endif /* DUAL_ROLE_MODE_ENABLED */

    /* link driver to the stack */
    udev->host.data = (void *)uhost;
    uhost->data = (void *)udev;

    /* upon initialize call USR call back */
    uhost->usr_cb->dev_init();
}

进入usbh_deinit

/*!
    \brief      de-initialize USB host
    \param[in]  uhost: pointer to USB host
    \param[out] none
    \retval     operation status
*/
usbh_status usbh_deinit(usbh_host *uhost)
{
    usb_core_driver *udev = (usb_core_driver *)uhost->data;
    if (NULL == udev) {
        printf("NULL!");
    }

    /* software initialize */
    uhost->cur_state = HOST_DEFAULT;
    uhost->backup_state = HOST_DEFAULT;
    uhost->enum_state = ENUM_DEFAULT;

    uhost->control.ctl_state = CTL_IDLE;
    uhost->control.max_len = USB_FS_EP0_MAX_LEN;

    uhost->dev_prop.addr = USBH_DEV_ADDR_DEFAULT;
    uhost->dev_prop.speed = PORT_SPEED_FULL;
    uhost->dev_prop.cur_itf = 0xFFU;

    usbh_pipe_free(udev, uhost->control.pipe_in_num);
    usbh_pipe_free(udev, uhost->control.pipe_out_num);

    return USBH_OK;
}

进入usbh_pipe_free

uint8_t usbh_pipe_free(usb_core_driver *udev, uint8_t pp_num)
{
    if(pp_num < HP_MAX) {
        udev->host.pipe[3].in_used = 0U;
    }

    return USBH_OK;
}

从usbh_deinit函数中可以看出udev是一个空指针,下面我加的判空验证也进去了,执行到usbh_pipe_free函数中udev->host.pipe[3].in_used = 0U;这一句时,对空指针解引用,这一句应该有问题吧?可是在我裸机运行此demo时却正常运行,没有异常。然而当移植到项目中时,项目运行的是RT-Thread,走到这一句直接报错hard fault。芯片是GD32H759IMK,想在此问问前辈大佬们,这是什么原因,感激不尽!


MessageRing 发表于 2025-11-18 16:17 | 显示全部楼层
这个字体看着好晕
您需要登录后才可以回帖 登录 | 注册

本版积分规则

2

主题

2

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部