Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Free replacement firmware for the **Turtle Beach VelocityOne Multi-Shift**, inst
- The Handbrake button as a normal button while the switch is in H
- The High/low button as a toggle, if you like
- A neutral button, for games that stay in gear when you move the lever to neutral
- Anti-jitter, so the gears and the handbrake axis hold still
- On Linux, games find the shifter as a joystick with no extra driver
- The Turtle Beach firmware can be put back at any time

Expand Down
24 changes: 20 additions & 4 deletions docs/protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ Settings page, as 16-bit halfwords:
| 0x2a to 0x36 | 13 auxiliary bytes (`24 23`) |
| 0x37 to 0x3e | per-gear match radius |
| 0x3f, 0x40 | sequential thresholds in y: lever pushed forward (positive), pulled back (negative) |
| 0x50 | OpenShifter options: high byte `0xA5` marks them as set; bit 0 logo LED on, bit 1 high/low button is a toggle, bit 3 gamma correction on, bit 4 neutral button on (0.5.0) |
| 0x50 | OpenShifter options: high byte `0xA5` marks them as set; bit 0 logo LED on, bit 1 high/low button is a toggle, bit 3 gamma correction on, bit 4 neutral button on (0.5.0), bits 5 and 6 anti-jitter level (0.6.0) |
| 0x51 to 0x53 | OpenShifter LED colour R, G, B |
| 0x54 to 0x56 | OpenShifter white balance gains R, G, B |
| 0x63 | `0x1254` once calibration is complete |

Stock never reads or writes halfwords 0x41 to 0x62. OpenShifter uses 0x50 to 0x56.
Without the `0xA5` marker in halfword 0x50 they mean the defaults: logo off,
high/low as a button, colour 255 182 78 (warm orange), gamma off, gains
255 162 96, neutral button off. The marker is there
255 162 96, neutral button off, anti-jitter none. The marker is there
because the page can come back from an update as zeros, not only as `0xFFFF`:
OpenShifter 0.2.0 read a zeroed page as colour 0 0 0, which turned every LED off.

Expand Down Expand Up @@ -97,7 +97,7 @@ application collection: 12 buttons and 4 constant padding bits, the
16-bit Z axis, and report 0x24 in and out on vendor page `0xFF00`, which
Linux's HID input driver ignores. The top-level usage (Generic Desktop,
Joystick) and the input and output report IDs and sizes are the same as
stock's. OpenShifter 0.4.0 and newer add feature report 0x25 (below).
stock's. OpenShifter 0.4.0 and newer add feature report 0x25, and 0.6.0 and newer input report 0x26 (below).

In 0.4.0 the 12 button bits are buttons 1 to 12. From 0.5.0 they are buttons
2 to 13, and the next bit (`buttons` bit 4) is button 1, the neutral button,
Expand Down Expand Up @@ -131,7 +131,7 @@ Input reports, sent every 2 ms:
| 13 + 6k | gear k+1 x, y, big-endian |
| 17, 29, 30, 35, 36, 41, 42, 47 | per-gear radius / 8 |
| 18 | flags: bit 0 button A, 1 button B, 2 switch in H, 3 handbrake mode, 4 lever pulled back, 5 lever pushed forward (the last two are `seq_down` and `seq_up` in the source) |
| 23 | OpenShifter only (stock sends 0): bit 7 set, bit 0 logo LED on, bit 1 high/low is a toggle, bit 2 toggle is on, bit 3 gamma on, bit 4 neutral button on |
| 23 | OpenShifter only (stock sends 0): bit 7 set, bit 0 logo LED on, bit 1 high/low is a toggle, bit 2 toggle is on, bit 3 gamma on, bit 4 neutral button on, bits 5 and 6 the anti-jitter level |
| 24, 53, 54 | OpenShifter 0.3.0 and newer (stock sends 0): white balance gains R, G, B |
| 48 | r / 8: the field strength at the sensor. With x and y it gives the lever's tilt, atan2(√(x² + y²) / 6.55, √(r² − (x² + y²) / 6.55²)) |
| 59 to 62 | handbrake rest / pulled x |
Expand All @@ -158,6 +158,7 @@ OpenShifter adds these (stock ignores them):
| `24 33 R G B s` | white balance: a gain per channel, 255 = full; saved when s is 1, live only when 0 |
| `24 34 v` | gamma correction: 1 on, 0 off (default); saved |
| `24 35 v` | neutral button (0.5.0 and newer): 1 on, 0 off (default); saved |
| `24 36 v` | anti-jitter on the lever position (0.6.0 and newer): 0 none (default), 1 low, 2 medium, 3 high; saved |
| `24 3F n` | light only LED n of the chain (0 to 2) in white, to find which LED is which; `24 3F FF` ends it. Not saved |

`tools/openshifter_ctl.py` sends all of these.
Expand All @@ -166,12 +167,27 @@ Commands marked "saved" write the settings page in flash, and every write erases

To change the LED colour continuously, for example to show RPM or the gear, use `24 20`. It is never saved.

While the settings page is erased and written (about 25 ms), OpenShifter takes no USB packet: EP0 ignores the host, so a control request then fails, and EP1 answers NAK, so input reports pause. On a unit, control requests during the erase froze it, with interrupts off or not.

Anti-jitter (`24 36`) steadies the lever position as the sensor reports it: x, y and z (s14). Everything else is worked out from those, so it applies to the gears, the sequential shifts, the handbrake axis, the x and y in report 0x24 and calibration. Each value runs through a running average of the last 2 samples (low and medium) or about 4 (high), then holds still until the average moves more than a band from it: 4, 8 or 16 units for x and y, 13, 16 or 20 for z. Going back the other way costs twice the band of free travel. If the average stays more than half a band away for 128 ms, the value moves to it, so a still lever reads within half a band of where it is. A change larger than 150 units in x or y, or 80 in z, passes at once, so shifts and a yanked handbrake are never late. Measured on a unit, x and y have a noise of about 2 (standard deviation) and z flickers by one step of its high byte (25 units). A single frame that jumps more than 600 in x or y, or 300 in z, from the one before is a bad read and is skipped; if the next frame agrees, it counts. With none (the default), the values are the raw ones, as on stock.

Feature report ID 0x25, 16 bytes, OpenShifter 0.4.0 and newer: `25` followed by
the version string, for example `0.4.0`, padded with NULs. It is read with
GET_REPORT (feature). Browsers cannot read the iConfiguration string, so the
web tool uses this report to tell OpenShifter versions apart. Stock firmware and
older OpenShifter versions do not declare it.

Input report ID 0x26, 5 bytes, OpenShifter 0.6.0 and newer: `26 x_hi x_lo y_hi y_lo`,
the lever's x and y as the sensor read them, before anti-jitter. It is sent while anti-jitter
is on, in every other slot of report 0x24, so the host gets no more reports than usual and
report 0x24 comes every 4 ms instead of 2. With anti-jitter off, report 0x24 already has the
raw reading. The web tool draws it under the position games see.

Two earlier designs failed on a real unit with Chrome. Reading report 0x26 as a feature
report every 15 ms froze the unit during a settings write (see below). Asking for it with a
command every 400 ms held Chrome's input reports back for 60 to 90 ms after each command;
the same commands from Python on Linux caused no gap, so the delay is in Chrome.

LEDs in OpenShifter:

| LED | Lit |
Expand Down
29 changes: 21 additions & 8 deletions docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ OpenShifter has been tested without hardware, in three layers. Run them all with

| Suite | Tests | What it covers |
|---|---|---|
| `tests/test_shifter.py` | 24 | the gear/sensor core built for the host with trapping UBSan, against exact float oracles; the OpenShifter settings, LEDs and buttons; when settings are written to flash |
| `tests/test_emulated.py` | 26 | the real `openshifter.bin`, and the stock image, on an emulated board |
| `tests/test_ctl.py` | 10 | `openshifter_ctl.py` against the shifter core running in a thread |
| `tests/test_shifter.py` | 33 | the gear/sensor core built for the host with trapping UBSan, against exact float oracles; the OpenShifter settings, LEDs and buttons; anti-jitter; when settings are written to flash |
| `tests/test_emulated.py` | 29 | the real `openshifter.bin`, and the stock image, on an emulated board |
| `tests/test_ctl.py` | 11 | `openshifter_ctl.py` against the shifter core running in a thread |
| `tests/test_flasher.py` | 21 | the flasher against a simulated bootloader and a fake sysfs tree |

## The emulator (`tests/emu`)
Expand Down Expand Up @@ -90,14 +90,27 @@ real bugs were found this way and fixed:
- 40 colour changes in a row cost one flash erase, made 1 s after the last
one; after 16 writes the next change waits, though the LEDs already show it
- `24 00` writes a waiting change before it resets
- with anti-jitter at high, a lever held with sensor noise reports still x
and y in H and a still handbrake axis, where none follows the noise; a full
pull still reaches 65535 at once, report 0x26 keeps giving the raw reading,
and the level survives a power cycle
- with anti-jitter on, report 0x26 takes every other report 0x24 slot, with the
total report rate unchanged; with it off there is no report 0x26
- control requests every 15 ms through a settings write: no USB packet and no
interrupt while the flash is busy, and only requests during the write fail
- with a worn settings page, `24 00` tries the flag twice, then leaves USB and
blinks red instead of resetting

`SettingsWrites` in `test_shifter.py` checks the write timing itself with a
fake clock: the 1 s wait, the 16-write burst, one more write per minute after
that, and the clock wrapping after 49 days.
that, and the clock wrapping after 49 days. `AntiJitter` checks each level
against noise on x, y and z at neutral, in a gear and on the handbrake; gear 7
right at its limit (the raw reading keeps dropping and taking it); shifts and
handbrake yanks (the same reports at the same samples as none); slow moves;
calibration on a noisy sensor; a single bad read (skipped) and a real jump
(at most one frame later); report 0x26; and a restart after the sensor resets.

None of these tests may write flash outside the FPEC rules. The emulator records every program and erase and checks it.
None of these tests may write flash outside the FPEC rules. The emulator records every program and erase and checks it. An erase takes 20 ms and a halfword 50 µs in the emulator, and taking an interrupt or a USB packet in that time is a violation too (a unit froze that way). The differential tests turn that timing off, because stock writes for about 70 ms and its timing would no longer match.

## Intentional differences

Expand All @@ -118,7 +131,8 @@ None of these tests may write flash outside the FPEC rules. The emulator records
| Gear and sequential debounce | 20 samples (40 ms) | 5 samples (10 ms) (`Core.test_calibration_round_trip_and_gears`); not yet checked on a real unit |
| Handbrake button LED | lit in every mode; green in handbrake mode | the set colour in H, off in sequential mode, green in handbrake mode |
| High/low button (A) | a button | a button, or a latching toggle (`24 32 1`) with a red/green LED |
| Report descriptor | 11 buttons in nested application collections without a usage; Linux maps them to `BTN_0`.. and udev does not tag a joystick | one Joystick collection with the 12 report bits as buttons 2 to 13, button 1 for the optional neutral button (`24 35 1`), padding, Z and report 0x24 on vendor page `0xFF00` (`OpenShifter.test_report_descriptor`); not yet checked on a real host |
| Report descriptor | 11 buttons in nested application collections without a usage; Linux maps them to `BTN_0`.. and udev does not tag a joystick | one Joystick collection with the 12 report bits as buttons 2 to 13, button 1 for the optional neutral button (`24 35 1`), padding, Z, and reports 0x24, 0x25 and 0x26 on vendor page `0xFF00` (`OpenShifter.test_report_descriptor`); not yet checked on a real host |
| Lever position | raw | optional anti-jitter on x, y and z (`24 36`, none by default); none gives stock's values |
| Report 0x24 bytes 23, 24, 53, 54 | 0 | OpenShifter options and white balance |
| Settings halfwords 0x50 to 0x56 | unused | OpenShifter options, colour and white balance |

Expand All @@ -142,8 +156,7 @@ Some timings differ by a few tens of milliseconds, all measured in the emulator:
- USB electrical and timing behaviour of the AIR32 USB block
- the AIR32 PLL at 72 MHz (the stock image only shows its 216 MHz configuration)
- the flash page size
- the delayed settings writes and the red stop after a failed flag write
have only run in the emulator
- the red stop after a failed flag write has only run in the emulator
- **The vendor bootloader.** It is not in any update package, so the flasher
is tested against a simulator written from Tuner's code. Two real updates
went through it without errors; an interrupted update has not been tried. See
Expand Down
2 changes: 1 addition & 1 deletion firmware/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# OpenShifter firmware. Needs clang + lld (LLVM >= 15); no libc, no libgcc.
VERSION ?= 0.5.0
VERSION ?= 0.6.0
BUILD := build
TARGET := $(BUILD)/openshifter
SRCS := $(wildcard src/*.c)
Expand Down
9 changes: 9 additions & 0 deletions firmware/src/flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,16 @@ int settings_write(const uint16_t st[SH_SETTINGS_HALFWORDS])
}
if (!changed)
return 0;
/* A unit froze when the host sent control requests during a settings
* write: the erase never finished. So the USB block takes no packets while
* the flash is busy (usb_pause), and no interrupt runs then either. Reports
* and the clock pause for the write, about 25 ms. */
write_active = 1;
irq_disable();
usb_pause();
int err = write_page();
usb_unpause();
irq_enable();
write_active = 0;
return err ? -1 : 1;
}
Expand Down Expand Up @@ -130,6 +138,7 @@ void enter_bootloader(void)
for (unsigned i = 0; i < PAGE_HALFWORDS; i++)
page[i] = *flash_hw(i);
page[SETTINGS_BOOT_FLAG] = 1;
usb_pause();
if (write_page() && write_page())
flash_failed();
board_reset();
Expand Down
17 changes: 15 additions & 2 deletions firmware/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,17 @@ static void in_send_one(void)
in_due &= (uint8_t)~IN_R1;
in_next = IN_R36;
} else {
shifter_report36(&sh, r);
usb_in_send(r, 64);
/* With anti-jitter on, every other 0x24 slot carries 0x26, so the
* host gets no more reports than usual. */
static uint8_t raw_turn;
raw_turn = shifter_raw_wanted(&sh) && !raw_turn;
if (raw_turn) {
shifter_report_raw(&sh, r);
usb_in_send(r, 5);
} else {
shifter_report36(&sh, r);
usb_in_send(r, 64);
}
in_due &= (uint8_t)~IN_R36;
in_next = IN_R1;
}
Expand All @@ -90,6 +99,10 @@ unsigned usb_get_report(uint8_t report_id, uint8_t *out)
shifter_report36(&sh, out);
return 64;
}
if (report_id == 0x26) {
shifter_report_raw(&sh, out);
return 5;
}
return 0;
}

Expand Down
Loading