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