embkernel
 All Classes Functions Variables Typedefs Groups Pages
UsbDevice.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_DEVICE_HPP_
7 #define USB_DEVICE_HPP_
8 
9 #include "RtosInclude.hpp"
10 #include "LibDmem.hpp"
11 #include "UsbHw.hpp"
12 #include "UsbDefs.hpp"
13 #include "UsbCfg.hpp"
14 #include "UsbInCallback.hpp"
15 #include "UsbOutCallback.hpp"
16 
17 class UsbDevice {
18  friend class UsbHw;
19 private:
20  typedef union {
21  struct {
22  uint16_t selfPowered :1;
23  uint16_t remoteWakeup :1;
24  uint16_t unused :14;
25  } bits;
26  uint16_t value;
27  } STATUS;
28 
29  static RtosTask* sTask;
30  static UsbHw* sUsbHw;
31  static RtosSemaphore sSemaEvent;
32  static RtosSemaphore sSemaLock;
33  static uint8_t sAddress;
34  static const uint8_t sDescriptor[];
35  static STATUS sStatus;
36  static size_t sEp0DataInLen;
37  static const uint8_t* sEp0DataInBuffer;
38  static bool sIsReady;
39  static UsbDefs::PKT sPkt;
40  static UsbInCallback* sInCallback[USB_CFG_MAX_EPN];
41  static UsbOutCallback* sOutCallback[USB_CFG_MAX_EPN];
42 
43  static void processEp0Setup(UsbDefs::PKT* pkt);
44  static void processEp0DataOut(UsbDefs::PKT* pkt);
45  static const uint8_t* findInsideDescriptor(UsbDefs::DESC_TYPE descType, uint8_t index);
46  static bool reqGetDeviceDescriptor(UsbDefs::DESC_TYPE descType, uint8_t index, uint16_t lang, uint16_t len);
47  static bool reqSetConfig(uint16_t index);
48 
49  static void run(RtosTask*);
50  static bool onEventInterrupt();
51  static void onDataIn(uint8_t ep, uint16_t len);
52  static void onDataOut(uint8_t ep, bool isSetup, uint16_t len);
53  static void onReset();
54 
55 public:
56  static void init(Rtos::PRIORITY priority, size_t stackSize, UsbHw* usbHw);
57  static bool isReady();
58  static void notifyDataInReadyForEp(uint8_t ep);
59  static int writeBuffer(uint8_t ep, const uint8_t* buffer, int len);
60  static void registerInCallback(uint8_t ep, UsbInCallback* cb);
61  static void registerOutCallback(uint8_t ep, UsbOutCallback* cb);
62 };
63 
64 #endif /* USB_DEVICE_HPP_ */