Merge pull request #280 from majbthrd/nuc505

add Nuvoton NUC505
This commit is contained in:
Ha Thach 2020-03-03 21:35:06 +07:00 committed by GitHub
commit 048f5e34dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 1021 additions and 12 deletions

View File

@ -38,7 +38,7 @@ The stack supports the following MCUs:
- **Sony:** CXD56
- **ST:** STM32 series: L0, F0, F1, F2, F3, F4, F7, H7 (device only)
- **[ValentyUSB](https://github.com/im-tomu/valentyusb)** eptri
- **Nuvoton:** NUC121/NUC125, NUC126
- **Nuvoton:** NUC121/NUC125, NUC126, NUC505
[Here is the list of supported Boards](docs/boards.md) that can be used with provided examples.

View File

@ -39,7 +39,7 @@
#error CFG_TUSB_MCU must be defined
#endif
#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX
#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX || CFG_TUSB_MCU == OPT_MCU_NUC505
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_HIGH_SPEED)
#else
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE

View File

@ -39,7 +39,7 @@
#error CFG_TUSB_MCU must be defined
#endif
#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX
#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX || CFG_TUSB_MCU == OPT_MCU_NUC505
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_HIGH_SPEED)
#else
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE

View File

@ -39,7 +39,7 @@
#error CFG_TUSB_MCU must be defined
#endif
#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX
#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX || CFG_TUSB_MCU == OPT_MCU_NUC505
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_HIGH_SPEED)
#else
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE

View File

@ -21,7 +21,7 @@
#error CFG_TUSB_MCU must be defined
#endif
#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX
#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX || CFG_TUSB_MCU == OPT_MCU_NUC505
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_HIGH_SPEED)
#else
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE

View File

@ -39,7 +39,7 @@
#error CFG_TUSB_MCU must be defined
#endif
#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX
#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX || CFG_TUSB_MCU == OPT_MCU_NUC505
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_HIGH_SPEED)
#else
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE

View File

@ -39,7 +39,7 @@
#error CFG_TUSB_MCU must be defined
#endif
#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX
#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX || CFG_TUSB_MCU == OPT_MCU_NUC505
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_HIGH_SPEED)
#else
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE

View File

@ -39,7 +39,7 @@
#error CFG_TUSB_MCU must be defined
#endif
#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX
#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX || CFG_TUSB_MCU == OPT_MCU_NUC505
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_HIGH_SPEED)
#else
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE

View File

@ -39,7 +39,7 @@
#error CFG_TUSB_MCU must be defined
#endif
#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX
#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX || CFG_TUSB_MCU == OPT_MCU_NUC505
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_HIGH_SPEED)
#else
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE

View File

@ -21,7 +21,7 @@
#error CFG_TUSB_MCU must be defined
#endif
#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX
#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX || CFG_TUSB_MCU == OPT_MCU_NUC505
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_HIGH_SPEED)
#else
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE

View File

@ -39,7 +39,7 @@
#error CFG_TUSB_MCU must be defined
#endif
#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX
#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX || CFG_TUSB_MCU == OPT_MCU_NUC505
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_HIGH_SPEED)
#else
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE

View File

@ -0,0 +1,52 @@
CFLAGS += \
-flto \
-mthumb \
-mabi=aapcs-linux \
-mcpu=cortex-m4 \
-DCFG_TUSB_MCU=OPT_MCU_NUC505
# All source paths should be relative to the top level.
LD_FILE = hw/bsp/nutiny_sdk_nuc505/nuc505_flashtoram.ld
SRC_C += \
hw/mcu/nuvoton/nuc505/Device/Nuvoton/NUC505Series/Source/system_NUC505Series.c \
hw/mcu/nuvoton/nuc505/StdDriver/src/adc.c \
hw/mcu/nuvoton/nuc505/StdDriver/src/clk.c \
hw/mcu/nuvoton/nuc505/StdDriver/src/gpio.c \
hw/mcu/nuvoton/nuc505/StdDriver/src/i2c.c \
hw/mcu/nuvoton/nuc505/StdDriver/src/i2s.c \
hw/mcu/nuvoton/nuc505/StdDriver/src/pwm.c \
hw/mcu/nuvoton/nuc505/StdDriver/src/rtc.c \
hw/mcu/nuvoton/nuc505/StdDriver/src/sd.c \
hw/mcu/nuvoton/nuc505/StdDriver/src/spi.c \
hw/mcu/nuvoton/nuc505/StdDriver/src/spim.c \
hw/mcu/nuvoton/nuc505/StdDriver/src/sys.c \
hw/mcu/nuvoton/nuc505/StdDriver/src/timer.c \
hw/mcu/nuvoton/nuc505/StdDriver/src/uart.c \
hw/mcu/nuvoton/nuc505/StdDriver/src/usbd.c \
hw/mcu/nuvoton/nuc505/StdDriver/src/wdt.c \
hw/mcu/nuvoton/nuc505/StdDriver/src/wwdt.c
SRC_S += \
hw/mcu/nuvoton/nuc505/Device/Nuvoton/NUC505Series/Source/GCC/startup_NUC505Series.S
INC += \
$(TOP)/hw/mcu/nuvoton/nuc505/Device/Nuvoton/NUC505Series/Include \
$(TOP)/hw/mcu/nuvoton/nuc505/StdDriver/inc \
$(TOP)/hw/mcu/nuvoton/nuc505/CMSIS/Include
# For TinyUSB port source
VENDOR = nuvoton
CHIP_FAMILY = nuc505
# For freeRTOS port source
FREERTOS_PORT = ARM_CM4
# For flash-jlink target
JLINK_DEVICE = NUC505YO13Y
JLINK_IF = swd
# Flash using Nuvoton's openocd fork at https://github.com/OpenNuvoton/OpenOCD-Nuvoton
# Please compile and install it from github source
flash: $(BUILD)/$(BOARD)-firmware.elf
openocd -f interface/nulink.cfg -f target/numicroM4.cfg -c "program $< reset exit"

View File

@ -0,0 +1,199 @@
/* Linker script to configure memory regions. */
MEMORY
{
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x80000 /* 512k */
RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x20000 /* 128k */
}
/* Library configurations */
GROUP(libgcc.a libc.a libm.a libnosys.a)
/* Linker script to place sections and symbol values. Should be used together
* with other linker script that defines memory regions FLASH and RAM.
* It references following symbols, which must be defined in code:
* Reset_Handler : Entry of reset handler
*
* It defines following symbols, which code can use without definition:
* __exidx_start
* __exidx_end
* __copy_table_start__
* __copy_table_end__
* __zero_table_start__
* __zero_table_end__
* __etext
* __data_start__
* __preinit_array_start
* __preinit_array_end
* __init_array_start
* __init_array_end
* __fini_array_start
* __fini_array_end
* __data_end__
* __bss_start__
* __bss_end__
* __end__
* end
* __HeapLimit
* __StackLimit
* __StackTop
* __stack
* __Vectors_End
* __Vectors_Size
*/
ENTRY(Reset_Handler)
SECTIONS
{
.startup :
{
KEEP(*(.vectors))
__Vectors_End = .;
__Vectors_Size = __Vectors_End - __Vectors;
__end__ = .;
KEEP(*(.preinit))
KEEP(*(.init))
KEEP(*(.fini))
} > FLASH
.ARM.extab :
{
*(.ARM.extab* .gnu.linkonce.armextab.*)
} > FLASH
__exidx_start = .;
.ARM.exidx :
{
*(.ARM.exidx* .gnu.linkonce.armexidx.*)
} > FLASH
__exidx_end = .;
/* To copy multiple ROM to RAM sections,
* uncomment .copy.table section and,
* define __STARTUP_COPY_MULTIPLE in startup_ARMCMx.S */
/*
.copy.table :
{
. = ALIGN(4);
__copy_table_start__ = .;
LONG (__etext)
LONG (__data_start__)
LONG (__data_end__ - __data_start__)
LONG (__etext2)
LONG (__data2_start__)
LONG (__data2_end__ - __data2_start__)
__copy_table_end__ = .;
} > FLASH
*/
/* To clear multiple BSS sections,
* uncomment .zero.table section and,
* define __STARTUP_CLEAR_BSS_MULTIPLE in startup_ARMCMx.S */
/*
.zero.table :
{
. = ALIGN(4);
__zero_table_start__ = .;
LONG (__bss_start__)
LONG (__bss_end__ - __bss_start__)
LONG (__bss2_start__)
LONG (__bss2_end__ - __bss2_start__)
__zero_table_end__ = .;
} > FLASH
*/
__etext = .;
.data : AT (__etext)
{
__data_start__ = .;
*(.text*)
/* .ctors */
*crtbegin.o(.ctors)
*crtbegin?.o(.ctors)
*(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors)
*(SORT(.ctors.*))
*(.ctors)
/* .dtors */
*crtbegin.o(.dtors)
*crtbegin?.o(.dtors)
*(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors)
*(SORT(.dtors.*))
*(.dtors)
*(.rodata*)
KEEP(*(.eh_frame*))
*(vtable)
*(.data*)
. = ALIGN(4);
/* preinit data */
PROVIDE_HIDDEN (__preinit_array_start = .);
KEEP(*(.preinit_array))
PROVIDE_HIDDEN (__preinit_array_end = .);
. = ALIGN(4);
/* init data */
PROVIDE_HIDDEN (__init_array_start = .);
KEEP(*(SORT(.init_array.*)))
KEEP(*(.init_array))
PROVIDE_HIDDEN (__init_array_end = .);
. = ALIGN(4);
/* finit data */
PROVIDE_HIDDEN (__fini_array_start = .);
KEEP(*(SORT(.fini_array.*)))
KEEP(*(.fini_array))
PROVIDE_HIDDEN (__fini_array_end = .);
KEEP(*(.jcr*))
. = ALIGN(4);
/* All data end */
__data_end__ = .;
} > RAM
.bss :
{
. = ALIGN(4);
__bss_start__ = .;
*(.bss*)
*(COMMON)
. = ALIGN(4);
__bss_end__ = .;
} > RAM
.heap (COPY):
{
__HeapBase = .;
__end__ = .;
end = __end__;
KEEP(*(.heap*))
__HeapLimit = .;
} > RAM
/* .stack_dummy section doesn't contains any symbols. It is only
* used for linker to calculate size of stack sections, and assign
* values to stack symbols later */
.stack_dummy (COPY):
{
KEEP(*(.stack*))
} > RAM
/* Set stack top to end of RAM, and stack limit move down by
* size of stack_dummy section */
__StackTop = ORIGIN(RAM) + LENGTH(RAM);
__StackLimit = __StackTop - SIZEOF(.stack_dummy);
PROVIDE(__stack = __StackTop);
/* Check if data + heap + stack exceeds RAM limit */
ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed with stack")
}

View File

@ -0,0 +1,118 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Ha Thach (tinyusb.org)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* This file is part of the TinyUSB stack.
*/
#include "bsp/board.h"
#include "NUC505Series.h"
#define LED_PORT PC
#define LED_PIN 3
#define LED_STATE_ON 0
void board_init(void)
{
/* Enable XTAL */
CLK->PWRCTL |= CLK_PWRCTL_HXTEN_Msk;
CLK_SetCoreClock(96000000);
/* Set PCLK divider */
CLK_SetModuleClock(PCLK_MODULE, 0, 1);
/* Update System Core Clock */
SystemCoreClockUpdate();
/* Enable USB IP clock */
CLK_EnableModuleClock(USBD_MODULE);
/* Select USB IP clock source */
CLK_SetModuleClock(USBD_MODULE, CLK_USBD_SRC_EXT, 0);
CLK_SetModuleClock(PCLK_MODULE, 0, 1);
/* Enable PHY */
USBD_ENABLE_PHY();
/* wait PHY clock ready */
while (1) {
USBD->EP[EPA].EPMPS = 0x20;
if (USBD->EP[EPA].EPMPS == 0x20)
break;
}
/* Force SE0, and then clear it to connect*/
USBD_SET_SE0();
#if CFG_TUSB_OS == OPT_OS_NONE
// 1ms tick timer
SysTick_Config(96000000 / 1000);
#endif
GPIO_SetMode(LED_PORT, 1UL << LED_PIN, GPIO_MODE_OUTPUT);
}
#if CFG_TUSB_OS == OPT_OS_NONE
volatile uint32_t system_ticks = 0;
void SysTick_Handler (void)
{
system_ticks++;
}
uint32_t board_millis(void)
{
return system_ticks;
}
#endif
//--------------------------------------------------------------------+
// Board porting API
//--------------------------------------------------------------------+
void board_led_write(bool state)
{
uint32_t current = (state) ? LED_STATE_ON : (1-LED_STATE_ON);
current <<= LED_PIN;
uint32_t irq_state = __get_PRIMASK();
__disable_irq();
current |= LED_PORT->DOUT & ~(1UL << LED_PIN);
LED_PORT->DOUT = current;
__set_PRIMASK(irq_state);
}
uint32_t board_button_read(void)
{
return 0;
}
int board_uart_read(uint8_t* buf, int len)
{
(void) buf; (void) len;
return 0;
}
int board_uart_write(void const * buf, int len)
{
(void) buf; (void) len;
return 0;
}

@ -1 +1 @@
Subproject commit dc96fff794d14818c93ea1d4d760d51a014d70c5
Subproject commit 2204191ec76283371419fbcec207da02e1bc22fa

View File

@ -0,0 +1,639 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2020 Peter Lawrence
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* This file is part of the TinyUSB stack.
*/
/*
Theory of operation:
The NUC505 USBD peripheral has twelve "EP"s, where each is simplex, in addition
to dedicated support for the control endpoint (EP0). The non-user endpoints
are referred to as "user" EPs in this code, and follow the datasheet
nomenclature of EPA through EPL.
*/
#include "tusb_option.h"
#if TUSB_OPT_DEVICE_ENABLED && (CFG_TUSB_MCU == OPT_MCU_NUC505)
#include "device/dcd.h"
#include "NUC505Series.h"
/* rather important info unfortunately not provided by device include files */
#define USBD_BUF_SIZE 2048 /* how much USB buffer space there is */
#define USBD_MAX_DMA_LEN 0x1000 /* max bytes that can be DMAed at one time */
enum ep_enum
{
PERIPH_EPA = 0,
PERIPH_EPB = 1,
PERIPH_EPC = 2,
PERIPH_EPD = 3,
PERIPH_EPE = 4,
PERIPH_EPF = 5,
PERIPH_EPG = 6,
PERIPH_EPH = 7,
PERIPH_EPI = 8,
PERIPH_EPJ = 9,
PERIPH_EPK = 10,
PERIPH_EPL = 11,
PERIPH_MAX_EP,
};
static const uint8_t epcfg_eptype_table[] =
{
[TUSB_XFER_CONTROL] = 0, /* won't happen, since control EPs have dedicated registers */
[TUSB_XFER_ISOCHRONOUS] = 3 << USBD_EPCFG_EPTYPE_Pos,
[TUSB_XFER_BULK] = 1 << USBD_EPCFG_EPTYPE_Pos,
[TUSB_XFER_INTERRUPT] = 2 << USBD_EPCFG_EPTYPE_Pos,
};
static const uint8_t eprspctl_eptype_table[] =
{
[TUSB_XFER_CONTROL] = 0, /* won't happen, since control EPs have dedicated registers */
[TUSB_XFER_ISOCHRONOUS] = 2 << USBD_EPRSPCTL_MODE_Pos, /* Fly Mode */
[TUSB_XFER_BULK] = 0 << USBD_EPRSPCTL_MODE_Pos, /* Auto-Validate Mode */
[TUSB_XFER_INTERRUPT] = 1 << USBD_EPRSPCTL_MODE_Pos, /* Manual-Validate Mode */
};
/* set by dcd_set_address() */
static volatile uint8_t assigned_address;
/* reset by bus_reset(), this is used by dcd_edpt_open() to assign USBD peripheral buffer addresses */
static uint32_t bufseg_addr;
/* RAM table needed to track ongoing transfers performed by dcd_edpt_xfer(), dcd_userEP_in_xfer(), and the ISR */
static struct xfer_ctl_t
{
uint8_t *data_ptr; /* data_ptr tracks where to next copy data to (for OUT) or from (for IN) */
union {
uint16_t in_remaining_bytes; /* for IN endpoints, we track how many bytes are left to transfer */
uint16_t out_bytes_so_far; /* but for OUT endpoints, we track how many bytes we've transferred so far */
};
uint16_t max_packet_size; /* needed since device driver only finds out this at runtime */
uint16_t total_bytes; /* quantity needed to pass as argument to dcd_event_xfer_complete() (for IN endpoints) */
uint8_t ep_addr;
bool dma_requested;
} xfer_table[PERIPH_MAX_EP];
/* in addition to xfer_table, additional bespoke bookkeeping is maintained for control EP0 IN */
static struct
{
uint8_t *data_ptr;
uint16_t in_remaining_bytes;
uint16_t total_bytes;
} ctrl_in_xfer;
static volatile bool configuration_changed;
static volatile struct xfer_ctl_t *current_dma_xfer;
/*
local helper functions
*/
static void usb_attach(void)
{
USBD->PHYCTL |= USBD_PHYCTL_DPPUEN_Msk;
}
static void usb_control_send_zlp(void)
{
USBD->CEPINTSTS = USBD_CEPINTSTS_STSDONEIF_Msk;
USBD->CEPCTL = 0; /* clear NAKCLR bit */
USBD->CEPINTEN = USBD_CEPINTEN_STSDONEIEN_Msk;
}
/* map 8-bit ep_addr into peripheral endpoint index (PERIPH_EPA...) */
static USBD_EP_T *ep_entry(uint8_t ep_addr, bool add)
{
USBD_EP_T *ep;
enum ep_enum ep_index;
struct xfer_ctl_t *xfer;
for (ep_index = PERIPH_EPA, xfer = &xfer_table[PERIPH_EPA], ep = USBD->EP; ep_index < PERIPH_MAX_EP; ep_index++, xfer++, ep++)
{
if (add)
{
/* take first peripheral endpoint that is unused */
if (0 == (ep->EPCFG & USBD_EPCFG_EPEN_Msk)) return ep;
}
else
{
/* find a peripheral endpoint that matches ep_addr */
if (xfer->ep_addr == ep_addr) return ep;
}
}
return NULL;
}
/* perform a non-control IN endpoint transfer; this is called by the ISR */
static void dcd_userEP_in_xfer(struct xfer_ctl_t *xfer, USBD_EP_T *ep)
{
uint16_t bytes_now = tu_min16(xfer->in_remaining_bytes, xfer->max_packet_size);
uint16_t countdown = bytes_now;
/* precompute what amount of data will be left */
xfer->in_remaining_bytes -= bytes_now;
/*
if there will be no more data to send, we replace the BUFEMPTYIF EP interrupt with TXPKIF;
that way, we alert TinyUSB as soon as this last packet has been sent
*/
if (0 == xfer->in_remaining_bytes)
{
ep->EPINTSTS = USBD_EPINTSTS_TXPKIF_Msk;
ep->EPINTEN = USBD_EPINTEN_TXPKIEN_Msk;
}
/* provided buffers are thankfully 32-bit aligned, allowing most data to be transfered as 32-bit */
while (countdown > 3)
{
ep->EPDAT = *(uint32_t *)xfer->data_ptr;
xfer->data_ptr += 4; countdown -= 4;
}
while (countdown--)
ep->EPDAT_BYTE = *xfer->data_ptr++;
/* for short packets, we must nudge the peripheral to say 'that's all folks' */
if (bytes_now != xfer->max_packet_size)
ep->EPRSPCTL = USBD_EPRSPCTL_SHORTTXEN_Msk;
}
/* called by dcd_init() as well as by the ISR during a USB bus reset */
static void bus_reset(void)
{
for (enum ep_enum ep_index = PERIPH_EPA; ep_index < PERIPH_MAX_EP; ep_index++)
{
USBD->EP[ep_index].EPCFG = 0;
xfer_table[ep_index].dma_requested = false;
}
USBD->DMACNT = 0;
USBD->DMACTL = USBD_DMACTL_DMARST_Msk;
USBD->DMACTL = 0;
/* allocate the default EP0 endpoints */
USBD->CEPBUFSTART = 0;
USBD->CEPBUFEND = 0 + CFG_TUD_ENDPOINT0_SIZE - 1;
/* USB RAM beyond what we've allocated above is available to the user */
bufseg_addr = CFG_TUD_ENDPOINT0_SIZE;
/* Reset USB device address */
USBD->FADDR = 0;
configuration_changed = false;
current_dma_xfer = NULL;
}
/* this must only be called by the ISR; it does its best to share the single DMA engine across all user EPs (IN and OUT) */
static void service_dma(void)
{
if (current_dma_xfer)
return;
enum ep_enum ep_index;
struct xfer_ctl_t *xfer;
USBD_EP_T *ep;
for (ep_index = PERIPH_EPA, xfer = &xfer_table[PERIPH_EPA], ep = &USBD->EP[PERIPH_EPA]; ep_index < PERIPH_MAX_EP; ep_index++, xfer++, ep++)
{
uint16_t const available_bytes = ep->EPDATCNT & USBD_EPDATCNT_DATCNT_Msk;
if (!xfer->dma_requested || !available_bytes)
continue;
/*
instruct DMA to copy the data from the PC to the previously provided buffer
when the bus interrupt DMADONEIEN subsequently fires, the transfer will have finished
*/
USBD->DMACTL = xfer->ep_addr & USBD_DMACTL_EPNUM_Msk;
USBD->DMAADDR = (uint32_t)xfer->data_ptr;
USBD->DMACNT = available_bytes;
USBD->BUSINTSTS = USBD_BUSINTSTS_DMADONEIF_Msk;
xfer->out_bytes_so_far += available_bytes;
current_dma_xfer = xfer;
USBD->DMACTL |= USBD_DMACTL_DMAEN_Msk;
return;
}
}
/* centralized location for USBD interrupt enable bit masks */
static const uint32_t enabled_irqs = USBD_GINTEN_USBIEN_Msk | \
USBD_GINTEN_EPAIEN_Msk | USBD_GINTEN_EPBIEN_Msk | USBD_GINTEN_EPCIEN_Msk | USBD_GINTEN_EPDIEN_Msk | USBD_GINTEN_EPEIEN_Msk | USBD_GINTEN_EPFIEN_Msk | \
USBD_GINTEN_EPGIEN_Msk | USBD_GINTEN_EPHIEN_Msk | USBD_GINTEN_EPIIEN_Msk | USBD_GINTEN_EPJIEN_Msk | USBD_GINTEN_EPKIEN_Msk | USBD_GINTEN_EPLIEN_Msk | \
USBD_GINTEN_CEPIEN_Msk;
/*
NUC505 TinyUSB API driver implementation
*/
void dcd_init(uint8_t rhport)
{
(void) rhport;
/* configure interrupts in their initial state; BUSINTEN and CEPINTEN will be subsequently and dynamically re-written as needed */
USBD->GINTEN = enabled_irqs;
USBD->BUSINTEN = USBD_BUSINTEN_RSTIEN_Msk | USBD_BUSINTEN_VBUSDETIEN_Msk | USBD_BUSINTEN_RESUMEIEN_Msk | USBD_BUSINTEN_DMADONEIEN_Msk;
USBD->CEPINTEN = 0;
bus_reset();
usb_attach();
}
void dcd_int_enable(uint8_t rhport)
{
(void) rhport;
NVIC_EnableIRQ(USBD_IRQn);
}
void dcd_int_disable(uint8_t rhport)
{
(void) rhport;
NVIC_DisableIRQ(USBD_IRQn);
}
void dcd_set_address(uint8_t rhport, uint8_t dev_addr)
{
(void) rhport;
usb_control_send_zlp(); /* SET_ADDRESS is the one exception where TinyUSB doesn't use dcd_edpt_xfer() to generate a ZLP */
assigned_address = dev_addr;
}
void dcd_set_config(uint8_t rhport, uint8_t config_num)
{
(void) rhport;
(void) config_num;
configuration_changed = true;
}
void dcd_remote_wakeup(uint8_t rhport)
{
(void) rhport;
USBD->OPER |= USBD_OPER_RESUMEEN_Msk;
}
bool dcd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc)
{
(void) rhport;
USBD_EP_T *ep = ep_entry(p_endpoint_desc->bEndpointAddress, true);
TU_ASSERT(ep);
/* mine the data for the information we need */
int const dir = tu_edpt_dir(p_endpoint_desc->bEndpointAddress);
int const size = p_endpoint_desc->wMaxPacketSize.size;
tusb_xfer_type_t const type = p_endpoint_desc->bmAttributes.xfer;
struct xfer_ctl_t *xfer = &xfer_table[ep - USBD->EP];
/* allocate buffer from USB RAM */
ep->EPBUFSTART = bufseg_addr;
bufseg_addr += size;
ep->EPBUFEND = bufseg_addr - 1;
TU_ASSERT(bufseg_addr <= USBD_BUF_SIZE);
ep->EPMPS = size;
ep->EPRSPCTL = USB_EP_RSPCTL_FLUSH | eprspctl_eptype_table[type];
/* construct USB Configuration Register value and then write it */
uint32_t cfg = (uint32_t)tu_edpt_number(p_endpoint_desc->bEndpointAddress) << USBD_EPCFG_EPNUM_Pos;
if (TUSB_DIR_IN == dir)
cfg |= USBD_EPCFG_EPDIR_Msk;
cfg |= epcfg_eptype_table[type] | USBD_EPCFG_EPEN_Msk;
ep->EPCFG = cfg;
/* make a note of the endpoint particulars */
xfer->max_packet_size = size;
xfer->ep_addr = p_endpoint_desc->bEndpointAddress;
return true;
}
bool dcd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t *buffer, uint16_t total_bytes)
{
(void) rhport;
if (0x80 == ep_addr) /* control EP0 IN */
{
if (total_bytes)
{
USBD->CEPCTL = USBD_CEPCTL_FLUSH_Msk;
ctrl_in_xfer.data_ptr = buffer;
ctrl_in_xfer.in_remaining_bytes = total_bytes;
ctrl_in_xfer.total_bytes = total_bytes;
USBD->CEPINTSTS = USBD_CEPINTSTS_INTKIF_Msk;
USBD->CEPINTEN = USBD_CEPINTEN_INTKIEN_Msk;
}
else
{
usb_control_send_zlp();
}
}
else if (0x00 == ep_addr) /* control EP0 OUT */
{
if (total_bytes)
{
/* if TinyUSB is asking for EP0 OUT data, it is almost certainly already in the buffer */
while (total_bytes < USBD->CEPRXCNT);
for (int count = 0; count < total_bytes; count++)
*buffer++ = USBD->CEPDAT_BYTE;
usb_control_send_zlp();
}
}
else
{
/* mine the data for the information we need */
tusb_dir_t dir = tu_edpt_dir(ep_addr);
USBD_EP_T *ep = ep_entry(ep_addr, false);
struct xfer_ctl_t *xfer = &xfer_table[ep - USBD->EP];
/* store away the information we'll needing now and later */
xfer->data_ptr = buffer;
xfer->in_remaining_bytes = total_bytes;
xfer->total_bytes = total_bytes;
if (TUSB_DIR_IN == dir)
{
ep->EPINTEN = USBD_EPINTEN_BUFEMPTYIEN_Msk;
}
else
{
xfer->out_bytes_so_far = 0;
ep->EPINTEN = USBD_EPINTEN_RXPKIEN_Msk;
}
}
return true;
}
void dcd_edpt_stall(uint8_t rhport, uint8_t ep_addr)
{
(void) rhport;
if (tu_edpt_number(ep_addr))
{
USBD_EP_T *ep = ep_entry(ep_addr, false);
ep->EPRSPCTL = (ep->EPRSPCTL & 0xf7) | USBD_EPRSPCTL_HALT_Msk;
}
else
{
USBD->CEPCTL = USBD_CEPCTL_STALLEN_Msk;
}
}
void dcd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr)
{
(void) rhport;
if (tu_edpt_number(ep_addr))
{
USBD_EP_T *ep = ep_entry(ep_addr, false);
ep->EPRSPCTL = USBD_EPRSPCTL_TOGGLE_Msk;
}
}
void USBD_IRQHandler(void)
{
uint32_t status = USBD->GINTSTS;
/* USB interrupt */
if (status & USBD_GINTSTS_USBIF_Msk)
{
uint32_t bus_state = USBD->BUSINTSTS;
if (bus_state & USBD_BUSINTSTS_SOFIF_Msk)
{
/* Start-Of-Frame event */
dcd_event_bus_signal(0, DCD_EVENT_SOF, true);
}
if (bus_state & USBD_BUSINTSTS_RSTIF_Msk)
{
bus_reset();
USBD->CEPINTEN = USBD_CEPINTEN_SETUPPKIEN_Msk;
USBD->BUSINTEN = USBD_BUSINTEN_RSTIEN_Msk | USBD_BUSINTEN_RESUMEIEN_Msk | USBD_BUSINTEN_SUSPENDIEN_Msk | USBD_BUSINTEN_DMADONEIEN_Msk;
USBD->CEPINTSTS = 0x1ffc;
dcd_event_bus_signal(0, DCD_EVENT_BUS_RESET, true);
}
if (bus_state & USBD_BUSINTSTS_RESUMEIF_Msk)
{
USBD->BUSINTEN = USBD_BUSINTEN_RSTIEN_Msk | USBD_BUSINTEN_SUSPENDIEN_Msk | USBD_BUSINTEN_DMADONEIEN_Msk;
dcd_event_bus_signal(0, DCD_EVENT_RESUME, true);
}
if (bus_state & USBD_BUSINTSTS_SUSPENDIF_Msk)
{
USBD->BUSINTEN = USBD_BUSINTEN_RSTIEN_Msk | USBD_BUSINTEN_RESUMEIEN_Msk | USBD_BUSINTEN_DMADONEIEN_Msk;
dcd_event_bus_signal(0, DCD_EVENT_SUSPEND, true);
}
if (bus_state & USBD_BUSINTSTS_HISPDIF_Msk)
{
USBD->CEPINTEN = USBD_CEPINTEN_SETUPPKIEN_Msk;
}
if (bus_state & USBD_BUSINTSTS_DMADONEIF_Msk)
{
if (current_dma_xfer)
{
current_dma_xfer->dma_requested = false;
uint16_t available_bytes = USBD->DMACNT & USBD_DMACNT_DMACNT_Msk;
/* if the most recent DMA finishes the transfer, alert TinyUSB; otherwise, the next RXPKIF/INTKIF endpoint interrupt will prompt the next DMA */
if ( (current_dma_xfer->total_bytes == current_dma_xfer->out_bytes_so_far) || (available_bytes < current_dma_xfer->max_packet_size) )
{
dcd_event_xfer_complete(0, current_dma_xfer->ep_addr, current_dma_xfer->out_bytes_so_far, XFER_RESULT_SUCCESS, true);
}
current_dma_xfer = NULL;
service_dma();
}
}
if (bus_state & USBD_BUSINTSTS_VBUSDETIF_Msk)
{
if (USBD->PHYCTL & USBD_PHYCTL_VBUSDET_Msk)
{
/* USB connect */
USBD->PHYCTL |= USBD_PHYCTL_PHYEN_Msk | USBD_PHYCTL_DPPUEN_Msk;
}
else
{
/* USB disconnect */
USBD->PHYCTL &= ~USBD_PHYCTL_DPPUEN_Msk;
}
}
USBD->BUSINTSTS = bus_state & (USBD_BUSINTSTS_SOFIF_Msk | USBD_BUSINTSTS_RSTIF_Msk | USBD_BUSINTSTS_RESUMEIF_Msk | USBD_BUSINTSTS_SUSPENDIF_Msk | USBD_BUSINTSTS_HISPDIF_Msk | USBD_BUSINTSTS_DMADONEIF_Msk | USBD_BUSINTSTS_PHYCLKVLDIF_Msk | USBD_BUSINTSTS_VBUSDETIF_Msk);
}
if (status & USBD_GINTSTS_CEPIF_Msk)
{
uint32_t cep_state = USBD->CEPINTSTS & USBD->CEPINTEN;
if (cep_state & USBD_CEPINTSTS_SETUPPKIF_Msk)
{
/* get SETUP packet from USB buffer */
uint8_t setup_packet[8];
setup_packet[0] = (uint8_t)(USBD->SETUP1_0 >> 0);
setup_packet[1] = (uint8_t)(USBD->SETUP1_0 >> 8);
setup_packet[2] = (uint8_t)(USBD->SETUP3_2 >> 0);
setup_packet[3] = (uint8_t)(USBD->SETUP3_2 >> 8);
setup_packet[4] = (uint8_t)(USBD->SETUP5_4 >> 0);
setup_packet[5] = (uint8_t)(USBD->SETUP5_4 >> 8);
setup_packet[6] = (uint8_t)(USBD->SETUP7_6 >> 0);
setup_packet[7] = (uint8_t)(USBD->SETUP7_6 >> 8);
dcd_event_setup_received(0, setup_packet, true);
}
else if (cep_state & USBD_CEPINTSTS_INTKIF_Msk)
{
USBD->CEPINTSTS = USBD_CEPINTSTS_TXPKIF_Msk;
if (!(cep_state & USBD_CEPINTSTS_STSDONEIF_Msk))
{
USBD->CEPINTEN = USBD_CEPINTEN_TXPKIEN_Msk;
uint16_t bytes_now = tu_min16(ctrl_in_xfer.in_remaining_bytes, CFG_TUD_ENDPOINT0_SIZE);
for (int count = 0; count < bytes_now; count++)
USBD->CEPDAT_BYTE = *ctrl_in_xfer.data_ptr++;
ctrl_in_xfer.in_remaining_bytes -= bytes_now;
USBD_START_CEP_IN(bytes_now);
}
else
{
USBD->CEPINTEN = USBD_CEPINTEN_TXPKIEN_Msk | USBD_CEPINTEN_STSDONEIEN_Msk;
}
}
else if (cep_state & USBD_CEPINTSTS_TXPKIF_Msk)
{
USBD->CEPINTSTS = USBD_CEPINTSTS_STSDONEIF_Msk;
USBD_SET_CEP_STATE(USB_CEPCTL_NAKCLR);
/* alert TinyUSB that the EP0 IN transfer has finished */
if ( (0 == ctrl_in_xfer.in_remaining_bytes) || (0 == ctrl_in_xfer.total_bytes) )
dcd_event_xfer_complete(0, 0x80, ctrl_in_xfer.total_bytes, XFER_RESULT_SUCCESS, true);
if (ctrl_in_xfer.in_remaining_bytes)
{
USBD->CEPINTSTS = USBD_CEPINTSTS_INTKIF_Msk;
USBD->CEPINTEN = USBD_CEPINTEN_INTKIEN_Msk;
}
else
{
/* TinyUSB does its own fragmentation and ZLP for EP0; a transfer of zero means a ZLP */
if (0 == ctrl_in_xfer.total_bytes) USBD->CEPCTL = USBD_CEPCTL_ZEROLEN_Msk;
USBD->CEPINTSTS = USBD_CEPINTSTS_STSDONEIF_Msk;
USBD->CEPINTEN = USBD_CEPINTEN_SETUPPKIEN_Msk | USBD_CEPINTEN_STSDONEIEN_Msk;
}
}
else if (cep_state & USBD_CEPINTSTS_STSDONEIF_Msk)
{
/* given ACK from host has happened, we can now set the address (if not already done) */
if((USBD->FADDR != assigned_address) && (USBD->FADDR == 0)) USBD->FADDR = assigned_address;
if (configuration_changed)
{
for (enum ep_enum ep_index = PERIPH_EPA; ep_index < PERIPH_MAX_EP; ep_index++)
{
if (USBD->EP[ep_index].EPCFG & USBD_EPCFG_EPEN_Msk) USBD->EP[ep_index].EPRSPCTL = USBD_EPRSPCTL_TOGGLE_Msk;
}
configuration_changed = false;
}
USBD->CEPINTEN = USBD_CEPINTEN_SETUPPKIEN_Msk;
}
USBD->CEPINTSTS = cep_state;
return;
}
if (status & (USBD_GINTSTS_EPAIF_Msk | USBD_GINTSTS_EPBIF_Msk | USBD_GINTSTS_EPCIF_Msk | USBD_GINTSTS_EPDIF_Msk | USBD_GINTSTS_EPEIF_Msk | USBD_GINTSTS_EPFIF_Msk | USBD_GINTSTS_EPGIF_Msk | USBD_GINTSTS_EPHIF_Msk | USBD_GINTSTS_EPIIF_Msk | USBD_GINTSTS_EPJIF_Msk | USBD_GINTSTS_EPKIF_Msk | USBD_GINTSTS_EPLIF_Msk))
{
/* service PERIPH_EPA through PERIPH_EPL */
enum ep_enum ep_index;
uint32_t mask;
struct xfer_ctl_t *xfer;
USBD_EP_T *ep;
for (ep_index = PERIPH_EPA, mask = USBD_GINTSTS_EPAIF_Msk, xfer = &xfer_table[PERIPH_EPA], ep = &USBD->EP[PERIPH_EPA]; ep_index < PERIPH_MAX_EP; ep_index++, mask <<= 1, xfer++, ep++)
{
if(status & mask)
{
uint8_t const ep_addr = xfer->ep_addr;
bool const out_ep = !(ep_addr & TUSB_DIR_IN_MASK);
uint32_t ep_state = ep->EPINTSTS & ep->EPINTEN;
if (out_ep)
{
#if 1
xfer->dma_requested = true;
service_dma();
#else
uint16_t const available_bytes = ep->EPDATCNT & USBD_EPDATCNT_DATCNT_Msk;
/* copy the data from the PC to the previously provided buffer */
for (int count = 0; (count < available_bytes) && (xfer->out_bytes_so_far < xfer->total_bytes); count++, xfer->out_bytes_so_far++)
*xfer->data_ptr++ = ep->EPDAT_BYTE;
/* when the transfer is finished, alert TinyUSB; otherwise, continue accepting more data */
if ( (xfer->total_bytes == xfer->out_bytes_so_far) || (available_bytes < xfer->max_packet_size) )
dcd_event_xfer_complete(0, ep_addr, xfer->out_bytes_so_far, XFER_RESULT_SUCCESS, true);
#endif
}
else if (ep_state & USBD_EPINTSTS_BUFEMPTYIF_Msk)
{
/* send any remaining data */
dcd_userEP_in_xfer(xfer, ep);
}
else if (ep_state & USBD_EPINTSTS_TXPKIF_Msk)
{
/* alert TinyUSB that we've finished */
dcd_event_xfer_complete(0, ep_addr, xfer->total_bytes, XFER_RESULT_SUCCESS, true);
ep->EPINTEN = 0;
}
ep->EPINTSTS = ep_state;
}
}
}
}
void dcd_isr(uint8_t rhport)
{
(void) rhport;
USBD_IRQHandler();
}
#endif

View File

@ -79,6 +79,7 @@
#define OPT_MCU_NUC121 800
#define OPT_MCU_NUC126 801
#define OPT_MCU_NUC505 803
#define OPT_MCU_ESP32S2 900 ///< Espressif ESP32-S2