Merge pull request #11932 from blckmn/init_osd
[betaflight.git] / docs / Serial.md
blobda3ffacb349c23ffd910e4dea4eb10a64d41c885
1 # Serial
3 Betaflight has enhanced serial port flexibility but configuration is slightly more complex as a result.
5 Betaflight has the concept of a function (MSP, GPS, Serial RX, etc) and a port (VCP, UARTx, SoftSerial x).
6 Not all functions can be used on all ports due to hardware pin mapping, conflicting features, hardware, and software
7 constraints.
9 ## Serial port types
11 * USB Virtual Com Port (VCP) - USB pins on a USB port connected directly to the processor without requiring
12 a dedicated USB to UART adapter.  VCP does not 'use' a physical UART port.
13 * UART - A pair of dedicated hardware transmit and receive pins with signal detection and generation done in hardware.
14 * SoftSerial - A pair of hardware transmit and receive pins with signal detection and generation done in software.
16 UART is the most efficient in terms of CPU usage.
17 SoftSerial is the least efficient and slowest, SoftSerial should only be used for low-bandwidth usages, such as telemetry transmission.
19 UART ports are sometimes exposed via on-board USB to UART converters, such as the CP2102 as found on the Naze and Flip32 boards.
20 If the flight controller does not have an on-board USB to UART converter and doesn't support VCP then an external USB to UART board is required.
21 These are sometimes referred to as FTDI boards.  FTDI is just a common manufacturer of a chip (the FT232RL) used on USB to UART boards.
23 When selecting a USB to UART converter choose one that has DTR exposed as well as a selector for 3.3v and 5v since they are more useful.
25 Examples:
27  * [FT232RL FTDI USB To TTL Serial Converter Adapter](http://www.banggood.com/FT232RL-FTDI-USB-To-TTL-Serial-Converter-Adapter-Module-For-Arduino-p-917226.html)
28  * [USB To TTL / COM Converter Module buildin-in CP2102](http://www.banggood.com/Wholesale-USB-To-TTL-Or-COM-Converter-Module-Buildin-in-CP2102-New-p-27989.html)
30 Both SoftSerial and UART ports can be connected to your computer via USB to UART converter boards. 
32 ## Serial Configuration
34 Serial port configuration is best done via the configurator.
36 Configure serial ports first, then enable/disable features that use the ports.  To configure SoftSerial ports the SOFTSERIAL feature must be enabled. 
38 ### Constraints
40 If the configuration is invalid the serial port configuration will reset to its defaults and features may be disabled.
42 * There must always be a port available to use for MSP/CLI.
43 * There is a maximum of 3 MSP ports.
44 * To use a port for a function, the function's corresponding feature must be also be enabled.
45 e.g. after configuring a port for GPS enable the GPS feature.
46 * If SoftSerial is used, then all SoftSerial ports must use the same baudrate.
47 * Softserial is limited to 19200 baud.
48 * All telemetry systems except MSP will ignore any attempts to override the baudrate.
49 * MSP/CLI can be shared with EITHER Blackbox OR telemetry.  In shared mode blackbox or telemetry will be output only when armed.
50 * Smartport telemetry cannot be shared with MSP.
51 * No other serial port sharing combinations are valid.
52 * You can use as many different telemetry systems as you like at the same time.
53 * You can only use each telemetry system once.  e.g.  FrSky telemetry cannot be used on two port, but MSP Telemetry + FrSky on different ports is fine.
55 ### Configuration via CLI
57 You can use the CLI for configuration but the commands are reserved for developers and advanced users.
59 The `serial` CLI command takes 6 arguments:
60 ```
61 serial <port identifier> <port function> <msp baudrate> <gps baudrate> <telemetry baudrate> <blackbox baudrate>
62 ```
64 | Serial cli command arguments |
65 | ---------------------------- |
66 | 1. Serial Port Identifier    |
67 | 2. Serial Port Function      |
68 | 3. MSP baud rate             |
69 | 4. GPS baud rate             |
70 | 5. Telemetry baud rate       |
71 | 6. Blackbox baudrate         |
73 Note: for Identifier see serialPortIdentifier_e in the source; for Function bitmask see serialPortFunction_e in the source code.
75 ### 1. Serial Port Identifier
77 | Identifier                 | Value |
78 | -------------------------- | ----- |
79 | SERIAL\_PORT\_NONE           | -1    |
80 | SERIAL\_PORT\_USART1         | 0     |
81 | SERIAL\_PORT\_USART2         | 1     |
82 | SERIAL\_PORT\_USART3         | 2     |
83 | SERIAL\_PORT\_UART4          | 3     |
84 | SERIAL\_PORT\_UART5          | 4     |
85 | SERIAL\_PORT\_USART6         | 5     |
86 | SERIAL\_PORT\_USART7         | 6     |
87 | SERIAL\_PORT\_USART8         | 7     |
88 | SERIAL\_PORT\_UART9          | 8     |
89 | SERIAL\_PORT\_USART10        | 9     |
90 | SERIAL\_PORT\_USB\_VCP       | 20    |
91 | SERIAL\_PORT\_SOFTSERIAL1    | 30    |
92 | SERIAL\_PORT\_SOFTSERIAL2    | 31    |
93 | SERIAL\_PORT\_LPUART1        | 40    |
95 ID's 0-19 reserved for UARTS 1-20
96 ID's 20-29 reserved for USB 1-10
97 ID's 30-39 reserved for SoftSerial 1-10
98 ID's 40-49 reserved for LPUART 1-10
99 Other devices can be added starting from id 50.
101 ### 2. Serial Port Function
103 | Function                     | Value  | Bit |
104 | ---------------------------- | ------ | --- |
105 | FUNCTION\_NONE                | 0      | 0   |
106 | FUNCTION\_MSP                 | 1      | 1 << 0 |
107 | FUNCTION\_GPS                 | 2      | 1 << 1 |
108 | FUNCTION\_TELEMETRY\_FRSKY_HUB | 4      | 1 << 2 |
109 | FUNCTION\_TELEMETRY\_HOTT      | 8      | 1 << 3 |
110 | FUNCTION\_TELEMETRY\_LTM       | 16     | 1 << 4 |
111 | FUNCTION\_TELEMETRY\_SMARTPORT | 32     | 1 << 5 |
112 | FUNCTION\_RX_SERIAL            | 64     | 1 << 6 |
113 | FUNCTION\_BLACKBOX             | 128    | 1 << 7 |
114 | NOT USED                       | 256    | 1 << 8 |
115 | FUNCTION\_TELEMETRY\_MAVLINK   | 512    | 1 << 9 |
116 | FUNCTION\_ESC\_SENSOR          | 1024   | 1 << 10 |
117 | FUNCTION\_VTX\_SMARTAUDIO      | 2048   | 1 << 11 |
118 | FUNCTION\_TELEMETRY\_IBUS      | 4096   | 1 << 12 |
119 | FUNCTION\_VTX\_TRAMP           | 8192   | 1 << 13 |
120 | FUNCTION\_RCDEVICE             | 16384  | 1 << 14 |
121 | FUNCTION\_LIDAR\_TF            | 32768  | 1 << 15 |
122 | FUNCTION\_FRSKY\_OSD           | 65536  | 1 << 16 |
123 | FUNCTION\_VTX\_MSP             | 131072 | 1 << 17 |
124 | FUNCTION\_MSP\_DISPLAYPORT     | 262145 | (1 << 18) \| FUNCTION\_MSP |
126 Note: `FUNCTION_FRSKY_OSD` = `(1<<16)` requires 17 bits.
127 Note2: We can use up to 32 bits (1<<32) here.
129 ### 3. MSP Baudrates
131 | Baudrate |
132 | -------- |
133 | 9600     |
134 | 19200    |
135 | 38400    |
136 | 57600    |
137 | 115200   |
138 | 230400   |
139 | 250000   |
140 | 500000   |
141 | 1000000  |
143 ### 4 GPS Baudrates
145 | Baudrate |
146 | -------- |
147 | 9600     |
148 | 19200    |
149 | 38400    |
150 | 57600    |
151 | 115200   |
153 Note: Also has a boolean AUTOBAUD. It is recommended to use a fixed baudrate. Configure GPS baudrate according to device documentation.
155 ### 5. Telemetry Baudrates
157 | Baudrate |
158 | -------- |
159 | AUTO     |
160 | 9600     |
161 | 19200    |
162 | 38400    |
163 | 57600    |
164 | 115200   |
166 ### 6. Blackbox Baudrates
168 | Baudrate |
169 | -------- |
170 | 19200    |
171 | 38400    |
172 | 57600    |
173 | 115200   |
174 | 230400   |
175 | 250000   |
176 | 400000   |
177 | 460800   |
178 | 500000   |
179 | 921600   |
180 | 1000000  |
181 | 1500000  |
182 | 2000000  |
183 | 2470000  |
185 ### Serial Port Baud Rates
187 The Serial Port baudrates are defined as follows:
189 | ID | Baudrate  |
190 | -- | --------- |
191 | 0  | Auto      |
192 | 1  | 9600      |
193 | 2  | 19200     |
194 | 3  | 38400     |
195 | 4  | 57600     |
196 | 5  | 115200    |
197 | 6  | 230400    |
198 | 7  | 250000    |
199 | 8  | 400000    |
200 | 9  | 460800    |
201 | 10 | 500000    |
202 | 11 | 921600    |
203 | 12 | 1000000   |
204 | 13 | 1500000   |
205 | 14 | 2000000   |
206 | 15 | 2470000   |
208 ### Passthrough
210 Betaflight can enter a special passthrough mode whereby it passes serial data through to a device connected to a UART/SoftSerial port. This is useful to change the configuration of a Betaflight peripheral such as an OSD, bluetooth dongle, serial RX etc.
212 To initiate passthrough mode, use the CLI command `serialpassthrough` This command takes four arguments.
214     serialpassthrough <port1 id> [port1 baud] [port1 mode] [port1 DTR PINIO] [port2 id] [port2 baud] [port2 mode]
216 `PortX ID` is the internal identifier of the serial port from Betaflight source code (see serialPortIdentifier_e in the source). For instance UART1-UART4 are 0-3 and SoftSerial1/SoftSerial2 are 30/31 respectively. PortX Baud is the desired baud rate, and portX mode is a combination of the keywords rx and tx (rxtx is full duplex). The baud and mode parameters can be used to override the configured values for the specified port. `port1 DTR PINIO` identifies the PINIO resource which is optionally connected to a DTR line of the attached device.
218 If port2 config(the last three arguments) is not specified, the passthrough will run between port1 and VCP. The last three arguments are used for `Passthrough between UARTs`, see that section to get detail.
220 For example. If you have your MWOSD connected to UART 2, you could enable communicaton to this device using the following command. This command does not specify the baud rate or mode, using the one configured for the port (see above).
222     serialpassthrough 1
224 If a baud rate is not specified, or is set to 0, then `serialpassthrough` supports changing of the baud rate over USB. This allows tools such as the MWOSD GUI to dynamically set the baud rate to, for example 57600 for reflashing the MWOSD firmware and then 115200 for adjusting settings without having to powercycle your flight control board between the two.
226 _To use a tool such as the MWOSD GUI, it is necessary to disconnect or exit Betaflight configurator._
228 **To exit serial passthrough mode, power cycle your flight control board.**
230 In order to reflash an Arduino based device such as a MWOSD via `serialpassthrough` if is necessary to connect the DTR line in addition to the RX and TX serial lines. The DTR is used as a reset line to invoke the bootloader. The DTR line may be connected to any GPIO pin on the flight control board. This pin must then be associated with a PINIO resource, the instance of which is then passed to the serialpassthrough command. If you don't need it, you can ignore it or set it to `none`. The DTR line associated with any given UART may be set using the CLI command `resource` specifying it as a PINIO resource.
232 For example, the following configuration for an OpenPilot Revolution shows the UART6 serial port to be configured with TX on pin C06, RX on pin C07 and a DTR connection using PINIO on pin C08.
235 resource SERIAL_TX 1 A09
236 resource SERIAL_TX 3 B10
237 resource SERIAL_TX 4 A00
238 resource SERIAL_TX 6 C06
239 resource SERIAL_RX 1 A10
240 resource SERIAL_RX 3 B11
241 resource SERIAL_RX 6 C07
243 resource PINIO 1 C08
246 To assign the DTR line to another pin use the following command.
249 resource PINIO 1 c05
252 To disassociate DTR from a pin use the following command.
255 resource PINIO 1 none
258 Having configured a PINIO resource assocaited with a DTR line as per the above example, connection to an MWOSD attached to an Openpilot Revolution could be achieved using the following command.
260 ```serialpassthrough 5 0 rxtx 1```
262 This will connect using UART 6, with the baud rate set over USB, full duplex, and with DTR driven on PINIO resource 1.
264 A (desirable) side effect of configuring the DTR line to be associated with a PINIO resource, is that when the FC is reset, the attached Arduino device will also be reset.
266 Note that if DTR is left configured on a port being used with a standard build of MWOSD firmware, the display will break-up when the flight controller is reset. This is because, by default, the MWOSD does not correctly handle resets from DTR. There are two solutions to this:
268 1. Assign the DTR pin using the resource command above prior to reflashing MWOSD, and then dissasociate DTR from the pin.
269 2. Rebuild MWOSD with MAX_SOFTRESET defined. The MWOSD will then be reset correctly every time the flight controller is reset.
271 ### Passthrough between UARTs
273 in BetaFlight 4.1 or later, you can make a serial passthrough between UARTs.
275 the last three arguments of `serialpassthrough` are used to the passthrough between UARTs: `[port2 id]` `[port2 baud]` `[port2 mode]`, if you don't need passthrough between UARTs, just ignore them, and use `serialpassthrough` according to above description.
276 if you want passthrough between UARTs, `[port2 id]` is a required argument, the value range is same with `port1 ID` argument, it is the internal identifier of the serial port. `[port2 baud]`and`[port2 mode]` is optional argument, the default of them are `57600` and `MODE_RXTX`.
278 For example. If you using a filght controller built-in BLE chip, and the BLE chip was inner connected to a UART, you can use the following command to let the UART to talk with other UART:
280 serialpassthrough 0 115200 rxtx none 4 19200
282 the command will run a serial passthrough between UART1 and UART5, UART1 baud is 115200, mode is MODE_RXTX, DTR is none, UART5 baud is 19200, mode is not specific, it will take default value MODE_RXTX.