diff --git a/demos/bsp/boards/board.c b/demos/bsp/boards/board.c index e766bd206..aae65ca25 100644 --- a/demos/bsp/boards/board.c +++ b/demos/bsp/boards/board.c @@ -1,150 +1,150 @@ -/**************************************************************************/ -/*! - @file board.c - @author hathach (tinyusb.org) - - @section LICENSE - - Software License Agreement (BSD License) - - Copyright (c) 2013, hathach (tinyusb.org) - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. Neither the name of the copyright holders nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - This file is part of the tinyusb stack. -*/ -/**************************************************************************/ - -#include "board.h" - -#if TUSB_CFG_OS == TUSB_OS_NONE // TODO may move to main.c -volatile uint32_t system_ticks = 0; - -void SysTick_Handler (void) -{ - system_ticks++; - tusb_tick_tock(); // TODO temporarily -} - -#endif - -void check_failed(uint8_t *file, uint32_t line) -{ - (void) file; - (void) line; -} - -#if 0 - -/** - * HardFault_HandlerAsm: - * Alternative Hard Fault handler to help debug the reason for a fault. - * To use, edit the vector table to reference this function in the HardFault vector - * This code is suitable for Cortex-M3 and Cortex-M0 cores - */ - -// Use the 'naked' attribute so that C stacking is not used. -__attribute__((naked)) -void HardFault_HandlerAsm(void){ - /* - * Get the appropriate stack pointer, depending on our mode, - * and use it as the parameter to the C handler. This function - * will never return - */ - - __asm( ".syntax unified\n" - "MOVS R0, #4 \n" - "MOV R1, LR \n" - "TST R0, R1 \n" - "BEQ _MSP \n" - "MRS R0, PSP \n" - "B HardFault_HandlerC \n" - "_MSP: \n" - "MRS R0, MSP \n" - "B HardFault_HandlerC \n" - ".syntax divided\n") ; -} - -/** - * HardFaultHandler_C: - * This is called from the HardFault_HandlerAsm with a pointer the Fault stack - * as the parameter. We can then read the values from the stack and place them - * into local variables for ease of reading. - * We then read the various Fault Status and Address Registers to help decode - * cause of the fault. - * The function ends with a BKPT instruction to force control back into the debugger - */ -void HardFault_HandlerC(unsigned long *hardfault_args){ - ATTR_UNUSED volatile unsigned long stacked_r0 ; - ATTR_UNUSED volatile unsigned long stacked_r1 ; - ATTR_UNUSED volatile unsigned long stacked_r2 ; - ATTR_UNUSED volatile unsigned long stacked_r3 ; - ATTR_UNUSED volatile unsigned long stacked_r12 ; - ATTR_UNUSED volatile unsigned long stacked_lr ; - ATTR_UNUSED volatile unsigned long stacked_pc ; - ATTR_UNUSED volatile unsigned long stacked_psr ; - ATTR_UNUSED volatile unsigned long _CFSR ; - ATTR_UNUSED volatile unsigned long _HFSR ; - ATTR_UNUSED volatile unsigned long _DFSR ; - ATTR_UNUSED volatile unsigned long _AFSR ; - ATTR_UNUSED volatile unsigned long _BFAR ; - ATTR_UNUSED volatile unsigned long _MMAR ; - - stacked_r0 = ((unsigned long)hardfault_args[0]) ; - stacked_r1 = ((unsigned long)hardfault_args[1]) ; - stacked_r2 = ((unsigned long)hardfault_args[2]) ; - stacked_r3 = ((unsigned long)hardfault_args[3]) ; - stacked_r12 = ((unsigned long)hardfault_args[4]) ; - stacked_lr = ((unsigned long)hardfault_args[5]) ; - stacked_pc = ((unsigned long)hardfault_args[6]) ; - stacked_psr = ((unsigned long)hardfault_args[7]) ; - - // Configurable Fault Status Register - // Consists of MMSR, BFSR and UFSR - _CFSR = (*((volatile unsigned long *)(0xE000ED28))) ; - - // Hard Fault Status Register - _HFSR = (*((volatile unsigned long *)(0xE000ED2C))) ; - - // Debug Fault Status Register - _DFSR = (*((volatile unsigned long *)(0xE000ED30))) ; - - // Auxiliary Fault Status Register - _AFSR = (*((volatile unsigned long *)(0xE000ED3C))) ; - - // Read the Fault Address Registers. These may not contain valid values. - // Check BFARVALID/MMARVALID to see if they are valid values - // MemManage Fault Address Register - _MMAR = (*((volatile unsigned long *)(0xE000ED34))) ; - // Bus Fault Address Register - _BFAR = (*((volatile unsigned long *)(0xE000ED38))) ; - -// if ((CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk)==CoreDebug_DHCSR_C_DEBUGEN_Msk) /* if there is debugger connected */ -// { -// __asm("BKPT #0\n"); -// } - - hal_debugger_breakpoint(); -} -#endif +/**************************************************************************/ +/*! + @file board.c + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2013, hathach (tinyusb.org) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + This file is part of the tinyusb stack. +*/ +/**************************************************************************/ + +#include "board.h" + +#if TUSB_CFG_OS == TUSB_OS_NONE // TODO may move to main.c +volatile uint32_t system_ticks = 0; + +void SysTick_Handler (void) +{ + system_ticks++; + tusb_tick_tock(); // TODO temporarily +} + +#endif + +void check_failed(uint8_t *file, uint32_t line) +{ + (void) file; + (void) line; +} + +#if 0 + +/** + * HardFault_HandlerAsm: + * Alternative Hard Fault handler to help debug the reason for a fault. + * To use, edit the vector table to reference this function in the HardFault vector + * This code is suitable for Cortex-M3 and Cortex-M0 cores + */ + +// Use the 'naked' attribute so that C stacking is not used. +__attribute__((naked)) +void HardFault_HandlerAsm(void){ + /* + * Get the appropriate stack pointer, depending on our mode, + * and use it as the parameter to the C handler. This function + * will never return + */ + + __asm( ".syntax unified\n" + "MOVS R0, #4 \n" + "MOV R1, LR \n" + "TST R0, R1 \n" + "BEQ _MSP \n" + "MRS R0, PSP \n" + "B HardFault_HandlerC \n" + "_MSP: \n" + "MRS R0, MSP \n" + "B HardFault_HandlerC \n" + ".syntax divided\n") ; +} + +/** + * HardFaultHandler_C: + * This is called from the HardFault_HandlerAsm with a pointer the Fault stack + * as the parameter. We can then read the values from the stack and place them + * into local variables for ease of reading. + * We then read the various Fault Status and Address Registers to help decode + * cause of the fault. + * The function ends with a BKPT instruction to force control back into the debugger + */ +void HardFault_HandlerC(unsigned long *hardfault_args){ + ATTR_UNUSED volatile unsigned long stacked_r0 ; + ATTR_UNUSED volatile unsigned long stacked_r1 ; + ATTR_UNUSED volatile unsigned long stacked_r2 ; + ATTR_UNUSED volatile unsigned long stacked_r3 ; + ATTR_UNUSED volatile unsigned long stacked_r12 ; + ATTR_UNUSED volatile unsigned long stacked_lr ; + ATTR_UNUSED volatile unsigned long stacked_pc ; + ATTR_UNUSED volatile unsigned long stacked_psr ; + ATTR_UNUSED volatile unsigned long _CFSR ; + ATTR_UNUSED volatile unsigned long _HFSR ; + ATTR_UNUSED volatile unsigned long _DFSR ; + ATTR_UNUSED volatile unsigned long _AFSR ; + ATTR_UNUSED volatile unsigned long _BFAR ; + ATTR_UNUSED volatile unsigned long _MMAR ; + + stacked_r0 = ((unsigned long)hardfault_args[0]) ; + stacked_r1 = ((unsigned long)hardfault_args[1]) ; + stacked_r2 = ((unsigned long)hardfault_args[2]) ; + stacked_r3 = ((unsigned long)hardfault_args[3]) ; + stacked_r12 = ((unsigned long)hardfault_args[4]) ; + stacked_lr = ((unsigned long)hardfault_args[5]) ; + stacked_pc = ((unsigned long)hardfault_args[6]) ; + stacked_psr = ((unsigned long)hardfault_args[7]) ; + + // Configurable Fault Status Register + // Consists of MMSR, BFSR and UFSR + _CFSR = (*((volatile unsigned long *)(0xE000ED28))) ; + + // Hard Fault Status Register + _HFSR = (*((volatile unsigned long *)(0xE000ED2C))) ; + + // Debug Fault Status Register + _DFSR = (*((volatile unsigned long *)(0xE000ED30))) ; + + // Auxiliary Fault Status Register + _AFSR = (*((volatile unsigned long *)(0xE000ED3C))) ; + + // Read the Fault Address Registers. These may not contain valid values. + // Check BFARVALID/MMARVALID to see if they are valid values + // MemManage Fault Address Register + _MMAR = (*((volatile unsigned long *)(0xE000ED34))) ; + // Bus Fault Address Register + _BFAR = (*((volatile unsigned long *)(0xE000ED38))) ; + +// if ((CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk)==CoreDebug_DHCSR_C_DEBUGEN_Msk) /* if there is debugger connected */ +// { +// __asm("BKPT #0\n"); +// } + + hal_debugger_breakpoint(); +} +#endif diff --git a/demos/bsp/boards/embedded_artists/ea4357/board_ea4357.c b/demos/bsp/boards/embedded_artists/ea4357/board_ea4357.c index 09d4e121c..d0ac98beb 100644 --- a/demos/bsp/boards/embedded_artists/ea4357/board_ea4357.c +++ b/demos/bsp/boards/embedded_artists/ea4357/board_ea4357.c @@ -1,175 +1,175 @@ -/**************************************************************************/ -/*! - @file board_ea4357.c - @author hathach (tinyusb.org) - - @section LICENSE - - Software License Agreement (BSD License) - - Copyright (c) 2013, hathach (tinyusb.org) - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. Neither the name of the copyright holders nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - This file is part of the tinyusb stack. -*/ -/**************************************************************************/ - -#include "../../board.h" - -#if BOARD == BOARD_EA4357 - -#define BOARD_UART_PORT LPC_USART0 -#define BOARD_UART_PIN_PORT 0x0f -#define BOARD_UART_PIN_TX 10 // PF.10 : UART0_TXD -#define BOARD_UART_PIN_RX 11 // PF.11 : UART0_RXD - -const static struct { - uint8_t mux_port; - uint8_t mux_pin; - - uint8_t gpio_port; - uint8_t gpio_pin; -}buttons[] = -{ - {0x0a, 3, 4, 10 }, // Joystick up - {0x09, 1, 4, 13 }, // Joystick down - {0x0a, 2, 4, 9 }, // Joystick left - {0x09, 0, 4, 12 }, // Joystick right - {0x0a, 1, 4, 8 }, // Joystick press - {0x02, 7, 0, 7 }, // SW6 -}; - -enum { - BOARD_BUTTON_COUNT = sizeof(buttons) / sizeof(buttons[0]) -}; - -void board_init(void) -{ - CGU_Init(); - -#if TUSB_CFG_OS == TUSB_OS_NONE // TODO may move to main.c - SysTick_Config(CGU_GetPCLKFrequency(CGU_PERIPHERAL_M4CORE) / CFG_TICKS_PER_SECOND); // 1 msec tick timer -#endif - - //------------- USB -------------// - // USB0 Power: EA4357 channel B U20 GPIO26 active low (base board), P2_3 on LPC4357 - scu_pinmux(0x02, 3, MD_PUP | MD_EZI, FUNC7); // USB0 VBus Power - - #if TUSB_CFG_CONTROLLER_0_MODE & TUSB_MODE_DEVICE - scu_pinmux(0x09, 5, GPIO_PDN, FUNC4); // P9_5 (GPIO5[18]) (GPIO28 on oem base) as USB connect, active low. - GPIO_SetDir(5, BIT_(18), 1); - #endif - - // USB1 Power: EA4357 channel A U20 is enabled by SJ5 connected to pad 1-2, no more action required - // TODO Remove R170, R171, solder a pair of 15K to USB1 D+/D- to test with USB1 Host - - //------------- LED -------------// - I2C_Init(LPC_I2C0, 100000); - I2C_Cmd(LPC_I2C0, ENABLE); - pca9532_init(); - - //------------- BUTTON -------------// - for(uint8_t i=0; iCREG6 &= ~0x7; - - /* RMII mode setup only */ - LPC_CREG->CREG6 |= 0x4; - - scu_pinmux(0x1, 18, (MD_EHS | MD_PLN | MD_ZI) , FUNC3); // ENET TXD0 - scu_pinmux(0x1, 20, (MD_EHS | MD_PLN | MD_ZI) , FUNC3); // ENET TXD1 - scu_pinmux(0x0, 1 , (MD_EHS | MD_PLN | MD_ZI) , FUNC6); // ENET TX Enable - - scu_pinmux(0x1, 15, (MD_EHS | MD_PLN | MD_EZI | MD_ZI), FUNC3); // ENET RXD0 - scu_pinmux(0x0, 0 , (MD_EHS | MD_PLN | MD_EZI | MD_ZI), FUNC2); // ENET RXD1 - scu_pinmux(0x1, 16, (MD_EHS | MD_PLN | MD_EZI | MD_ZI), FUNC7); // ENET RX Data Valid - - scu_pinmux(0x1, 19, (MD_EHS | MD_PLN | MD_EZI | MD_ZI), FUNC0); // ENET REF CLK - scu_pinmux(0x1, 17, (MD_EHS | MD_PLN | MD_EZI | MD_ZI), FUNC3); // ENET MDIO - scu_pinmux(0xC, 1 , (MD_EHS | MD_PLN | MD_ZI) , FUNC3); // ENET MDC -#endif - -} - -//--------------------------------------------------------------------+ -// LEDS -//--------------------------------------------------------------------+ -void board_leds(uint32_t on_mask, uint32_t off_mask) -{ - pca9532_setLeds( on_mask << 8, off_mask << 8); -} - -//--------------------------------------------------------------------+ -// BUTTONS -//--------------------------------------------------------------------+ -static bool button_read(uint8_t id) -{ - return !BIT_TEST_( GPIO_ReadValue(buttons[id].gpio_port), buttons[id].gpio_pin ); // button is active low -} - -uint32_t board_buttons(void) -{ - uint32_t result = 0; - - for(uint8_t i=0; iCREG6 &= ~0x7; + + /* RMII mode setup only */ + LPC_CREG->CREG6 |= 0x4; + + scu_pinmux(0x1, 18, (MD_EHS | MD_PLN | MD_ZI) , FUNC3); // ENET TXD0 + scu_pinmux(0x1, 20, (MD_EHS | MD_PLN | MD_ZI) , FUNC3); // ENET TXD1 + scu_pinmux(0x0, 1 , (MD_EHS | MD_PLN | MD_ZI) , FUNC6); // ENET TX Enable + + scu_pinmux(0x1, 15, (MD_EHS | MD_PLN | MD_EZI | MD_ZI), FUNC3); // ENET RXD0 + scu_pinmux(0x0, 0 , (MD_EHS | MD_PLN | MD_EZI | MD_ZI), FUNC2); // ENET RXD1 + scu_pinmux(0x1, 16, (MD_EHS | MD_PLN | MD_EZI | MD_ZI), FUNC7); // ENET RX Data Valid + + scu_pinmux(0x1, 19, (MD_EHS | MD_PLN | MD_EZI | MD_ZI), FUNC0); // ENET REF CLK + scu_pinmux(0x1, 17, (MD_EHS | MD_PLN | MD_EZI | MD_ZI), FUNC3); // ENET MDIO + scu_pinmux(0xC, 1 , (MD_EHS | MD_PLN | MD_ZI) , FUNC3); // ENET MDC +#endif + +} + +//--------------------------------------------------------------------+ +// LEDS +//--------------------------------------------------------------------+ +void board_leds(uint32_t on_mask, uint32_t off_mask) +{ + pca9532_setLeds( on_mask << 8, off_mask << 8); +} + +//--------------------------------------------------------------------+ +// BUTTONS +//--------------------------------------------------------------------+ +static bool button_read(uint8_t id) +{ + return !BIT_TEST_( GPIO_ReadValue(buttons[id].gpio_port), buttons[id].gpio_pin ); // button is active low +} + +uint32_t board_buttons(void) +{ + uint32_t result = 0; + + for(uint8_t i=0; i>= 4; - } -} - -static void setLeds(void) -{ - uint8_t buf[5]; - uint8_t ls[4] = {0,0,0,0}; - uint16_t states = ledStateShadow; - - /* LEDs in On/Off state */ - setLsStates(states, ls, LS_MODE_ON); - - /* set the LEDs that should blink */ - setLsStates(blink0Shadow, ls, LS_MODE_BLINK0); - setLsStates(blink1Shadow, ls, LS_MODE_BLINK1); - - - buf[0] = PCA9532_LS0 | PCA9532_AUTO_INC; - buf[1] = ls[0]; - buf[2] = ls[1]; - buf[3] = ls[2]; - buf[4] = ls[3]; - I2CWrite(PCA9532_I2C_ADDR, buf, 5); -} - -/****************************************************************************** - * Public Functions - *****************************************************************************/ - -/****************************************************************************** - * - * Description: - * Initialize the PCA9532 Device - * - *****************************************************************************/ -void pca9532_init (void) -{ - /* nothing to initialize */ -} - -/****************************************************************************** - * - * Description: - * Get the LED states - * - * Params: - * [in] shadow - TRUE if the states should be retrieved from the shadow - * variables. The shadow variable are updated when any - * of setLeds, setBlink0Leds and/or setBlink1Leds are - * called. - * - * FALSE if the state should be retrieved from the PCA9532 - * device. A blinkin LED may be reported as on or off - * depending on the state when calling the function. - * - * Returns: - * A mask where a 1 indicates that a LED is on (or blinking). - * - *****************************************************************************/ -uint16_t pca9532_getLedState (uint32_t shadow) -{ - uint8_t buf[2]; - uint16_t ret = 0; - - if (shadow) { - /* a blink LED is reported as on*/ - ret = (ledStateShadow | blink0Shadow | blink1Shadow); - } - else { - - /* - * A blinking LED may be reported as on or off depending on - * its state when reading the Input register. - */ - - buf[0] = PCA9532_INPUT0; - I2CWrite(PCA9532_I2C_ADDR, buf, 1); - - I2CRead(PCA9532_I2C_ADDR, buf, 1); - ret = buf[0]; - - - buf[0] = PCA9532_INPUT1; - I2CWrite(PCA9532_I2C_ADDR, buf, 1); - - I2CRead(PCA9532_I2C_ADDR, buf, 1); - ret |= (buf[0] << 8); - - - /* invert since LEDs are active low */ - ret = ((~ret) & 0xFFFF); - } - - return (ret & ~PCA9532_NOT_USED); -} - - -/****************************************************************************** - * - * Description: - * Set LED states (on or off). - * - * Params: - * [in] ledOnMask - The LEDs that should be turned on. This mask has - * priority over ledOffMask - * [in] ledOffMask - The LEDs that should be turned off. - * - *****************************************************************************/ -void pca9532_setLeds (uint16_t ledOnMask, uint16_t ledOffMask) -{ - /* turn off leds */ - ledStateShadow &= (~(ledOffMask) & 0xffff); - - /* ledOnMask has priority over ledOffMask */ - ledStateShadow |= ledOnMask; - - /* turn off blinking */ - blink0Shadow &= (~(ledOffMask) & 0xffff); - blink1Shadow &= (~(ledOffMask) & 0xffff); - - setLeds(); -} - -/****************************************************************************** - * - * Description: - * Set the blink period for PWM0. Valid values are 0 - 255 where 0 - * means 152 Hz and 255 means 0.59 Hz. A value of 151 means 1 Hz. - * - * Params: - * [in] period - the period for pwm0 - * - *****************************************************************************/ -void pca9532_setBlink0Period(uint8_t period) -{ - uint8_t buf[2]; - - buf[0] = PCA9532_PSC0; - buf[1] = period; - I2CWrite(PCA9532_I2C_ADDR, buf, 2); -} - -/****************************************************************************** - * - * Description: - * Set the duty cycle for PWM0. Valid values are 0 - 100. 25 means the LED - * is on 25% of the period. - * - * Params: - * [in] duty - duty cycle - * - *****************************************************************************/ -void pca9532_setBlink0Duty(uint8_t duty) -{ - uint8_t buf[2]; - uint32_t tmp = duty; - if (tmp > 100) { - tmp = 100; - } - - tmp = (256 * tmp)/100; - - buf[0] = PCA9532_PWM0; - buf[1] = tmp; - I2CWrite(PCA9532_I2C_ADDR, buf, 2); -} - -/****************************************************************************** - * - * Description: - * Set the LEDs that should blink with rate and duty cycle from PWM0. - * Blinking is turned off with pca9532_setLeds. - * - * Params: - * [in] ledMask - LEDs that should blink. - * - *****************************************************************************/ -void pca9532_setBlink0Leds(uint16_t ledMask) -{ - blink0Shadow |= ledMask; - setLeds(); -} - -/****************************************************************************** - * - * Description: - * Set the blink period for PWM1. Valid values are 0 - 255 where 0 - * means 152 Hz and 255 means 0.59 Hz. A value of 151 means 1 Hz. - * - * Params: - * [in] period - The period for PWM1 - * - *****************************************************************************/ -void pca9532_setBlink1Period(uint8_t period) -{ - uint8_t buf[2]; - - buf[0] = PCA9532_PSC1; - buf[1] = period; - I2CWrite(PCA9532_I2C_ADDR, buf, 2); -} - -/****************************************************************************** - * - * Description: - * Set the duty cycle for PWM1. Valid values are 0 - 100. 25 means the LED - * is on 25% of the period. - * - * Params: - * [in] duty - duty cycle. - * - *****************************************************************************/ -void pca9532_setBlink1Duty(uint8_t duty) -{ - uint8_t buf[2]; - - uint32_t tmp = duty; - if (tmp > 100) { - tmp = 100; - } - - tmp = (256 * tmp)/100; - - buf[0] = PCA9532_PWM1; - buf[1] = tmp; - I2CWrite(PCA9532_I2C_ADDR, buf, 2); -} - -/****************************************************************************** - * - * Description: - * Set the LEDs that should blink with rate and duty cycle from PWM1. - * Blinking is turned off with pca9532_setLeds. - * - * Params: - * [in] ledMask - LEDs that should blink. - * - *****************************************************************************/ -void pca9532_setBlink1Leds(uint16_t ledMask) -{ - blink1Shadow |= ledMask; - setLeds(); -} - -#endif +/***************************************************************************** + * + * Copyright(C) 2011, Embedded Artists AB + * All rights reserved. + * + ****************************************************************************** + * Software that is described herein is for illustrative purposes only + * which provides customers with programming information regarding the + * products. This software is supplied "AS IS" without any warranties. + * Embedded Artists AB assumes no responsibility or liability for the + * use of the software, conveys no license or title under any patent, + * copyright, or mask work right to the product. Embedded Artists AB + * reserves the right to make changes in the software without + * notification. Embedded Artists AB also make no representation or + * warranty that such application will be suitable for the specified + * use without further testing or modification. + *****************************************************************************/ + +/* + * NOTE: I2C must have been initialized before calling any functions in this + * file. + */ + +/****************************************************************************** + * Includes + *****************************************************************************/ +#include "../../board.h" + +#if BOARD == BOARD_EA4357 + +#include "lpc43xx_i2c.h" +#include "lpc43xx_cgu.h" +#include "lpc_types.h" +#include "pca9532.h" + +/****************************************************************************** + * Defines and typedefs + *****************************************************************************/ + +#define I2C_PORT (LPC_I2C0) + +#define LS_MODE_ON 0x01 +#define LS_MODE_BLINK0 0x02 +#define LS_MODE_BLINK1 0x03 + +/****************************************************************************** + * External global variables + *****************************************************************************/ + + +/****************************************************************************** + * Local variables + *****************************************************************************/ + +static uint16_t blink0Shadow = 0; +static uint16_t blink1Shadow = 0; +static uint16_t ledStateShadow = 0; + +/****************************************************************************** + * Local Functions + *****************************************************************************/ + +static Status I2CWrite(uint32_t addr, uint8_t* buf, uint32_t len) +{ + I2C_M_SETUP_Type i2cData; + + i2cData.sl_addr7bit = addr; + i2cData.tx_data = buf; + i2cData.tx_length = len; + i2cData.rx_data = NULL; + i2cData.rx_length = 0; + i2cData.retransmissions_max = 3; + + return I2C_MasterTransferData(I2C_PORT, &i2cData, I2C_TRANSFER_POLLING); +} + +static Status I2CRead(uint32_t addr, uint8_t* buf, uint32_t len) +{ + I2C_M_SETUP_Type i2cData; + + i2cData.sl_addr7bit = addr; + i2cData.tx_data = NULL; + i2cData.tx_length = 0; + i2cData.rx_data = buf; + i2cData.rx_length = len; + i2cData.retransmissions_max = 3; + + return I2C_MasterTransferData(I2C_PORT, &i2cData, I2C_TRANSFER_POLLING); +} + +static void setLsStates(uint16_t states, uint8_t* ls, uint8_t mode) +{ +#define IS_LED_SET(bit, x) ( ( ((x) & (bit)) != 0 ) ? 1 : 0 ) + + int i = 0; + + for (i = 0; i < 4; i++) { + + ls[i] |= ( (IS_LED_SET(0x0001, states)*mode << 0) + | (IS_LED_SET(0x0002, states)*mode << 2) + | (IS_LED_SET(0x0004, states)*mode << 4) + | (IS_LED_SET(0x0008, states)*mode << 6) ); + + states >>= 4; + } +} + +static void setLeds(void) +{ + uint8_t buf[5]; + uint8_t ls[4] = {0,0,0,0}; + uint16_t states = ledStateShadow; + + /* LEDs in On/Off state */ + setLsStates(states, ls, LS_MODE_ON); + + /* set the LEDs that should blink */ + setLsStates(blink0Shadow, ls, LS_MODE_BLINK0); + setLsStates(blink1Shadow, ls, LS_MODE_BLINK1); + + + buf[0] = PCA9532_LS0 | PCA9532_AUTO_INC; + buf[1] = ls[0]; + buf[2] = ls[1]; + buf[3] = ls[2]; + buf[4] = ls[3]; + I2CWrite(PCA9532_I2C_ADDR, buf, 5); +} + +/****************************************************************************** + * Public Functions + *****************************************************************************/ + +/****************************************************************************** + * + * Description: + * Initialize the PCA9532 Device + * + *****************************************************************************/ +void pca9532_init (void) +{ + /* nothing to initialize */ +} + +/****************************************************************************** + * + * Description: + * Get the LED states + * + * Params: + * [in] shadow - TRUE if the states should be retrieved from the shadow + * variables. The shadow variable are updated when any + * of setLeds, setBlink0Leds and/or setBlink1Leds are + * called. + * + * FALSE if the state should be retrieved from the PCA9532 + * device. A blinkin LED may be reported as on or off + * depending on the state when calling the function. + * + * Returns: + * A mask where a 1 indicates that a LED is on (or blinking). + * + *****************************************************************************/ +uint16_t pca9532_getLedState (uint32_t shadow) +{ + uint8_t buf[2]; + uint16_t ret = 0; + + if (shadow) { + /* a blink LED is reported as on*/ + ret = (ledStateShadow | blink0Shadow | blink1Shadow); + } + else { + + /* + * A blinking LED may be reported as on or off depending on + * its state when reading the Input register. + */ + + buf[0] = PCA9532_INPUT0; + I2CWrite(PCA9532_I2C_ADDR, buf, 1); + + I2CRead(PCA9532_I2C_ADDR, buf, 1); + ret = buf[0]; + + + buf[0] = PCA9532_INPUT1; + I2CWrite(PCA9532_I2C_ADDR, buf, 1); + + I2CRead(PCA9532_I2C_ADDR, buf, 1); + ret |= (buf[0] << 8); + + + /* invert since LEDs are active low */ + ret = ((~ret) & 0xFFFF); + } + + return (ret & ~PCA9532_NOT_USED); +} + + +/****************************************************************************** + * + * Description: + * Set LED states (on or off). + * + * Params: + * [in] ledOnMask - The LEDs that should be turned on. This mask has + * priority over ledOffMask + * [in] ledOffMask - The LEDs that should be turned off. + * + *****************************************************************************/ +void pca9532_setLeds (uint16_t ledOnMask, uint16_t ledOffMask) +{ + /* turn off leds */ + ledStateShadow &= (~(ledOffMask) & 0xffff); + + /* ledOnMask has priority over ledOffMask */ + ledStateShadow |= ledOnMask; + + /* turn off blinking */ + blink0Shadow &= (~(ledOffMask) & 0xffff); + blink1Shadow &= (~(ledOffMask) & 0xffff); + + setLeds(); +} + +/****************************************************************************** + * + * Description: + * Set the blink period for PWM0. Valid values are 0 - 255 where 0 + * means 152 Hz and 255 means 0.59 Hz. A value of 151 means 1 Hz. + * + * Params: + * [in] period - the period for pwm0 + * + *****************************************************************************/ +void pca9532_setBlink0Period(uint8_t period) +{ + uint8_t buf[2]; + + buf[0] = PCA9532_PSC0; + buf[1] = period; + I2CWrite(PCA9532_I2C_ADDR, buf, 2); +} + +/****************************************************************************** + * + * Description: + * Set the duty cycle for PWM0. Valid values are 0 - 100. 25 means the LED + * is on 25% of the period. + * + * Params: + * [in] duty - duty cycle + * + *****************************************************************************/ +void pca9532_setBlink0Duty(uint8_t duty) +{ + uint8_t buf[2]; + uint32_t tmp = duty; + if (tmp > 100) { + tmp = 100; + } + + tmp = (256 * tmp)/100; + + buf[0] = PCA9532_PWM0; + buf[1] = tmp; + I2CWrite(PCA9532_I2C_ADDR, buf, 2); +} + +/****************************************************************************** + * + * Description: + * Set the LEDs that should blink with rate and duty cycle from PWM0. + * Blinking is turned off with pca9532_setLeds. + * + * Params: + * [in] ledMask - LEDs that should blink. + * + *****************************************************************************/ +void pca9532_setBlink0Leds(uint16_t ledMask) +{ + blink0Shadow |= ledMask; + setLeds(); +} + +/****************************************************************************** + * + * Description: + * Set the blink period for PWM1. Valid values are 0 - 255 where 0 + * means 152 Hz and 255 means 0.59 Hz. A value of 151 means 1 Hz. + * + * Params: + * [in] period - The period for PWM1 + * + *****************************************************************************/ +void pca9532_setBlink1Period(uint8_t period) +{ + uint8_t buf[2]; + + buf[0] = PCA9532_PSC1; + buf[1] = period; + I2CWrite(PCA9532_I2C_ADDR, buf, 2); +} + +/****************************************************************************** + * + * Description: + * Set the duty cycle for PWM1. Valid values are 0 - 100. 25 means the LED + * is on 25% of the period. + * + * Params: + * [in] duty - duty cycle. + * + *****************************************************************************/ +void pca9532_setBlink1Duty(uint8_t duty) +{ + uint8_t buf[2]; + + uint32_t tmp = duty; + if (tmp > 100) { + tmp = 100; + } + + tmp = (256 * tmp)/100; + + buf[0] = PCA9532_PWM1; + buf[1] = tmp; + I2CWrite(PCA9532_I2C_ADDR, buf, 2); +} + +/****************************************************************************** + * + * Description: + * Set the LEDs that should blink with rate and duty cycle from PWM1. + * Blinking is turned off with pca9532_setLeds. + * + * Params: + * [in] ledMask - LEDs that should blink. + * + *****************************************************************************/ +void pca9532_setBlink1Leds(uint16_t ledMask) +{ + blink1Shadow |= ledMask; + setLeds(); +} + +#endif diff --git a/demos/bsp/boards/lpcxpresso/board_lpcxpresso1347.c b/demos/bsp/boards/lpcxpresso/board_lpcxpresso1347.c index 1a6f9493f..816e1c951 100644 --- a/demos/bsp/boards/lpcxpresso/board_lpcxpresso1347.c +++ b/demos/bsp/boards/lpcxpresso/board_lpcxpresso1347.c @@ -1,133 +1,133 @@ -/**************************************************************************/ -/*! - @file board_lpcexpresso1347.c - @author hathach (tinyusb.org) - - @section LICENSE - - Software License Agreement (BSD License) - - Copyright (c) 2013, hathach (tinyusb.org) - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. Neither the name of the copyright holders nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - This file is part of the tinyusb stack. -*/ -/**************************************************************************/ - -#include "../board.h" - -#if BOARD == BOARD_LPCXPRESSO1347 - -#define LED_PORT (0) -#define LED_PIN (7) -#define LED_ON (1) -#define LED_OFF (0) - -const static struct { - uint8_t port; - uint8_t pin; -} buttons[] = -{ - {1, 22 }, // Joystick up - {1, 20 }, // Joystick down - {1, 23 }, // Joystick left - {1, 21 }, // Joystick right - {1, 19 }, // Joystick press - {0, 1 }, // SW3 -// {1, 4 }, // SW4 (require to remove J28) -}; - -enum { - BOARD_BUTTON_COUNT = sizeof(buttons) / sizeof(buttons[0]) -}; - - -void board_init(void) -{ - SystemInit(); - -#if TUSB_CFG_OS == TUSB_OS_NONE // TODO may move to main.c - SysTick_Config(SystemCoreClock / CFG_TICKS_PER_SECOND); // 1 msec tick timer -#endif - - GPIOInit(); - - //------------- LED -------------// - GPIOSetDir(LED_PORT, LED_PIN, 1); - LPC_GPIO->CLR[LED_PORT] = (1 << LED_PIN); - - //------------- BUTTON -------------// - for(uint8_t i=0; iCLR[LED_PORT] = (1 << LED_PIN); + + //------------- BUTTON -------------// + for(uint8_t i=0; iSET[BOARD_LED0_GPIO_PORT] = (1 << BOARD_LED0_GPIO_PIN); - } - - if (off_mask & 0x01) - { - LPC_GPIO_PORT->CLR[BOARD_LED0_GPIO_PORT] = (1 << BOARD_LED0_GPIO_PIN); - } -} - -//--------------------------------------------------------------------+ -// UART -//--------------------------------------------------------------------+ -#if CFG_UART_ENABLE -uint32_t board_uart_send(uint8_t *buffer, uint32_t length) -{ - return UART_Send(BOARD_UART_PORT, buffer, length, BLOCKING); -} - -uint32_t board_uart_recv(uint8_t *buffer, uint32_t length) -{ - return UART_Receive(BOARD_UART_PORT, buffer, length, BLOCKING); -} -#endif - -#endif +/**************************************************************************/ +/*! + @file board_lpc4357usb.c + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2013, hathach (tinyusb.org) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + This file is part of the tinyusb stack. +*/ +/**************************************************************************/ + +#include "../board.h" + +#if BOARD == BOARD_LPC4357USB + +#define BOARD_UART_PORT (LPC_USART0) +#define BOARD_UART_PIN_PORT (0x0F) +#define BOARD_UART_PIN_TX (10) // PF.10 : UART0_TXD +#define BOARD_UART_PIN_RX (11) // PF.11 : UART0_RXD + +#define BOARD_LED0_PORT (0x0C) +#define BOARD_LED0_PIN (2) // PC.2 = User LED +#define BOARD_LED0_FUNCTION (4) // GPIO multiplexed as function 4 on PC.2 +#define BOARD_LED0_GPIO_PORT (6) +#define BOARD_LED0_GPIO_PIN (1) // PC.2 = GPIO 6[1] + +void board_init(void) +{ + CGU_Init(); + + /* Setup the systick time for 1ms ticks */ + SysTick_Config(CGU_GetPCLKFrequency(CGU_PERIPHERAL_M4CORE) / CFG_TICKS_PER_SECOND); + + /* Configure LED0 as GPIO */ + scu_pinmux(BOARD_LED0_PORT, BOARD_LED0_PIN, MD_PDN, BOARD_LED0_FUNCTION); + GPIO_SetDir(BOARD_LED0_GPIO_PORT, (1 << BOARD_LED0_GPIO_PIN), 1); + + /* Configure TRACE pins */ + scu_pinmux(0xF, 4, MD_PDN, 0x2); /* PF_4 = TRACECLK */ + scu_pinmux(0x7, 4, MD_PDN, 0x5); /* P7_4 = TRACEDATA[0] */ + scu_pinmux(0x7, 5, MD_PDN, 0x5); /* P7_5 = TRACEDATA[1] */ + scu_pinmux(0x7, 6, MD_PDN, 0x5); /* P7_6 = TRACEDATA[2] */ + scu_pinmux(0x7, 7, MD_PDN, 0x5); /* P7_7 = TRACEDATA[3] */ + + // USB0 Power: EA4357 channel B U20 GPIO26 active low (base board), P2_3 on LPC4357 + scu_pinmux(0x2, 3, MD_PUP | MD_EZI, FUNC7); // USB0 VBus Power + + // 1.5Kohm pull-up resistor is needed on the USB DP data signal. GPIO28 (base), P9_5 (LPC4357) controls + //scu_pinmux(0x9, 5, MD_PUP|MD_EZI|MD_ZI, FUNC4); // GPIO5[18] + //GPIO_SetDir(5, BIT_(18), 1); // output + //GPIO_ClearValue(5, BIT_(18)); + + /* Init I2C @ 400kHz */ + I2C_Init(LPC_I2C0, 400000); + I2C_Cmd(LPC_I2C0, ENABLE); + +#if CFG_UART_ENABLE + //------------- UART init -------------// + scu_pinmux(BOARD_UART_PIN_PORT, BOARD_UART_PIN_TX, MD_PDN , FUNC1); + scu_pinmux(BOARD_UART_PIN_PORT, BOARD_UART_PIN_RX, MD_PLN|MD_EZI|MD_ZI, FUNC1); + + UART_CFG_Type UARTConfigStruct; + UART_ConfigStructInit(&UARTConfigStruct); + UARTConfigStruct.Baud_rate = CFG_UART_BAUDRATE; + UARTConfigStruct.Clock_Speed = 0; + + UART_Init(BOARD_UART_PORT, &UARTConfigStruct); + UART_TxCmd(BOARD_UART_PORT, ENABLE); // Enable UART Transmit +#endif +} + +//--------------------------------------------------------------------+ +// LEDS +//--------------------------------------------------------------------+ +void board_leds(uint32_t on_mask, uint32_t off_mask) +{ + if (on_mask & 0x01) + { + LPC_GPIO_PORT->SET[BOARD_LED0_GPIO_PORT] = (1 << BOARD_LED0_GPIO_PIN); + } + + if (off_mask & 0x01) + { + LPC_GPIO_PORT->CLR[BOARD_LED0_GPIO_PORT] = (1 << BOARD_LED0_GPIO_PIN); + } +} + +//--------------------------------------------------------------------+ +// UART +//--------------------------------------------------------------------+ +#if CFG_UART_ENABLE +uint32_t board_uart_send(uint8_t *buffer, uint32_t length) +{ + return UART_Send(BOARD_UART_PORT, buffer, length, BLOCKING); +} + +uint32_t board_uart_recv(uint8_t *buffer, uint32_t length) +{ + return UART_Receive(BOARD_UART_PORT, buffer, length, BLOCKING); +} +#endif + +#endif diff --git a/demos/bsp/lpc13uxx/LPC13Uxx_DriverLib/inc/type.h b/demos/bsp/lpc13uxx/LPC13Uxx_DriverLib/inc/type.h index ff9520927..8360217ee 100644 --- a/demos/bsp/lpc13uxx/LPC13Uxx_DriverLib/inc/type.h +++ b/demos/bsp/lpc13uxx/LPC13Uxx_DriverLib/inc/type.h @@ -1,52 +1,52 @@ -/**************************************************************************** - * $Id:: type.h 6172 2011-01-13 18:22:51Z usb00423 $ - * Project: NXP LPC13Uxx software example - * - * Description: - * This file contains different type definition. - * - **************************************************************************** - * Software that is described herein is for illustrative purposes only - * which provides customers with programming information regarding the - * products. This software is supplied "AS IS" without any warranties. - * NXP Semiconductors assumes no responsibility or liability for the - * use of the software, conveys no license or title under any patent, - * copyright, or mask work right to the product. NXP Semiconductors - * reserves the right to make changes in the software without - * notification. NXP Semiconductors also make no representation or - * warranty that such application will be suitable for the specified - * use without further testing or modification. -****************************************************************************/ -#ifndef __TYPE_H__ -#define __TYPE_H__ - -#if defined (__GNUC__) -#include - -#else -/* exact-width signed integer types */ -typedef signed char int8_t; -typedef signed short int int16_t; -typedef signed int int32_t; -typedef signed __int64 int64_t; - - /* exact-width unsigned integer types */ -typedef unsigned char uint8_t; -typedef unsigned short int uint16_t; -typedef unsigned int uint32_t; -typedef unsigned __int64 uint64_t; -#endif - -#ifndef NULL -#define NULL ((void *)0) -#endif - -#ifndef FALSE -#define FALSE (0) -#endif - -#ifndef TRUE -#define TRUE (1) -#endif - -#endif /* __TYPE_H__ */ +/**************************************************************************** + * $Id:: type.h 6172 2011-01-13 18:22:51Z usb00423 $ + * Project: NXP LPC13Uxx software example + * + * Description: + * This file contains different type definition. + * + **************************************************************************** + * Software that is described herein is for illustrative purposes only + * which provides customers with programming information regarding the + * products. This software is supplied "AS IS" without any warranties. + * NXP Semiconductors assumes no responsibility or liability for the + * use of the software, conveys no license or title under any patent, + * copyright, or mask work right to the product. NXP Semiconductors + * reserves the right to make changes in the software without + * notification. NXP Semiconductors also make no representation or + * warranty that such application will be suitable for the specified + * use without further testing or modification. +****************************************************************************/ +#ifndef __TYPE_H__ +#define __TYPE_H__ + +#if defined (__GNUC__) +#include + +#else +/* exact-width signed integer types */ +typedef signed char int8_t; +typedef signed short int int16_t; +typedef signed int int32_t; +//typedef signed __int64 int64_t; + + /* exact-width unsigned integer types */ +typedef unsigned char uint8_t; +typedef unsigned short int uint16_t; +typedef unsigned int uint32_t; +//typedef unsigned __int64 uint64_t; +#endif + +#ifndef NULL +#define NULL ((void *)0) +#endif + +#ifndef FALSE +#define FALSE (0) +#endif + +#ifndef TRUE +#define TRUE (1) +#endif + +#endif /* __TYPE_H__ */ diff --git a/demos/device/device_os_none/device_os_none.ewp b/demos/device/device_os_none/device_os_none.ewp index 57be60bff..fc7810f2b 100644 --- a/demos/device/device_os_none/device_os_none.ewp +++ b/demos/device/device_os_none/device_os_none.ewp @@ -3690,6 +3690,928 @@ + + Board LPCXpresso1347 + + ARM + + 1 + + General + 3 + + 21 + 1 + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ICCARM + 2 + + 28 + 1 + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + AARM + 2 + + 8 + 1 + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + OBJCOPY + 0 + + 1 + 1 + 1 + + + + + + + + + CUSTOM + 3 + + + + + + + BICOMP + 0 + + + + BUILDACTION + 1 + + + + + + + ILINK + 0 + + 15 + 1 + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + IARCHIVE + 0 + + 0 + 1 + 1 + + + + + + + BILINK + 0 + + + app @@ -3730,6 +4652,9 @@ $PROJ_DIR$\..\..\bsp\boards\microbuilder\board_lpc4357usb.c + + $PROJ_DIR$\..\..\bsp\boards\lpcxpresso\board_lpcxpresso1347.c + $PROJ_DIR$\..\..\bsp\boards\lpcxpresso\board_lpcxpresso1769.c @@ -3746,8 +4671,10 @@ lpc11uxx + Board EA4357 Board LPCXpresso1769 - Board rf1ghznode + Board NGX4330 + Board LPCXpresso1347 $PROJ_DIR$\..\..\bsp\lpc11uxx\CMSISv2p00_LPC11Uxx\src\core_cm0.c @@ -3765,11 +4692,34 @@ $PROJ_DIR$\..\..\bsp\lpc11uxx\CMSISv2p00_LPC11Uxx\src\system_LPC11Uxx.c + + lpc13uxx + + Board EA4357 + Board LPCXpresso1769 + Board NGX4330 + Board rf1ghznode + + + $PROJ_DIR$\..\..\bsp\lpc13uxx\LPC13Uxx_DriverLib\src\gpio.c + + + $PROJ_DIR$\..\..\bsp\lpc13uxx\startup_iar\startup_lpc13xx.s + + + $PROJ_DIR$\..\..\bsp\lpc13uxx\CMSISv2p10_LPC13Uxx\src\system_LPC13Uxx.c + + + $PROJ_DIR$\..\..\bsp\lpc13uxx\LPC13Uxx_DriverLib\src\uart.c + + lpc175x_6x Board EA4357 Board NGX4330 + Board rf1ghznode + Board LPCXpresso1347 $PROJ_DIR$\..\..\bsp\lpc175x_6x\CMSISv2p00_LPC17xx\src\core_cm3.c @@ -3798,6 +4748,7 @@ Board LPCXpresso1769 Board rf1ghznode + Board LPCXpresso1347 $PROJ_DIR$\..\..\bsp\lpc43xx\CMSIS_LPC43xx_DriverLib\src\lpc43xx_cgu.c diff --git a/demos/device/device_os_none/device_os_none.uvopt b/demos/device/device_os_none/device_os_none.uvopt index 34031745b..26a8c20b9 100644 --- a/demos/device/device_os_none/device_os_none.uvopt +++ b/demos/device/device_os_none/device_os_none.uvopt @@ -73,7 +73,7 @@ 1 0 - 0 + 1 8 @@ -119,6 +119,13 @@ Segger\JL2CM3.dll + + + 0 + JL2CM3 + -U268003250 -O78 -S8 -A0 -C0 -JU1 -JI127.0.0.1 -JP0 -RST0 -N00("ARM CoreSight SW-DP") -D00(2BA01477) -L00(4) -TO18 -TC10000000 -TP21 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -TB1 -TFE0 -FO15 -FD10000000 -FC800 -FN2 -FF0LPC18xx43xx_512_BA -FS01A000000 -FL080000 -FF1LPC18xx43xx_512_BB -FS11B000000 -FL180000 + + 0 @@ -623,7 +630,7 @@ 1 0 - 1 + 0 8 @@ -920,7 +927,7 @@ 0 24 0 - 60 + 10 11 0 ..\src\main.c @@ -1044,7 +1051,7 @@ tinyusb - 1 + 0 0 0 0 @@ -1436,7 +1443,7 @@ boards - 1 + 0 0 0 0 @@ -1648,7 +1655,7 @@ 0 19 0 - 20 + 21 23 0 ..\..\bsp\lpc11uxx\LPC11Uxx_DriverLib\lpc11uxx_gpio.c @@ -1664,7 +1671,7 @@ 0 19 0 - 11 + 12 14 0 ..\..\bsp\lpc11uxx\LPC11Uxx_DriverLib\lpc11uxx_uart.c @@ -2008,7 +2015,7 @@ 0 30 0 - 146 + 147 150 0 ..\..\bsp\lpc43xx\startup_keil\startup_LPC43xx.s diff --git a/demos/device/src/tusb_config.h b/demos/device/src/tusb_config.h index 70200268d..77cc628a9 100644 --- a/demos/device/src/tusb_config.h +++ b/demos/device/src/tusb_config.h @@ -114,7 +114,7 @@ #define TUSB_CFG_ATTR_USBRAM _Pragma("location=\".ahb_sram1\"") #elif (TUSB_CFG_MCU == MCU_LPC175X_6X) #define TUSB_CFG_ATTR_USBRAM - #elif (TUSB_CFG_MCU == MCU_LPC11UXX) + #elif (TUSB_CFG_MCU == MCU_LPC11UXX) || (TUSB_CFG_MCU == MCU_LPC13UXX) #define TUSB_CFG_ATTR_USBRAM _Pragma("location=\"USB_PACKET_MEMORY\"") #endif diff --git a/demos/device/src/tusb_descriptors.h b/demos/device/src/tusb_descriptors.h index adb5fce92..a2cd62a54 100644 --- a/demos/device/src/tusb_descriptors.h +++ b/demos/device/src/tusb_descriptors.h @@ -1,193 +1,193 @@ -/**************************************************************************/ -/*! - @file tusb_descriptors.h - @author hathach (tinyusb.org) - - @section LICENSE - - Software License Agreement (BSD License) - - Copyright (c) 2013, hathach (tinyusb.org) - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. Neither the name of the copyright holders nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - This file is part of the tinyusb stack. -*/ -/**************************************************************************/ - -#ifndef _TUSB_DESCRIPTORS_H_ -#define _TUSB_DESCRIPTORS_H_ - -#include "tusb.h" - -//--------------------------------------------------------------------+ -// Descriptors Value (calculated by enabled Classes) -//--------------------------------------------------------------------+ -#define CFG_VENDORID 0x1FC9 // NXP -//#define CFG_PRODUCTID 0x4567 // use auto product id to prevent conflict with pc's driver - -// each combination of interfaces need to have a unique productid, as windows will bind & remember device driver after the first plug. -#ifndef CFG_PRODUCTID // Auto ProductID layout's Bitmap: (MSB) MassStorage | Generic | Mouse | Key | CDC (LSB) - #define PRODUCTID_BITMAP(interface, n) ( (TUSB_CFG_DEVICE_##interface) << (n) ) - #define CFG_PRODUCTID (0x4000 | ( PRODUCTID_BITMAP(CDC, 0) | PRODUCTID_BITMAP(HID_KEYBOARD, 1) | \ - PRODUCTID_BITMAP(HID_MOUSE, 2) | PRODUCTID_BITMAP(HID_GENERIC, 3) | \ - PRODUCTID_BITMAP(MSC, 4) ) ) -#endif - - -#define INTERFACE_NUM_CDC 0 -#define INTERFACE_NUM_HID_KEYBOARD (INTERFACE_NUM_CDC + 2*TUSB_CFG_DEVICE_CDC ) -#define INTERFACE_NUM_HID_MOUSE (INTERFACE_NUM_HID_KEYBOARD + TUSB_CFG_DEVICE_HID_KEYBOARD ) -#define INTERFACE_NUM_HID_GENERIC (INTERFACE_NUM_HID_MOUSE + TUSB_CFG_DEVICE_HID_MOUSE ) -#define INTERFACE_NUM_MSC (INTERFACE_NUM_HID_GENERIC + TUSB_CFG_DEVICE_HID_GENERIC ) - -#define TOTAL_INTEFACES (2*TUSB_CFG_DEVICE_CDC + TUSB_CFG_DEVICE_HID_KEYBOARD + TUSB_CFG_DEVICE_HID_MOUSE + \ - TUSB_CFG_DEVICE_HID_GENERIC + TUSB_CFG_DEVICE_MSC) - -// Interface Assosication Descriptor is required when enable CDC -#define IAD_DESC_REQUIRED ( TUSB_CFG_DEVICE_CDC ) - -//--------------------------------------------------------------------+ -// Endpoints Address & Max Packet Size -//--------------------------------------------------------------------+ -#define EDPT_IN(x) (0x80 | (x)) -#define EDPT_OUT(x) (x) - -#if TUSB_CFG_MCU == MCU_LPC175X_6X -//------------- These MCUs's endpoint number has a fixed type -------------// -//------------- CDC -------------// -#define CDC_EDPT_NOTIFICATION_ADDR EDPT_IN (1) -#define CDC_EDPT_NOTIFICATION_PACKETSIZE 64 - -#define CDC_EDPT_DATA_OUT_ADDR EDPT_OUT(2) -#define CDC_EDPT_DATA_IN_ADDR EDPT_IN (2) -#define CDC_EDPT_DATA_PACKETSIZE 64 - -//------------- HID Keyboard -------------// -#define HID_KEYBOARD_EDPT_ADDR EDPT_IN (4) -#define HID_KEYBOARD_EDPT_PACKETSIZE 8 - -//------------- HID Mouse -------------// -#define HID_MOUSE_EDPT_ADDR EDPT_IN (7) -#define HID_MOUSE_EDPT_PACKETSIZE 8 - -//------------- HID Generic -------------// - -//------------- Mass Storage -------------// -#define MSC_EDPT_OUT_ADDR EDPT_OUT(5) -#define MSC_EDPT_IN_ADDR EDPT_IN (5) -#define MSC_EDPT_PACKETSIZE (TUSB_CFG_DEVICE_FULLSPEED ? 64 : 512) - - -#else - -//------------- CDC -------------// -#define CDC_EDPT_NOTIFICATION_ADDR EDPT_IN (INTERFACE_NUM_CDC+1) -#define CDC_EDPT_NOTIFICATION_PACKETSIZE 64 - -#define CDC_EDPT_DATA_OUT_ADDR EDPT_OUT(INTERFACE_NUM_CDC+2) -#define CDC_EDPT_DATA_IN_ADDR EDPT_IN (INTERFACE_NUM_CDC+2) -#define CDC_EDPT_DATA_PACKETSIZE 64 - -//------------- HID Keyboard -------------// -#define HID_KEYBOARD_EDPT_ADDR EDPT_IN (INTERFACE_NUM_HID_KEYBOARD+1) -#define HID_KEYBOARD_EDPT_PACKETSIZE 8 - -//------------- HID Mouse -------------// -#define HID_MOUSE_EDPT_ADDR EDPT_IN (INTERFACE_NUM_HID_MOUSE+1) -#define HID_MOUSE_EDPT_PACKETSIZE 8 - -//------------- HID Generic -------------// - -//------------- Mass Storage -------------// -#define MSC_EDPT_OUT_ADDR EDPT_OUT(INTERFACE_NUM_MSC+1) -#define MSC_EDPT_IN_ADDR EDPT_IN (INTERFACE_NUM_MSC+1) -#define MSC_EDPT_PACKETSIZE (TUSB_CFG_DEVICE_FULLSPEED ? 64 : 512) - -#endif - -//--------------------------------------------------------------------+ -// CONFIGURATION DESCRIPTOR -//--------------------------------------------------------------------+ -typedef ATTR_PACKED_STRUCT(struct) -{ - tusb_descriptor_configuration_t configuration; - - //------------- CDC -------------// -#if TUSB_CFG_DEVICE_CDC - #if IAD_DESC_REQUIRED - tusb_descriptor_interface_association_t cdc_iad; - #endif - - //CDC Control Interface - tusb_descriptor_interface_t cdc_comm_interface; - cdc_desc_func_header_t cdc_header; - cdc_desc_func_abstract_control_management_t cdc_acm; - cdc_desc_func_union_t cdc_union; - tusb_descriptor_endpoint_t cdc_endpoint_notification; - - //CDC Data Interface - tusb_descriptor_interface_t cdc_data_interface; - tusb_descriptor_endpoint_t cdc_endpoint_out; - tusb_descriptor_endpoint_t cdc_endpoint_in; -#endif - - //------------- HID Keyboard -------------// -#if TUSB_CFG_DEVICE_HID_KEYBOARD - tusb_descriptor_interface_t keyboard_interface; - tusb_hid_descriptor_hid_t keyboard_hid; - tusb_descriptor_endpoint_t keyboard_endpoint; -#endif - -//------------- HID Mouse -------------// -#if TUSB_CFG_DEVICE_HID_MOUSE - tusb_descriptor_interface_t mouse_interface; - tusb_hid_descriptor_hid_t mouse_hid; - tusb_descriptor_endpoint_t mouse_endpoint; -#endif - -//------------- Mass Storage -------------// -#if TUSB_CFG_DEVICE_MSC - tusb_descriptor_interface_t msc_interface; - tusb_descriptor_endpoint_t msc_endpoint_in; - tusb_descriptor_endpoint_t msc_endpoint_out; -#endif - -} app_descriptor_configuration_t; - -//--------------------------------------------------------------------+ -// STRINGS DESCRIPTOR -//--------------------------------------------------------------------+ -extern uint8_t * const desc_str_table[TUSB_CFG_DEVICE_STRING_DESCRIPTOR_COUNT]; - -//--------------------------------------------------------------------+ -// Export descriptors -//--------------------------------------------------------------------+ -extern tusb_descriptor_device_t app_tusb_desc_device; -extern app_descriptor_configuration_t app_tusb_desc_configuration; - -extern uint8_t app_tusb_keyboard_desc_report[]; -extern uint8_t app_tusb_mouse_desc_report[]; - -#endif +/**************************************************************************/ +/*! + @file tusb_descriptors.h + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2013, hathach (tinyusb.org) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + This file is part of the tinyusb stack. +*/ +/**************************************************************************/ + +#ifndef _TUSB_DESCRIPTORS_H_ +#define _TUSB_DESCRIPTORS_H_ + +#include "tusb.h" + +//--------------------------------------------------------------------+ +// Descriptors Value (calculated by enabled Classes) +//--------------------------------------------------------------------+ +#define CFG_VENDORID 0x1FC9 // NXP +//#define CFG_PRODUCTID 0x4567 // use auto product id to prevent conflict with pc's driver + +// each combination of interfaces need to have a unique productid, as windows will bind & remember device driver after the first plug. +#ifndef CFG_PRODUCTID // Auto ProductID layout's Bitmap: (MSB) MassStorage | Generic | Mouse | Key | CDC (LSB) + #define PRODUCTID_BITMAP(interface, n) ( (TUSB_CFG_DEVICE_##interface) << (n) ) + #define CFG_PRODUCTID (0x4000 | ( PRODUCTID_BITMAP(CDC, 0) | PRODUCTID_BITMAP(HID_KEYBOARD, 1) | \ + PRODUCTID_BITMAP(HID_MOUSE, 2) | PRODUCTID_BITMAP(HID_GENERIC, 3) | \ + PRODUCTID_BITMAP(MSC, 4) ) ) +#endif + + +#define INTERFACE_NUM_CDC 0 +#define INTERFACE_NUM_HID_KEYBOARD (INTERFACE_NUM_CDC + 2*TUSB_CFG_DEVICE_CDC ) +#define INTERFACE_NUM_HID_MOUSE (INTERFACE_NUM_HID_KEYBOARD + TUSB_CFG_DEVICE_HID_KEYBOARD ) +#define INTERFACE_NUM_HID_GENERIC (INTERFACE_NUM_HID_MOUSE + TUSB_CFG_DEVICE_HID_MOUSE ) +#define INTERFACE_NUM_MSC (INTERFACE_NUM_HID_GENERIC + TUSB_CFG_DEVICE_HID_GENERIC ) + +#define TOTAL_INTEFACES (2*TUSB_CFG_DEVICE_CDC + TUSB_CFG_DEVICE_HID_KEYBOARD + TUSB_CFG_DEVICE_HID_MOUSE + \ + TUSB_CFG_DEVICE_HID_GENERIC + TUSB_CFG_DEVICE_MSC) + +// Interface Assosication Descriptor is required when enable CDC +#define IAD_DESC_REQUIRED ( TUSB_CFG_DEVICE_CDC ) + +//--------------------------------------------------------------------+ +// Endpoints Address & Max Packet Size +//--------------------------------------------------------------------+ +#define EDPT_IN(x) (0x80 | (x)) +#define EDPT_OUT(x) (x) + +#if TUSB_CFG_MCU == MCU_LPC175X_6X +//------------- These MCUs's endpoint number has a fixed type -------------// +//------------- CDC -------------// +#define CDC_EDPT_NOTIFICATION_ADDR EDPT_IN (1) +#define CDC_EDPT_NOTIFICATION_PACKETSIZE 64 + +#define CDC_EDPT_DATA_OUT_ADDR EDPT_OUT(2) +#define CDC_EDPT_DATA_IN_ADDR EDPT_IN (2) +#define CDC_EDPT_DATA_PACKETSIZE 64 + +//------------- HID Keyboard -------------// +#define HID_KEYBOARD_EDPT_ADDR EDPT_IN (4) +#define HID_KEYBOARD_EDPT_PACKETSIZE 8 + +//------------- HID Mouse -------------// +#define HID_MOUSE_EDPT_ADDR EDPT_IN (7) +#define HID_MOUSE_EDPT_PACKETSIZE 8 + +//------------- HID Generic -------------// + +//------------- Mass Storage -------------// +#define MSC_EDPT_OUT_ADDR EDPT_OUT(5) +#define MSC_EDPT_IN_ADDR EDPT_IN (5) +#define MSC_EDPT_PACKETSIZE (TUSB_CFG_DEVICE_FULLSPEED ? 64 : 512) + + +#else + +//------------- CDC -------------// +#define CDC_EDPT_NOTIFICATION_ADDR EDPT_IN (INTERFACE_NUM_CDC+1) +#define CDC_EDPT_NOTIFICATION_PACKETSIZE 64 + +#define CDC_EDPT_DATA_OUT_ADDR EDPT_OUT(INTERFACE_NUM_CDC+2) +#define CDC_EDPT_DATA_IN_ADDR EDPT_IN (INTERFACE_NUM_CDC+2) +#define CDC_EDPT_DATA_PACKETSIZE 64 + +//------------- HID Keyboard -------------// +#define HID_KEYBOARD_EDPT_ADDR EDPT_IN (INTERFACE_NUM_HID_KEYBOARD+1) +#define HID_KEYBOARD_EDPT_PACKETSIZE 8 + +//------------- HID Mouse -------------// +#define HID_MOUSE_EDPT_ADDR EDPT_IN (INTERFACE_NUM_HID_MOUSE+1) +#define HID_MOUSE_EDPT_PACKETSIZE 8 + +//------------- HID Generic -------------// + +//------------- Mass Storage -------------// +#define MSC_EDPT_OUT_ADDR EDPT_OUT(INTERFACE_NUM_MSC+1) +#define MSC_EDPT_IN_ADDR EDPT_IN (INTERFACE_NUM_MSC+1) +#define MSC_EDPT_PACKETSIZE (TUSB_CFG_DEVICE_FULLSPEED ? 64 : 512) + +#endif + +//--------------------------------------------------------------------+ +// CONFIGURATION DESCRIPTOR +//--------------------------------------------------------------------+ +typedef ATTR_PACKED_STRUCT(struct) +{ + tusb_descriptor_configuration_t configuration; + + //------------- CDC -------------// +#if TUSB_CFG_DEVICE_CDC + #if IAD_DESC_REQUIRED + tusb_descriptor_interface_association_t cdc_iad; + #endif + + //CDC Control Interface + tusb_descriptor_interface_t cdc_comm_interface; + cdc_desc_func_header_t cdc_header; + cdc_desc_func_abstract_control_management_t cdc_acm; + cdc_desc_func_union_t cdc_union; + tusb_descriptor_endpoint_t cdc_endpoint_notification; + + //CDC Data Interface + tusb_descriptor_interface_t cdc_data_interface; + tusb_descriptor_endpoint_t cdc_endpoint_out; + tusb_descriptor_endpoint_t cdc_endpoint_in; +#endif + + //------------- HID Keyboard -------------// +#if TUSB_CFG_DEVICE_HID_KEYBOARD + tusb_descriptor_interface_t keyboard_interface; + tusb_hid_descriptor_hid_t keyboard_hid; + tusb_descriptor_endpoint_t keyboard_endpoint; +#endif + +//------------- HID Mouse -------------// +#if TUSB_CFG_DEVICE_HID_MOUSE + tusb_descriptor_interface_t mouse_interface; + tusb_hid_descriptor_hid_t mouse_hid; + tusb_descriptor_endpoint_t mouse_endpoint; +#endif + +//------------- Mass Storage -------------// +#if TUSB_CFG_DEVICE_MSC + tusb_descriptor_interface_t msc_interface; + tusb_descriptor_endpoint_t msc_endpoint_in; + tusb_descriptor_endpoint_t msc_endpoint_out; +#endif + +} app_descriptor_configuration_t; + +//--------------------------------------------------------------------+ +// STRINGS DESCRIPTOR +//--------------------------------------------------------------------+ +extern uint8_t * const desc_str_table[TUSB_CFG_DEVICE_STRING_DESCRIPTOR_COUNT]; + +//--------------------------------------------------------------------+ +// Export descriptors +//--------------------------------------------------------------------+ +extern tusb_descriptor_device_t app_tusb_desc_device; +extern app_descriptor_configuration_t app_tusb_desc_configuration; + +extern uint8_t app_tusb_keyboard_desc_report[]; +extern uint8_t app_tusb_mouse_desc_report[]; + +#endif diff --git a/tinyusb/common/assertion.h b/tinyusb/common/assertion.h index 91d10632b..2a847731a 100644 --- a/tinyusb/common/assertion.h +++ b/tinyusb/common/assertion.h @@ -1,203 +1,203 @@ -/**************************************************************************/ -/*! - @file assertion.h - @author hathach (tinyusb.org) - - @section LICENSE - - Software License Agreement (BSD License) - - Copyright (c) 2013, hathach (tinyusb.org) - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. Neither the name of the copyright holders nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - This file is part of the tinyusb stack. -*/ -/**************************************************************************/ - -/** \ingroup TBD - * \defgroup TBD - * \brief TBD - * - * @{ - */ - -#ifndef _TUSB_ASSERTION_H_ -#define _TUSB_ASSERTION_H_ - -#ifdef __cplusplus -extern "C" -{ -#endif - -#include "tusb_option.h" -#include "hal/hal.h" // TODO find a way to break hal dependency - -#define VOID_RETURN - -//--------------------------------------------------------------------+ -// Compile-time Assert -//--------------------------------------------------------------------+ -#ifdef __ICCARM__ - #define STATIC_ASSERT static_assert -#else - #if defined __COUNTER__ && __COUNTER__ != __COUNTER__ - #define _ASSERT_COUNTER __COUNTER__ - #else - #define _ASSERT_COUNTER __LINE__ - #endif - - #define STATIC_ASSERT(const_expr, message) enum { XSTRING_CONCAT_(static_assert_, _ASSERT_COUNTER) = 1/(!!(const_expr)) } -#endif - - //#if ( defined CFG_PRINTF_UART || defined CFG_PRINTF_USBCDC || defined CFG_PRINTF_DEBUG ) -#if TUSB_CFG_DEBUG == 3 - #define _PRINTF(...) printf(__VA_ARGS__) // PRINTF -#else - #define _PRINTF(...) -#endif - -//--------------------------------------------------------------------+ -// Assert Helper -//--------------------------------------------------------------------+ -#ifndef _TEST_ - #define ASSERT_MESSAGE(format, ...)\ - _PRINTF("Assert at %s: %s: %d: " format "\n", __BASE_FILE__, __PRETTY_FUNCTION__, __LINE__, __VA_ARGS__) -#else - #define ASSERT_MESSAGE(format, ...)\ - _PRINTF("%d:note: Assert " format "\n", __LINE__, __VA_ARGS__) -#endif - -#ifndef _TEST_ASSERT_ - #define ASSERT_ERROR_HANDLER(x, para) \ - return x -#else - #define ASSERT_ERROR_HANDLER(x, para) Throw(x) -#endif - -#define ASSERT_DEFINE_WITH_HANDLER(error_handler, handler_para, setup_statement, condition, error, format, ...) \ - do{\ - setup_statement;\ - if (!(condition)) {\ - if (hal_debugger_is_attached()){\ - hal_debugger_breakpoint();\ - }else{\ - ASSERT_MESSAGE(format, __VA_ARGS__);\ - }\ - error_handler(error, handler_para);\ - }\ - }while(0) - -#define ASSERT_DEFINE(...) ASSERT_DEFINE_WITH_HANDLER(ASSERT_ERROR_HANDLER, NULL, __VA_ARGS__) - -//--------------------------------------------------------------------+ -// tusb_error_t Status Assert TODO use ASSERT_DEFINE -//--------------------------------------------------------------------+ -#define ASSERT_STATUS_MESSAGE(sts, message) \ - ASSERT_DEFINE(tusb_error_t status = (tusb_error_t)(sts),\ - TUSB_ERROR_NONE == status, status, "%s: %s", TUSB_ErrorStr[status], message) - -#define ASSERT_STATUS(sts) \ - ASSERT_DEFINE(tusb_error_t status = (tusb_error_t)(sts),\ - TUSB_ERROR_NONE == status, status, "%s", TUSB_ErrorStr[status]) - -//--------------------------------------------------------------------+ -// Logical Assert -//--------------------------------------------------------------------+ -#define ASSERT(...) ASSERT_TRUE(__VA_ARGS__) -#define ASSERT_TRUE(condition , error) ASSERT_DEFINE( , (condition), error, "%s", "evaluated to false") -#define ASSERT_FALSE(condition , error) ASSERT_DEFINE( ,!(condition), error, "%s", "evaluated to true") - -//--------------------------------------------------------------------+ -// Pointer Assert -//--------------------------------------------------------------------+ -#define ASSERT_PTR(...) ASSERT_PTR_NOT_NULL(__VA_ARGS__) -#define ASSERT_PTR_NOT_NULL(pointer, error) ASSERT_DEFINE( , NULL != (pointer), error, "%s", "pointer is NULL") -#define ASSERT_PTR_NULL(pointer, error) ASSERT_DEFINE( , NULL == (pointer), error, "%s", "pointer is not NULL") - -//--------------------------------------------------------------------+ -// Integral Assert -//--------------------------------------------------------------------+ -#define ASSERT_XXX_EQUAL(type_format, expected, actual, error) \ - ASSERT_DEFINE(\ - uint32_t exp = (expected); uint32_t act = (actual),\ - exp==act,\ - error,\ - "expected " type_format ", actual " type_format, exp, act) - -#define ASSERT_XXX_WITHIN(type_format, lower, upper, actual, error) \ - ASSERT_DEFINE(\ - uint32_t low = (lower); uint32_t up = (upper); uint32_t act = (actual),\ - (low <= act) && (act <= up),\ - error,\ - "expected within " type_format " - " type_format ", actual " type_format, low, up, act) - -//--------------------------------------------------------------------+ -// Integer Assert -//--------------------------------------------------------------------+ -#define ASSERT_INT(...) ASSERT_INT_EQUAL(__VA_ARGS__) -#define ASSERT_INT_EQUAL(...) ASSERT_XXX_EQUAL("%d", __VA_ARGS__) -#define ASSERT_INT_WITHIN(...) ASSERT_XXX_WITHIN("%d", __VA_ARGS__) - -//--------------------------------------------------------------------+ -// Hex Assert -//--------------------------------------------------------------------+ -#define ASSERT_HEX(...) ASSERT_HEX_EQUAL(__VA_ARGS__) -#define ASSERT_HEX_EQUAL(...) ASSERT_XXX_EQUAL("0x%x", __VA_ARGS__) -#define ASSERT_HEX_WITHIN(...) ASSERT_XXX_WITHIN("0x%x", __VA_ARGS__) - -//--------------------------------------------------------------------+ -// TODO Bin Assert -//--------------------------------------------------------------------+ -#define BIN8_PRINTF_PATTERN "%d%d%d%d%d%d%d%d" -#define BIN8_PRINTF_CONVERT(byte) \ - ((byte) & 0x80 ? 1 : 0), \ - ((byte) & 0x40 ? 1 : 0), \ - ((byte) & 0x20 ? 1 : 0), \ - ((byte) & 0x10 ? 1 : 0), \ - ((byte) & 0x08 ? 1 : 0), \ - ((byte) & 0x04 ? 1 : 0), \ - ((byte) & 0x02 ? 1 : 0), \ - ((byte) & 0x01 ? 1 : 0) - -#define ASSERT_BIN8(...) ASSERT_BIN8_EQUAL(__VA_ARGS__) -#define ASSERT_BIN8_EQUAL(expected, actual, error)\ - ASSERT_DEFINE(\ - uint8_t exp = (expected); uint8_t act = (actual),\ - exp==act,\ - error,\ - "expected " BIN8_PRINTF_PATTERN ", actual " BIN8_PRINTF_PATTERN, BIN8_PRINTF_CONVERT(exp), BIN8_PRINTF_CONVERT(act) ) - -//--------------------------------------------------------------------+ -// TODO Bit Assert -//--------------------------------------------------------------------+ - - -#ifdef __cplusplus -} -#endif - -#endif /* _TUSB_ASSERTION_H_ */ - -/** @} */ +/**************************************************************************/ +/*! + @file assertion.h + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2013, hathach (tinyusb.org) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + This file is part of the tinyusb stack. +*/ +/**************************************************************************/ + +/** \ingroup TBD + * \defgroup TBD + * \brief TBD + * + * @{ + */ + +#ifndef _TUSB_ASSERTION_H_ +#define _TUSB_ASSERTION_H_ + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include "tusb_option.h" +#include "hal/hal.h" // TODO find a way to break hal dependency + +#define VOID_RETURN + +//--------------------------------------------------------------------+ +// Compile-time Assert +//--------------------------------------------------------------------+ +#ifdef __ICCARM__ + #define STATIC_ASSERT static_assert +#else + #if defined __COUNTER__ && __COUNTER__ != __COUNTER__ + #define _ASSERT_COUNTER __COUNTER__ + #else + #define _ASSERT_COUNTER __LINE__ + #endif + + #define STATIC_ASSERT(const_expr, message) enum { XSTRING_CONCAT_(static_assert_, _ASSERT_COUNTER) = 1/(!!(const_expr)) } +#endif + + //#if ( defined CFG_PRINTF_UART || defined CFG_PRINTF_USBCDC || defined CFG_PRINTF_DEBUG ) +#if TUSB_CFG_DEBUG == 3 + #define _PRINTF(...) printf(__VA_ARGS__) // PRINTF +#else + #define _PRINTF(...) +#endif + +//--------------------------------------------------------------------+ +// Assert Helper +//--------------------------------------------------------------------+ +#ifndef _TEST_ + #define ASSERT_MESSAGE(format, ...)\ + _PRINTF("Assert at %s: %s: %d: " format "\n", __BASE_FILE__, __PRETTY_FUNCTION__, __LINE__, __VA_ARGS__) +#else + #define ASSERT_MESSAGE(format, ...)\ + _PRINTF("%d:note: Assert " format "\n", __LINE__, __VA_ARGS__) +#endif + +#ifndef _TEST_ASSERT_ + #define ASSERT_ERROR_HANDLER(x, para) \ + return x +#else + #define ASSERT_ERROR_HANDLER(x, para) Throw(x) +#endif + +#define ASSERT_DEFINE_WITH_HANDLER(error_handler, handler_para, setup_statement, condition, error, format, ...) \ + do{\ + setup_statement;\ + if (!(condition)) {\ + if (hal_debugger_is_attached()){\ + hal_debugger_breakpoint();\ + }else{\ + ASSERT_MESSAGE(format, __VA_ARGS__);\ + }\ + error_handler(error, handler_para);\ + }\ + }while(0) + +#define ASSERT_DEFINE(...) ASSERT_DEFINE_WITH_HANDLER(ASSERT_ERROR_HANDLER, NULL, __VA_ARGS__) + +//--------------------------------------------------------------------+ +// tusb_error_t Status Assert TODO use ASSERT_DEFINE +//--------------------------------------------------------------------+ +#define ASSERT_STATUS_MESSAGE(sts, message) \ + ASSERT_DEFINE(tusb_error_t status = (tusb_error_t)(sts),\ + TUSB_ERROR_NONE == status, status, "%s: %s", TUSB_ErrorStr[status], message) + +#define ASSERT_STATUS(sts) \ + ASSERT_DEFINE(tusb_error_t status = (tusb_error_t)(sts),\ + TUSB_ERROR_NONE == status, status, "%s", TUSB_ErrorStr[status]) + +//--------------------------------------------------------------------+ +// Logical Assert +//--------------------------------------------------------------------+ +#define ASSERT(...) ASSERT_TRUE(__VA_ARGS__) +#define ASSERT_TRUE(condition , error) ASSERT_DEFINE( , (condition), error, "%s", "evaluated to false") +#define ASSERT_FALSE(condition , error) ASSERT_DEFINE( ,!(condition), error, "%s", "evaluated to true") + +//--------------------------------------------------------------------+ +// Pointer Assert +//--------------------------------------------------------------------+ +#define ASSERT_PTR(...) ASSERT_PTR_NOT_NULL(__VA_ARGS__) +#define ASSERT_PTR_NOT_NULL(pointer, error) ASSERT_DEFINE( , NULL != (pointer), error, "%s", "pointer is NULL") +#define ASSERT_PTR_NULL(pointer, error) ASSERT_DEFINE( , NULL == (pointer), error, "%s", "pointer is not NULL") + +//--------------------------------------------------------------------+ +// Integral Assert +//--------------------------------------------------------------------+ +#define ASSERT_XXX_EQUAL(type_format, expected, actual, error) \ + ASSERT_DEFINE(\ + uint32_t exp = (expected); uint32_t act = (actual),\ + exp==act,\ + error,\ + "expected " type_format ", actual " type_format, exp, act) + +#define ASSERT_XXX_WITHIN(type_format, lower, upper, actual, error) \ + ASSERT_DEFINE(\ + uint32_t low = (lower); uint32_t up = (upper); uint32_t act = (actual),\ + (low <= act) && (act <= up),\ + error,\ + "expected within " type_format " - " type_format ", actual " type_format, low, up, act) + +//--------------------------------------------------------------------+ +// Integer Assert +//--------------------------------------------------------------------+ +#define ASSERT_INT(...) ASSERT_INT_EQUAL(__VA_ARGS__) +#define ASSERT_INT_EQUAL(...) ASSERT_XXX_EQUAL("%d", __VA_ARGS__) +#define ASSERT_INT_WITHIN(...) ASSERT_XXX_WITHIN("%d", __VA_ARGS__) + +//--------------------------------------------------------------------+ +// Hex Assert +//--------------------------------------------------------------------+ +#define ASSERT_HEX(...) ASSERT_HEX_EQUAL(__VA_ARGS__) +#define ASSERT_HEX_EQUAL(...) ASSERT_XXX_EQUAL("0x%x", __VA_ARGS__) +#define ASSERT_HEX_WITHIN(...) ASSERT_XXX_WITHIN("0x%x", __VA_ARGS__) + +//--------------------------------------------------------------------+ +// TODO Bin Assert +//--------------------------------------------------------------------+ +#define BIN8_PRINTF_PATTERN "%d%d%d%d%d%d%d%d" +#define BIN8_PRINTF_CONVERT(byte) \ + ((byte) & 0x80 ? 1 : 0), \ + ((byte) & 0x40 ? 1 : 0), \ + ((byte) & 0x20 ? 1 : 0), \ + ((byte) & 0x10 ? 1 : 0), \ + ((byte) & 0x08 ? 1 : 0), \ + ((byte) & 0x04 ? 1 : 0), \ + ((byte) & 0x02 ? 1 : 0), \ + ((byte) & 0x01 ? 1 : 0) + +#define ASSERT_BIN8(...) ASSERT_BIN8_EQUAL(__VA_ARGS__) +#define ASSERT_BIN8_EQUAL(expected, actual, error)\ + ASSERT_DEFINE(\ + uint8_t exp = (expected); uint8_t act = (actual),\ + exp==act,\ + error,\ + "expected " BIN8_PRINTF_PATTERN ", actual " BIN8_PRINTF_PATTERN, BIN8_PRINTF_CONVERT(exp), BIN8_PRINTF_CONVERT(act) ) + +//--------------------------------------------------------------------+ +// TODO Bit Assert +//--------------------------------------------------------------------+ + + +#ifdef __cplusplus +} +#endif + +#endif /* _TUSB_ASSERTION_H_ */ + +/** @} */ diff --git a/tinyusb/common/compiler/compiler.h b/tinyusb/common/compiler/compiler.h index 4c81047c7..98aa924ad 100644 --- a/tinyusb/common/compiler/compiler.h +++ b/tinyusb/common/compiler/compiler.h @@ -1,76 +1,76 @@ -/**************************************************************************/ -/*! - @file compiler.h - @author hathach (tinyusb.org) - - @section LICENSE - - Software License Agreement (BSD License) - - Copyright (c) 2013, hathach (tinyusb.org) - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. Neither the name of the copyright holders nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - This file is part of the tinyusb stack. -*/ -/**************************************************************************/ - -/** \ingroup Group_Common - * \defgroup Group_Compiler Compiler - * \brief Group_Compiler brief - * - * @{ - */ - -#ifndef _TUSB_COMPILER_H_ -#define _TUSB_COMPILER_H_ - -#ifndef _TEST_ - #define STATIC_ static - #define INLINE_ inline - #define ATTR_TEST_WEAK - - #if TUSB_CFG_DEBUG == 3 - #define ATTR_ALWAYS_INLINE // no inline for debug = 3 - #define STATIC_VAR - #else - #define STATIC_VAR static - #endif - -#else - #define ATTR_ALWAYS_INLINE - #define STATIC_ - #define STATIC_VAR - #define INLINE_ - -#endif - -#if defined(__GNUC__) - #include "compiler_gcc.h" -#elif defined __ICCARM__ // IAR compiler - #include "compiler_iar.h" -#endif - -#endif /* _TUSB_COMPILER_H_ */ -/// @} +/**************************************************************************/ +/*! + @file compiler.h + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2013, hathach (tinyusb.org) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + This file is part of the tinyusb stack. +*/ +/**************************************************************************/ + +/** \ingroup Group_Common + * \defgroup Group_Compiler Compiler + * \brief Group_Compiler brief + * + * @{ + */ + +#ifndef _TUSB_COMPILER_H_ +#define _TUSB_COMPILER_H_ + +#ifndef _TEST_ + #define STATIC_ static + #define INLINE_ inline + #define ATTR_TEST_WEAK + + #if TUSB_CFG_DEBUG == 3 + #define ATTR_ALWAYS_INLINE // no inline for debug = 3 + #define STATIC_VAR + #else + #define STATIC_VAR static + #endif + +#else + #define ATTR_ALWAYS_INLINE + #define STATIC_ + #define STATIC_VAR + #define INLINE_ + +#endif + +#if defined(__GNUC__) + #include "compiler_gcc.h" +#elif defined __ICCARM__ // IAR compiler + #include "compiler_iar.h" +#endif + +#endif /* _TUSB_COMPILER_H_ */ +/// @} diff --git a/tinyusb/common/compiler/compiler_iar.h b/tinyusb/common/compiler/compiler_iar.h index 7103ee092..1703ea45f 100644 --- a/tinyusb/common/compiler/compiler_iar.h +++ b/tinyusb/common/compiler/compiler_iar.h @@ -1,91 +1,91 @@ -/**************************************************************************/ -/*! - @file compiler_iar.h - @author hathach (tinyusb.org) - - @section LICENSE - - Software License Agreement (BSD License) - - Copyright (c) 2013, hathach (tinyusb.org) - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. Neither the name of the copyright holders nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - This file is part of the tinyusb stack. -*/ -/**************************************************************************/ - -/** \file - * \brief IAR Compiler - */ - -/** \ingroup Group_Compiler - * \defgroup Group_IAR IAR ARM - * @{ - */ - -#ifndef _TUSB_COMPILER_IAR_H_ -#define _TUSB_COMPILER_IAR_H_ - -#ifdef __cplusplus - extern "C" { -#endif - -#define ALIGN_OF(x) __ALIGNOF__(x) - -#define ATTR_PACKED_STRUCT(x) __packed x -#define ATTR_PREPACKED __packed -#define ATTR_PACKED -//#define ATTR_SECTION(section) _Pragma((#section)) - -#define ATTR_ALIGNED(bytes) _Pragma(XSTRING_(data_alignment=##bytes)) - -#ifndef ATTR_ALWAYS_INLINE -/// Generally, functions are not inlined unless optimization is specified. For functions declared inline, this attribute inlines the function even if no optimization level is specified -#define ATTR_ALWAYS_INLINE error -#endif - -#define ATTR_PURE // TODO IAR pure function attribute -#define ATTR_CONST // TODO IAR const function attribute -#define ATTR_WEAK __weak - -#define ATTR_WARN_UNUSED_RESULT -#define ATTR_USED -#define ATTR_UNUSED - -// built-in function to convert 32-bit Big-Endian to Little-Endian -//#if __LITTLE_ENDIAN__ -#define __be2n __REV -#define __n2be __be2n - -#define __n2be_16(u16) ((uint16_t) __REV16(u16)) -#define __be2n_16(u16) __n2be_16(u16) - -#ifdef __cplusplus - } -#endif - -#endif /* _TUSB_COMPILER_IAR_H_ */ - -/** @} */ +/**************************************************************************/ +/*! + @file compiler_iar.h + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2013, hathach (tinyusb.org) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + This file is part of the tinyusb stack. +*/ +/**************************************************************************/ + +/** \file + * \brief IAR Compiler + */ + +/** \ingroup Group_Compiler + * \defgroup Group_IAR IAR ARM + * @{ + */ + +#ifndef _TUSB_COMPILER_IAR_H_ +#define _TUSB_COMPILER_IAR_H_ + +#ifdef __cplusplus + extern "C" { +#endif + +#define ALIGN_OF(x) __ALIGNOF__(x) + +#define ATTR_PACKED_STRUCT(x) __packed x +#define ATTR_PREPACKED __packed +#define ATTR_PACKED +//#define ATTR_SECTION(section) _Pragma((#section)) + +#define ATTR_ALIGNED(bytes) _Pragma(XSTRING_(data_alignment=##bytes)) + +#ifndef ATTR_ALWAYS_INLINE +/// Generally, functions are not inlined unless optimization is specified. For functions declared inline, this attribute inlines the function even if no optimization level is specified +#define ATTR_ALWAYS_INLINE error +#endif + +#define ATTR_PURE // TODO IAR pure function attribute +#define ATTR_CONST // TODO IAR const function attribute +#define ATTR_WEAK __weak + +#define ATTR_WARN_UNUSED_RESULT +#define ATTR_USED +#define ATTR_UNUSED + +// built-in function to convert 32-bit Big-Endian to Little-Endian +//#if __LITTLE_ENDIAN__ +#define __be2n __REV +#define __n2be __be2n + +#define __n2be_16(u16) ((uint16_t) __REV16(u16)) +#define __be2n_16(u16) __n2be_16(u16) + +#ifdef __cplusplus + } +#endif + +#endif /* _TUSB_COMPILER_IAR_H_ */ + +/** @} */ diff --git a/tinyusb/common/errors.h b/tinyusb/common/errors.h index 9b47fc332..c43143708 100644 --- a/tinyusb/common/errors.h +++ b/tinyusb/common/errors.h @@ -1,121 +1,121 @@ -/**************************************************************************/ -/*! - @file errors.h - @author hathach (tinyusb.org) - - @section LICENSE - - Software License Agreement (BSD License) - - Copyright (c) 2013, hathach (tinyusb.org) - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. Neither the name of the copyright holders nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - This file is part of the tinyusb stack. -*/ -/**************************************************************************/ - -/** \file - * \brief Error Header - * - * \note TBD - */ - -/** \ingroup Group_Common - * \defgroup Group_Error Error Codes - * @{ - */ - -#ifndef _TUSB_ERRORS_H_ -#define _TUSB_ERRORS_H_ - -#include "tusb_option.h" - -#ifdef __cplusplus - extern "C" { -#endif - -#define ERROR_ENUM(x) x, -#define ERROR_STRING(x) #x, - -#define ERROR_TABLE(ENTRY) \ - ENTRY(TUSB_ERROR_NONE )\ - ENTRY(TUSB_ERROR_INVALID_PARA )\ - ENTRY(TUSB_ERROR_DEVICE_NOT_READY )\ - ENTRY(TUSB_ERROR_INTERFACE_IS_BUSY )\ - ENTRY(TUSB_ERROR_HCD_FAILED )\ - ENTRY(TUSB_ERROR_HCD_OPEN_PIPE_FAILED )\ - ENTRY(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND )\ - ENTRY(TUSB_ERROR_USBH_MOUNT_CONFIG_DESC_TOO_LONG )\ - ENTRY(TUSB_ERROR_USBH_DESCRIPTOR_CORRUPTED )\ - ENTRY(TUSB_ERROR_USBH_XFER_STALLED )\ - ENTRY(TUSB_ERROR_USBH_XFER_FAILED )\ - ENTRY(TUSB_ERROR_OSAL_TIMEOUT )\ - ENTRY(TUSB_ERROR_OSAL_WAITING ) /* only used by OSAL_NONE in the subtask */ \ - ENTRY(TUSB_ERROR_OSAL_TASK_FAILED )\ - ENTRY(TUSB_ERROR_OSAL_TASK_CREATE_FAILED )\ - ENTRY(TUSB_ERROR_OSAL_QUEUE_FAILED )\ - ENTRY(TUSB_ERROR_OSAL_SEMAPHORE_FAILED )\ - ENTRY(TUSB_ERROR_OSAL_MUTEX_FAILED )\ - ENTRY(TUSB_ERROR_EHCI_NOT_ENOUGH_QTD )\ - ENTRY(TUSB_ERROR_HIDD_DESCRIPTOR_INTERFACE )\ - ENTRY(TUSB_ERROR_HIDH_NOT_SUPPORTED_PROTOCOL )\ - ENTRY(TUSB_ERROR_HIDH_NOT_SUPPORTED_SUBCLASS )\ - ENTRY(TUSB_ERROR_CDC_UNSUPPORTED_SUBCLASS )\ - ENTRY(TUSB_ERROR_CDC_UNSUPPORTED_PROTOCOL )\ - ENTRY(TUSB_ERROR_CDCH_DEVICE_NOT_MOUNTED )\ - ENTRY(TUSB_ERROR_MSC_UNSUPPORTED_PROTOCOL )\ - ENTRY(TUSB_ERROR_MSCH_UNKNOWN_SCSI_COMMAND )\ - ENTRY(TUSB_ERROR_MSCH_DEVICE_NOT_MOUNTED )\ - ENTRY(TUSB_ERROR_HUB_FEATURE_NOT_SUPPORTED )\ - ENTRY(TUSB_ERROR_DESCRIPTOR_CORRUPTED )\ - ENTRY(TUSB_ERROR_DCD_FAILED )\ - ENTRY(TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT )\ - ENTRY(TUSB_ERROR_DCD_NOT_ENOUGH_QTD )\ - ENTRY(TUSB_ERROR_DCD_OPEN_PIPE_FAILED )\ - ENTRY(TUSB_ERROR_NOT_SUPPORTED_YET )\ - ENTRY(TUSB_ERROR_USBD_DEVICE_NOT_CONFIGURED )\ - ENTRY(TUSB_ERROR_FAILED )\ - - -/** \enum tusb_error_t - * \brief Error Code returned - */ -typedef enum { - ERROR_TABLE(ERROR_ENUM) - TUSB_ERROR_COUNT -}tusb_error_t; - -#if TUSB_CFG_DEBUG == 3 -/// Enum to String for debugging purposes. Only available if \ref TUSB_CFG_DEBUG > 0 -extern char const* const TUSB_ErrorStr[TUSB_ERROR_COUNT]; -#endif - -#ifdef __cplusplus - } -#endif - -#endif /* _TUSB_ERRORS_H_ */ - - /** @} */ +/**************************************************************************/ +/*! + @file errors.h + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2013, hathach (tinyusb.org) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + This file is part of the tinyusb stack. +*/ +/**************************************************************************/ + +/** \file + * \brief Error Header + * + * \note TBD + */ + +/** \ingroup Group_Common + * \defgroup Group_Error Error Codes + * @{ + */ + +#ifndef _TUSB_ERRORS_H_ +#define _TUSB_ERRORS_H_ + +#include "tusb_option.h" + +#ifdef __cplusplus + extern "C" { +#endif + +#define ERROR_ENUM(x) x, +#define ERROR_STRING(x) #x, + +#define ERROR_TABLE(ENTRY) \ + ENTRY(TUSB_ERROR_NONE )\ + ENTRY(TUSB_ERROR_INVALID_PARA )\ + ENTRY(TUSB_ERROR_DEVICE_NOT_READY )\ + ENTRY(TUSB_ERROR_INTERFACE_IS_BUSY )\ + ENTRY(TUSB_ERROR_HCD_FAILED )\ + ENTRY(TUSB_ERROR_HCD_OPEN_PIPE_FAILED )\ + ENTRY(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND )\ + ENTRY(TUSB_ERROR_USBH_MOUNT_CONFIG_DESC_TOO_LONG )\ + ENTRY(TUSB_ERROR_USBH_DESCRIPTOR_CORRUPTED )\ + ENTRY(TUSB_ERROR_USBH_XFER_STALLED )\ + ENTRY(TUSB_ERROR_USBH_XFER_FAILED )\ + ENTRY(TUSB_ERROR_OSAL_TIMEOUT )\ + ENTRY(TUSB_ERROR_OSAL_WAITING ) /* only used by OSAL_NONE in the subtask */ \ + ENTRY(TUSB_ERROR_OSAL_TASK_FAILED )\ + ENTRY(TUSB_ERROR_OSAL_TASK_CREATE_FAILED )\ + ENTRY(TUSB_ERROR_OSAL_QUEUE_FAILED )\ + ENTRY(TUSB_ERROR_OSAL_SEMAPHORE_FAILED )\ + ENTRY(TUSB_ERROR_OSAL_MUTEX_FAILED )\ + ENTRY(TUSB_ERROR_EHCI_NOT_ENOUGH_QTD )\ + ENTRY(TUSB_ERROR_HIDD_DESCRIPTOR_INTERFACE )\ + ENTRY(TUSB_ERROR_HIDH_NOT_SUPPORTED_PROTOCOL )\ + ENTRY(TUSB_ERROR_HIDH_NOT_SUPPORTED_SUBCLASS )\ + ENTRY(TUSB_ERROR_CDC_UNSUPPORTED_SUBCLASS )\ + ENTRY(TUSB_ERROR_CDC_UNSUPPORTED_PROTOCOL )\ + ENTRY(TUSB_ERROR_CDCH_DEVICE_NOT_MOUNTED )\ + ENTRY(TUSB_ERROR_MSC_UNSUPPORTED_PROTOCOL )\ + ENTRY(TUSB_ERROR_MSCH_UNKNOWN_SCSI_COMMAND )\ + ENTRY(TUSB_ERROR_MSCH_DEVICE_NOT_MOUNTED )\ + ENTRY(TUSB_ERROR_HUB_FEATURE_NOT_SUPPORTED )\ + ENTRY(TUSB_ERROR_DESCRIPTOR_CORRUPTED )\ + ENTRY(TUSB_ERROR_DCD_FAILED )\ + ENTRY(TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT )\ + ENTRY(TUSB_ERROR_DCD_NOT_ENOUGH_QTD )\ + ENTRY(TUSB_ERROR_DCD_OPEN_PIPE_FAILED )\ + ENTRY(TUSB_ERROR_NOT_SUPPORTED_YET )\ + ENTRY(TUSB_ERROR_USBD_DEVICE_NOT_CONFIGURED )\ + ENTRY(TUSB_ERROR_FAILED )\ + + +/** \enum tusb_error_t + * \brief Error Code returned + */ +typedef enum { + ERROR_TABLE(ERROR_ENUM) + TUSB_ERROR_COUNT +}tusb_error_t; + +#if TUSB_CFG_DEBUG == 3 +/// Enum to String for debugging purposes. Only available if \ref TUSB_CFG_DEBUG > 0 +extern char const* const TUSB_ErrorStr[TUSB_ERROR_COUNT]; +#endif + +#ifdef __cplusplus + } +#endif + +#endif /* _TUSB_ERRORS_H_ */ + + /** @} */ diff --git a/tinyusb/device/dcd.c b/tinyusb/device/dcd.c index 9ff5803b0..441c8970e 100644 --- a/tinyusb/device/dcd.c +++ b/tinyusb/device/dcd.c @@ -1,44 +1,44 @@ -/**************************************************************************/ -/*! - @file dcd.c - @author hathach (tinyusb.org) - - @section LICENSE - - Software License Agreement (BSD License) - - Copyright (c) 2013, hathach (tinyusb.org) - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. Neither the name of the copyright holders nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - This file is part of the tinyusb stack. -*/ -/**************************************************************************/ - -#include "dcd.h" - -#if MODE_DEVICE_SUPPORTED - - -#endif +/**************************************************************************/ +/*! + @file dcd.c + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2013, hathach (tinyusb.org) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + This file is part of the tinyusb stack. +*/ +/**************************************************************************/ + +#include "dcd.h" + +#if MODE_DEVICE_SUPPORTED + + +#endif diff --git a/tinyusb/device/usbd.c b/tinyusb/device/usbd.c index 0b9044db9..b91cf6991 100644 --- a/tinyusb/device/usbd.c +++ b/tinyusb/device/usbd.c @@ -1,422 +1,422 @@ -/**************************************************************************/ -/*! - @file usbd.c - @author hathach (tinyusb.org) - - @section LICENSE - - Software License Agreement (BSD License) - - Copyright (c) 2013, hathach (tinyusb.org) - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. Neither the name of the copyright holders nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - This file is part of the tinyusb stack. -*/ -/**************************************************************************/ - -#include "tusb_option.h" - -#if MODE_DEVICE_SUPPORTED - -#define _TINY_USB_SOURCE_FILE_ - -//--------------------------------------------------------------------+ -// INCLUDE -//--------------------------------------------------------------------+ -#include "tusb.h" -#include "tusb_descriptors.h" // TODO callback include -#include "usbd_dcd.h" - -//--------------------------------------------------------------------+ -// MACRO CONSTANT TYPEDEF -//--------------------------------------------------------------------+ -usbd_device_info_t usbd_devices[CONTROLLER_DEVICE_NUMBER]; - -// TODO fix/compress number of class driver -static usbd_class_driver_t const usbd_class_drivers[TUSB_CLASS_MAPPED_INDEX_START] = -{ -#if DEVICE_CLASS_HID - [TUSB_CLASS_HID] = - { - .init = hidd_init, - .open = hidd_open, - .control_request_subtask = hidd_control_request_subtask, - .xfer_cb = hidd_xfer_cb, - .close = hidd_close - }, -#endif - -#if TUSB_CFG_DEVICE_MSC - [TUSB_CLASS_MSC] = - { - .init = mscd_init, - .open = mscd_open, - .control_request_subtask = mscd_control_request_subtask, - .xfer_cb = mscd_xfer_cb, - .close = mscd_close - }, -#endif - -#if TUSB_CFG_DEVICE_CDC - [TUSB_CLASS_CDC] = - { - .init = cdcd_init, - .open = cdcd_open, - .control_request_subtask = cdcd_control_request_subtask, - .xfer_cb = cdcd_xfer_cb, - .close = cdcd_close - }, -#endif - -}; - -//--------------------------------------------------------------------+ -// INTERNAL OBJECT & FUNCTION DECLARATION -//--------------------------------------------------------------------+ -static tusb_error_t usbd_set_configure_received(uint8_t coreid, uint8_t config_number); -static tusb_error_t get_descriptor(uint8_t coreid, tusb_control_request_t const * const p_request, uint8_t ** pp_buffer, uint16_t * p_length); - -//--------------------------------------------------------------------+ -// APPLICATION INTERFACE -//--------------------------------------------------------------------+ -bool tusbd_is_configured(uint8_t coreid) -{ - return usbd_devices[coreid].state == TUSB_DEVICE_STATE_CONFIGURED; -} - -//--------------------------------------------------------------------+ -// IMPLEMENTATION -//--------------------------------------------------------------------+ - -//------------- OSAL Task -------------// -enum { - USBD_TASK_QUEUE_DEPTH = 8 -}; - -typedef enum { - USBD_EVENTID_SETUP_RECEIVED = 1, - USBD_EVENTID_XFER_DONE -}usbd_eventid_t; - -typedef struct ATTR_ALIGNED(4) -{ - uint8_t coreid; - uint8_t event_id; - uint8_t sub_event_id; - uint8_t reserved; - - union { - tusb_control_request_t setup_received; // USBD_EVENTID_SETUP_RECEIVED - - struct { // USBD_EVENTID_XFER_DONE - endpoint_handle_t edpt_hdl; - uint32_t xferred_byte; - }xfer_done; - }; -}usbd_task_event_t; - -STATIC_ASSERT(sizeof(usbd_task_event_t) <= 12, "size is not correct"); - -OSAL_TASK_DEF(usbd_task, 150, TUSB_CFG_OS_TASK_PRIO); -OSAL_QUEUE_DEF(usbd_queue_def, USBD_TASK_QUEUE_DEPTH, usbd_task_event_t); -OSAL_SEM_DEF(usbd_control_xfer_semaphore_def); - -static osal_queue_handle_t usbd_queue_hdl; -/*static*/ osal_semaphore_handle_t usbd_control_xfer_sem_hdl; // TODO may need to change to static with wrapper function - -tusb_error_t usbd_control_request_subtask(uint8_t coreid, tusb_control_request_t const * const p_request) -{ - OSAL_SUBTASK_BEGIN - - tusb_error_t error = TUSB_ERROR_NONE; - - //------------- Standard Control such as those in enumeration -------------// - if( TUSB_REQUEST_RECIPIENT_DEVICE == p_request->bmRequestType_bit.recipient && - TUSB_REQUEST_TYPE_STANDARD == p_request->bmRequestType_bit.type ) - { - if ( TUSB_REQUEST_GET_DESCRIPTOR == p_request->bRequest ) - { - uint8_t* p_buffer = NULL; - uint16_t length = 0; - - error = get_descriptor(coreid, p_request, &p_buffer, &length); - - if ( TUSB_ERROR_NONE == error ) - { - dcd_pipe_control_xfer(coreid, p_request->bmRequestType_bit.direction, p_buffer, length, false); - } - } - else if ( TUSB_REQUEST_SET_ADDRESS == p_request->bRequest ) - { - dcd_controller_set_address(coreid, (uint8_t) p_request->wValue); - usbd_devices[coreid].state = TUSB_DEVICE_STATE_ADDRESSED; - } - else if ( TUSB_REQUEST_SET_CONFIGURATION == p_request->bRequest ) - { - usbd_set_configure_received(coreid, (uint8_t) p_request->wValue); - }else - { - error = TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT; - } - } - - //------------- Class/Interface Specific Request -------------// - else if ( TUSB_REQUEST_RECIPIENT_INTERFACE == p_request->bmRequestType_bit.recipient) - { - OSAL_VAR tusb_std_class_code_t class_code; - - class_code = usbd_devices[coreid].interface2class[ u16_low_u8(p_request->wIndex) ]; - - if ( (TUSB_CLASS_AUDIO <= class_code) && (class_code <= TUSB_CLASS_AUDIO_VIDEO) && - usbd_class_drivers[class_code].control_request_subtask ) - { - OSAL_SUBTASK_INVOKED_AND_WAIT( usbd_class_drivers[class_code].control_request_subtask(coreid, p_request), error ); - }else - { - error = TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT; - } - } - - //------------- Endpoint Request -------------// - else if ( TUSB_REQUEST_RECIPIENT_ENDPOINT == p_request->bmRequestType_bit.recipient && - TUSB_REQUEST_TYPE_STANDARD == p_request->bmRequestType_bit.type && - TUSB_REQUEST_CLEAR_FEATURE == p_request->bRequest ) - { - dcd_pipe_clear_stall(coreid, u16_low_u8(p_request->wIndex) ); - } else - { - error = TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT; - } - - if(TUSB_ERROR_NONE != error) - { // Response with Protocol Stall if request is not supported - dcd_pipe_control_stall(coreid); - // ASSERT(error == TUSB_ERROR_NONE, VOID_RETURN); - }else if (p_request->wLength == 0) - { - dcd_pipe_control_xfer(coreid, p_request->bmRequestType_bit.direction, NULL, 0, false); // zero length for non-data - } - - OSAL_SUBTASK_END -} - -// To enable the TASK_ASSERT style (quick return on false condition) in a real RTOS, a task must act as a wrapper -// and is used mainly to call subtasks. Within a subtask return statement can be called freely, the task with -// forever loop cannot have any return at all. -OSAL_TASK_FUNCTION(usbd_task) (void* p_task_para) -{ - OSAL_TASK_LOOP_BEGIN - - OSAL_VAR usbd_task_event_t event; - tusb_error_t error = TUSB_ERROR_NONE; - - osal_queue_receive(usbd_queue_hdl, &event, OSAL_TIMEOUT_WAIT_FOREVER, &error); - SUBTASK_ASSERT_STATUS(error); - - if ( USBD_EVENTID_SETUP_RECEIVED == event.event_id ) - { - OSAL_SUBTASK_INVOKED_AND_WAIT( usbd_control_request_subtask(event.coreid, &event.setup_received), error ); - }else - { - uint8_t class_index; - class_index = std_class_code_to_index( event.xfer_done.edpt_hdl.class_code ); - - if (usbd_class_drivers[class_index].xfer_cb) - { - usbd_class_drivers[class_index].xfer_cb( event.xfer_done.edpt_hdl, event.sub_event_id, event.xfer_done.xferred_byte); - }else - { - hal_debugger_breakpoint(); // something wrong, no one claims the isr's source - } - } - - OSAL_TASK_LOOP_END -} - -tusb_error_t usbd_init (void) -{ - ASSERT_STATUS ( dcd_init() ); - - //------------- Task init -------------// - usbd_queue_hdl = osal_queue_create( OSAL_QUEUE_REF(usbd_queue_def) ); - ASSERT_PTR(usbd_queue_hdl, TUSB_ERROR_OSAL_QUEUE_FAILED); - - usbd_control_xfer_sem_hdl = osal_semaphore_create( OSAL_SEM_REF(usbd_control_xfer_semaphore_def) ); - ASSERT_PTR(usbd_queue_hdl, TUSB_ERROR_OSAL_SEMAPHORE_FAILED); - - ASSERT_STATUS( osal_task_create( OSAL_TASK_REF(usbd_task) )); - - //------------- class init -------------// - for (tusb_std_class_code_t class_code = TUSB_CLASS_AUDIO; class_code <= TUSB_CLASS_AUDIO_VIDEO; class_code++) - { - if ( usbd_class_drivers[class_code].init ) - { - usbd_class_drivers[class_code].init(); - } - } - - return TUSB_ERROR_NONE; -} - -//--------------------------------------------------------------------+ -// CONTROL REQUEST -//--------------------------------------------------------------------+ -// TODO Host (windows) can get HID report descriptor before set configured -// need to open interface before set configured -static tusb_error_t usbd_set_configure_received(uint8_t coreid, uint8_t config_number) -{ - dcd_controller_set_configuration(coreid); - usbd_devices[coreid].state = TUSB_DEVICE_STATE_CONFIGURED; - - //------------- parse configuration & open drivers -------------// - uint8_t* p_desc_configure = (uint8_t*) &app_tusb_desc_configuration; - uint8_t* p_desc = p_desc_configure + sizeof(tusb_descriptor_configuration_t); - - while( p_desc < p_desc_configure + ((tusb_descriptor_configuration_t*)p_desc_configure)->wTotalLength ) - { - if ( TUSB_DESC_TYPE_INTERFACE_ASSOCIATION == p_desc[DESCRIPTOR_OFFSET_TYPE]) - { - p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH]; // ignore IAD - }else - { - ASSERT( TUSB_DESC_TYPE_INTERFACE == p_desc[DESCRIPTOR_OFFSET_TYPE], TUSB_ERROR_NOT_SUPPORTED_YET ); - - uint8_t class_index; - tusb_descriptor_interface_t* p_desc_interface = (tusb_descriptor_interface_t*) p_desc; - - class_index = p_desc_interface->bInterfaceClass; - - ASSERT( class_index != 0 && usbd_class_drivers[class_index].open != NULL, TUSB_ERROR_NOT_SUPPORTED_YET ); - ASSERT( 0 == usbd_devices[coreid].interface2class[p_desc_interface->bInterfaceNumber], TUSB_ERROR_FAILED); // duplicate interface number TODO alternate setting - - usbd_devices[coreid].interface2class[p_desc_interface->bInterfaceNumber] = class_index; - - uint16_t length=0; - ASSERT_STATUS( usbd_class_drivers[class_index].open( coreid, p_desc_interface, &length ) ); - - ASSERT( length >= sizeof(tusb_descriptor_interface_t), TUSB_ERROR_FAILED ); - p_desc += length; - } - } - - return TUSB_ERROR_NONE; -} - -static tusb_error_t get_descriptor(uint8_t coreid, tusb_control_request_t const * const p_request, uint8_t ** pp_buffer, uint16_t * p_length) -{ - tusb_std_descriptor_type_t const desc_type = u16_high_u8(p_request->wValue); - uint8_t const desc_index = u16_low_u8( p_request->wValue ); - - if ( TUSB_DESC_TYPE_DEVICE == desc_type ) - { - (*pp_buffer) = (uint8_t *) &app_tusb_desc_device; - (*p_length) = sizeof(tusb_descriptor_device_t); - } - else if ( TUSB_DESC_TYPE_CONFIGURATION == desc_type ) - { - (*pp_buffer) = (uint8_t *) &app_tusb_desc_configuration; - (*p_length) = sizeof(app_tusb_desc_configuration); - } - else if ( TUSB_DESC_TYPE_STRING == desc_type ) - { - if ( ! (desc_index < TUSB_CFG_DEVICE_STRING_DESCRIPTOR_COUNT) ) return TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT; - - (*pp_buffer) = (uint8_t *) desc_str_table[desc_index]; - (*p_length) = **pp_buffer; - }else - { - return TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT; - } - - (*p_length) = min16_of(p_request->wLength, (*p_length) ); // cannot return more than hosts requires - - return TUSB_ERROR_NONE; -} -//--------------------------------------------------------------------+ -// USBD-CLASS API -//--------------------------------------------------------------------+ - -//--------------------------------------------------------------------+ -// USBD-DCD Callback API -//--------------------------------------------------------------------+ -void usbd_dcd_bus_event_isr(uint8_t coreid, usbd_bus_event_type_t bus_event) -{ - switch(bus_event) - { - case USBD_BUS_EVENT_RESET : - case USBD_BUS_EVENT_UNPLUGGED : - memclr_(&usbd_devices[coreid], sizeof(usbd_device_info_t)); - for (tusb_std_class_code_t class_code = TUSB_CLASS_AUDIO; class_code <= TUSB_CLASS_AUDIO_VIDEO; class_code++) - { - if ( usbd_class_drivers[class_code].close ) usbd_class_drivers[class_code].close( coreid ); - } - break; - - case USBD_BUS_EVENT_SUSPENDED: - usbd_devices[coreid].state = TUSB_DEVICE_STATE_SUSPENDED; - break; - - default: break; - } -} - -void usbd_setup_received_isr(uint8_t coreid, tusb_control_request_t * p_request) -{ - usbd_task_event_t task_event = - { - .coreid = coreid, - .event_id = USBD_EVENTID_SETUP_RECEIVED, - }; - - task_event.setup_received = (*p_request); - osal_queue_send(usbd_queue_hdl, &task_event); -} - -void usbd_xfer_isr(endpoint_handle_t edpt_hdl, tusb_event_t event, uint32_t xferred_bytes) -{ - if (edpt_hdl.class_code == 0 ) // Control Transfer - { - osal_semaphore_post( usbd_control_xfer_sem_hdl ); - }else - { - usbd_task_event_t task_event = - { - .coreid = edpt_hdl.coreid, - .event_id = USBD_EVENTID_XFER_DONE, - .sub_event_id = event - }; - - task_event.xfer_done.xferred_byte = xferred_bytes; - task_event.xfer_done.edpt_hdl = edpt_hdl; - - osal_queue_send(usbd_queue_hdl, &task_event); - } -} - -//--------------------------------------------------------------------+ -// HELPER -//--------------------------------------------------------------------+ - -#endif +/**************************************************************************/ +/*! + @file usbd.c + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2013, hathach (tinyusb.org) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + This file is part of the tinyusb stack. +*/ +/**************************************************************************/ + +#include "tusb_option.h" + +#if MODE_DEVICE_SUPPORTED + +#define _TINY_USB_SOURCE_FILE_ + +//--------------------------------------------------------------------+ +// INCLUDE +//--------------------------------------------------------------------+ +#include "tusb.h" +#include "tusb_descriptors.h" // TODO callback include +#include "usbd_dcd.h" + +//--------------------------------------------------------------------+ +// MACRO CONSTANT TYPEDEF +//--------------------------------------------------------------------+ +usbd_device_info_t usbd_devices[CONTROLLER_DEVICE_NUMBER]; + +// TODO fix/compress number of class driver +static usbd_class_driver_t const usbd_class_drivers[TUSB_CLASS_MAPPED_INDEX_START] = +{ +#if DEVICE_CLASS_HID + [TUSB_CLASS_HID] = + { + .init = hidd_init, + .open = hidd_open, + .control_request_subtask = hidd_control_request_subtask, + .xfer_cb = hidd_xfer_cb, + .close = hidd_close + }, +#endif + +#if TUSB_CFG_DEVICE_MSC + [TUSB_CLASS_MSC] = + { + .init = mscd_init, + .open = mscd_open, + .control_request_subtask = mscd_control_request_subtask, + .xfer_cb = mscd_xfer_cb, + .close = mscd_close + }, +#endif + +#if TUSB_CFG_DEVICE_CDC + [TUSB_CLASS_CDC] = + { + .init = cdcd_init, + .open = cdcd_open, + .control_request_subtask = cdcd_control_request_subtask, + .xfer_cb = cdcd_xfer_cb, + .close = cdcd_close + }, +#endif + +}; + +//--------------------------------------------------------------------+ +// INTERNAL OBJECT & FUNCTION DECLARATION +//--------------------------------------------------------------------+ +static tusb_error_t usbd_set_configure_received(uint8_t coreid, uint8_t config_number); +static tusb_error_t get_descriptor(uint8_t coreid, tusb_control_request_t const * const p_request, uint8_t ** pp_buffer, uint16_t * p_length); + +//--------------------------------------------------------------------+ +// APPLICATION INTERFACE +//--------------------------------------------------------------------+ +bool tusbd_is_configured(uint8_t coreid) +{ + return usbd_devices[coreid].state == TUSB_DEVICE_STATE_CONFIGURED; +} + +//--------------------------------------------------------------------+ +// IMPLEMENTATION +//--------------------------------------------------------------------+ + +//------------- OSAL Task -------------// +enum { + USBD_TASK_QUEUE_DEPTH = 8 +}; + +typedef enum { + USBD_EVENTID_SETUP_RECEIVED = 1, + USBD_EVENTID_XFER_DONE +}usbd_eventid_t; + +typedef struct ATTR_ALIGNED(4) +{ + uint8_t coreid; + uint8_t event_id; + uint8_t sub_event_id; + uint8_t reserved; + + union { + tusb_control_request_t setup_received; // USBD_EVENTID_SETUP_RECEIVED + + struct { // USBD_EVENTID_XFER_DONE + endpoint_handle_t edpt_hdl; + uint32_t xferred_byte; + }xfer_done; + }; +}usbd_task_event_t; + +STATIC_ASSERT(sizeof(usbd_task_event_t) <= 12, "size is not correct"); + +OSAL_TASK_DEF(usbd_task, 150, TUSB_CFG_OS_TASK_PRIO); +OSAL_QUEUE_DEF(usbd_queue_def, USBD_TASK_QUEUE_DEPTH, usbd_task_event_t); +OSAL_SEM_DEF(usbd_control_xfer_semaphore_def); + +static osal_queue_handle_t usbd_queue_hdl; +/*static*/ osal_semaphore_handle_t usbd_control_xfer_sem_hdl; // TODO may need to change to static with wrapper function + +tusb_error_t usbd_control_request_subtask(uint8_t coreid, tusb_control_request_t const * const p_request) +{ + OSAL_SUBTASK_BEGIN + + tusb_error_t error = TUSB_ERROR_NONE; + + //------------- Standard Control such as those in enumeration -------------// + if( TUSB_REQUEST_RECIPIENT_DEVICE == p_request->bmRequestType_bit.recipient && + TUSB_REQUEST_TYPE_STANDARD == p_request->bmRequestType_bit.type ) + { + if ( TUSB_REQUEST_GET_DESCRIPTOR == p_request->bRequest ) + { + uint8_t* p_buffer = NULL; + uint16_t length = 0; + + error = get_descriptor(coreid, p_request, &p_buffer, &length); + + if ( TUSB_ERROR_NONE == error ) + { + dcd_pipe_control_xfer(coreid, p_request->bmRequestType_bit.direction, p_buffer, length, false); + } + } + else if ( TUSB_REQUEST_SET_ADDRESS == p_request->bRequest ) + { + dcd_controller_set_address(coreid, (uint8_t) p_request->wValue); + usbd_devices[coreid].state = TUSB_DEVICE_STATE_ADDRESSED; + } + else if ( TUSB_REQUEST_SET_CONFIGURATION == p_request->bRequest ) + { + usbd_set_configure_received(coreid, (uint8_t) p_request->wValue); + }else + { + error = TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT; + } + } + + //------------- Class/Interface Specific Request -------------// + else if ( TUSB_REQUEST_RECIPIENT_INTERFACE == p_request->bmRequestType_bit.recipient) + { + OSAL_VAR tusb_std_class_code_t class_code; + + class_code = usbd_devices[coreid].interface2class[ u16_low_u8(p_request->wIndex) ]; + + if ( (TUSB_CLASS_AUDIO <= class_code) && (class_code <= TUSB_CLASS_AUDIO_VIDEO) && + usbd_class_drivers[class_code].control_request_subtask ) + { + OSAL_SUBTASK_INVOKED_AND_WAIT( usbd_class_drivers[class_code].control_request_subtask(coreid, p_request), error ); + }else + { + error = TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT; + } + } + + //------------- Endpoint Request -------------// + else if ( TUSB_REQUEST_RECIPIENT_ENDPOINT == p_request->bmRequestType_bit.recipient && + TUSB_REQUEST_TYPE_STANDARD == p_request->bmRequestType_bit.type && + TUSB_REQUEST_CLEAR_FEATURE == p_request->bRequest ) + { + dcd_pipe_clear_stall(coreid, u16_low_u8(p_request->wIndex) ); + } else + { + error = TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT; + } + + if(TUSB_ERROR_NONE != error) + { // Response with Protocol Stall if request is not supported + dcd_pipe_control_stall(coreid); + // ASSERT(error == TUSB_ERROR_NONE, VOID_RETURN); + }else if (p_request->wLength == 0) + { + dcd_pipe_control_xfer(coreid, p_request->bmRequestType_bit.direction, NULL, 0, false); // zero length for non-data + } + + OSAL_SUBTASK_END +} + +// To enable the TASK_ASSERT style (quick return on false condition) in a real RTOS, a task must act as a wrapper +// and is used mainly to call subtasks. Within a subtask return statement can be called freely, the task with +// forever loop cannot have any return at all. +OSAL_TASK_FUNCTION(usbd_task) (void* p_task_para) +{ + OSAL_TASK_LOOP_BEGIN + + OSAL_VAR usbd_task_event_t event; + tusb_error_t error = TUSB_ERROR_NONE; + + osal_queue_receive(usbd_queue_hdl, &event, OSAL_TIMEOUT_WAIT_FOREVER, &error); + SUBTASK_ASSERT_STATUS(error); + + if ( USBD_EVENTID_SETUP_RECEIVED == event.event_id ) + { + OSAL_SUBTASK_INVOKED_AND_WAIT( usbd_control_request_subtask(event.coreid, &event.setup_received), error ); + }else + { + uint8_t class_index; + class_index = std_class_code_to_index( event.xfer_done.edpt_hdl.class_code ); + + if (usbd_class_drivers[class_index].xfer_cb) + { + usbd_class_drivers[class_index].xfer_cb( event.xfer_done.edpt_hdl, event.sub_event_id, event.xfer_done.xferred_byte); + }else + { + hal_debugger_breakpoint(); // something wrong, no one claims the isr's source + } + } + + OSAL_TASK_LOOP_END +} + +tusb_error_t usbd_init (void) +{ + ASSERT_STATUS ( dcd_init() ); + + //------------- Task init -------------// + usbd_queue_hdl = osal_queue_create( OSAL_QUEUE_REF(usbd_queue_def) ); + ASSERT_PTR(usbd_queue_hdl, TUSB_ERROR_OSAL_QUEUE_FAILED); + + usbd_control_xfer_sem_hdl = osal_semaphore_create( OSAL_SEM_REF(usbd_control_xfer_semaphore_def) ); + ASSERT_PTR(usbd_queue_hdl, TUSB_ERROR_OSAL_SEMAPHORE_FAILED); + + ASSERT_STATUS( osal_task_create( OSAL_TASK_REF(usbd_task) )); + + //------------- class init -------------// + for (tusb_std_class_code_t class_code = TUSB_CLASS_AUDIO; class_code <= TUSB_CLASS_AUDIO_VIDEO; class_code++) + { + if ( usbd_class_drivers[class_code].init ) + { + usbd_class_drivers[class_code].init(); + } + } + + return TUSB_ERROR_NONE; +} + +//--------------------------------------------------------------------+ +// CONTROL REQUEST +//--------------------------------------------------------------------+ +// TODO Host (windows) can get HID report descriptor before set configured +// need to open interface before set configured +static tusb_error_t usbd_set_configure_received(uint8_t coreid, uint8_t config_number) +{ + dcd_controller_set_configuration(coreid); + usbd_devices[coreid].state = TUSB_DEVICE_STATE_CONFIGURED; + + //------------- parse configuration & open drivers -------------// + uint8_t* p_desc_configure = (uint8_t*) &app_tusb_desc_configuration; + uint8_t* p_desc = p_desc_configure + sizeof(tusb_descriptor_configuration_t); + + while( p_desc < p_desc_configure + ((tusb_descriptor_configuration_t*)p_desc_configure)->wTotalLength ) + { + if ( TUSB_DESC_TYPE_INTERFACE_ASSOCIATION == p_desc[DESCRIPTOR_OFFSET_TYPE]) + { + p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH]; // ignore IAD + }else + { + ASSERT( TUSB_DESC_TYPE_INTERFACE == p_desc[DESCRIPTOR_OFFSET_TYPE], TUSB_ERROR_NOT_SUPPORTED_YET ); + + uint8_t class_index; + tusb_descriptor_interface_t* p_desc_interface = (tusb_descriptor_interface_t*) p_desc; + + class_index = p_desc_interface->bInterfaceClass; + + ASSERT( class_index != 0 && usbd_class_drivers[class_index].open != NULL, TUSB_ERROR_NOT_SUPPORTED_YET ); + ASSERT( 0 == usbd_devices[coreid].interface2class[p_desc_interface->bInterfaceNumber], TUSB_ERROR_FAILED); // duplicate interface number TODO alternate setting + + usbd_devices[coreid].interface2class[p_desc_interface->bInterfaceNumber] = class_index; + + uint16_t length=0; + ASSERT_STATUS( usbd_class_drivers[class_index].open( coreid, p_desc_interface, &length ) ); + + ASSERT( length >= sizeof(tusb_descriptor_interface_t), TUSB_ERROR_FAILED ); + p_desc += length; + } + } + + return TUSB_ERROR_NONE; +} + +static tusb_error_t get_descriptor(uint8_t coreid, tusb_control_request_t const * const p_request, uint8_t ** pp_buffer, uint16_t * p_length) +{ + tusb_std_descriptor_type_t const desc_type = u16_high_u8(p_request->wValue); + uint8_t const desc_index = u16_low_u8( p_request->wValue ); + + if ( TUSB_DESC_TYPE_DEVICE == desc_type ) + { + (*pp_buffer) = (uint8_t *) &app_tusb_desc_device; + (*p_length) = sizeof(tusb_descriptor_device_t); + } + else if ( TUSB_DESC_TYPE_CONFIGURATION == desc_type ) + { + (*pp_buffer) = (uint8_t *) &app_tusb_desc_configuration; + (*p_length) = sizeof(app_tusb_desc_configuration); + } + else if ( TUSB_DESC_TYPE_STRING == desc_type ) + { + if ( ! (desc_index < TUSB_CFG_DEVICE_STRING_DESCRIPTOR_COUNT) ) return TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT; + + (*pp_buffer) = (uint8_t *) desc_str_table[desc_index]; + (*p_length) = **pp_buffer; + }else + { + return TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT; + } + + (*p_length) = min16_of(p_request->wLength, (*p_length) ); // cannot return more than hosts requires + + return TUSB_ERROR_NONE; +} +//--------------------------------------------------------------------+ +// USBD-CLASS API +//--------------------------------------------------------------------+ + +//--------------------------------------------------------------------+ +// USBD-DCD Callback API +//--------------------------------------------------------------------+ +void usbd_dcd_bus_event_isr(uint8_t coreid, usbd_bus_event_type_t bus_event) +{ + switch(bus_event) + { + case USBD_BUS_EVENT_RESET : + case USBD_BUS_EVENT_UNPLUGGED : + memclr_(&usbd_devices[coreid], sizeof(usbd_device_info_t)); + for (tusb_std_class_code_t class_code = TUSB_CLASS_AUDIO; class_code <= TUSB_CLASS_AUDIO_VIDEO; class_code++) + { + if ( usbd_class_drivers[class_code].close ) usbd_class_drivers[class_code].close( coreid ); + } + break; + + case USBD_BUS_EVENT_SUSPENDED: + usbd_devices[coreid].state = TUSB_DEVICE_STATE_SUSPENDED; + break; + + default: break; + } +} + +void usbd_setup_received_isr(uint8_t coreid, tusb_control_request_t * p_request) +{ + usbd_task_event_t task_event = + { + .coreid = coreid, + .event_id = USBD_EVENTID_SETUP_RECEIVED, + }; + + task_event.setup_received = (*p_request); + osal_queue_send(usbd_queue_hdl, &task_event); +} + +void usbd_xfer_isr(endpoint_handle_t edpt_hdl, tusb_event_t event, uint32_t xferred_bytes) +{ + if (edpt_hdl.class_code == 0 ) // Control Transfer + { + osal_semaphore_post( usbd_control_xfer_sem_hdl ); + }else + { + usbd_task_event_t task_event = + { + .coreid = edpt_hdl.coreid, + .event_id = USBD_EVENTID_XFER_DONE, + .sub_event_id = event + }; + + task_event.xfer_done.xferred_byte = xferred_bytes; + task_event.xfer_done.edpt_hdl = edpt_hdl; + + osal_queue_send(usbd_queue_hdl, &task_event); + } +} + +//--------------------------------------------------------------------+ +// HELPER +//--------------------------------------------------------------------+ + +#endif diff --git a/tinyusb/hal/hal.h b/tinyusb/hal/hal.h index 65c3776b1..c7df7d493 100644 --- a/tinyusb/hal/hal.h +++ b/tinyusb/hal/hal.h @@ -1,137 +1,137 @@ -/**************************************************************************/ -/*! - @file hal.h - @author hathach (tinyusb.org) - - @section LICENSE - - Software License Agreement (BSD License) - - Copyright (c) 2013, hathach (tinyusb.org) - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. Neither the name of the copyright holders nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - This file is part of the tinyusb stack. -*/ -/**************************************************************************/ - -#ifndef _TUSB_HAL_H_ -#define _TUSB_HAL_H_ - -/** \addtogroup Port Port - * @{ - * \defgroup Port_Hal Hardware Abtract Layer (HAL) - * Hardware Abstraction Layer (HAL) is an abstraction layer, between the physical hardware and the tinyusb stack. - * Its function is to hide differences in hardware from most of MCUs, so that most of the stack code does not need to be changed to - * run on systems with a different MCU. - * HAL are sets of routines that emulate some platform-specific details, giving programs direct access to the hardware resources. - * @{ - */ - -//--------------------------------------------------------------------+ -// INCLUDES -//--------------------------------------------------------------------+ -#include "tusb_option.h" -#include "common/primitive_types.h" -#include "common/errors.h" -#include "common/compiler/compiler.h" - -//--------------------------------------------------------------------+ -// HAL API -//--------------------------------------------------------------------+ -// callback from tusb.h -extern void tusb_isr(uint8_t coreid); - -/** \brief Initialize USB controller hardware - * \returns \ref tusb_error_t type to indicate success or error condition. - * \note This function is invoked by \ref tusb_init as part of the initialization. - */ -tusb_error_t hal_init(void); - -/** \brief Enable USB Interrupt on a specific USB Controller - * \param[in] coreid is a zero-based index to identify USB controller's ID - * \note Some MCUs such as NXP LPC43xx has multiple USB controllers. It is necessary to know which USB controller for - * those MCUs. - */ -static inline void hal_interrupt_enable(uint8_t coreid) ATTR_ALWAYS_INLINE; - -/** \brief Disable USB Interrupt on a specific USB Controller - * \param[in] coreid is a zero-based index to identify USB controller's ID - * \note Some MCUs such as NXP LPC43xx has multiple USB controllers. It is necessary to know which USB controller for - * those MCUs. - */ -static inline void hal_interrupt_disable(uint8_t coreid) ATTR_ALWAYS_INLINE; - -//--------------------------------------------------------------------+ -// INCLUDE DRIVEN -//--------------------------------------------------------------------+ -#if TUSB_CFG_MCU == MCU_LPC11UXX - #include "hal_lpc11uxx.h" -#elif TUSB_CFG_MCU == MCU_LPC13UXX - #include "hal_lpc13uxx.h" -#elif TUSB_CFG_MCU == MCU_LPC43XX - #include "hal_lpc43xx.h" -#elif TUSB_CFG_MCU == MCU_LPC175X_6X - #include "hal_lpc175x_6x.h" -#else - #error MCU is not defined or supported yet -#endif - -#ifdef __cplusplus -extern "C" { -#endif - - -static inline bool hal_debugger_is_attached(void) ATTR_PURE ATTR_ALWAYS_INLINE; -static inline bool hal_debugger_is_attached(void) -{ -// TODO check core M3/M4 defined instead -#if !defined(_TEST_) && !(TUSB_CFG_MCU==MCU_LPC11UXX) - return ( (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk) == CoreDebug_DHCSR_C_DEBUGEN_Msk ); -#elif TUSB_CFG_DEBUG == 3 - return true; // force to break into breakpoint with debug = 3 -#else - return false -#endif -} - -static inline void hal_debugger_breakpoint(void) ATTR_ALWAYS_INLINE; -static inline void hal_debugger_breakpoint(void) -{ -#ifndef _TEST_ - if (hal_debugger_is_attached()) /* if there is debugger connected */ - { - __asm("BKPT #0\n"); - } -#endif -} - -#ifdef __cplusplus - } -#endif - -#endif /* _TUSB_HAL_H_ */ - -/** @} */ -/** @} */ +/**************************************************************************/ +/*! + @file hal.h + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2013, hathach (tinyusb.org) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + This file is part of the tinyusb stack. +*/ +/**************************************************************************/ + +#ifndef _TUSB_HAL_H_ +#define _TUSB_HAL_H_ + +/** \addtogroup Port Port + * @{ + * \defgroup Port_Hal Hardware Abtract Layer (HAL) + * Hardware Abstraction Layer (HAL) is an abstraction layer, between the physical hardware and the tinyusb stack. + * Its function is to hide differences in hardware from most of MCUs, so that most of the stack code does not need to be changed to + * run on systems with a different MCU. + * HAL are sets of routines that emulate some platform-specific details, giving programs direct access to the hardware resources. + * @{ + */ + +//--------------------------------------------------------------------+ +// INCLUDES +//--------------------------------------------------------------------+ +#include "tusb_option.h" +#include "common/primitive_types.h" +#include "common/errors.h" +#include "common/compiler/compiler.h" + +//--------------------------------------------------------------------+ +// HAL API +//--------------------------------------------------------------------+ +// callback from tusb.h +extern void tusb_isr(uint8_t coreid); + +/** \brief Initialize USB controller hardware + * \returns \ref tusb_error_t type to indicate success or error condition. + * \note This function is invoked by \ref tusb_init as part of the initialization. + */ +tusb_error_t hal_init(void); + +/** \brief Enable USB Interrupt on a specific USB Controller + * \param[in] coreid is a zero-based index to identify USB controller's ID + * \note Some MCUs such as NXP LPC43xx has multiple USB controllers. It is necessary to know which USB controller for + * those MCUs. + */ +static inline void hal_interrupt_enable(uint8_t coreid) ATTR_ALWAYS_INLINE; + +/** \brief Disable USB Interrupt on a specific USB Controller + * \param[in] coreid is a zero-based index to identify USB controller's ID + * \note Some MCUs such as NXP LPC43xx has multiple USB controllers. It is necessary to know which USB controller for + * those MCUs. + */ +static inline void hal_interrupt_disable(uint8_t coreid) ATTR_ALWAYS_INLINE; + +//--------------------------------------------------------------------+ +// INCLUDE DRIVEN +//--------------------------------------------------------------------+ +#if TUSB_CFG_MCU == MCU_LPC11UXX + #include "hal_lpc11uxx.h" +#elif TUSB_CFG_MCU == MCU_LPC13UXX + #include "hal_lpc13uxx.h" +#elif TUSB_CFG_MCU == MCU_LPC43XX + #include "hal_lpc43xx.h" +#elif TUSB_CFG_MCU == MCU_LPC175X_6X + #include "hal_lpc175x_6x.h" +#else + #error MCU is not defined or supported yet +#endif + +#ifdef __cplusplus +extern "C" { +#endif + + +static inline bool hal_debugger_is_attached(void) ATTR_PURE ATTR_ALWAYS_INLINE; +static inline bool hal_debugger_is_attached(void) +{ +// TODO check core M3/M4 defined instead +#if !defined(_TEST_) && !(TUSB_CFG_MCU==MCU_LPC11UXX) + return ( (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk) == CoreDebug_DHCSR_C_DEBUGEN_Msk ); +#elif TUSB_CFG_DEBUG == 3 + return true; // force to break into breakpoint with debug = 3 +#else + return false +#endif +} + +static inline void hal_debugger_breakpoint(void) ATTR_ALWAYS_INLINE; +static inline void hal_debugger_breakpoint(void) +{ +#ifndef _TEST_ + if (hal_debugger_is_attached()) /* if there is debugger connected */ + { + __asm("BKPT #0\n"); + } +#endif +} + +#ifdef __cplusplus + } +#endif + +#endif /* _TUSB_HAL_H_ */ + +/** @} */ +/** @} */ diff --git a/tinyusb/hal/hal_lpc13uxx.h b/tinyusb/hal/hal_lpc13uxx.h index 51d17ca59..1ccad5df1 100644 --- a/tinyusb/hal/hal_lpc13uxx.h +++ b/tinyusb/hal/hal_lpc13uxx.h @@ -1,73 +1,73 @@ -/**************************************************************************/ -/*! - @file hal_lpc13uxx.h - @author hathach (tinyusb.org) - - @section LICENSE - - Software License Agreement (BSD License) - - Copyright (c) 2013, hathach (tinyusb.org) - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. Neither the name of the copyright holders nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - This file is part of the tinyusb stack. -*/ -/**************************************************************************/ - -/** \ingroup TBD - * \defgroup TBD - * \brief TBD - * - * @{ - */ - -#ifndef _TUSB_HAL_LPC13UXX_H_ -#define _TUSB_HAL_LPC13UXX_H_ - -#include "LPC13Uxx.h" - -#ifdef __cplusplus - extern "C" { -#endif - -static inline void hal_interrupt_enable(uint8_t coreid) -{ - (void) coreid; // discard compiler's warning - NVIC_EnableIRQ(USB_IRQ_IRQn); -} - -static inline void hal_interrupt_disable(uint8_t coreid) -{ - (void) coreid; // discard compiler's warning - NVIC_DisableIRQ(USB_IRQ_IRQn); -} - -#ifdef __cplusplus - } -#endif - -#endif /* _TUSB_HAL_LPC13UXX_H_ */ - -/** @} */ +/**************************************************************************/ +/*! + @file hal_lpc13uxx.h + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2013, hathach (tinyusb.org) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + This file is part of the tinyusb stack. +*/ +/**************************************************************************/ + +/** \ingroup TBD + * \defgroup TBD + * \brief TBD + * + * @{ + */ + +#ifndef _TUSB_HAL_LPC13UXX_H_ +#define _TUSB_HAL_LPC13UXX_H_ + +#include "LPC13Uxx.h" + +#ifdef __cplusplus + extern "C" { +#endif + +static inline void hal_interrupt_enable(uint8_t coreid) +{ + (void) coreid; // discard compiler's warning + NVIC_EnableIRQ(USB_IRQ_IRQn); +} + +static inline void hal_interrupt_disable(uint8_t coreid) +{ + (void) coreid; // discard compiler's warning + NVIC_DisableIRQ(USB_IRQ_IRQn); +} + +#ifdef __cplusplus + } +#endif + +#endif /* _TUSB_HAL_LPC13UXX_H_ */ + +/** @} */ diff --git a/tinyusb/osal/osal_none.c b/tinyusb/osal/osal_none.c index 068de840b..8e1d3d098 100644 --- a/tinyusb/osal/osal_none.c +++ b/tinyusb/osal/osal_none.c @@ -1,61 +1,61 @@ -/**************************************************************************/ -/*! - @file osal_none.c - @author hathach (tinyusb.org) - - @section LICENSE - - Software License Agreement (BSD License) - - Copyright (c) 2013, hathach (tinyusb.org) - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. Neither the name of the copyright holders nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - This file is part of the tinyusb stack. -*/ -/**************************************************************************/ - -#include "tusb_option.h" -#include "osal.h" // TODO refractor - -#if TUSB_CFG_OS == TUSB_OS_NONE - -#define _TINY_USB_SOURCE_FILE_ - -//--------------------------------------------------------------------+ -// INCLUDE -//--------------------------------------------------------------------+ -#include "osal_none.h" - -//--------------------------------------------------------------------+ -// INTERNAL OBJECT & FUNCTION DECLARATION -//--------------------------------------------------------------------+ -volatile uint32_t osal_tick_current = 0; - -//--------------------------------------------------------------------+ -// IMPLEMENTATION -//--------------------------------------------------------------------+ - - -#endif +/**************************************************************************/ +/*! + @file osal_none.c + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2013, hathach (tinyusb.org) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + This file is part of the tinyusb stack. +*/ +/**************************************************************************/ + +#include "tusb_option.h" +#include "osal.h" // TODO refractor + +#if TUSB_CFG_OS == TUSB_OS_NONE + +#define _TINY_USB_SOURCE_FILE_ + +//--------------------------------------------------------------------+ +// INCLUDE +//--------------------------------------------------------------------+ +#include "osal_none.h" + +//--------------------------------------------------------------------+ +// INTERNAL OBJECT & FUNCTION DECLARATION +//--------------------------------------------------------------------+ +volatile uint32_t osal_tick_current = 0; + +//--------------------------------------------------------------------+ +// IMPLEMENTATION +//--------------------------------------------------------------------+ + + +#endif diff --git a/tinyusb/osal/osal_none.h b/tinyusb/osal/osal_none.h index 7816cda33..300233f42 100644 --- a/tinyusb/osal/osal_none.h +++ b/tinyusb/osal/osal_none.h @@ -1,344 +1,344 @@ -/**************************************************************************/ -/*! - @file osal_none.h - @author hathach (tinyusb.org) - - @section LICENSE - - Software License Agreement (BSD License) - - Copyright (c) 2013, hathach (tinyusb.org) - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. Neither the name of the copyright holders nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - This file is part of the tinyusb stack. -*/ -/**************************************************************************/ - -/** \file - * \brief TBD - * - * \note TBD - */ - -/** \ingroup TBD - * \defgroup TBD - * \brief TBD - * - * @{ - */ - -#ifndef _TUSB_OSAL_NONE_H_ -#define _TUSB_OSAL_NONE_H_ - -#include "osal_common.h" - -#ifdef __cplusplus - extern "C" { -#endif - -// variable that retains value after OSAL blocking service need to declare with OSAL_VAR -#define OSAL_VAR static - -//--------------------------------------------------------------------+ -// TICK API -//--------------------------------------------------------------------+ -extern volatile uint32_t osal_tick_current; -static inline void osal_tick_tock(void) ATTR_ALWAYS_INLINE; -static inline void osal_tick_tock(void) -{ - osal_tick_current++; -} - -static inline uint32_t osal_tick_get(void) ATTR_ALWAYS_INLINE; -static inline uint32_t osal_tick_get(void) -{ - return osal_tick_current; -} - -//--------------------------------------------------------------------+ -// TASK API -// NOTES: Each blocking OSAL_NONE services such as semaphore wait, -// queue receive embedded return statement, therefore local variable -// retain value before/after such services needed to declare as static -// OSAL_TASK_LOOP -// { -// OSAL_TASK_LOOP_BEGIN -// -// task body statements -// -// OSAL_TASK_LOOP_ENG -// } -//--------------------------------------------------------------------+ -#define OSAL_TASK_DEF(code, stack_depth, prio) -#define OSAL_TASK_REF -#define osal_task_create(x) TUSB_ERROR_NONE - -#define OSAL_TASK_FUNCTION(task_func) \ - tusb_error_t task_func - -#define TASK_RESTART \ - state = 0 - -#define OSAL_TASK_LOOP_BEGIN \ - ATTR_UNUSED static uint32_t timeout = 0;\ - static uint16_t state = 0;\ - switch(state) { \ - case 0: { \ - -#define OSAL_TASK_LOOP_END \ - default:\ - TASK_RESTART;\ - }}\ - return TUSB_ERROR_NONE; - - -#define osal_task_delay(msec) \ - do {\ - timeout = osal_tick_get();\ - state = __LINE__; case __LINE__:\ - if ( timeout + osal_tick_from_msec(msec) > osal_tick_get() ) /* time out */ \ - return TUSB_ERROR_OSAL_WAITING;\ - }while(0) - -//--------------------------------------------------------------------+ -// SUBTASK (a sub function that uses OS blocking services & called by a task -//--------------------------------------------------------------------+ -//------------- Sub Task -------------// -#define OSAL_SUBTASK_INVOKED_AND_WAIT(subtask, status) \ - do {\ - state = __LINE__; case __LINE__:\ - {\ - status = subtask; /* invoke sub task */\ - if (TUSB_ERROR_OSAL_WAITING == status) /* sub task not finished -> continue waiting */\ - return TUSB_ERROR_OSAL_WAITING;\ - }\ - }while(0) - -#define OSAL_SUBTASK_BEGIN OSAL_TASK_LOOP_BEGIN -#define OSAL_SUBTASK_END OSAL_TASK_LOOP_END - -//------------- Sub Task Assert -------------// -#define SUBTASK_EXIT(error) \ - do {\ - TASK_RESTART; return error;\ - }while(0) - -#define _SUBTASK_ASSERT_ERROR_HANDLER(error, func_call) \ - func_call; TASK_RESTART; return error - -#define SUBTASK_ASSERT_STATUS(sts) \ - ASSERT_DEFINE_WITH_HANDLER(_SUBTASK_ASSERT_ERROR_HANDLER, , tusb_error_t status = (tusb_error_t)(sts),\ - TUSB_ERROR_NONE == status, status, "%s", TUSB_ErrorStr[status]) - -#define SUBTASK_ASSERT_STATUS_WITH_HANDLER(sts, func_call) \ - ASSERT_DEFINE_WITH_HANDLER(_SUBTASK_ASSERT_ERROR_HANDLER, func_call, tusb_error_t status = (tusb_error_t)(sts),\ - TUSB_ERROR_NONE == status, status, "%s", TUSB_ErrorStr[status]) - -// TODO allow to specify error return -#define SUBTASK_ASSERT(condition) \ - ASSERT_DEFINE_WITH_HANDLER(_SUBTASK_ASSERT_ERROR_HANDLER, , , \ - (condition), TUSB_ERROR_OSAL_TASK_FAILED, "%s", "evaluated to false") -// TODO remove assert with handler by catching error in enum main task -#define SUBTASK_ASSERT_WITH_HANDLER(condition, func_call) \ - ASSERT_DEFINE_WITH_HANDLER(_SUBTASK_ASSERT_ERROR_HANDLER, func_call, ,\ - condition, TUSB_ERROR_OSAL_TASK_FAILED, "%s", "evaluated to false") - -//--------------------------------------------------------------------+ -// Semaphore API -//--------------------------------------------------------------------+ -typedef volatile uint8_t osal_semaphore_t; -typedef osal_semaphore_t * osal_semaphore_handle_t; - -#define OSAL_SEM_DEF(name)\ - osal_semaphore_t name - -#define OSAL_SEM_REF(name)\ - &name - -static inline osal_semaphore_handle_t osal_semaphore_create(osal_semaphore_t * p_sem) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE; -static inline osal_semaphore_handle_t osal_semaphore_create(osal_semaphore_t * p_sem) -{ - (*p_sem) = 0; // TODO consider to have initial count parameter - return (osal_semaphore_handle_t) p_sem; -} - -static inline tusb_error_t osal_semaphore_post(osal_semaphore_handle_t sem_hdl) ATTR_ALWAYS_INLINE; -static inline tusb_error_t osal_semaphore_post(osal_semaphore_handle_t sem_hdl) -{ - (*sem_hdl)++; - - return TUSB_ERROR_NONE; -} - -static inline void osal_semaphore_reset(osal_semaphore_handle_t sem_hdl) ATTR_ALWAYS_INLINE; -static inline void osal_semaphore_reset(osal_semaphore_handle_t sem_hdl) -{ - (*sem_hdl) = 0; -} - -#define osal_semaphore_wait(sem_hdl, msec, p_error) \ - do {\ - timeout = osal_tick_get();\ - state = __LINE__; case __LINE__:\ - if( *(sem_hdl) == 0 ) {\ - if ( ( ((uint32_t) (msec)) != OSAL_TIMEOUT_WAIT_FOREVER) && (timeout + osal_tick_from_msec(msec) <= osal_tick_get()) ) /* time out */ \ - *(p_error) = TUSB_ERROR_OSAL_TIMEOUT;\ - else\ - return TUSB_ERROR_OSAL_WAITING;\ - } else{\ - (*(sem_hdl))--; /*TODO mutex hal_interrupt_disable consideration*/\ - *(p_error) = TUSB_ERROR_NONE;\ - }\ - }while(0) - -//--------------------------------------------------------------------+ -// MUTEX API (priority inheritance) -//--------------------------------------------------------------------+ -typedef osal_semaphore_t osal_mutex_t; -typedef osal_semaphore_handle_t osal_mutex_handle_t; - -#define OSAL_MUTEX_DEF(name)\ - osal_mutex_t name - -#define OSAL_MUTEX_REF(name)\ - &name - -static inline osal_mutex_handle_t osal_mutex_create(osal_mutex_t * p_mutex) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE; -static inline osal_mutex_handle_t osal_mutex_create(osal_mutex_t * p_mutex) -{ - (*p_mutex) = 1; - return (osal_mutex_handle_t) p_mutex; -} - -static inline tusb_error_t osal_mutex_release(osal_mutex_handle_t mutex_hdl) ATTR_ALWAYS_INLINE; -static inline tusb_error_t osal_mutex_release(osal_mutex_handle_t mutex_hdl) -{ - (*mutex_hdl) = 1; // mutex is a binary semaphore - - return TUSB_ERROR_NONE; -} - -static inline void osal_mutex_reset(osal_mutex_handle_t mutex_hdl) ATTR_ALWAYS_INLINE; -static inline void osal_mutex_reset(osal_mutex_handle_t mutex_hdl) -{ - (*mutex_hdl) = 1; -} - -#define osal_mutex_wait osal_semaphore_wait - -//--------------------------------------------------------------------+ -// QUEUE API -//--------------------------------------------------------------------+ -typedef struct{ - uint8_t* const buffer ; ///< buffer pointer - uint8_t const depth ; ///< max items - uint8_t const item_size ; ///< size of each item - volatile uint8_t count ; ///< number of items in queue - volatile uint8_t wr_idx ; ///< write pointer - volatile uint8_t rd_idx ; ///< read pointer -} osal_queue_t; - -typedef osal_queue_t * osal_queue_handle_t; - -// use to declare a queue, within the scope of tinyusb, should only use primitive type only -#define OSAL_QUEUE_DEF(name, queue_depth, type)\ - STATIC_ASSERT(queue_depth < 256, "OSAL Queue only support up to 255 depth");\ - type name##_buffer[queue_depth];\ - osal_queue_t name = {\ - .buffer = (uint8_t*) name##_buffer,\ - .depth = queue_depth,\ - .item_size = sizeof(type)\ - } - -#define OSAL_QUEUE_REF(name) (&name) - -static inline osal_queue_handle_t osal_queue_create(osal_queue_t * const p_queue) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE; -static inline osal_queue_handle_t osal_queue_create(osal_queue_t * const p_queue) -{ - p_queue->count = p_queue->wr_idx = p_queue->rd_idx = 0; - return (osal_queue_handle_t) p_queue; -} - -// TODO move to osal_none.c -// when queue is full, it will overwrite the oldest data in the queue -static inline tusb_error_t osal_queue_send(osal_queue_handle_t const queue_hdl, void const * data) ATTR_ALWAYS_INLINE; -static inline tusb_error_t osal_queue_send(osal_queue_handle_t const queue_hdl, void const * data) -{ - //TODO mutex lock hal_interrupt_disable - - memcpy( queue_hdl->buffer + (queue_hdl->wr_idx * queue_hdl->item_size), - data, - queue_hdl->item_size); - - queue_hdl->wr_idx = (queue_hdl->wr_idx + 1) % queue_hdl->depth; - - if (queue_hdl->depth == queue_hdl->count) // queue is full, 1st rd is overwritten - { - queue_hdl->rd_idx = queue_hdl->wr_idx; // keep full state - }else - { - queue_hdl->count++; - } - - //TODO mutex unlock hal_interrupt_enable - - return TUSB_ERROR_NONE; -} - -static inline void osal_queue_flush(osal_queue_handle_t const queue_hdl) ATTR_ALWAYS_INLINE; -static inline void osal_queue_flush(osal_queue_handle_t const queue_hdl) -{ - queue_hdl->count = queue_hdl->rd_idx = queue_hdl->wr_idx = 0; -} - -#define osal_queue_receive(queue_hdl, p_data, msec, p_error) \ - do {\ - timeout = osal_tick_get();\ - state = __LINE__; case __LINE__:\ - if( queue_hdl->count == 0 ) {\ - if ( (msec != OSAL_TIMEOUT_WAIT_FOREVER) && ( timeout + osal_tick_from_msec(msec) <= osal_tick_get() )) /* time out */ \ - *(p_error) = TUSB_ERROR_OSAL_TIMEOUT;\ - else\ - return TUSB_ERROR_OSAL_WAITING;\ - } else{\ - /*TODO mutex lock hal_interrupt_disable */\ - memcpy(p_data, queue_hdl->buffer + (queue_hdl->rd_idx * queue_hdl->item_size), queue_hdl->item_size);\ - queue_hdl->rd_idx = (queue_hdl->rd_idx + 1) % queue_hdl->depth;\ - queue_hdl->count--;\ - /*TODO mutex unlock hal_interrupt_enable */\ - *(p_error) = TUSB_ERROR_NONE;\ - }\ - }while(0) - - -// queue_send, queue_receive - -#ifdef __cplusplus - } -#endif - -#endif /* _TUSB_OSAL_NONE_H_ */ - -/** @} */ +/**************************************************************************/ +/*! + @file osal_none.h + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2013, hathach (tinyusb.org) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + This file is part of the tinyusb stack. +*/ +/**************************************************************************/ + +/** \file + * \brief TBD + * + * \note TBD + */ + +/** \ingroup TBD + * \defgroup TBD + * \brief TBD + * + * @{ + */ + +#ifndef _TUSB_OSAL_NONE_H_ +#define _TUSB_OSAL_NONE_H_ + +#include "osal_common.h" + +#ifdef __cplusplus + extern "C" { +#endif + +// variable that retains value after OSAL blocking service need to declare with OSAL_VAR +#define OSAL_VAR static + +//--------------------------------------------------------------------+ +// TICK API +//--------------------------------------------------------------------+ +extern volatile uint32_t osal_tick_current; +static inline void osal_tick_tock(void) ATTR_ALWAYS_INLINE; +static inline void osal_tick_tock(void) +{ + osal_tick_current++; +} + +static inline uint32_t osal_tick_get(void) ATTR_ALWAYS_INLINE; +static inline uint32_t osal_tick_get(void) +{ + return osal_tick_current; +} + +//--------------------------------------------------------------------+ +// TASK API +// NOTES: Each blocking OSAL_NONE services such as semaphore wait, +// queue receive embedded return statement, therefore local variable +// retain value before/after such services needed to declare as static +// OSAL_TASK_LOOP +// { +// OSAL_TASK_LOOP_BEGIN +// +// task body statements +// +// OSAL_TASK_LOOP_ENG +// } +//--------------------------------------------------------------------+ +#define OSAL_TASK_DEF(code, stack_depth, prio) +#define OSAL_TASK_REF +#define osal_task_create(x) TUSB_ERROR_NONE + +#define OSAL_TASK_FUNCTION(task_func) \ + tusb_error_t task_func + +#define TASK_RESTART \ + state = 0 + +#define OSAL_TASK_LOOP_BEGIN \ + ATTR_UNUSED static uint32_t timeout = 0;\ + static uint16_t state = 0;\ + switch(state) { \ + case 0: { \ + +#define OSAL_TASK_LOOP_END \ + default:\ + TASK_RESTART;\ + }}\ + return TUSB_ERROR_NONE; + + +#define osal_task_delay(msec) \ + do {\ + timeout = osal_tick_get();\ + state = __LINE__; case __LINE__:\ + if ( timeout + osal_tick_from_msec(msec) > osal_tick_get() ) /* time out */ \ + return TUSB_ERROR_OSAL_WAITING;\ + }while(0) + +//--------------------------------------------------------------------+ +// SUBTASK (a sub function that uses OS blocking services & called by a task +//--------------------------------------------------------------------+ +//------------- Sub Task -------------// +#define OSAL_SUBTASK_INVOKED_AND_WAIT(subtask, status) \ + do {\ + state = __LINE__; case __LINE__:\ + {\ + status = subtask; /* invoke sub task */\ + if (TUSB_ERROR_OSAL_WAITING == status) /* sub task not finished -> continue waiting */\ + return TUSB_ERROR_OSAL_WAITING;\ + }\ + }while(0) + +#define OSAL_SUBTASK_BEGIN OSAL_TASK_LOOP_BEGIN +#define OSAL_SUBTASK_END OSAL_TASK_LOOP_END + +//------------- Sub Task Assert -------------// +#define SUBTASK_EXIT(error) \ + do {\ + TASK_RESTART; return error;\ + }while(0) + +#define _SUBTASK_ASSERT_ERROR_HANDLER(error, func_call) \ + func_call; TASK_RESTART; return error + +#define SUBTASK_ASSERT_STATUS(sts) \ + ASSERT_DEFINE_WITH_HANDLER(_SUBTASK_ASSERT_ERROR_HANDLER, , tusb_error_t status = (tusb_error_t)(sts),\ + TUSB_ERROR_NONE == status, status, "%s", TUSB_ErrorStr[status]) + +#define SUBTASK_ASSERT_STATUS_WITH_HANDLER(sts, func_call) \ + ASSERT_DEFINE_WITH_HANDLER(_SUBTASK_ASSERT_ERROR_HANDLER, func_call, tusb_error_t status = (tusb_error_t)(sts),\ + TUSB_ERROR_NONE == status, status, "%s", TUSB_ErrorStr[status]) + +// TODO allow to specify error return +#define SUBTASK_ASSERT(condition) \ + ASSERT_DEFINE_WITH_HANDLER(_SUBTASK_ASSERT_ERROR_HANDLER, , , \ + (condition), TUSB_ERROR_OSAL_TASK_FAILED, "%s", "evaluated to false") +// TODO remove assert with handler by catching error in enum main task +#define SUBTASK_ASSERT_WITH_HANDLER(condition, func_call) \ + ASSERT_DEFINE_WITH_HANDLER(_SUBTASK_ASSERT_ERROR_HANDLER, func_call, ,\ + condition, TUSB_ERROR_OSAL_TASK_FAILED, "%s", "evaluated to false") + +//--------------------------------------------------------------------+ +// Semaphore API +//--------------------------------------------------------------------+ +typedef volatile uint8_t osal_semaphore_t; +typedef osal_semaphore_t * osal_semaphore_handle_t; + +#define OSAL_SEM_DEF(name)\ + osal_semaphore_t name + +#define OSAL_SEM_REF(name)\ + &name + +static inline osal_semaphore_handle_t osal_semaphore_create(osal_semaphore_t * p_sem) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE; +static inline osal_semaphore_handle_t osal_semaphore_create(osal_semaphore_t * p_sem) +{ + (*p_sem) = 0; // TODO consider to have initial count parameter + return (osal_semaphore_handle_t) p_sem; +} + +static inline tusb_error_t osal_semaphore_post(osal_semaphore_handle_t sem_hdl) ATTR_ALWAYS_INLINE; +static inline tusb_error_t osal_semaphore_post(osal_semaphore_handle_t sem_hdl) +{ + (*sem_hdl)++; + + return TUSB_ERROR_NONE; +} + +static inline void osal_semaphore_reset(osal_semaphore_handle_t sem_hdl) ATTR_ALWAYS_INLINE; +static inline void osal_semaphore_reset(osal_semaphore_handle_t sem_hdl) +{ + (*sem_hdl) = 0; +} + +#define osal_semaphore_wait(sem_hdl, msec, p_error) \ + do {\ + timeout = osal_tick_get();\ + state = __LINE__; case __LINE__:\ + if( *(sem_hdl) == 0 ) {\ + if ( ( ((uint32_t) (msec)) != OSAL_TIMEOUT_WAIT_FOREVER) && (timeout + osal_tick_from_msec(msec) <= osal_tick_get()) ) /* time out */ \ + *(p_error) = TUSB_ERROR_OSAL_TIMEOUT;\ + else\ + return TUSB_ERROR_OSAL_WAITING;\ + } else{\ + (*(sem_hdl))--; /*TODO mutex hal_interrupt_disable consideration*/\ + *(p_error) = TUSB_ERROR_NONE;\ + }\ + }while(0) + +//--------------------------------------------------------------------+ +// MUTEX API (priority inheritance) +//--------------------------------------------------------------------+ +typedef osal_semaphore_t osal_mutex_t; +typedef osal_semaphore_handle_t osal_mutex_handle_t; + +#define OSAL_MUTEX_DEF(name)\ + osal_mutex_t name + +#define OSAL_MUTEX_REF(name)\ + &name + +static inline osal_mutex_handle_t osal_mutex_create(osal_mutex_t * p_mutex) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE; +static inline osal_mutex_handle_t osal_mutex_create(osal_mutex_t * p_mutex) +{ + (*p_mutex) = 1; + return (osal_mutex_handle_t) p_mutex; +} + +static inline tusb_error_t osal_mutex_release(osal_mutex_handle_t mutex_hdl) ATTR_ALWAYS_INLINE; +static inline tusb_error_t osal_mutex_release(osal_mutex_handle_t mutex_hdl) +{ + (*mutex_hdl) = 1; // mutex is a binary semaphore + + return TUSB_ERROR_NONE; +} + +static inline void osal_mutex_reset(osal_mutex_handle_t mutex_hdl) ATTR_ALWAYS_INLINE; +static inline void osal_mutex_reset(osal_mutex_handle_t mutex_hdl) +{ + (*mutex_hdl) = 1; +} + +#define osal_mutex_wait osal_semaphore_wait + +//--------------------------------------------------------------------+ +// QUEUE API +//--------------------------------------------------------------------+ +typedef struct{ + uint8_t* const buffer ; ///< buffer pointer + uint8_t const depth ; ///< max items + uint8_t const item_size ; ///< size of each item + volatile uint8_t count ; ///< number of items in queue + volatile uint8_t wr_idx ; ///< write pointer + volatile uint8_t rd_idx ; ///< read pointer +} osal_queue_t; + +typedef osal_queue_t * osal_queue_handle_t; + +// use to declare a queue, within the scope of tinyusb, should only use primitive type only +#define OSAL_QUEUE_DEF(name, queue_depth, type)\ + STATIC_ASSERT(queue_depth < 256, "OSAL Queue only support up to 255 depth");\ + type name##_buffer[queue_depth];\ + osal_queue_t name = {\ + .buffer = (uint8_t*) name##_buffer,\ + .depth = queue_depth,\ + .item_size = sizeof(type)\ + } + +#define OSAL_QUEUE_REF(name) (&name) + +static inline osal_queue_handle_t osal_queue_create(osal_queue_t * const p_queue) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE; +static inline osal_queue_handle_t osal_queue_create(osal_queue_t * const p_queue) +{ + p_queue->count = p_queue->wr_idx = p_queue->rd_idx = 0; + return (osal_queue_handle_t) p_queue; +} + +// TODO move to osal_none.c +// when queue is full, it will overwrite the oldest data in the queue +static inline tusb_error_t osal_queue_send(osal_queue_handle_t const queue_hdl, void const * data) ATTR_ALWAYS_INLINE; +static inline tusb_error_t osal_queue_send(osal_queue_handle_t const queue_hdl, void const * data) +{ + //TODO mutex lock hal_interrupt_disable + + memcpy( queue_hdl->buffer + (queue_hdl->wr_idx * queue_hdl->item_size), + data, + queue_hdl->item_size); + + queue_hdl->wr_idx = (queue_hdl->wr_idx + 1) % queue_hdl->depth; + + if (queue_hdl->depth == queue_hdl->count) // queue is full, 1st rd is overwritten + { + queue_hdl->rd_idx = queue_hdl->wr_idx; // keep full state + }else + { + queue_hdl->count++; + } + + //TODO mutex unlock hal_interrupt_enable + + return TUSB_ERROR_NONE; +} + +static inline void osal_queue_flush(osal_queue_handle_t const queue_hdl) ATTR_ALWAYS_INLINE; +static inline void osal_queue_flush(osal_queue_handle_t const queue_hdl) +{ + queue_hdl->count = queue_hdl->rd_idx = queue_hdl->wr_idx = 0; +} + +#define osal_queue_receive(queue_hdl, p_data, msec, p_error) \ + do {\ + timeout = osal_tick_get();\ + state = __LINE__; case __LINE__:\ + if( queue_hdl->count == 0 ) {\ + if ( (msec != OSAL_TIMEOUT_WAIT_FOREVER) && ( timeout + osal_tick_from_msec(msec) <= osal_tick_get() )) /* time out */ \ + *(p_error) = TUSB_ERROR_OSAL_TIMEOUT;\ + else\ + return TUSB_ERROR_OSAL_WAITING;\ + } else{\ + /*TODO mutex lock hal_interrupt_disable */\ + memcpy(p_data, queue_hdl->buffer + (queue_hdl->rd_idx * queue_hdl->item_size), queue_hdl->item_size);\ + queue_hdl->rd_idx = (queue_hdl->rd_idx + 1) % queue_hdl->depth;\ + queue_hdl->count--;\ + /*TODO mutex unlock hal_interrupt_enable */\ + *(p_error) = TUSB_ERROR_NONE;\ + }\ + }while(0) + + +// queue_send, queue_receive + +#ifdef __cplusplus + } +#endif + +#endif /* _TUSB_OSAL_NONE_H_ */ + +/** @} */ diff --git a/tinyusb/tusb.c b/tinyusb/tusb.c index f9a457599..980e19402 100644 --- a/tinyusb/tusb.c +++ b/tinyusb/tusb.c @@ -1,91 +1,91 @@ -/**************************************************************************/ -/*! - @file tusb.c - @author hathach (tinyusb.org) - - @section LICENSE - - Software License Agreement (BSD License) - - Copyright (c) 2013, hathach (tinyusb.org) - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. Neither the name of the copyright holders nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - This file is part of the tinyusb stack. -*/ -/**************************************************************************/ - -#define _TINY_USB_SOURCE_FILE_ - -#include "tusb.h" - -tusb_error_t tusb_init(void) -{ - ASSERT_STATUS( hal_init() ) ; // hardware init - -#if MODE_HOST_SUPPORTED - ASSERT_STATUS( usbh_init() ); // host stack init -#endif - -#if MODE_DEVICE_SUPPORTED - ASSERT_STATUS ( usbd_init() ); // device stack init -#endif - -#if (TUSB_CFG_CONTROLLER_0_MODE) - hal_interrupt_enable(0); -#endif - -#if (TUSB_CFG_CONTROLLER_1_MODE) - hal_interrupt_enable(1); -#endif - - return TUSB_ERROR_NONE; -} - -// called from hal layer -void tusb_isr(uint8_t controller_id) -{ -#if MODE_HOST_SUPPORTED - hcd_isr(controller_id); -#endif - -#if MODE_DEVICE_SUPPORTED - dcd_isr(controller_id); -#endif - -} - -#if TUSB_CFG_OS == TUSB_OS_NONE -// periodically/continuously called in the main loop -void tusb_task_runner(void) -{ - #if MODE_HOST_SUPPORTED - usbh_enumeration_task(NULL); - #endif - - #if MODE_DEVICE_SUPPORTED - usbd_task(NULL); - #endif -} -#endif +/**************************************************************************/ +/*! + @file tusb.c + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2013, hathach (tinyusb.org) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + This file is part of the tinyusb stack. +*/ +/**************************************************************************/ + +#define _TINY_USB_SOURCE_FILE_ + +#include "tusb.h" + +tusb_error_t tusb_init(void) +{ + ASSERT_STATUS( hal_init() ) ; // hardware init + +#if MODE_HOST_SUPPORTED + ASSERT_STATUS( usbh_init() ); // host stack init +#endif + +#if MODE_DEVICE_SUPPORTED + ASSERT_STATUS ( usbd_init() ); // device stack init +#endif + +#if (TUSB_CFG_CONTROLLER_0_MODE) + hal_interrupt_enable(0); +#endif + +#if (TUSB_CFG_CONTROLLER_1_MODE) + hal_interrupt_enable(1); +#endif + + return TUSB_ERROR_NONE; +} + +// called from hal layer +void tusb_isr(uint8_t controller_id) +{ +#if MODE_HOST_SUPPORTED + hcd_isr(controller_id); +#endif + +#if MODE_DEVICE_SUPPORTED + dcd_isr(controller_id); +#endif + +} + +#if TUSB_CFG_OS == TUSB_OS_NONE +// periodically/continuously called in the main loop +void tusb_task_runner(void) +{ + #if MODE_HOST_SUPPORTED + usbh_enumeration_task(NULL); + #endif + + #if MODE_DEVICE_SUPPORTED + usbd_task(NULL); + #endif +} +#endif