µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
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
41// ----------------------------------------------------------------------------
42
45
46#include <cstdint>
47#include <cstddef>
48
49// ----------------------------------------------------------------------------
50
51#pragma GCC diagnostic push
52
53#if defined(__clang__)
54#pragma clang diagnostic ignored "-Wc++98-compat"
55#endif
56
57// ----------------------------------------------------------------------------
58
59namespace os
60{
61 namespace driver
62 {
63 namespace usb
64 {
65 namespace device
66 {
67 // ==================================================================
68
69#pragma GCC diagnostic push
70#pragma GCC diagnostic ignored "-Wpadded"
71
76 {
77 public:
78
79 // For compatibility with ARM CMSIS, these bits should be
80 // exactly in this order.
81
84
87
90
91 };
92
93#pragma GCC diagnostic pop
94
95 // ==================================================================
96 // ----- USB Device Status -----
97
98#pragma GCC diagnostic push
99#pragma GCC diagnostic ignored "-Wpadded"
100
104 class Status
105 {
106 public:
107
108 bool
109 is_vbus_on (void) const noexcept;
110
111 speed_t
112 get_speed (void) const noexcept;
113
114 bool
115 is_active (void) const noexcept;
116
117 // ----------------------------------------------------------------
118
119 public:
120
121 // For compatibility with ARM CMSIS, these bits should be
122 // exactly in this order.
123
125 bool vbus :1;
126
129
131 bool active :1;
132 };
133
134#pragma GCC diagnostic pop
135
136 // ==================================================================
137 // ----- USB Device Events -----
138
143 : event_t
144 {
145 //
146
148 vbus_on = (1UL << 0),
149
151 vbus_off = (1UL << 1),
152
154 reset = (1UL << 2),
155
157 high_speed = (1UL << 3),
158
160 suspend = (1UL << 4),
161
163 resume = (1UL << 5)
164 };
165
166 // ==================================================================
167 // ----- USB Device Endpoint Events -----
168
173 : event_t
174 {
175 //
176
178 setup = (1UL << 0),
179
181 out = (1UL << 1),
182
184 in = (1UL << 2),
185 };
186
187 // ------------------------------------------------------------------
188
189 typedef void
190 (*signal_device_event_t) (const void* object, event_t event);
191
192 typedef void
193 (*signal_endpoint_event_t) (const void* object, endpoint_t ep_addr,
194 event_t event);
195
196 } /* namespace device */
197
198 // ====================================================================
199
200#pragma GCC diagnostic push
201#pragma GCC diagnostic ignored "-Wpadded"
202
203 class Device : public Base
204 {
205
206 public:
207
208 // ------------------------------------------------------------------
209
210 Device () noexcept;
211
212 Device (const Device&) = delete;
213
214 Device (Device&&) = delete;
215
216 Device&
217 operator= (const Device&) = delete;
218
219 Device&
220 operator= (Device&&) = delete;
221
222 virtual
223 ~Device () noexcept override;
224
225 // ------------------------------------------------------------------
226
234 void
235 register_device_callback (device::signal_device_event_t cb_func,
236 const void* cb_object = nullptr) noexcept;
237
238 void
239 register_endpoint_callback (device::signal_endpoint_event_t cb_func,
240 const void* cb_object = nullptr) noexcept;
241
242 // ------------------------------------------------------------------
243
248 const device::Capabilities&
249 get_capabilities (void) noexcept;
250
256 connect (void) noexcept;
257
263 disconnect (void) noexcept;
264
269 device::Status&
270 get_status (void) noexcept;
271
277 wakeup_remote (void) noexcept;
278
285 configure_address (device_address_t dev_addr) noexcept;
286
293 read_setup_packet (uint8_t* buf) noexcept;
294
300 get_frame_number (void) noexcept;
301
313 packet_size_t ep_max_packet_size) noexcept;
314
323 unconfigure_endpoint (endpoint_t ep_addr) noexcept;
324
336 stall_endpoint (endpoint_t ep_addr, bool stall) noexcept;
337
348 transfer (endpoint_t ep_addr, uint8_t* data, std::size_t num) noexcept;
349
357 std::size_t
358 get_transfer_count (endpoint_t ep_addr) noexcept;
359
368 abort_transfer (endpoint_t ep_addr) noexcept;
369
376 void
377 signal_device_event (event_t event) noexcept;
378
388 void
389 signal_endpoint_event (endpoint_t ep_addr, event_t event) noexcept;
390
391 protected:
392
393 virtual const device::Capabilities&
394 do_get_capabilities (void) noexcept = 0;
395
396 virtual return_t
397 do_connect (void) noexcept = 0;
398
399 virtual return_t
400 do_disconnect (void) noexcept = 0;
401
402 virtual device::Status&
403 do_get_status (void) noexcept = 0;
404
405 virtual return_t
406 do_wakeup_remote (void) noexcept = 0;
407
408 virtual return_t
409 do_configure_address (device_address_t dev_addr) noexcept = 0;
410
411 virtual return_t
412 do_read_setup_packet (uint8_t* buf) noexcept = 0;
413
414 virtual frame_number_t
415 do_get_frame_number (void) noexcept = 0;
416
417 virtual return_t
419 packet_size_t ep_max_packet_size) noexcept = 0;
420
421 virtual return_t
422 do_unconfigure_endpoint (endpoint_t ep_addr) noexcept = 0;
423
424 virtual return_t
425 do_stall_endpoint (endpoint_t ep_addr, bool stall) noexcept = 0;
426
427 virtual return_t
428 do_transfer (endpoint_t ep_addr, uint8_t* data, std::size_t num)
429 noexcept = 0;
430
431 virtual std::size_t
432 do_get_transfer_count (endpoint_t ep_addr) noexcept = 0;
433
434 virtual return_t
435 do_abort_transfer (endpoint_t ep_addr) noexcept = 0;
436
437 private:
438
440 device::signal_device_event_t cb_device_func_;
441
443 const void* cb_device_object_;
444
446 device::signal_endpoint_event_t cb_endpoint_func_;
447
450
451 protected:
452
453 device::Status status_;
454 };
455
456#pragma GCC diagnostic pop
457
458 // ----------------------------------------------------------------------
459 // ----- Definitions -----
460
461 namespace device
462 {
463 // --------------------------------------------------------------------
464
465 inline bool
466 Status::is_vbus_on (void) const noexcept
467 {
468 return vbus;
469 }
470
471 inline speed_t
472 Status::get_speed (void) const noexcept
473 {
474 return speed;
475 }
476
477 inline bool
478 Status::is_active (void) const noexcept
479 {
480 return active;
481 }
482
483 } /* namespace device */
484
485 // --------------------------------------------------------------------
486
487 inline const device::Capabilities&
489 {
490 return do_get_capabilities ();
491 }
492
493 inline device::Status&
494 Device::get_status (void) noexcept
495 {
496 return do_get_status ();
497 }
498
499 inline return_t
500 Device::connect (void) noexcept
501 {
502 return do_connect ();
503 }
504
505 inline return_t
506 Device::disconnect (void) noexcept
507 {
508 return do_disconnect ();
509 }
510
511 inline return_t
512 Device::wakeup_remote (void) noexcept
513 {
514 return do_wakeup_remote ();
515 }
516
517 inline return_t
518 Device::configure_address (uint8_t dev_addr) noexcept
519 {
520 return do_configure_address (dev_addr);
521 }
522
523 inline frame_number_t
525 {
526 return do_get_frame_number ();
527 }
528
529 inline return_t
531 packet_size_t ep_max_packet_size) noexcept
532 {
533 return do_configure_endpoint (ep_addr, ep_type, ep_max_packet_size);
534 }
535
536 inline return_t
538 {
539 return do_unconfigure_endpoint (ep_addr);
540 }
541
542 inline return_t
543 Device::stall_endpoint (endpoint_t ep_addr, bool stall) noexcept
544 {
545 return do_stall_endpoint (ep_addr, stall);
546 }
547
548 inline std::size_t
550 {
551 return do_get_transfer_count (ep_addr);
552 }
553
554 inline return_t
556 {
557 return do_abort_transfer (ep_addr);
558 }
559
560 } /* namespace usb */
561 } /* namespace driver */
562} /* namespace os */
563
564#pragma GCC diagnostic pop
565
566// ----------------------------------------------------------------------------
567
568#endif /* __cplusplus */
569
570// ----------------------------------------------------------------------------
571
572#endif /* CMSIS_PLUS_DRIVER_USB_DEVICE_H_ */
virtual return_t do_stall_endpoint(endpoint_t ep_addr, bool stall) noexcept=0
virtual return_t do_configure_address(device_address_t dev_addr) noexcept=0
virtual std::size_t do_get_transfer_count(endpoint_t ep_addr) noexcept=0
void signal_endpoint_event(endpoint_t ep_addr, event_t event) noexcept
Signal USB Endpoint Event.
return_t connect(void) noexcept
Connect USB Device.
Definition usb-device.h:500
virtual const device::Capabilities & do_get_capabilities(void) noexcept=0
device::Status status_
Definition usb-device.h:453
return_t unconfigure_endpoint(endpoint_t ep_addr) noexcept
Unconfigure USB Endpoint.
Definition usb-device.h:537
virtual return_t do_wakeup_remote(void) noexcept=0
virtual return_t do_unconfigure_endpoint(endpoint_t ep_addr) noexcept=0
std::size_t get_transfer_count(endpoint_t ep_addr) noexcept
Get result of USB Endpoint transfer.
Definition usb-device.h:549
virtual ~Device() noexcept override
void signal_device_event(event_t event) noexcept
Signal device events.
virtual frame_number_t do_get_frame_number(void) noexcept=0
device::signal_device_event_t cb_device_func_
Pointer to static function that implements the device callback.
Definition usb-device.h:440
return_t wakeup_remote(void) noexcept
Trigger USB Remote Wakeup.
Definition usb-device.h:512
return_t read_setup_packet(uint8_t *buf) noexcept
Read setup packet received over Control Endpoint.
const device::Capabilities & get_capabilities(void) noexcept
Get driver capabilities.
Definition usb-device.h:488
virtual return_t do_transfer(endpoint_t ep_addr, uint8_t *data, std::size_t num) noexcept=0
virtual return_t do_connect(void) noexcept=0
Device & operator=(const Device &)=delete
virtual return_t do_abort_transfer(endpoint_t ep_addr) noexcept=0
return_t transfer(endpoint_t ep_addr, uint8_t *data, std::size_t num) noexcept
Read data from or Write data to USB Endpoint.
void register_device_callback(device::signal_device_event_t cb_func, const void *cb_object=nullptr) noexcept
Register device event callback.
Device(Device &&)=delete
device::signal_endpoint_event_t cb_endpoint_func_
Pointer to static function that implements the endpoint callback.
Definition usb-device.h:446
const void * cb_endpoint_object_
Pointer to object instance associated with the endpoint callback.
Definition usb-device.h:449
virtual return_t do_configure_endpoint(endpoint_t ep_addr, Endpoint_type ep_type, packet_size_t ep_max_packet_size) noexcept=0
virtual return_t do_read_setup_packet(uint8_t *buf) noexcept=0
virtual return_t do_disconnect(void) noexcept=0
return_t configure_address(device_address_t dev_addr) noexcept
Set USB Device Address.
Definition usb-device.h:518
return_t disconnect(void) noexcept
Disconnect USB Device.
Definition usb-device.h:506
frame_number_t get_frame_number(void) noexcept
Get current USB Frame Number.
Definition usb-device.h:524
void register_endpoint_callback(device::signal_endpoint_event_t cb_func, const void *cb_object=nullptr) noexcept
return_t abort_transfer(endpoint_t ep_addr) noexcept
Abort current USB Endpoint transfer.
Definition usb-device.h:555
const void * cb_device_object_
Pointer to object instance associated with the device callback.
Definition usb-device.h:443
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:530
device::Status & get_status(void) noexcept
Get current USB Device Status.
Definition usb-device.h:494
virtual device::Status & do_get_status(void) noexcept=0
Device(const Device &)=delete
return_t stall_endpoint(endpoint_t ep_addr, bool stall) noexcept
Set/Clear Stall for USB Endpoint.
Definition usb-device.h:543
USB device driver capabilities.
Definition usb-device.h:76
bool vbus_detection
< VBUS detection
Definition usb-device.h:83
bool event_vbus_on
Signal VBUS Off event.
Definition usb-device.h:86
speed_t get_speed(void) const noexcept
Definition usb-device.h:472
speed_t speed
USB Device active flag.
Definition usb-device.h:128
bool is_vbus_on(void) const noexcept
Definition usb-device.h:466
bool is_active(void) const noexcept
Definition usb-device.h:478
bool vbus
< USB Device VBUS flag
Definition usb-device.h:125
void(* signal_endpoint_event_t)(const void *object, endpoint_t ep_addr, event_t event)
Definition usb-device.h:193
Endpoint_event
USB Device Endpoint Events.
Definition usb-device.h:174
@ setup
SETUP Packet.
Definition usb-device.h:178
Device_event
USB Device Events.
Definition usb-device.h:144
@ vbus_on
USB Device VBUS On.
Definition usb-device.h:148
@ high_speed
USB Suspend occurred.
Definition usb-device.h:157
@ suspend
USB Resume occurred.
Definition usb-device.h:160
@ reset
USB switch to High Speed occurred.
Definition usb-device.h:154
@ vbus_off
USB Reset occurred.
Definition usb-device.h:151
void(* signal_device_event_t)(const void *object, event_t event)
Definition usb-device.h:190
uint8_t speed_t
Definition usb.h:62
uint8_t endpoint_t
Definition usb.h:84
uint8_t device_address_t
Definition usb.h:122
uint16_t packet_size_t
Definition usb.h:115
uint16_t frame_number_t
Definition usb.h:120
Endpoint_type
Endpoint type.
Definition usb.h:98
int32_t return_t
Definition common.h:63
uint32_t event_t
Definition common.h:62
System namespace.
Standard std namespace.