embkernel
 All Classes Functions Variables Typedefs Groups Pages
UsbHw.hpp
1 //------------------------------------------------------------------------------
2 //This file is part of embKernel.
3 //See license.txt for the full license governing this code.
4 //------------------------------------------------------------------------------
5 
6 #ifndef USB_HW_HPP_
7 #define USB_HW_HPP_
8 
9 #include "UsbDefs.hpp"
10 #include "UsbCfg.hpp"
11 #include "RtosInterrupt.hpp"
12 
13 class UsbHw {
14  friend class UsbDevice;
15  friend class RtosInterrupt;
16 private:
17  static UsbHw* sInstance;
18 
19  struct USB_REGISTERS {
20  struct {
21  union {
22  struct {
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;
33  } bits;
34  volatile uint16_t value;
35  };
36  volatile uint16_t reserved;
37  } EPnR[8];
38  uint32_t reserver1[8];
39  volatile uint16_t CNTR;
40  uint16_t reserver2;
41  volatile uint16_t ISTR;
42  uint16_t reserved3;
43  volatile uint16_t FNR;
44  uint16_t reserved4;
45  volatile uint16_t DADDR;
46  uint16_t reserved5;
47  volatile uint16_t BTABLE;
48  uint16_t reserved6;
49  }__attribute__((packed)) *USB_DEVICE = (USB_REGISTERS*) 0x40005C00;
50 
51  const static uint16_t REG_CNTR_FRES = ((uint16_t) 0x0001); //Force USB Reset
52  const static uint16_t REG_CNTR_PDWN = ((uint16_t) 0x0002); //Power down
53  const static uint16_t REG_CNTR_LP_MODE = ((uint16_t) 0x0004); //Low-power mode
54  const static uint16_t REG_CNTR_FSUSP = ((uint16_t) 0x0008); //Force suspend
55  const static uint16_t REG_CNTR_RESUME = ((uint16_t) 0x0010); //Resume request
56  const static uint16_t REG_CNTR_ESOFM = ((uint16_t) 0x0100); //Expected Start Of Frame Interrupt Mask
57  const static uint16_t REG_CNTR_SOFM = ((uint16_t) 0x0200); //Start Of Frame Interrupt Mask
58  const static uint16_t REG_CNTR_RESETM = ((uint16_t) 0x0400); //RESET Interrupt Mask
59  const static uint16_t REG_CNTR_SUSPM = ((uint16_t) 0x0800); //Suspend mode Interrupt Mask
60  const static uint16_t REG_CNTR_WKUPM = ((uint16_t) 0x1000); //Wakeup Interrupt Mask
61  const static uint16_t REG_CNTR_ERRM = ((uint16_t) 0x2000); //Error Interrupt Mask
62  const static uint16_t REG_CNTR_PMAOVRM = ((uint16_t) 0x4000); //Packet Memory Area Over / Underrun Interrupt Mask
63  const static uint16_t REG_CNTR_CTRM = ((uint16_t) 0x8000); //Correct Transfer Interrupt Mask
64 
65  const static uint16_t REG_ISTR_EP_ID = ((uint16_t) 0x000F); //Endpoint Identifier
66  const static uint16_t REG_ISTR_DIR = ((uint16_t) 0x0010); //Direction of transaction
67  const static uint16_t REG_ISTR_ESOF = ((uint16_t) 0x0100); //Expected Start Of Frame
68  const static uint16_t REG_ISTR_SOF = ((uint16_t) 0x0200); //Start Of Frame
69  const static uint16_t REG_ISTR_RESET = ((uint16_t) 0x0400); //USB RESET request */
70  const static uint16_t REG_ISTR_SUSP = ((uint16_t) 0x0800); //Suspend mode request
71  const static uint16_t REG_ISTR_WKUP = ((uint16_t) 0x1000); //Wake up
72  const static uint16_t REG_ISTR_ERR = ((uint16_t) 0x2000); //Error
73  const static uint16_t REG_ISTR_PMAOVR = ((uint16_t) 0x4000); //Packet Memory Area Over / Underrun
74  const static uint16_t REG_ISTR_CTR = ((uint16_t) 0x8000); //Correct Transfer
75 
76  const static uint16_t REG_DADDR_ADD = ((uint8_t) 0x7F); // ADD[6:0] bits (Device Address)
77  const static uint16_t REG_DADDR_EF = ((uint8_t) 0x80); // Enable Function
78 
79  const static uint16_t REG_EPXR_EA = ((uint16_t) 0x000F); //Endpoint Address
80 
81  const static uint16_t REG_EPXR_STAT_TX = ((uint16_t) 0x0030); //STAT_TX[1:0] bits (Status bits, for transmission transfers)
82  const static uint16_t REG_EPXR_STAT_TX_0 = ((uint16_t) 0x0010); //Bit 0
83  const static uint16_t REG_EPXR_STAT_TX_1 = ((uint16_t) 0x0020); //Bit 1
84 
85  const static uint16_t REG_EPXR_DTOG_TX = ((uint16_t) 0x0040); //Data Toggle, for transmission transfers
86  const static uint16_t REG_EPXR_CTR_TX = ((uint16_t) 0x0080); //Correct Transfer for transmission
87  const static uint16_t REG_EPXR_EP_KIND = ((uint16_t) 0x0100); //Endpoint Kind
88 
89  const static uint16_t REG_EPXR_EP_TYPE = ((uint16_t) 0x0600); //EP_TYPE[1:0] bits (Endpoint type)
90  const static uint16_t REG_EPXR_EP_TYPE_0 = ((uint16_t) 0x0200); //Bit 0
91  const static uint16_t REG_EPXR_EP_TYPE_1 = ((uint16_t) 0x0400); //Bit 1
92 
93  const static uint16_t REG_EPXR_SETUP = ((uint16_t) 0x0800); //Setup transaction completed
94 
95  const static uint16_t REG_EPXR_STAT_RX = ((uint16_t) 0x3000); //STAT_RX[1:0] bits (Status bits, for reception transfers)
96  const static uint16_t REG_EPXR_STAT_RX_0 = ((uint16_t) 0x1000); //Bit 0
97  const static uint16_t REG_EPXR_STAT_RX_1 = ((uint16_t) 0x2000); //Bit 1
98 
99  const static uint16_t REG_EPXR_DTOG_RX = ((uint16_t) 0x4000); //Data Toggle, for reception transfers
100  const static uint16_t REG_EPXR_CTR_RX = ((uint16_t) 0x8000); //Correct Transfer for reception
101 
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;
106 
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;
111 
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;
116 
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];
121 
122  struct USB_TABLE_LOCATION {
123  struct {
124  volatile uint16_t ADDR_TX;
125  uint16_t reserved1;
126  volatile uint16_t COUNT_TX;
127  uint16_t reserved2;
128  volatile uint16_t ADDR_RX;
129  uint16_t reserved3;
130  volatile struct {
131  uint16_t countRx :10;
132  uint16_t numBlock :5;
133  uint16_t blSize :1;
134  } COUNT_RX;
135  uint16_t reserved4;
136  } EP[8];
137  }__attribute__((packed)) *USB_TABLE = (USB_TABLE_LOCATION*) PACKET_MEM_BASE;
138 
139  void init();
140  void reset();
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);
146 
147  void epResetAll();
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();
156 };
157 
158 #endif /* USB_HW_HPP_ */