µOS++ IIIe Reference  v6.3.15
“Perfekt ist nicht gut genug”
The third edition of µOS++, a POSIX inspired open source system, written in C++.
usb-device.h
Go to the documentation of this file.
1 /*
2  * This file is part of the µOS++ distribution.
3  * (https://github.com/micro-os-plus)
4  * Copyright (c) 2016 Liviu Ionescu.
5  * Copyright (c) 2013-2014 ARM Ltd.
6  *
7  * Permission is hereby granted, free of charge, to any person
8  * obtaining a copy of this software and associated documentation
9  * files (the "Software"), to deal in the Software without
10  * restriction, including without limitation the rights to use,
11  * copy, modify, merge, publish, distribute, sublicense, and/or
12  * sell copies of the Software, and to permit persons to whom
13  * the Software is furnished to do so, subject to the following
14  * conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26  * OTHER DEALINGS IN THE SOFTWARE.
27  */
28 
29 /*
30  * The code is inspired by ARM CMSIS Driver_USBD.h file, v2.01,
31  * and tries to remain functionally close to the CMSIS specifications.
32  */
33 
34 #ifndef CMSIS_PLUS_DRIVER_USB_DEVICE_H_
35 #define CMSIS_PLUS_DRIVER_USB_DEVICE_H_
36 
37 // ----------------------------------------------------------------------------
38 
39 #ifdef __cplusplus
40 
42 #include <cmsis-plus/driver/usb.h>
43 
44 #include <cstdint>
45 #include <cstddef>
46 
47 namespace os
48 {
49  namespace driver
50  {
51  namespace usb
52  {
53  namespace device
54  {
55  // ==================================================================
56 
57 #pragma GCC diagnostic push
58 #pragma GCC diagnostic ignored "-Wpadded"
59 
64  {
65  public:
66 
67  // For compatibility with ARM CMSIS, these bits should be
68  // exactly in this order.
69 
71  bool vbus_detection :1;
72 
74  bool event_vbus_on :1;
75 
77  bool event_vbus_off :1;
78 
79  };
80 
81 #pragma GCC diagnostic pop
82 
83  // ==================================================================
84  // ----- USB Device Status -----
85 
86 #pragma GCC diagnostic push
87 #pragma GCC diagnostic ignored "-Wpadded"
88 
92  class Status
93  {
94  public:
95 
96  bool
97  is_vbus_on (void) const noexcept;
98 
99  speed_t
100  get_speed (void) const noexcept;
101 
102  bool
103  is_active (void) const noexcept;
104 
105  // ----------------------------------------------------------------
106 
107  public:
108 
109  // For compatibility with ARM CMSIS, these bits should be
110  // exactly in this order.
111 
113  bool vbus :1;
114 
117 
119  bool active :1;
120  };
121 
122 #pragma GCC diagnostic pop
123 
124  // ==================================================================
125  // ----- USB Device Events -----
126 
131  : event_t
132  {
133  //
134 
136  vbus_on = (1UL << 0),
137 
139  vbus_off = (1UL << 1),
140 
142  reset = (1UL << 2),
143 
145  high_speed = (1UL << 3),
146 
148  suspend = (1UL << 4),
149 
151  resume = (1UL << 5)
152  };
153 
154  // ==================================================================
155  // ----- USB Device Endpoint Events -----
156 
161  : event_t
162  {
163  //
164 
166  setup = (1UL << 0),
167 
169  out = (1UL << 1),
170 
172  in = (1UL << 2),
173  };
174 
175  // ------------------------------------------------------------------
176 
177  typedef void
178  (*signal_device_event_t) (const void* object, event_t event);
179 
180  typedef void
181  (*signal_endpoint_event_t) (const void* object, endpoint_t ep_addr,
182  event_t event);
183 
184  } /* namespace device */
185 
186  // ====================================================================
187 
188 #pragma GCC diagnostic push
189 #pragma GCC diagnostic ignored "-Wpadded"
190 
191  class Device : public Base
192  {
193 
194  public:
195 
196  // ------------------------------------------------------------------
197 
198  Device () noexcept;
199 
200  Device (const Device&) = delete;
201 
202  Device (Device&&) = delete;
203 
204  Device&
205  operator= (const Device&) = delete;
206 
207  Device&
208  operator= (Device&&) = delete;
209 
210  virtual
211  ~Device () noexcept;
212 
213  // ------------------------------------------------------------------
214 
221  void
222  register_device_callback (device::signal_device_event_t cb_func,
223  const void* cb_object = nullptr) noexcept;
224 
225  void
226  register_endpoint_callback (device::signal_endpoint_event_t cb_func,
227  const void* cb_object = nullptr) noexcept;
228 
229  // ------------------------------------------------------------------
230 
235  const device::Capabilities&
236  get_capabilities (void) noexcept;
237 
242  return_t
243  connect (void) noexcept;
244 
249  return_t
250  disconnect (void) noexcept;
251 
257  get_status (void) noexcept;
258 
263  return_t
264  wakeup_remote (void) noexcept;
265 
271  return_t
272  configure_address (device_address_t dev_addr) noexcept;
273 
279  return_t
280  read_setup_packet (uint8_t* buf) noexcept;
281 
287  get_frame_number (void) noexcept;
288 
298  return_t
299  configure_endpoint (endpoint_t ep_addr, Endpoint_type ep_type,
300  packet_size_t ep_max_packet_size) noexcept;
301 
309  return_t
310  unconfigure_endpoint (endpoint_t ep_addr) noexcept;
311 
322  return_t
323  stall_endpoint (endpoint_t ep_addr, bool stall) noexcept;
324 
334  return_t
335  transfer (endpoint_t ep_addr, uint8_t* data, std::size_t num) noexcept;
336 
344  std::size_t
345  get_transfer_count (endpoint_t ep_addr) noexcept;
346 
354  return_t
355  abort_transfer (endpoint_t ep_addr) noexcept;
356 
362  void
363  signal_device_event (event_t event) noexcept;
364 
373  void
374  signal_endpoint_event (endpoint_t ep_addr, event_t event) noexcept;
375 
376  protected:
377 
378  virtual const device::Capabilities&
379  do_get_capabilities (void) noexcept = 0;
380 
381  virtual return_t
382  do_connect (void) noexcept = 0;
383 
384  virtual return_t
385  do_disconnect (void) noexcept = 0;
386 
387  virtual device::Status&
388  do_get_status (void) noexcept = 0;
389 
390  virtual return_t
391  do_wakeup_remote (void) noexcept = 0;
392 
393  virtual return_t
394  do_configure_address (device_address_t dev_addr) noexcept = 0;
395 
396  virtual return_t
397  do_read_setup_packet (uint8_t* buf) noexcept = 0;
398 
399  virtual frame_number_t
400  do_get_frame_number (void) noexcept = 0;
401 
402  virtual return_t
403  do_configure_endpoint (endpoint_t ep_addr, Endpoint_type ep_type,
404  packet_size_t ep_max_packet_size) noexcept = 0;
405 
406  virtual return_t
407  do_unconfigure_endpoint (endpoint_t ep_addr) noexcept = 0;
408 
409  virtual return_t
410  do_stall_endpoint (endpoint_t ep_addr, bool stall) noexcept = 0;
411 
412  virtual return_t
413  do_transfer (endpoint_t ep_addr, uint8_t* data, std::size_t num)
414  noexcept = 0;
415 
416  virtual std::size_t
417  do_get_transfer_count (endpoint_t ep_addr) noexcept = 0;
418 
419  virtual return_t
420  do_abort_transfer (endpoint_t ep_addr) noexcept = 0;
421 
422  private:
423 
426 
428  const void* cb_device_object_;
429 
432 
434  const void* cb_endpoint_object_;
435 
436  protected:
437 
439  };
440 
441 #pragma GCC diagnostic pop
442 
443  // ----------------------------------------------------------------------
444  // ----- Definitions -----
445 
446  namespace device
447  {
448  // --------------------------------------------------------------------
449 
450  inline bool
451  Status::is_vbus_on (void) const noexcept
452  {
453  return vbus;
454  }
455 
456  inline speed_t
457  Status::get_speed (void) const noexcept
458  {
459  return speed;
460  }
461 
462  inline bool
463  Status::is_active (void) const noexcept
464  {
465  return active;
466  }
467 
468  } /* namespace device */
469 
470  // --------------------------------------------------------------------
471 
472  inline const device::Capabilities&
473  Device::get_capabilities (void) noexcept
474  {
475  return do_get_capabilities ();
476  }
477 
478  inline device::Status&
479  Device::get_status (void) noexcept
480  {
481  return do_get_status ();
482  }
483 
484  inline return_t
485  Device::connect (void) noexcept
486  {
487  return do_connect ();
488  }
489 
490  inline return_t
491  Device::disconnect (void) noexcept
492  {
493  return do_disconnect ();
494  }
495 
496  inline return_t
497  Device::wakeup_remote (void) noexcept
498  {
499  return do_wakeup_remote ();
500  }
501 
502  inline return_t
503  Device::configure_address (uint8_t dev_addr) noexcept
504  {
505  return do_configure_address (dev_addr);
506  }
507 
508  inline frame_number_t
509  Device::get_frame_number (void) noexcept
510  {
511  return do_get_frame_number ();
512  }
513 
514  inline return_t
516  packet_size_t ep_max_packet_size) noexcept
517  {
518  return do_configure_endpoint (ep_addr, ep_type, ep_max_packet_size);
519  }
520 
521  inline return_t
523  {
524  return do_unconfigure_endpoint (ep_addr);
525  }
526 
527  inline return_t
528  Device::stall_endpoint (endpoint_t ep_addr, bool stall) noexcept
529  {
530  return do_stall_endpoint (ep_addr, stall);
531  }
532 
533  inline std::size_t
535  {
536  return do_get_transfer_count (ep_addr);
537  }
538 
539  inline return_t
541  {
542  return do_abort_transfer (ep_addr);
543  }
544 
545  } /* namespace usb */
546  } /* namespace driver */
547 } /* namespace os */
548 
549 #endif /* __cplusplus */
550 
551 // ----------------------------------------------------------------------------
552 
553 #endif /* CMSIS_PLUS_DRIVER_USB_DEVICE_H_ */
Disable Transmitter.
Definition: serial.h:208
const void * cb_endpoint_object_
Pointer to object instance associated with the endpoint callback.
Definition: usb-device.h:434
device::Status status_
Definition: usb-device.h:438
bool is_active(void) const noexcept
Definition: usb-device.h:463
return_t unconfigure_endpoint(endpoint_t ep_addr) noexcept
Unconfigure USB Endpoint.
Definition: usb-device.h:522
device::Status & get_status(void) noexcept
Get current USB Device Status.
Definition: usb-device.h:479
const void * cb_device_object_
Pointer to object instance associated with the device callback.
Definition: usb-device.h:428
void(* signal_endpoint_event_t)(const void *object, endpoint_t ep_addr, event_t event)
Definition: usb-device.h:181
USB device driver capabilities.
Definition: usb-device.h:63
System namespace.
bool event_vbus_on
Signal VBUS Off event.
Definition: usb-device.h:74
return_t configure_address(device_address_t dev_addr) noexcept
Set USB Device Address.
Definition: usb-device.h:503
speed_t get_speed(void) const noexcept
Definition: usb-device.h:457
uint8_t endpoint_t
Definition: usb.h:74
device::signal_device_event_t cb_device_func_
Pointer to static function that implements the device callback.
Definition: usb-device.h:425
bool vbus
< USB Device VBUS flag
Definition: usb-device.h:113
speed_t speed
USB Device active flag.
Definition: usb-device.h:116
uint32_t event_t
Definition: common.h:50
Endpoint_event
USB Device Endpoint Events.
Definition: usb-device.h:160
bool is_vbus_on(void) const noexcept
Definition: usb-device.h:451
USB Suspend occurred.
Definition: usb-device.h:145
Endpoint_type
Endpoint type.
Definition: usb.h:86
return_t configure_endpoint(endpoint_t ep_addr, Endpoint_type ep_type, packet_size_t ep_max_packet_size) noexcept
Configure USB Endpoint.
Definition: usb-device.h:515
return_t stall_endpoint(endpoint_t ep_addr, bool stall) noexcept
Set/Clear Stall for USB Endpoint.
Definition: usb-device.h:528
USB Device Connected to Port.
Definition: usb-host.h:141
void(* signal_device_event_t)(const void *object, event_t event)
Definition: usb-device.h:178
uint8_t device_address_t
Definition: usb.h:112
USB Device VBUS On.
Definition: usb-device.h:136
USB Device caused Overcurrent.
Definition: usb-host.h:144
return_t wakeup_remote(void) noexcept
Trigger USB Remote Wakeup.
Definition: usb-device.h:497
return_t connect(void) noexcept
Connect USB Device.
Definition: usb-device.h:485
frame_number_t get_frame_number(void) noexcept
Get current USB Frame Number.
Definition: usb-device.h:509
std::size_t get_transfer_count(endpoint_t ep_addr) noexcept
Get result of USB Endpoint transfer.
Definition: usb-device.h:534
return_t abort_transfer(endpoint_t ep_addr) noexcept
Abort current USB Endpoint transfer.
Definition: usb-device.h:540
device::signal_endpoint_event_t cb_endpoint_func_
Pointer to static function that implements the endpoint callback.
Definition: usb-device.h:431
uint8_t speed_t
Definition: usb.h:52
return_t disconnect(void) noexcept
Disconnect USB Device.
Definition: usb-device.h:491
uint16_t packet_size_t
Definition: usb.h:105
int32_t return_t
Definition: common.h:51
uint16_t frame_number_t
Definition: usb.h:110
bool vbus_detection
< VBUS detection
Definition: usb-device.h:71
USB Resume occurred.
Definition: usb-device.h:148
Device_event
USB Device Events.
Definition: usb-device.h:130
USB switch to High Speed occurred.
Definition: usb-device.h:142
const device::Capabilities & get_capabilities(void) noexcept
Get driver capabilities.
Definition: usb-device.h:473