1 /*
2  * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef _PICO_STDIO_USB_H
8 #define _PICO_STDIO_USB_H
9 
10 #include "pico/stdio.h"
11 
12 /** \brief Support for stdin/stdout over USB serial (CDC)
13  *  \defgroup pico_stdio_usb pico_stdio_usb
14  *  \ingroup pico_stdio
15  *
16  *  Linking this library or calling `pico_enable_stdio_usb(TARGET ENABLED)` in the CMake (which
17  *  achieves the same thing) will add USB CDC to the drivers used for standard input/output
18  *
19  *  Note this library is a developer convenience. It is not applicable in all cases; for one it takes full control of the USB device precluding your
20  *  use of the USB in device or host mode. For this reason, this library will automatically disengage if you try to using it alongside \ref tinyusb_device or
21  *  \ref tinyusb_host. It also takes control of a lower level IRQ and sets up a periodic background task.
22  *
23  *  This library also includes (by default) functionality to enable the RP2040 to be reset over the USB interface.
24  */
25 
26 // PICO_CONFIG: PICO_STDIO_USB_DEFAULT_CRLF, Default state of CR/LF translation for USB output, type=bool, default=PICO_STDIO_DEFAULT_CRLF, group=pico_stdio_usb
27 #ifndef PICO_STDIO_USB_DEFAULT_CRLF
28 #define PICO_STDIO_USB_DEFAULT_CRLF PICO_STDIO_DEFAULT_CRLF
29 #endif
30 
31 // PICO_CONFIG: PICO_STDIO_USB_STDOUT_TIMEOUT_US, Number of microseconds to be blocked trying to write USB output before assuming the host has disappeared and discarding data, default=500000, group=pico_stdio_usb
32 #ifndef PICO_STDIO_USB_STDOUT_TIMEOUT_US
33 #define PICO_STDIO_USB_STDOUT_TIMEOUT_US 500000
34 #endif
35 
36 // todo perhaps unnecessarily frequent?
37 // PICO_CONFIG: PICO_STDIO_USB_TASK_INTERVAL_US, Period of microseconds between calling tud_task in the background, default=1000, advanced=true, group=pico_stdio_usb
38 #ifndef PICO_STDIO_USB_TASK_INTERVAL_US
39 #define PICO_STDIO_USB_TASK_INTERVAL_US 1000
40 #endif
41 
42 // PICO_CONFIG: PICO_STDIO_USB_LOW_PRIORITY_IRQ, Explicit User IRQ number to claim for tud_task() background execution instead of letting the implementation pick a free one dynamically (deprecated), advanced=true, group=pico_stdio_usb
43 #ifndef PICO_STDIO_USB_LOW_PRIORITY_IRQ
44 // this variable is no longer set by default (one is claimed dynamically), but will be respected if specified
45 #endif
46 
47 // PICO_CONFIG: PICO_STDIO_USB_ENABLE_RESET_VIA_BAUD_RATE, Enable/disable resetting into BOOTSEL mode if the host sets the baud rate to a magic value (PICO_STDIO_USB_RESET_MAGIC_BAUD_RATE), type=bool, default=1, group=pico_stdio_usb
48 #ifndef PICO_STDIO_USB_ENABLE_RESET_VIA_BAUD_RATE
49 #define PICO_STDIO_USB_ENABLE_RESET_VIA_BAUD_RATE 1
50 #endif
51 
52 // PICO_CONFIG: PICO_STDIO_USB_RESET_MAGIC_BAUD_RATE, baud rate that if selected causes a reset into BOOTSEL mode (if PICO_STDIO_USB_ENABLE_RESET_VIA_BAUD_RATE is set), default=1200, group=pico_stdio_usb
53 #ifndef PICO_STDIO_USB_RESET_MAGIC_BAUD_RATE
54 #define PICO_STDIO_USB_RESET_MAGIC_BAUD_RATE 1200
55 #endif
56 
57 // PICO_CONFIG: PICO_STDIO_USB_CONNECT_WAIT_TIMEOUT_MS, Maximum number of milliseconds to wait during initialization for a CDC connection from the host (negative means indefinite) during initialization, default=0, group=pico_stdio_usb
58 #ifndef PICO_STDIO_USB_CONNECT_WAIT_TIMEOUT_MS
59 #define PICO_STDIO_USB_CONNECT_WAIT_TIMEOUT_MS 0
60 #endif
61 
62 // PICO_CONFIG: PICO_STDIO_USB_POST_CONNECT_WAIT_DELAY_MS, Number of extra milliseconds to wait when using PICO_STDIO_USB_CONNECT_WAIT_TIMEOUT_MS after a host CDC connection is detected (some host terminals seem to sometimes lose transmissions sent right after connection), default=50, group=pico_stdio_usb
63 #ifndef PICO_STDIO_USB_POST_CONNECT_WAIT_DELAY_MS
64 #define PICO_STDIO_USB_POST_CONNECT_WAIT_DELAY_MS 50
65 #endif
66 
67 // PICO_CONFIG: PICO_STDIO_USB_RESET_BOOTSEL_ACTIVITY_LED, Optionally define a pin to use as bootloader activity LED when BOOTSEL mode is entered via USB (either VIA_BAUD_RATE or VIA_VENDOR_INTERFACE), type=int, min=0, max=29, group=pico_stdio_usb
68 
69 // PICO_CONFIG: PICO_STDIO_USB_RESET_BOOTSEL_FIXED_ACTIVITY_LED, Whether the pin specified by PICO_STDIO_USB_RESET_BOOTSEL_ACTIVITY_LED is fixed or can be modified by picotool over the VENDOR USB interface, type=bool, default=0, group=pico_stdio_usb
70 #ifndef PICO_STDIO_USB_RESET_BOOTSEL_FIXED_ACTIVITY_LED
71 #define PICO_STDIO_USB_RESET_BOOTSEL_FIXED_ACTIVITY_LED 0
72 #endif
73 
74 // Any modes disabled here can't be re-enabled by picotool via VENDOR_INTERFACE.
75 // PICO_CONFIG: PICO_STDIO_USB_RESET_BOOTSEL_INTERFACE_DISABLE_MASK, Optionally disable either the mass storage interface (bit 0) or the PICOBOOT interface (bit 1) when entering BOOTSEL mode via USB (either VIA_BAUD_RATE or VIA_VENDOR_INTERFACE), type=int, min=0, max=3, default=0, group=pico_stdio_usb
76 #ifndef PICO_STDIO_USB_RESET_BOOTSEL_INTERFACE_DISABLE_MASK
77 #define PICO_STDIO_USB_RESET_BOOTSEL_INTERFACE_DISABLE_MASK 0u
78 #endif
79 
80 // PICO_CONFIG: PICO_STDIO_USB_ENABLE_RESET_VIA_VENDOR_INTERFACE, Enable/disable resetting into BOOTSEL mode via an additional VENDOR USB interface - enables picotool based reset, type=bool, default=1, group=pico_stdio_usb
81 #ifndef PICO_STDIO_USB_ENABLE_RESET_VIA_VENDOR_INTERFACE
82 #define PICO_STDIO_USB_ENABLE_RESET_VIA_VENDOR_INTERFACE 1
83 #endif
84 
85 // PICO_CONFIG: PICO_STDIO_USB_RESET_INTERFACE_SUPPORT_RESET_TO_BOOTSEL, If vendor reset interface is included allow rebooting to BOOTSEL mode, type=bool, default=1, group=pico_stdio_usb
86 #ifndef PICO_STDIO_USB_RESET_INTERFACE_SUPPORT_RESET_TO_BOOTSEL
87 #define PICO_STDIO_USB_RESET_INTERFACE_SUPPORT_RESET_TO_BOOTSEL 1
88 #endif
89 
90 // PICO_CONFIG: PICO_STDIO_USB_RESET_INTERFACE_SUPPORT_RESET_TO_FLASH_BOOT, If vendor reset interface is included allow rebooting with regular flash boot, type=bool, default=1, group=pico_stdio_usb
91 #ifndef PICO_STDIO_USB_RESET_INTERFACE_SUPPORT_RESET_TO_FLASH_BOOT
92 #define PICO_STDIO_USB_RESET_INTERFACE_SUPPORT_RESET_TO_FLASH_BOOT 1
93 #endif
94 
95 // PICO_CONFIG: PICO_STDIO_USB_RESET_RESET_TO_FLASH_DELAY_MS, delays in ms before rebooting via regular flash boot, default=100, group=pico_stdio_usb
96 #ifndef PICO_STDIO_USB_RESET_RESET_TO_FLASH_DELAY_MS
97 #define PICO_STDIO_USB_RESET_RESET_TO_FLASH_DELAY_MS 100
98 #endif
99 
100 // PICO_CONFIG: PICO_STDIO_USB_CONNECTION_WITHOUT_DTR, Disable use of DTR for connection checking meaning connection is assumed to be valid, type=bool, default=0, group=pico_stdio_usb
101 #ifndef PICO_STDIO_USB_CONNECTION_WITHOUT_DTR
102 #define PICO_STDIO_USB_CONNECTION_WITHOUT_DTR 0
103 #endif
104 
105 // PICO_CONFIG: PICO_STDIO_USB_DEVICE_SELF_POWERED, Set USB device as self powered device, type=bool, default=0, group=pico_stdio_usb
106 #ifndef PICO_STDIO_USB_DEVICE_SELF_POWERED
107 #define PICO_STDIO_USB_DEVICE_SELF_POWERED 0
108 #endif
109 
110 // PICO_CONFIG: PICO_STDIO_USB_SUPPORT_CHARS_AVAILABLE_CALLBACK, Enable USB STDIO support for stdio_set_chars_available_callback. Can be disabled to make use of USB CDC RX callback elsewhere, type=bool, default=1, group=pico_stdio_usb
111 #ifndef PICO_STDIO_USB_SUPPORT_CHARS_AVAILABLE_CALLBACK
112 #define PICO_STDIO_USB_SUPPORT_CHARS_AVAILABLE_CALLBACK 1
113 #endif
114 
115 #ifdef __cplusplus
116 extern "C" {
117 #endif
118 
119 extern stdio_driver_t stdio_usb;
120 
121 /*! \brief Explicitly initialize USB stdio and add it to the current set of stdin drivers
122  *  \ingroup pico_stdio_usb
123  *
124  *  \ref PICO_STDIO_USB_CONNECT_WAIT_TIMEOUT_MS can be set to cause this method to wait for a CDC connection
125  *  from the host before returning, which is useful if you don't want any initial stdout output to be discarded
126  *  before the connection is established.
127  *
128  *  \return true if the USB CDC was initialized, false if an error occurred
129  */
130 bool stdio_usb_init(void);
131 
132 /*! \brief Check if there is an active stdio CDC connection to a host
133  *  \ingroup pico_stdio_usb
134  *
135  *  \return true if stdio is connected over CDC
136  */
137 bool stdio_usb_connected(void);
138 #ifdef __cplusplus
139 }
140 #endif
141 
142 #endif
143