ClearCore Library
UsbManager.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Teknic, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 
40 #ifndef __USBMANAGER_H__
41 #define __USBMANAGER_H__
42 
43 #include <stdint.h>
44 #include <stdio.h>
45 #include <sam.h>
46 
47 // extern "C" {
48 // #include "cdcdf_acm.h"
49 // }
50 
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54 #include "hpl_usb.h"
55 #include "cdcdf_acm.h"
56 #include "cdcdf_acm_desc.h"
57 #include "hal_usb_device.h"
58 #include "hri_mclk_e53.h"
59 #include "hri_gclk_e53.h"
60 #include "hal_gpio.h"
61 #include "hri_port_e53.h"
62 #ifdef __cplusplus
63 }
64 #endif
65 namespace ClearCore {
66 
68 #ifndef USB_SERIAL_BUFFER_SIZE
69 #define USB_SERIAL_BUFFER_SIZE 64
70 #endif
71 
72 // List of all UsbStatusReg items. Will be used to generate bitfield, enums, and
73 // masks. Ensures that the three are kept up to date with each other.
74 #define USB_STATUS_REG_LIST(Func) \
75  Func(UnhandledSetupReq) \
76  Func(UnhandledDescReq) \
77  Func(UnhandledStringReq) \
78  Func(UnhandledFeatureReq) \
79  Func(FailedStandardSetup) \
80  Func(FailedClassSetup) \
81  Func(FailedDescriptor) \
82  Func(FailedTransferIn) \
83  Func(FailedTransferOut) \
84  Func(TimeoutRead) \
85  Func(TimeoutWrite) \
86  Func(TimeoutSync) \
87  Func(RamAccessError) \
88  Func(FrameNumberCrcError) \
89  Func(ReadBufferOverflow) \
90 
91 
92 // Used to populate the bit fields in a union
93 #ifdef STRUCTIFY
94 #undef STRUCTIFY
95 #endif
96 #define STRUCTIFY(item) \
97 uint32_t item : 1;
98 
99 // Used to populate the items in a macro
100 #ifdef ENUMIFY
101 #undef ENUMIFY
102 #endif
103 #define ENUMIFY(item) \
104  item,
105 
106 // Used to generate masks from a previously defined enum.
107 #ifdef MASKIFY
108 #undef MASKIFY
109 #endif
110 #define MASKIFY(item) \
111  const uint32_t item##Mask = 1UL << item ;
112 
113 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
114 
115 
116 #ifndef HIDE_FROM_DOXYGEN
117 
120 union UsbStatusRegister {
124  uint32_t reg;
128  struct {
129  USB_STATUS_REG_LIST(STRUCTIFY)
130  } bit;
131 };
132 #endif
133 
134 #ifndef HIDE_FROM_DOXYGEN
135 
141 typedef enum {
142  USB_STATUS_REG_LIST(ENUMIFY)
143  USB_STATUS_REG_FIELD_LAST_ITEM
144 } UsbStatusRegFields;
145 
146 #define UNUSED(expr) (void)(expr)
147 
148 #endif
149 
155 USB_STATUS_REG_LIST(MASKIFY)
156 
157 
158 
178 class UsbManager {
179  friend class SysManager;
180 public:
181 #ifndef HIDE_FROM_DOXYGEN
182 
185  static UsbManager &Instance();
186 #endif
187 
194  bool Speed(uint32_t bitsPerSecond);
195 
201  uint32_t Speed();
202 
208  bool PortIsOpen();
209 
210  void PortOpen();
211  void PortClose();
212 
213  void FlushInput();
214 
215  void WaitForWriteFinish();
216 
220  int32_t AvailableForRead();
221 
225  int32_t AvailableForWrite();
226 
230  int16_t CharGet();
231 
235  int16_t CharPeek();
236 
240  bool SendChar(uint8_t charToSend);
241 
245  operator bool();
246 
247  const volatile usb_cdc_control_signal_t &LineState() {
248  return m_lineState;
249  }
250 
251  UsbManager();
252 
253 private:
254 
255  void Refresh();
256 
261  void RxProc();
262 
266  void TxPump();
267  static bool CBLineStateChanged(usb_cdc_control_signal_t state);
268  static bool TxComplete(const uint8_t ep,
269  const enum usb_xfer_code rc,
270  const uint32_t count);
271  static bool RxComplete(const uint8_t ep,
272  const enum usb_xfer_code rc,
273  const uint32_t count);
274 
275  void cdc_device_acm_init(void);
276 
277 
278  // Serial Buffers
279  __attribute__((__aligned__(4))) uint8_t m_bufferIn[USB_SERIAL_BUFFER_SIZE];
280  __attribute__((__aligned__(4))) uint8_t m_bufferOut[USB_SERIAL_BUFFER_SIZE];
281 
282  __attribute__((__aligned__(4))) uint8_t m_usbReadBuf[USB_SERIAL_BUFFER_SIZE];
283  __attribute__((__aligned__(4))) uint8_t m_usbWriteBuf[USB_SERIAL_BUFFER_SIZE];
284  // Indices for head and tails of the ring buffers
285  volatile uint32_t m_inHead, m_inTail;
286  volatile uint32_t m_outHead, m_outTail;
287 
288  volatile bool m_sendActive;
289  volatile bool m_readActive;
290  usb_cdc_control_signal_t m_lineState;
291  uint8_t *m_readBufPtr;
292  uint32_t m_readBufAvail;
293 
294  bool m_portOpen;
295 
299  bool Initialize();
304  void Reset();
305  void RxCopyToRingBuf();
309  bool Connected();
310 
311 }; // UsbManager
312 
313 } // ClearCore namespace
314 #endif //__USBMANAGER_H__
Namespace to encompass the ClearCore board API.
Definition: AdcManager.h:36
USB manager Class.
Definition: UsbManager.h:178
#define USB_SERIAL_BUFFER_SIZE
Definition: UsbManager.h:69
ClearCore Board Supervisory System Manager.
Definition: SysManager.h:58