Mistake on this page?
Report an issue in GitHub or email us
USBCDC_ECM.h
1 /*
2  * Copyright (c) 2018-2019, Arm Limited and affiliates.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef USBCDC_ECM_H
19 #define USBCDC_ECM_H
20 
21 #if defined(MBED_CONF_RTOS_PRESENT) || defined(DOXYGEN_ONLY)
22 #include "USBDescriptor.h"
23 #include "USBDevice.h"
24 #include "ByteBuffer.h"
25 #include "rtos/Mutex.h"
26 #include "EventFlags.h"
27 #include "events/EventQueue.h"
28 #include "rtos/Thread.h"
29 #include "Callback.h"
30 
31 #define MAX_PACKET_SIZE_INT (64)
32 #define MAX_PACKET_SIZE_BULK (64)
33 #define MAX_PACKET_SIZE_EP0 (64)
34 #define DEFAULT_CONFIGURATION (1)
35 
36 #define PACKET_TYPE_PROMISCUOUS (1<<0)
37 #define PACKET_TYPE_ALL_MULTICAST (1<<1)
38 #define PACKET_TYPE_DIRECTED (1<<2)
39 #define PACKET_TYPE_BROADCAST (1<<3)
40 #define PACKET_TYPE_MULTICAST (1<<4)
41 
42 /**
43  * \defgroup drivers_USBCDC_ECM USBCDC_ECM class
44  * \ingroup drivers-public-api-usb
45  * @{
46  * @note Bare metal profile: This class is not supported.
47  */
48 
49 class USBCDC_ECM: public USBDevice {
50 public:
51 
52  /**
53  * Basic constructor
54  *
55  * Construct this object optionally connecting and blocking until it is ready.
56  *
57  * @note Do not use this constructor in derived classes.
58  *
59  * @param connect_blocking true to perform a blocking connect, false to start in a disconnected state
60  * @param vendor_id Your vendor_id
61  * @param product_id Your product_id
62  * @param product_release Your product_release
63  */
64 
65  USBCDC_ECM(bool connect_blocking = true, uint16_t vendor_id = 0x0700, uint16_t product_id = 0x0101, uint16_t product_release = 0x0001);
66 
67  /**
68  * Fully featured constructor
69  *
70  * Construct this object with the supplied USBPhy and parameters. The user
71  * this object is responsible for calling connect() or init().
72  *
73  * @note Derived classes must use this constructor and call init() or
74  * connect() themselves. Derived classes should also call deinit() in
75  * their destructor. This ensures that no interrupts can occur when the
76  * object is partially constructed or destroyed.
77  *
78  * @param phy USB phy to use
79  * @param vendor_id Your vendor_id
80  * @param product_id Your product_id
81  * @param product_release Your product_release
82  */
83  USBCDC_ECM(USBPhy *phy, uint16_t vendor_id, uint16_t product_id, uint16_t product_release);
84 
85  /**
86  * Destroy this object
87  *
88  * Any classes which inherit from this class must call deinit
89  * before this destructor runs.
90  */
91  virtual ~USBCDC_ECM();
92 
93  /**
94  * Check if this class is ready
95  *
96  * @return true if configured, false otherwise
97  */
98  bool ready();
99 
100  /**
101  * Block until this device is configured
102  */
103  void wait_ready();
104 
105  /**
106  * Send a buffer
107  *
108  * This function blocks until the full contents have been sent.
109  *
110  * @param buffer buffer to be sent
111  * @param size length of the buffer
112  * @returns true if successful false if interrupted due to a state change
113  */
114  bool send(uint8_t *buffer, uint32_t size);
115 
116  /**
117  * Read from the receive buffer
118  *
119  * @param buffer buffer to fill with data
120  * @param size maximum number of bytes read
121  * @param actual a pointer to where to store the number of bytes actually received
122  */
123  void receive_nb(uint8_t *buffer, uint32_t size, uint32_t *actual);
124 
125  /**
126  * Return ethernet packet filter bitmap
127  *
128  * The Packet Filter is the inclusive OR of the bitmap
129  * D0: PACKET_TYPE_PROMISCUOUS
130  * D1: PACKET_TYPE_ALL_MULTICAST
131  * D2: PACKET_TYPE_DIRECTED
132  * D3: PACKET_TYPE_BROADCAST
133  * D4: PACKET_TYPE_MULTICAST
134  * D5-D15: Reserved (zero)
135  *
136  * @return ethernet packet filter bitmap
137  */
138  uint16_t read_packet_filter();
139 
140  /**
141  * Attach a callback for when an ethernet packet is received
142  *
143  * @param cb code to call when a packet is received
144  */
145  void attach_rx(mbed::Callback<void()> cb);
146 
147  /**
148  * Attach a callback for when a request to configure device ethernet
149  * packet filter is received
150  *
151  * @param cb code to call when a packet filter request is received
152  */
153  void attach_filter(mbed::Callback<void()> cb);
154 
155 protected:
156 
157  /*
158  * Called when USB changes state
159  *
160  * @param new_state The new state of the USBDevice
161  *
162  * Warning: Called in ISR context
163  */
164  virtual void callback_state_change(DeviceState new_state);
165 
166  /*
167  * This is used to handle extensions to standard requests
168  * and class specific requests with a data phase
169  */
170  virtual void callback_request_xfer_done(const setup_packet_t *setup, bool aborted);
171 
172  /*
173  * Called by USBDevice layer. Set configuration of the device.
174  * For instance, you can add all endpoints that you need on this function.
175  *
176  * @param configuration Number of the configuration
177  * @returns true if class handles this request
178  */
179  virtual void callback_set_configuration(uint8_t configuration);
180 
181  /*
182  * Called by USBDevice layer in response to set_interface.
183  *
184  * Upon reception of this command endpoints of any previous interface
185  * if any must be removed with endpoint_remove and new endpoint added with
186  * endpoint_add.
187  *
188  * @param configuration Number of the configuration
189  *
190  * Warning: Called in ISR context
191  */
192  virtual void callback_set_interface(uint16_t interface, uint8_t alternate);
193 
194  /*
195  * Get device descriptor.
196  *
197  * @returns pointer to the device descriptor
198  */
199  virtual const uint8_t *device_desc();
200 
201  /*
202  * Get string product descriptor
203  *
204  * @returns pointer to the string product descriptor
205  */
206  virtual const uint8_t *string_iproduct_desc();
207 
208  /*
209  * Get string configuration descriptor
210  *
211  * @returns pointer to the string configuration descriptor
212  */
213  virtual const uint8_t *string_iconfiguration_desc();
214 
215  /*
216  * Get string serial descriptor
217  *
218  * @returns pointer to the string serial descriptor
219  */
220  virtual const uint8_t *string_iserial_desc();
221 
222  /*
223  * Get configuration descriptor
224  *
225  * @returns pointer to the configuration descriptor
226  */
227  virtual const uint8_t *configuration_desc(uint8_t index);
228 
229  /*
230  * This is used to handle extensions to standard requests
231  * and class specific requests
232  */
233  virtual void callback_request(const setup_packet_t *setup);
234 
235  /*
236  * Called by USBDevice layer on bus reset.
237  *
238  * complete_reset must be called after
239  * the device is fully reset.
240  *
241  * Warning: Called in ISR context
242  */
243  virtual void callback_reset();
244 
245  uint8_t device_descriptor[18];
246 
247 private:
248 
249  usb_ep_t _int_in;
250  usb_ep_t _bulk_in;
251  usb_ep_t _bulk_out;
252 
253  uint8_t _config_descriptor[80];
254  uint8_t _string_imac_addr[26];
255 
256  uint8_t _bulk_buf[MAX_PACKET_SIZE_BULK];
257  uint16_t _packet_filter;
258  ByteBuffer _rx_queue;
259 
260  rtos::EventFlags _flags;
261  rtos::Mutex _write_mutex;
262 
263  events::EventQueue _queue;
264  rtos::Thread _thread;
265  mbed::Callback<void()> _callback_rx;
266  mbed::Callback<void()> _callback_filter;
267 
268  void _init();
269  void _int_callback();
270  void _bulk_in_callback();
271  void _bulk_out_callback();
272  bool _notify_network_connection(uint8_t value);
273  bool _notify_connection_speed_change(uint32_t up, uint32_t down);
274  bool _write_bulk(uint8_t *buffer, uint32_t size);
275  void _notify_connect();
276 };
277 
278 /** @}*/
279 #endif // defined(MBED_CONF_RTOS_PRESENT)
280 #endif
void wait_ready()
Block until this device is configured.
The Thread class allow defining, creating, and controlling thread functions in the system...
Definition: Thread.h:92
virtual ~USBCDC_ECM()
Destroy this object.
The EventFlags class is used to control event flags or wait for event flags other threads control...
Definition: EventFlags.h:53
EventQueue.
Definition: EventQueue.h:62
uint16_t read_packet_filter()
Return ethernet packet filter bitmap.
void attach_rx(mbed::Callback< void()> cb)
Attach a callback for when an ethernet packet is received.
void receive_nb(uint8_t *buffer, uint32_t size, uint32_t *actual)
Read from the receive buffer.
virtual void callback_state_change(DeviceState new_state)
Called when USB changes state.
bool ready()
Check if this class is ready.
void attach_filter(mbed::Callback< void()> cb)
Attach a callback for when a request to configure device ethernet packet filter is received...
Abstract interface to physical USB hardware.
Definition: USBPhy.h:82
virtual void callback_reset()
Called by USBDevice layer on bus reset.
The Mutex class is used to synchronize the execution of threads.
Definition: Mutex.h:70
Core USB Device driver.
Definition: USBDevice.h:38
USBCDC_ECM(bool connect_blocking=true, uint16_t vendor_id=0x0700, uint16_t product_id=0x0101, uint16_t product_release=0x0001)
Basic constructor.
virtual void callback_request_xfer_done(const setup_packet_t *setup, bool aborted)
Called by USBDevice on data stage completion.
Callback class based on template specialization.
Definition: Callback.h:53
bool send(uint8_t *buffer, uint32_t size)
Send a buffer.
virtual void callback_request(const setup_packet_t *setup)
Called by USBDevice on Endpoint0 request.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.