Mistake on this page?
Report an issue in GitHub or email us
USBCDC.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_H
19 #define USBCDC_H
20 
21 /* These headers are included for child class. */
22 #include "USBDescriptor.h"
23 #include "USBDevice_Types.h"
24 
25 #include "USBDevice.h"
26 #include "OperationList.h"
27 
28 class AsyncOp;
29 
30 class USBCDC: public USBDevice {
31 public:
32 
33  /**
34  * Basic constructor
35  *
36  * Construct this object optionally connecting and blocking until it is ready.
37  *
38  * @note Do not use this constructor in derived classes.
39  *
40  * @param connect_blocking true to perform a blocking connect, false to start in a disconnected state
41  * @param vendor_id Your vendor_id
42  * @param product_id Your product_id
43  * @param product_release Your product_release
44  */
45  USBCDC(bool connect_blocking = true, uint16_t vendor_id = 0x1f00, uint16_t product_id = 0x2012, uint16_t product_release = 0x0001);
46 
47  /**
48  * Fully featured constructor
49  *
50  * Construct this object with the supplied USBPhy and parameters. The user
51  * this object is responsible for calling connect() or init().
52  *
53  * @note Derived classes must use this constructor and call init() or
54  * connect() themselves. Derived classes should also call deinit() in
55  * their destructor. This ensures that no interrupts can occur when the
56  * object is partially constructed or destroyed.
57  *
58  * @param phy USB phy to use
59  * @param vendor_id Your vendor_id
60  * @param product_id Your product_id
61  * @param product_release Your product_release
62  */
63  USBCDC(USBPhy *phy, uint16_t vendor_id, uint16_t product_id, uint16_t product_release);
64 
65  /**
66  * Destroy this object
67  *
68  * Any classes which inherit from this class must call deinit
69  * before this destructor runs.
70  */
71  virtual ~USBCDC();
72 
73  /**
74  * Check if this class is ready
75  *
76  * @return true if a terminal is connected, false otherwise
77  */
78  bool ready();
79 
80  /**
81  * Block until the terminal is connected
82  */
83  void wait_ready();
84 
85  /*
86  * Send a buffer
87  *
88  * This function blocks until the full contents have been sent.
89  *
90  * @param buffer buffer to be sent
91  * @param size length of the buffer
92  * @returns true if successful false if interrupted due to a state change
93  */
94  bool send(uint8_t *buffer, uint32_t size);
95 
96  /**
97  * Send what there is room for
98  *
99  * @param buffer data to send
100  * @param size maximum number of bytes to send
101  * @param actual a pointer to where to store the number of bytes sent
102  * @param now true to start data transmission, false to wait
103  */
104  void send_nb(uint8_t *buffer, uint32_t size, uint32_t *actual, bool now = true);
105 
106  /*
107  * Read a buffer from a certain endpoint. Warning: blocking
108  *
109  * Blocks until at least one byte of data has been read (actual != NULL) or
110  * until the full size has been read (actual == NULL).
111  *
112  * @param buffer buffer where will be stored bytes
113  * @param size the maximum number of bytes to read
114  * @param actual A pointer to where to store the number of bytes actually read
115  * or NULL to read the full size
116  * @returns true if successful false if interrupted due to a state change
117  */
118  bool receive(uint8_t *buffer, uint32_t size, uint32_t *actual = NULL);
119 
120  /**
121  * Read from the receive buffer
122  *
123  * @param buffer buffer to fill with data
124  * @param size maximum number of bytes read
125  * @param actual a pointer to where to store the number of bytes actually received
126  */
127  void receive_nb(uint8_t *buffer, uint32_t size, uint32_t *actual);
128 
129 protected:
130  /*
131  * Get device descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
132  *
133  * @returns pointer to the device descriptor
134  */
135  virtual const uint8_t *device_desc();
136 
137  /*
138  * Get string product descriptor
139  *
140  * @returns pointer to the string product descriptor
141  */
142  virtual const uint8_t *string_iproduct_desc();
143 
144  /*
145  * Get string interface descriptor
146  *
147  * @returns pointer to the string interface descriptor
148  */
149  virtual const uint8_t *string_iinterface_desc();
150 
151  /*
152  * Get configuration descriptor
153  *
154  * @param index descriptor index
155  * @returns pointer to the configuration descriptor
156  */
157  virtual const uint8_t *configuration_desc(uint8_t index);
158 
159  /*
160  * Called by USBCallback_requestCompleted when CDC line coding is changed
161  * Warning: Called in ISR
162  *
163  * @param baud The baud rate
164  * @param bits The number of bits in a word (5-8)
165  * @param parity The parity
166  * @param stop The number of stop bits (1 or 2)
167  */
168  virtual void line_coding_changed(int baud, int bits, int parity, int stop) {};
169 
170  /*
171  * Called when there is data that can be read
172  */
173  virtual void data_rx() {}
174 
175  /*
176  * Called when there is space in the TX buffer
177  */
178  virtual void data_tx() {}
179 
180 protected:
181 
182  class AsyncWrite;
183  class AsyncRead;
184  class AsyncWait;
185 
186  virtual void callback_reset();
187  virtual void callback_state_change(DeviceState new_state);
188  virtual void callback_request(const setup_packet_t *setup);
189  virtual void callback_request_xfer_done(const setup_packet_t *setup, bool aborted);
190  virtual void callback_set_configuration(uint8_t configuration);
191  virtual void callback_set_interface(uint16_t interface, uint8_t alternate);
192 
193  void _init();
194 
195  void _change_terminal_connected(bool connected);
196 
197  void _send_isr_start();
198  void _send_isr();
199 
200  void _receive_isr_start();
201  void _receive_isr();
202 
203  usb_ep_t _bulk_in;
204  usb_ep_t _bulk_out;
205  usb_ep_t _int_in;
206 
207  uint8_t _cdc_line_coding[7];
208  uint8_t _cdc_new_line_coding[7];
209  uint8_t _config_descriptor[75];
210 
211  OperationList<AsyncWait> _connected_list;
212  bool _terminal_connected;
213 
214  OperationList<AsyncWrite> _tx_list;
215  bool _tx_in_progress;
216  uint8_t _tx_buffer[64];
217  uint8_t *_tx_buf;
218  uint32_t _tx_size;
219 
220  OperationList<AsyncRead> _rx_list;
221  bool _rx_in_progress;
222  uint8_t _rx_buffer[64];
223  uint8_t *_rx_buf;
224  uint32_t _rx_size;
225 };
226 
227 #endif
virtual ~USBCDC()
Destroy this object.
Definition: USBCDC.h:30
void receive_nb(uint8_t *buffer, uint32_t size, uint32_t *actual)
Read from the receive buffer.
Abstract interface to physical USB hardware.
Definition: USBPhy.h:82
virtual void callback_request(const setup_packet_t *setup)
Called by USBDevice on Endpoint0 request.
bool ready()
Check if this class is ready.
virtual void callback_state_change(DeviceState new_state)
Called when USB changes state.
virtual void callback_reset()
Called by USBDevice layer on bus reset.
void send_nb(uint8_t *buffer, uint32_t size, uint32_t *actual, bool now=true)
Send what there is room for.
Core USB Device driver.
Definition: USBDevice.h:44
USBCDC(bool connect_blocking=true, uint16_t vendor_id=0x1f00, uint16_t product_id=0x2012, 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.
void wait_ready()
Block until the terminal is connected.
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.