Electronic – PIC programming through FPGA

bootloaderfpgapicpickitprogrammer

I would like to know if there is a way to program a PIC for the first time (write in Flash) through an FPGA card.

The PIC is already soldered to the FPGA and I can't remove it. No bootloader exists on the PIC. Thus I need to program it in USART / SPI / I2C mode with a bootloader so that it can receive data from the FPGA.

I know that normally I have to unsolder it, connect it to a hardware programmer like PICKit and program it with a software on the computer like MPLAB, but I cannot afford that.

I can generate the bootloader with MPLAB, which is a .hex file, but how can I transfer it to the FPGA and then to the PIC through the FPGA pins?

PIC used is Microchip PIC12F1822.

Best Answer

No, you don't need to, in fact you can't, use SPI, UART, or I2C to program the PIC. The only way to get a new program into a PIC that doesn't have special code loaded for that purpose (a bootloader) is to use the external hardware programming interface. Electrically, this means connecting to Vss, MCLR, PGC, and PGD. It can be useful to have the programmer also connected to Vdd, but not necessary for this chip as long as the Vdd level is known and the programmer adjusted accordingly.

The low level hardware interface is quite simple. PGD is the data line, which is sampled by the PIC on the falling edge of PGC (the clock line). To get the PIC into programming mode in the first place, a special 32 bit key is clocked in relative to specific edges on MCLR (although see note below regarding high voltage programming).

The higher level protocol gets more complicated. Most things are done with 6 bit commands, some of which are followed by 14 bit data words. You have to read the programming spec carefully. Note that the programming spec is a separate document from the datasheet. Go to the product page for your particular PIC on the Microchip web site, and you will find a link to the programming spec in the documents section.

Added about high voltage programming

These kinds of PICs have two ways to enter programming mode, high voltage (HVP) and low voltage (LVP). The high voltage method requires raising MCLR to between 8 and 9 volts and to keep it there during programming. This method always works, regardless of any possible data programmed into the PIC.

The low voltage method of entering programming mode starts by driving MCLR high, then low, then clocking in a special 32 bit key sequence using PGC and PGD normally. The part will enter programming mode upon the correct key sequence, and will stay in programming mode as long as MCLR is held low.

The low voltage method can be disabled by one of the config bits. However, the erased state of the config bit allows low voltage programming, it is shipped from the factory that way, and this config bit can only be set to disallow LVP if programming was entered with the high voltage method. Therefore, for LVP to not be enabled, all the following have to be true:

  1. The PIC was last programmed with a HVP-capable programmer, and the HVP program entry mode was used.

  2. The HEX file programmed into the PIC deliberately sets the LVP bit in config word 2 to the non-erased state.

Since disabling LVP pretty much requires a deliberate action and the right programmer, it is likely still enable. If it was deliberately disabled for some strange reason, then you have to supply 8-9 V on MCLR to get the PIC into programming mode at least long enough to perform a bulk erase (which re-enables LVP).