11 #include "RtosInterrupt.hpp"
14 friend class UsbDevice;
15 friend class RtosInterrupt;
17 static UsbHw* sInstance;
19 struct USB_REGISTERS {
23 volatile uint16_t ea :4;
24 volatile uint16_t statTx :2;
25 volatile uint16_t dtogTx :1;
26 volatile uint16_t ctrTx :1;
27 volatile uint16_t epKind :1;
28 volatile uint16_t epType :2;
29 volatile uint16_t setup :1;
30 volatile uint16_t statRx :2;
31 volatile uint16_t dtogRx :1;
32 volatile uint16_t ctrRx :1;
34 volatile uint16_t value;
36 volatile uint16_t reserved;
38 uint32_t reserver1[8];
39 volatile uint16_t CNTR;
41 volatile uint16_t ISTR;
43 volatile uint16_t FNR;
45 volatile uint16_t DADDR;
47 volatile uint16_t BTABLE;
49 }__attribute__((packed)) *USB_DEVICE = (USB_REGISTERS*) 0x40005C00;
51 const static uint16_t REG_CNTR_FRES = ((uint16_t) 0x0001);
52 const static uint16_t REG_CNTR_PDWN = ((uint16_t) 0x0002);
53 const static uint16_t REG_CNTR_LP_MODE = ((uint16_t) 0x0004);
54 const static uint16_t REG_CNTR_FSUSP = ((uint16_t) 0x0008);
55 const static uint16_t REG_CNTR_RESUME = ((uint16_t) 0x0010);
56 const static uint16_t REG_CNTR_ESOFM = ((uint16_t) 0x0100);
57 const static uint16_t REG_CNTR_SOFM = ((uint16_t) 0x0200);
58 const static uint16_t REG_CNTR_RESETM = ((uint16_t) 0x0400);
59 const static uint16_t REG_CNTR_SUSPM = ((uint16_t) 0x0800);
60 const static uint16_t REG_CNTR_WKUPM = ((uint16_t) 0x1000);
61 const static uint16_t REG_CNTR_ERRM = ((uint16_t) 0x2000);
62 const static uint16_t REG_CNTR_PMAOVRM = ((uint16_t) 0x4000);
63 const static uint16_t REG_CNTR_CTRM = ((uint16_t) 0x8000);
65 const static uint16_t REG_ISTR_EP_ID = ((uint16_t) 0x000F);
66 const static uint16_t REG_ISTR_DIR = ((uint16_t) 0x0010);
67 const static uint16_t REG_ISTR_ESOF = ((uint16_t) 0x0100);
68 const static uint16_t REG_ISTR_SOF = ((uint16_t) 0x0200);
69 const static uint16_t REG_ISTR_RESET = ((uint16_t) 0x0400);
70 const static uint16_t REG_ISTR_SUSP = ((uint16_t) 0x0800);
71 const static uint16_t REG_ISTR_WKUP = ((uint16_t) 0x1000);
72 const static uint16_t REG_ISTR_ERR = ((uint16_t) 0x2000);
73 const static uint16_t REG_ISTR_PMAOVR = ((uint16_t) 0x4000);
74 const static uint16_t REG_ISTR_CTR = ((uint16_t) 0x8000);
76 const static uint16_t REG_DADDR_ADD = ((uint8_t) 0x7F);
77 const static uint16_t REG_DADDR_EF = ((uint8_t) 0x80);
79 const static uint16_t REG_EPXR_EA = ((uint16_t) 0x000F);
81 const static uint16_t REG_EPXR_STAT_TX = ((uint16_t) 0x0030);
82 const static uint16_t REG_EPXR_STAT_TX_0 = ((uint16_t) 0x0010);
83 const static uint16_t REG_EPXR_STAT_TX_1 = ((uint16_t) 0x0020);
85 const static uint16_t REG_EPXR_DTOG_TX = ((uint16_t) 0x0040);
86 const static uint16_t REG_EPXR_CTR_TX = ((uint16_t) 0x0080);
87 const static uint16_t REG_EPXR_EP_KIND = ((uint16_t) 0x0100);
89 const static uint16_t REG_EPXR_EP_TYPE = ((uint16_t) 0x0600);
90 const static uint16_t REG_EPXR_EP_TYPE_0 = ((uint16_t) 0x0200);
91 const static uint16_t REG_EPXR_EP_TYPE_1 = ((uint16_t) 0x0400);
93 const static uint16_t REG_EPXR_SETUP = ((uint16_t) 0x0800);
95 const static uint16_t REG_EPXR_STAT_RX = ((uint16_t) 0x3000);
96 const static uint16_t REG_EPXR_STAT_RX_0 = ((uint16_t) 0x1000);
97 const static uint16_t REG_EPXR_STAT_RX_1 = ((uint16_t) 0x2000);
99 const static uint16_t REG_EPXR_DTOG_RX = ((uint16_t) 0x4000);
100 const static uint16_t REG_EPXR_CTR_RX = ((uint16_t) 0x8000);
102 const static uint16_t EP_TYPE_BULK = 0;
103 const static uint16_t EP_TYPE_CONTROL = REG_EPXR_EP_TYPE_0;
104 const static uint16_t EP_TYPE_ISO = REG_EPXR_EP_TYPE_1;
105 const static uint16_t EP_TYPE_INTERRUPT = REG_EPXR_EP_TYPE_1 | REG_EPXR_EP_TYPE_0;
107 const static uint16_t EP_STAT_RX_DISABLED = 0;
108 const static uint16_t EP_STAT_RX_STALL = REG_EPXR_STAT_RX_0;
109 const static uint16_t EP_STAT_RX_NAK = REG_EPXR_STAT_RX_1;
110 const static uint16_t EP_STAT_RX_VALID = REG_EPXR_STAT_RX_1 | REG_EPXR_STAT_RX_0;
112 const static uint16_t EP_STAT_TX_DISABLED = 0;
113 const static uint16_t EP_STAT_TX_STALL = REG_EPXR_STAT_TX_0;
114 const static uint16_t EP_STAT_TX_NAK = REG_EPXR_STAT_TX_1;
115 const static uint16_t EP_STAT_TX_VALID = REG_EPXR_STAT_TX_1 | REG_EPXR_STAT_TX_0;
117 const static uint32_t PACKET_MEM_BASE = 0x40006000;
118 const static size_t PACKET_MEM_SIZE = 0x200;
119 uint32_t mPacketMemPosition;
120 uint16_t mMaxInPktSize[USB_CFG_MAX_EPN];
122 struct USB_TABLE_LOCATION {
124 volatile uint16_t ADDR_TX;
126 volatile uint16_t COUNT_TX;
128 volatile uint16_t ADDR_RX;
131 uint16_t countRx :10;
132 uint16_t numBlock :5;
137 }__attribute__((packed)) *USB_TABLE = (USB_TABLE_LOCATION*) PACKET_MEM_BASE;
141 void setAddress(
int address);
142 void processEvents();
143 uint16_t readEp(uint8_t ep, uint8_t* dst, uint16_t len);
144 uint16_t writeEp(uint8_t ep,
const uint8_t* src, uint16_t len);
145 uint16_t getEpInFree(uint8_t ep);
148 void epConfig(
const UsbDefs::USB_DESC_EP* epDesc);
149 void setEpRxStatus(uint8_t ep, uint16_t status);
150 void setEpTxStatus(uint8_t ep, uint16_t status);
151 void enableInterrupts();
152 void disableInterrupts();
153 void writePktMem(
const uint8_t *src, uint16_t dstAddr, uint16_t len);
154 void readPktMem(uint8_t *dst, uint16_t addr, uint16_t len);
155 inline void onEvent();