Skip to content

Bebop 2 motor board (PSoC 3): pad byte and tick length

Static analysis of bebop2_mine.bin (flat image) and bebop1_mine.bin. Nothing was run, flashed or touched. Labels: EVIDENCE = flat-image address plus bytes/disassembly. INFERENCE = reasoning on top. UNKNOWN = not derivable. Disassembly command: r2 -q -a 8051 -b 8 -m 0 -e scr.color=0 -e asm.lines=false -e asm.xrefs=false -e asm.comments=false -c 's 0xADDR; pd N' file </dev/null (zsh note: echo == breaks in zsh, and xxd/r2 need </dev/null in the harness or they hang.)

Verdict: (a), with one qualification. The pad’s VALUE is only used as an input to the XOR check. It gates nothing else.

Section titled “Verdict: (a), with one qualification. The pad’s VALUE is only used as an input to the XOR check. It gates nothing else.”

Qualification: “only” is proven for every idata access I could find by direct operand and by immediate pointer loads, plus a structural argument about who owns the buffer. A pointer computed at run time into idata 0x65..0x84 cannot be fully excluded by a linear scan (see remaining items).

  • 0x53a4..0x53ac call 0x513b with r3:r2:r1 = 00:00:65, r5 = 0x20. 0x513b (0x5154..0x5171) stores r3:r2:r1 to xdata 0x1eb..0x1ed (write-buffer pointer), r5 to xdata 0x1e6 (size 0x20), and clears xdata 0x1ef (receive count). Generic pointer type 0 means idata (0x1c11: cjne r3,#1, below 1 falls to mov @r1,a; 0x1bf8 is the matching read). So the I2C write buffer is idata 0x65..0x84, size 0x20.
  • The sister call at 0x539d..0x53a1 + 0x50b5 sets the read buffer to idata 0x45, size 0x20 (xdata 0x1e8..0x1ea, 0x1f0).
  • The I2C slave ISR is at 0x2b31 (pushes at 0x2b31, I2C regs 0x49d6/0x49d7/0x49d8). Receive path 0x2c42..0x2c82: reads I2C_D (0x49d8), stores through the generic pointer at 0x1eb+count (lcall 0x1c11 at 0x2c79), increments xdata 0x1ef. So received bytes land straight in idata 0x65.. as they arrive. There is no copy loop into XDATA 0x42.., and nothing else writes the buffer.
  • The ISR calls the command handler 0x1fb5 at 0x2bac (INFERENCE: this is the stop/write-complete case, state byte xdata 0x1fc == 0x11).
  • The receive count is NOT checked for cmd 0x02 (EVIDENCE: no read of 0x1ef on that path). cmd 0x82 (0x22e4, 0x22f4) and cmd 0x20 (0x2097) do check it.

Dispatch (EVIDENCE, table bytes at 0x1fc4, entries are hi,lo,key; terminator 0000 then default 23a2)

Section titled “Dispatch (EVIDENCE, table bytes at 0x1fc4, entries are hi,lo,key; terminator 0000 then default 23a2)”

xxd -s 0x1fc4 -l 0x28 bebop2_mine.bin gives: key 02 -> 1fe6, 20 -> 2088, 40 -> 219c, 4d -> 2188, 60 -> 21b7, 80 -> 21d3, 82 -> 22b4, 8d -> 2386, a0 -> 2200, fa -> 2296, default -> 23a2. Lookup routine 0x1efe (compares key against entry byte +2). Correction to the brief: 0x2088 is the handler for cmd 0x20 (it builds the READ reply in idata 0x45.. from xdata 0x42.., 0x8b.., 0x80.., and XORs a check into it, loop 0x216a..0x2186). It does not parse set_ref_speed. The set_ref_speed handler is the code that starts at 0x1fe6 itself.

The 0x02 handler (EVIDENCE, 0x1fe6..0x2086)

Section titled “The 0x02 handler (EVIDENCE, 0x1fe6..0x2086)”
  • 0x1fe6 75 43 0a: counter 10. Loop 0x1fe9..0x1ffa: dec 0x43, r1 = 0x65 + counter, r0 = 0x6f, [0x6f] ^= [r1]. Counter runs 9..0, so idata 0x65..0x6e inclusive (the pad byte is index 9, the last byte XORed). The accumulator is idata 0x6f itself, so the 11th received byte seeds the XOR and the result replaces it.
  • 0x1ffc..0x2001: mov r0,#0x6f; mov a,@r0; jz 0x2003; clr 0x20.5. Nonzero means bad frame: 0x20.5 cleared. 0x2003: jb 0x20.5,0x2009 else ljmp 0x23a4.
  • 0x2009..0x2016: requires xdata[0] == 3 or 4, else ljmp 0x23a4.
  • 0x2019..0x2086: k = 3..0, hi = idata[0x66+2k], lo = idata[0x67+2k] (add a,0xe0 is ACC doubling, then add a,#0x66 / #0x67), clamp to [0x05DC, 0x3A98], store to xdata 0x2a+2k, 0x2b+2k. This reads 0x66..0x6d only. 0x6e is never read here.
  • Common exit 0x23a4: clears bits of xdata 0x1e7, clears xdata 0x1ef, then mov c,0x20.5; mov 0x20.4,c (0x23b0/0x23b2). 0x23a2 (default/unknown cmd) does clr 0x20.5 first. 0x1fbc sets 0x20.5 at dispatch start (d2 05).

Every idata 0x65..0x84 access found (EVIDENCE, scripts scan_idata.py, scan_ptr.py)

Section titled “Every idata 0x65..0x84 access found (EVIDENCE, scripts scan_idata.py, scan_ptr.py)”
addr what reads 0x6e?
0x1fbe mov r0,#0x65; mov a,@r0 command byte no
0x1ff0/0x1ff5/0x1ffc XOR loop and check (above) yes, XOR only
0x202a/0x2033 speed bytes 0x66..0x6d no
0x2188 cmd 0x4d reads 0x66 only (bit 1 -> GPIO 0x51c0 bit 0) no
0x22be, 0x22e8 cmd 0x82 reads 0x66 no
0x2345 cmd 0x82 (count==6) reads 0x67..0x6a -> xdata 0xbd..0xc0 no (max 0x6a)
0x2386 cmd 0x8d reads 0x66,0x67 -> idata 0x08..0x0b (watchdog reload and countdown) no
0x53a8 write-buffer init (pointer only) n/a
Direct operands in 0x60..0x7f and immediate pointer loads in 0x40..0x90 over 0x1900..0x5e00 were scanned; all other hits are arithmetic constants (for example subb a,#0x64) or config-table pointers (0x2ab6 mov r1,#0x62 is the low byte of xdata address 0x5162 passed to the copy routine 0x5446). No other code names idata 0x6e.
  • Watchdog 0x541f (EVIDENCE): 0x5425 jb 0x20.4,0x543d reloads idata 0x08:0x09 from 0x0a:0x0b and clears 0x20.4; otherwise it counts down and raises errno 4 via 0x572e at 0. Only 0x20.4 matters. 0x20.4 = 0x20.5 at the end of each frame.
  • So the pad value reaches the watchdog only through the checksum: for cmd 0x02, a frame whose XOR over 0x65..0x6e (seeded by the received 0x6f) is nonzero clears 0x20.5, which means no activity credit. A nonzero pad is accepted if the check byte accounts for it (INFERENCE from the loop, trivially true of XOR). ArduPilot always sends 0, so this never arises.
  • Errno 3 path (0x26c4, filtered speed estimate): no read of idata 0x6e exists anywhere near it, and the speed estimate is not fed from the I2C buffer. The errno-3 rule itself was not re-derived, as instructed.
  • Other commands do not run the XOR check at all (only 0x02 does), so their 0x20.5 stays set and they count as activity regardless of content.

Side finding (INFERENCE, depends on run-time state)

Section titled “Side finding (INFERENCE, depends on run-time state)”

The length is never checked for cmd 0x02 and the check seeds from idata 0x6f. After a good 11-byte frame, 0x6f is left at 0 (the XOR result). A following 10-byte frame [02 + 8 bytes + xor] puts its xor at 0x6e, stale 0x6f is 0, so the loop sums to 0 and the frame is accepted. That is, a 10-byte frame (no pad) would pass right after a good 11-byte frame. After a bad frame 0x6f is nonzero and it would fail. I did not execute anything; this is read off the loop. Worth a note, not a recommendation.

Same structure: write buffer at 0x4fa8 (mov r1,#0x65), dispatch at 0x1ed0, key table xxd -s 0x1ed8 -l 0x28 bebop1_mine.bin, XOR loop at 0x1ef8 (75 43 0a, 0x65 + counter, accumulator 0x6f), check at 0x1f0e..0x1f13 (clr 0x20.5), speed reads add a,#0x66/#0x67 at 0x1f3c/0x1f45, common exit 0x2234..0x2242 with mov 0x20.4,c. idata 0x66 only at 0x20b1, 0x20d0, 0x220e, 0x2216 (0x66/0x67 for the watchdog-reload command, 0x2216..0x2230 writes idata 0x08..0x0b). No direct 0x6e read. Same verdict: (a).

  • A pointer computed at run time into 0x6e would escape the immediate-pointer scan. I found none, and the only code that holds a pointer to this buffer is the ISR, but this is a bounded search over linear-sweep disassembly (data tables inside the code range make the sweep imperfect), not a proof.
  • Behaviour on silicon (does the master ever send an 11th byte that is not the pad?) is not an image question.

Verdict: UNKNOWN. The image does not contain the clock-distribution settings that would give it.

Section titled “Verdict: UNKNOWN. The image does not contain the clock-distribution settings that would give it.”
  • 0x4dee spins on 0x6462 bit 0 (0x5da2 mov dptr,#0x6462; movx a,@dptr; mov r7,a; ret) then writes 0xF9 to 0x6401 (0x5db4, accessors at 0x5dae..0x5dc0). PSoC 3 register names for 0x6401/0x6462 (UDB01 A0 / UDB02 status-class register) are my memory of the register map, not verified, and which UDB owns them in the design is not decoded.
  • The configuration table consumed by routine 0x533d (called at 0x29a5 with r6:r7 = 0x5e4c) decodes exactly: 39 records starting at 0x5e4c, ending exactly at 0x6fc4 where the next table begins. Record format from the routine: byte count of records, then per record [SFR 0xEA][SFR 0xA0][n] and n pairs (low address, value) written by movx @r0. python3 decode_cfg.py prints the records. Pages are 0x004f, 0x0052, 0x0064, 0x0065 and 0x01_00..0x01_50 (2177 byte writes in total). The 0x01_xxxx pages are, by my memory of the PSoC 3 map, the UDB array configuration (PLD, datapath, routing); I did not check this against a datasheet.
  • 0x6fc4 table: 8 zero-fill entries of 5 bytes (r3 r2 r1 len16, loop 0x2919..0x2950): 0x49c8 len 0x15, 0x4f00 len 0x30, 0x5100 len 0x10, then 0x01_0000.. blocks. 0x7004: 2 copy entries of 7 bytes (loop 0x2955..).
  • None of these tables writes any clock register. The clock-tree bytes decoded from code (these are the only explicit clock-area writes in 0x1900..0x5e00, found by scanning for 0x42xx/0x47xx operands, scan_regwrites.py):
    • 0x33e4: xdata 0x4300 <- 0x02. 0x33ea: 0x4000 <- 0x08. 0x33f0: 0x4200 <- 0x01.
    • 0x3405..0x340d (helper 0x5a4c writes r5 to [addr], r4 to [addr+1]): 0x4222 <- 0x10, 0x4223 <- 0x01. 0x3412..0x341a: 0x4220 <- 0x12, 0x4221 <- 0x51.
    • 0x3447: polls 0x4225 bit 0, then the code at 0x344e..0x3457 combines it into xdata 0x1a5 (consistent with waiting for a PLL lock bit, INFERENCE from the register name by memory).
    • 0x34a1..0x34ba: 0x4004 <- 0x00, 0x4005 <- 0x01, then 0x4004 <- 0x07, 0x4006 <- 0x00, 0x4008 <- 0x48 (not interpreted).
    • 0x4880/0x4888 are only READ at startup (0x3f0f..0x3f16).
  • No code in the app writes 0x4700..0x470b (CLKDIST master/bus/UDB dividers) and no table does. I searched raw bytes for 90 47 xx and 7a 47 79 xx: the only 0x47xx operands are 0x4720/0x4722 (SPC, per the notes) and nothing in 0x4700..0x470b. Also no DCFG/ACFG writes (0x4880..0x49ff) other than the I2C regs at 0x49c8..0x49db.
  1. IMO frequency: 0x4200 <- 0x01 is written, but what 0x01 selects needs the datasheet. Assumption A (my recollection of the PSoC 3 IMO_CR frequency field): value 1 = 6 MHz. Assumption B: 3 MHz. Neither is verified here, so no frequency is stated.
  2. PLL: values 0x12, 0x51 (0x4220/0x4221), 0x10 (0x4222), 0x01 (0x4223) are decoded as bytes. Their meaning (source, enable, P and Q encoding) is not derived. If the bus were derived from PLL output the arithmetic would need those encodings.
  3. Master clock select and bus divider (CLKDIST 0x4704.. region): UNKNOWN, never written by the app. Either they keep the bootloader’s setting (the bootloader is not in this image, 0x0000..0x18FF) or they are at reset defaults. I cannot tell which from the file.
  4. UDB clock: which clock-distribution output feeds the timer’s UDB, and its divider (CLKDIST DCFG registers, 0x4880+), is not set by the app. Which UDB drives 0x6462/0x6401 and what its clock input is requires decoding the routing/PLD bytes in the 0x01_xxxx records plus that clock side.
  5. The 250-count period (0xF9 reload) is therefore not convertible to time: tick = 250 / f_udb_clock and f_udb_clock is UNKNOWN. Not filled by the 1 ms hint.

Cross-check available inside the image (EVIDENCE, no clock needed)

Section titled “Cross-check available inside the image (EVIDENCE, no clock needed)”

Code counts 1000 ticks (0x03E8 reload of xdata 0xa4:0xa5, per the previous note) for a “second” of flight time. That is only the firmware’s own assumption, not a measurement. It supports “designed for 1 ms” only if the designer chose it; it is not derived from clocks.

  • The bootloader image (0x0000..0x18FF), or a PSoC Creator project / cyfitter_cfg.c / cyfitter.h for this board: gives CLKDIST settings.
  • A read of the live registers 0x4200, 0x4220..0x4225, 0x4700..0x470b, 0x4880+ via the debug interface or any runtime dump (not done; forbidden here).
  • Datasheet: IMO_CR encoding, PLL_CFG0/CFG1 bit meanings, CLKDIST layout.
  • Or a measurement: scope the main-loop pacing or the I2C-loss timeout (1025 ticks) on a bench unit.
  • scan_idata.py: lists idata 0x60..0x7f direct operands and immediates in the linear listing (python3 scan_idata.py bebop2_mine.lin).
  • scan_ptr.py: lists mov r0/r1,#imm pointer loads in 0x40..0x90.
  • scan_regwrites.py: lists explicit register writes in 0x4000..0x4fff.
  • decode_cfg.py: decodes the 0x5e4c register-write table (writes cfg_writes.json).
  • bebop2_mine.lin / bebop1_mine.lin: r2 linear listings used by the scans (regenerate with pd 15000 from 0x1800). All under the scratch folder.
  1. Does a run-time-computed pointer ever land in idata 0x65..0x84? (Not found; only a bounded search.)
  2. Does the 10-byte-frame-after-good-frame acceptance really occur on silicon? (Read off the loop only.)
  3. What clock config does the bootloader leave in CLKDIST, and does the app rely on it?
  4. Which UDB is behind 0x6401/0x6462, and what feeds its clock input?
  5. Meaning of 0x4200=0x01, 0x4220/21=0x12/0x51, 0x4222/23=0x10/0x01, and 0x4004..0x4008 writes.
  6. What are the 0x4004/0x4006/0x4008 writes at 0x34a1..0x34ba for (not interpreted)?

Comments

Spotted a mistake, or tried this on your own Bebop? Leave a note. Comments are kept on this site's own server.