0
mirror of https://github.com/torvalds/GuitarPedal.git synced 2026-08-18 21:26:54 +00:00
Files
torvalds-GuitarPedal/Firmware/pio/bitstream.pio
Linus Torvalds 0c1b9c3db3 Split Software/ into the four things it actually was
'Software' was the directory everything that was not KiCad ended up in,
which stopped describing anything a while ago - Validation and the web
app are software too.  Worse, it put the shared parts inside the
firmware, where they read as the firmware's own.

They are not.  Effects/ has three consumers built from it: the firmware,
Validation's bench, and the web app's controls, all generated from the
same POT: comments by gen_effects.py.  Audio/ has two - the bench
compiles the same biquads, the same envelope followers and the same
single_sample(), which is the whole reason a measurement on a
workstation says anything about the pedal.  Neither belongs under
Firmware/, so neither is under it any more:

  Effects/    one file per effect
  Audio/      the DSP they are built from, and the audio loop
  Firmware/   the rest of what runs on the pedal, and the submodules
  WebMIDI/    the web app
  scripts/    what the build runs
  Validation/ unchanged
  Hardware/, Documentation/, Images/

CMakeLists.txt and the wrapper Makefile move to the top with them,
because the build now consumes four of those directories and generates
into a fifth.  board.local and build/ come along; MIDI_CC_MAP.md is
generated into Documentation/ rather than into the old Software/ root.

scripts/ goes with the build rather than staying under the firmware,
because six of the ten had nothing to do with the firmware: gen_effects.py
reads Effects/ and writes to three different places, pow2/log2/quarter_sine
generate Audio/'s tables, check-readme.py compares Effects/ against the
README, and server.py serves the web app.  Four of them are invoked from
Validation, which was reaching into Firmware/ for tooling - the same
burying this commit is undoing.  The four that really are about the
firmware are ELF checks the top-level build drives anyway, and a second
scripts directory would only be a second place to look.

C includes say "Audio/foo.h" and the generated map says
"Effects/bar.h", with the repository root on the include path for both
the firmware and the bench.  Spelling the directory out rather than
relying on a bare name is what keeps Audio/cycles.h shimmable: a quoted
include searches the including file's own directory first.

The submodules are renamed as well as moved.  git mv updates their paths
but leaves the section names, and 'Software/pico-sdk' surviving in
.gitmodules would be the word this commit removes, still load-bearing.
That meant the nested modules under pico-sdk too - six .git files
pointing into .git/modules/Software - which is why 'git submodule update
--init --recursive' is worth running once after pulling this.

Verified rather than assumed: a clean configure and build, make check
(failing only on the missing-eeprom case it already failed on),
check-effects, all four analysis pages reproducing every series and
drawing every chart, and a flash to the board that still measures a
routed reverb where it did before.

One latent bug fell out of it.  bench/coeff declared only quarter_sine.h
of the three generated math tables, and Audio/util.h includes pow2.h and
log2.h as well - so building that target with an empty gen/ could never
have worked.  'make bench' builds bench/bench first, which generates all
three, so it stayed hidden until this rebuilt everything from nothing.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2026-08-11 13:48:26 -07:00

75 lines
2.9 KiB
Plaintext

;
; A fixed-rate one-bit output engine.
;
; This deliberately knows nothing about WS2812Bs, or about any other
; protocol. It shifts bits out of the FIFO onto a pin at a constant
; rate, forever, and that is the entire specification. Whatever those
; bits mean is decided by whoever filled the buffer.
;
; It replaces a program that encoded the WS2812B waveform itself, in
; delay slots and a branch. That version had to be told each colour and
; could not produce the inter-frame reset gap at all, because the gap is
; not made of bits and the program only knew how to send bits - so the
; timing of the gap lived in software, in a different file, enforced by
; a sleep. Here the gap is just zeroes in memory like everything else,
; and there is nothing left in the program that could disagree with the
; datasheet, because the program no longer contains any claim about it.
;
; What that buys, beyond the tidiness: the protocol now lives in a table
; a script generates, which can be tested on a host that has no PIO in
; it. "Is this bit pattern right" is a question you can answer with a
; diff. "Is that delay slot 3 or 4" is a question you can only answer
; with a logic analyser.
;
; The one number that still matters here is the rate, and it is a
; parameter rather than a constant: pass the desired output bit rate to
; bitstream_program_init() and the divisor is worked out against the
; system clock at run time. The program it replaces baked '.clock_div
; 18' into this file, which quietly meant a different bit rate on every
; board that chose a different system clock.
;
; How many bits make up a word is a parameter, not a constant, because
; it is not this program's business: it belongs to whatever encoding the
; buffer holds. bitstream_program_init() takes it and sets the autopull
; threshold from it. The WS2812B wants 32 - four output bits per
; protocol bit, eight bits to a colour byte - but that is a fact about
; WS2812Bs and it lives in pixels.h where the rest of them are.
;
; Bits leave MSB first, so an encoding fills each word from the top.
;
.program bitstream
.out 1 left auto 32
.wrap_target
out pins, 1
.wrap
% c-sdk {
#include "hardware/clocks.h"
static inline void bitstream_program_init(PIO pio, uint sm, uint offset,
uint pin, float rate_hz, uint bits)
{
pio_gpio_init(pio, pin);
pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true);
pio_sm_config c = bitstream_program_get_default_config(offset);
sm_config_set_out_pins(&c, pin, 1);
// shift left, autopull on, refill after this many bits
sm_config_set_out_shift(&c, false, true, bits);
//
// One instruction, one cycle, one output bit, so the state
// machine clock *is* the bit rate. Derived rather than
// asserted: this is the only place left where the hardware
// could be silently wrong about the protocol.
//
sm_config_set_clkdiv(&c, (float)clock_get_hz(clk_sys) / rate_hz);
pio_sm_init(pio, sm, offset, &c);
pio_sm_set_enabled(pio, sm, true);
}
%}