mirror of
https://github.com/array-in-a-matrix/SAROO.git
synced 2025-04-02 10:31:43 -04:00
341 lines
12 KiB
C
341 lines
12 KiB
C
/*
|
|
* _______ _ _ _____ ____
|
|
* |__ __| | | | |/ ____| _ \
|
|
* | | ___ ___ _ __ _ _| | | | (___ | |_) |
|
|
* | |/ _ \/ _ \ '_ \| | | | | | |\___ \| _ <
|
|
* | | __/ __/ | | | |_| | |__| |____) | |_) |
|
|
* |_|\___|\___|_| |_|\__, |\____/|_____/|____/
|
|
* __/ |
|
|
* |___/
|
|
*
|
|
* TeenyUSB - light weight usb stack for STM32 micro controllers
|
|
*
|
|
* Copyright (c) 2019 XToolBox - admin@xtoolbox.org
|
|
* www.tusb.org
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*/
|
|
|
|
#ifndef __TUSB_DEF_H__
|
|
#define __TUSB_DEF_H__
|
|
|
|
#include "stdint.h"
|
|
|
|
|
|
#define USB_LEN_DEV_QUALIFIER_DESC 0x0A
|
|
#define USB_LEN_DEV_DESC 0x12
|
|
#define USB_LEN_CFG_DESC 0x09
|
|
#define USB_LEN_IF_DESC 0x09
|
|
#define USB_LEN_EP_DESC 0x07
|
|
#define USB_LEN_OTG_DESC 0x03
|
|
#define USB_LEN_LANGID_STR_DESC 0x04
|
|
#define USB_LEN_OTHER_SPEED_DESC_SIZ 0x09
|
|
#define USB_LEN_IF_ASSOC_DESC 0x08
|
|
#define USB_LEN_FUNCTION_DESC 0x03
|
|
|
|
#define USBD_IDX_LANGID_STR 0x00
|
|
#define USBD_IDX_MFC_STR 0x01
|
|
#define USBD_IDX_PRODUCT_STR 0x02
|
|
#define USBD_IDX_SERIAL_STR 0x03
|
|
#define USBD_IDX_CONFIG_STR 0x04
|
|
#define USBD_IDX_INTERFACE_STR 0x05
|
|
|
|
#define USB_REQ_TYPE_STANDARD 0x00
|
|
#define USB_REQ_TYPE_CLASS 0x20
|
|
#define USB_REQ_TYPE_VENDOR 0x40
|
|
#define USB_REQ_TYPE_MASK 0x60
|
|
|
|
#define USB_REQ_RECIPIENT_DEVICE 0x00
|
|
#define USB_REQ_RECIPIENT_INTERFACE 0x01
|
|
#define USB_REQ_RECIPIENT_ENDPOINT 0x02
|
|
#define USB_REQ_RECIPIENT_MASK 0x03
|
|
|
|
#define USB_REQ_GET_STATUS 0x00
|
|
#define USB_REQ_CLEAR_FEATURE 0x01
|
|
#define USB_REQ_SET_FEATURE 0x03
|
|
#define USB_REQ_SET_ADDRESS 0x05
|
|
#define USB_REQ_GET_DESCRIPTOR 0x06
|
|
#define USB_REQ_SET_DESCRIPTOR 0x07
|
|
#define USB_REQ_GET_CONFIGURATION 0x08
|
|
#define USB_REQ_SET_CONFIGURATION 0x09
|
|
#define USB_REQ_GET_INTERFACE 0x0A
|
|
#define USB_REQ_SET_INTERFACE 0x0B
|
|
#define USB_REQ_SYNCH_FRAME 0x0C
|
|
|
|
#define USB_DESC_TYPE_DEVICE 1
|
|
#define USB_DESC_TYPE_CONFIGURATION 2
|
|
#define USB_DESC_TYPE_STRING 3
|
|
#define USB_DESC_TYPE_INTERFACE 4
|
|
#define USB_DESC_TYPE_ENDPOINT 5
|
|
#define USB_DESC_TYPE_DEVICE_QUALIFIER 6
|
|
#define USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION 7
|
|
#define USB_DESC_TYPE_BOS 0x0F
|
|
#define USB_DESC_TYPE_DEVICE_CAPABILITY 16
|
|
|
|
#define USB_DEVICE_DESCRIPTOR_TYPE 0x01
|
|
#define USB_CONFIGURATION_DESCRIPTOR_TYPE 0x02
|
|
#define USB_STRING_DESCRIPTOR_TYPE 0x03
|
|
#define USB_INTERFACE_DESCRIPTOR_TYPE 0x04
|
|
#define USB_ENDPOINT_DESCRIPTOR_TYPE 0x05
|
|
#define USB_IAD_DESCRIPTOR_TYPE 0x0B
|
|
#define USB_HUB_DESCRIPTOR_TYPE 0x29
|
|
#define USB_HID_DESCRIPTOR_TYPE 0x21
|
|
#define USB_REPORT_DESCRIPTOR_TYPE 0x22
|
|
#define USB_DESC_TYPE_REPORT 0x22
|
|
#define USB_FUCNTION_DESCRIPTOR_TYPE 0x24
|
|
|
|
|
|
#define USB_CLASS_DEVICE 0x00
|
|
#define USB_CLASS_AUDIO 0x01
|
|
#define USB_CLASS_CDC 0x02
|
|
#define USB_CLASS_HID 0x03
|
|
#define USB_CLASS_PHYSICAL 0x05
|
|
#define USB_CLASS_IMAGE 0x06
|
|
#define USB_CLASS_PRINTER 0x07
|
|
#define USB_CLASS_MASS_STORAGE 0x08
|
|
#define USB_CLASS_HUB 0x09
|
|
#define USB_CLASS_CDC_DATA 0x0a
|
|
#define USB_CLASS_SMART_CARD 0x0b
|
|
#define USB_CLASS_SECURITY 0x0d
|
|
#define USB_CLASS_VIDEO 0x0e
|
|
#define USB_CLASS_HEALTHCARE 0x0f
|
|
#define USB_CLASS_DIAG_DEVICE 0xdc
|
|
#define USB_CLASS_WIRELESS 0xe0
|
|
#define USB_CLASS_MISC 0xef
|
|
#define USB_CLASS_APP_SPECIFIC 0xfe
|
|
#define USB_CLASS_VEND_SPECIFIC 0xff
|
|
|
|
|
|
#define USB_CONFIG_REMOTE_WAKEUP 2
|
|
#define USB_CONFIG_SELF_POWERED 1
|
|
|
|
#define USB_FEATURE_EP_HALT 0
|
|
#define USB_FEATURE_REMOTE_WAKEUP 1
|
|
#define USB_FEATURE_TEST_MODE 2
|
|
|
|
#define USB_DEVICE_CAPABITY_TYPE 0x10
|
|
|
|
#define USB_HS_MAX_PACKET_SIZE 512
|
|
#define USB_FS_MAX_PACKET_SIZE 64
|
|
#define USB_MAX_EP0_SIZE 64
|
|
|
|
/* Device Status */
|
|
#define USBD_STATE_DEFAULT 1
|
|
#define USBD_STATE_ADDRESSED 2
|
|
#define USBD_STATE_CONFIGURED 3
|
|
#define USBD_STATE_SUSPENDED 4
|
|
|
|
|
|
/* EP0 State */
|
|
#define USBD_EP0_IDLE 0
|
|
#define USBD_EP0_SETUP 1
|
|
#define USBD_EP0_DATA_IN 2
|
|
#define USBD_EP0_DATA_OUT 3
|
|
#define USBD_EP0_STATUS_IN 4
|
|
#define USBD_EP0_STATUS_OUT 5
|
|
#define USBD_EP0_STALL 6
|
|
|
|
#define USBD_EP_TYPE_CTRL 0
|
|
#define USBD_EP_TYPE_ISOC 1
|
|
#define USBD_EP_TYPE_BULK 2
|
|
#define USBD_EP_TYPE_INTR 3
|
|
#define USBD_EP_TYPE_MASK 3
|
|
|
|
|
|
#ifndef SWAPBYTE
|
|
#define SWAPBYTE(addr) (((uint16_t)(*((uint8_t *)(addr)))) + \
|
|
(((uint16_t)(*(((uint8_t *)(addr)) + 1))) << 8))
|
|
#endif
|
|
|
|
#ifndef LOBYTE
|
|
#define LOBYTE(x) ((uint8_t)(x & 0x00FF))
|
|
#endif
|
|
|
|
#ifndef HIBYTE
|
|
#define HIBYTE(x) ((uint8_t)((x & 0xFF00) >>8))
|
|
#endif
|
|
|
|
#ifndef MIN
|
|
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
|
|
#endif
|
|
|
|
#ifndef MAX
|
|
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
|
|
#endif
|
|
|
|
#define WBVAL(x) (x&0xFF),((x>>8)&0xFF)
|
|
|
|
|
|
#if defined ( __GNUC__ )
|
|
#ifndef __weak
|
|
#define __weak __attribute__((weak))
|
|
#endif /* __weak */
|
|
#ifndef __packed
|
|
#define __packed __attribute__((__packed__))
|
|
#endif /* __packed */
|
|
#endif /* __GNUC__ */
|
|
|
|
|
|
#define WEAK __weak
|
|
|
|
/* In HS mode and when the DMA is used, all variables and data structures dealing
|
|
with the DMA during the transaction process should be 4-bytes aligned */
|
|
|
|
#if defined (__GNUC__) /* GNU Compiler */
|
|
#define __ALIGN_END __attribute__ ((aligned (4)))
|
|
#define __ALIGN_BEGIN
|
|
#else
|
|
#define __ALIGN_END
|
|
#if defined (__CC_ARM) /* ARM Compiler */
|
|
#define __ALIGN_BEGIN __align(4)
|
|
#elif defined (__ICCARM__) /* IAR Compiler */
|
|
#define __ALIGN_BEGIN
|
|
#elif defined (__TASKING__) /* TASKING Compiler */
|
|
#define __ALIGN_BEGIN __align(4)
|
|
#endif /* __CC_ARM */
|
|
#endif /* __GNUC__ */
|
|
|
|
#define STATIC_ASSERT4(COND,MSG) typedef char static_assertion_##MSG[(!!(COND))*2-1];
|
|
#define STATIC_ASSERT3(X,L) STATIC_ASSERT4(X,_at_line_##L)
|
|
#define STATIC_ASSERT2(X,L) STATIC_ASSERT3(X,L)
|
|
#define STATIC_ASSERT(X) STATIC_ASSERT2(X,__LINE__)
|
|
|
|
#if defined (__GNUC__)
|
|
#define __PACK_BEGIN
|
|
#define __PACK_END __packed
|
|
#else
|
|
#define __PACK_BEGIN __packed
|
|
#define __PACK_END
|
|
#endif
|
|
|
|
|
|
#define __CAT(x,y) x ## y
|
|
#define CAT(x,y) __CAT(x,y)
|
|
|
|
#define USB_ARRAY_DESC(...) {.bLength = 2 + sizeof((uint16_t[]){__VA_ARGS__}),\
|
|
.bDescriptorType = USB_DESC_TYPE_STRING,\
|
|
.wString = {__VA_ARGS__}}
|
|
|
|
#define USB_STRING_DESC(s) {.bLength = sizeof(CAT(L, s)),\
|
|
.bDescriptorType = USB_DESC_TYPE_STRING,\
|
|
.wString = {CAT(L, s)}}
|
|
|
|
|
|
typedef __PACK_BEGIN struct _usb_device_descriptor {
|
|
uint8_t bLength;
|
|
uint8_t bDescriptorType;
|
|
uint16_t bcdUSB;
|
|
uint8_t bDeviceClass;
|
|
uint8_t bDeviceSubClass;
|
|
uint8_t bDeviceProtocol;
|
|
uint8_t bMaxPacketSize;
|
|
uint16_t idVendor;
|
|
uint16_t idProduct;
|
|
uint16_t bcdDevice;
|
|
uint8_t iManufacturer;
|
|
uint8_t iProduct;
|
|
uint8_t iSerialNumber;
|
|
uint8_t bNumConfigurations;
|
|
} __PACK_END usb_device_descriptor_t;
|
|
|
|
STATIC_ASSERT(sizeof(usb_device_descriptor_t) == USB_LEN_DEV_DESC)
|
|
|
|
|
|
typedef __PACK_BEGIN struct _usb_device_qualifier_descriptor {
|
|
uint8_t bLength;
|
|
uint8_t bDescriptorType;
|
|
uint16_t bcdUSB;
|
|
uint8_t bDeviceClass;
|
|
uint8_t bDeviceSubClass;
|
|
uint8_t bDeviceProtocol;
|
|
uint8_t bMaxPacketSize;
|
|
uint8_t bNumConfigurations;
|
|
uint8_t bReserved;
|
|
} __PACK_END usb_device_qualifier_descriptor_t;
|
|
|
|
STATIC_ASSERT(sizeof(usb_device_qualifier_descriptor_t) == USB_LEN_DEV_QUALIFIER_DESC)
|
|
|
|
|
|
|
|
typedef __PACK_BEGIN struct _usb_config_descriptor {
|
|
uint8_t bLength;
|
|
uint8_t bDescriptorType;
|
|
uint16_t wTotalLength;
|
|
uint8_t bNumInterfaces;
|
|
uint8_t bConfigurationValue;
|
|
uint8_t iConfiguration;
|
|
uint8_t bmAttributes;
|
|
uint8_t bMaxPower;
|
|
} __PACK_END usb_config_descriptor_t;
|
|
|
|
STATIC_ASSERT(sizeof(usb_config_descriptor_t) == USB_LEN_CFG_DESC)
|
|
|
|
typedef __PACK_BEGIN struct _usb_interface_descriptor {
|
|
uint8_t bLength;
|
|
uint8_t bDescriptorType;
|
|
uint8_t bInterfaceNumber;
|
|
uint8_t bAlternateSetting;
|
|
uint8_t bNumEndpoints;
|
|
uint8_t bInterfaceClass;
|
|
uint8_t bInterfaceSubClass;
|
|
uint8_t bInterfaceProtocol;
|
|
uint8_t iInterface;
|
|
} __PACK_END usb_interface_descriptor_t;
|
|
|
|
STATIC_ASSERT(sizeof(usb_interface_descriptor_t) == USB_LEN_IF_DESC)
|
|
|
|
typedef __PACK_BEGIN struct _usb_endpoint_descriptor {
|
|
uint8_t bLength;
|
|
uint8_t bDescriptorType;
|
|
uint8_t bEndpointAddress;
|
|
uint8_t bmAttributes;
|
|
uint16_t wMaxPacketSize;
|
|
uint8_t bInterval;
|
|
} __PACK_END usb_endpoint_descriptor_t;
|
|
|
|
STATIC_ASSERT(sizeof(usb_endpoint_descriptor_t) == USB_LEN_EP_DESC)
|
|
|
|
typedef __PACK_BEGIN struct _usb_string_descriptor {
|
|
uint8_t bLength;
|
|
uint8_t bDescriptorType;
|
|
uint16_t wString[];
|
|
} __PACK_END usb_string_descriptor_t;
|
|
|
|
typedef __PACK_BEGIN struct _usb_interface_association_descriptor {
|
|
uint8_t bLength;
|
|
uint8_t bDescriptorType;
|
|
uint8_t bFirstInterface;
|
|
uint8_t bInterfaceCount;
|
|
uint8_t bFunctionClass;
|
|
uint8_t bFunctionSubClass;
|
|
uint8_t bFunctionProtocol;
|
|
uint8_t iFunction;
|
|
} __PACK_END usb_interface_association_descriptor_t;
|
|
|
|
STATIC_ASSERT(sizeof(usb_interface_association_descriptor_t) == USB_LEN_IF_ASSOC_DESC)
|
|
|
|
typedef __PACK_BEGIN struct _usb_function_descriptor {
|
|
uint8_t bLength;
|
|
uint8_t bDescriptorType;
|
|
uint8_t bDescriptorSubtype;
|
|
} __PACK_END usb_function_descriptor_t;
|
|
|
|
STATIC_ASSERT(sizeof(usb_function_descriptor_t) == USB_LEN_FUNCTION_DESC)
|
|
|
|
#endif
|