📄 drvusb.h
字号:
/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2009 Nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
#ifndef _DRVUSB_H
#define _DRVUSB_H
#include "NUC1xx.h"
#ifdef __cplusplus
extern "C"
{
#endif
/*---------------------------------------------------------------------------------------------------------*/
/* Macro, type and constant definitions */
/*---------------------------------------------------------------------------------------------------------*/
#define DRVUSB_MAJOR_NUM 1
#define DRVUSB_MINOR_NUM 2
#define DRVUSB_BUILD_NUM 2
/* Define module version number */
#define DRVUSB_VERSION_NUM _SYSINFRA_VERSION(DRVUSB_MAJOR_NUM, DRVUSB_MINOR_NUM, DRVUSB_BUILD_NUM)
//E_DRVUSB_NULL_POINTER Callback function is NULL.
//E_DRVUSB_INVALID_EP_NUM EndPoint number error.
//E_DRVUSB_SIZE_TOO_LONG Data size is too long.
//E_DRVUSB_VALUE_INVALID Input argument is invalid.
#define E_DRVUSB_NULL_POINTER _SYSINFRA_ERRCODE(TRUE, MODULE_ID_DRVUSB, 1)
#define E_DRVUSB_INVALID_EP_NUM _SYSINFRA_ERRCODE(TRUE, MODULE_ID_DRVUSB, 2)
#define E_DRVUSB_SIZE_TOO_LONG _SYSINFRA_ERRCODE(TRUE, MODULE_ID_DRVUSB, 3)
#define E_DRVUSB_VALUE_INVALID _SYSINFRA_ERRCODE(TRUE, MODULE_ID_DRVUSB, 4)
/* Define USB Register Address */
#define USBD_INTEN (USBD_BASE + 0x00)
#define USBD_INTSTS (USBD_BASE + 0x04)
#define USBD_FADDR (USBD_BASE + 0x08)
#define USBD_EPSTS (USBD_BASE + 0x0C)
#define USBD_ATTR (USBD_BASE + 0x10)
#define USBD_FLDET (USBD_BASE + 0x14)
#define USBD_BUFSEG (USBD_BASE + 0x18)
#define USBD_BUFSEG0 (USBD_BASE + 0x20)
#define USBD_MXPLD0 (USBD_BASE + 0x24)
#define USBD_CFG0 (USBD_BASE + 0x28)
#define USBD_CFGP0 (USBD_BASE + 0x2C)
#define USBD_BUFSEG1 (USBD_BASE + 0x30)
#define USBD_MXPLD1 (USBD_BASE + 0x34)
#define USBD_CFG1 (USBD_BASE + 0x38)
#define USBD_CFGP1 (USBD_BASE + 0x3C)
#define USBD_BUFSEG2 (USBD_BASE + 0x40)
#define USBD_MXPLD2 (USBD_BASE + 0x44)
#define USBD_CFG2 (USBD_BASE + 0x48)
#define USBD_CFGP2 (USBD_BASE + 0x4C)
#define USBD_BUFSEG3 (USBD_BASE + 0x50)
#define USBD_MXPLD3 (USBD_BASE + 0x54)
#define USBD_CFG3 (USBD_BASE + 0x58)
#define USBD_CFGP3 (USBD_BASE + 0x5C)
#define USBD_BUFSEG4 (USBD_BASE + 0x60)
#define USBD_MXPLD4 (USBD_BASE + 0x64)
#define USBD_CFG4 (USBD_BASE + 0x68)
#define USBD_CFGP4 (USBD_BASE + 0x6C)
#define USBD_BUFSEG5 (USBD_BASE + 0x70)
#define USBD_MXPLD5 (USBD_BASE + 0x74)
#define USBD_CFG5 (USBD_BASE + 0x78)
#define USBD_CFGP5 (USBD_BASE + 0x7C)
#define USBD_DRVSE0 (USBD_BASE + 0x90)
#define USB_SRAM_BASE (USBD_BASE + 0x100)
#define INTEN_INNAK 0x00008000
#define INTEN_WAKEUPEN 0x00000100
#define INTEN_WAKEUP 0x00000008
#define INTEN_FLDET 0x00000004
#define INTEN_USB 0x00000002
#define INTEN_BUS 0x00000001
#define INTSTS_SETUP 0x80000000
#define INTSTS_EPTF5 0x00200000
#define INTSTS_EPTF4 0x00100000
#define INTSTS_EPTF3 0x00080000
#define INTSTS_EPTF2 0x00040000
#define INTSTS_EPTF1 0x00020000
#define INTSTS_EPTF0 0x00010000
#define INTSTS_WAKEUP 0x00000008
#define INTSTS_FLDET 0x00000004
#define INTSTS_USB 0x00000002
#define INTSTS_BUS 0x00000001
#define ATTR_BYTEM 0x00000400
#define ATTR_PWRDN 0x00000200
#define ATTR_DPPU_EN 0x00000100
#define ATTR_USB_EN 0x00000080
#define ATTR_RWAKEUP 0x00000020
#define ATTR_PHY_EN 0x00000010
#define ATTR_TIMEOUT 0x00000008
#define ATTR_RESUME 0x00000004
#define ATTR_SUSPEND 0x00000002
#define ATTR_USBRST 0x00000001
#define CFG_CSTALL 0x00000200
#define CFG_DSQ_SYNC 0x00000080
#define CFG_STATE 0x00000060
#define CFG_EPT_IN 0x00000040 /* 10: IN endpoint */
#define CFG_EPT_OUT 0x00000020 /* 01: Out endpoint */
#define CFG_ISOCH 0x00000010
#define CFG_EPT 0x0000000F
#define CFGP_SSTALL 0x00000002
#define CFGP_CLRRDY 0x00000001
#define EPSTS_IN_ACK 0x00 /* 000: In ACK */
#define EPSTS_IN_NAK 0x10 /* 001: In NAK */
#define EPSTS_OUT0_ACK 0x20 /* 010: Out 0 ACK */
#define EPSTS_OUT1_ACK 0x60 /* 110: Out 1 ACK */
#define EPSTS_SETUP_ACK 0x30 /* 011: Setup ACK */
#define EPSTS_ISO 0x70 /* 111: Isochronous translation end */
#define EPSTS_STS 0x70 /* A mask to get EP state */
#define _DRVUSB_DATA0(Data) ((Data) & 0x37F)
#define _DRVUSB_DATA1(Data) ((Data) | 0x80)
#define _DRVUSB_ENABLE_MISC_INT(u32Flags) outp32(USBD_INTEN, (u32Flags) & (INTEN_WAKEUP + INTEN_FLDET + INTEN_USB + INTEN_BUS))
#define _DRVUSB_ENABLE_WAKEUP() USBD->INTEN.WAKEUP_EN = 1
#define _DRVUSB_DISABLE_WAKEUP() USBD->INTEN.WAKEUP_EN = 0
#define _DRVUSB_ENABLE_WAKEUP_INT() USBD->INTEN.WAKEUP_IE = 1
#define _DRVUSB_DISABLE_WAKEUP_INT() USBD->INTEN.WAKEUP_IE = 0
#define _DRVUSB_ENABLE_FLDET_INT() USBD->INTEN.FLDET_IE = 1
#define _DRVUSB_DISABLE_FLDET_INT() USBD->INTEN.FLDET_IE = 0
#define _DRVUSB_ENABLE_USB_INT() USBD->INTEN.USB_IE = 1
#define _DRVUSB_DISABLE_USB_INT() USBD->INTEN.USB_IE = 0
#define _DRVUSB_ENABLE_BUS_INT() USBD->INTEN.BUS_IE = 1
#define _DRVUSB_DISABLE_BUS_INT() USBD->INTEN.BUS_IE = 0
/* Respond EPx (x = 0 ~ 5) STALL packet */
#define _DRVUSB_CLEAR_EP_READY_AND_TRIG_STALL(u32EpId) outp32(USBD_CFGP0+(0x10*(u32EpId)), (inp32(USBD_CFGP0+(0x10*(u32EpId))) | (CFGP_SSTALL | CFGP_CLRRDY)))
/* Clear EPx (x = 0 ~ 5) In/Out ready */
#define _DRVUSB_CLEAR_EP_READY(u32EpId) outp32(USBD_CFGP0+(0x10*(u32EpId)), (inp32(USBD_CFGP0+(0x10*(u32EpId))) | CFGP_CLRRDY))
/* Specify USB buffer for SETUP packet */
#define _DRVUSB_SET_SETUP_BUF(u32BufAddr) outp32(USBD_BUFSEG, (u32BufAddr) - USB_SRAM_BASE)
/* Specify USB buffer for EPx (x = 0 ~ 5) */
#define _DRVUSB_SET_EP_BUF(u32EpId, u32BufAddr) outp32(USBD_BUFSEG0+(0x10 * (u32EpId)), (u32BufAddr) - USB_SRAM_BASE)
/* Trigger next transaction for EPx (x = 0 ~ 5) */
#define _DRVUSB_TRIG_EP(u32EpId, u32TrigSize) outp32((USBD_MXPLD0+(0x10*(u32EpId))), (u32TrigSize))
/* Set toggle bit for EPx (x = 0 ~ 5) */
#define _DRVUSB_SET_EP_TOG_BIT(u32EpId, bData0) outp32(USBD_CFG0+(0x10*(u32EpId)), bData0 ? _DRVUSB_DATA0(inp32(USBD_CFG0+(0x10*(u32EpId)))) : _DRVUSB_DATA1(inp32(USBD_CFG0+(0x10*(u32EpId)))))
/* Get Data Out size for EPx (x = 0 ~ 5) */
#define _DRVUSB_GET_EP_DATA_SIZE(u32EpId) inp8(USBD_MXPLD0 + (0x10*(u32EpId)))
#define _DRVUSB_SET_EVENT_FLAG(u32Data) outp32((&USBD->INTSTS), (u32Data))
#define _DRVUSB_GET_EVENT_FLAG() inp32((&USBD->INTSTS))
/* Clear EPx (x = 0 ~ 5) Force device to response STALL */
#define _DRVUSB_CLEAR_EP_STALL(u32EpId) outp32(USBD_CFGP0+(0x10 * (u32EpId)), inp32(USBD_CFGP0+(0x10 * (u32EpId))) & ~CFGP_SSTALL)
/* Trigger EPx (x = 0 ~ 5) Force device to response STALL */
#define _DRVUSB_TRIG_EP_STALL(u32EpId) outp32(USBD_CFGP0+(0x10*(u32EpId)), inp32(USBD_CFGP0+(0x10*(u32EpId))) | CFGP_SSTALL)
/* Clear EPx (x = 0 ~ 5) Specify Data 0 or 1 after IN token toggle automatically after host ACK */
#define _DRVUSB_CLEAR_EP_DSQ_SYNC(u32EpId) outp32(USBD_CFG0+(0x10*(u32EpId)), inp32(USBD_CFG0+(0x10*(u32EpId))) & ~CFG_DSQ_SYNC)
/* Configure Set CFGx (x = 0 ~ 5) */
#define _DRVUSB_SET_CFG(u32CFGNum,u32Data) outp32(USBD_CFG0+(0x10*(u32CFGNum)), (u32Data) & 0x03FF)
/* Configure Get CFGx (x = 0 ~ 5) */
#define _DRVUSB_GET_CFG(u32CFGNum) (inp32(USBD_CFG0 + (u32CFGNum) * 0x10) & 0x03FF)
/* Set Function Address */
#define _DRVUSB_SET_FADDR(u32Addr) (USBD->FADDR.FADDR = u32Addr)
/* Get Function Address */
#define _DRVUSB_GET_FADDR() (USBD->FADDR.FADDR)
/* Get System states */
#define _DRVUSB_GET_EPSTS() inp32((&USBD->EPSTS))
/* Configure Set CFGPx (x = 0 ~ 5) */
#define _DRVUSB_SET_CFGP(u8CFGPNum, u32Data) outp32(USBD_CFGP0+(0x10 * (u8CFGPNum)), (u32Data) & 0x03)
#define _DRVUSB_SET_CFG_EP0(u32Data) outp32(USBD_CFGP0, (u32Data) & 0x03)
#define _DRVUSB_SET_CFG_EP1(u32Data) outp32(USBD_CFGP1, (u32Data) & 0x03)
#define _DRVUSB_SET_CFG_EP2(u32Data) outp32(USBD_CFGP2, (u32Data) & 0x03)
#define _DRVUSB_SET_CFG_EP3(u32Data) outp32(USBD_CFGP3, (u32Data) & 0x03)
#define _DRVUSB_SET_CFG_EP4(u32Data) outp32(USBD_CFGP4, (u32Data) & 0x03)
#define _DRVUSB_SET_CFG_EP5(u32Data) outp32(USBD_CFGP5, (u32Data) & 0x03)
/* Configure Get CFGPx (x = 0 ~ 5) */
#define _DRVUSB_GET_CFGP(u32CFGPNum) (inp32(USBD_CFGP0+(0x10 * (u32CFGPNum))) & 0x03)
/* Enable USB , PHY and use remote wake-up */
#define _DRVUSB_ENABLE_USB() outp32((&USBD->ATTR), 0x7D0)
/* Disable USB, PHY and use remote wake-up */
#define _DRVUSB_DISABLE_USB() outp32((&USBD->ATTR), 0x760)
/* Disable PHY and don't use remote wake-up */
#define _DRVUSB_DISABLE_PHY() outp32((&USBD->ATTR), 0x7C0)
#define _DRVUSB_ENABLE_SE0() (USBD->DRVSE0.DRVSE0 = 1)
#define _DRVUSB_DISABLE_SE0() (USBD->DRVSE0.DRVSE0 = 0)
/*---------------------------------------------------------------------------------------------------------*/
/* USB Request */
/*---------------------------------------------------------------------------------------------------------*/
/* Request Type */
#define REQ_STANDARD 0x00
#define REQ_CLASS 0x20
#define REQ_VENDOR 0x40
/* Request */
#define GET_STATUS 0x00
#define CLEAR_FEATURE 0x01
#define SET_FEATURE 0x03
#define SET_ADDRESS 0x05
#define GET_DESCRIPTOR 0x06
#define SET_DESCRIPTOR 0x07
#define GET_CONFIGURATION 0x08
#define SET_CONFIGURATION 0x09
#define GET_INTERFACE 0x0A
#define SET_INTERFACE 0x0B
#define SYNC_FRAME 0x0C
/*---------------------------------------------------------------------------------------------------------*/
/* USB Descriptor Type */
/*---------------------------------------------------------------------------------------------------------*/
#define DESC_DEVICE 0x01
#define DESC_CONFIG 0x02
#define DESC_STRING 0x03
#define DESC_INTERFACE 0x04
#define DESC_ENDPOINT 0x05
#define DESC_QUALIFIER 0x06
#define DESC_OTHERSPEED 0x07
/* For HID */
#define DESC_HID 0x21
#define DESC_HID_RPT 0x22
/*---------------------------------------------------------------------------------------------------------*/
/* USB Descriptor Length */
/*---------------------------------------------------------------------------------------------------------*/
#define LEN_DEVICE 18
#define LEN_CONFIG 9
#define LEN_INTERFACE 9
#define LEN_ENDPOINT 7
/* For HID */
#define LEN_HID 0x09
/* USB Endpoint Type */
#define EP_ISO 0x01
#define EP_BULK 0x02
#define EP_INT 0x03
/*---------------------------------------------------------------------------------------------------------*/
/* USB Feature Selector */
/*---------------------------------------------------------------------------------------------------------*/
#define FEATURE_DEVICE_REMOTE_WAKEUP 0x01
#define FEATURE_ENDPOINT_HALT 0x00
#define EP_INPUT 0x80
#define EP_OUTPUT 0x00
#define MAX_EP_ID 6
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -