The PIC16F18446 is an 8-bit Microchip microcontroller: 16K words of 14-bit program flash (28 KB of code space, stored as 32 KB in a hex file), 2 KB of RAM and 256 bytes of data EEPROM, in a 20-pin package. It has no SWD port and no bootloader from the factory. The way in is ICSP, In-Circuit Serial Programming: a clock pin, a data pin and the reset pin, MCLR. Normally that means a PICkit, an MPLAB SNAP or the debugger on a Curiosity board.
A BenchPod can do it without any of them. The pod runs the chip's programming protocol itself, on two of its logic analyzer pins and its reset pin, as one of its pin programmers. This article is part of a series, one per chip we have run on real hardware: the overview of how the BenchPod flashes microcontrollers lists them all. It covers how ICSP works on this chip, how the pod implements it, how to wire a PIC16F18446 Curiosity Nano, what we measured, and the chip details worth knowing.
ICSP in one paragraph
ICSP is a synchronous serial protocol. The programmer drives the clock, ICSPCLK, and the data line, ICSPDAT, carries bits in both directions, one at a time. The chip has no clock of its own on this interface and never speaks unasked: the programmer sends a command, and for a read it releases the data line and clocks the answer out of the chip. There is no framing, no checksum and no acknowledge; the programmer knows from the command how many bits follow and who drives them. MCLR, the reset pin, decides whether the chip runs its program or listens on ICSP.
Getting in: low-voltage entry
Older PICs entered programming mode with about 9 V on MCLR. The PIC16F18446 also supports low-voltage programming (LVP), which needs nothing above the supply:
- Hold ICSPCLK and ICSPDAT low, then pull MCLR low. The chip stops and holds its program in reset.
- Clock in the 32-bit key `MCHP`,
0x4D434850, most significant bit first: 32 clocks, one bit each, with the data set up before the falling edge where the chip latches it. - Wait (the pod waits 500 µs) before the first command.
This works only while the chip's LVP configuration bit is set. It is set on a fresh chip and in any image that leaves CONFIG4 at its erased value. An image that clears it turns low-voltage entry off for good; from then on the chip needs a high-voltage programmer.
The older PIC16F1xxx parts use the same key with a 6-bit command set, and there it goes in least significant bit first with a 33rd clock. The pod picks the order from the chip family.
Commands and payloads
The PIC16F18xxx and PIC18 Q families use the 8-bit command set. Every command is 8 bits, most significant bit first. A command that carries data is followed by a 24-bit payload: a 0 start bit, 22 data bits and a 0 stop bit, which amounts to the value shifted left by one. A 14-bit program word sits in the low data bits.
| Command | Code | Payload |
|---|---|---|
| Load PC | 0x80 | the word address |
| Load Data | 0x00 | a word to write at the PC |
| Load Data, increment PC | 0x02 | a word to write, then the PC moves on |
| Read Data, increment PC | 0xFE | the chip sends the word at the PC |
| Increment Address | 0xF8 | none |
| Begin Programming | 0xE0 | none: writes the loaded row |
| Bulk Erase | 0x18 | none: the PC selects what is erased |
| Row Erase | 0xF0 | none: erases the row at the PC |
Who drives the data line. When the programmer sends, it sets each bit while the clock is low and the chip latches it on the falling edge. On a read, the programmer releases ICSPDAT after the command; the chip then drives each bit after the rising edge and holds it until the next rising edge. On our bench the chip's data changed 60 to 100 ns after each rising edge. The pod samples the bit while the clock is high, just before the falling edge.
The memory map
Everything is addressed in 14-bit words. Flash runs from word 0 to 0x3FFF. The other locations sit above it, in configuration space:
| Word address | Hex file byte address | What it is |
|---|---|---|
0x0000 to 0x3FFF | 0x0000 to 0x7FFF | Program flash, 32-word rows |
0x8000 to 0x8003 | 0x10000 to 0x10007 | User ID |
0x8005 | 0x1000A | Revision ID |
0x8006 | 0x1000C | Device ID |
0x8007 to 0x800B | 0x1000E to 0x10017 | CONFIG1 to CONFIG5 |
A hex file from XC8 or MPLAB X stores each word as two bytes, little-endian, so the hex file address is twice the word address. The PIC16F18446 reads device ID `0x30D4`; the revision on our chip was 0x2004.
Programming the chip
- Enter with MCLR low and the key.
- Read the ID. Load PC
0x8005, then Read Data with increment twice: the revision, then the device ID. The pod compares the ID with the chip the host expects before it erases anything. - Bulk erase. Load PC
0x8000, then Bulk Erase. With the PC in configuration space, the erase covers flash, the user ID and the configuration words, and it also clears code protection. - Write rows. For each 32-word row: Load PC to the row, Load Data with increment for the first 31 words, Load Data for the last, then Begin Programming and a wait while the chip programs the row (about 2.8 ms, the pod allows 6 ms).
- Verify. The pod reads every word back with Read Data and compares it with the file.
- Configuration words. CONFIG1 to CONFIG5 are written one word at a time, each with Load PC, Load Data and Begin Programming, and read back.
- Leave. The pod releases MCLR. The chip comes out of reset and runs its program.
How the pod runs it
The work is split three ways:
- The host (the Python SDK or the MCP server) reads the
.hex,.elfor.bin, asks the pod for the chip's ID, looks the ID up in its device table, and cuts the image into the chip's memories: flash, configuration words and user ID, at the hex file addresses above (the data EEPROM is not written). It stages each region in the pod's memory and starts one job per region, then polls its progress. - The pod's MCU runs ICSP as a small state machine: the entry, the ID check, the erase, and the write-then-verify loop for every row. Because the protocol runs next to the pins, its timing does not depend on the network: the same job takes the same time over the LAN, USB or the cloud.
- The FPGA clocks the bits out on two of the 14 LA pins at the target's I/O voltage (1.8 or 3.3 V), switches ICSPDAT between driving and listening, and samples the chip's bits during reads. MCLR is the pod's reset pin, an open-drain output: the pod pulls it low and releases it to the chip's own pull-up.
Wiring a PIC16F18446 Curiosity Nano
The PIC16F18446 Curiosity Nano has an on-board debugger. With its USB cable unplugged that debugger is unpowered, and the target's ICSP lines are on the edge pins labeled DBG0, DBG1 and DBG3:
| Pod (J1) | Curiosity Nano | Signal |
|---|---|---|
| LA3 | DBG0 | ICSPDAT (RA0) |
| LA4 | DBG1 | ICSPCLK (RA1) |
| pin 22, reset | DBG3 | MCLR (RA3) |
| GND | GND | ground |
| pin 17, +3V3 | VTG | target supply |
Leave the Nano's USB unplugged, so the pod is the only thing powering and driving the board. The board's LED0 is on RA2, which makes a blink program an easy check that the chip runs. Name the LA pins ICSPCLK and ICSPDAT in the pod's wiring profile and every tool finds them; otherwise pass them as clk and data.
Bench results
We ran the PIC16F18446 Curiosity Nano at 3.3 V with the wiring above:
- The chip identified itself: device ID `0x30D4`, revision
0x2004, both with and without a device name given. - A blink program, 64 bytes of flash and 10 bytes of configuration words, was written and verified in 0.9 s in total. The chip then ran it and the LED blinked.
- A full 32 KB image of random data was written and verified, every word read back, in 26 s.
Things to know
Erased configuration words do not make a running program. On the PIC16F18446, an erased CONFIG1 (all ones) selects an external clock (FEXTOSC = ECH and RSTOSC = EXT1X), and an erased CONFIG3 turns the watchdog on. A program flashed without configuration words then waits for a clock that is not there, or keeps resetting. Put the configuration words in the image: XC8 does that from the #pragma config lines. Our blink program used CONFIG1 0x3FEC (external oscillator off, HFINTOSC at 1 MHz) and CONFIG3 0x3F9F (watchdog off), and left CONFIG4 (with the LVP bit) and CONFIG5 (code protection) at 0x3FFF.
Keep the LVP bit set. Clearing it in CONFIG4 turns off low-voltage entry, and with it every programmer that has no high voltage for MCLR.
A bulk erase needs enough supply. On the PIC16F18446 the bulk erase needs VDD above about 2.7 V. At 1.8 V the chip can ignore it and keep its old contents and any code protection, so the SDK refuses a chip erase at that voltage. On an erased chip, erase="page" erases only the rows written.
Code protection blocks reads, not erases. A protected chip does not return its flash, so a verify fails. A bulk erase with the PC in configuration space clears the protection along with everything else, which is why it is the default.
The read timing is the chip's, not the programmer's. The chip changes ICSPDAT shortly after each rising clock edge and holds it until the next one. Sampling right after the rising edge reads the previous bit; sample while the clock is high, near the falling edge.
Using it
From Python, with the pytest package:
from embeddedci.benchpod import BenchPod
with BenchPod("192.168.1.50", la_voltage=3.3) as bp:
# Only enter programming mode and read the ID: a wiring check, nothing is written.
print(bp.flash(target="pic", clk=4, data=3, dry_run=True).stdout)
r = bp.flash(target="pic", device="PIC16F18446", clk=4, data=3, file="blink.hex")
assert r.ok, r.stderrdevice is optional: without it the SDK identifies the chip from its ID. With it, a different chip on the pins is an error before anything is erased. MCLR is the pod's reset pin by default; reset_pin puts it on an LA pin instead. erase is "chip" by default, "page" erases only the rows written.
The CLI's benchpod flash --target pic does not handle the PIC16F18xxx and PIC18 Q chips yet; use the SDK or the MCP server for them.
From an AI agent, the MCP server's flash tool takes the same arguments: flash(target="pic", device="PIC16F18446", clk=4, data=3, file="blink.hex"), and dry_run=true to read the ID first.
Underneath both is one JSON command per region. Stage the region with load_bin into the pod's memory, then:
{"cmd":"prog_flash","op":"start","proto":"pic","clk":4,"data":3,"mem":"flash","addr":0,"len":64,
"page":64,"erase":"chip","expect_id":"d430","cmd_set":"8bit","cp_bit":0,"cp_addr":65558,"run":true}
{"cmd":"prog_flash","op":"status"}cmd_set selects the 8-bit command set, cp_addr is the hex file address of the configuration word that holds the code-protect bit and cp_bit the bit; the SDK takes them from its device table. Firmware before 4.0.6 takes the command set as flags 1 (and the bit number in flags too). status reports the phase (enter, id, erase, write, verify, leave, done), rows done out of the total, the chip's ID and, at the end, ok or a reason with what to check. The full reference is in the API docs.
PIC programming works on real chips from firmware 4.0.5 and SDK 4.0.5, with the pin-programmer image installed with benchpod install-blobs. The same pic target covers the PIC16F1xxx, PIC16F18xxx and PIC18 Q families (Programming the PIC18F57Q43 over ICSP covers what PIC18 Q does differently); the pin programmers page lists the other protocols. Every chip and method we have verified on real hardware is in How the BenchPod flashes microcontrollers.