Case study · OpenHW on ALINX · Episode 11

CVE2 meets an OV5640: SCCB from all-NACK to 0x5640, with 720p60 bars still running

This episode does exactly two things: teach CVE2 to identify the on-board OV5640 over SCCB, and keep the 720p60 colour bars running on the same board. The SCCB master runs at 100 kHz with 16-bit register addresses and 7-bit device address 0x3C, reading 0x300A=0x56 and 0x300B=0x40, i.e. 0x5640. The video side is a pure hardware test pattern. There is no image capture path in this episode, so the monitor will never show a camera picture — worth stating up front so the bars are not mistaken for a failure.

Verified on hardware · 2026-11-23 CVE2 · 50 MHz · RV32IM OV5640 / AN5641 · SCCB 100 kHz HDMI 1280×720@60 · pclk 75 MHz 11 / 12
0x5640
OV5640 chip ID read from 0x300A / 0x300B
0x00000078
Only address answering the full scan, 7-bit 0x3C
1,251
SCL edges counted for one identify plus the full scan
60.8 fps
145 frames in 2.384 s; design value 60.6 Hz
+0.861 / −0.850 ns
WNS / WHS over 21,780 endpoints, 7 hold endpoints failing
7,714 LUT · 13.5 BRAM
37.09% LUT, 27% BRAM, 10 DSP
2,192,137 B
r31 bitstream, md5 7945bb0a…
Scope first: there is no camera-to-monitor path in this episode. The project rtl/ contains only cam/sccb_m.sv, video/video_pattern.sv, video/video_timing.sv, dvi/dvi_tx_all.sv and the Ethernet set; mipi, csi, dphy and frame_buf match nothing. EP11 identifies the sensor and exposes a register window to software; a picture path needs another episode with MIPI CSI-2 capture and a frame buffer. The bars plus a moving marker are the expected result here, not a failure.
EP11 on hardware: ten colour bars and the moving dark-grey marker on the monitor, with the OV5640/AN5641 module on the board at right
Captured on hardware at 16:38 on 2026-11-23: ten colour bars plus the moving dark-grey marker, with the board-mounted OV5640 / AN5641 module on the bubble wrap at right. This episode has no picture path, so no camera image can appear — the photo proves the bars and SCCB running together, not a capture chain.

On hardware: identify the sensor, scan the bus, hold 60 fps

The order is “prove the CSR window is alive, then prove the bus talks, then prove the address, then prove the camera activity does not disturb video”. Every line below comes from one power-up capture, nothing written after the fact.

StepActionResult
1 · CSR checkRead 0x4007_0000CAM_CSR_ID=45501101, window mapped correctly
2 · Polarity sweepCAM_GPIO (M3) at 1 then 0, 200k idle loops, read ID each timeGPIO=1 answers; GPIO=0 leaves the whole bus silent (scan NONE)
3 · Chip IDRead 0x300A, then 0x300B0x56 / 0x40 → SENSOR_ID=0x5640, STATUS=0
4 · Address scan7-bit addresses 0x08..0x77, reading 0x300AOnly 0x78 (0x3C) answers; no address conflict
5 · Video lockPoll CAM_VID bit0 forty timesVID_LOCK=1 throughout; camera traffic does not disturb the pixel clock
6 · Frame rateFrame counter plus mcycle145 frames / 119,189,892 cycles = 2.384 s → 60.8 fps

Step 2 is deliberate: the AN5641 enable pin has no public documentation, so both levels are swept. With GPIO=1 the edge counter reaches 1,251 and the scan answers; with GPIO=0 the bus is completely silent and the scan prints NONE.

Key serial evidence

The full 3,172 B capture is in the file list (logs/ep11_uart_r31.log). First screen and the last lines below, with the 40 T n polls trimmed.

EP11 CAMERA SCCB + HDMI 720P60
CAM_BASE=0x40070000 DEV=0x3c
CAM_CSR_ID=45501101
CAM_LINE_SCL_SDA=00000003
VID_LOCK=1
GPIO=1 SENSOR_ID=0x00005640 SCCB_STATUS=0x00000000 LINE=00000003
SENSOR_ID=0x00005640 SCCB_STATUS=0x00000000
SCCB_SCAN ACK: 0x00000078
SCL_EDGES=1251
CAM=DETECTED OV5640 AN5641
T 0 VID_LOCK=1 SCCB_OK=1 SENSOR_ID=0x00005640 FRAMES=12 CYC=9399295
...
T 39 VID_LOCK=1 SCCB_OK=1 SENSOR_ID=0x00005640 FRAMES=157 CYC=128589187
EP11 DONE

Compare with r28 (before the fix): SCCB_STATUS=0x4, SCCB_SCAN ACK: NONE, SCL_EDGES=1131 — same module, same firmware flow, the difference was entirely the SCL rate in RTL.

Three RTL defects, each seen on the board first and reproduced in simulation

The real output of this episode is not “one ID read”, it is taking the SCCB master from all-NACK to stable. All three defects live in rtl/cam/sccb_m.sv.

  1. SCL rate out of control. The divider was written as a 4-bit ph counter compared against PDIV-1; with PDIV=125 the compare value is 124 and a 4-bit register tops out at 15, so the terminal count is never reached — the divider behaved like 16. On the scope SCL was 320 ns per period (≈3.1 MHz) with 20–40 ns high time, far outside what an OV5640 can sample. Fix: ph became a 2-bit four-phase value, a separate 8-bit div does the /125, the FSM advances only on phase boundaries, and start latches into req so a host write while busy is not lost.
  2. Only 7 of 8 bits read back. With SCL fixed, SENSOR_ID=0x2B00, exactly the 7 high bits of 0x5640. One cnt served both “bits left” and “bit index”: the sample completed at ph3 when cnt==1, but cnt had already decremented at ph2, dropping the LSB. Fix: ph2 only samples and shifts, ph3 decrements and tests separately. Then 0x300A returned 0x56, but the following 0x300B still NACKed.
  3. ACK instead of NACK at the end of a read. Symptom: the first read always works, the second always NACKs, and a power cycle restores “first read works”. The last bit of an SCCB read is the master’s ACK/NACK; returning ACK tells the OV5640 “send more”, so the slave keeps driving SDA and the following START is swallowed. Fix: ST_MACK drives sda_o high on its first phase (NACK) and then issues STOP. Result: SENSOR_ID=0x5640, STATUS=0.
RevisionBitstream md5Board behaviour
r28a8c4d72b014a2797ac41d768fb97d5ddAll NACK, SCL ≈3.1 MHz, scan NONE
r2982e9bdc9ca4babcd7ec84de81d0b30fcSCL 100 kHz fine, read returns 7 bits (0x2B00)
r302e71cb3b5128281397b3f593dda646c10x300A reads 0x56, 0x300B still NACKs
r317945bb0a02bdb564bdc0f309b37d2522ID 0x5640, STATUS 0, video untouched

Both pre-fix sources ship with the bundle: rtl/sccb_m_r28_before_fix.sv (four-phase divider) and rtl/sccb_m_r30_before_nack_fix.sv (ACK still returned).

SCCB timing and the register window

From the 50 MHz system clock a /125 divider plus four-phase gating yields a 100 kHz SCL with 50% duty. Each byte is 9 SCL pulses (8 data + 1 acknowledge), and the 16-bit register address goes out as two bytes. A read is “device write + repeated START + device read + data + master NACK + STOP”.

start
  -> dev + W(0x78)
  -> reg_hi, reg_lo
  -> [read] repeated START -> dev + R(0x79) -> data[7:0] -> master NACK -> STOP
  -> [write] data[7:0] -> slave ACK -> STOP

Software reaches it through 11 registers based at 0x4007_0000:

OffsetNameMeaning
+0x00IDWindow magic 0x45501101
+0x04CTRLbit0 GO one-shot, bit1 RD selects a read
+0x08DEV7-bit device address, fixed at 0x3C here
+0x0CREG16-bit sensor register address
+0x10WDATAWrite data
+0x14RDATARead data
+0x18STATUSbit0 busy, bit1 done, bit2 nack
+0x1CVIDbit0 video lock, bit2:1 SCL/SDA levels
+0x20GPIOCamera module enable, pin M3
+0x24FRAMESVideo frame counter
+0x28SCL_EDGESCL edge counter

Video path: 720p60 bars, and the extra band

Video is pure hardware: video_timing generates 1,650 × 750 (1,280 × 720 active, pclk 75 MHz), video_pattern picks the colour per pixel, dvi_tx_all does TMDS encoding and serialisation. Bars are 128 px wide, an 80×80 dark grey marker moves +8 px in X and +4 px in Y per frame with bounce, and a 4 px border frames the picture.

Cosmetic detail, recorded as-is: the colour index only uses x[9:7], so the first 1,024 px carry 8 bars; in the remaining 256 px the index wraps back to bars 0/1, and the screen shows white and yellow repeated plus the 8 defined bars = 10 bands. The file header still claims “8 colour bars”. It does not affect lock or frame rate, and was not changed in this episode.

60.8 fps is computed from the frame-counter delta over the mcycle delta; the nominal 1,650 × 750 @ 75 MHz is 60.606 Hz, so the 0.4% difference sits inside the counting window’s quantisation.

Why defect 3 needed simulation: a hand-written SCCB slave

The board only shows “first read works, second NACKs”; it cannot show who is holding SDA. So a clocked SCCB slave model was written (sim/sccb_slave.sv): it samples SCL edges at 50 MHz, acknowledges after the eighth bit, drives data[7] on the first beat of a read, and returns 0x56 for 0x300A and 0x40 for 0x300B, matching the real part. The model had two bugs of its own: a 2-bit phase assigned 2'd4 (truncated to 0) and read data driven one phase late. Once fixed, xsim reported rdata=0x56 nack=0, and the board confirmed the second-read NACK was caused by the master returning ACK, not by the slave.

Timing and resources

+0.861 ns
WNS, 0 failing of 21,780 endpoints
+7.909 / +0.135 ns
New video clock group, 272 endpoints all met
−0.850 ns
Worst hold, rgmii_rx→rgmii_tx, 5 endpoints
−0.023 ns
sys_clk hold on 2 endpoints, BRAM→LSU read data
37.09%
7,714 LUT of 20,800; 8,210 FF (19.74%)
13.5 / 50
BRAM 27%; 10 DSP (11.11%); 52 IOB

Hold violations are not hidden: the worst −0.850 ns sits in the rgmii_rx → rgmii_tx inter-clock table (5 endpoints), an Ethernet RX→TX clock relationship inherited from EP10 rather than new cam/video logic; the new video group meets timing on all 272 endpoints. A separate sys_clk hold of −0.023 ns (u_dmem BRAM → load_store_unit read data) coexists with +0.980 ns setup, and CPU, UART and SCCB all behave on the board.

Limits and untested items

Reproduce

# build (Vivado 2025.2, xc7a35t-2fgg484, roughly 5–6 minutes)
vivado -mode batch -source build_ep11.tcl -log build_ep11_r31.log
vivado -mode batch -source program_ep11.tcl

# firmware: after editing main.c, repack into BRAM and rebuild
cd sw && make clean all

# serial capture: start cat before programming, or the first screen is lost
stty -F /dev/ttyUSB1 115200 cs8 -cstopb -parenb raw -echo
nohup timeout 200 cat /dev/ttyUSB1 > /tmp/u31.log 2>/dev/null < /dev/null &
vivado -mode batch -source program_ep11.tcl

Firmware is packed into BRAM through INIT_FILE, so editing main.c — or the SCCB timing — requires a rebuild. The programming script needs an absolute -source path; a relative path fails with couldn't read file inside a combined SSH command.

Related

Evidence files

37 files, about 3.7 MB: bitstream, routed timing and utilisation reports, build/programming/serial logs (including the r28 all-NACK capture), RTL with both pre-fix sources, XDC, Tcl, firmware sources with ELF/HEX/disassembly, the simulation slave model and testbench, monitor photos, the defect write-up and structured metrics.

README.txtBundle notes and headline results · 5 KB SHA256SUMSSHA256 manifest for every file · 3 KB bit/top_cve2_ep11.bitr31 bitstream, 2.2 MB · 2.1 MB docs/EP11_SCCB三处缺陷与修正.mdDefect write-up · 5 KB logs/build_ep11_r31.logBuild log with final slack · 208 KB logs/ep11_program_r31.logJTAG programming log · 2 KB logs/ep11_uart_r28_all_nack.logr28 all-NACK capture · 4 KB logs/ep11_uart_r31.logr31 UART capture, 3.2 KB · 3 KB metrics/ep11_metrics.jsonStructured metrics · 3 KB photos/ep11_hdmi_frameA.jpgFrame A photo · 105 KB photos/ep11_hdmi_r31.jpgMonitor photo, 10 bars + marker · 105 KB photos/ep11_hdmi_screen_bars_frameB.pngFrame B bar screenshot · 841 KB reports/ep11_timing_summary_r31.rptRouted timing summary · 104 KB reports/ep11_utilization_r31.rptUtilization report · 12 KB rtl/dvi_tx_all.svTMDS encoder and serializer · 13 KB rtl/sccb_m.svSCCB master, final · 7 KB rtl/sccb_m_r28_before_fix.svBefore fix: 4-bit phase divider · 7 KB rtl/sccb_m_r30_before_nack_fix.svBefore fix: ACK at read end · 7 KB rtl/top_cve2_ep11.svTop level · 75 KB rtl/video_pattern.svColour bars and marker generator · 2 KB rtl/video_timing.sv720p60 timing generator · 1 KB sim/sccb_m_sim.svMaster simulation wrapper · 7 KB sim/sccb_slave.svHand-written SCCB slave model · 4 KB sim/tb_read.svRead transfer testbench · 1 KB sw/MakefileFirmware build · 1 KB sw/bin2hex.pyROM converter · 1 KB sw/firmware.disDisassembly · 27 KB sw/firmware.elfELF with symbols · 9 KB sw/firmware.hexFirmware image packed into BRAM · 36 KB sw/link.ldLinker script · 1 KB sw/main.cFirmware · 7 KB sw/start.SStartup code · 2 KB tcl/build_ep11.tclBuild script · 4 KB tcl/mkip.tclBRAM/FIFO IP generation · 13 KB tcl/mkip_clk.tclClock IP generation · 1 KB tcl/program_ep11.tclJTAG programming script · 0 KB xdc/pi_ar035_f1_1_cve2_ep11.xdcPin constraints · 8 KB

Subscribe to fpga.love

Daily Briefing · Weekly Digest · Monthly Deep Dive