Skip to content

Commit

Permalink
add Serial 2.0 proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
soundanalogous committed Nov 14, 2016
1 parent 9b25a91 commit 2fa9134
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 4 deletions.
11 changes: 8 additions & 3 deletions protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ N END_SYSEX (0xF7) (MIDI End of SysEx - EOX)
Following are SysEx commands used in this version of the protocol:
```
RESERVED 0x00-0x0F // The first 16 bytes are reserved for custom commands
SERIAL_MESSAGE 0x60 // communicate with serial devices, including other boards
ENCODER_DATA 0x61 // reply with encoders current positions
STEPPER2_DATA 0x62 // control a stepper motor
STEPPER_DATA 0x62 // control a stepper motor
SERIAL_MESSAGE 0x64 // communicate with serial devices, including other boards
ANALOG_MAPPING_QUERY 0x69 // ask for mapping of analog to pin numbers
ANALOG_MAPPING_RESPONSE 0x6A // reply with mapping info
CAPABILITY_QUERY 0x6B // ask for supported modes and resolution of all pins
Expand All @@ -143,6 +143,10 @@ SAMPLEING_INTERVAL 0x7A // the interval at which analog input is sample
SCHEDULER_DATA 0x7B // send a createtask/deletetask/addtotask/schedule/querytasks/querytask request to the scheduler
SYSEX_NON_REALTIME 0x7E // MIDI Reserved for non-realtime messages
SYSEX_REALTIME 0X7F // MIDI Reserved for realtime messages
// Deprecated command IDs
SERIAL_MESSAGE 0x60 // communicate with serial devices, including other boards
STEPPER_DATA 0x72 // control a stepper motor
```

Query Firmware Name and Version
Expand Down Expand Up @@ -331,4 +335,5 @@ See specific files:
* [scheduler](https://github.com/firmata/protocol/blob/master/scheduler.md)
* [onewire](https://github.com/firmata/protocol/blob/master/onewire.md)
* [encoder](https://github.com/firmata/protocol/blob/master/encoder.md)
* [serial](https://github.com/firmata/protocol/blob/master/serial.md)
* [serial-1.0](https://github.com/firmata/protocol/blob/master/serial-1.0.md)
* [serial-2.0](https://github.com/firmata/protocol/blob/master/serial-2.0.md)
2 changes: 1 addition & 1 deletion serial.md → serial-1.0.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Serial
#Serial 1.0

Enables control of up to 4 software and 4 hardware (UART) serial ports. Multiple ports can be
used simultaneously (depending on restrictions of a given microcontroller board's capabilities).
Expand Down
190 changes: 190 additions & 0 deletions serial-2.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
#Serial 2.0

Enables control of up to 4 software and 4 hardware (UART) serial ports. Multiple ports can be
used simultaneously (depending on restrictions of a given microcontroller board's capabilities).

Example files:
* Version 2.0 of the Serial feature has not yet been implemented.

Added in version 2.6.0

## Constants

### Port IDs

Use these constants to identify the hardware or software serial port to address for each command.

```
HW_SERIAL0 = 0x00 (for using Serial when another transport is used for the Firmata Stream)
HW_SERIAL1 = 0x01
HW_SERIAL2 = 0x02
HW_SERIAL3 = 0x03
SW_SERIAL0 = 0x08
SW_SERIAL1 = 0x09
SW_SERIAL2 = 0x0A
SW_SERIAL3 = 0x0B
```

### Serial pin capability response

Use these constants to identify the pin type in a [capability query response](https://github.com/firmata/protocol/blob/master/protocol.md#capability-query).

```
// Where the pin mode = "Serial" and the pin resolution = one of the following:
RES_RX0 = 0x00
RES_TX0 = 0x01
RES_RX1 = 0x02
RES_TX1 = 0x03
RES_RX2 = 0x04
RES_TX2 = 0x05
RES_RX3 = 0x06
RES_TX3 = 0x07
// extensible up to 8 HW ports
```

### Serial pin mode

```
PIN_MODE_SERIAL = 0x0A
```

## Commands

### Serial Config

Configures the specified hardware or software serial port. RX and TX pins are optional and should
only be specified if the platform requires them to be set.

```
0 START_SYSEX (0xF0)
1 SERIAL_MESSAGE (0x64) // command byte
2 SERIAL_CONFIG (0x00)
3 port (HW_SERIALn OR SW_SERIALn)
4 baud (bits 0 - 6)
5 baud (bits 7 - 13)
6 baud (bits 14 - 20) // need to send 3 bytes for baud even if value is < 14 bits
7 rxPin (0-127) [optional] // only set if platform requires RX pin number
8 txPin (0-127) [optional] // only set if platform requires TX pin number
7|9 END_SYSEX (0xF7)
```

### Serial Write

Firmata client -> Board

Receive serial data from Firmata client, reassemble and write for each byte received.

```
0 START_SYSEX (0xF0)
1 SERIAL_MESSAGE (0x64)
2 SERIAL_WRITE (0x01)
3 port (HW_SERIALn OR SW_SERIALn)
4 data 0 (LSB)
5 data 0 (MSB)
6 data 1 (LSB)
7 data 1 (MSB)
... // up to max buffer - 5
n END_SYSEX (0xF7)
```

### Serial Read

Board -> Firmata client

Read contents of serial buffer and send to Firmata client (send with `SERIAL_REPLY`).

`maxBytesToRead` optionally specifies how many bytes to read for each iteration. Set to 0 (or do not
define) to read all available bytes. If there are less bytes in the buffer than the number of bytes
specified by `maxBytesToRead` then the lesser number of bytes will be returned.

```
0 START_SYSEX (0xF0)
1 SERIAL_MESSAGE (0x64)
2 SERIAL_READ (0x02)
3 port (HW_SERIALn OR SW_SERIALn)
4 SERIAL_READ_MODE (0x00) // 0x00 => read continuously, 0x01 => stop reading
5 maxBytesToRead (lsb) [optional]
6 maxBytesToRead (msb) [optional]
5|7 END_SYSEX (0xF7)
```

### Serial Reply

Board -> Firmata client

Sent in response to a SERIAL_READ event or on each iteration of the reporting loop if `SERIAL_READ_CONTINUOUSLY` is set.

```
0 START_SYSEX (0xF0)
1 SERIAL_MESSAGE (0x64)
2 SERIAL_REPLY (0x03)
3 port (HW_SERIALn OR SW_SERIALn)
4 data 0 (LSB)
5 data 0 (MSB)
6 data 1 (LSB)
7 data 1 (MSB)
... // up to max buffer - 5
n END_SYSEX (0xF7)
```

### Serial Close

Close the serial port. If you close a port, you will need to send a `SERIAL_CONFIG` message to
reopen it.

```
0 START_SYSEX (0xF0)
1 SERIAL_MESSAGE (0x64)
2 SERIAL_CLOSE (0x04)
3 port (HW_SERIALn OR SW_SERIALn)
4 END_SYSEX (0xF7)
```

### Serial Flush

Flush the serial port. The exact behavior of flush depends on the underlying platform. For example,
with Arduino, calling `flush` on a HW serial port will drain the TX output buffer, calling `flush`
on a SW serial port will reset the RX buffer to the beginning, abandoning any data in the buffer.
Other platforms may define `flush` differently as well so see the documentation of flush for the
platform you are working with to understand exactly how it functions.

Generally `flush` is rarely needed so this functionality is primarily provided for advanced use
cases.

```
0 START_SYSEX (0xF0)
1 SERIAL_MESSAGE (0x64)
2 SERIAL_FLUSH (0x05)
3 port (HW_SERIALn OR SW_SERIALn)
4 END_SYSEX (0xF7)
```

### Serial Listen

Enable switching serial ports. Necessary for Arduino SoftwareSerial but may not be applicable to
other platforms.

```
0 START_SYSEX (0xF0)
1 SERIAL_MESSAGE (0x64)
2 SERIAL_LISTEN (0x06)
3 port (HW_SERIALn OR SW_SERIALn)
4 END_SYSEX (0xF7)
```

### Serial Update Baud

Update the baud rate on a configured serial port.

```
0 START_SYSEX (0xF0)
1 SERIAL_MESSAGE (0x64)
2 SERIAL_UPDATE_BAUD (0x07)
3 port (HW_SERIALn OR SW_SERIALn)
4 baud (bits 0 - 6)
5 baud (bits 7 - 13)
6 baud (bits 14 - 20) // need to send 3 bytes for baud even if value is < 14 bits
7 END_SYSEX (0xF7)
```

0 comments on commit 2fa9134

Please sign in to comment.