Skip to content

Latest commit

 

History

History
355 lines (312 loc) · 18.7 KB

implementationdetails.md

File metadata and controls

355 lines (312 loc) · 18.7 KB

OAMControl - Implementation details

  1. Purpose

This application runs on a Raspberry Pi Pico 2 W. It controls the output AOM of a Coherent Monaco laser with microsecond precision through the laser's Gate 1 input.

The Pico 2 W receives three TTL signals on three input pins: two from a 2-photon Neurolabware microscope (Scanbox) and one from the stimulus/experimental control computer. The stimulus control computer sends a TTL to arm the Pico application. Once armed, TTLs from the microscope (frame and line onsets) determine the onset delay and uptime of a TTL that is sent to the Gate 1 input of the Monaco laser and to the Scanbox computer for logging.

After a frame onset, the Pico waits for a set number of line onsets and then a delay. It then closes the PMTs, and 1 us later the gate output goes high for a configured duration. The Monaco laser emits pulses as long as Gate 1 is high. 1 us after the gate goes low, the PMTs open again.

Scan mode, line count, delay, duration and pulse count are configurable at run time through a command interface (see section 6).

  1. Hardware and pin map:

https://claude.ai/artifact/WCeWWooAtD8uy6jNvMCtzp

Current pin assignment (see OAMControl.c). The firmware uses RP2350 GPIO numbers (GPn); the physical header pin is listed separately. Pins 3, 8, 13, 18, 23, 28, 33 and 38 are GND.

Signal GPIO Pin Direction Source / destination ARM GP2 4 input stimulus computer TTL TRIGGER GP3 5 input 2-photon microscope TTL (frame onset) LINE GP4 6 input 2-photon microscope TTL (line onset) PMT GP14 19 output H11706 PMT shutter; high = closed (see 2.3) GATE GP15 20 output Monaco Gate 1 (via 5 V buffer, see 2.1)

Neighbouring GND pins for the signal returns: pin 3 (next to ARM/TRIGGER), pin 8 (next to LINE) and pin 18 (next to PMT/GATE).

There is no status LED: the device state (armed, remaining pulses, gate, PMT) is read with the GET command. Do not use GPIO23, 24, 25 or 29; on the Pico 2 W they are internal to the CYW43 WiFi chip and not on the header.

2.1 Gate output to the Monaco (Gate 1) Monaco manual, "Gate 1 and Gate 2" (p. 3-18):

  • Connector: HD-BNC (Amphenol 034-1030); HD-BNC-to-BNC adapters are in the laser accessory kit.
  • High input: 3.3 V to 5.0 V. Low input: 0 to 0.5 V.
  • The input has a 10 kohm series resistor (high impedance, ~0.5 mA at 5 V).
  • Gate 1 controls the laser pulse train. Gate 2 is a spare "for future use" and does nothing. Use Gate 1.
  • The laser must be in Gated Mode (PM=1, or "Enable External Gate" on the Triggering tab of the Coherent GUI) or Burst Mode; see Q9.

A 3.3 V Pico GPIO sits exactly at the minimum high level, with zero margin. Regulator tolerance, ground offset or cable noise can then give an intermittent gate. Therefore:

  • Drive Gate 1 through a 5 V buffer, e.g. 74AHCT1G125 (accepts 3.3 V input, outputs 5 V, a few ns of delay).
  • Power the buffer from 5 V. VBUS only carries 5 V when the Pico is powered over USB; with another power source, use that 5 V supply.
  • Put a pull-down (~10 kohm to GND) on the buffer input so the gate stays low during boot, reset, flashing and whenever the pin is not driven.

2.2 TTL inputs (ARM, TRIGGER, LINE)

  • RP2350 inputs only tolerate 5 V while the Pico is powered. A 5 V TTL arriving while the Pico is off can damage it. Add a ~1 kohm series resistor per input as a minimum, or an input buffer.
  • If ground loops between rigs are a problem, use a fast digital isolator (e.g. ADuM series). Do not use optocouplers: they add microseconds of variable delay.

2.3 PMT shutter output (Hamamatsu H11706)

  • Shutter input (yellow wire, GND black): 500 ohm, high >= 2.0 V. High = shutter closed (PMT off), low = PMT on (normal imaging); the PMT is also on when the Pico is off.
  • Shutter limits: closed for 1 us to 10 ms at most, at up to 10 kHz. The firmware caps the duration so that duration + 2 us <= 10 ms; the rate limit is not reached at microscope frame rates.
  • Driven directly from GP14, no buffer: the firmware sets the pin to 12 mA drive strength (the 4 mA default is too weak for 500 ohm). Use a 100 ohm series resistor at the Pico end: ~2.7 V and ~5.5 mA at the shutter input, and it damps cable reflections and limits the current if the cable is shorted.
  • Pull-down (10 kohm to GND) on GP14, so the shutter stays open (PMT on) during boot, reset and flashing.
  • The H11706 closes within 1 us, hence the fixed 1 us guard (Q12).
  • GP14 and GP15 (GATE) are consecutive GPIOs, so the planned PIO program can drive both as side-set pins.
  1. Signal timing

Example with line_count = 2:

ARM /‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ TRIGGER /‾__________________________________ LINE /‾___/‾_________________________ |<- delay ->|1us|<- duration ->|1us| PMT __________________________/‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾_ (high = closed) GATE __________________________/‾‾‾‾‾‾‾‾‾‾‾‾‾‾_ LASER _________________________|‖‖‖‖‖‖‖‖‖‖‖‖‖‖| (pulses at laser rep rate)

With line_count = 0 the delay starts at the TRIGGER edge.

Important: in Gated Mode the laser's own pulse clock keeps running, and the gate only lets pulses through. The first laser pulse therefore comes up to one laser period after the gate goes high (1 us at 1 MHz). The number of pulses is roughly duration x rep rate. Burst Mode is the alternative; see Q9.

  1. Arming and triggering behaviour

Current implementation:

  • A rising edge on the ARM input arms the controller, using the line_count, delay, duration and pulse_count set at that moment. There is no arm command: only the stimulus computer's ARM TTL can arm the controller.
  • Each rising edge on TRIGGER (frame onset), while armed, starts this sequence:
    1. wait for line_count whole lines (default 0: skip), i.e. for the onset of line line_count + 1;
    2. wait for the delay (default 0), counted from that line onset, or from the TRIGGER edge when line_count is 0;
    3. PMT high (PMTs closed);
    4. after 1 us, GATE high for the duration, then GATE low;
    5. after 1 us, PMT low (PMTs open).
  • The first line onset of a frame (line 1) coincides with the frame onset (seen on the oscilloscope). It is never counted: a LINE edge up to 2 us after TRIGGER is taken as line 1, so whether it counts does not depend on which edge the polling loop sees first. A frame of 512 lines therefore has 511 countable line onsets after TRIGGER.
  • line_count must be smaller than the scan mode's lines per frame: 0..511 for uni (default), 0..255 for bi; line_count = 511 waits for the onset of the last line (512).
  • The controller disarms by itself after pulse_count gate pulses (SET_COUNT, >= 1, default 1).
  • An armed run cannot be stopped, restarted or changed: ARM edges are ignored and SET_* commands are rejected while armed.
  • Triggers while not armed are ignored.
  • The whole sequence ends within the frame of its trigger (Q14): lines + delay + 1 us + duration + 1 us must be shorter than the frame period. SET_LINES, SET_DELAY and SET_PULSE refuse values that break this, so the next trigger always arrives after the PMTs have opened. The lines are estimated as line_count x measured line period.
  • A trigger during a sequence is ignored (measured, but it starts no sequence and is not counted). This only happens when the timing was not checked: parameters set while the microscope was stopped, or a frame rate change after setting them. The gate then runs into the next frame, that frame gets no gate, and because it is not counted the run takes one frame longer. The GUI warns when the parameters do not fit the measured frame period.
  • Duration cap: at most 9998 us (shutter closed <= 10 ms), checked by SET_PULSE and applied again when the run is armed.
  • After the run the user can change parameters and arm again.

Frame and line measurement:

  • Core 1 timestamps every TRIGGER and LINE rising edge, armed or not.
  • frame_us: the interval between the last two TRIGGER edges.
  • line_ns: the mean LINE interval within the last full frame (first to last LINE edge, divided by the number of intervals), in ns so that line_count x line period stays accurate over 511 lines.
  • frame_lines: the number of LINE edges in the last full frame.
  • When no TRIGGER has arrived for 2 s, the microscope is taken to have stopped and all three read 0 (not measured). The first interval after such a gap is not used.
  • Without a measured frame period, SET_LINES, SET_DELAY and SET_PULSE are not checked against the frame (the shutter limit is always checked). Set the timing while the microscope is scanning.

Decisions: Q1 Arming is edge-based: a rising edge on ARM arms the controller; the ARM level afterwards has no effect. ARM edges while armed are ignored (no re-arm). Q2 One gate pulse per microscope TTL until disarmed. The number of gate pulses per arm is set with SET_COUNT; the controller disarms after the last one. Q3 Only the pulse count disarms: the controller disarms automatically after pulse_count gate pulses. There is no reset or abort command; an armed run cannot be interrupted by the user. Q4 The microscope TTL is the frame onset, so each gate pulse is triggered by a frame onset. The trigger rate is the frame rate. The whole sequence, including the end of the gate, falls within the frame of its trigger (Q14). Q5 A trigger during a sequence never restarts it; it is ignored. With the Q14 check this only happens when the timing was not checked. Q6 The rising edge of the microscope TTL marks frame onset. No glitch filtering for now; to be revisited after verifying the inputs with an oscilloscope. Q7 No pulse trains: exactly one gate pulse per trigger. Q9 Laser mode is the user's choice, set on the laser; the firmware does not depend on it. Gated Mode (PM=1): the laser emits pulses for as long as the gate is high, so the gate duration sets the pulse count (roughly duration x rep rate). Burst Mode: the laser emits a fixed 1..1,000,000 pulses when Gate 1 goes high, giving an exact count; set the gate duration so the burst fits inside it. Q8 No parameter changes while armed. All SET_* commands are rejected with "error: armed" until the run has ended. Core 1 also latches line_count, delay, duration and count at arm time, so a SET that races an ARM edge cannot affect the run. Q10 While armed, trigger polling must not be interrupted by serial IO. Core 1 polls ARM, TRIGGER and LINE and generates the PMT and gate outputs with no interrupts; core 0 handles serial IO and only sets parameters. Q11 The delay is confined by the frame rate; the number of line inputs per frame is confined by the scan mode. Scan mode is a parameter: uni (max 512 lines per frame) or bi (max 256 lines per frame). line_count must be smaller than that maximum; SET_LINES and SET_SCANMODE reject a combination that breaks this (no clipping). Q12 The PMTs are closed around every gate pulse: PMT high 1 us before GATE goes high, PMT low 1 us after GATE goes low. High = closed. The PMTs close within 1 us, so the guard is fixed (not a parameter). The 1 us guards are timed in CPU cycles, so each is at least 1 us. Q13 The Pico measures the frame period, line period and lines per frame from TRIGGER and LINE, and uses them to check SET_LINES, SET_DELAY and SET_PULSE. LINE edges do not stop during a run. Q14 The end of the gate (and the PMTs reopening) always falls within the frame that triggered it: lines + delay + duration + 2 us < frame period, enforced when the parameters are set. Because the shutter limits the gate to 10 ms, there is no need to handle gates running into the next frame: no timeout, no carried-over frames, no skipped frames. A SET that does not lengthen the sequence is always accepted, so a configuration that no longer fits can be repaired.

Open questions: none.

  1. Timing requirements

Units: microseconds for all times (delay and duration); line_count is a number of LINE edges.

  • Line count range: 0 to max_lines - 1 (511 uni, 255 bi).
  • Sequence: lines + delay + duration + 2 us < frame period.
  • Delay range: 0 us up to that limit.
  • Duration range: 1 us up to that limit, and at most 9998 us (shutter closed <= 10 ms).
  • Trigger-to-gate latency at delay 0: TBD (target < 1 us).
  • Jitter: target < 50 ns.
  • Duration accuracy: +/- 1 us.

Implementation target: the trigger-to-gate path must not depend on CPU interrupts. The current code busy-polls the GPIOs on core 1 (no interrupts on that core) and times delay and duration against the 1 us system timer. This gives roughly 1 us of jitter, independent of USB or WiFi activity on core 0. The target design is a PIO state machine: wait for trigger, count the delay, raise the gate, count the duration, lower the gate. Jitter is then one system clock (~6.7 ns at 150 MHz). The CPU only loads delay/duration into the PIO FIFO. The CYW43 uses one PIO block; the RP2350 has three.

  1. Control interface

Current: line-based text commands over USB CDC serial (and UART stdio):

SET_SCANMODE <uni|bi> -> scanmode=<uni|bi> max_lines=<512|256> SET_LINES -> line_count= (whole lines after TRIGGER, n < max_lines) SET_DELAY -> delay_us= SET_PULSE -> pulse_us= SET_COUNT -> pulse_count= (gate pulses per arm, n >= 1) GET -> armed=<0|1> delay_us= pulse_us= pulse_count= remaining= gate=<0|1> scanmode=<uni|bi> max_lines= line_count= pmt=<0|1> frame_us= line_ns= frame_lines= (pmt=1: PMTs closed; frame_us, line_ns, frame_lines: measured, 0 = not measured)

SET_* while armed -> error: armed SET_COUNT 0 -> error: count must be >= 1 SET_SCANMODE other -> error: scanmode must be uni or bi SET_SCANMODE bi with line_count >= 256 -> error: line_count must be < 256 for bi SET_LINES n >= max_lines -> error: line_count must be < <max_lines> SET_PULSE p > 9998 -> error: pulse_us must be <= 9998 SET_LINES, SET_DELAY or SET_PULSE that makes line_count x line period + delay + pulse + 2 >= frame_us -> error: lines + delay + pulse + 2 must be < frame_us ()

The frame check is only made while a frame period is measured (the microscope is scanning), and uses the other two values the Pico holds at that moment. A SET that does not lengthen the sequence is always accepted. So when changing several values, send the ones that shorten the sequence first. To switch to bi with a smaller line count, send SET_LINES first; to switch to uni with a larger line count, send SET_SCANMODE first.

USB serial is the primary interface: no network setup, no packet loss, and the stimulus computer is nearby. UDP over WiFi may be added later as an option; if so, every command must get a reply (acknowledgement) and use the same command names and units.

Every command gets exactly one reply line, so a client can match replies to commands by sending one command at a time.

GUI: gui/OAMControl.html, a single-page web app that talks to the Pico over USB serial with the Web Serial API (Chrome or Edge, desktop; open the file directly, no server). It polls GET four times a second, sends SET_*, locks the parameter fields while armed (arming is only by the ARM input), shows the pulses left and the measured frame and line periods, warns when lines + delay + duration + 2 us is not shorter than the measured frame period, and sends the timing SETs in an order the Pico accepts. A MATLAB client can use the same commands with serialport().

Planned additions: STATUS (counters, firmware version), SAVE (store config in flash), FIRE (manual test pulse, bench only), range checking with an error reply, and a small Python client for the stimulus computer (e.g. PsychoPy).

  1. Safety

  • The gate output must be low by default: at boot, reset, during flashing and after a crash (external pull-down, watchdog forces GATE low).
  • The PMTs are closed whenever GATE is high: PMT goes high before GATE and low only after GATE is low again.
  • The PMT output is low (PMTs open) at boot, reset and flashing (external pull-down).
  • Duration cap in firmware: duration + 2 us <= 10 ms (H11706 shutter limit), checked by SET_PULSE and applied again when a run is armed. The frame check (Q14) additionally keeps each gate inside its frame.
  • Reject out-of-range or malformed parameter values; never apply a partially parsed value.
  1. Non-goals

  • No control of other laser parameters (power, rep rate, pulse mode); these stay in the Coherent GUI / laser serial interface.
  • No WiFi in the first version.
  1. Acceptance tests

Measure with an oscilloscope on TRIGGER, LINE, PMT and GATE (and the laser photodiode or Sync output where available):

  • 1000 triggers at minimum delay, maximum delay and maximum trigger rate.
  • Line counting: with line_count = N, the delay starts at the onset of line N + 1 (the Nth LINE edge after the one that coincides with TRIGGER), for N = 0, 1 and max_lines - 1 in both scan modes, every frame, also when LINE leads or lags TRIGGER by a few hundred ns.
  • PMT guards: PMT rises >= 1 us before GATE rises and falls >= 1 us after GATE falls, on every pulse.
  • Measurement: frame_us, line_ns and frame_lines in GET match the oscilloscope, and read 0 within 2 s of stopping the microscope.
  • Frame check: SET_LINES, SET_DELAY and SET_PULSE that make lines + delay + duration + 2 us >= frame period are rejected; changes that shorten the sequence are accepted. At the longest accepted setting, PMT falls before the next TRIGGER on every frame.
  • Shutter limit: SET_PULSE above 9998 us is rejected.
  • Report latency, jitter and duration error against section 5.
  • Verify GATE stays low through power-up, reset and flashing.
  • Verify Gate 1 reads high reliably at the buffer output (> 4.5 V).