2023-02-25 10:47:04 +08:00
|
|
|
/*
|
2023-11-09 16:38:19 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2023-02-25 10:47:04 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2023-02-28 leo first version
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <rtdevice.h>
|
|
|
|
#include "drv_common.h"
|
|
|
|
|
2023-11-09 16:38:19 +08:00
|
|
|
#if defined(BSP_USING_DEVICE_USBOTG1) || defined(BSP_USING_DEVICE_USBOTG2)
|
2023-02-27 14:59:47 +08:00
|
|
|
#include "usbd_int.h"
|
2023-11-09 16:38:19 +08:00
|
|
|
#include "drv_usbotg.h"
|
2023-02-27 14:59:47 +08:00
|
|
|
#include "drv_config.h"
|
2023-02-25 10:47:04 +08:00
|
|
|
|
2023-11-09 16:38:19 +08:00
|
|
|
static struct at32_usbotg *p_usbotg_instance = RT_NULL;
|
2023-02-25 10:47:04 +08:00
|
|
|
static struct ep_id endpoint_pool[] =
|
|
|
|
{
|
|
|
|
{0x0, USB_EP_ATTR_CONTROL, USB_DIR_INOUT, 64, ID_ASSIGNED },
|
|
|
|
{0x1, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
|
|
|
|
{0x1, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
|
|
|
|
{0x2, USB_EP_ATTR_INT, USB_DIR_IN, 64, ID_UNASSIGNED},
|
|
|
|
{0x2, USB_EP_ATTR_INT, USB_DIR_OUT, 64, ID_UNASSIGNED},
|
|
|
|
{0x3, USB_EP_ATTR_ISOC, USB_DIR_IN, 64, ID_UNASSIGNED},
|
|
|
|
{0x3, USB_EP_ATTR_ISOC, USB_DIR_OUT, 64, ID_UNASSIGNED},
|
|
|
|
{0xFF, USB_EP_ATTR_TYPE_MASK, USB_DIR_MASK, 0, ID_ASSIGNED },
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2023-11-09 16:38:19 +08:00
|
|
|
#ifdef BSP_USING_DEVICE_USBOTG1
|
|
|
|
USBOTG1_INDEX,
|
2023-02-25 10:47:04 +08:00
|
|
|
#endif
|
2023-11-09 16:38:19 +08:00
|
|
|
#ifdef BSP_USING_DEVICE_USBOTG2
|
|
|
|
USBOTG2_INDEX,
|
2023-02-25 10:47:04 +08:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2023-11-09 16:38:19 +08:00
|
|
|
static struct at32_usbotg usbotgd_config[] = {
|
|
|
|
#ifdef BSP_USING_DEVICE_USBOTG1
|
|
|
|
USBOTG1_CONFIG,
|
2023-02-25 10:47:04 +08:00
|
|
|
#endif
|
2023-11-09 16:38:19 +08:00
|
|
|
#ifdef BSP_USING_DEVICE_USBOTG2
|
|
|
|
USBOTG2_CONFIG,
|
2023-02-25 10:47:04 +08:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2023-11-09 16:38:19 +08:00
|
|
|
#ifdef BSP_USING_DEVICE_USBOTG1
|
2023-02-25 10:47:04 +08:00
|
|
|
void OTGFS1_IRQHandler(void)
|
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
2023-11-09 16:38:19 +08:00
|
|
|
usbd_irq_handler(p_usbotg_instance->p_otg_core);
|
2023-02-25 10:47:04 +08:00
|
|
|
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-11-09 16:38:19 +08:00
|
|
|
#ifdef BSP_USING_DEVICE_USBOTG2
|
2023-02-25 10:47:04 +08:00
|
|
|
void OTGFS2_IRQHandler(void)
|
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
2023-11-09 16:38:19 +08:00
|
|
|
usbd_irq_handler(p_usbotg_instance->p_otg_core);
|
2023-02-25 10:47:04 +08:00
|
|
|
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void usbd_reset_callback(usbd_core_type *udev)
|
|
|
|
{
|
|
|
|
udcd_t udcd = (udcd_t)udev->pdata;
|
|
|
|
rt_usbd_reset_handler(udcd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void usbd_connectCallback(usbd_core_type *udev)
|
|
|
|
{
|
|
|
|
udcd_t udcd = (udcd_t)udev->pdata;
|
|
|
|
rt_usbd_connect_handler(udcd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void usbd_disconnectCallback(usbd_core_type *udev)
|
|
|
|
{
|
|
|
|
udcd_t udcd = (udcd_t)udev->pdata;
|
|
|
|
rt_usbd_disconnect_handler(udcd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void usbd_setup_phase_done_callback(usbd_core_type *udev)
|
|
|
|
{
|
|
|
|
udcd_t udcd = (udcd_t)udev->pdata;
|
|
|
|
rt_usbd_ep0_setup_handler(udcd, (struct urequest*)udev->setup_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void usbd_data_in_stage_callback(usbd_core_type *udev, uint32_t ept_num)
|
|
|
|
{
|
|
|
|
udcd_t udcd = (udcd_t)udev->pdata;
|
|
|
|
|
|
|
|
if (ept_num == 0)
|
|
|
|
{
|
|
|
|
rt_usbd_ep0_in_handler(udcd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_usbd_ep_in_handler(udcd, 0x80 | ept_num, udev->ept_in[ept_num].trans_len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void usbd_sof_callback(usbd_core_type *udev)
|
|
|
|
{
|
|
|
|
udcd_t udcd = (udcd_t)udev->pdata;
|
|
|
|
rt_usbd_sof_handler(udcd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void usbd_data_out_stage_callback(usbd_core_type *udev, uint32_t ept_num)
|
|
|
|
{
|
|
|
|
udcd_t udcd = (udcd_t)udev->pdata;
|
|
|
|
|
|
|
|
if (ept_num != 0)
|
|
|
|
{
|
|
|
|
rt_usbd_ep_out_handler(udcd, ept_num, udev->ept_out[ept_num].trans_len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_usbd_ep0_out_handler(udcd, udev->ept_out[0].trans_len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t _ep_set_stall(rt_uint8_t address)
|
|
|
|
{
|
2023-11-09 16:38:19 +08:00
|
|
|
usbd_set_stall(&p_usbotg_instance->p_otg_core->dev, address);
|
2023-02-25 10:47:04 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t _ep_clear_stall(rt_uint8_t address)
|
|
|
|
{
|
2023-11-09 16:38:19 +08:00
|
|
|
usbd_clear_stall(&p_usbotg_instance->p_otg_core->dev, address);
|
2023-02-25 10:47:04 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t _set_address(rt_uint8_t address)
|
|
|
|
{
|
2023-11-09 16:38:19 +08:00
|
|
|
usbd_set_device_addr(&p_usbotg_instance->p_otg_core->dev, address);
|
2023-02-25 10:47:04 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t _set_config(rt_uint8_t address)
|
|
|
|
{
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t _ep_enable(uep_t ep)
|
|
|
|
{
|
|
|
|
RT_ASSERT(ep != RT_NULL);
|
|
|
|
RT_ASSERT(ep->ep_desc != RT_NULL);
|
|
|
|
|
2023-11-09 16:38:19 +08:00
|
|
|
usbd_ept_open(&p_usbotg_instance->p_otg_core->dev, ep->ep_desc->bEndpointAddress, \
|
2023-02-25 10:47:04 +08:00
|
|
|
ep->ep_desc->bmAttributes, ep->ep_desc->wMaxPacketSize);
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t _ep_disable(uep_t ep)
|
|
|
|
{
|
|
|
|
RT_ASSERT(ep != RT_NULL);
|
|
|
|
RT_ASSERT(ep->ep_desc != RT_NULL);
|
|
|
|
|
2023-11-09 16:38:19 +08:00
|
|
|
usbd_ept_close(&p_usbotg_instance->p_otg_core->dev, ep->ep_desc->bEndpointAddress);
|
2023-02-25 10:47:04 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2024-04-30 13:11:18 +08:00
|
|
|
static rt_ssize_t _ep_read(rt_uint8_t address, void *buffer)
|
2023-02-25 10:47:04 +08:00
|
|
|
{
|
|
|
|
rt_size_t size = 0;
|
|
|
|
RT_ASSERT(buffer != RT_NULL);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2024-04-30 13:11:18 +08:00
|
|
|
static rt_ssize_t _ep_read_prepare(rt_uint8_t address, void *buffer, rt_size_t size)
|
2023-02-25 10:47:04 +08:00
|
|
|
{
|
2023-11-09 16:38:19 +08:00
|
|
|
usbd_ept_recv(&p_usbotg_instance->p_otg_core->dev, address, buffer, size);
|
2023-02-25 10:47:04 +08:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2024-04-30 13:11:18 +08:00
|
|
|
static rt_ssize_t _ep_write(rt_uint8_t address, void *buffer, rt_size_t size)
|
2023-02-25 10:47:04 +08:00
|
|
|
{
|
2023-11-09 16:38:19 +08:00
|
|
|
usbd_ept_send(&p_usbotg_instance->p_otg_core->dev, address, buffer, size);
|
2023-02-25 10:47:04 +08:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t _ep0_send_status(void)
|
|
|
|
{
|
2023-11-09 16:38:19 +08:00
|
|
|
usbd_ept_send(&p_usbotg_instance->p_otg_core->dev, 0x00, RT_NULL, 0);
|
2023-02-25 10:47:04 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t _suspend(void)
|
|
|
|
{
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t _wakeup(void)
|
|
|
|
{
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t at32_dcd_init(rt_device_t device)
|
|
|
|
{
|
|
|
|
/* usb gpio config */
|
|
|
|
at32_msp_usb_init(device);
|
|
|
|
|
|
|
|
/* enable otgfs irq */
|
2023-11-09 16:38:19 +08:00
|
|
|
nvic_irq_enable(p_usbotg_instance->irqn, 2, 0);
|
2023-02-25 10:47:04 +08:00
|
|
|
|
|
|
|
/* init usb */
|
2023-11-09 16:38:19 +08:00
|
|
|
p_usbotg_instance->p_otg_core->usb_reg = usb_global_select_core(p_usbotg_instance->id);
|
2023-02-25 10:47:04 +08:00
|
|
|
|
|
|
|
/* usb device core config */
|
2023-11-09 16:38:19 +08:00
|
|
|
usb_core_config(p_usbotg_instance->p_otg_core, p_usbotg_instance->dev_spd);
|
2023-02-25 10:47:04 +08:00
|
|
|
|
2023-11-09 16:38:19 +08:00
|
|
|
if(p_usbotg_instance->p_otg_core->cfg.sof_out)
|
2023-02-25 10:47:04 +08:00
|
|
|
{
|
2023-11-09 16:38:19 +08:00
|
|
|
p_usbotg_instance->p_otg_core->usb_reg->gccfg_bit.sofouten = TRUE;
|
2023-02-25 10:47:04 +08:00
|
|
|
}
|
|
|
|
|
2023-11-09 16:38:19 +08:00
|
|
|
if(p_usbotg_instance->p_otg_core->cfg.vbusig)
|
2023-02-25 10:47:04 +08:00
|
|
|
{
|
2023-11-09 16:38:19 +08:00
|
|
|
p_usbotg_instance->p_otg_core->usb_reg->gccfg_bit.vbusig = TRUE;
|
2023-02-25 10:47:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* usb device core init */
|
2023-11-09 16:38:19 +08:00
|
|
|
usbd_core_init(&(p_usbotg_instance->p_otg_core->dev), p_usbotg_instance->p_otg_core->usb_reg,
|
|
|
|
p_usbotg_instance->dev_spd);
|
2023-02-25 10:47:04 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
const static struct udcd_ops _udc_ops =
|
|
|
|
{
|
|
|
|
_set_address,
|
|
|
|
_set_config,
|
|
|
|
_ep_set_stall,
|
|
|
|
_ep_clear_stall,
|
|
|
|
_ep_enable,
|
|
|
|
_ep_disable,
|
|
|
|
_ep_read_prepare,
|
|
|
|
_ep_read,
|
|
|
|
_ep_write,
|
|
|
|
_ep0_send_status,
|
|
|
|
_suspend,
|
|
|
|
_wakeup,
|
|
|
|
};
|
|
|
|
|
|
|
|
int at32_usbd_register(void)
|
|
|
|
{
|
|
|
|
rt_size_t obj_num;
|
|
|
|
rt_err_t result = 0;
|
|
|
|
int index;
|
|
|
|
|
2023-11-09 16:38:19 +08:00
|
|
|
obj_num = sizeof(usbotgd_config) / sizeof(struct at32_usbotg);
|
2023-02-25 10:47:04 +08:00
|
|
|
|
|
|
|
for (index = 0; index < obj_num; index++) {
|
|
|
|
udcd_t udcd = (udcd_t)rt_malloc(sizeof(struct udcd));
|
|
|
|
if (udcd == RT_NULL)
|
|
|
|
{
|
|
|
|
rt_kprintf("uhcd malloc failed\r\n");
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
rt_memset((void *)udcd, 0, sizeof(struct udcd));
|
|
|
|
|
|
|
|
otg_core_type *p_otg_core = (otg_core_type *)rt_malloc(sizeof(otg_core_type));
|
|
|
|
if (p_otg_core == RT_NULL)
|
|
|
|
{
|
|
|
|
rt_kprintf("otg_core malloc failed\r\n");
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
rt_memset((void *)p_otg_core, 0, sizeof(otg_core_type));
|
|
|
|
|
|
|
|
udcd->parent.type = RT_Device_Class_USBDevice;
|
|
|
|
udcd->parent.init = at32_dcd_init;
|
|
|
|
|
|
|
|
udcd->parent.user_data = &(p_otg_core->dev);
|
|
|
|
udcd->ops = &_udc_ops;
|
|
|
|
p_otg_core->dev.pdata = udcd;
|
2023-11-09 16:38:19 +08:00
|
|
|
usbotgd_config[index].p_otg_core = p_otg_core;
|
2023-02-25 10:47:04 +08:00
|
|
|
|
|
|
|
/* register endpoint infomation */
|
|
|
|
udcd->ep_pool = endpoint_pool;
|
|
|
|
udcd->ep0.id = &endpoint_pool[0];
|
|
|
|
|
2023-11-09 16:38:19 +08:00
|
|
|
result = rt_device_register((rt_device_t)udcd, usbotgd_config[index].name, 0);
|
2023-02-25 10:47:04 +08:00
|
|
|
RT_ASSERT(result == RT_EOK);
|
|
|
|
|
2023-11-09 16:38:19 +08:00
|
|
|
p_usbotg_instance = &usbotgd_config[index];
|
2023-02-25 10:47:04 +08:00
|
|
|
|
|
|
|
result = rt_usb_device_init();
|
|
|
|
RT_ASSERT(result == RT_EOK);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
INIT_DEVICE_EXPORT(at32_usbd_register);
|
|
|
|
|
|
|
|
#endif
|