mirror of
https://github.com/sakumisu/CherryUSB.git
synced 2025-05-09 00:21:44 +08:00
feat(port/kinetis): add kinetis usbip
This commit is contained in:
parent
0b81f3b2a2
commit
e425b992be
12
Kconfig
12
Kconfig
@ -48,6 +48,12 @@ if CHERRYUSB
|
||||
bool "musb_bk"
|
||||
config CHERRYUSB_DEVICE_MUSB_CUSTOM
|
||||
bool "musb_custom"
|
||||
config CHERRYUSB_DEVICE_KINETIS_MCX
|
||||
bool "kinetis_mcx"
|
||||
config CHERRYUSB_DEVICE_KINETIS_MM32
|
||||
bool "kinetis_mm32"
|
||||
config CHERRYUSB_DEVICE_KINETIS_CUSTOM
|
||||
bool "kinetis_custom"
|
||||
config CHERRYUSB_DEVICE_CHIPIDEA_MCX
|
||||
bool "chipidea_mcx"
|
||||
config CHERRYUSB_DEVICE_CHIPIDEA_CUSTOM
|
||||
@ -201,6 +207,12 @@ if CHERRYUSB
|
||||
bool "xhci_phytium"
|
||||
config CHERRYUSB_HOST_XHCI_CUSTOM
|
||||
bool "xhci"
|
||||
config CHERRYUSB_HOST_KINETIS_MCX
|
||||
bool "kinetis_mcx"
|
||||
config CHERRYUSB_HOST_KINETIS_MM32
|
||||
bool "kinetis_mm32"
|
||||
config CHERRYUSB_HOST_KINETIS_CUSTOM
|
||||
bool "kinetis_custom"
|
||||
endchoice
|
||||
|
||||
config CHERRYUSB_HOST_CDC_ACM
|
||||
|
@ -185,7 +185,7 @@ USB basic concepts and how the CherryUSB Device stack is implemented, see [Cherr
|
||||
|Phytium | e2000 | pusb2/xhci |[phytium_repo](https://gitee.com/phytium_embedded/phytium-free-rtos-sdk)|>=1.4.0 | Long-term |
|
||||
|Artinchip | d12x/d13x/d21x | aic/ehci/ohci |[luban-lite](https://gitee.com/artinchip/luban-lite)|<= latest | Long-term |
|
||||
|Espressif | esp32s2/esp32s3/esp32p4 | dwc2 |[esp32_repo](https://github.com/CherryUSB/cherryusb_esp32)|<= latest | Long-term |
|
||||
|NXP | mcx | chipidea/ehci |[nxp_mcx_repo](https://github.com/CherryUSB/cherryusb_mcx)|<= latest | Long-term |
|
||||
|NXP | mcx | kinetis/chipidea/ehci |[nxp_mcx_repo](https://github.com/CherryUSB/cherryusb_mcx)|<= latest | Long-term |
|
||||
|AllwinnerTech | F1C100S/F1C200S | musb |[cherryusb_rtt_f1c100s](https://github.com/CherryUSB/cherryusb_rtt_f1c100s)|<= latest | the same with musb |
|
||||
|Bekencorp | bk7256/bk7258 | musb |[bk_idk](https://github.com/CherryUSB/bk_idk)| v0.7.0 | the same with musb |
|
||||
|Sophgo | cv18xx | dwc2 |[cvi_alios_open](https://github.com/CherryUSB/cvi_alios_open)| v0.7.0 | TBD |
|
||||
|
@ -186,7 +186,7 @@ CherryUSB 快速入门、USB 基本概念,API 手册,Class 基本概念和
|
||||
|Phytium | e2000 | pusb2/xhci |[phytium_repo](https://gitee.com/phytium_embedded/phytium-free-rtos-sdk)|>=1.4.0 | Long-term |
|
||||
|Artinchip | d12x/d13x/d21x | aic/ehci/ohci |[luban-lite](https://gitee.com/artinchip/luban-lite)|<= latest | Long-term |
|
||||
|Espressif | esp32s2/esp32s3/esp32p4 | dwc2 |[esp32_repo](https://github.com/CherryUSB/cherryusb_esp32)|<= latest | Long-term |
|
||||
|NXP | mcx | chipidea/ehci |[nxp_mcx_repo](https://github.com/CherryUSB/cherryusb_mcx)|<= latest | Long-term |
|
||||
|NXP | mcx | kinetis/chipidea/ehci |[nxp_mcx_repo](https://github.com/CherryUSB/cherryusb_mcx)|<= latest | Long-term |
|
||||
|AllwinnerTech | F1C100S/F1C200S | musb |[cherryusb_rtt_f1c100s](https://github.com/CherryUSB/cherryusb_rtt_f1c100s)|<= latest | the same with musb |
|
||||
|Bekencorp | bk7256/bk7258 | musb |[bk_idk](https://github.com/CherryUSB/bk_idk)| v0.7.0 | the same with musb |
|
||||
|Sophgo | cv18xx | dwc2 |[cvi_alios_open](https://github.com/CherryUSB/cvi_alios_open)| v0.7.0 | TBD |
|
||||
|
10
SConscript
10
SConscript
@ -58,6 +58,11 @@ if GetDepend(['PKG_CHERRYUSB_DEVICE']):
|
||||
src += Glob('port/musb/usb_glue_bk.c')
|
||||
if GetDepend(['PKG_CHERRYUSB_DEVICE_MUSB_CUSTOM']):
|
||||
src += Glob('port/musb/usb_dc_musb.c')
|
||||
if GetDepend(['PKG_CHERRYUSB_DEVICE_KINETIS_MCX']):
|
||||
src += Glob('port/kinetis/usb_dc_kinetis.c')
|
||||
src += Glob('port/kinetis/usb_glue_mcx.c')
|
||||
if GetDepend(['PKG_CHERRYUSB_DEVICE_KINETIS_CUSTOM']):
|
||||
src += Glob('port/kinetis/usb_dc_kinetis.c')
|
||||
if GetDepend(['PKG_CHERRYUSB_DEVICE_CHIPIDEA_MCX']):
|
||||
src += Glob('port/chipidea/usb_dc_chipidea.c')
|
||||
src += Glob('port/chipidea/usb_glue_mcx.c')
|
||||
@ -187,6 +192,11 @@ if GetDepend(['PKG_CHERRYUSB_HOST']):
|
||||
src += Glob('port/musb/usb_glue_bk.c')
|
||||
if GetDepend(['PKG_CHERRYUSB_HOST_MUSB_CUSTOM']):
|
||||
src += Glob('port/musb/usb_hc_musb.c')
|
||||
if GetDepend(['PKG_CHERRYUSB_HOST_KINETIS_MCX']):
|
||||
src += Glob('port/kinetis/usb_hc_kinetis.c')
|
||||
src += Glob('port/kinetis/usb_glue_mcx.c')
|
||||
if GetDepend(['PKG_CHERRYUSB_HOST_KINETIS_CUSTOM']):
|
||||
src += Glob('port/kinetis/usb_hc_kinetis.c')
|
||||
if GetDepend(['PKG_CHERRYUSB_HOST_PUSB2']):
|
||||
path += [cwd + '/port/pusb2/rt-thread']
|
||||
src += Glob('port/pusb2/rt-thread/usb_hc_glue_phytium.c')
|
||||
|
@ -105,6 +105,9 @@ if(CONFIG_CHERRYUSB_DEVICE)
|
||||
elseif("${CONFIG_CHERRYUSB_DEVICE_DCD}" STREQUAL "chipidea_mcx")
|
||||
list(APPEND cherryusb_srcs ${CMAKE_CURRENT_LIST_DIR}/port/chipidea/usb_dc_chipidea.c)
|
||||
list(APPEND cherryusb_srcs ${CMAKE_CURRENT_LIST_DIR}/port/chipidea/usb_glue_mcx.c)
|
||||
elseif("${CONFIG_CHERRYUSB_DEVICE_DCD}" STREQUAL "kinetis_mcx")
|
||||
list(APPEND cherryusb_srcs ${CMAKE_CURRENT_LIST_DIR}/port/kinetis/usb_dc_kinetis.c)
|
||||
list(APPEND cherryusb_srcs ${CMAKE_CURRENT_LIST_DIR}/port/kinetis/usb_glue_mcx.c)
|
||||
elseif("${CONFIG_CHERRYUSB_DEVICE_DCD}" STREQUAL "hpm")
|
||||
list(APPEND cherryusb_srcs ${CMAKE_CURRENT_LIST_DIR}/port/hpm/usb_dc_hpm.c)
|
||||
elseif("${CONFIG_CHERRYUSB_DEVICE_DCD}" STREQUAL "bl")
|
||||
@ -260,6 +263,9 @@ if(CONFIG_CHERRYUSB_HOST)
|
||||
elseif("${CONFIG_CHERRYUSB_HOST_HCD}" STREQUAL "musb_bk")
|
||||
list(APPEND cherryusb_srcs ${CMAKE_CURRENT_LIST_DIR}/port/musb/usb_hc_musb.c)
|
||||
list(APPEND cherryusb_srcs ${CMAKE_CURRENT_LIST_DIR}/port/musb/usb_glue_bk.c)
|
||||
elseif("${CONFIG_CHERRYUSB_HOST_HCD}" STREQUAL "kinetis_mcx")
|
||||
list(APPEND cherryusb_srcs ${CMAKE_CURRENT_LIST_DIR}/port/kinetis/usb_hc_kinetis.c)
|
||||
list(APPEND cherryusb_srcs ${CMAKE_CURRENT_LIST_DIR}/port/kinetis/usb_glue_mcx.c)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
|
18
port/kinetis/README.md
Normal file
18
port/kinetis/README.md
Normal file
@ -0,0 +1,18 @@
|
||||
# Note
|
||||
|
||||
## Support Chip List
|
||||
|
||||
### NXP
|
||||
|
||||
Modify USB_NOCACHE_RAM_SECTION
|
||||
|
||||
```
|
||||
#define USB_NOCACHE_RAM_SECTION __attribute__((section(".NonCacheable")))
|
||||
```
|
||||
|
||||
- MCXC/MCXA (device only)
|
||||
- MCXN
|
||||
|
||||
### MM32
|
||||
|
||||
- MM32F3/MM32F5
|
448
port/kinetis/usb_dc_kinetis.c
Normal file
448
port/kinetis/usb_dc_kinetis.c
Normal file
@ -0,0 +1,448 @@
|
||||
/*
|
||||
* Copyright (c) 2024, sakumisu
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#include "usbd_core.h"
|
||||
#include "usb_kinetis_reg.h"
|
||||
|
||||
#define USB_OTG_DEV ((KINETIS_TypeDef *)g_usbdev_bus[busid].reg_base)
|
||||
|
||||
/* Endpoint state */
|
||||
struct kinetis_ep_state {
|
||||
uint16_t ep_mps; /* Endpoint max packet size */
|
||||
uint8_t ep_type; /* Endpoint type */
|
||||
uint8_t ep_stalled; /* Endpoint stall flag */
|
||||
uint8_t ep_enable; /* Endpoint enable */
|
||||
bool ep_odd; /* Endpoint odd */
|
||||
uint8_t *xfer_buf;
|
||||
uint32_t xfer_len;
|
||||
uint32_t actual_xfer_len;
|
||||
};
|
||||
|
||||
/* Driver state */
|
||||
struct kinetis_udc {
|
||||
uint8_t dev_addr;
|
||||
struct kinetis_ep_state in_ep[CONFIG_USBDEV_EP_NUM]; /*!< IN endpoint parameters*/
|
||||
struct kinetis_ep_state out_ep[CONFIG_USBDEV_EP_NUM]; /*!< OUT endpoint parameters */
|
||||
} g_kinetis_udc[CONFIG_USBDEV_MAX_BUS];
|
||||
|
||||
USB_NOCACHE_RAM_SECTION __attribute__((aligned(512))) kinetis_bd_table_t g_kinetis_bdt[CONFIG_USBDEV_MAX_BUS];
|
||||
USB_NOCACHE_RAM_SECTION __attribute__((aligned(32))) uint8_t setup_packet[CONFIG_USBDEV_MAX_BUS][8];
|
||||
|
||||
static int kinetis_start_transfer(uint8_t busid, uint8_t ep, uint8_t *buffer, uint16_t buflen)
|
||||
{
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
uint8_t dir;
|
||||
uint8_t odd;
|
||||
uint16_t ep_mps;
|
||||
kinetis_bd_t *bd;
|
||||
kinetis_bd_t *next;
|
||||
|
||||
if (USB_EP_DIR_IS_OUT(ep)) {
|
||||
dir = 0;
|
||||
odd = g_kinetis_udc[busid].out_ep[ep_idx].ep_odd;
|
||||
ep_mps = g_kinetis_udc[busid].out_ep[ep_idx].ep_mps;
|
||||
} else {
|
||||
dir = 1;
|
||||
odd = g_kinetis_udc[busid].in_ep[ep_idx].ep_odd;
|
||||
ep_mps = g_kinetis_udc[busid].in_ep[ep_idx].ep_mps;
|
||||
}
|
||||
|
||||
bd = &g_kinetis_bdt[busid].table[ep_idx][dir][odd];
|
||||
|
||||
if (bd->own) {
|
||||
USB_LOG_INFO("ep%02x is busy\r\n", ep);
|
||||
return -1;
|
||||
}
|
||||
|
||||
bd->bc = buflen >= ep_mps ? ep_mps : buflen;
|
||||
bd->addr = (uint32_t)buffer;
|
||||
bd->own = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void kinetis_read_setup(uint8_t busid)
|
||||
{
|
||||
uint8_t out_odd = g_kinetis_udc[busid].out_ep[0].ep_odd;
|
||||
uint8_t in_odd = g_kinetis_udc[busid].in_ep[0].ep_odd;
|
||||
|
||||
if (g_kinetis_bdt[busid].table[0][0][out_odd].own) {
|
||||
USB_LOG_INFO("ep0 is busy\r\n");
|
||||
return;
|
||||
}
|
||||
|
||||
g_kinetis_bdt[busid].table[0][0][out_odd].data = 0;
|
||||
g_kinetis_bdt[busid].table[0][0][out_odd ^ 1].data = 1;
|
||||
g_kinetis_bdt[busid].table[0][1][in_odd].data = 1;
|
||||
g_kinetis_bdt[busid].table[0][1][in_odd ^ 1].data = 0;
|
||||
|
||||
kinetis_start_transfer(busid, USB_CONTROL_OUT_EP0, setup_packet[busid], 8);
|
||||
}
|
||||
|
||||
__WEAK void usb_dc_low_level_init(uint8_t busid)
|
||||
{
|
||||
}
|
||||
|
||||
__WEAK void usb_dc_low_level_deinit(uint8_t busid)
|
||||
{
|
||||
}
|
||||
|
||||
int usb_dc_init(uint8_t busid)
|
||||
{
|
||||
usb_dc_low_level_init(busid);
|
||||
|
||||
memset(&g_kinetis_udc[busid], 0, sizeof(g_kinetis_udc[busid]));
|
||||
|
||||
USB_OTG_DEV->BDTPAGE1 = (uint8_t)((uintptr_t)&g_kinetis_bdt[busid] >> 8);
|
||||
USB_OTG_DEV->BDTPAGE2 = (uint8_t)((uintptr_t)&g_kinetis_bdt[busid] >> 16);
|
||||
USB_OTG_DEV->BDTPAGE3 = (uint8_t)((uintptr_t)&g_kinetis_bdt[busid] >> 24);
|
||||
|
||||
USB_OTG_DEV->INTEN = USB_INTEN_USBRSTEN_MASK | USB_INTEN_TOKDNEEN_MASK |
|
||||
USB_INTEN_SLEEPEN_MASK | USB_INTEN_RESUMEEN_MASK |
|
||||
USB_INTEN_ERROREN_MASK;
|
||||
|
||||
USB_OTG_DEV->CTL |= USB_CTL_USBENSOFEN_MASK;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int usb_dc_deinit(uint8_t busid)
|
||||
{
|
||||
usb_dc_low_level_deinit(busid);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int usbd_set_address(uint8_t busid, const uint8_t addr)
|
||||
{
|
||||
g_kinetis_udc[busid].dev_addr = addr;
|
||||
|
||||
if (addr == 0) {
|
||||
USB_OTG_DEV->ADDR = 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int usbd_set_remote_wakeup(uint8_t busid)
|
||||
{
|
||||
USB_OTG_DEV->CTL |= USB_CTL_RESUME_MASK;
|
||||
|
||||
usbd_kinetis_delay_ms(10);
|
||||
|
||||
USB_OTG_DEV->CTL &= ~USB_CTL_RESUME_MASK;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t usbd_get_port_speed(uint8_t busid)
|
||||
{
|
||||
return USB_SPEED_FULL;
|
||||
}
|
||||
|
||||
int usbd_ep_open(uint8_t busid, const struct usb_endpoint_descriptor *ep)
|
||||
{
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep->bEndpointAddress);
|
||||
uint8_t odd;
|
||||
uint8_t dir;
|
||||
kinetis_bd_t *bd;
|
||||
uint8_t regval;
|
||||
|
||||
/* Must not exceed max endpoint number */
|
||||
if (ep_idx >= CONFIG_USBDEV_EP_NUM) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (USB_EP_DIR_IS_OUT(ep->bEndpointAddress)) {
|
||||
g_kinetis_udc[busid].out_ep[ep_idx].ep_mps = USB_GET_MAXPACKETSIZE(ep->wMaxPacketSize);
|
||||
g_kinetis_udc[busid].out_ep[ep_idx].ep_type = USB_GET_ENDPOINT_TYPE(ep->bmAttributes);
|
||||
g_kinetis_udc[busid].out_ep[ep_idx].ep_enable = true;
|
||||
|
||||
dir = 0;
|
||||
odd = g_kinetis_udc[busid].out_ep[ep_idx].ep_odd;
|
||||
} else {
|
||||
g_kinetis_udc[busid].in_ep[ep_idx].ep_mps = USB_GET_MAXPACKETSIZE(ep->wMaxPacketSize);
|
||||
g_kinetis_udc[busid].in_ep[ep_idx].ep_type = USB_GET_ENDPOINT_TYPE(ep->bmAttributes);
|
||||
g_kinetis_udc[busid].in_ep[ep_idx].ep_enable = true;
|
||||
|
||||
dir = 1;
|
||||
odd = g_kinetis_udc[busid].in_ep[ep_idx].ep_odd;
|
||||
}
|
||||
|
||||
if (ep_idx != 0) {
|
||||
regval = USB_ENDPT_EPCTLDIS_MASK;
|
||||
regval |= (USB_GET_ENDPOINT_TYPE(ep->bmAttributes) != USB_ENDPOINT_TYPE_ISOCHRONOUS) ? USB_ENDPT_EPHSHK_MASK : 0;
|
||||
regval |= dir ? USB_ENDPT_EPTXEN_MASK : USB_ENDPT_EPRXEN_MASK;
|
||||
USB_OTG_DEV->ENDPOINT[ep_idx].ENDPT |= regval;
|
||||
|
||||
if (USB_GET_ENDPOINT_TYPE(ep->bmAttributes) != USB_ENDPOINT_TYPE_ISOCHRONOUS) {
|
||||
bd = &g_kinetis_bdt[busid].table[ep_idx][dir][odd];
|
||||
bd->dts = 1;
|
||||
bd->data = 0;
|
||||
|
||||
bd = &g_kinetis_bdt[busid].table[ep_idx][dir][odd ^ 1];
|
||||
bd->dts = 1;
|
||||
bd->data = 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int usbd_ep_close(uint8_t busid, const uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
uint8_t dir;
|
||||
kinetis_bd_t *bd;
|
||||
|
||||
if (USB_EP_DIR_IS_OUT(ep)) {
|
||||
g_kinetis_udc[busid].out_ep[ep_idx].ep_enable = false;
|
||||
dir = 0;
|
||||
} else {
|
||||
g_kinetis_udc[busid].in_ep[ep_idx].ep_enable = false;
|
||||
dir = 1;
|
||||
}
|
||||
|
||||
bd = &g_kinetis_bdt[busid].table[ep_idx][dir][0];
|
||||
bd->head = 0;
|
||||
|
||||
bd = &g_kinetis_bdt[busid].table[ep_idx][dir][1];
|
||||
bd->head = 0;
|
||||
|
||||
USB_OTG_DEV->ENDPOINT[ep_idx].ENDPT &= ~(dir ? USB_ENDPT_EPTXEN_MASK : USB_ENDPT_EPRXEN_MASK);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int usbd_ep_set_stall(uint8_t busid, const uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
kinetis_bd_t *bd;
|
||||
uint8_t odd;
|
||||
uint8_t dir;
|
||||
|
||||
if (0 == ep_idx) {
|
||||
USB_OTG_DEV->ENDPOINT[ep_idx].ENDPT |= USB_ENDPT_EPSTALL_MASK;
|
||||
|
||||
if (ep_idx == 0) {
|
||||
kinetis_read_setup(busid);
|
||||
}
|
||||
} else {
|
||||
if (USB_EP_DIR_IS_OUT(ep)) {
|
||||
dir = 0;
|
||||
odd = g_kinetis_udc[busid].out_ep[ep_idx].ep_odd;
|
||||
} else {
|
||||
dir = 1;
|
||||
odd = g_kinetis_udc[busid].in_ep[ep_idx].ep_odd;
|
||||
}
|
||||
|
||||
bd = &g_kinetis_bdt[busid].table[ep_idx][dir][odd];
|
||||
|
||||
bd->bdt_stall = 1;
|
||||
bd->own = 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int usbd_ep_clear_stall(uint8_t busid, const uint8_t ep)
|
||||
{
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
kinetis_bd_t *bd;
|
||||
uint8_t odd;
|
||||
uint8_t dir;
|
||||
|
||||
if (USB_EP_DIR_IS_OUT(ep)) {
|
||||
dir = 0;
|
||||
odd = g_kinetis_udc[busid].out_ep[ep_idx].ep_odd;
|
||||
} else {
|
||||
dir = 1;
|
||||
odd = g_kinetis_udc[busid].in_ep[ep_idx].ep_odd;
|
||||
}
|
||||
|
||||
bd = &g_kinetis_bdt[busid].table[ep_idx][dir][odd];
|
||||
|
||||
bd->own = 0;
|
||||
bd->bdt_stall = 0;
|
||||
bd->data = 0;
|
||||
|
||||
bd = &g_kinetis_bdt[busid].table[ep_idx][dir][odd ^ 1];
|
||||
bd->data = 1;
|
||||
|
||||
uint8_t regval = USB_OTG_DEV->ENDPOINT[ep_idx].ENDPT;
|
||||
if (regval & USB_ENDPT_EPSTALL_MASK) {
|
||||
USB_OTG_DEV->ENDPOINT[ep_idx].ENDPT = regval & ~USB_ENDPT_EPSTALL_MASK;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int usbd_ep_is_stalled(uint8_t busid, const uint8_t ep, uint8_t *stalled)
|
||||
{
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
uint8_t regval = USB_OTG_DEV->ENDPOINT[ep_idx].ENDPT;
|
||||
if (regval & USB_ENDPT_EPSTALL_MASK) {
|
||||
*stalled = 1;
|
||||
} else {
|
||||
*stalled = 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int usbd_ep_start_write(uint8_t busid, const uint8_t ep, const uint8_t *data,
|
||||
uint32_t data_len)
|
||||
{
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
if (!data && data_len) {
|
||||
return -1;
|
||||
}
|
||||
if (!g_kinetis_udc[busid].in_ep[ep_idx].ep_enable) {
|
||||
return -2;
|
||||
}
|
||||
|
||||
g_kinetis_udc[busid].in_ep[ep_idx].xfer_buf = (uint8_t *)data;
|
||||
g_kinetis_udc[busid].in_ep[ep_idx].xfer_len = data_len;
|
||||
g_kinetis_udc[busid].in_ep[ep_idx].actual_xfer_len = 0;
|
||||
|
||||
return kinetis_start_transfer(busid, ep, (uint8_t *)data, MIN(data_len, g_kinetis_udc[busid].in_ep[ep_idx].ep_mps));
|
||||
}
|
||||
|
||||
int usbd_ep_start_read(uint8_t busid, const uint8_t ep, uint8_t *data,
|
||||
uint32_t data_len)
|
||||
{
|
||||
uint8_t ep_idx = USB_EP_GET_IDX(ep);
|
||||
|
||||
if (!data && data_len) {
|
||||
return -1;
|
||||
}
|
||||
if (!g_kinetis_udc[busid].out_ep[ep_idx].ep_enable) {
|
||||
return -2;
|
||||
}
|
||||
|
||||
g_kinetis_udc[busid].out_ep[ep_idx].xfer_buf = (uint8_t *)data;
|
||||
g_kinetis_udc[busid].out_ep[ep_idx].xfer_len = data_len;
|
||||
g_kinetis_udc[busid].out_ep[ep_idx].actual_xfer_len = 0;
|
||||
|
||||
return kinetis_start_transfer(busid, ep, (uint8_t *)data, MIN(data_len, g_kinetis_udc[busid].out_ep[ep_idx].ep_mps));
|
||||
}
|
||||
|
||||
void USBD_IRQHandler(uint8_t busid)
|
||||
{
|
||||
uint8_t s;
|
||||
uint8_t pid;
|
||||
uint8_t ep_idx;
|
||||
uint8_t dir;
|
||||
uint8_t odd;
|
||||
uint16_t bc;
|
||||
uint8_t is = USB_OTG_DEV->ISTAT;
|
||||
uint8_t mask = USB_OTG_DEV->INTEN;
|
||||
kinetis_bd_t *bd;
|
||||
|
||||
USB_OTG_DEV->ISTAT = is & ~mask;
|
||||
is &= mask;
|
||||
|
||||
if (is & USB_ISTAT_ERROR_MASK) {
|
||||
uint32_t es = USB_OTG_DEV->ERRSTAT;
|
||||
USB_OTG_DEV->ERRSTAT = es;
|
||||
USB_OTG_DEV->ISTAT = is;
|
||||
}
|
||||
|
||||
if (is & USB_ISTAT_USBRST_MASK) {
|
||||
USB_OTG_DEV->ISTAT = is;
|
||||
USB_OTG_DEV->CTL |= USB_CTL_ODDRST_MASK;
|
||||
USB_OTG_DEV->ADDR = 0;
|
||||
|
||||
USB_OTG_DEV->ENDPOINT[0].ENDPT = USB_ENDPT_EPHSHK_MASK | USB_ENDPT_EPRXEN_MASK | USB_ENDPT_EPTXEN_MASK;
|
||||
for (uint8_t i = 1; i < 16; i++) {
|
||||
USB_OTG_DEV->ENDPOINT[i].ENDPT = 0;
|
||||
}
|
||||
|
||||
memset(&g_kinetis_bdt[busid], 0, sizeof(g_kinetis_bdt[busid]));
|
||||
memset(g_kinetis_udc[busid].in_ep, 0, sizeof(struct kinetis_ep_state) * CONFIG_USBDEV_EP_NUM);
|
||||
memset(g_kinetis_udc[busid].out_ep, 0, sizeof(struct kinetis_ep_state) * CONFIG_USBDEV_EP_NUM);
|
||||
usbd_event_reset_handler(busid);
|
||||
|
||||
kinetis_read_setup(busid);
|
||||
|
||||
USB_OTG_DEV->CTL &= ~USB_CTL_ODDRST_MASK;
|
||||
}
|
||||
|
||||
if (is & USB_ISTAT_SLEEP_MASK) {
|
||||
USB_OTG_DEV->ISTAT = USB_ISTAT_SLEEP_MASK;
|
||||
}
|
||||
|
||||
if (is & USB_ISTAT_RESUME_MASK) {
|
||||
USB_OTG_DEV->ISTAT = USB_ISTAT_RESUME_MASK;
|
||||
}
|
||||
|
||||
if (is & USB_ISTAT_SOFTOK_MASK) {
|
||||
USB_OTG_DEV->ISTAT = USB_ISTAT_SOFTOK_MASK;
|
||||
}
|
||||
|
||||
if (is & USB_ISTAT_STALL_MASK) {
|
||||
USB_OTG_DEV->ISTAT = USB_ISTAT_STALL_MASK;
|
||||
}
|
||||
|
||||
if (is & USB_ISTAT_TOKDNE_MASK) {
|
||||
s = USB_OTG_DEV->STAT;
|
||||
USB_OTG_DEV->ISTAT = USB_ISTAT_TOKDNE_MASK; /* must be cleared after get STAT */
|
||||
|
||||
ep_idx = (s & USB_STAT_ENDP_MASK) >> USB_STAT_ENDP_SHIFT;
|
||||
dir = (s & USB_STAT_TX_MASK) >> USB_STAT_TX_SHIFT;
|
||||
odd = (s & USB_STAT_ODD_MASK) >> USB_STAT_ODD_SHIFT;
|
||||
|
||||
bd = &g_kinetis_bdt[busid].table[ep_idx][dir][odd];
|
||||
|
||||
pid = bd->tok_pid;
|
||||
bc = bd->bc;
|
||||
|
||||
bd->bdt_stall = 0;
|
||||
bd->dts = 1;
|
||||
bd->ninc = 0;
|
||||
bd->keep = 0;
|
||||
|
||||
if (dir) {
|
||||
g_kinetis_udc[busid].in_ep[ep_idx].ep_odd = odd ^ 1;
|
||||
} else {
|
||||
g_kinetis_udc[busid].out_ep[ep_idx].ep_odd = odd ^ 1;
|
||||
}
|
||||
|
||||
if (pid == USB_TOKEN_PID_SETUP) {
|
||||
USB_OTG_DEV->CTL &= ~USB_CTL_TXSUSPENDTOKENBUSY_MASK;
|
||||
usbd_event_ep0_setup_complete_handler(busid, (uint8_t *)bd->addr);
|
||||
return;
|
||||
}
|
||||
|
||||
if (dir) {
|
||||
g_kinetis_udc[busid].in_ep[ep_idx].xfer_buf += bc;
|
||||
g_kinetis_udc[busid].in_ep[ep_idx].xfer_len -= bc;
|
||||
g_kinetis_udc[busid].in_ep[ep_idx].actual_xfer_len += bc;
|
||||
|
||||
if (g_kinetis_udc[busid].in_ep[ep_idx].xfer_len == 0) {
|
||||
usbd_event_ep_in_complete_handler(busid, ep_idx | 0x80, g_kinetis_udc[busid].in_ep[ep_idx].actual_xfer_len);
|
||||
} else {
|
||||
kinetis_start_transfer(busid, ep_idx | 0x80, g_kinetis_udc[busid].in_ep[ep_idx].xfer_buf,
|
||||
MIN(g_kinetis_udc[busid].in_ep[ep_idx].xfer_len, g_kinetis_udc[busid].in_ep[ep_idx].ep_mps));
|
||||
}
|
||||
} else {
|
||||
g_kinetis_udc[busid].out_ep[ep_idx].xfer_buf += bc;
|
||||
g_kinetis_udc[busid].out_ep[ep_idx].xfer_len -= bc;
|
||||
g_kinetis_udc[busid].out_ep[ep_idx].actual_xfer_len += bc;
|
||||
|
||||
if ((bc < g_kinetis_udc[busid].out_ep[ep_idx].ep_mps) || (g_kinetis_udc[busid].out_ep[ep_idx].xfer_len == 0)) {
|
||||
usbd_event_ep_out_complete_handler(busid, ep_idx, g_kinetis_udc[busid].out_ep[ep_idx].actual_xfer_len);
|
||||
} else {
|
||||
kinetis_start_transfer(busid, ep_idx, g_kinetis_udc[busid].out_ep[ep_idx].xfer_buf,
|
||||
MIN(g_kinetis_udc[busid].out_ep[ep_idx].xfer_len, g_kinetis_udc[busid].out_ep[ep_idx].ep_mps));
|
||||
}
|
||||
}
|
||||
|
||||
if ((bc == 0) && (ep_idx == 0)) {
|
||||
if ((g_kinetis_udc[busid].dev_addr > 0) && dir) {
|
||||
USB_OTG_DEV->ADDR = g_kinetis_udc[busid].dev_addr;
|
||||
g_kinetis_udc[busid].dev_addr = 0;
|
||||
}
|
||||
|
||||
kinetis_read_setup(busid);
|
||||
}
|
||||
}
|
||||
}
|
51
port/kinetis/usb_glue_mcx.c
Normal file
51
port/kinetis/usb_glue_mcx.c
Normal file
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) 2024, sakumisu
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#include "usbd_core.h"
|
||||
#include "fsl_common.h"
|
||||
#include "usb_kinetis_reg.h"
|
||||
|
||||
#define USB_OTG_DEV ((KINETIS_MCX_TypeDef *)g_usbdev_bus[busid].reg_base)
|
||||
|
||||
void USB0_FS_IRQHandler(void)
|
||||
{
|
||||
extern void USBD_IRQHandler(uint8_t busid);
|
||||
USBD_IRQHandler(0);
|
||||
}
|
||||
|
||||
void USB_ClockInit(void)
|
||||
{
|
||||
CLOCK_AttachClk(kCLK_48M_to_USB0);
|
||||
CLOCK_EnableClock(kCLOCK_Usb0Ram);
|
||||
CLOCK_EnableClock(kCLOCK_Usb0Fs);
|
||||
CLOCK_EnableUsbfsClock();
|
||||
}
|
||||
|
||||
void usb_dc_low_level_init(uint8_t busid)
|
||||
{
|
||||
USB_ClockInit();
|
||||
/* Install isr, set priority, and enable IRQ. */
|
||||
NVIC_SetPriority((IRQn_Type)USB0_FS_IRQn, 3);
|
||||
EnableIRQ((IRQn_Type)USB0_FS_IRQn);
|
||||
|
||||
USB_OTG_DEV->USBTRC0 |= USB_USBTRC0_USBRESET_MASK;
|
||||
while (USB_OTG_DEV->USBTRC0 & USB_USBTRC0_USBRESET_MASK)
|
||||
;
|
||||
|
||||
USB_OTG_DEV->USBTRC0 |= USB_USBTRC0_VREGIN_STS(1); /* software must set this bit to 1 */
|
||||
USB_OTG_DEV->USBCTRL = 0;
|
||||
USB_OTG_DEV->CONTROL |= USB_CONTROL_DPPULLUPNONOTG_MASK;
|
||||
}
|
||||
|
||||
void usb_dc_low_level_deinit(uint8_t busid)
|
||||
{
|
||||
USB_OTG_DEV->CONTROL &= ~USB_CONTROL_DPPULLUPNONOTG_MASK;
|
||||
DisableIRQ((IRQn_Type)USB0_FS_IRQn);
|
||||
}
|
||||
|
||||
void usbd_kinetis_delay_ms(uint8_t ms)
|
||||
{
|
||||
|
||||
}
|
1
port/kinetis/usb_hc_kinetis.c
Normal file
1
port/kinetis/usb_hc_kinetis.c
Normal file
@ -0,0 +1 @@
|
||||
TODO
|
1487
port/kinetis/usb_kinetis_reg.h
Normal file
1487
port/kinetis/usb_kinetis_reg.h
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user