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