Skip to content

examples: pru_i2c: SMBus protocols, PEC, open-drain bus with clock stretching (stacked on TI #171) - #2

Open
pratheesh wants to merge 2 commits into
fix/pr137-pru-i2c-reviewfrom
feat/pru-i2c-smbus
Open

pratheesh wants to merge 2 commits into
fix/pr137-pru-i2c-reviewfrom
feat/pru-i2c-smbus

Conversation

@pratheesh

Copy link
Copy Markdown
Owner

SMBus support for the PRU I2C master, stacked on TexasInstruments#171. Merging this into fix/pr137-pru-i2c-review adds it to TexasInstruments#171.

What it adds

SMBus protocols (commands 0x14..0x1C, which were stubs): quick command, send/receive byte, write/read byte, write/read word, block write and block read.

  • A transfer has a write phase and, for the read protocols, a repeated START followed by a read phase. The write phase is the command code, then the block count for block writes, then the Tx data.
  • The command code and block count are staged in the two bytes just below the Tx buffer, so the existing byte engine sends one contiguous stream.
  • Block read returns the target's byte count in the count register and the data in RX[0..N-1].
  • SMBus always sends START and STOP and NACKs the last byte read, whatever CON is set to.
  • 10-bit addressing is rejected with ADDRESSING_MODE_FAILED.

PEC (CON bit 6, ICSS_I2C_PEC_BIT): CRC-8 (polynomial 0x07), updated bit by bit over every address and data bit. It is appended to write-only transfers and checked on reads. A mismatch returns the new response PEC_ERROR (0x050F).

Open-drain bus. SCL and SDA are never driven high:

  • R30 keeps both pin bits at 0.
  • Each line is switched with its OUTDISABLE bit in ICSSMx_PRUy_GPIO_OUT_CTRL, through a shadow register, so each edge costs a single store.
  • The SDA direction macros are gone, and the master can no longer fight a target on either line.

Clock stretching. Every state that runs after an SCL release waits for SCL to read high.

  • New SCL-low timeout at instance offset 0xE8 (ticks; 0 = none), which ends the transfer with TIME_OUT_ERROR.
  • After a stretch, the high period is counted from the real rising edge.

Errors end with a STOP: address NACK, data NACK, and a bad block count.

Bus timing (UM10204 Table 10; SMBus uses the Standard-mode values):

  • START hold, repeated-START setup and hold, and STOP setup are now two ticks. One tick is 2.5 µs at 100 kHz, under the 4.0/4.7 µs minimums.
  • The "400 kHz" tick is now 680 IEP counts, which gives 367.6 kHz. With 625 counts the two-tick SCL low was 1.25 µs, under the Fast-mode tLOW of 1.3 µs. 100 kHz and 1 MHz are unchanged.

Removed: the unreachable CMD_CODE_* and *_BURST states, and the SMBus burst CON latch (bit 5 is now reserved).

Hardware note: SCL now needs a pull-up and its input path (pad configured with rx), the same as SDA.

Verification ([BUILD+MODEL], not run on hardware)

All results are from the same pru-simulator harness as TexasInstruments#171: real clpru ELFs, AM261x ICSSM map, wired-AND bus.

The SMBus target model was written separately from the SMBus 3.x spec without seeing this firmware, so it acts as an independent oracle. Its own suite is 61 tests, driven by a bit-banged reference master. Each check looks at three things: the firmware response, the target's record of the transaction (protocol, data, PEC verdict), and a wire decode.

Results, the same under both IEP compare models (crossing and exact match):

Suite Result
SMBus 54/54
I2C regression on this firmware, with timing enforced 9/9

SMBus coverage:

  • All 9 protocols, with PEC off, PEC on, at 100 kHz, and with a target that stretches the clock.
  • Block write at 253 bytes, and 252 with PEC; 254 bytes and 0 bytes are rejected without touching the bus.
  • Block read of 1 byte, and a target that reports a count of 0.
  • PEC enabled against a target without PEC returns PEC_ERROR.
  • An absent address gets NACK + STOP.
  • SCL held low past the timeout returns TIME_OUT_ERROR, and the next transfer works.
  • SMBus framing still works with CON START, STOP and NACK cleared.
  • 10-bit addressing is rejected.
  • Clock stretching at 100 kHz across 10 release phases.

I2C regression coverage:

Bus timing is measured on the wire: tLOW, tHIGH, tHD;STA, tSU;STA, tSU;STO and tSU;DAT against the spec minimums.

Parameter 100 kHz "400 kHz" 1 MHz
tLOW 4985 ns (≥ 4700) 1335 (≥ 1300) 625 (≥ 500)
tHIGH 4985 (≥ 4000) 1345 (≥ 600) 625 (≥ 260)
tHD;STA 4990 (≥ 4000) 1350 (≥ 600) 630 (≥ 260)
tSU;STO 4990 (≥ 4000) 1350 (≥ 600) 630 (≥ 260)

Ablation: reverting each change on its own fails its scenario, 14 of 14. The reverts covered:

  • The stretch wait.
  • The post-stretch tick.
  • The timeout.
  • PEC update.
  • PEC check.
  • The block-length bound.
  • The block-count-0 check.
  • Forced NACK.
  • Forced START.
  • START hold, Sr setup and STOP setup.
  • The 400 kHz tick.
  • SMBus dispatch.

Builds: clpru 2.3.3 for PRU0 and PRU1 with 0 warnings. The regenerated pru0/pru1_load_bin.h are byte-identical to the simulated ELFs.

Model limits: no pad electricals or rise times, no real IEP compare semantics, the MSS_CTRL access latency is an estimate, and a single master. This needs a run on an AM261x-LP with a real SMBus device before it is merged upstream.

The R5F example (TexasInstruments#172) is updated to match: SCL rx and pull-up, both lines released before start, the timing-safe tick, the 25 ms SCL timeout, and SMBus documentation.

🤖 Generated with Claude Code

pratheesh and others added 2 commits October 2, 2026 14:47
SMBus (commands 0x14..0x1C, previously stubs):
  quick command, send/receive byte, write/read byte, write/read word,
  block write/read. A transfer is a write phase (command code and block
  count staged as prefix bytes in front of the Tx buffer, then the Tx
  data) and, for the reads, a repeated START and a read phase. Block read
  reports the target's byte count in the count register. SMBus always
  sends START/STOP and NACKs the last byte read, whatever CON says.
  10-bit addressing is rejected (ADDRESSING_MODE_FAILED).
PEC (CON bit 6, ICSS_I2C_PEC_BIT): CRC-8 (0x07) updated bit by bit over
  every address and data bit; appended to write-only transfers and checked
  on reads (new response PEC_ERROR 0x050F).
Open-drain bus: SCL and SDA are never driven high. R30 keeps both bits at
  0 and the lines are switched with their OUTDISABLE bits in
  ICSSMx_PRUy_GPIO_OUT_CTRL through a shadow register (one store per edge).
  The SDA direction macros are gone; the master can no longer fight a
  target on either line.
Clock stretching: every state that follows an SCL release waits for SCL
  to read high. A new SCL-low timeout (instance offset 0xE8, in ticks,
  0 = none) ends a transfer with TIME_OUT_ERROR. After a stretch the high
  period is counted from the real rising edge (tHIGH).
Errors now end with a STOP: address NACK, data NACK, bad block count.
Bus timing (UM10204 Table 10, SMBus = Standard-mode):
  - START hold, repeated START setup/hold and STOP setup are two ticks
    (one tick is 2.5 us at 100 kHz, below the 4.0/4.7 us minimums).
  - The 400 kHz tick is 680 IEP counts (367.6 kHz): with 625 the 2-tick
    SCL low was 1.25 us, under the Fast-mode tLOW of 1.3 us.
Removed: the unreachable CMD_CODE_* / *_BURST states and the SMBus burst
  CON latch (bit 5 is now reserved).

New interface fields: ICSS_I2C_PEC_BIT, ICSS_I2C_SCL_TIMEOUT_OFFSET,
PEC_ERROR; ICSS_I2C_PRU_CMD_CODE_OFFSET is the SMBus command code (bit 0
is the R/W bit for quick command); instance offsets 0xFE/0xFF are used to
stage the SMBus prefix bytes.

Hardware note: SCL now needs a pull-up and its GPI path (the pad
configured with rx), like SDA.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Pratheesh Gangadhar <pratheesh.gangadhar@gmail.com>
Byte-identical to the ELFs exercised on pru-simulator.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Pratheesh Gangadhar <pratheesh.gangadhar@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant