µ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.cpp
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
36#include <cassert>
37
38// ----------------------------------------------------------------------------
39
40#if defined(__clang__)
41#pragma clang diagnostic ignored "-Wc++98-compat"
42#endif
43
44// ----------------------------------------------------------------------------
45
46namespace os
47{
48 namespace driver
49 {
50 namespace usb
51 {
52 // ----------------------------------------------------------------------
53
54 Device::Device () noexcept
55 {
56 trace::printf ("%s() %p\n", __func__, this);
57
58 cb_device_func_ = nullptr;
59 cb_device_object_ = nullptr;
60
61 cb_endpoint_func_ = nullptr;
62 cb_endpoint_object_ = nullptr;
63 }
64
65 Device::~Device () noexcept
66 {
67 trace::printf ("%s() %p\n", __func__, this);
68 }
69
70 void
72 const void* cb_object) noexcept
73 {
74 cb_device_func_ = cb_func;
75 cb_device_object_ = cb_object;
76 }
77
78 void
80 device::signal_endpoint_event_t cb_func, const void* cb_object) noexcept
81 {
82 cb_endpoint_func_ = cb_func;
83 cb_endpoint_object_ = cb_object;
84 }
85
86 // ----------------------------------------------------------------------
87
89 Device::read_setup_packet (uint8_t* buf) noexcept
90 {
91 assert (buf != nullptr);
92 return do_read_setup_packet (buf);
93 }
94
96 Device::transfer (endpoint_t ep_addr, uint8_t* data, std::size_t num) noexcept
97 {
98 assert (data != nullptr);
99 if (num == 0)
100 {
101 return RETURN_OK;
102 }
103 return do_transfer (ep_addr, data, num);
104 }
105
106 // ----------------------------------------------------------------------
107
108 void
110 {
111 if (cb_device_func_ != nullptr)
112 {
113 // Forward event to registered callback.
114 cb_device_func_ (cb_device_object_, event);
115 }
116 }
117
118 void
120 {
121 if (cb_endpoint_func_ != nullptr)
122 {
123 // Forward event to registered callback.
124 cb_endpoint_func_ (cb_endpoint_object_, ep_addr, event);
125 }
126 }
127
128 } /* namespace usb */
129 } /* namespace driver */
130} /* namespace os */
131
132// ----------------------------------------------------------------------------
void signal_endpoint_event(endpoint_t ep_addr, event_t event) noexcept
Signal USB Endpoint Event.
virtual ~Device() noexcept override
void signal_device_event(event_t event) noexcept
Signal device events.
device::signal_device_event_t cb_device_func_
Pointer to static function that implements the device callback.
Definition usb-device.h:440
return_t read_setup_packet(uint8_t *buf) noexcept
Read setup packet received over Control Endpoint.
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::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
void register_endpoint_callback(device::signal_endpoint_event_t cb_func, const void *cb_object=nullptr) noexcept
const void * cb_device_object_
Pointer to object instance associated with the device callback.
Definition usb-device.h:443
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:74
void(* signal_endpoint_event_t)(const void *object, endpoint_t ep_addr, event_t event)
Definition usb-device.h:193
void(* signal_device_event_t)(const void *object, event_t event)
Definition usb-device.h:190
uint8_t endpoint_t
Definition usb.h:84
constexpr return_t RETURN_OK
< Operation succeeded
Definition common.h:69
int32_t return_t
Definition common.h:63
uint32_t event_t
Definition common.h:62
System namespace.