diff --git a/os/hal/ports/NUMICRO/LLD/USBv1/driver.mk b/os/hal/ports/NUMICRO/LLD/USBv1/driver.mk index 5fb8fe88..218ab5ed 100644 --- a/os/hal/ports/NUMICRO/LLD/USBv1/driver.mk +++ b/os/hal/ports/NUMICRO/LLD/USBv1/driver.mk @@ -1,9 +1,11 @@ -ifeq ($(USE_SMART_BUILD),yes) -ifneq ($(findstring HAL_USE_USB TRUE,$(HALCONF)),) -PLATFORMSRC += $(CHIBIOS_CONTRIB)/os/hal/ports/NUMICRO/LLD/USBv1/hal_usb_lld.c -endif -else -PLATFORMSRC += $(CHIBIOS_CONTRIB)/os/hal/ports/NUMICRO/LLD/USBv1/hal_usb_lld.c -endif - -PLATFORMINC += $(CHIBIOS_CONTRIB)/os/hal/ports/NUMICRO/LLD/USBv1 +ifeq ($(USE_SMART_BUILD),yes) +ifneq ($(findstring HAL_USE_USB TRUE,$(HALCONF)),) +PLATFORMSRC += $(CHIBIOS_CONTRIB)/os/hal/ports/NUMICRO/LLD/USBv1/hal_usb_lld.c +PLATFORMASM += $(CHIBIOS_CONTRIB)/os/hal/ports/NUMICRO/LLD/USBv1/usb_memcpy.S +endif +else +PLATFORMSRC += $(CHIBIOS_CONTRIB)/os/hal/ports/NUMICRO/LLD/USBv1/hal_usb_lld.c +PLATFORMASM += $(CHIBIOS_CONTRIB)/os/hal/ports/NUMICRO/LLD/USBv1/usb_memcpy.S +endif + +PLATFORMINC += $(CHIBIOS_CONTRIB)/os/hal/ports/NUMICRO/LLD/USBv1 diff --git a/os/hal/ports/NUMICRO/LLD/USBv1/hal_usb_lld.c b/os/hal/ports/NUMICRO/LLD/USBv1/hal_usb_lld.c index 793cdb3c..ecd553b8 100644 --- a/os/hal/ports/NUMICRO/LLD/USBv1/hal_usb_lld.c +++ b/os/hal/ports/NUMICRO/LLD/USBv1/hal_usb_lld.c @@ -1,6 +1,5 @@ /* - ChibiOS - Copyright (C) 2017 Frank Zschockelt - ChibiOS - Copyright (C) 2019 /u/KeepItUnder + Copyright (C) 2020 Alex Lewontin Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -25,12 +24,50 @@ #include "hal.h" -#if (HAL_USE_USB == TRUE) || defined(__DOXYGEN__) +/* + * TODO: + * As it stands, the behavior of hardware endpoint 6 (which corresponds to the + * OUT direction of logical endpoint 3) may malfunction if hardware endpoint 5 + * (the IN direction of logical endpoint 2) is in use, due to a flaw in the + * hardware (for more info, see the errata ER_6000_NUC123AN_EN_Rev.1.04.pdf). + * + * The only way to ensure both hardware endpoints function properly is to set + * them both as OUT. Under the current alternating scheme, this would be tricky. + * However, hardware endpoint 5 is currently configured as an IN endpoint, which + * means hardware endpoint 6 will malfunction under any circumstances (from a + * user's perspective, this means that logical endpoint 3 cannot handle outward + * bound traffic). + * + * The first step in the fix is switching the alternation pattern so that + * hardware endpoint 5 serves as an OUT endpoint. Then, logical endpoint 3 will + * be able to handle IN traffic, and can handle two directional traffic IF it is + * in ISOC mode. + * + * Eventually, it may be the case that the alternating scheme needs to be + * abandoned, and some more sophisticated scheme for allocating hardware + * endpoints adopted. + */ + +#if HAL_USE_USB || defined(__DOXYGEN__) /*===========================================================================*/ /* Driver local definitions. */ /*===========================================================================*/ +#define NUC123_USB_HW_ENDPOINTS 8 + +#define NUC123_USBD_CFG_OUT (1UL << USBD_CFG_STATE_Pos) +#define NUC123_USBD_CFG_IN (2UL << USBD_CFG_STATE_Pos) + +#define USBD_SRAM_BASE ((volatile uint8_t*)(USBD_BASE + 0x100)) + +#define NUC123_USBD_BUSSTATUS_INACK 0UL +#define NUC123_USBD_BUSSTATUS_INNAK 1UL +#define NUC123_USBD_BUSSTATUS_OUTDATA0ACK 2UL +#define NUC123_USBD_BUSSTATUS_SETUPACK 3UL +#define NUC123_USBD_BUSSTATUS_OUTDATA1ACK 6UL +#define NUC123_USBD_BUSSTATUS_ISOEND 7UL + /*===========================================================================*/ /* Driver exported variables. */ /*===========================================================================*/ @@ -38,7 +75,7 @@ /** * @brief USB1 driver identifier. */ -#if (NUC123_USB_USE_USB1 == TRUE) || defined(__DOXYGEN__) +#if NUC123_USB_USE_USB1 || defined(__DOXYGEN__) USBDriver USBD1; #endif @@ -46,184 +83,306 @@ USBDriver USBD1; /* Driver local variables and types. */ /*===========================================================================*/ -uint8_t * const usbd_sram = (uint8_t*)(USBD_BASE + 0x100); - /** * @brief EP0 state. + * @note It is an union because IN and OUT endpoints are never used at the + * same time for EP0. */ +static union { /** * @brief IN EP0 state. */ - USBInEndpointState ep0_in; + USBInEndpointState in; /** * @brief OUT EP0 state. */ - USBOutEndpointState ep0_out; - -static uint8_t ep0setup_buffer[8]; + USBOutEndpointState out; +} ep0_state; /** * @brief EP0 initialization structure. */ -static const USBEndpointConfig ep0config = { - USB_EP_MODE_TYPE_CTRL, - _usb_ep0setup, - _usb_ep0in, - _usb_ep0out, - 0x40, - 0x40, - &ep0_in, - &ep0_out, - 1, - ep0setup_buffer -}; +static const USBEndpointConfig ep0config = {USB_EP_MODE_TYPE_CTRL, + _usb_ep0setup, + _usb_ep0in, + _usb_ep0out, + 0x40, + 0x40, + &ep0_state.in, + &ep0_state.out}; -/*===========================================================================*/ -/* Driver local variables and types. */ -/*===========================================================================*/ +/** + * @brief Tracks the first unallocated word in the SRAM buffer + */ +static uint32_t sram_free_dword_offset = 1UL; /*===========================================================================*/ /* Driver local functions. */ /*===========================================================================*/ -void _toggle_dsq(int ep, uint8_t *dsq) + +/** + * @brief Converts a clock division factor to the appropriate bit setting + */ +#define NUC123_CLK_CLKDIV_USB(n) (((n)-1) << CLK_CLKDIV_USB_N_Pos) + +/** + * @name Endpoint number convenience macros + * @{ + */ +#define HW_OUT_EPN(lepn) (2 * (lepn)) +#define HW_IN_EPN(lepn) (HW_OUT_EPN(lepn) + 1) +#define HW_EP(hwepn) ((USBD->EP) + (hwepn)) +#define HW_OUT_EP(lepn) (HW_EP(HW_OUT_EPN(lepn))) +#define HW_IN_EP(lepn) (HW_EP(HW_IN_EPN(lepn))) +#define LOGICAL_EPN(hwepn) ((hwepn) / 2) +/** @} */ + +/** + * @brief Convenience macro to round up to the nearest double word, used for SRAM allocation + */ +#define BYTES_TO_NEXT_DWORD(bytes) (((bytes) + 7) >> 3) + +/** + * @brief Returns the minimum of two unsigned values. A safer implementation + * of the classic macro + * + * @param[in] x An unsigned value + * @param[in] y An unsigned value + * + * @return The smaller of x and y + * + * @notapi + */ +static inline unsigned min(unsigned x, unsigned y) { - if (*dsq) { - USBD->EP[ep].CFG &= ~USBD_CFG_DSQ_SYNC_Msk; + return ((x > y) ? y : x); +} + +/** + * @brief Returns the maximum of two unsigned values. A safer implementation + * of the classic macro + * + * @param[in] x An unsigned value + * @param[in] y An unsigned value + * + * @return The larger of x and y + * + * @notapi + */ +static inline unsigned max(unsigned x, unsigned y) +{ + return ((x > y) ? x : y); +} + +/** + * @brief memcpy-like function that should be used when either the source + * or the destination is the dedicated USB SRAM buffer. Ensures the correct + * memory access sizes are used. + * + * @note This is only the forward declaration. This function is defined in usb_memcpy.S + * + * @param[in] destination Buffer to copy the data to + * @param[in] source Buffer to copy the data from + * @param[in] num Number of bytes to copy + * + * @return The number of bytes copied. This always exactly equals num, but + * is returned purely for convenience. + * @notapi + */ +size_t usb_memcpy(volatile void* destination, const volatile void* source, + size_t num); + +/** + * @brief Common ISR code, serves the EP-related interrupts. + * + * @param[in] epn endpoint number + * + * @notapi + */ +static void usb_serve_out_endpoint(uint32_t epn) +{ + USBDriver *const usbp = &USBD1; + uint32_t mxpld = HW_OUT_EP(epn)->MXPLD; + uint32_t rxsize_actual = min(mxpld, usbp->epc[epn]->out_state->rxcnt - usbp->epc[epn]->out_state->rxsize); + usbp->epc[epn]->out_state->rxcnt += rxsize_actual; + usb_memcpy(usbp->epc[epn]->out_state->rxbuf + + usbp->epc[epn]->out_state->rxcnt, + USBD_SRAM_BASE + (HW_OUT_EP(epn)->BUFSEG), + rxsize_actual); + + if ((rxsize_actual < usbp->epc[epn]->out_maxsize) || + (usbp->epc[epn]->out_state->rxcnt >= + usbp->epc[epn]->out_state->rxsize)) { + _usb_isr_invoke_out_cb(usbp, epn); + } else { + HW_OUT_EP(epn)->MXPLD = min(usbp->epc[epn]->out_maxsize, + usbp->epc[epn]->out_state->rxsize - + usbp->epc[epn]->out_state->rxcnt); } - else { - USBD->EP[ep].CFG |= USBD_CFG_DSQ_SYNC_Msk; +} + +/** + * @brief Common ISR code, serves the EP-related interrupts. + * + * @param[in] epn endpoint number + * + * @notapi + */ +static void usb_serve_in_endpoint(uint32_t epn) +{ + USBDriver *const usbp = &USBD1; + + usbp->epc[epn]->in_state->txcnt += HW_IN_EP(epn)->MXPLD; + + if (usbp->epc[epn]->in_state->txcnt >= usbp->epc[epn]->in_state->txsize) { + _usb_isr_invoke_in_cb(usbp, epn); + } else { + HW_IN_EP(epn)->MXPLD = usb_memcpy( + USBD_SRAM_BASE + (HW_IN_EP(epn)->BUFSEG), + usbp->epc[epn]->in_state->txbuf + usbp->epc[epn]->in_state->txcnt, + min(usbp->epc[epn]->in_maxsize, + usbp->epc[epn]->in_state->txsize - + usbp->epc[epn]->in_state->txcnt)); + } +} + +/** + * @brief Common ISR code, serves general events and calls the appropriate endpoint routine + * + * @param[in] usbp pointer to a @p USBDriver object + * + * @notapi + */ +static void usb_lld_serve_interrupt(USBDriver* usbp) +{ + + uint32_t intsts = USBD->INTSTS; + uint32_t bussts = USBD->ATTR & 0xF; + + if (intsts & USBD_INTSTS_FLDET_STS_Msk) { + if (USBD->FLDET & USBD_FLDET_FLDET_Msk) { + USBD->ATTR |= (USBD_ATTR_PHY_EN_Msk | USBD_ATTR_USB_EN_Msk); + usbConnectBus(&USBD1); + } else { + usbDisconnectBus(&USBD1); + USBD->ATTR &= ~USBD_ATTR_USB_EN_Msk; + } + USBD->INTSTS = USBD_INTSTS_FLDET_STS_Msk; + } + + if (intsts & USBD_INTSTS_WAKEUP_STS_Msk) { + /* Clear event flag */ + USBD->INTSTS = USBD_INTSTS_WAKEUP_STS_Msk; + _usb_wakeup(usbp); + } + + if (intsts & USBD_INTSTS_BUS_STS_Msk) { + /* Clear event flag */ + USBD->INTSTS = USBD_INTSTS_BUS_STS_Msk; + + if (bussts & USBD_ATTR_USBRST_Msk) { + /* Bus reset */ + USBD->ATTR |= (USBD_ATTR_PHY_EN_Msk | USBD_ATTR_USB_EN_Msk); + _usb_reset(usbp); + } + if (bussts & USBD_ATTR_SUSPEND_Msk) { + /* Enable USB but disable PHY */ + USBD->ATTR &= ~USBD_ATTR_PHY_EN_Msk; + _usb_suspend(usbp); + } + if (bussts & USBD_ATTR_RESUME_Msk) { + /* Enable USB and enable PHY */ + USBD->ATTR |= (USBD_ATTR_PHY_EN_Msk | USBD_ATTR_USB_EN_Msk); + + } + } + + if (intsts & USBD_INTSTS_USB_STS_Msk) { + if (intsts & USBD_INTSTS_SETUP_Msk) { + /* Clear event flag */ + USBD->INTSTS = USBD_INTSTS_SETUP_Msk; + + /* Clear the data IN/OUT ready flag of control end-points */ + HW_IN_EP(0)->CFGP |= USBD_CFGP_CLRRDY_Msk; + HW_OUT_EP(0)->CFGP |= USBD_CFGP_CLRRDY_Msk; + + _usb_isr_invoke_setup_cb(&USBD1, 0); + HW_IN_EP(0)->CFG |= USBD_CFG_DSQ_SYNC_Msk; + } + + /* EP events */ + if (intsts & USBD_INTSTS_EPEVT0_Msk) { + /* Clear event flag */ + USBD->INTSTS = USBD_INTSTS_EPEVT0_Msk; + + usb_serve_out_endpoint(0); + } + + if (intsts & USBD_INTSTS_EPEVT1_Msk) { + /* Clear event flag */ + USBD->INTSTS = (USBD_INTSTS_EPEVT1_Msk); + + usb_serve_in_endpoint(0); + } + + if (intsts & USBD_INTSTS_EPEVT2_Msk) { + /* Clear event flag */ + USBD->INTSTS = (USBD_INTSTS_EPEVT2_Msk); + usb_serve_out_endpoint(1); + } + + if (intsts & USBD_INTSTS_EPEVT3_Msk) { + /* Clear event flag */ + USBD->INTSTS = (USBD_INTSTS_EPEVT3_Msk); + usb_serve_in_endpoint(1); + } + + if (intsts & USBD_INTSTS_EPEVT4_Msk) { + /* Clear event flag */ + USBD->INTSTS = (USBD_INTSTS_EPEVT4_Msk); + usb_serve_out_endpoint(2); + } + + if (intsts & USBD_INTSTS_EPEVT5_Msk) { + /* Clear event flag */ + USBD->INTSTS = (USBD_INTSTS_EPEVT5_Msk); + usb_serve_in_endpoint(2); + } + + if (intsts & USBD_INTSTS_EPEVT6_Msk) { + /* Clear event flag */ + USBD->INTSTS = (USBD_INTSTS_EPEVT6_Msk); + usb_serve_out_endpoint(3); + } + + if (intsts & USBD_INTSTS_EPEVT7_Msk) { + /* Clear event flag */ + USBD->INTSTS = (USBD_INTSTS_EPEVT7_Msk); + usb_serve_in_endpoint(3); + } } - *dsq = !*dsq; } /*===========================================================================*/ /* Driver interrupt handlers and threads. */ /*===========================================================================*/ -static void serve_endpoint_irq(USBDriver *usbp, - uint32_t endpoint_mask, - uint32_t epstatus) +#if NUC123_USB_USE_USB1 || defined(__DOXYGEN__) + +/** + * @brief USB event handler + * + * @isr + */ +OSAL_IRQ_HANDLER(NUC123_USB1_HANDLER) { - for (int hwEp = 0; hwEp < USB_MAX_ENDPOINTS; hwEp++) { - if (endpoint_mask & (1 << hwEp)) { - uint32_t bus_status = ((epstatus >> (hwEp*(USBD_EPSTS_EPSTS1_Pos - USBD_EPSTS_EPSTS0_Pos))) & USBD_EPSTS_EPSTS0_Msk) >> USBD_EPSTS_EPSTS0_Pos; - usbep_t ep = (USBD->EP[hwEp].CFG & USBD_CFG_EP_NUM_Msk) >> USBD_CFG_EP_NUM_Pos; - - switch (bus_status) { - case 0: /* In ACK */ - { - USBInEndpointState *iesp = usbp->epc[ep]->in_state; - iesp->txcnt += USBD->EP[hwEp].MXPLD; - if (iesp->txcnt >= iesp->txsize) { - _usb_isr_invoke_in_cb(usbp, ep); - } - else { - uint32_t txcnt; - if ((iesp->txcnt + usbp->epc[ep]->in_maxsize) > iesp->txsize) { - txcnt = iesp->txsize - iesp->txcnt; - } - else { - txcnt = usbp->epc[ep]->in_maxsize; - } - for (uint32_t n = 0; n < txcnt; n++) { - usbd_sram[(USBD->EP[hwEp].BUFSEG) + n] = iesp->txbuf[iesp->txcnt + n]; - } - _toggle_dsq(hwEp, &(iesp->dsq)); - USBD->EP[hwEp].MXPLD = txcnt; - } - } - break; - case 0x1: /* In NAK */ - break; - case 0x2: /* Out Packet Data 0 ACK */ - case 0x6: /* Out Packet Data 1 ACK */ - { - USBOutEndpointState *oesp = usbp->epc[ep]->out_state; - for (uint32_t n = 0; n < USBD->EP[hwEp].MXPLD; n++) { - oesp->rxbuf[oesp->rxcnt] = usbd_sram[USBD->EP[hwEp].BUFSEG + n]; - oesp->rxcnt++; - } - if (oesp->rxcnt == oesp->rxsize) { - _usb_isr_invoke_out_cb(usbp, ep); - } - else { - _toggle_dsq(hwEp, &(oesp->dsq)); - if ((oesp->rxcnt + usbp->epc[ep]->out_maxsize) > oesp->rxsize) { - USBD->EP[hwEp].MXPLD = oesp->rxsize - oesp->rxcnt; - } - else { - USBD->EP[hwEp].MXPLD = usbp->epc[ep]->out_maxsize; - } - } - } - break; - case 0x3: /* Setup ACK */ - break; - case 0x7: /* Isochronous transfer end */ - default: - osalDbgAssert(FALSE, "not supported"); - } - } - } -} - -static void serve_usb_irq(USBDriver *usbp) { - uint32_t intsts = USBD->INTSTS; - uint32_t epsts = USBD->EPSTS; - - if (intsts & USBD_INTSTS_FLDET_STS_Msk) { - USBD->INTSTS |= USBD_INTSTS_FLDET_STS_Msk; - if (USBD->FLDET) { - usbConnectBus(usbp); - } - else { - usbDisconnectBus(usbp); - } - } - - if (intsts & USBD_INTSTS_BUS_STS_Msk) { - uint32_t bus_attr = USBD->ATTR; - USBD->INTSTS |= USBD_INTSTS_BUS_STS_Msk; - if (bus_attr & USBD_ATTR_USBRST_Msk) { - _usb_reset(usbp); - } - if (bus_attr & USBD_ATTR_SUSPEND_Msk) { - if (usbp->state == USB_ACTIVE) { - _usb_suspend(usbp); - } - } - if (bus_attr & USBD_ATTR_RESUME_Msk) { - usbConnectBus(usbp); - (usbp)->receiving = 0; - (usbp)->transmitting = 0; - _usb_wakeup(usbp); - } - } - - if (intsts & USBD_INTSTS_USB_STS_Msk) { - /* check endpoints first */ - serve_endpoint_irq(usbp, (intsts & USBD_INTSTS_EPEVT_Msk) >> USBD_INTSTS_EPEVT_Pos, epsts); - USBD->INTSTS = intsts & USBD_INTSTS_EPEVT_Msk; - /* then handle setup packets */ - if (intsts & USBD_INTSTS_SETUP_Msk) { - USBD->INTSTS |= USBD_INTSTS_SETUP_Msk; - USBInEndpointState *iesp = usbp->epc[0]->in_state; - iesp->dsq = 0; - _usb_isr_invoke_setup_cb(usbp, 0); - } - } - if (intsts & USBD_INTSTS_WAKEUP_STS_Msk) { - USBD->INTSTS |= USBD_INTSTS_WAKEUP_STS_Msk; - USBD->ATTR |= USBD_ATTR_PHY_EN_Msk; - } -} - -OSAL_IRQ_HANDLER(NUC123_USB1_HANDLER) { OSAL_IRQ_PROLOGUE(); - serve_usb_irq(&USBD1); + usb_lld_serve_interrupt(&USBD1); OSAL_IRQ_EPILOGUE(); } +#endif /* NUC123_USB_USE_USB1 */ + /*===========================================================================*/ /* Driver exported functions. */ /*===========================================================================*/ @@ -233,12 +392,19 @@ OSAL_IRQ_HANDLER(NUC123_USB1_HANDLER) { * * @notapi */ -void usb_lld_init(void) { +void usb_lld_init(void) +{ + CLK->CLKDIV = (CLK->CLKDIV & (~CLK_CLKDIV_USB_N_Msk)) | + NUC123_CLK_CLKDIV_USB(NUC123_USBD_CLKDIV); -#if NUC123_USB_USE_USB1 == TRUE + for (uint8_t i = 0; i < NUC123_USB_HW_ENDPOINTS; ++i) { + USBD->EP[i].CFG &= ~USBD_CFG_DSQ_SYNC_Msk; + } + +#if NUC123_USB_USE_USB1 /* Driver initialization.*/ usbObjectInit(&USBD1); -#endif +#endif /* NUC123_USB_USE_USB1 */ } /** @@ -248,44 +414,41 @@ void usb_lld_init(void) { * * @notapi */ -void usb_lld_start(USBDriver *usbp) { +void usb_lld_start(USBDriver* usbp) +{ + if (usbp->state == USB_STOP) { /* Enables the peripheral.*/ -#if NUC123_USB_USE_USB1 == TRUE + CLK->APBCLK |= CLK_APBCLK_USBD_EN_Msk; + +#if NUC123_USB_USE_USB1 if (&USBD1 == usbp) { - uint16_t delay; - /* Enable USB Clock */ - UNLOCKREG(); - CLK->APBCLK |= CLK_APBCLK_USBD_EN_Msk; - - /* Reset USB */ - SYS->IPRSTC2 |= SYS_IPRSTC2_USBD_RST_Msk; - for (delay=0x100; delay > 0; delay--); - SYS->IPRSTC2 &= ~SYS_IPRSTC2_USBD_RST_Msk; - LOCKREG(); - - usbConnectBus(usbp); - USBD->ATTR = (USBD_ATTR_BYTEM_Msk | USBD_ATTR_PWRDN_Msk | - USBD_ATTR_DPPU_EN_Msk |USBD_ATTR_USB_EN_Msk | - USBD_ATTR_PHY_EN_Msk); - - - USBD->DRVSE0 = 1u; - for (delay=0x100; delay > 0; delay--); - USBD->DRVSE0 = 0u; - - USBD->INTSTS = USBD->INTSTS; - - nvicEnableVector(NUC123_USB1_NUMBER, NUC123_USB_USB1_IRQ_PRIORITY); - - USBD->INTEN = (USBD_INTEN_WAKEUP_EN_Msk | - USBD_INTEN_WAKEUP_IE_Msk | - USBD_INTEN_FLDET_IE_Msk | - USBD_INTEN_USB_IE_Msk | - USBD_INTEN_BUS_IE_Msk); } -#endif +#endif /* NUC123_USB_USE_USB1 */ } + /* Configures the peripheral.*/ + /* Reset procedure enforced on driver start.*/ + + SYS->IPRSTC2 |= SYS_IPRSTC2_USBD_RST_Msk; + SYS->IPRSTC2 &= ~(SYS_IPRSTC2_USBD_RST_Msk); + + /* Post reset initialization.*/ + /* Initial USB engine */ + USBD->ATTR = USBD_ATTR_PWRDN_Msk | USBD_ATTR_DPPU_EN_Msk | + USBD_ATTR_USB_EN_Msk | USBD_ATTR_PHY_EN_Msk; + + USBD->STBUFSEG = 0UL; + + USBD->INTSTS = USBD_INTSTS_BUS_STS_Msk | USBD_INTSTS_FLDET_STS_Msk | + USBD_INTSTS_USB_STS_Msk | USBD_INTSTS_WAKEUP_STS_Msk; + + USBD->INTEN |= (USBD_INTEN_BUS_IE_Msk | USBD_INTEN_FLDET_IE_Msk | + USBD_INTEN_USB_IE_Msk | USBD_INTEN_WAKEUP_IE_Msk | + USBD_INTEN_WAKEUP_EN_Msk); + + nvicEnableVector(USBD_IRQn, NUC123_USB_IRQ_PRIORITY); + + usb_lld_reset(usbp); } /** @@ -295,15 +458,21 @@ void usb_lld_start(USBDriver *usbp) { * * @notapi */ -void usb_lld_stop(USBDriver *usbp) { - if (usbp->state == USB_READY) { - /* Resets the peripheral.*/ - /* Disables the peripheral.*/ -#if NUC123_USB_USE_USB1 == TRUE - if (&USBD1 == usbp) { +void usb_lld_stop(USBDriver* usbp) +{ + if (usbp->state == USB_READY) { + /* Resets the peripheral. */ + + /* Disables the peripheral. */ +#if NUC123_USB_USE_USB1 + if (&USBD1 == usbp) { } -#endif +#endif /* NUC123_USB_USE_USB1 */ + + usbDisconnectBus(usbp); + CLK->APBCLK &= ~CLK_APBCLK_USBD_EN_Msk; + nvicDisableVector(USBD_IRQn); } } @@ -314,17 +483,17 @@ void usb_lld_stop(USBDriver *usbp) { * * @notapi */ -void usb_lld_reset(USBDriver *usbp) { - /* Post reset initialization.*/ +void usb_lld_reset(USBDriver* usbp) +{ + uint_fast16_t i; + sram_free_dword_offset = 1UL; + for (i = 0; i < 0x200; ++i) { + USBD_SRAM_BASE[i] = 0x55; + } + USBD->FADDR = 0; /* EP0 initialization.*/ usbp->epc[0] = &ep0config; - /* NUC123 has 512b SRAM for EP-buffers; the first 8b are reserved for setup packets */ - usbp->bufnext = 8; - usbp->epnext = 0; - usb_lld_init_endpoint(usbp, 0); - usbConnectBus(usbp); - USBD->FADDR = 0; } /** @@ -334,19 +503,11 @@ void usb_lld_reset(USBDriver *usbp) { * * @notapi */ -void usb_lld_set_address(USBDriver *usbp) { - USBD->FADDR = usbp->address; +void usb_lld_set_address(USBDriver* usbp) +{ + USBD->FADDR = USBD_FADDR_FADDR_Msk & usbp->address; } -uint32_t usb_alloc_buf(USBDriver *usbp, size_t size) { - uint32_t buf; - buf = usbp->bufnext; - usbp->bufnext += size; - osalDbgAssert(usbp->bufnext <= 512, "usb buffer space full"); - return buf; -} - - /** * @brief Enables an endpoint. * @@ -355,39 +516,76 @@ uint32_t usb_alloc_buf(USBDriver *usbp, size_t size) { * * @notapi */ -void usb_lld_init_endpoint(USBDriver *usbp, usbep_t ep) { - const USBEndpointConfig *epcp = usbp->epc[ep]; - uint8_t hwep; - - - if (epcp->in_state != NULL) { - hwep = usbp->epnext; - usbp->epnext += 1; - osalDbgAssert(usbp->epnext <= USB_MAX_ENDPOINTS, "No endpoints left"); - - USBD->EP[hwep].BUFSEG = usb_alloc_buf(usbp, epcp->in_maxsize); - if (epcp->ep_mode == USB_EP_MODE_TYPE_CTRL) - /* 2 == in */ - USBD->EP[hwep].CFG = (ep << USBD_CFG_EP_NUM_Pos) | (2 << USBD_CFG_STATE_Pos) | USBD_CFG_CSTALL_Msk; - else - USBD->EP[hwep].CFG = (ep << USBD_CFG_EP_NUM_Pos) | (2 << USBD_CFG_STATE_Pos); - epcp->in_state->hwEp = hwep; +void usb_lld_init_endpoint(USBDriver* usbp, usbep_t ep) +{ + if (usbp->epc[ep]->ep_mode == USB_EP_MODE_TYPE_ISOC) { + osalDbgAssert(FALSE, "isochronous mode not yet supported"); + osalDbgAssert((usbp->epc[ep]->in_state == NULL) || + (usbp->epc[ep]->out_state == NULL), + "isochronous EP cannot be IN and OUT"); } - if (epcp->out_state != NULL) { - hwep = usbp->epnext; - usbp->epnext += 1; - osalDbgAssert(usbp->epnext <= USB_MAX_ENDPOINTS, "No endpoints left"); + /* If the out logical endpoint is used */ + if (usbp->epc[ep]->out_state) { + switch (usbp->epc[ep]->ep_mode) { + case USB_EP_MODE_TYPE_CTRL: + HW_OUT_EP(ep)->CFG = + ((ep << USBD_CFG_EP_NUM_Pos) & USBD_CFG_EP_NUM_Msk) | + NUC123_USBD_CFG_OUT | USBD_CFG_CSTALL_Msk; + break; + case USB_EP_MODE_TYPE_ISOC: + HW_OUT_EP(ep)->CFG = + ((ep << USBD_CFG_EP_NUM_Pos) & USBD_CFG_EP_NUM_Msk) | + NUC123_USBD_CFG_OUT | USBD_CFG_ISOCH_Msk; + break; + case USB_EP_MODE_TYPE_BULK: + case USB_EP_MODE_TYPE_INTR: + default: + HW_OUT_EP(ep)->CFG = + ((ep << USBD_CFG_EP_NUM_Pos) & USBD_CFG_EP_NUM_Msk) | + NUC123_USBD_CFG_OUT; + } + HW_OUT_EP(ep)->BUFSEG = sram_free_dword_offset << USBD_BUFSEG_BUFSEG_Pos; + sram_free_dword_offset += + BYTES_TO_NEXT_DWORD((uint32_t)usbp->epc[ep]->out_maxsize); - USBD->EP[hwep].BUFSEG = usb_alloc_buf(usbp, epcp->out_maxsize); - if (epcp->ep_mode == USB_EP_MODE_TYPE_CTRL) - /* 1 == Out */ - USBD->EP[hwep].CFG = (ep << USBD_CFG_EP_NUM_Pos) | (1 << USBD_CFG_STATE_Pos) | USBD_CFG_CSTALL_Msk; - else - USBD->EP[hwep].CFG = (ep << USBD_CFG_EP_NUM_Pos) | (1 << USBD_CFG_STATE_Pos); - epcp->out_state->hwEp = hwep; + } else { + /* The only important bits here are STATE ([6:5]), and we just need + to set them to 0 to disable the endpoint. + */ + HW_OUT_EP(ep)->CFG = 0; } + /* If the in logical endpoint is used */ + if (usbp->epc[ep]->in_state) { + switch (usbp->epc[ep]->ep_mode) { + case USB_EP_MODE_TYPE_CTRL: + HW_IN_EP(ep)->CFG = + ((ep << USBD_CFG_EP_NUM_Pos) & USBD_CFG_EP_NUM_Msk) | + NUC123_USBD_CFG_IN | USBD_CFG_CSTALL_Msk; + break; + case USB_EP_MODE_TYPE_ISOC: + HW_IN_EP(ep)->CFG = + ((ep << USBD_CFG_EP_NUM_Pos) & USBD_CFG_EP_NUM_Msk) | + NUC123_USBD_CFG_IN | USBD_CFG_ISOCH_Msk; + break; + case USB_EP_MODE_TYPE_BULK: + case USB_EP_MODE_TYPE_INTR: + default: + HW_IN_EP(ep)->CFG = + ((ep << USBD_CFG_EP_NUM_Pos) & USBD_CFG_EP_NUM_Msk) | + NUC123_USBD_CFG_IN; + } + + HW_IN_EP(ep)->BUFSEG = sram_free_dword_offset << USBD_BUFSEG_BUFSEG_Pos; + sram_free_dword_offset += + BYTES_TO_NEXT_DWORD((uint32_t)usbp->epc[ep]->in_maxsize); + } else { + /* The only important bits here are STATE ([6:5]), and we just need + to set them to 0 to disable the endpoint. + */ + HW_IN_EP(ep)->CFG = 0; + } } /** @@ -397,13 +595,16 @@ void usb_lld_init_endpoint(USBDriver *usbp, usbep_t ep) { * * @notapi */ -void usb_lld_disable_endpoints(USBDriver *usbp) { - (void)usbp; - - for (int i = 0; i < USB_MAX_ENDPOINTS; i++) { +void usb_lld_disable_endpoints(USBDriver* usbp) +{ + for (uint8_t i = 2; i < NUC123_USB_HW_ENDPOINTS; ++i) { USBD->EP[i].CFGP |= USBD_CFGP_CLRRDY_Msk; USBD->EP[i].CFG &= ~USBD_CFG_STATE_Msk; } + + sram_free_dword_offset = 1UL + + BYTES_TO_NEXT_DWORD(usbp->epc[0]->in_maxsize) + + BYTES_TO_NEXT_DWORD(usbp->epc[0]->out_maxsize); } /** @@ -418,11 +619,20 @@ void usb_lld_disable_endpoints(USBDriver *usbp) { * * @notapi */ -usbepstatus_t usb_lld_get_status_out(USBDriver *usbp, usbep_t ep) { - (void)usbp; - (void)ep; +usbepstatus_t usb_lld_get_status_out(USBDriver* usbp, usbep_t ep) +{ - return EP_STATUS_DISABLED; + (void)usbp; + + if (!(HW_OUT_EP(ep)->CFG & USBD_CFG_STATE_Msk)) { + return EP_STATUS_DISABLED; + } + + if (HW_OUT_EP(ep)->CFGP & USBD_CFGP_SSTALL_Msk) { + return EP_STATUS_STALLED; + } + + return EP_STATUS_ACTIVE; } /** @@ -437,8 +647,19 @@ usbepstatus_t usb_lld_get_status_out(USBDriver *usbp, usbep_t ep) { * * @notapi */ -usbepstatus_t usb_lld_get_status_in(USBDriver *usbp, usbep_t ep) { - return EP_STATUS_DISABLED; +usbepstatus_t usb_lld_get_status_in(USBDriver* usbp, usbep_t ep) +{ + (void)usbp; + + if (!(HW_IN_EP(ep)->CFG & USBD_CFG_STATE_Msk)) { + return EP_STATUS_DISABLED; + } + + if (HW_IN_EP(ep)->CFGP & USBD_CFGP_SSTALL_Msk) { + return EP_STATUS_STALLED; + } + + return EP_STATUS_ACTIVE; } /** @@ -455,19 +676,36 @@ usbepstatus_t usb_lld_get_status_in(USBDriver *usbp, usbep_t ep) { * * @notapi */ -void usb_lld_read_setup(USBDriver *usbp, usbep_t ep, uint8_t *buf) { +void usb_lld_read_setup(USBDriver* usbp, usbep_t ep, uint8_t* buf) +{ + /* The setup packet should be exactly 8 bytes long. + We skip the frills of memcpy because it is always word-aligned, + and always 8 bytes long. However, we drop down into assembly because + we need to guarantee we only ever use word accesses to SRAM + (limitation of the hardware), and while I doubt any compiler + would optimize 32-bit reads and writes to multiple ldrb/strb + pairs, why not easily get the guarantee? + */ + + uint32_t rm, rn; + + __ASM volatile( + "ldr %0, [%[src], #0]\n" + "str %0, [%[dest], #0]\n" + "ldr %0, [%[src], #4]\n" + "str %0, [%[dest], #4]\n" + : "=&r"(rm) /* Output operands */ + : [src] "r"(USBD_SRAM_BASE), [dest] "r"(buf) /* Input operands */ + : "memory" /* Clobber list */ + ); + + (void)rn; + (void)rm; (void)usbp; (void)ep; - int i; - for (i = 0; i < 8; i++) { - *buf = usbd_sram[i]; - //*buf = ep0setup_buffer[i]; - ep0setup_buffer[i] = usbd_sram[i]; - buf += 1; - } + (void)buf; } - /** * @brief Starts a receive operation on an OUT endpoint. * @@ -476,17 +714,10 @@ void usb_lld_read_setup(USBDriver *usbp, usbep_t ep, uint8_t *buf) { * * @notapi */ -void usb_lld_start_out(USBDriver *usbp, usbep_t ep) { - uint32_t rxcnt; - USBOutEndpointState *oesp = usbp->epc[ep]->out_state; - if (oesp->rxsize > usbp->epc[ep]->out_maxsize) { - rxcnt = usbp->epc[ep]->out_maxsize; - } - else { - rxcnt = oesp->rxsize; - } - _toggle_dsq(oesp->hwEp, &(oesp->dsq)); - USBD->EP[oesp->hwEp].MXPLD = rxcnt; +void usb_lld_start_out(USBDriver* usbp, usbep_t ep) +{ + HW_OUT_EP(ep)->MXPLD = + min(usbp->epc[ep]->out_state->rxsize, usbp->epc[ep]->out_maxsize); } /** @@ -497,20 +728,13 @@ void usb_lld_start_out(USBDriver *usbp, usbep_t ep) { * * @notapi */ -void usb_lld_start_in(USBDriver *usbp, usbep_t ep) { - uint32_t i, txcnt; - USBInEndpointState *iesp = usbp->epc[ep]->in_state; - if (iesp->txsize > usbp->epc[ep]->in_maxsize) { - txcnt = usbp->epc[ep]->in_maxsize; - } - else { - txcnt = iesp->txsize; - } - for (i = 0; i < txcnt; i++) { - usbd_sram[(USBD->EP[iesp->hwEp].BUFSEG) + i] = iesp->txbuf[i]; - } - _toggle_dsq(iesp->hwEp, &(iesp->dsq)); - USBD->EP[iesp->hwEp].MXPLD = txcnt; +void usb_lld_start_in(USBDriver* usbp, usbep_t ep) +{ + uint32_t txsize = + min(usbp->epc[ep]->in_state->txsize, usbp->epc[ep]->in_maxsize); + HW_IN_EP(ep)->MXPLD = usb_memcpy(USBD_SRAM_BASE + (HW_IN_EP(ep)->BUFSEG), + usbp->epc[ep]->in_state->txbuf, + txsize); } /** @@ -521,9 +745,10 @@ void usb_lld_start_in(USBDriver *usbp, usbep_t ep) { * * @notapi */ -void usb_lld_stall_out(USBDriver *usbp, usbep_t ep) { - USBOutEndpointState *oesp = usbp->epc[ep]->out_state; - USBD->EP[oesp->hwEp].CFGP |= (USBD_CFGP_SSTALL_Msk | USBD_CFGP_CLRRDY_Msk); +void usb_lld_stall_out(USBDriver* usbp, usbep_t ep) +{ + (void)usbp; + HW_OUT_EP(ep)->CFGP |= (USBD_CFGP_SSTALL_Msk | USBD_CFGP_CLRRDY_Msk); } /** @@ -534,9 +759,10 @@ void usb_lld_stall_out(USBDriver *usbp, usbep_t ep) { * * @notapi */ -void usb_lld_stall_in(USBDriver *usbp, usbep_t ep) { - USBInEndpointState *iesp = usbp->epc[ep]->in_state; - USBD->EP[iesp->hwEp].CFGP |= (USBD_CFGP_SSTALL_Msk | USBD_CFGP_CLRRDY_Msk); +void usb_lld_stall_in(USBDriver* usbp, usbep_t ep) +{ + (void)usbp; + HW_IN_EP(ep)->CFGP |= (USBD_CFGP_SSTALL_Msk | USBD_CFGP_CLRRDY_Msk); } /** @@ -547,9 +773,10 @@ void usb_lld_stall_in(USBDriver *usbp, usbep_t ep) { * * @notapi */ -void usb_lld_clear_out(USBDriver *usbp, usbep_t ep) { - USBOutEndpointState *oesp = usbp->epc[ep]->out_state; - USBD->EP[oesp->hwEp].CFGP &= ~USBD_CFGP_SSTALL_Msk; +void usb_lld_clear_out(USBDriver* usbp, usbep_t ep) +{ + (void)usbp; + HW_OUT_EP(ep)->CFGP &= ~USBD_CFGP_SSTALL_Msk; } /** @@ -560,11 +787,13 @@ void usb_lld_clear_out(USBDriver *usbp, usbep_t ep) { * * @notapi */ -void usb_lld_clear_in(USBDriver *usbp, usbep_t ep) { - USBInEndpointState *iesp = usbp->epc[ep]->in_state; - USBD->EP[iesp->hwEp].CFGP &= ~USBD_CFGP_SSTALL_Msk; +void usb_lld_clear_in(USBDriver* usbp, usbep_t ep) +{ + (void)usbp; + HW_IN_EP(ep)->CFG &= ~USBD_CFG_DSQ_SYNC_Msk; + HW_IN_EP(ep)->CFGP &= ~USBD_CFGP_SSTALL_Msk; } -#endif /* HAL_USE_USB == TRUE */ +#endif /* HAL_USE_USB */ /** @} */ diff --git a/os/hal/ports/NUMICRO/LLD/USBv1/hal_usb_lld.h b/os/hal/ports/NUMICRO/LLD/USBv1/hal_usb_lld.h index 5acc2973..dd7f709a 100644 --- a/os/hal/ports/NUMICRO/LLD/USBv1/hal_usb_lld.h +++ b/os/hal/ports/NUMICRO/LLD/USBv1/hal_usb_lld.h @@ -1,7 +1,6 @@ /* - ChibiOS - Copyright (C) 2017 Frank Zschockelt - ChibiOS - Copyright (C) 2019 /u/KeepItUnder - + Copyright (C) 2020 Alex Lewontin + Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at @@ -26,7 +25,7 @@ #ifndef HAL_USB_LLD_H #define HAL_USB_LLD_H -#if (HAL_USE_USB == TRUE) || defined(__DOXYGEN__) +#if HAL_USE_USB || defined(__DOXYGEN__) /*===========================================================================*/ /* Driver constants. */ @@ -34,23 +33,29 @@ /** * @brief Maximum endpoint address. - */ -#define USB_MAX_ENDPOINTS 8 + * @details This value does not include the endpoint 0 which is always present. +*/ +#define USB_MAX_ENDPOINTS 3 /** * @brief Status stage handling method. */ -#define USB_EP0_STATUS_STAGE USB_EP0_STATUS_STAGE_SW +#define USB_EP0_STATUS_STAGE USB_EP0_STATUS_STAGE_SW /** * @brief The address can be changed immediately upon packet reception. */ -#define USB_SET_ADDRESS_MODE USB_LATE_SET_ADDRESS +#define USB_SET_ADDRESS_MODE USB_LATE_SET_ADDRESS /** * @brief Method for set address acknowledge. */ -#define USB_SET_ADDRESS_ACK_HANDLING USB_SET_ADDRESS_ACK_SW +#define USB_SET_ADDRESS_ACK_HANDLING USB_SET_ADDRESS_ACK_SW + +/** + * @brief Speed of the USB hardware's input clock. This must be 48 MHz. + */ +#define NUC123_USBD_CLK 48000000UL /*===========================================================================*/ /* Driver pre-compile time settings. */ @@ -63,34 +68,37 @@ /** * @brief USB driver enable switch. * @details If set to @p TRUE the support for USB1 is included. - * @note The default is @p FALSE. + * @note The default is @p TRUE. */ #if !defined(NUC123_USB_USE_USB1) || defined(__DOXYGEN__) -#define NUC123_USB_USE_USB1 FALSE +#define NUC123_USB_USE_USB1 TRUE #endif /** - * @brief USB interrupt priority level setting. + * @brief USB1 interrupt priority level setting. */ -#if !defined(NUC123_USB_USB1_IRQ_PRIORITY) || defined(__DOXYGEN__) -#define NUC123_USB_USB1_IRQ_PRIORITY 2 +#if !defined(NUC123_USB_IRQ_PRIORITY) || defined(__DOXYGEN__) +#define NUC123_USB_IRQ_PRIORITY 3 #endif - -/** - * @brief Host wake-up procedure duration. - */ -#if !defined(USB_HOST_WAKEUP_DURATION) || defined(__DOXYGEN__) -#define USB_HOST_WAKEUP_DURATION 2 -#endif - /** @} */ /*===========================================================================*/ /* Derived constants and error checks. */ /*===========================================================================*/ -#if (USB_HOST_WAKEUP_DURATION < 2) || (USB_HOST_WAKEUP_DURATION > 15) -#error "invalid USB_HOST_WAKEUP_DURATION setting, it must be between 2 and 15" +#if NUC123_PLLCLK < NUC123_USBD_CLK +#error "NUC123_PLLCLK must be at least 48 MHz for USB" +#endif + +#define NUC123_USBD_CLKDIV ((NUC123_PLLCLK) / (NUC123_USBD_CLK)) + +#if ((NUC123_USBD_CLKDIV * NUC123_USBD_CLK) != NUC123_PLLCLK) || \ + (16 < NUC123_USBD_CLKDIV) +#error "Cannot generate required 48MHz from the configured PLL frequency" +#endif + +#if !defined(NUC123_USB_USE_USB1) || (!NUC123_USB_USE_USB1) +#error "NUC123 USB subsystem enabled, but no driver configured" #endif /*===========================================================================*/ @@ -104,24 +112,22 @@ typedef struct { /** * @brief Requested transmit transfer size. */ - size_t txsize; + size_t txsize; /** * @brief Transmitted bytes so far. */ - size_t txcnt; + size_t txcnt; /** * @brief Pointer to the transmission linear buffer. */ - const uint8_t *txbuf; -#if (USB_USE_WAIT == TRUE) || defined(__DOXYGEN__) + const uint8_t *txbuf; +#if USB_USE_WAIT || defined(__DOXYGEN__) /** * @brief Waiting thread. */ - thread_reference_t thread; + thread_reference_t thread; #endif - /* End of the mandatory fields.*/ - uint8_t hwEp; - uint8_t dsq; + /* End of the mandatory fields.*/ } USBInEndpointState; /** @@ -131,24 +137,22 @@ typedef struct { /** * @brief Requested receive transfer size. */ - size_t rxsize; + size_t rxsize; /** * @brief Received bytes so far. */ - size_t rxcnt; + size_t rxcnt; /** * @brief Pointer to the receive linear buffer. */ - uint8_t *rxbuf; -#if (USB_USE_WAIT == TRUE) || defined(__DOXYGEN__) + uint8_t *rxbuf; +#if USB_USE_WAIT || defined(__DOXYGEN__) /** * @brief Waiting thread. */ - thread_reference_t thread; + thread_reference_t thread; #endif /* End of the mandatory fields.*/ - uint8_t hwEp; - uint8_t dsq; } USBOutEndpointState; /** @@ -159,7 +163,7 @@ typedef struct { /** * @brief Type and mode of the endpoint. */ - uint32_t ep_mode; + uint32_t ep_mode; /** * @brief Setup packet notification callback. * @details This callback is invoked when a setup packet has been @@ -170,53 +174,43 @@ typedef struct { * endpoints, it should be set to @p NULL for other endpoint * types. */ - usbepcallback_t setup_cb; + usbepcallback_t setup_cb; /** * @brief IN endpoint notification callback. * @details This field must be set to @p NULL if the IN endpoint is not * used. */ - usbepcallback_t in_cb; + usbepcallback_t in_cb; /** * @brief OUT endpoint notification callback. * @details This field must be set to @p NULL if the OUT endpoint is not * used. */ - usbepcallback_t out_cb; + usbepcallback_t out_cb; /** * @brief IN endpoint maximum packet size. * @details This field must be set to zero if the IN endpoint is not * used. */ - uint16_t in_maxsize; + uint16_t in_maxsize; /** * @brief OUT endpoint maximum packet size. * @details This field must be set to zero if the OUT endpoint is not * used. */ - uint16_t out_maxsize; + uint16_t out_maxsize; /** * @brief @p USBEndpointState associated to the IN endpoint. * @details This structure maintains the state of the IN endpoint. */ - USBInEndpointState *in_state; + USBInEndpointState *in_state; /** * @brief @p USBEndpointState associated to the OUT endpoint. * @details This structure maintains the state of the OUT endpoint. */ - USBOutEndpointState *out_state; + USBOutEndpointState *out_state; /* End of the mandatory fields.*/ - /** - * @brief Reserved field, not currently used. - * @note Initialize this field to 1 in order to be forward compatible. - */ - uint16_t ep_buffers; - /** - * @brief Pointer to a buffer for setup packets. - * @details Setup packets require a dedicated 8-bytes buffer, set this - * field to @p NULL for non-control endpoints. - */ - uint8_t *setup_buf; + } USBEndpointConfig; /** @@ -227,22 +221,22 @@ typedef struct { * @brief USB events callback. * @details This callback is invoked when an USB driver event is registered. */ - usbeventcb_t event_cb; + usbeventcb_t event_cb; /** * @brief Device GET_DESCRIPTOR request callback. * @note This callback is mandatory and cannot be set to @p NULL. */ - usbgetdescriptor_t get_descriptor_cb; + usbgetdescriptor_t get_descriptor_cb; /** * @brief Requests hook callback. * @details This hook allows to be notified of standard requests or to * handle non standard requests. */ - usbreqhandler_t requests_hook_cb; + usbreqhandler_t requests_hook_cb; /** * @brief Start Of Frame callback. */ - usbcallback_t sof_cb; + usbcallback_t sof_cb; /* End of the mandatory fields.*/ } USBConfig; @@ -253,79 +247,77 @@ struct USBDriver { /** * @brief Driver state. */ - usbstate_t state; + usbstate_t state; /** * @brief Current configuration data. */ - const USBConfig *config; + const USBConfig *config; /** * @brief Bit map of the transmitting IN endpoints. */ - uint16_t transmitting; + uint16_t transmitting; /** * @brief Bit map of the receiving OUT endpoints. */ - uint16_t receiving; + uint16_t receiving; /** * @brief Active endpoints configurations. */ - const USBEndpointConfig *epc[USB_MAX_ENDPOINTS + 1]; + const USBEndpointConfig *epc[USB_MAX_ENDPOINTS + 1]; /** * @brief Fields available to user, it can be used to associate an * application-defined handler to an IN endpoint. * @note The base index is one, the endpoint zero does not have a * reserved element in this array. */ - void *in_params[USB_MAX_ENDPOINTS]; + void *in_params[USB_MAX_ENDPOINTS]; /** * @brief Fields available to user, it can be used to associate an * application-defined handler to an OUT endpoint. * @note The base index is one, the endpoint zero does not have a * reserved element in this array. */ - void *out_params[USB_MAX_ENDPOINTS]; + void *out_params[USB_MAX_ENDPOINTS]; /** * @brief Endpoint 0 state. */ - usbep0state_t ep0state; + usbep0state_t ep0state; /** * @brief Next position in the buffer to be transferred through endpoint 0. */ - uint8_t *ep0next; + uint8_t *ep0next; /** * @brief Number of bytes yet to be transferred through endpoint 0. */ - size_t ep0n; + size_t ep0n; /** * @brief Endpoint 0 end transaction callback. */ - usbcallback_t ep0endcb; + usbcallback_t ep0endcb; /** * @brief Setup packet buffer. */ - uint8_t setup[8]; + uint8_t setup[8]; /** * @brief Current USB device status. */ - uint16_t status; + uint16_t status; /** * @brief Assigned USB address. */ - uint8_t address; + uint8_t address; /** * @brief Current USB device configuration. */ - uint8_t configuration; + uint8_t configuration; /** * @brief State of the driver when a suspend happened. */ - usbstate_t saved_state; + usbstate_t saved_state; #if defined(USB_DRIVER_EXT_FIELDS) USB_DRIVER_EXT_FIELDS #endif /* End of the mandatory fields.*/ - uint32_t bufnext; - uint8_t epnext; }; /*===========================================================================*/ @@ -364,14 +356,20 @@ struct USBDriver { * * @api */ -#define usb_lld_connect_bus(usbp) (USBD->ATTR |= USBD_ATTR_USB_EN_Msk | USBD_ATTR_PHY_EN_Msk) +#define usb_lld_connect_bus(usbp) \ + do { \ + USBD->DRVSE0 = (0 << USBD_DRVSE0_DRVSE0_Pos); \ + } while (0) /** * @brief Disconnect the USB device. * * @api */ -#define usb_lld_disconnect_bus(usbp) (USBD->ATTR &= ~USBD_ATTR_USB_EN_Msk) +#define usb_lld_disconnect_bus(usbp) \ + do { \ + USBD->DRVSE0 = (1 << USBD_DRVSE0_DRVSE0_Pos); \ + } while (0) /** * @brief Start of host wake-up procedure. @@ -379,46 +377,45 @@ struct USBDriver { * @notapi */ #define usb_lld_wakeup_host(usbp) \ - do{ \ + do { \ USBD->ATTR |= USBD_ATTR_RWAKEUP_Msk; \ - osalThreadSleepMilliseconds(USB_HOST_WAKEUP_DURATION); \ + osalThreadSleepMilliseconds(10); \ USBD->ATTR &= ~USBD_ATTR_RWAKEUP_Msk; \ - } while (false) + _usb_wakeup(&USBD1); \ + } while (0) /*===========================================================================*/ /* External declarations. */ /*===========================================================================*/ -#if (NUC123_USB_USE_USB1 == TRUE) && !defined(__DOXYGEN__) +#if NUC123_USB_USE_USB1 && !defined(__DOXYGEN__) extern USBDriver USBD1; #endif #ifdef __cplusplus extern "C" { #endif - void usb_lld_init(void); - void usb_lld_start(USBDriver *usbp); - void usb_lld_stop(USBDriver *usbp); - void usb_lld_reset(USBDriver *usbp); - void usb_lld_set_address(USBDriver *usbp); - void usb_lld_init_endpoint(USBDriver *usbp, usbep_t ep); - void usb_lld_disable_endpoints(USBDriver *usbp); - usbepstatus_t usb_lld_get_status_in(USBDriver *usbp, usbep_t ep); - usbepstatus_t usb_lld_get_status_out(USBDriver *usbp, usbep_t ep); - void usb_lld_read_setup(USBDriver *usbp, usbep_t ep, uint8_t *buf); - void usb_lld_prepare_receive(USBDriver *usbp, usbep_t ep); - void usb_lld_prepare_transmit(USBDriver *usbp, usbep_t ep); - void usb_lld_start_out(USBDriver *usbp, usbep_t ep); - void usb_lld_start_in(USBDriver *usbp, usbep_t ep); - void usb_lld_stall_out(USBDriver *usbp, usbep_t ep); - void usb_lld_stall_in(USBDriver *usbp, usbep_t ep); - void usb_lld_clear_out(USBDriver *usbp, usbep_t ep); - void usb_lld_clear_in(USBDriver *usbp, usbep_t ep); +void usb_lld_init(void); +void usb_lld_start(USBDriver *usbp); +void usb_lld_stop(USBDriver *usbp); +void usb_lld_reset(USBDriver *usbp); +void usb_lld_set_address(USBDriver *usbp); +void usb_lld_init_endpoint(USBDriver *usbp, usbep_t ep); +void usb_lld_disable_endpoints(USBDriver *usbp); +usbepstatus_t usb_lld_get_status_in(USBDriver *usbp, usbep_t ep); +usbepstatus_t usb_lld_get_status_out(USBDriver *usbp, usbep_t ep); +void usb_lld_read_setup(USBDriver *usbp, usbep_t ep, uint8_t *buf); +void usb_lld_start_out(USBDriver *usbp, usbep_t ep); +void usb_lld_start_in(USBDriver *usbp, usbep_t ep); +void usb_lld_stall_out(USBDriver *usbp, usbep_t ep); +void usb_lld_stall_in(USBDriver *usbp, usbep_t ep); +void usb_lld_clear_out(USBDriver *usbp, usbep_t ep); +void usb_lld_clear_in(USBDriver *usbp, usbep_t ep); #ifdef __cplusplus } #endif -#endif /* HAL_USE_USB == TRUE */ +#endif /* HAL_USE_USB */ #endif /* HAL_USB_LLD_H */ diff --git a/os/hal/ports/NUMICRO/LLD/USBv1/usb_memcpy.S b/os/hal/ports/NUMICRO/LLD/USBv1/usb_memcpy.S new file mode 100644 index 00000000..90a6126e --- /dev/null +++ b/os/hal/ports/NUMICRO/LLD/USBv1/usb_memcpy.S @@ -0,0 +1,159 @@ +/* + ChibiOS - Copyright (C) 2020 Alex Lewontin + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/** + * @file usb_memcpy.S + * @brief Definition of the usb_memcpy function + * + * @addtogroup USB + * @{ + */ + +#if !defined(__DOXYGEN__) + + .syntax unified + .cpu cortex-m0 + .thumb + + .section .text, "ax" + .align 4 + .global usb_memcpy + +/* size_t usb_memcpy(volatile void* destination, const volatile void* source, size_t num); */ +usb_memcpy: + cmp r2, #0 + beq.n zero_return + push {r2, r4, r5, r6, r7, lr} + + movs r3, #3 + +check_alignment: + tst r0, r3 + bne.n offset + tst r1, r3 + bne.n unaligned + +aligned: +/* r0: src, r1: dest, r2: num, r3: #3 */ + movs r4, r2 + + bics r4, r3 + movs r6, r4 + + b.n aligned_loop_check +aligned_loop_top: + subs r4, r4, #4 + ldr r5, [r1, r4] + str r5, [r0, r4] +aligned_loop_check: + bne.n aligned_loop_top + + +leftover_check: + ands r2, r3 + beq.n return + +/* if there are leftovers, we can read them all in one */ + +/* byte mode */ + ldr r4, =0x40060000 + ldr r5, [r4, #16] + movs r7, #128 + lsls r7, r7, #3 /* USBD_ATTR_BYTEM_Msk */ + orrs r5, r7 + str r5, [r4, #16] + adds r1, r1, r6 + adds r0, r0, r6 + +/* + * r0: src + * r1: dest + * r2 is 1, 2, or 3 (# of bytes left) + * r3: #3 + * r4: &USBD + * r5: USBD->ATTR + * r6: leftover offset base + * r7: USBD_ATTR_BYTEM_Msk + */ + ldrb r3, [r1, #0] + strb r3, [r0, #0] + + cmp r2, #1 + beq.n word_mode_return + + ldrb r3, [r1, #1] + strb r3, [r0, #1] + + cmp r2, #2 + beq.n word_mode_return + + ldrb r3, [r1, #2] + strb r3, [r0, #2] + +word_mode_return: + ldr r5, [r4, #16] + bics r5, r7 + str r5, [r4, #16] +return: + pop {r0, r4, r5, r6, r7, pc} +zero_return: + movs r0, #0 + bx.n lr + +offset: + ldr r4, =0x40060000 + ldr r5, [r4, #16] + movs r7, #128 + lsls r7, r7, #3 /* USBD_ATTR_BYTEM_Msk */ + orrs r5, r7 + str r5, [r4, #16] + +offset_loop: + ldrb r6, [r0, #0] + strb r6, [r1, #0] + adds r0, r0, #1 + adds r1, r1, #1 + subs r2, r2, #1 + + beq.n word_mode_return + tst r0, r3 + bne.n offset_loop + tst r1, r3 + bne.n unaligned_loop_top + + ldr r5, [r4, #16] + bics r5, r7 + str r5, [r4, #16] + b.n aligned + +unaligned: + ldr r4, =0x40060000 + ldr r5, [r4, #16] + movs r3, #128 + lsls r3, r3, #3 /* USBD_ATTR_BYTEM_Msk */ + orrs r5, r3 + str r5, [r4, #16] + +unaligned_loop_top: + ldrb r5, [r1, r2] + strb r5, [r0, r2] + subs r2, r2, #1 +unaligned_loop_check: + bne.n unaligned_loop_top + + b.n word_mode_return + +#endif diff --git a/os/hal/ports/NUMICRO/NUC123/hal_lld.c b/os/hal/ports/NUMICRO/NUC123/hal_lld.c index 2d13d697..26e18d0d 100644 --- a/os/hal/ports/NUMICRO/NUC123/hal_lld.c +++ b/os/hal/ports/NUMICRO/NUC123/hal_lld.c @@ -17,7 +17,7 @@ /** * @file hal_lld.c - * @brief NUC123xxxANx HAL subsystem low level driver source. + * @brief NUC123 HAL subsystem low level driver source. * * @addtogroup HAL * @{ @@ -410,17 +410,17 @@ static uint32_t set_core_clock(uint32_t clkCore) /* Is HXT stable ? */ if (CLK->CLKSTATUS & CLK_CLKSTATUS_XTL12M_STB_Msk) { /* Use __HXT as PLL source */ - clkCore = enable_pll(NUC123_PLLSRC_HSE, (clkCore)); + clkCore = enable_pll(NUC123_PLLSRC_HSE, (2 * clkCore)); } else { /* Use __HIRC as PLL source */ - clkCore = enable_pll(NUC123_PLLSRC_HSI, (clkCore)); + clkCore = enable_pll(NUC123_PLLSRC_HSI, (2 * clkCore)); /* Read HIRC clock source stable flag again (since we're using it now) */ stableHIRC = CLK->CLKSTATUS & CLK_CLKSTATUS_OSC22M_STB_Msk; } /* Set HCLK clock source to PLL */ - set_HCLK(NUC123_HCLKSRC_PLL, CLK_CLKDIV_HCLK(1)); + set_HCLK(NUC123_HCLKSRC_PLL_2, CLK_CLKDIV_HCLK(1)); /* Disable HIRC if HIRC was disabled before we started */ if (stableHIRC == 0) { diff --git a/os/hal/ports/NUMICRO/NUC123/hal_lld.h b/os/hal/ports/NUMICRO/NUC123/hal_lld.h index f576dea4..639c77b6 100644 --- a/os/hal/ports/NUMICRO/NUC123/hal_lld.h +++ b/os/hal/ports/NUMICRO/NUC123/hal_lld.h @@ -158,25 +158,11 @@ #define NUC123_PLL_ENABLED FALSE #endif -/** - * @brief Main clock source selection. - */ -#if !defined(NUC123_HCLKSRC) || defined(__DOXYGEN__) -#define NUC123_HCLKSRC NUC123_HCLKSRC_HSI -#endif - -/** - * @brief Main clock divider value. - */ -#if !defined(NUC123_HCLKDIV) || defined(__DOXYGEN__) -#define NUC123_HCLKDIV 1 -#endif - /** * @brief Clock source for the PLL. */ -#if !defined(NUC123_PLLCLK) || defined(__DOXYGEN__) -#define NUC123_PLLCLK 72000000UL +#if !defined(NUC123_HCLK) || defined(__DOXYGEN__) +#define NUC123_HCLK 72000000UL #endif /** @} */ @@ -203,73 +189,7 @@ #endif #endif -/* - * PLL checks - */ - -#if NUC123_PLL_ENABLED - -#if !defined(NUC123_PLLCLK) -#error "PLL frequency not defined" -#endif - -#if (NUC123_PLLCLK < NUC123_PLLCLK_MIN) || \ - (NUC123_PLLCLK > NUC123_PLLCLK_MAX) -#error \ - "NUC123_PLLCLK outside acceptable range (NUC123_PLLCLK_MIN...NUC123_PLLCLK_MAX)" -#endif -#endif /* NUC123_PLL_ENABLED */ - -/* - * HCLK checks. - */ - -#if (NUC123_HCLKDIV < NUC123_HCLKDIV_MIN || \ - NUC123_HCLKDIV > NUC123_HCLKDIV_MAX) -#error \ - "NUC123_HCLKDIV outside acceptable range (NUC123_HCLKDIV_MIN...NUC123_HCLDIV_MAX)" -#endif - -#if NUC123_HCLKSRC == NUC123_HCLKSRC_HSE - -#if !NUC123_HSE_ENABLED -#error "Cannot use HSE as a HCLK source if it is disabled." -#endif - -#define NUC123_HCLK (NUC123_HSECLK / NUC123_HCLKDIV) - -#elif NUC123_HCLKSRC == NUC123_HCLKSRC_PLL_2 - -#if !NUC123_PLL_ENABLED -#error "Cannot use PLL as a HCLK source if it is disabled." -#endif - -#define NUC123_HCLK (NUC123_PLLCLK / 2) - -#elif NUC123_HCLKSRC == NUC123_HCLKSRC_PLL - -#if !NUC123_PLL_ENABLED -#error "Cannot use PLL as a HCLK source if it is disabled." -#endif - -#define NUC123_HCLK (NUC123_PLLCLK / NUC123_HCLKDIV) - -#elif NUC123_HCLKSRC == NUC123_HCLKSRC_LSI - -#if !NUC123_LSI_ENABLED -#error "Cannot use LSI as a HCLK source if it is disabled." -#endif - -#define NUC123_HCLK (NUC123_LSICLK / NUC123_HCLKDIV) - -#elif NUC123_HCLKSRC == NUC123_HCLKSRC_HSI -#if !NUC123_HSI_ENABLED -#error "Cannot use HSI as a HCLK source if it is disabled." -#endif - -#define NUC123_HCLK (NUC123_HSICLK / NUC123_HCLKDIV) - -#endif +#define NUC123_PLLCLK (NUC123_HCLK * 2) /*===========================================================================*/ /* Driver data structures and types. */ diff --git a/os/hal/ports/NUMICRO/NUC123/platform.mk b/os/hal/ports/NUMICRO/NUC123/platform.mk index e0f3e034..e29a300c 100644 --- a/os/hal/ports/NUMICRO/NUC123/platform.mk +++ b/os/hal/ports/NUMICRO/NUC123/platform.mk @@ -17,5 +17,6 @@ include $(CHIBIOS_CONTRIB)/os/hal/ports/NUMICRO/LLD/TIMv1/driver.mk include $(CHIBIOS_CONTRIB)/os/hal/ports/NUMICRO/LLD/USBv1/driver.mk # Shared variables -ALLCSRC += $(PLATFORMSRC) -ALLINC += $(PLATFORMINC) +ALLCSRC += $(PLATFORMSRC) +ALLXASMSRC += $(PLATFORMASM) +ALLINC += $(PLATFORMINC) diff --git a/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/Client/darwin/.skip b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/Client/darwin/.skip new file mode 100644 index 00000000..e69de29b diff --git a/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/Client/darwin/Makefile b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/Client/darwin/Makefile new file mode 100644 index 00000000..67c3dd7e --- /dev/null +++ b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/Client/darwin/Makefile @@ -0,0 +1,33 @@ +HIDAPI_HEADER_PATH = /usr/local/include/hidapi + +CC=gcc +IFLAGS=-I$(HIDAPI_HEADER_PATH) +LDLIBS=-lhidapi +CFLAGS = -Wall -Wextra -O2 -g $(IFLAGS) + +BUILDDIR = ./build +DEPDIR = ./.dep + +SRCS = $(wildcard *.c) +OBJS = $(SRCS:%.c=$(BUILDDIR)/%.o) +EXE = $(BUILDDIR)/test-usb-hid + +all: $(EXE) + +$(EXE): $(OBJS) + +-include $(subst .c,.d,$(SRCS)) + +$(BUILDDIR)/%.o: %.o + mv $^ $@ + +$(DEPDIR)/%.d: %.c + $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -MM -MF $@ -MP -MT $(subst .c,.o,$<) $< + +clean: + rm -f $(EXE) + rm -f $(OBJS) + rm -f $(subst .c,.d,$(SRCS)) + rm -f *~ + +.PHONY: clean all diff --git a/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/Client/darwin/test-usb-hid.c b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/Client/darwin/test-usb-hid.c new file mode 100644 index 00000000..4d0c6731 --- /dev/null +++ b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/Client/darwin/test-usb-hid.c @@ -0,0 +1,177 @@ +/* + Copyright (c) 2014 Guillaume Duc + Modifications copyright (c) 2020 Alex Lewontin + + 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. + +*/ + +#include "hidapi.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define USB_HID_IN_REPORT_SIZE 1 +#define USB_HID_OUT_REPORT_SIZE 1 + +struct usb_hid_in_report_s { + uint8_t sequence_number; +}; + +struct usb_hid_out_report_s { + uint8_t sequence_number; +}; + +static uint8_t usb_hid_in_report_buf[USB_HID_IN_REPORT_SIZE]; +/* +1 for the report index */ +static uint8_t usb_hid_out_report_buf[USB_HID_OUT_REPORT_SIZE + 1]; + +static struct usb_hid_in_report_s* usb_hid_in_report = + (struct usb_hid_in_report_s*)usb_hid_in_report_buf; + +static struct usb_hid_out_report_s* usb_hid_out_report = + (struct usb_hid_out_report_s*)(&usb_hid_out_report_buf[1]); + +static hid_device* handle; + +static void close_hidapi() __attribute__((noreturn)); +static void close_client() __attribute__((noreturn)); + +static void read_in_report() +{ + int res, i; + + printf("read()\n"); + res = hid_read(handle, usb_hid_in_report_buf, USB_HID_IN_REPORT_SIZE); + if (res < 0) { + perror("read"); + exit(EXIT_FAILURE); + } else { + printf("read() read %d bytes:\t", res); + for (i = 0; i < res; i++) + printf("%02hhx ", usb_hid_in_report_buf[i]); + printf("\n"); + } +} + +static void send_out_report() +{ + int res; + + usb_hid_out_report_buf[0] = 0; + + res = + hid_write(handle, usb_hid_out_report_buf, USB_HID_OUT_REPORT_SIZE + 1); + if (res < 0) { + perror("write"); + exit(EXIT_FAILURE); + } + + usb_hid_out_report->sequence_number++; +} + +static void close_hidapi() +{ + int res = hid_exit(); + if (res) { + perror("Could not close hidapi library"); + exit(EXIT_FAILURE); + } + exit(EXIT_SUCCESS); +} + +static void close_client() +{ + hid_close(handle); + close_hidapi(); +} + +int main(int argc, char** argv) +{ + int res; + unsigned long vid, pid; + struct hid_device_info *devs, *cur_dev; + + if (argc < 2) { + fprintf(stderr, "Usage: %s [VID] [PID]\n", argv[0]); + exit(EXIT_FAILURE); + } + vid = strtoul(argv[1], NULL, 16); + pid = strtoul(argv[2], NULL, 16); + + devs = hid_enumerate(0x0, 0x0); + cur_dev = devs; + while (cur_dev) { + printf("Device Found\n type: %04hx %04hx\n path: %s\n serial_number: " + "%ls", + cur_dev->vendor_id, + cur_dev->product_id, + cur_dev->path, + cur_dev->serial_number); + printf("\n"); + printf(" Manufacturer: %ls\n", cur_dev->manufacturer_string); + printf(" Product: %ls\n", cur_dev->product_string); + printf(" Release: %hx\n", cur_dev->release_number); + printf(" Interface: %d\n", cur_dev->interface_number); + printf(" Usage (page): 0x%hx (0x%hx)\n", + cur_dev->usage, + cur_dev->usage_page); + printf("\n"); + cur_dev = cur_dev->next; + } + hid_free_enumeration(devs); + + /* Make sure we clean up on CTRL-C interrupt */ + signal(SIGINT, close_client); + + res = hid_init(); + if (res) { + perror("Could not load hidapi library"); + exit(EXIT_FAILURE); + } + handle = hid_open(vid, pid, NULL); + if (!handle) { + perror("Unable to open device"); + close_hidapi(); + exit(EXIT_FAILURE); + } + + usb_hid_out_report->sequence_number = 4; + send_out_report(); + + while (1) { + read_in_report(); + + if (usb_hid_in_report->sequence_number == 40) { + usb_hid_out_report->sequence_number = + usb_hid_in_report->sequence_number / 2; + send_out_report(); + } + } +} diff --git a/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/Client/linux/Makefile b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/Client/linux/Makefile new file mode 100644 index 00000000..ed84ee9a --- /dev/null +++ b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/Client/linux/Makefile @@ -0,0 +1,23 @@ +CC=gcc +CFLAGS = -Wall -Wextra -O2 -g + +SRCS = $(wildcard *.c) +OBJS = $(SRCS:%.c=%.o) +EXE = test-usb-hid + +all: $(EXE) + +$(EXE): $(OBJS) + +-include $(subst .c,.d,$(SRCS)) + +%.d: %.c + $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -MM -MF $@ -MP -MT $(subst .c,.o,$<) $< + +clean: + rm -f $(EXE) + rm -f $(OBJS) + rm -f $(subst .c,.d,$(SRCS)) + rm -f *~ + +.PHONY: clean all diff --git a/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/Client/linux/test-usb-hid.c b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/Client/linux/test-usb-hid.c new file mode 100644 index 00000000..f9f85735 --- /dev/null +++ b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/Client/linux/test-usb-hid.c @@ -0,0 +1,161 @@ +/* + + Copyright (c) 2014 Guillaume Duc + + 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. + +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define USB_HID_IN_REPORT_SIZE 1 +#define USB_HID_OUT_REPORT_SIZE 1 + +struct usb_hid_in_report_s { + uint8_t sequence_number; +}; + +struct usb_hid_out_report_s { + uint8_t sequence_number; +}; + +static uint8_t usb_hid_in_report_buf[USB_HID_IN_REPORT_SIZE]; +/* +1 for the report index */ +static uint8_t usb_hid_out_report_buf[USB_HID_OUT_REPORT_SIZE + 1]; + +static struct usb_hid_in_report_s* usb_hid_in_report = + (struct usb_hid_in_report_s*)usb_hid_in_report_buf; + +static struct usb_hid_out_report_s* usb_hid_out_report = + (struct usb_hid_out_report_s*)(&usb_hid_out_report_buf[1]); + +static int usb_hid_fd; +static uint8_t wkup_pb_old_value = 0; + +static void read_in_report() +{ + int res, i; + + printf("read()\n"); + res = read(usb_hid_fd, usb_hid_in_report_buf, USB_HID_IN_REPORT_SIZE); + if (res < 0) { + perror("read"); + exit(EXIT_FAILURE); + } else { + printf("read() read %d bytes:\t", res); + for (i = 0; i < res; i++) + printf("%02hhx ", usb_hid_in_report_buf[i]); + printf("\n"); + } +} + +static void send_out_report() +{ + int res; + + usb_hid_out_report_buf[0] = 0; + + res = write(usb_hid_fd, usb_hid_out_report_buf, USB_HID_OUT_REPORT_SIZE + 1); + if (res < 0) { + perror("write"); + exit(EXIT_FAILURE); + } + + usb_hid_out_report->sequence_number++; +} + +static void usb_hid_init(const char* dev_name) +{ + int i, res; + int desc_size = 0; + char buf[256]; + + struct hidraw_report_descriptor rpt_desc; + struct hidraw_devinfo info; + + usb_hid_fd = open(dev_name, O_RDWR); + + if (usb_hid_fd < 0) { + perror("Unable to open device"); + exit(EXIT_FAILURE); + } + + memset(&rpt_desc, 0x0, sizeof(rpt_desc)); + memset(&info, 0x0, sizeof(info)); + memset(buf, 0x0, sizeof(buf)); + + /* Get Report Descriptor Size */ + res = ioctl(usb_hid_fd, HIDIOCGRDESCSIZE, &desc_size); + if (res < 0) + perror("HIDIOCGRDESCSIZE"); + else + printf("Report Descriptor Size: %d\n", desc_size); + + /* Get Report Descriptor */ + rpt_desc.size = desc_size; + res = ioctl(usb_hid_fd, HIDIOCGRDESC, &rpt_desc); + if (res < 0) { + perror("HIDIOCGRDESC"); + } else { + printf("Report Descriptor:\n"); + for (i = 0; i < rpt_desc.size; i++) + printf("%02hhx ", rpt_desc.value[i]); + puts("\n"); + } +} + +int main(int argc, char** argv) +{ + if (argc < 2) { + fprintf(stderr, "Usage: %s /dev/hidrawX\n", argv[0]); + return EXIT_FAILURE; + } + + memset(usb_hid_out_report_buf, 0, sizeof(usb_hid_out_report_buf)); + + usb_hid_init(argv[1]); + usb_hid_out_report->sequence_number = 4; + send_out_report(); + + while (1) { + read_in_report(); + + if (usb_hid_in_report->sequence_number == 40) { + usb_hid_out_report->sequence_number = usb_hid_in_report->sequence_number / 2; + send_out_report(); + } + } + + close(usb_hid_fd); + + return EXIT_SUCCESS; +} diff --git a/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/Client/linux/udev.rules b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/Client/linux/udev.rules new file mode 100644 index 00000000..8c93f15b --- /dev/null +++ b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/Client/linux/udev.rules @@ -0,0 +1,2 @@ +SUBSYSTEM=="usb", ATTR{idVendor}=="0179", ATTR{idProduct}=="0002", MODE:="0666" +KERNEL=="hidraw*", SUBSYSTEM=="hidraw", ATTRS{idVendor}=="0179", ATTRS{idProduct}=="0002", MODE:="0666" diff --git a/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/Makefile b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/Makefile new file mode 100644 index 00000000..da1b85f1 --- /dev/null +++ b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/Makefile @@ -0,0 +1,205 @@ +############################################################################## +# Build global options +# NOTE: Can be overridden externally. +# +# Compiler options here. +ifeq ($(USE_OPT),) + USE_OPT = -ggdb3 -g3 -gdwarf-3 -gstrict-dwarf -fomit-frame-pointer -falign-functions=16 -pedantic -Wextra -Wall +endif + +# C specific options here (added to USE_OPT). +ifeq ($(USE_COPT),) + USE_COPT = +endif + +# C++ specific options here (added to USE_OPT). +ifeq ($(USE_CPPOPT),) + USE_CPPOPT = -fno-rtti +endif + +# Enable this if you want the linker to remove unused code and data. +ifeq ($(USE_LINK_GC),) + USE_LINK_GC = yes +endif + +# Linker extra options here. +ifeq ($(USE_LDOPT),) + USE_LDOPT = +endif + +# Enable this if you want link time optimizations (LTO). +ifeq ($(USE_LTO),) + USE_LTO = yes +endif + +# Enable this if you want to see the full log while compiling. +ifeq ($(USE_VERBOSE_COMPILE),) + USE_VERBOSE_COMPILE = no +endif + +# If enabled, this option makes the build process faster by not compiling +# modules not used in the current configuration. +ifeq ($(USE_SMART_BUILD),) + USE_SMART_BUILD = yes +endif + +# +# Build global options +############################################################################## + +############################################################################## +# Architecture or project specific options +# + +# Stack size to be allocated to the Cortex-M process stack. This stack is +# the stack used by the main() thread. +ifeq ($(USE_PROCESS_STACKSIZE),) + USE_PROCESS_STACKSIZE = 0x400 +endif + +# Stack size to the allocated to the Cortex-M main/exceptions stack. This +# stack is used for processing interrupts and exceptions. +ifeq ($(USE_EXCEPTIONS_STACKSIZE),) + USE_EXCEPTIONS_STACKSIZE = 0x400 +endif + +# Enables the use of FPU (no, softfp, hard). +ifeq ($(USE_FPU),) + USE_FPU = no +endif + +# FPU-related options. +ifeq ($(USE_FPU_OPT),) + USE_FPU_OPT = -mfloat-abi=$(USE_FPU) -mfpu=fpv4-sp-d16 +endif + +# +# Architecture or project specific options +############################################################################## + +############################################################################## +# Project, target, sources and paths +# + +# Define project name here +PROJECT = ch + +# Target settings. +MCU = cortex-m0 + +# Imported source files and paths. +BASE_PATH := /Users/lewontin/Documents/development/Projects +CHIBIOS := $(BASE_PATH)/ChibiOS/ChibiOS +CHIBIOS_CONTRIB := $(BASE_PATH)/ChibiOS/ChibiOS-Contrib +CONFDIR := ./cfg +BUILDDIR := ./build +DEPDIR := ./.dep + +# Licensing files. +include $(CHIBIOS)/os/license/license.mk +# Startup files. +include $(CHIBIOS_CONTRIB)/os/common/startup/ARMCMx/compilers/GCC/mk/startup_NUC123.mk +# HAL-OSAL files (optional). +#include $(CHIBIOS)/os/hal/hal.mk +include $(CHIBIOS_CONTRIB)/os/hal/hal.mk +include $(CHIBIOS_CONTRIB)/os/hal/ports/NUMICRO/NUC123/platform.mk +include $(CHIBIOS_CONTRIB)/os/hal/boards/NUTINY-SDK-NUC123-V2.0/board.mk +include $(CHIBIOS)/os/hal/osal/rt-nil/osal.mk +# RTOS files (optional). +include $(CHIBIOS)/os/rt/rt.mk +#include $(CHIBIOS)/os/common/ports/ARMv6-M/compilers/GCC/mk/port.mk +include $(CHIBIOS)/os/common/ports/ARMCMx/compilers/GCC/mk/port_v6m.mk +# Auto-build files in ./source recursively. +include $(CHIBIOS)/tools/mk/autobuild.mk +# Other files (optional). +#include $(CHIBIOS)/os/hal/lib/streams/streams.mk + +# Define linker script file here +LDSCRIPT= $(STARTUPLD_CONTRIB)/NUC123xD4xx0.ld + +# C sources that can be compiled in ARM or THUMB mode depending on the global +# setting. +CSRC = $(ALLCSRC) \ + $(TESTSRC) \ + usbcfg.c \ + main.c + +# C++ sources that can be compiled in ARM or THUMB mode depending on the global +# setting. +CPPSRC = $(ALLCPPSRC) + +# List ASM source files here. +ASMSRC = $(ALLASMSRC) + +# List ASM with preprocessor source files here. +ASMXSRC = $(ALLXASMSRC) + +# Inclusion directories. +INCDIR = $(CONFDIR) $(ALLINC) $(TESTINC) + +# Define C warning options here. +CWARN = -Wall -Wextra -Wundef -Wstrict-prototypes + +# Define C++ warning options here. +CPPWARN = -Wall -Wextra -Wundef + +# +# Project, target, sources and paths +############################################################################## + +############################################################################## +# Start of user section +# + +# List all user C define here, like -D_DEBUG=1 +UDEFS = + +# Define ASM defines here +UADEFS = + +# List all user directories here +UINCDIR = + +# List the user directory to look for the libraries here +ULIBDIR = + +# List all user libraries here +ULIBS = + +# +# End of user section +############################################################################## + +############################################################################## +# Common rules +# + +RULESPATH = $(CHIBIOS)/os/common/startup/ARMCMx/compilers/GCC/mk +include $(RULESPATH)/arm-none-eabi.mk +include $(RULESPATH)/rules.mk + +# +# Common rules +############################################################################## + +############################################################################## +# Custom rules +# + +READLINK:=greadlink +OPENOCD:=$(shell $(READLINK) -f `which openocd`) +OPENOCDPATH:=$(shell dirname $(OPENOCD))/../share/openocd + +install-openocd-config: + rm $(OPENOCDPATH)/scripts/target/numicroM0.cfg && cp numicroM0.cfg $(OPENOCDPATH)/scripts/target/ + +connect: + openocd -f ../scripts/interface/nulink.cfg -f ../scripts/target/numicroM0.cfg + +flash: $(BUILDDIR)/$(PROJECT).elf + openocd -f ../scripts/interface/nulink.cfg -f ../scripts/target/numicroM0.cfg -c "program $< verify reset exit" + + +# +# Custom rules +############################################################################## diff --git a/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/cfg/chconf.h b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/cfg/chconf.h new file mode 100644 index 00000000..5568045f --- /dev/null +++ b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/cfg/chconf.h @@ -0,0 +1,766 @@ +/* + ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/** + * @file rt/templates/chconf.h + * @brief Configuration file template. + * @details A copy of this file must be placed in each project directory, it + * contains the application specific kernel settings. + * + * @addtogroup config + * @details Kernel related settings and hooks. + * @{ + */ + +#ifndef CHCONF_H +#define CHCONF_H + +#define _CHIBIOS_RT_CONF_ +#define _CHIBIOS_RT_CONF_VER_6_1_ + +/*===========================================================================*/ +/** + * @name System timers settings + * @{ + */ +/*===========================================================================*/ + +/** + * @brief System time counter resolution. + * @note Allowed values are 16, 32 or 64 bits. + */ +#if !defined(CH_CFG_ST_RESOLUTION) +#define CH_CFG_ST_RESOLUTION 32 +#endif + +/** + * @brief System tick frequency. + * @details Frequency of the system timer that drives the system ticks. This + * setting also defines the system tick time unit. + */ +#if !defined(CH_CFG_ST_FREQUENCY) +#define CH_CFG_ST_FREQUENCY 10000 +#endif + +/** + * @brief Time intervals data size. + * @note Allowed values are 16, 32 or 64 bits. + */ +#if !defined(CH_CFG_INTERVALS_SIZE) +#define CH_CFG_INTERVALS_SIZE 32 +#endif + +/** + * @brief Time types data size. + * @note Allowed values are 16 or 32 bits. + */ +#if !defined(CH_CFG_TIME_TYPES_SIZE) +#define CH_CFG_TIME_TYPES_SIZE 32 +#endif + +/** + * @brief Time delta constant for the tick-less mode. + * @note If this value is zero then the system uses the classic + * periodic tick. This value represents the minimum number + * of ticks that is safe to specify in a timeout directive. + * The value one is not valid, timeouts are rounded up to + * this value. + */ +#if !defined(CH_CFG_ST_TIMEDELTA) +#define CH_CFG_ST_TIMEDELTA 0 +#endif + +/** @} */ + +/*===========================================================================*/ +/** + * @name Kernel parameters and options + * @{ + */ +/*===========================================================================*/ + +/** + * @brief Round robin interval. + * @details This constant is the number of system ticks allowed for the + * threads before preemption occurs. Setting this value to zero + * disables the preemption for threads with equal priority and the + * round robin becomes cooperative. Note that higher priority + * threads can still preempt, the kernel is always preemptive. + * @note Disabling the round robin preemption makes the kernel more compact + * and generally faster. + * @note The round robin preemption is not supported in tickless mode and + * must be set to zero in that case. + */ +#if !defined(CH_CFG_TIME_QUANTUM) +#define CH_CFG_TIME_QUANTUM 0 +#endif + +/** + * @brief Idle thread automatic spawn suppression. + * @details When this option is activated the function @p chSysInit() + * does not spawn the idle thread. The application @p main() + * function becomes the idle thread and must implement an + * infinite loop. + */ +#if !defined(CH_CFG_NO_IDLE_THREAD) +#define CH_CFG_NO_IDLE_THREAD FALSE +#endif + +/** @} */ + +/*===========================================================================*/ +/** + * @name Performance options + * @{ + */ +/*===========================================================================*/ + +/** + * @brief OS optimization. + * @details If enabled then time efficient rather than space efficient code + * is used when two possible implementations exist. + * + * @note This is not related to the compiler optimization options. + * @note The default is @p TRUE. + */ +#if !defined(CH_CFG_OPTIMIZE_SPEED) +#define CH_CFG_OPTIMIZE_SPEED TRUE +#endif + +/** @} */ + +/*===========================================================================*/ +/** + * @name Subsystem options + * @{ + */ +/*===========================================================================*/ + +/** + * @brief Time Measurement APIs. + * @details If enabled then the time measurement APIs are included in + * the kernel. + * + * @note The default is @p TRUE. + */ +#if !defined(CH_CFG_USE_TM) +#define CH_CFG_USE_TM FALSE +#endif + +/** + * @brief Threads registry APIs. + * @details If enabled then the registry APIs are included in the kernel. + * + * @note The default is @p TRUE. + */ +#if !defined(CH_CFG_USE_REGISTRY) +#define CH_CFG_USE_REGISTRY FALSE +#endif + +/** + * @brief Threads synchronization APIs. + * @details If enabled then the @p chThdWait() function is included in + * the kernel. + * + * @note The default is @p TRUE. + */ +#if !defined(CH_CFG_USE_WAITEXIT) +#define CH_CFG_USE_WAITEXIT TRUE +#endif + +/** + * @brief Semaphores APIs. + * @details If enabled then the Semaphores APIs are included in the kernel. + * + * @note The default is @p TRUE. + */ +#if !defined(CH_CFG_USE_SEMAPHORES) +#define CH_CFG_USE_SEMAPHORES FALSE +#endif + +/** + * @brief Semaphores queuing mode. + * @details If enabled then the threads are enqueued on semaphores by + * priority rather than in FIFO order. + * + * @note The default is @p FALSE. Enable this if you have special + * requirements. + * @note Requires @p CH_CFG_USE_SEMAPHORES. + */ +#if !defined(CH_CFG_USE_SEMAPHORES_PRIORITY) +#define CH_CFG_USE_SEMAPHORES_PRIORITY FALSE +#endif + +/** + * @brief Mutexes APIs. + * @details If enabled then the mutexes APIs are included in the kernel. + * + * @note The default is @p TRUE. + */ +#if !defined(CH_CFG_USE_MUTEXES) +#define CH_CFG_USE_MUTEXES TRUE +#endif + +/** + * @brief Enables recursive behavior on mutexes. + * @note Recursive mutexes are heavier and have an increased + * memory footprint. + * + * @note The default is @p FALSE. + * @note Requires @p CH_CFG_USE_MUTEXES. + */ +#if !defined(CH_CFG_USE_MUTEXES_RECURSIVE) +#define CH_CFG_USE_MUTEXES_RECURSIVE FALSE +#endif + +/** + * @brief Conditional Variables APIs. + * @details If enabled then the conditional variables APIs are included + * in the kernel. + * + * @note The default is @p TRUE. + * @note Requires @p CH_CFG_USE_MUTEXES. + */ +#if !defined(CH_CFG_USE_CONDVARS) +#define CH_CFG_USE_CONDVARS FALSE +#endif + +/** + * @brief Conditional Variables APIs with timeout. + * @details If enabled then the conditional variables APIs with timeout + * specification are included in the kernel. + * + * @note The default is @p TRUE. + * @note Requires @p CH_CFG_USE_CONDVARS. + */ +#if !defined(CH_CFG_USE_CONDVARS_TIMEOUT) +#define CH_CFG_USE_CONDVARS_TIMEOUT FALSE +#endif + +/** + * @brief Events Flags APIs. + * @details If enabled then the event flags APIs are included in the kernel. + * + * @note The default is @p TRUE. + */ +#if !defined(CH_CFG_USE_EVENTS) +#define CH_CFG_USE_EVENTS FALSE +#endif + +/** + * @brief Events Flags APIs with timeout. + * @details If enabled then the events APIs with timeout specification + * are included in the kernel. + * + * @note The default is @p TRUE. + * @note Requires @p CH_CFG_USE_EVENTS. + */ +#if !defined(CH_CFG_USE_EVENTS_TIMEOUT) +#define CH_CFG_USE_EVENTS_TIMEOUT FALSE +#endif + +/** + * @brief Synchronous Messages APIs. + * @details If enabled then the synchronous messages APIs are included + * in the kernel. + * + * @note The default is @p TRUE. + */ +#if !defined(CH_CFG_USE_MESSAGES) +#define CH_CFG_USE_MESSAGES FALSE +#endif + +/** + * @brief Synchronous Messages queuing mode. + * @details If enabled then messages are served by priority rather than in + * FIFO order. + * + * @note The default is @p FALSE. Enable this if you have special + * requirements. + * @note Requires @p CH_CFG_USE_MESSAGES. + */ +#if !defined(CH_CFG_USE_MESSAGES_PRIORITY) +#define CH_CFG_USE_MESSAGES_PRIORITY FALSE +#endif + +/** + * @brief Dynamic Threads APIs. + * @details If enabled then the dynamic threads creation APIs are included + * in the kernel. + * + * @note The default is @p TRUE. + * @note Requires @p CH_CFG_USE_WAITEXIT. + * @note Requires @p CH_CFG_USE_HEAP and/or @p CH_CFG_USE_MEMPOOLS. + */ +#if !defined(CH_CFG_USE_DYNAMIC) +#define CH_CFG_USE_DYNAMIC FALSE +#endif + +/** @} */ + +/*===========================================================================*/ +/** + * @name OSLIB options + * @{ + */ +/*===========================================================================*/ + +/** + * @brief Mailboxes APIs. + * @details If enabled then the asynchronous messages (mailboxes) APIs are + * included in the kernel. + * + * @note The default is @p TRUE. + * @note Requires @p CH_CFG_USE_SEMAPHORES. + */ +#if !defined(CH_CFG_USE_MAILBOXES) +#define CH_CFG_USE_MAILBOXES FALSE +#endif + +/** + * @brief Core Memory Manager APIs. + * @details If enabled then the core memory manager APIs are included + * in the kernel. + * + * @note The default is @p TRUE. + */ +#if !defined(CH_CFG_USE_MEMCORE) +#define CH_CFG_USE_MEMCORE TRUE +#endif + +/** + * @brief Managed RAM size. + * @details Size of the RAM area to be managed by the OS. If set to zero + * then the whole available RAM is used. The core memory is made + * available to the heap allocator and/or can be used directly through + * the simplified core memory allocator. + * + * @note In order to let the OS manage the whole RAM the linker script must + * provide the @p __heap_base__ and @p __heap_end__ symbols. + * @note Requires @p CH_CFG_USE_MEMCORE. + */ +#if !defined(CH_CFG_MEMCORE_SIZE) +#define CH_CFG_MEMCORE_SIZE 0 +#endif + +/** + * @brief Heap Allocator APIs. + * @details If enabled then the memory heap allocator APIs are included + * in the kernel. + * + * @note The default is @p TRUE. + * @note Requires @p CH_CFG_USE_MEMCORE and either @p CH_CFG_USE_MUTEXES or + * @p CH_CFG_USE_SEMAPHORES. + * @note Mutexes are recommended. + */ +#if !defined(CH_CFG_USE_HEAP) +#define CH_CFG_USE_HEAP TRUE +#endif + +/** + * @brief Memory Pools Allocator APIs. + * @details If enabled then the memory pools allocator APIs are included + * in the kernel. + * + * @note The default is @p TRUE. + */ +#if !defined(CH_CFG_USE_MEMPOOLS) +#define CH_CFG_USE_MEMPOOLS FALSE +#endif + +/** + * @brief Objects FIFOs APIs. + * @details If enabled then the objects FIFOs APIs are included + * in the kernel. + * + * @note The default is @p TRUE. + */ +#if !defined(CH_CFG_USE_OBJ_FIFOS) +#define CH_CFG_USE_OBJ_FIFOS FALSE +#endif + +/** + * @brief Pipes APIs. + * @details If enabled then the pipes APIs are included + * in the kernel. + * + * @note The default is @p TRUE. + */ +#if !defined(CH_CFG_USE_PIPES) +#define CH_CFG_USE_PIPES FALSE +#endif + +/** + * @brief Objects Caches APIs. + * @details If enabled then the objects caches APIs are included + * in the kernel. + * + * @note The default is @p TRUE. + */ +#if !defined(CH_CFG_USE_OBJ_CACHES) +#define CH_CFG_USE_OBJ_CACHES FALSE +#endif + +/** + * @brief Delegate threads APIs. + * @details If enabled then the delegate threads APIs are included + * in the kernel. + * + * @note The default is @p TRUE. + */ +#if !defined(CH_CFG_USE_DELEGATES) +#define CH_CFG_USE_DELEGATES FALSE +#endif + +/** + * @brief Jobs Queues APIs. + * @details If enabled then the jobs queues APIs are included + * in the kernel. + * + * @note The default is @p TRUE. + */ +#if !defined(CH_CFG_USE_JOBS) +#define CH_CFG_USE_JOBS FALSE +#endif + +/** @} */ + +/*===========================================================================*/ +/** + * @name Objects factory options + * @{ + */ +/*===========================================================================*/ + +/** + * @brief Objects Factory APIs. + * @details If enabled then the objects factory APIs are included in the + * kernel. + * + * @note The default is @p FALSE. + */ +#if !defined(CH_CFG_USE_FACTORY) +#define CH_CFG_USE_FACTORY FALSE +#endif + +/** + * @brief Maximum length for object names. + * @details If the specified length is zero then the name is stored by + * pointer but this could have unintended side effects. + */ +#if !defined(CH_CFG_FACTORY_MAX_NAMES_LENGTH) +#define CH_CFG_FACTORY_MAX_NAMES_LENGTH 8 +#endif + +/** + * @brief Enables the registry of generic objects. + */ +#if !defined(CH_CFG_FACTORY_OBJECTS_REGISTRY) +#define CH_CFG_FACTORY_OBJECTS_REGISTRY FALSE +#endif + +/** + * @brief Enables factory for generic buffers. + */ +#if !defined(CH_CFG_FACTORY_GENERIC_BUFFERS) +#define CH_CFG_FACTORY_GENERIC_BUFFERS FALSE +#endif + +/** + * @brief Enables factory for semaphores. + */ +#if !defined(CH_CFG_FACTORY_SEMAPHORES) +#define CH_CFG_FACTORY_SEMAPHORES FALSE +#endif + +/** + * @brief Enables factory for mailboxes. + */ +#if !defined(CH_CFG_FACTORY_MAILBOXES) +#define CH_CFG_FACTORY_MAILBOXES FALSE +#endif + +/** + * @brief Enables factory for objects FIFOs. + */ +#if !defined(CH_CFG_FACTORY_OBJ_FIFOS) +#define CH_CFG_FACTORY_OBJ_FIFOS FALSE +#endif + +/** + * @brief Enables factory for Pipes. + */ +#if !defined(CH_CFG_FACTORY_PIPES) || defined(__DOXYGEN__) +#define CH_CFG_FACTORY_PIPES FALSE +#endif + +/** @} */ + +/*===========================================================================*/ +/** + * @name Debug options + * @{ + */ +/*===========================================================================*/ + +/** + * @brief Debug option, kernel statistics. + * + * @note The default is @p FALSE. + */ +#if !defined(CH_DBG_STATISTICS) +#define CH_DBG_STATISTICS FALSE +#endif + +/** + * @brief Debug option, system state check. + * @details If enabled the correct call protocol for system APIs is checked + * at runtime. + * + * @note The default is @p FALSE. + */ +#if !defined(CH_DBG_SYSTEM_STATE_CHECK) +#define CH_DBG_SYSTEM_STATE_CHECK TRUE +#endif + +/** + * @brief Debug option, parameters checks. + * @details If enabled then the checks on the API functions input + * parameters are activated. + * + * @note The default is @p FALSE. + */ +#if !defined(CH_DBG_ENABLE_CHECKS) +#define CH_DBG_ENABLE_CHECKS TRUE +#endif + +/** + * @brief Debug option, consistency checks. + * @details If enabled then all the assertions in the kernel code are + * activated. This includes consistency checks inside the kernel, + * runtime anomalies and port-defined checks. + * + * @note The default is @p FALSE. + */ +#if !defined(CH_DBG_ENABLE_ASSERTS) +#define CH_DBG_ENABLE_ASSERTS TRUE +#endif + +/** + * @brief Debug option, trace buffer. + * @details If enabled then the trace buffer is activated. + * + * @note The default is @p CH_DBG_TRACE_MASK_DISABLED. + */ +#if !defined(CH_DBG_TRACE_MASK) +#define CH_DBG_TRACE_MASK CH_DBG_TRACE_MASK_ALL +#endif + +/** + * @brief Trace buffer entries. + * @note The trace buffer is only allocated if @p CH_DBG_TRACE_MASK is + * different from @p CH_DBG_TRACE_MASK_DISABLED. + */ +#if !defined(CH_DBG_TRACE_BUFFER_SIZE) +#define CH_DBG_TRACE_BUFFER_SIZE 128 +#endif + +/** + * @brief Debug option, stack checks. + * @details If enabled then a runtime stack check is performed. + * + * @note The default is @p FALSE. + * @note The stack check is performed in a architecture/port dependent way. + * It may not be implemented or some ports. + * @note The default failure mode is to halt the system with the global + * @p panic_msg variable set to @p NULL. + */ +#if !defined(CH_DBG_ENABLE_STACK_CHECK) +#define CH_DBG_ENABLE_STACK_CHECK TRUE +#endif + +/** + * @brief Debug option, stacks initialization. + * @details If enabled then the threads working area is filled with a byte + * value when a thread is created. This can be useful for the + * runtime measurement of the used stack. + * + * @note The default is @p FALSE. + */ +#if !defined(CH_DBG_FILL_THREADS) +#define CH_DBG_FILL_THREADS TRUE +#endif + +/** + * @brief Debug option, threads profiling. + * @details If enabled then a field is added to the @p thread_t structure that + * counts the system ticks occurred while executing the thread. + * + * @note The default is @p FALSE. + * @note This debug option is not currently compatible with the + * tickless mode. + */ +#if !defined(CH_DBG_THREADS_PROFILING) +#define CH_DBG_THREADS_PROFILING FALSE +#endif + +/** @} */ + +/*===========================================================================*/ +/** + * @name Kernel hooks + * @{ + */ +/*===========================================================================*/ + +/** + * @brief System structure extension. + * @details User fields added to the end of the @p ch_system_t structure. + */ +#define CH_CFG_SYSTEM_EXTRA_FIELDS /* Add threads custom fields here.*/ + +/** + * @brief System initialization hook. + * @details User initialization code added to the @p chSysInit() function + * just before interrupts are enabled globally. + */ +#define CH_CFG_SYSTEM_INIT_HOOK() \ + { \ + /* Add threads initialization code here.*/ \ + } + +/** + * @brief Threads descriptor structure extension. + * @details User fields added to the end of the @p thread_t structure. + */ +#define CH_CFG_THREAD_EXTRA_FIELDS /* Add threads custom fields here.*/ + +/** + * @brief Threads initialization hook. + * @details User initialization code added to the @p _thread_init() function. + * + * @note It is invoked from within @p _thread_init() and implicitly from all + * the threads creation APIs. + */ +#define CH_CFG_THREAD_INIT_HOOK(tp) \ + { \ + /* Add threads initialization code here.*/ \ + } + +/** + * @brief Threads finalization hook. + * @details User finalization code added to the @p chThdExit() API. + */ +#define CH_CFG_THREAD_EXIT_HOOK(tp) \ + { \ + /* Add threads finalization code here.*/ \ + } + +/** + * @brief Context switch hook. + * @details This hook is invoked just before switching between threads. + */ +#define CH_CFG_CONTEXT_SWITCH_HOOK(ntp, otp) \ + { \ + /* Context switch code here.*/ \ + } + +/** + * @brief ISR enter hook. + */ +#define CH_CFG_IRQ_PROLOGUE_HOOK() \ + { \ + /* IRQ prologue code here.*/ \ + } + +/** + * @brief ISR exit hook. + */ +#define CH_CFG_IRQ_EPILOGUE_HOOK() \ + { \ + /* IRQ epilogue code here.*/ \ + } + +/** + * @brief Idle thread enter hook. + * @note This hook is invoked within a critical zone, no OS functions + * should be invoked from here. + * @note This macro can be used to activate a power saving mode. + */ +#define CH_CFG_IDLE_ENTER_HOOK() \ + { \ + /* Idle-enter code here.*/ \ + } + +/** + * @brief Idle thread leave hook. + * @note This hook is invoked within a critical zone, no OS functions + * should be invoked from here. + * @note This macro can be used to deactivate a power saving mode. + */ +#define CH_CFG_IDLE_LEAVE_HOOK() \ + { \ + /* Idle-leave code here.*/ \ + } + +/** + * @brief Idle Loop hook. + * @details This hook is continuously invoked by the idle thread loop. + */ +#define CH_CFG_IDLE_LOOP_HOOK() \ + { \ + /* Idle loop code here.*/ \ + } + +/** + * @brief System tick event hook. + * @details This hook is invoked in the system tick handler immediately + * after processing the virtual timers queue. + */ +#define CH_CFG_SYSTEM_TICK_HOOK() \ + { \ + /* System tick event code here.*/ \ + } + +/** + * @brief System halt hook. + * @details This hook is invoked in case to a system halting error before + * the system is halted. + */ +#define CH_CFG_SYSTEM_HALT_HOOK(reason) \ + { \ + /* System halt code here.*/ \ + } + +/** + * @brief Trace hook. + * @details This hook is invoked each time a new record is written in the + * trace buffer. + */ +#define CH_CFG_TRACE_HOOK(tep) \ + { \ + /* Trace code here.*/ \ + } + +/** @} */ + +/*===========================================================================*/ +/* Port-specific settings (override port settings defaulted in chcore.h). */ +/*===========================================================================*/ + +#endif /* CHCONF_H */ + +/** @} */ diff --git a/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/cfg/halconf.h b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/cfg/halconf.h new file mode 100644 index 00000000..bfdbb9db --- /dev/null +++ b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/cfg/halconf.h @@ -0,0 +1,533 @@ +/* + ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/** + * @file templates/halconf.h + * @brief HAL configuration header. + * @details HAL configuration file, this file allows to enable or disable the + * various device drivers from your application. You may also use + * this file in order to override the device drivers default settings. + * + * @addtogroup HAL_CONF + * @{ + */ + +#ifndef HALCONF_H +#define HALCONF_H + +#define _CHIBIOS_HAL_CONF_ +#define _CHIBIOS_HAL_CONF_VER_7_1_ + +#include "mcuconf.h" + +/** + * @brief Enables the PAL subsystem. + */ +#if !defined(HAL_USE_PAL) || defined(__DOXYGEN__) +#define HAL_USE_PAL TRUE +#endif + +/** + * @brief Enables the ADC subsystem. + */ +#if !defined(HAL_USE_ADC) || defined(__DOXYGEN__) +#define HAL_USE_ADC FALSE +#endif + +/** + * @brief Enables the CAN subsystem. + */ +#if !defined(HAL_USE_CAN) || defined(__DOXYGEN__) +#define HAL_USE_CAN FALSE +#endif + +/** + * @brief Enables the cryptographic subsystem. + */ +#if !defined(HAL_USE_CRY) || defined(__DOXYGEN__) +#define HAL_USE_CRY FALSE +#endif + +/** + * @brief Enables the DAC subsystem. + */ +#if !defined(HAL_USE_DAC) || defined(__DOXYGEN__) +#define HAL_USE_DAC FALSE +#endif + +/** + * @brief Enables the EFlash subsystem. + */ +#if !defined(HAL_USE_EFL) || defined(__DOXYGEN__) +#define HAL_USE_EFL FALSE +#endif + +/** + * @brief Enables the GPT subsystem. + */ +#if !defined(HAL_USE_GPT) || defined(__DOXYGEN__) +#define HAL_USE_GPT FALSE +#endif + +/** + * @brief Enables the I2C subsystem. + */ +#if !defined(HAL_USE_I2C) || defined(__DOXYGEN__) +#define HAL_USE_I2C FALSE +#endif + +/** + * @brief Enables the I2S subsystem. + */ +#if !defined(HAL_USE_I2S) || defined(__DOXYGEN__) +#define HAL_USE_I2S FALSE +#endif + +/** + * @brief Enables the ICU subsystem. + */ +#if !defined(HAL_USE_ICU) || defined(__DOXYGEN__) +#define HAL_USE_ICU FALSE +#endif + +/** + * @brief Enables the MAC subsystem. + */ +#if !defined(HAL_USE_MAC) || defined(__DOXYGEN__) +#define HAL_USE_MAC FALSE +#endif + +/** + * @brief Enables the MMC_SPI subsystem. + */ +#if !defined(HAL_USE_MMC_SPI) || defined(__DOXYGEN__) +#define HAL_USE_MMC_SPI FALSE +#endif + +/** + * @brief Enables the PWM subsystem. + */ +#if !defined(HAL_USE_PWM) || defined(__DOXYGEN__) +#define HAL_USE_PWM FALSE +#endif + +/** + * @brief Enables the RTC subsystem. + */ +#if !defined(HAL_USE_RTC) || defined(__DOXYGEN__) +#define HAL_USE_RTC FALSE +#endif + +/** + * @brief Enables the SDC subsystem. + */ +#if !defined(HAL_USE_SDC) || defined(__DOXYGEN__) +#define HAL_USE_SDC FALSE +#endif + +/** + * @brief Enables the SERIAL subsystem. + */ +#if !defined(HAL_USE_SERIAL) || defined(__DOXYGEN__) +#define HAL_USE_SERIAL FALSE +#endif + +/** + * @brief Enables the SERIAL over USB subsystem. + */ +#if !defined(HAL_USE_SERIAL_USB) || defined(__DOXYGEN__) +#define HAL_USE_SERIAL_USB FALSE +#endif + +/** + * @brief Enables the SIO subsystem. + */ +#if !defined(HAL_USE_SIO) || defined(__DOXYGEN__) +#define HAL_USE_SIO FALSE +#endif + +/** + * @brief Enables the SPI subsystem. + */ +#if !defined(HAL_USE_SPI) || defined(__DOXYGEN__) +#define HAL_USE_SPI FALSE +#endif + +/** + * @brief Enables the TRNG subsystem. + */ +#if !defined(HAL_USE_TRNG) || defined(__DOXYGEN__) +#define HAL_USE_TRNG FALSE +#endif + +/** + * @brief Enables the UART subsystem. + */ +#if !defined(HAL_USE_UART) || defined(__DOXYGEN__) +#define HAL_USE_UART FALSE +#endif + +/** + * @brief Enables the USB subsystem. + */ +#if !defined(HAL_USE_USB) || defined(__DOXYGEN__) +#define HAL_USE_USB TRUE +#endif + +/** + * @brief Enables the WDG subsystem. + */ +#if !defined(HAL_USE_WDG) || defined(__DOXYGEN__) +#define HAL_USE_WDG FALSE +#endif + +/** + * @brief Enables the WSPI subsystem. + */ +#if !defined(HAL_USE_WSPI) || defined(__DOXYGEN__) +#define HAL_USE_WSPI FALSE +#endif + +/*===========================================================================*/ +/* PAL driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Enables synchronous APIs. + * @note Disabling this option saves both code and data space. + */ +#if !defined(PAL_USE_CALLBACKS) || defined(__DOXYGEN__) +#define PAL_USE_CALLBACKS FALSE +#endif + +/** + * @brief Enables synchronous APIs. + * @note Disabling this option saves both code and data space. + */ +#if !defined(PAL_USE_WAIT) || defined(__DOXYGEN__) +#define PAL_USE_WAIT FALSE +#endif + +/*===========================================================================*/ +/* ADC driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Enables synchronous APIs. + * @note Disabling this option saves both code and data space. + */ +#if !defined(ADC_USE_WAIT) || defined(__DOXYGEN__) +#define ADC_USE_WAIT TRUE +#endif + +/** + * @brief Enables the @p adcAcquireBus() and @p adcReleaseBus() APIs. + * @note Disabling this option saves both code and data space. + */ +#if !defined(ADC_USE_MUTUAL_EXCLUSION) || defined(__DOXYGEN__) +#define ADC_USE_MUTUAL_EXCLUSION TRUE +#endif + +/*===========================================================================*/ +/* CAN driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Sleep mode related APIs inclusion switch. + */ +#if !defined(CAN_USE_SLEEP_MODE) || defined(__DOXYGEN__) +#define CAN_USE_SLEEP_MODE TRUE +#endif + +/** + * @brief Enforces the driver to use direct callbacks rather than OSAL events. + */ +#if !defined(CAN_ENFORCE_USE_CALLBACKS) || defined(__DOXYGEN__) +#define CAN_ENFORCE_USE_CALLBACKS FALSE +#endif + +/*===========================================================================*/ +/* CRY driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Enables the SW fall-back of the cryptographic driver. + * @details When enabled, this option, activates a fall-back software + * implementation for algorithms not supported by the underlying + * hardware. + * @note Fall-back implementations may not be present for all algorithms. + */ +#if !defined(HAL_CRY_USE_FALLBACK) || defined(__DOXYGEN__) +#define HAL_CRY_USE_FALLBACK FALSE +#endif + +/** + * @brief Makes the driver forcibly use the fall-back implementations. + */ +#if !defined(HAL_CRY_ENFORCE_FALLBACK) || defined(__DOXYGEN__) +#define HAL_CRY_ENFORCE_FALLBACK FALSE +#endif + +/*===========================================================================*/ +/* DAC driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Enables synchronous APIs. + * @note Disabling this option saves both code and data space. + */ +#if !defined(DAC_USE_WAIT) || defined(__DOXYGEN__) +#define DAC_USE_WAIT TRUE +#endif + +/** + * @brief Enables the @p dacAcquireBus() and @p dacReleaseBus() APIs. + * @note Disabling this option saves both code and data space. + */ +#if !defined(DAC_USE_MUTUAL_EXCLUSION) || defined(__DOXYGEN__) +#define DAC_USE_MUTUAL_EXCLUSION TRUE +#endif + +/*===========================================================================*/ +/* I2C driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Enables the mutual exclusion APIs on the I2C bus. + */ +#if !defined(I2C_USE_MUTUAL_EXCLUSION) || defined(__DOXYGEN__) +#define I2C_USE_MUTUAL_EXCLUSION TRUE +#endif + +/*===========================================================================*/ +/* MAC driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Enables the zero-copy API. + */ +#if !defined(MAC_USE_ZERO_COPY) || defined(__DOXYGEN__) +#define MAC_USE_ZERO_COPY FALSE +#endif + +/** + * @brief Enables an event sources for incoming packets. + */ +#if !defined(MAC_USE_EVENTS) || defined(__DOXYGEN__) +#define MAC_USE_EVENTS TRUE +#endif + +/*===========================================================================*/ +/* MMC_SPI driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Delays insertions. + * @details If enabled this options inserts delays into the MMC waiting + * routines releasing some extra CPU time for the threads with + * lower priority, this may slow down the driver a bit however. + * This option is recommended also if the SPI driver does not + * use a DMA channel and heavily loads the CPU. + */ +#if !defined(MMC_NICE_WAITING) || defined(__DOXYGEN__) +#define MMC_NICE_WAITING TRUE +#endif + +/*===========================================================================*/ +/* SDC driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Number of initialization attempts before rejecting the card. + * @note Attempts are performed at 10mS intervals. + */ +#if !defined(SDC_INIT_RETRY) || defined(__DOXYGEN__) +#define SDC_INIT_RETRY 100 +#endif + +/** + * @brief Include support for MMC cards. + * @note MMC support is not yet implemented so this option must be kept + * at @p FALSE. + */ +#if !defined(SDC_MMC_SUPPORT) || defined(__DOXYGEN__) +#define SDC_MMC_SUPPORT FALSE +#endif + +/** + * @brief Delays insertions. + * @details If enabled this options inserts delays into the MMC waiting + * routines releasing some extra CPU time for the threads with + * lower priority, this may slow down the driver a bit however. + */ +#if !defined(SDC_NICE_WAITING) || defined(__DOXYGEN__) +#define SDC_NICE_WAITING TRUE +#endif + +/** + * @brief OCR initialization constant for V20 cards. + */ +#if !defined(SDC_INIT_OCR_V20) || defined(__DOXYGEN__) +#define SDC_INIT_OCR_V20 0x50FF8000U +#endif + +/** + * @brief OCR initialization constant for non-V20 cards. + */ +#if !defined(SDC_INIT_OCR) || defined(__DOXYGEN__) +#define SDC_INIT_OCR 0x80100000U +#endif + +/*===========================================================================*/ +/* SERIAL driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Default bit rate. + * @details Configuration parameter, this is the baud rate selected for the + * default configuration. + */ +#if !defined(SERIAL_DEFAULT_BITRATE) || defined(__DOXYGEN__) +#define SERIAL_DEFAULT_BITRATE 38400 +#endif + +/** + * @brief Serial buffers size. + * @details Configuration parameter, you can change the depth of the queue + * buffers depending on the requirements of your application. + * @note The default is 16 bytes for both the transmission and receive + * buffers. + */ +#if !defined(SERIAL_BUFFERS_SIZE) || defined(__DOXYGEN__) +#define SERIAL_BUFFERS_SIZE 16 +#endif + +/*===========================================================================*/ +/* SERIAL_USB driver related setting. */ +/*===========================================================================*/ + +/** + * @brief Serial over USB buffers size. + * @details Configuration parameter, the buffer size must be a multiple of + * the USB data endpoint maximum packet size. + * @note The default is 256 bytes for both the transmission and receive + * buffers. + */ +#if !defined(SERIAL_USB_BUFFERS_SIZE) || defined(__DOXYGEN__) +#define SERIAL_USB_BUFFERS_SIZE 256 +#endif + +/** + * @brief Serial over USB number of buffers. + * @note The default is 2 buffers. + */ +#if !defined(SERIAL_USB_BUFFERS_NUMBER) || defined(__DOXYGEN__) +#define SERIAL_USB_BUFFERS_NUMBER 2 +#endif + +/*===========================================================================*/ +/* SPI driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Enables synchronous APIs. + * @note Disabling this option saves both code and data space. + */ +#if !defined(SPI_USE_WAIT) || defined(__DOXYGEN__) +#define SPI_USE_WAIT TRUE +#endif + +/** + * @brief Enables circular transfers APIs. + * @note Disabling this option saves both code and data space. + */ +#if !defined(SPI_USE_CIRCULAR) || defined(__DOXYGEN__) +#define SPI_USE_CIRCULAR FALSE +#endif + +/** + * @brief Enables the @p spiAcquireBus() and @p spiReleaseBus() APIs. + * @note Disabling this option saves both code and data space. + */ +#if !defined(SPI_USE_MUTUAL_EXCLUSION) || defined(__DOXYGEN__) +#define SPI_USE_MUTUAL_EXCLUSION TRUE +#endif + +/** + * @brief Handling method for SPI CS line. + * @note Disabling this option saves both code and data space. + */ +#if !defined(SPI_SELECT_MODE) || defined(__DOXYGEN__) +#define SPI_SELECT_MODE SPI_SELECT_MODE_PAD +#endif + +/*===========================================================================*/ +/* UART driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Enables synchronous APIs. + * @note Disabling this option saves both code and data space. + */ +#if !defined(UART_USE_WAIT) || defined(__DOXYGEN__) +#define UART_USE_WAIT FALSE +#endif + +/** + * @brief Enables the @p uartAcquireBus() and @p uartReleaseBus() APIs. + * @note Disabling this option saves both code and data space. + */ +#if !defined(UART_USE_MUTUAL_EXCLUSION) || defined(__DOXYGEN__) +#define UART_USE_MUTUAL_EXCLUSION FALSE +#endif + +/*===========================================================================*/ +/* USB driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Enables synchronous APIs. + * @note Disabling this option saves both code and data space. + */ +#if !defined(USB_USE_WAIT) || defined(__DOXYGEN__) +#define USB_USE_WAIT FALSE +#endif + +/*===========================================================================*/ +/* WSPI driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Enables synchronous APIs. + * @note Disabling this option saves both code and data space. + */ +#if !defined(WSPI_USE_WAIT) || defined(__DOXYGEN__) +#define WSPI_USE_WAIT TRUE +#endif + +/** + * @brief Enables the @p wspiAcquireBus() and @p wspiReleaseBus() APIs. + * @note Disabling this option saves both code and data space. + */ +#if !defined(WSPI_USE_MUTUAL_EXCLUSION) || defined(__DOXYGEN__) +#define WSPI_USE_MUTUAL_EXCLUSION TRUE +#endif + +#include "halconf_community.h" + +#endif /* HALCONF_H */ + +/** @} */ diff --git a/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/cfg/halconf_community.h b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/cfg/halconf_community.h new file mode 100644 index 00000000..fb7ebf6b --- /dev/null +++ b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/cfg/halconf_community.h @@ -0,0 +1,180 @@ +/* + ChibiOS - Copyright (C) 2014 Uladzimir Pylinsky aka barthess + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +#ifndef HALCONF_COMMUNITY_H +#define HALCONF_COMMUNITY_H + +/** + * @brief Enables the community overlay. + */ +#if !defined(HAL_USE_COMMUNITY) || defined(__DOXYGEN__) +#define HAL_USE_COMMUNITY TRUE +#endif + +/** + * @brief Enables the FSMC subsystem. + */ +#if !defined(HAL_USE_FSMC) || defined(__DOXYGEN__) +#define HAL_USE_FSMC FALSE +#endif + +/** + * @brief Enables the NAND subsystem. + */ +#if !defined(HAL_USE_NAND) || defined(__DOXYGEN__) +#define HAL_USE_NAND FALSE +#endif + +/** + * @brief Enables the 1-wire subsystem. + */ +#if !defined(HAL_USE_ONEWIRE) || defined(__DOXYGEN__) +#define HAL_USE_ONEWIRE FALSE +#endif + +/** + * @brief Enables the EICU subsystem. + */ +#if !defined(HAL_USE_EICU) || defined(__DOXYGEN__) +#define HAL_USE_EICU FALSE +#endif + +/** + * @brief Enables the CRC subsystem. + */ +#if !defined(HAL_USE_CRC) || defined(__DOXYGEN__) +#define HAL_USE_CRC FALSE +#endif + +/** + * @brief Enables the RNG subsystem. + */ +#if !defined(HAL_USE_RNG) || defined(__DOXYGEN__) +#define HAL_USE_RNG FALSE +#endif + +/** + * @brief Enables the EEPROM subsystem. + */ +#if !defined(HAL_USE_EEPROM) || defined(__DOXYGEN__) +#define HAL_USE_EEPROM FALSE +#endif + +/** + * @brief Enables the TIMCAP subsystem. + */ +#if !defined(HAL_USE_TIMCAP) || defined(__DOXYGEN__) +#define HAL_USE_TIMCAP FALSE +#endif + +/** + * @brief Enables the TIMCAP subsystem. + */ +#if !defined(HAL_USE_COMP) || defined(__DOXYGEN__) +#define HAL_USE_COMP FALSE +#endif + +/** + * @brief Enables the QEI subsystem. + */ +#if !defined(HAL_USE_QEI) || defined(__DOXYGEN__) +#define HAL_USE_QEI FALSE +#endif + +/** + * @brief Enables the USBH subsystem. + */ +#if !defined(HAL_USE_USBH) || defined(__DOXYGEN__) +#define HAL_USE_USBH FALSE +#endif + +/** + * @brief Enables the USB_MSD subsystem. + */ +#if !defined(HAL_USE_USB_MSD) || defined(__DOXYGEN__) +#define HAL_USE_USB_MSD FALSE +#endif + +/** + * @brief Enables the USB_HID subsystem. + */ +#if !defined(HAL_USE_USB_HID) || defined(__DOXYGEN__) +#define HAL_USE_USB_HID TRUE +#endif + +/*===========================================================================*/ +/* FSMCNAND driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Enables the @p nandAcquireBus() and @p nanReleaseBus() APIs. + * @note Disabling this option saves both code and data space. + */ +#if !defined(NAND_USE_MUTUAL_EXCLUSION) || defined(__DOXYGEN__) +#define NAND_USE_MUTUAL_EXCLUSION TRUE +#endif + +/*===========================================================================*/ +/* 1-wire driver related settings. */ +/*===========================================================================*/ +/** + * @brief Enables strong pull up feature. + * @note Disabling this option saves both code and data space. + */ +#define ONEWIRE_USE_STRONG_PULLUP FALSE + +/** + * @brief Enables search ROM feature. + * @note Disabling this option saves both code and data space. + */ +#define ONEWIRE_USE_SEARCH_ROM TRUE + +/*===========================================================================*/ +/* QEI driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Enables discard of overlow + */ +#if !defined(QEI_USE_OVERFLOW_DISCARD) || defined(__DOXYGEN__) +#define QEI_USE_OVERFLOW_DISCARD FALSE +#endif + +/** + * @brief Enables min max of overlow + */ +#if !defined(QEI_USE_OVERFLOW_MINMAX) || defined(__DOXYGEN__) +#define QEI_USE_OVERFLOW_MINMAX FALSE +#endif + +/*===========================================================================*/ +/* EEProm driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Enables 24xx series I2C eeprom device driver. + * @note Disabling this option saves both code and data space. + */ +#define EEPROM_USE_EE24XX FALSE + /** + * @brief Enables 25xx series SPI eeprom device driver. + * @note Disabling this option saves both code and data space. + */ +#define EEPROM_USE_EE25XX FALSE + +#endif /* HALCONF_COMMUNITY_H */ + +/** @} */ diff --git a/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/cfg/mcuconf.h b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/cfg/mcuconf.h new file mode 100644 index 00000000..1cbd325b --- /dev/null +++ b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/cfg/mcuconf.h @@ -0,0 +1,41 @@ +/* + ChibiOS - Copyright (C) 2020 Alex Lewontin + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +#ifndef _MCUCONF_H_ +#define _MCUCONF_H_ + +/* + * Board setting + */ + +/* + * HAL driver system settings. + */ +#define NUC123_HSE_ENABLED TRUE +#define NUC123_PLL_ENABLED TRUE +#define NUC123_PLLSRC NUC123_PLLSRC_HSE +#define NUC123_HCLKSRC NUC123_HCLKSRC_PLL +#define NUC123_HCLKDIV 2 +#define NUC123_PLL_NF 144 +#define NUC123_USB_USE_USB0 TRUE +#define NUC123_USB_USE_USB1 TRUE + +#define NUC123_SERIAL_USE_UART0 TRUE +#define NUC123_SERIAL_CLKSRC NUC123_SERIAL_CLKSRC_HSI + +#define NUC123_MCUCONF + +#endif /* _MCUCONF_H_ */ diff --git a/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/cfg/osalconf.h b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/cfg/osalconf.h new file mode 100644 index 00000000..0b67c88d --- /dev/null +++ b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/cfg/osalconf.h @@ -0,0 +1,15 @@ +/* + ChibiOS - Copyright (C) 2020 Alex Lewontin + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ diff --git a/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/main.c b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/main.c new file mode 100644 index 00000000..70791384 --- /dev/null +++ b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/main.c @@ -0,0 +1,96 @@ +/* + Copyright (C) 2016 Jonathan Struebel + Modifications copyright (C) 2020 Alex Lewontin + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +#include "hal.h" + +#include "usbcfg.h" + +/* + * Onboard LED blinker thread, times are in milliseconds. + */ +static THD_WORKING_AREA(waBlinkThread, 128); +static THD_FUNCTION(BlinkThread, arg) +{ + + (void)arg; + + chRegSetThreadName("blinker"); + while (true) { + systime_t time = USBD1.state == USB_ACTIVE ? 250 : 500; + OnboardLED_Toggle(); + chThdSleepMilliseconds(time); + } +} + +/* + * Application entry point. + */ +int main(void) +{ + /* + * System initializations. + * - HAL initialization, this also initializes the configured device drivers + * and performs the board-specific initializations. + * - Kernel initialization, the main() function becomes a thread and the + * RTOS is active. + */ + halInit(); + chSysInit(); + OnboardLED_Init(); + + /* + * Turn off the onboard LED. + */ + OnboardLED_Off(); + + chDbgSuspendTrace(CH_DBG_TRACE_MASK_SWITCH); + /* + * Initializes a USB HID driver. + */ + hidObjectInit(&UHD1); + hidStart(&UHD1, &usbhidcfg); + + /* + * Activates the USB driver and then the USB bus pull-up on D+. + * Note, a delay is inserted in order to not have to disconnect the cable + * after a reset. + */ + + usbDisconnectBus(usbhidcfg.usbp); + chThdSleepMilliseconds(1000); + usbStart(usbhidcfg.usbp, &usbcfg); + chThdSleepMilliseconds(1000); + usbConnectBus(usbhidcfg.usbp); + + /* + * Creates the blinker thread. + */ + chThdCreateStatic( + waBlinkThread, sizeof(waBlinkThread), NORMALPRIO, BlinkThread, NULL); + + while (true) { + if (usbhidcfg.usbp->state == USB_ACTIVE) { + uint8_t report; + size_t n = hidGetReport(0, &report, sizeof(report)); + hidWriteReport(&UHD1, &report, n); + n = hidReadReportt(&UHD1, &report, sizeof(report), TIME_IMMEDIATE); + if (n > 0) + hidSetReport(0, &report, n); + } + + chThdSleepMilliseconds(1000); + } +} diff --git a/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/readme.txt b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/readme.txt new file mode 100644 index 00000000..af865c29 --- /dev/null +++ b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/readme.txt @@ -0,0 +1,36 @@ +***************************************************************************** +** ChibiOS/HAL - USB driver demo for NUC123. ** +***************************************************************************** + +** TARGET ** + +The demo runs on a NUTINY-SDK-NUC123-V2.0 board with a NUC123SD4AN0 MCU. + +** The Demo ** + +The application demonstrates the use of the NUC123 USB driver. A successful run of the test +should begin with the on-board LED blinking slowly, then faster when the USB driver initializes. +The host should recognize the board as a USB HID, and when run with the appropriate VID/PID, the +supplied client application should communicate with the board. + +** Board Setup ** + +- None + +** Build Procedure ** + +The demo has been tested using gcc version 9.3.1 (GNU Arm Embedded Toolchain 9-2020-q2-update). +Just modify the TRGT line in the makefile in order to use different GCC ports. + +Two versions of the client code are provided. The Linux version uses the kernel's native hidraw API. +The Darwin version uses the hidapi from libusb (https://github.com/libusb/hidapi) + +The Darwin client has only been tested using Apple clang version 12.0.0 (clang-1200.0.32.2), on +macOS Catalina 10.15.7. However, it should be easily portable to any platform supported by hidapi. + +To build, adjust HIDAPI_HEADER_PATH in Client/darwin/Makefile to the appropriate location. + +** Notes ** + +This test was adapted from Jonathan Struebel's USB_HID test for the KINETIS FRDM-KL25Z. All files +are copyright their original authors, as indicated in the headers. diff --git a/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/usbcfg.c b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/usbcfg.c new file mode 100644 index 00000000..ec722e56 --- /dev/null +++ b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/usbcfg.c @@ -0,0 +1,391 @@ +/* + Copyright (C) 2016 Jonathan Struebel + Modifications copyright (C) 2020 Alex Lewontin + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/** + * @file usbcfg.c + * @brief USB driver config. + * + * @addtogroup USB + * @{ + */ +#include "hal.h" +#include "usbcfg.h" + +#define VID 0x1209 +#define PID 0x0003 +/* + * Endpoints to be used for USBD1. + */ +#define USBD1_DATA_REQUEST_EP 1 +#define USBD1_DATA_AVAILABLE_EP 1 + +/* + * USB HID Driver structure. + */ +USBHIDDriver UHD1; + +/* + * Data used for feedback + */ +uint8_t increment_var = 0; + +/* + * USB Device Descriptor. + */ +static const uint8_t hid_device_descriptor_data[] = { + USB_DESC_DEVICE (0x0110, /* bcdUSB (1.1). */ + 0x00, /* bDeviceClass. */ + 0x00, /* bDeviceSubClass. */ + 0x00, /* bDeviceProtocol. */ + 0x40, /* bMaxPacketSize. */ + VID, /* idVendor. */ + PID, /* idProduct. */ + 0x000, /* bcdDevice. */ + 1, /* iManufacturer. */ + 2, /* iProduct. */ + 3, /* iSerialNumber. */ + 1) /* bNumConfigurations. */ +}; + +/* + * Device Descriptor wrapper. + */ +static const USBDescriptor hid_device_descriptor = { + sizeof hid_device_descriptor_data, + hid_device_descriptor_data +}; + +/* + * Configuration Descriptor tree for a HID device + * + * The HID Specifications version 1.11 require the following order: + * - Configuration Descriptor + * - Interface Descriptor + * - HID Descriptor + * - Endpoints Descriptors + */ +#define HID_DESCRIPTOR_OFFSET 18 +#define HID_DESCRIPTOR_SIZE USB_DESC_HID_SIZE + +static const uint8_t hid_configuration_descriptor_data[] = { + /* Configuration Descriptor.*/ + USB_DESC_CONFIGURATION(41, /* wTotalLength. */ + 0x01, /* bNumInterfaces. */ + 0x01, /* bConfigurationValue. */ + 0, /* iConfiguration. */ + 0xC0, /* bmAttributes (self powered). */ + 50), /* bMaxPower (100mA). */ + /* Interface Descriptor.*/ + USB_DESC_INTERFACE (0x00, /* bInterfaceNumber. */ + 0x00, /* bAlternateSetting. */ + 0x02, /* bNumEndpoints. */ + 0x03, /* bInterfaceClass (HID Interface + Class). */ + 0x00, /* bInterfaceSubClass (None). */ + 0x00, /* bInterfaceProtocol (None). */ + 0), /* iInterface. */ + /* HID Descriptor.*/ + USB_DESC_HID (0x0110, /* bcdHID. */ + 0x00, /* bCountryCode. */ + 0x01, /* bNumDescriptors. */ + 0x22, /* bDescriptorType (Report + Descriptor). */ + 34), /* wDescriptorLength. */ + /* Endpoint 1 Descriptor.*/ + USB_DESC_ENDPOINT (USBD1_DATA_AVAILABLE_EP, /* bEndpointAddress.*/ + 0x03, /* bmAttributes (Interrupt). */ + 0x0040, /* wMaxPacketSize. */ + 0x0A), /* bInterval (10ms). */ + /* Endpoint 1 Descriptor.*/ + USB_DESC_ENDPOINT (USBD1_DATA_REQUEST_EP|0x80, /* bEndpointAddress.*/ + 0x03, /* bmAttributes (Interrupt). */ + 0x0040, /* wMaxPacketSize. */ + 0x0A) /* bInterval (10ms). */ +}; + +/* + * Configuration Descriptor wrapper. + */ +static const USBDescriptor hid_configuration_descriptor = { + sizeof hid_configuration_descriptor_data, + hid_configuration_descriptor_data +}; + +/* + * HID Descriptor wrapper. + */ +static const USBDescriptor hid_descriptor = { + HID_DESCRIPTOR_SIZE, + &hid_configuration_descriptor_data[HID_DESCRIPTOR_OFFSET] +}; + +/* + * HID Report Descriptor + * + * This is the description of the format and the content of the + * different IN or/and OUT reports that your application can + * receive/send + * + * See "Device Class Definition for Human Interface Devices (HID)" + * (http://www.usb.org/developers/hidpage/HID1_11.pdf) for the + * detailed description of all the fields + */ +static const uint8_t hid_report_descriptor_data[] = { + USB_DESC_BYTE (0x06), /* Usage Page - */ + USB_DESC_WORD (0xFF00), /* Vendor Defined. */ + USB_DESC_BYTE (0x09), /* Usage - */ + USB_DESC_BYTE (0x01), /* Vendor Defined. */ + USB_DESC_BYTE (0xA1), /* Collection - */ + USB_DESC_BYTE (0x01), /* Application. */ + + USB_DESC_BYTE (0x09), /* Usage - */ + USB_DESC_BYTE (0x01), /* Vendor Defined. */ + USB_DESC_BYTE (0x15), /* Logical Minimum - */ + USB_DESC_BYTE (0x00), /* 0. */ + USB_DESC_BYTE (0x26), /* Logical Maximum - */ + USB_DESC_WORD (0x00FF), /* 255. */ + USB_DESC_BYTE (0x75), /* Report size - */ + USB_DESC_BYTE (0x08), /* 8 bits. */ + USB_DESC_BYTE (0x95), /* Report count - */ + USB_DESC_BYTE (0x01), /* 1. */ + USB_DESC_BYTE (0x81), /* Input - */ + USB_DESC_BYTE (0x02), /* Data, Variable, Absolute. */ + + USB_DESC_BYTE (0x09), /* Usage - */ + USB_DESC_BYTE (0x01), /* Vendor Defined. */ + USB_DESC_BYTE (0x15), /* Logical Minimum - */ + USB_DESC_BYTE (0x00), /* 0. */ + USB_DESC_BYTE (0x26), /* Logical Maximum - */ + USB_DESC_WORD (0x00FF), /* 255. */ + USB_DESC_BYTE (0x75), /* Report Size - */ + USB_DESC_BYTE (0x08), /* 8 bits. */ + USB_DESC_BYTE (0x95), /* Report Count - */ + USB_DESC_BYTE (0x01), /* 1. */ + USB_DESC_BYTE (0x91), /* Output - */ + USB_DESC_BYTE (0x02), /* Data, Variable, Absolute. */ + + USB_DESC_BYTE (0xC0) /* End Collection. */ +}; + +/* + * HID Report Descriptor wrapper + */ +static const USBDescriptor hid_report_descriptor = { + sizeof hid_report_descriptor_data, + hid_report_descriptor_data +}; + +/* + * U.S. English language identifier. + */ +static const uint8_t usb_string_langid[] = + USB_DESC_STRING(USB_DESC_WORD(0x0409)); /* wLANGID (U.S. English) */ + +/* + * Vendor string. + */ +static const uint8_t usb_string_vendor[] = + USB_DESC_STRING('N', 0, 'u', 0, 'v', 0, 'o', 0, 't', 0, 'o', 0, 'n', 0); + +/* + * Serial Number string. + */ +static const uint8_t usb_string_serial[] = + USB_DESC_STRING('0', 0, 'x', 0, 'D', 0, 'E', 0, 'A', 0, 'D', 0, 'B', 0, 'E', 0, 'E', 0, 'F', 0); + +/* + * Device Description string. + */ +static const uint8_t usb_string_description[] = + USB_DESC_STRING('C', 0, 'h', 0, 'i', 0, 'b', 0, 'i', 0, 'O', 0, 'S', 0, '/', 0, 'H', 0, 'A', 0, + 'L', 0, ' ', 0, 'U', 0, 'S', 0, 'B', 0, ' ', 0, 'D', 0, 'e', 0, 'm', 0, 'o', 0); +/* + * Strings wrappers array. + */ +static const USBDescriptor hid_strings[] = { + {sizeof usb_string_langid, usb_string_langid}, + {sizeof usb_string_vendor, usb_string_vendor}, + {sizeof usb_string_description, usb_string_description}, + {sizeof usb_string_serial, usb_string_serial} +}; + +/* + * Handles the GET_DESCRIPTOR callback. All required descriptors must be + * handled here. + */ +static const USBDescriptor *get_descriptor(USBDriver *usbp, + uint8_t dtype, + uint8_t dindex, + uint16_t lang) { + (void)usbp; + (void)lang; + switch (dtype) { + case USB_DESCRIPTOR_DEVICE: + return &hid_device_descriptor; + case USB_DESCRIPTOR_CONFIGURATION: + return &hid_configuration_descriptor; + case USB_DESCRIPTOR_STRING: + if (dindex < 4) + return &hid_strings[dindex]; + case USB_DESCRIPTOR_INTERFACE: + break; + case USB_DESCRIPTOR_ENDPOINT: + break; + case USB_DESCRIPTOR_HID: + return &hid_descriptor; + case HID_REPORT: + return &hid_report_descriptor; + default: + break; + } + return NULL; +} + +/** + * @brief IN EP1 state. + */ +static USBInEndpointState ep1instate; + +/** + * @brief OUT EP1 state. + */ +static USBOutEndpointState ep1outstate; + +/** + * @brief EP1 initialization structure (both IN and OUT). + */ +static const USBEndpointConfig ep1config = { + USB_EP_MODE_TYPE_INTR, + NULL, + hidDataTransmitted, + hidDataReceived, + 0x0040, + 0x0040, + &ep1instate, + &ep1outstate +}; + +/* + * Handles the USB driver global events. + */ +static void usb_event(USBDriver *usbp, usbevent_t event) { + switch (event) { + case USB_EVENT_RESET: + return; + case USB_EVENT_ADDRESS: + return; + case USB_EVENT_CONFIGURED: + osalSysLockFromISR(); + + /* Enables the endpoints specified into the configuration. + Note, this callback is invoked from an ISR so I-Class functions + must be used.*/ + usbInitEndpointI(usbp, USBD1_DATA_REQUEST_EP, &ep1config); + + /* Resetting the state of the CDC subsystem.*/ + hidConfigureHookI(&UHD1); + + osalSysUnlockFromISR(); + return; + case USB_EVENT_UNCONFIGURED: + return; + case USB_EVENT_SUSPEND: + return; + case USB_EVENT_WAKEUP: + return; + case USB_EVENT_STALLED: + return; + } + return; +} + +static bool req_handler(USBDriver *usbp) { + size_t n; + + if ((usbp->setup[0] & USB_RTYPE_TYPE_MASK) == USB_RTYPE_TYPE_CLASS) { + switch (usbp->setup[1]) { + case HID_GET_REPORT: + n = hidGetReport(0, &increment_var, sizeof(increment_var)); + usbSetupTransfer(usbp, &increment_var, n, NULL); + return true; + default: + return hidRequestsHook(usbp); + } + } + return hidRequestsHook(usbp); +} + +/** + * @brief Generate HID Report + * @details This function generates the data for an HID report so + * that it can be transferred to the host. + * + * @param[in] id report ID + * @param[out] bp data buffer pointer + * @param[in] n the maximum number of bytes for data buffer + * @return number of bytes of report in data buffer + */ +size_t hidGetReport(uint8_t id, uint8_t *bp, size_t n) { + + (void) id; + (void) n; + + increment_var++; + *bp = increment_var; + return sizeof(increment_var); +} + +/** + * @brief Set HID Report + * @details This function sets the data for an HID report + * that was transferred from the host. + * + * @param[in] id report ID + * @param[in] bp data buffer pointer + * @param[in] n the number of bytes in data buffer + * @return The operation status. + * @retval MSG_OK if the report was set. + */ +msg_t hidSetReport(uint8_t id, uint8_t *bp, size_t n) { + + (void) id; + (void) n; + + increment_var = *bp; + return MSG_OK; +} + +/* + * USB driver configuration. + */ +const USBConfig usbcfg = { + usb_event, + get_descriptor, + req_handler, + NULL +}; + +/* + * USB HID driver configuration. + */ +const USBHIDConfig usbhidcfg = { + &USBD1, + USBD1_DATA_REQUEST_EP, + USBD1_DATA_AVAILABLE_EP +}; + +/** @} */ diff --git a/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/usbcfg.h b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/usbcfg.h new file mode 100644 index 00000000..f6728822 --- /dev/null +++ b/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/USB_HID/usbcfg.h @@ -0,0 +1,52 @@ +/* + Copyright (C) 2016 Jonathan Struebel + Modifications copyright (C) 2020 Alex Lewontin + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/** + * @file usbcfg.h + * @brief USB driver config header. + * + * @addtogroup USB + * @{ + */ + +#ifndef USBCFG_H +#define USBCFG_H + +#include "hal_usb_lld.h" + +extern const USBConfig usbcfg; +extern const USBHIDConfig usbhidcfg; +extern USBHIDDriver UHD1; + +#define USB_DESC_STRING(...) \ + { \ + USB_DESC_BYTE((sizeof((int[]){__VA_ARGS__}) / sizeof(int)) + 2), \ + USB_DESC_BYTE(USB_DESCRIPTOR_STRING), __VA_ARGS__ \ + } + +#ifdef __cplusplus +extern "C" { +#endif + size_t hidGetReport(uint8_t id, uint8_t *bp, size_t n); + msg_t hidSetReport(uint8_t id, uint8_t *bp, size_t n); +#ifdef __cplusplus +} +#endif + +#endif /* USBCFG_H */ + +/** @} */