A camera sensor stays silent until power, clock and reset arrive in the right order. Bring up a virtual sensor, fix its init table, and make the test pattern appear.
Learn the first hour of every camera board: which rail comes up first, when the chip can hear you, what to write into it, and what “no link” really means.
Pick a board, then press Run bring-up. Watch the rails, the clock and the reset rise, then the first I2C question. Try all four boards. Then we build, piece by piece, every step the firmware takes.
You need to know what a voltage is and a little C. We build the rest from zero: I2C, clocks, resets, registers and the camera link.
Built from primary sources: the Linux imx219 and ov5640 sensor drivers, the IMX219 device tree binding, the kernel’s CSI-2 transmitter notes and the NXP I2C specification. No course slides. Lesson 1 of the camera track in Firmware from Zero to Expertise.
This is a model, not silicon. The pins and values are an IMX219-class sensor: I2C address 0x10, chip ID 0x0219, rails VDIG 1.8 V, VANA 2.8 V, VDDL 1.2 V, a 24 MHz clock, and a 6.2 ms wait after reset (all from the Linux driver and its binding). The model enforces the rail order I/O, analog, core, which many datasheets ask for. The real IMX219 driver says its rails may come up in any order, so treat the order rule as a teaching rule. Times are illustrative.
Chapter 0
See the four gates between a power switch and a picture, and learn why the first failure hides all the others
A new board arrives on your desk. A small camera module sits on a ribbon cable. You flash the firmware, press the button, and the screen stays black. No error. No log line. Nothing.
You have not written a bug yet. You have not even reached the code you plan to write. The chip on that ribbon is still asleep, or half awake, or waiting for something you never gave it. Making a chip like this answer is called bring-up: the first hours of a new board, when you prove each part works before you build on it.
Camera teams treat this as a core skill: bring up a new sensor on a prototype board with correct power, reset and clock sequencing, then keep the link stable. The same teams work with MIPI-CSI-2, USB Video Class or parallel RGB, and they own logic analyzers, oscilloscopes and JTAG probes. This lesson teaches the power, clock, reset and control half of that work from zero.
A camera sensor is a chip with a grid of light-sensitive dots (pixels), a small amount of logic that reads them out, and a transmitter that sends the numbers to your processor. Think of it as a tiny remote factory. Your processor does not reach in and take pixels. It starts the factory, sends it a work order, and waits for boxes to arrive on a conveyor.
Five things connect your board to that factory. Power rails are the supply wires, each at its own voltage (1.8 V, 2.8 V, 1.2 V in our model). A clock is a steady square wave that gives the chip its heartbeat. A reset wire holds the chip in a known empty state until you let go. A slow control bus, called I2C, carries your work orders. And a fast camera link, called MIPI CSI-2, carries the pictures back.
That list is also the order of the work. Each item only works if the one before it worked. Miss one, and everything after it fails in the same way: silence.
Picture four gates in a corridor. Gate one is power: all rails up, in the right order. Gate two is clock and reset: the clock runs, then reset lets go, then you wait. Gate three is control: you talk over I2C and the chip answers. Gate four is streaming: you write the work order, and pixels flow over the link.
If gate one is shut, gates two, three and four cannot open. The screen is black. If gate three is shut, the screen is black too. If gate four is shut, the screen is black again. One symptom, four possible causes. That is why bring-up feels hard: the symptom does not tell you which gate failed. Try it below.
Each button opens or closes one gate. The first closed gate turns red. Every gate after it turns grey, because it never got a chance to run.
A model of the idea, not of one chip. Gate 4 starts closed: you have not written the work order yet. Turn it on to see the picture arrive.
Look at what the device tells you. With gate one shut, the read-out says three gates never ran. You cannot judge your I2C code, because it never had a chance. The fix for this is a habit, and it is the plan for the whole lesson: open the gates in order, and prove each one with a measurement before you touch the next.
By the end you can look at a silent board and name the closed gate. You will read a power-up timing diagram. You will decode an I2C transaction on a logic analyzer. You will write an init table in the right order, turn on a test pattern, and tell a sensor fault from a link fault. The sensor here is a model of an IMX219-class part, with numbers from the Linux kernel driver. The ideas carry to any sensor.
One honest limit. Firmware cannot run on real silicon inside a web page. Every device here is a model that follows the real rules and numbers, and each core chapter ends with an “On real hardware” note that says which board to use and what to measure.
You also need three quick pieces of background, and we teach them where they are needed: what I2C is (Chapter 2), what a register is (Chapter 2), and what a differential serial link is (Chapter 7). If you have only ever written Arduino sketches, you are ready.
Chapter 1
Follow every wire on a camera module and see why a slow control road and a fast pixel road never share a wire
Turn a camera module over and count the pins. A few carry power. One brings in a clock. One is a reset. Two make a small bus for talking. The rest, usually in matched pairs, send pictures out. Every sensor you will meet has this same shape, even when the connector looks different.
The idea to hold on to is simple. There are two roads between your processor and the sensor, and they do very different jobs. One road is slow and talks both ways. The other is fast and runs one way only.
The control bus is how firmware asks the sensor to do things. On almost every camera sensor it is I2C (say “eye squared see”), a two-wire bus. One wire, SCL, carries a clock. The other, SDA, carries the data. Firmware writes a number into a named slot inside the sensor, or reads one back. Chapter 2 builds this from zero.
The buses in full (I2C, SPI, UART, USB) are the subject of buses on the wire. The path from sensor to CSI is the topic of the Physical AI lesson on sensors and CSI, which follows this one.
Control traffic is small and rare. A whole setup is a few hundred writes, then silence. But it must work in both directions, because you need answers: “what is your chip ID?” So this road is slow and two-way. The NXP I2C specification sets the speed limits: up to 100 kbit/s in Standard-mode, 400 kbit/s in Fast-mode and 1 Mbit/s in Fast-mode Plus.
The camera link carries the picture. On phones, robots and most Linux cameras it is MIPI CSI-2, a serial link where the sensor sends and the processor only listens. The wires come in pairs, called lanes: one clock lane and one, two or four data lanes. Chapter 7 explains the signalling.
Why is this road so much faster? Do the arithmetic for a small video. A 1920 by 1080 frame has 2,073,600 pixels. A RAW10 pixel (the raw sensor value, 10 bits) takes 10 bits. So one frame is 20.7 million bits. At 30 frames per second that is 622 megabits every second, and it never stops while the camera runs.
The Linux device tree example for the IMX219 sets the link frequency to 456 MHz and uses two data lanes. The link sends data on both edges of its clock, so each lane carries 912 Mbit/s. Two lanes carry 1,824 Mbit/s. That is 4,560 times the 400 kbit/s of the control bus. Try the picture below.
The host is on the left, the sensor on the right. Choose a road. Moving dots show which way it carries things.
Numbers: the I2C limits are from the NXP specification UM10204. The 456 MHz link frequency and two lanes are from the example in the Linux IMX219 device tree binding. Two bits per link clock cycle is how this kind of link works; the 1080p frame size is plain arithmetic, labelled illustrative because your sensor mode may differ.
Look at who does the work on each road. Your code drives the control road itself, one write at a time. Nobody can do that for the pixel road. At 622 Mbit/s no processor loop can read bits one by one. A dedicated block in the chip, the CSI receiver, catches the bits, rebuilds them into lines, and uses DMA (direct memory access, a copy engine that moves data without the CPU) to place whole frames in memory.
So firmware has two jobs. Control the sensor, by hand, over the slow road. And configure the receiver, once, so it can catch what the fast road delivers. If these two disagree, for example about the lane count, the pixel road delivers nothing. Chapter 7 is about exactly that.
Control: a write is a 16-bit register address plus a value; a read returns a value. Pixels: a stream of packets, each carrying one line of pixels, framed by a start-of-frame and an end-of-frame marker. Clock: a square wave, 24 MHz in the IMX219 driver, which the driver checks before it goes on. Reset: one wire, high or low. Power: no data at all, just the energy to run.
Here is the useful trick. The two roads are independent. So one test cuts the bug search in half. Symptom: black screen. Measurement: read the chip ID over control. Meaning: if the ID comes back right, gates one to three are open, the clock and power are fine, and your hunt moves to streaming and the link. If the read fails, the hunt stays on power, clock, reset and the address. You will use this split in every chapter that follows.
One more honest note. Full sensor datasheets are often only given to customers who sign an agreement. The Linux kernel sensor drivers are public, and they hold the same facts as code: the address, the chip ID, the delays, the table. That is why this lesson reads drivers, and why real teams read them too.
Chapter 2
Decode one I2C read, bit by bit, and learn what an ACK, a NACK, a register and a chip ID tell you
Imagine a classroom roll call. The teacher says a name. If that student is present, they say “here”. If nobody says anything, the teacher knows the seat is empty. I2C works the same way, and the “here” is the most useful signal in all of bring-up.
I2C has a controller (your processor, which starts every conversation) and one or more targets (the sensor and other chips). It uses two wires. SCL carries the clock, always driven by the controller. SDA carries the data, driven by whoever has something to say. Both wires have a pull-up resistor, a part that drags the wire to the high level when nobody is pulling it down. So the idle state is high, and any chip can pull a wire low.
A talk begins with a START: SDA falls while SCL is high. Then the controller sends the target’s address. That is 7 bits, followed by one bit for direction: 0 means the controller will write, 1 means it will read. Each bit rides one clock pulse, most significant bit first.
Then comes the ninth clock, and this is the part to remember. The controller lets go of SDA. If a target owns that address, it pulls SDA low during the ninth clock. That is the ACK, the “here”. If SDA stays high, that is a NACK: nobody answered. The NXP specification defines it exactly this way: when SDA remains high during the ninth clock pulse, that is the Not Acknowledge signal.
Our sensor model sits at 7-bit address 0x10, the address the IMX219 device tree binding gives. The first byte on the wire is therefore 0x10 shifted left one place, with the write bit on the end: 0x20 for a write, 0x21 for a read. Press the button below and watch these nine clocks.
The top trace is SCL, the bottom is SDA. The first nine clocks are the address byte and the answer. Choose an address, then press Read chip ID.
A model of the wire, not a capture. The sensor answers only at 7-bit address 0x10 and only when it is awake (here it is). Times are the bus clock period times the clocks in one transfer; start, stop and setup gaps are left out.
An ACK only proves someone is home. To learn who, you read a register: a numbered slot inside the chip that holds one value. Think of a wall of mailboxes, each with a number, each holding one small value. Some boxes you read, some you write, and writing certain boxes makes the chip do something.
Many camera sensors use 16-bit register addresses, because they have thousands of slots. So a read has two phases. First a write that sends the slot number, high byte then low byte. Then a repeated START (a START without a STOP, which keeps the bus), the address with the read bit, and the answer bytes. For the IMX219 the Linux driver reads register 0x0000 and expects 0x0219. That is the chip ID: the chip’s name tag.
On the wire, a good read looks like this, where A is ACK and N is NACK from the controller to end the read:
S 0x20 W A 0x00 A 0x00 A Sr 0x21 R A 0x02 A 0x19 N P
Count the bytes: six of them, each with nine clocks, so 54 clocks. At 400 kHz that is 54 times 2.5 microseconds, about 135 microseconds. At 100 kHz it is 540 microseconds, and at 1 MHz it is 54. A table of 500 writes, four bytes each (one register write is the address byte, two register bytes and the value), takes 500 times 36 clocks, or 18,000 clocks. At 400 kHz that is about 45 ms. So speed matters for start-up time, and almost nowhere else.
Symptom: every probe returns NACK, even though the chip is powered. Measurement: on the logic analyzer, the first byte is 0x10 where you expected 0x20. The firmware gave a 7-bit address to a function that wants an 8-bit one. Cause: the STM32 HAL expects the address already shifted left by one. If you pass 0x10 and then the HAL shifts it again, or the code shifts it twice, the wire carries a different address. Try the 0x08 button above: the byte is 0x10, nobody owns it, and the ninth clock stays high.
The meaning of a NACK also depends on when it comes. A NACK on the address byte means no chip owns that address right now: wrong address, no power, held in reset, or no clock. A NACK later in a transfer means the chip is home but refuses that byte. Reading that difference saves hours.
Chapter 3
Bring up three supply rails one after another, and see why a meter on the bench cannot tell you the order was wrong
You plug in the board. A multimeter on each supply pin reads 1.8 V, 2.8 V and 1.2 V. Exactly what the schematic says. And still the sensor never answers. The meter told the truth, and the truth was not enough.
The problem is time. The meter shows where each voltage ends up. It does not show which one got there first. For many chips, that order is part of the rules.
A rail is one supply wire at one fixed voltage. A camera sensor usually has several, because its parts need different things. In the Linux device tree binding for the IMX219, the three are called VANA, VDIG and VDDL. VANA is the analog supply, 2.8 V, for the pixel array and the circuits that measure light. VDIG is the digital I/O supply, 1.8 V, for the pins that talk to the outside world. VDDL is the digital core supply, 1.2 V, for the logic inside.
Each rail comes from a regulator, a small chip that turns a battery or a 5 V input into a steady lower voltage. Firmware turns a regulator on with an enable pin or a command over a bus. That means your code decides the order. A power domain is the part of the chip that one rail feeds.
Think of three rooms in a house with a shared hallway. The I/O pins are the front door to the hallway. If someone pushes through the front door while the rooms behind it are dark, current can flow where nobody planned. Inside real chips, each pin has small protection diodes to the rails. Drive a pin while its rail is off, and those diodes can pass current into the dead rail. The chip can power itself half-way through its pins, or wake with its logic in a state nobody designed.
That is why datasheets list a sequence. They differ. The Linux OV5640 driver lists its I/O rail (DOVDD) first, then the analog rail (AVDD), then the core rail (DVDD), and its power-up comment counts the first delay from “DOVDD stable”. The Linux IMX219 driver, on the other hand, carries a comment that its supplies “can be enabled in any order”, and it simply walks its list: VANA, then VDIG, then VDDL. Read your own part’s datasheet. This lesson’s model uses the common rule, I/O then analog then core, so you can feel what a violation looks like.
Another point of care: a rail does not jump to its voltage. It ramps over some time, and a regulator may report power good when it is steady. Start the next rail after the last one is good, not just after you gave the command.
In the device below, three sliders set when each rail rises. The clock and reset are fixed later (22 ms and 24 ms) and the probe comes at 40 ms. So only the rail order can change the result.
Drag each slider to set when that rail rises, from 0 to 20 ms. The scope below shows the rails, then the clock, the reset and the first I2C probe.
The order rule (I/O, then analog, then core, each strictly later than the last) is a teaching rule of this model. The Linux IMX219 driver says its rails may come in any order. The clock at 22 ms, the reset at 24 ms and the 6.2 ms wait are real driver numbers for the IMX219 (the wait) and illustrative placement (the rest).
Start with the defaults: VDIG at 0 ms, VANA at 2 ms, VDDL at 4 ms. The order is I/O, analog, core, each strictly later. Clock starts at 22 ms. Reset is released at 24 ms. The sensor is ready 6.2 ms later, at 30.2 ms. The probe at 40 ms comes after that, so it gets an ACK. Now drag VANA to 0 ms. VANA and VDIG rise together, which breaks the strict order, and the same probe at the same 40 ms gets a NACK. Nothing about the clock, the reset or the I2C code changed.
Symptom: every probe is NACK, but the rails read right on a meter. Measurement: use an oscilloscope with several channels, or a logic analyzer fast enough to see the enable lines. Trigger on the first rail and put the others on the same time axis. Now the order is visible, as in the device above. Fix: reorder the enables in firmware or in the device tree supply list, add a wait or a power-good check between rails, and power-cycle the board before testing again. A chip that was upset once may stay upset until its power falls fully.
That last point catches many people. After a bad sequence, writing the right sequence on top may still fail, because the chip never lost power. Every test of a new sequence needs a full power-off first.
Chapter 4
Start the sensor’s heartbeat, release its reset, wait the right number of milliseconds, and only then ask it a question
Your firmware boots in a few milliseconds. The rails are up. You release the reset wire and, on the very next line of code, you read the chip ID. NACK. You add a breakpoint on that line, run under the debugger, and it works. Remove the breakpoint and it fails again. A bug that hides when you look at it usually has one cause: you are asking too soon.
A sensor needs a steady clock before it can do anything. The clock input is often named XCLK or MCLK, an external clock that your processor or a small oscillator on the module supplies. Inside, the sensor multiplies this clock up with a circuit called a PLL (phase-locked loop) to make the fast clocks it needs for the pixel array and the camera link.
Sensors are picky about their clock frequency. The Linux IMX219 driver defines the clock as 24 MHz and refuses to start if the platform gives it something else. The OV5640 driver accepts a range, from 6 MHz to 54 MHz. A wrong frequency does not always stop I2C. It can quietly give you the wrong frame rate or the wrong link speed later, which makes it a nasty bug to find.
Reset is a single wire that holds the chip in a known empty state. On the IMX219 the pin is called XCLR. While it is held low, the chip ignores I2C and does nothing. When you raise it, the chip begins its own start-up. The Linux driver adds a comment that the XCLR pin uses a logic level that is the opposite of what a normal reset signal uses, so a GPIO value of 1 means the pin is high and the sensor runs. When you read any driver, check which level means “running”.
The driver’s power-on order is exactly the chapter title: enable the regulators, turn on the clock, set the reset GPIO to run, then wait. The wait is 6200 to 7200 microseconds: at least 6.2 ms, with up to 1 ms of slack. Power-off runs backwards: reset low first, then the regulators, then the clock.
That wait is the whole point of this chapter. After the reset is released, the sensor needs time to get ready. Until then, it does not answer. The OV5640 needs far more. Its driver holds reset, switches the power-down pin off for 5 to 10 ms, switches it back on for 1 to 2 ms, then releases reset and waits 20 to 25 ms before the first I2C write. A comment in the driver cites section 2.7 of the datasheet, the power-up sequence, for at least 20 ms from reset release to a stable I2C bus. So the wait is not a universal number. It is a property of the part, written in its datasheet, and copied into its driver.
The rails rise at 0, 2 and 4 ms. Set when the clock starts, when reset is released, and when you probe. The shaded band is the sensor start-up wait.
Wait values: 6.2 ms is the minimum delay in the Linux IMX219 driver; 20 ms is the minimum from reset release to stable I2C in the Linux OV5640 driver’s power-up code. The model uses one address for both so the only change is the wait. Rails are fixed at 0, 2 and 4 ms and the clock must start no later than the reset release (a model rule).
Take the defaults: the clock starts at 6 ms, reset is released at 8 ms, and the probe is at 9 ms. The sensor is ready at 8 + 6.2 = 14.2 ms. The probe is 5.2 ms too early, so the answer is NACK. Move the probe to 15 ms and it is 0.8 ms past ready, so it gets ACK. Now switch to the OV5640-class part: ready is 8 + 20 = 28 ms, and a probe at 15 ms fails again. The same firmware, a different chip, a different wait. This is why a bring-up routine takes its delay from a table of per-part numbers, never from a habit.
Two more cases are worth trying. Disconnect the clock, and no probe ever works. Set the clock start later than the reset release, and the sensor wakes without a clock, which this model treats as a failure too.
Symptom: the first chip ID read fails, a second try works, or it works only under the debugger. Measurement: put one scope channel on the reset line and one on SDA. Measure the time between the reset rising edge and the first I2C START. Compare it to the datasheet minimum. Fix: add a delay in the bring-up code (on a Cortex-M, HAL_Delay(8) waits at least 8 ms), and set it from the datasheet plus a margin. A polite extra: poll the chip ID until it answers or a 50 ms timeout passes. But still print the time it took, or the retry loop will hide a timing bug until a faster board arrives.
Chapter 5
Write the settings a sensor needs in the right order, and learn why an ACK on every write still does not mean it worked
The sensor answers. Chip ID 0x0219, as it should. You feel good, start the stream, and the receiver sees nothing. The chip is awake, but it has no work order. It does not know how many lanes to use, what pixel format to send, or whether to send at all.
A sensor wakes from reset in standby: powered, listening on I2C, sending nothing. To make it work, firmware writes a list of settings. That list is the init table: pairs of a register address and a value, written in order. Each entry is a small sentence: “put 0x01 into slot 0x0114”.
On the wire, one entry is a write transaction: the sensor address with the write bit, two bytes of register address (high first), then the value. For an 8-bit register that is three bytes after the address byte. Some registers are 16 bits wide. In the Linux IMX219 driver the data format register 0x018C and the test pattern register 0x0600 are both 16-bit, and a 16-bit value goes out high byte first. So writing 0x0A0A to 0x018C means two byte writes: 0x0A to 0x018C and 0x0A to 0x018D.
Real tables are long. The IMX219 driver has a common table of vendor-supplied entries, and its first few lines are plain: 0x0100 gets 0x00 (standby), then 0x30EB gets 0x05, then 0x0C. The same register, 0x30EB, is written four times in the first dozen entries, with the values 0x05, 0x0C, 0x05 and 0x09. The driver gives no description of these registers. The vendor says “write exactly this, in this order”, and you do. This is also why order is part of the data, not a style choice.
The Linux driver’s stream-start routine shows the pattern. First it powers the sensor. Then it writes the common table. Then it sets the lane mode (register 0x0114: value 1 means 2 lanes, value 3 means 4 lanes). Then it sets the pixel format (0x018C). Then it applies the controls such as gain and exposure. And only last does it write register 0x0100 with 1, which means “stream”.
The rule behind this order is easy to say. Configure in standby. Stream last. A sensor that is already streaming may ignore or half-apply changes to the settings that shape the stream. The model below enforces that rule so you can feel it.
Below is a table with five bugs in it. The receiver expects two lanes and RAW10 pixels. Tap a value to change it. Use the arrows to move a row. Keep going until the colour bars appear.
Each row writes one register. Tap the value button to cycle it. Use the arrows to reorder. The receiver on the right expects 2 data lanes and RAW10, and the sensor must be in standby while you configure it.
A teaching subset: five of the registers the Linux IMX219 driver sets (0x0100, 0x0114, 0x018C, 0x0600), shown as 16-bit values where the driver uses 16-bit registers. The real common table is much longer and holds undocumented vendor entries. That the sensor ignores other writes while streaming is a model rule.
In the buggy table, row 1 writes 0x0100 = 0x01. The sensor starts streaming at once. Rows 2 to 4 are then ignored, because the model sensor is already streaming. Row 5 sets standby. The result: the sensor ends up stopped, so the receiver sees no frame. Change row 1 to standby, and row 5 to stream. The writes now land in standby. But row 2 says 4 lanes, and the receiver expects 2, so you get no link. Fix the lane mode and the format is next: RAW8 into a RAW10 receiver gives a garbled frame. Fix that, set the test pattern to colour bars, and the bars appear. Five faults, five different symptoms.
Symptom: every write in the table returns ACK, and no picture comes. Why: an ACK on a register write says the chip took the byte. It does not say the value is right, or that the chip was in a state that could use it. Measurement: read the key registers back, 0x0100, 0x0114 and 0x018C, and compare them with the table. Fix: write the table through a function that returns the index of the first failed row, then read back what matters, and log both.
Here is that loop in C for an STM32 part, using the HAL:
typedef struct { uint16_t addr; uint8_t val; } reg8_t; static const reg8_t init_tbl[] = { { 0x0100, 0x00 }, /* standby: configure only here */ { 0x0114, 0x01 }, /* CSI lane mode: 2 lanes */ { 0x018C, 0x0A }, { 0x018D, 0x0A }, /* RAW10, 16-bit reg */ { 0x0600, 0x00 }, { 0x0601, 0x02 }, /* colour bars */ { 0x0100, 0x01 }, /* stream: always last */ }; int sensor_write_table(const reg8_t *t, size_t n) { for (size_t i = 0; i < n; i++) { if (HAL_I2C_Mem_Write(&hi2c1, SENSOR_ADDR << 1, t[i].addr, I2C_MEMADD_SIZE_16BIT, (uint8_t *)&t[i].val, 1, 100) != HAL_OK) return -(int)i - 1; /* name the row that failed */ } return 0; }
Seven entries at four bytes of traffic each (the address byte, two register bytes and the value) take about 28 bytes at 9 clocks each, or 252 clocks, well under a millisecond at 400 kHz. A real table of 500 entries takes about 45 ms. That is short next to the 6.2 ms wait, but long if you rewrite it on every mode change.
Chapter 6
Make the sensor draw a known image from inside, and use it to split every picture bug into two halves
The stream is on. The receiver gets a frame. The frame is black. Is the sensor broken? Is the lens cap on? Is the exposure at zero? Is the link dropping bits? Four very different bugs, one black rectangle.
You need a picture whose right answer you already know. Sensors give you one. It is called the test pattern, and it is the best debug tool on the chip.
A test pattern generator is a small circuit in the sensor that makes up pixel values by a fixed rule, and sends them down the link in place of the real ones. No light, no lens, no exposure matters. The Linux IMX219 driver exposes one register for it: 16-bit register 0x0600. Its modes are: 0 off, 1 solid colour, 2 colour bars, 3 grey colour bars, 4 PN9, 5 sixteen split colour bars, 6 the same inverted, 7 column counter, 8 inverted column counter and 9 PN31. For the solid colour, four more registers set the red, green and blue levels: 0x0602, 0x0604, 0x0606 and 0x0608.
Colour bars are the old television test image: vertical stripes of fixed colours. If the frame is 1920 pixels wide and there are 8 bars, each bar is 1920 divided by 8, which is 240 pixels wide. You know where each edge should fall, and what colour sits in each stripe.
Picture the path a real image takes. Light falls on the pixel array, the circuits read it, and the numbers are packed, sent over the link, caught by the receiver and placed in memory. The test pattern enters the path late, after the pixel array and before the transmitter. So it skips light, lens, exposure and the analog parts. It still crosses the link, the receiver and the memory copy.
That is a clean experiment. If the pattern arrives correct, the digital path is good, and any trouble with the real image is earlier: lens cap, exposure, gain, analog rail or the pixel array. If the pattern arrives wrong, the digital path is bad: lanes, speed, format, receiver set-up or DMA. (This holds for sensors whose generator sits after the array, which is the usual design. Check your datasheet.)
| Test pattern | Real image | Look here |
|---|---|---|
| Correct | Correct | Nothing. Move on. |
| Correct | Black, dark or noisy | Before the pattern: lens, light, exposure, gain, analog rail, pixel array. |
| Wrong | Wrong | After the pattern: link, lanes, format, receiver, DMA. |
| Wrong | Correct | The pattern register itself. Check what you wrote to 0x0600. |
Colour bars are good for the eye. A machine wants a pattern it can check by itself. PN9 is a pseudo-random bit sequence made by a 9-bit shift register with feedback. In the common definition the feedback uses the polynomial x9 + x5 + 1, and the sequence repeats every 29 − 1 = 511 bits. It looks like noise. But the receiver can run the same register from the same start, and compare bit by bit. Any bit that differs is a transmission error, and the count gives a bit error rate.
The device below sends 2,044 PN9 bits, four full periods. You can inject random bit errors, or slip the receiver by one bit, as a badly aligned deserializer would. The receiver regenerates the sequence and marks every difference in red.
Each small square is one bit: light for 1, dark for 0. Red squares are bits the receiver got wrong. Drag the error rate, or switch on the one-bit slip.
A model of the check. The 9-bit register uses taps 9 and 5 and starts at all ones, and the model sensor is assumed to use the same rule. A real sensor’s start value and bit order come from its datasheet. The error pattern is a seeded random draw, the same on every visit. The rate is a percentage of bits, up to 5%.
Set the error rate to 1.0 percent. Of 2,044 bits you expect about 20 to be wrong, and the read-out shows the exact count for this draw. Now return to 0 and switch on the slip. Every bit is compared against its neighbour. Because the sequence looks random, about half of the bits differ: roughly 1,020 of 2,044. A slip makes a 50 percent error rate. A noisy wire makes a small one. Two very different faults, told apart by one number.
The column counter modes work the same way for position faults. Each pixel’s value is its column number, so a jump, a repeat or a swap in the received row shows exactly where a line was cut or lanes were crossed.
Symptom: the real image is black or odd. Measurement: write 0x0600 to select colour bars, stream, and look at the frame. Then write PN9 and have the receiver (or a script that reads the captured frame) count the errors. Fix: a correct pattern means look at light, exposure and gain. A wrong pattern means look at lanes, link speed and format, which is Chapter 7. And always check a pattern first on a new board, before you spend an hour on 3A or the ISP.
Chapter 7
Match the receiver to the sensor on lanes, speed and clock mode, and learn how a burst of pixels starts
The chip ID reads 0x0219. Every write in the table gets an ACK. Streaming is on. And the receiver driver prints a timeout: no start of frame, no data. The control road is perfect. The pixel road is dead. This is the famous no link problem, and it is the last gate.
The camera link, MIPI CSI-2, usually rides on a physical layer called D-PHY. It sends each signal on a differential pair: two wires carrying opposite voltages, so that noise which hits both wires equally cancels out when the receiver subtracts them. One pair is a lane. There is one clock lane and one, two or four data lanes. Our model sensor uses two data lanes, as in the IMX219 device tree example (data-lanes = <1 2>).
A lane has two ways of talking. In low-power mode (LP) the two wires are driven as ordinary slow signals. In high-speed mode (HS) they carry small, fast, differential swings. Between bursts a lane rests in the stop state, called LP-11, with both wires high. To send a line of pixels, the transmitter steps out of LP-11 through a short hand-over sequence, then sends the burst in HS, then returns to LP-11. The Linux kernel notes say a CSI-2 transmitter “typically briefly sets the bus to LP-11 or LP-111 state” when it switches to high speed, and that some devices power up in LP-00 instead. The receiver uses these low-power states to know a burst is coming, and tunes its input for it.
The receiver does not guess the sensor’s settings. Someone has to tell it three things, and that someone is your firmware or your device tree. First, the lane count. Second, the link frequency, because the receiver sets its timing for catching the start of a burst from it. The kernel notes say the link-frequency control exists “to tell the receiver the frequency of the bus”. The IMX219 example uses 456 MHz. Third, the clock mode: in continuous mode the clock lane keeps running, and in non-continuous mode it drops back to LP between bursts. The IMX219 example sets clock-noncontinuous.
If the sensor and the receiver disagree on any of these, the symptom is not a bad picture. It is often no picture at all. Set the receiver in the device below and watch each lane.
The sensor sends 2 data lanes at 456 MHz with a non-continuous clock. Set what the receiver expects. Green lanes carry data; red lanes are mismatched.
A model. The sensor values (2 lanes, 456 MHz, non-continuous clock) are the example in the Linux IMX219 binding. Which mismatches break a real receiver depends on the SoC: the rule that a frequency more than 20% off breaks the start of a burst, and the result of a clock-mode mismatch, are illustrative. Check your SoC reference manual for its CSI-2 receiver.
At 456 MHz a lane sends 2 bits per clock cycle, so 912 Mbit/s per lane. Two lanes give 1,824 Mbit/s. A RAW10 stream of 1920 by 1080 pixels at 30 frames per second needs 62.208 million pixels per second, or 622 Mbit/s of picture. That is 34 percent of the link, and the rest is blanking, packet headers and margin. If you ask for 4K at 60 frames per second on the same two lanes, the arithmetic fails before any wire does.
The pixel rate follows from the same numbers: link frequency 456 MHz times 2 bits times 2 lanes, divided by 10 bits per pixel, equals 182.4 million pixels per second. A driver reports it so the rest of the camera stack can check what a mode needs.
Symptom: I2C is healthy, the table ACKs, and the receiver reports a timeout or frame-start errors. Measurement, in this order. One: compare the sensor’s lane-mode register (0x0114: 1 means 2 lanes, 3 means 4) with the receiver’s lane count. Two: compare the link frequency in the sensor driver with the one the receiver was given. Three: with a high-bandwidth differential probe on lane 0, look for a burst after you write the stream-on register. Four: switch to the PN9 test pattern and count errors, as in Chapter 6. Fix: make both sides agree, and arm the receiver before the sensor starts to stream. The kernel offers a pre-stream callback for a related job: it puts the transmitter into the LP-11 state early, so a receiver that needs software help can see that state and get ready before the stream starts.
One more cause to know. A receiver set up too late misses the first burst and waits for a frame start that already passed. So the safe order is: configure the receiver, arm it, then write 0x0100 = 1 on the sensor.
Chapter 8
Pick the cheapest measurement that splits the problem in half, and find six hidden faults with as few as you can
You now know the four gates and the tools for each. Real boards do not label their faults. You get a black screen and a bench full of instruments. The skill left to learn is choosing the next measurement.
Good debuggers do not try random fixes. They ask a question that rules out half of what could be wrong, and they ask the cheapest one first. A multimeter takes ten seconds. Reading a chip ID takes a minute. A scope on every rail takes ten minutes. Start with the cheap tools, and let the answers lead you.
A multimeter reads a steady voltage. It tells you the end state of a rail, never the timing. A logic analyzer records many digital signals on one time axis, and decodes I2C into bytes with ACK and NACK marked. An oscilloscope shows analog shape: a rail ramp, a clock edge, a reset glitch. On Linux boards, i2cdetect -y 1 (from the i2c-tools package) scans the bus and shows which addresses answer. And v4l2-ctl can list a camera’s controls, set its test pattern and capture a frame, so you can look at what the receiver got.
The job of the first measurement is to place the fault at one of the four gates. Here is the map from silence to the first move:
| What you see | Gate | First measurement |
|---|---|---|
| NACK on every probe | 1 to 3 | Scope on rails, clock and reset together; check the first I2C byte for the address. |
| First read fails, second works | 2 | Scope: gap from reset release to first START against the datasheet wait. |
| Chip ID right, no frame | 4 | Read back 0x0100. Is it 1? Did the stream-on write come last? |
| Chip ID right, receiver says no link | 4 | Compare lane count and link frequency on both sides; arm the receiver first. |
| Test pattern good, scene black | after 4 | Exposure, gain, lens cap, analog rail. |
Below are six boards. Each has a black screen and a single hidden fault. Choose a board, run measurements, and then name the fault. Each measurement you use is counted, because a good bench engineer wastes few. Work out a plan before you press: which tool would cut the six possible faults in half?
Pick a board. Press measurements to fill the log. Then name the fault in the last row. The tools are cheap at the top and dear at the bottom.
Illustrative faults on a model sensor. The numbers shown (6.2 ms wait, address 0x10, chip ID 0x0219, 2 lanes) are the IMX219-class values used through this lesson. The measurement outputs are written to match each fault.
Try this plan on the boards. The chip ID read splits faults into two groups: those where it fails (rails, early probe, clock, address) and those where it works (stream and lane faults). The receiver status splits the second group. And only the scope tells the first group apart, because rails, clock and reset need a shared time axis. After one good split, each remaining measurement removes about half of what is left. Six faults need about three well-chosen measurements.
Three habits make this work on every board. First, change one thing at a time, and write down what you changed. Second, power-cycle before every test, so a chip upset by an earlier try does not fool you. Third, keep a known-good board or a known-good table. When a new board fails and the old one passes, the difference is the answer.
There is a number behind this advice. Six equally likely faults hold log2(6), about 2.6 bits of doubt. A yes-or-no measurement that splits the faults into two equal groups removes one bit. So you need at least three such measurements, and no clever tool can do better than that. In practice, one scope capture of the rails, clock and reset can remove several bits at once, because it answers three questions on one time axis. That is why the scope sits at the dear end of the list but is often worth its cost.
Write each step as one line: what you changed, what you measured, what you saw. For example: “11:02, moved VDIG enable before VANA, scope on all rails, order correct, chip ID read still NACK.” Two weeks later, when a second board fails, the log tells you what you already ruled out. It also tells a colleague where you stopped. A log turns a day of poking into a path you can retrace.
On a Linux board, the same faults show up in different words. A failed chip ID read appears in the kernel log as a probe error from the sensor driver. A mismatch in lanes appears as a timeout from the capture driver when you run v4l2-ctl --stream-mmap. Reading both the kernel log and the bus capture tells you which gate is closed without opening a debugger.
Chapter 9
Keep the numbers, the checklist and the core code, and see where this lesson sits in the camera track
A camera sensor is a separate computer that obeys a strict start-up ritual. Power rails rise in the order its datasheet asks for. The clock runs before reset lets go. A wait follows. Then I2C talks, first to read the chip ID, then to write the init table in standby with streaming last. The receiver must agree on lanes, speed and clock mode. The test pattern proves the digital path before you trust any real image.
| Item | Value | Source |
|---|---|---|
| IMX219 I2C address | 7-bit 0x10 (bytes 0x20 write, 0x21 read) | Linux IMX219 binding |
| IMX219 chip ID | register 0x0000 (16-bit) reads 0x0219 | Linux imx219.c |
| IMX219 rails | VANA 2.8 V, VDIG 1.8 V, VDDL 1.2 V | Linux IMX219 binding |
| IMX219 clock | XCLK 24 MHz, driver refuses other rates | Linux imx219.c |
| IMX219 wait after XCLR high | 6200 to 7200 microseconds | Linux imx219.c |
| IMX219 mode select | 0x0100: 0 standby, 1 streaming | Linux imx219.c |
| IMX219 lane mode | 0x0114: 1 is 2 lanes, 3 is 4 lanes | Linux imx219.c |
| IMX219 data format | 0x018C holds (bits << 8) | bits, so RAW10 is 0x0A0A | Linux imx219.c |
| IMX219 test pattern | 0x0600: 0 off, 1 solid, 2 colour bars, 4 PN9, 9 PN31 (ten values, 0 to 9) | Linux imx219.c |
| IMX219 link | 456 MHz, 2 lanes: 912 Mbit/s per lane | Binding example; two bits per cycle |
| OV5640 address, ID | 0x3C; register 0x300A reads 0x5640 | Linux ov5640.c |
| OV5640 wait after reset release | 20 to 25 ms | Linux ov5640.c |
| OV5640 power control | 0x3008: 0x42 standby, 0x02 run, 0x82 software reset | Linux ov5640.c |
| I2C speeds | 100 kbit/s, 400 kbit/s, 1 Mbit/s | NXP UM10204 |
| One chip ID read | 6 bytes, 54 clocks, about 135 microseconds at 400 kHz | Counted in Chapter 2 |
/* STM32 HAL, IMX219-class sensor. The rail order is a teaching rule; use your datasheet's. */ #define SENSOR_ADDR 0x10u /* 7-bit; HAL wants it shifted left by one */ typedef enum { BU_OK, BU_ID, BU_TABLE } bu_err_t; static int rd16(uint16_t reg, uint16_t *v) { uint8_t b[2]; if (HAL_I2C_Mem_Read(&hi2c1, SENSOR_ADDR << 1, reg, I2C_MEMADD_SIZE_16BIT, b, 2, 100) != HAL_OK) return -1; *v = (uint16_t)(b[0] << 8 | b[1]); /* high byte first */ return 0; } bu_err_t sensor_bringup(void) { gpio_write(XCLR, 0); /* hold in reset */ rail_on(VDIG); HAL_Delay(2); /* I/O first, wait for PG */ rail_on(VANA); HAL_Delay(2); rail_on(VDDL); HAL_Delay(2); xclk_start_24mhz(); /* clock before reset ends */ gpio_write(XCLR, 1); /* release reset */ HAL_Delay(7); /* 6.2 ms minimum + margin */ uint16_t id; if (rd16(0x0000, &id) || id != 0x0219) return BU_ID; if (sensor_write_table(init_tbl, N_INIT) < 0) return BU_TABLE; return BU_OK; /* table ends with 0x0100 = 1 */ }
On Linux, the same facts live in the device tree. This is the example from the IMX219 binding, trimmed:
imx219: sensor@10 { compatible = "sony,imx219"; reg = <0x10>; clocks = <&imx219_clk>; VANA-supply = <&imx219_vana>; /* 2.8v */ VDIG-supply = <&imx219_vdig>; /* 1.8v */ VDDL-supply = <&imx219_vddl>; /* 1.2v */ port { imx219_0: endpoint { data-lanes = <1 2>; clock-noncontinuous; link-frequencies = /bits/ 64 <456000000>; }; }; };
This is the first lesson of the camera track in Firmware from Zero to Expertise. Before it, read how to read a datasheet, the clock and reset basics in first light, and the buses in buses on the wire. The sensor-to-CSI path and the ISP are the topic of the Physical AI camera lessons, which come next. After it, cameras on a microcontroller shows the parallel port. UVC webcams and the Linux sensor driver follow in the same track. For the board-level view see hardware interfacing and sensor and actuator integration, and for the camera maths see camera models and calibration.
Now press Teach and explain, from memory, why a probe 1 ms after reset gets a NACK, what an init table must end with, and how PN9 turns a bad link into a number. Then go back to the bring-up board at the top and run all four boards again. This lesson is a build from public drivers and standards, not from a course; the credit belongs to the authors of the Linux media drivers and bindings, to NXP for the I2C specification.