Mistake on this page?
Report an issue in GitHub or email us
USBSerial.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 USBSERIAL_H
19 #define USBSERIAL_H
20 
21 #include "USBCDC.h"
22 #include "platform/Stream.h"
23 #include "Callback.h"
24 
25 /**
26  * \defgroup drivers_USBSerial USBSerial class
27  * \ingroup drivers-public-api-usb
28  * @{
29  */
30 
31 /**
32 * USBSerial example
33 *
34 * @code
35 * #include "mbed.h"
36 * #include "USBSerial.h"
37 *
38 * //Virtual serial port over USB
39 * USBSerial serial;
40 *
41 * int main(void) {
42 *
43 * while(1)
44 * {
45 * serial.printf("I am a virtual serial port\n");
46 * wait(1);
47 * }
48 * }
49 * @endcode
50 */
51 class USBSerial: public USBCDC, public mbed::Stream {
52 public:
53 
54  /**
55  * Basic constructor
56  *
57  * Construct this object optionally connecting and blocking until it is ready.
58  *
59  * @note Do not use this constructor in derived classes.
60  *
61  * @param connect_blocking true to perform a blocking connect, false to start in a disconnected state
62  * @param vendor_id Your vendor_id (default: 0x1f00)
63  * @param product_id Your product_id (default: 0x2012)
64  * @param product_release Your product_release (default: 0x0001)
65  *
66  */
67  USBSerial(bool connect_blocking = true, uint16_t vendor_id = 0x1f00, uint16_t product_id = 0x2012, uint16_t product_release = 0x0001);
68 
69  /**
70  * Fully featured constructor
71  *
72  * Construct this object with the supplied USBPhy and parameters. The user
73  * this object is responsible for calling connect() or init().
74  *
75  * @note Derived classes must use this constructor and call init() or
76  * connect() themselves. Derived classes should also call deinit() in
77  * their destructor. This ensures that no interrupts can occur when the
78  * object is partially constructed or destroyed.
79  *
80  * @param phy USB phy to use
81  * @param vendor_id Your vendor_id (default: 0x1f00)
82  * @param product_id Your product_id (default: 0x2012)
83  * @param product_release Your product_release (default: 0x0001)
84  *
85  */
86  USBSerial(USBPhy *phy, uint16_t vendor_id = 0x1f00, uint16_t product_id = 0x2012, uint16_t product_release = 0x0001);
87 
88  /**
89  * Destroy this object
90  *
91  * Any classes which inherit from this class must call deinit
92  * before this destructor runs.
93  */
94  virtual ~USBSerial();
95 
96  /**
97  * Send a character. You can use puts, printf.
98  *
99  * @param c character to be sent
100  * @returns true if there is no error, false otherwise
101  */
102  virtual int _putc(int c);
103 
104  /**
105  * Read a character: blocking
106  *
107  * @returns character read
108  */
109  virtual int _getc();
110 
111  /**
112  * Check the number of bytes available.
113  *
114  * @returns the number of bytes available
115  */
116  uint8_t available();
117 
118  /**
119  * Check if the terminal is connected.
120  *
121  * @returns connection status
122  */
123  bool connected();
124 
125  /** Determine if there is a character available to read
126  *
127  * @returns
128  * 1 if there is a character available to read,
129  * 0 otherwise
130  */
131  int readable()
132  {
133  return available() ? 1 : 0;
134  }
135 
136  /** Determine if there is space available to write a character
137  *
138  * @returns
139  * 1 if there is space to write a character,
140  * 0 otherwise
141  */
142  int writeable()
143  {
144  return 1; // always return 1, for write operation is blocking
145  }
146 
147  /**
148  * Attach a member function to call when a packet is received.
149  *
150  * @param tptr pointer to the object to call the member function on
151  * @param mptr pointer to the member function to be called
152  */
153  template<typename T>
154  void attach(T *tptr, void (T::*mptr)(void))
155  {
156  USBCDC::lock();
157 
158  if ((mptr != NULL) && (tptr != NULL)) {
159  rx = mbed::Callback<void()>(tptr, mptr);
160  }
161 
162  USBCDC::unlock();
163  }
164 
165  /**
166  * Attach a callback called when a packet is received
167  *
168  * @param fptr function pointer
169  */
170  void attach(void (*fptr)(void))
171  {
172  USBCDC::lock();
173 
174  if (fptr != NULL) {
175  rx = mbed::Callback<void()>(fptr);
176  }
177 
178  USBCDC::unlock();
179  }
180 
181  /**
182  * Attach a Callback called when a packet is received
183  *
184  * @param cb Callback to attach
185  */
186  void attach(mbed::Callback<void()> &cb)
187  {
188  USBCDC::lock();
189 
190  rx = cb;
191 
192  USBCDC::unlock();
193  }
194 
195  /**
196  * Attach a callback to call when serial's settings are changed.
197  *
198  * @param fptr function pointer
199  */
200  void attach(void (*fptr)(int baud, int bits, int parity, int stop))
201  {
202  USBCDC::lock();
203 
204  _settings_changed_callback = fptr;
205 
206  USBCDC::unlock();
207  }
208 
209 protected:
210  virtual void data_rx();
211  virtual void line_coding_changed(int baud, int bits, int parity, int stop)
212  {
213  assert_locked();
214 
215  if (_settings_changed_callback) {
216  _settings_changed_callback(baud, bits, parity, stop);
217  }
218  }
219 
220 private:
222  void (*_settings_changed_callback)(int baud, int bits, int parity, int stop);
223 };
224 
225 /** @}*/
226 
227 #endif
void attach(T *tptr, void(T::*mptr)(void))
Attach a member function to call when a packet is received.
Definition: USBSerial.h:154
File stream.
Definition: Stream.h:42
Definition: USBCDC.h:36
void attach(void(*fptr)(void))
Attach a callback called when a packet is received.
Definition: USBSerial.h:170
USBSerial example.
Definition: USBSerial.h:51
void attach(void(*fptr)(int baud, int bits, int parity, int stop))
Attach a callback to call when serial&#39;s settings are changed.
Definition: USBSerial.h:200
void attach(mbed::Callback< void()> &cb)
Attach a Callback called when a packet is received.
Definition: USBSerial.h:186
Abstract interface to physical USB hardware.
Definition: USBPhy.h:82
uint8_t available()
Check the number of bytes available.
virtual int _putc(int c)
Send a character.
bool connected()
Check if the terminal is connected.
virtual int _getc()
Read a character: blocking.
int readable()
Determine if there is a character available to read.
Definition: USBSerial.h:131
int writeable()
Determine if there is space available to write a character.
Definition: USBSerial.h:142
virtual void lock()
Acquire exclusive access to this instance USBDevice.
virtual void unlock()
Release exclusive access to this instance USBDevice.
USBSerial(bool connect_blocking=true, uint16_t vendor_id=0x1f00, uint16_t product_id=0x2012, uint16_t product_release=0x0001)
Basic constructor.
virtual ~USBSerial()
Destroy this object.
virtual void assert_locked()
Assert that the current thread of execution holds the lock.
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.