Getting to the final result took me days of work, many hours in the hardware manual, and plenty of failed attempts. It was initially triggered by a Reddit post, where someone mentioned being able to dump flash by uploading a UART dumper routine and new reset vector - as long as flash was only read (and not write) protected.
Initially, before I even started on this project, I had to remove a lot of components from the PCB and provide my own 5v power supply to the MCU. The original voltage regulator was integrated with an external watchdog which kept resetting the MCU. In the end I lifted off every IC and transistor around the chip and hooked up my own power supply. The module I'm working on (FG Falcon BEM) is cheap and I bought a few of them assuming I would have to sacrifice a couple.
You'll also need to find the hardware manual for your specific version of the V850, find the pinout, and identify the FLMD0 / Reset / 5v / GND and UART connections.
The overall idea is we compile some code to loop over and read bytes from flash, then spit them out a UART. We then need to write that to an unused area of flash memory, and then overwrite with reset vector with NOP instructions until we find space to write a new vector pointing to our code. Luckily in V850 assembly, NOP is a 2 byte all zero instruction "0x00 0x00", and since flash memory can be programmed by flipping bits from 1 to 0 (but not the other way around), its possible to overwrite any existing data with NOPs.
My UART dumper for μPD70F3375 is attached: It was compiled with V850 GCC (https://www.gnutoolchains.com/v850-elf/). I originally tried to use the Renesas CS+ IDE to generate me some setup code, but it didn't seem to support code generation for this older v850. It didn't even seem to have libraries that defined register addresses. So in the end I had to find all the registers in the hardware manual and configure them manually. It should be fairly trivial to port to other flavors of V850 chip - read up in the appropriate hardware manual for your chip. The functionality is very simple:
Code: Select all
void main(void) {
volatile unsigned long i; // C89 requires all variables declared at start of function
volatile unsigned char *flash_ptr;
unsigned char sync_byte;
/* HARDWARE SETUP */
WDTM2 = 0x1F; // Disable watchdog
PRCMD = 0x00; // Scratch the write protection register (to enable clock write)
MCM = 0x00; // Use 8mhz internal oscillator (should default to this anyway) - much simpler to config
PFCE9L |= 0x03; // Select alternate function 3 for pins P90/P91 (TXDD1/RXDD1)
PFC9L &= ~0x03;
PMC9L |= 0x03; // Enable alternate mode
/* UART SETUP */
UD1CTL1 = 0x03; // UART Base clock: fXP1/8 = 1MHz (assuming 8MHz system clock and cleared PRSI)
UD1CTL2 = 52; // Baud rate divisor for 9600 baud (assuming PRSI in option byte 0x7B cleared, otherwise 4800 baud)
UD1CTL0 = 0xC2; // 11000010 - Power ON, TX enabled, 8-bit, 1 stop, no parity
for(i=0; i<1000; i++) { }; // Small delay for UART to stabilize
/* TRANSMIT 32 SYNC BYTES */
sync_byte = 0xAA; // Just incase I need to account for baud rate error
for(i=0; i<32; i++) {
while(UD1STR & 0x80);
UD1TX = sync_byte;
}
/* DUMP FLASH */
flash_ptr = (volatile unsigned char*)0x00000;
for(i=0; i<=0x3FFFF; i++) {
while(UD1STR & 0x80); // Wait while transmitting
UD1TX = *flash_ptr; // Write byte to transmit
flash_ptr++;
}
/* FOREVER LOOP */
while(1);
};
I spent quite some time sniffing the serial communication between the Renesas debugger and MCU. This is only UART and you can actually use any USB-UART adaptor with the flash programmer (you don't need the renesas debugger).
Packets of data seem to be transmitted in this format:
byte 0: 01 from debugger, 02 from MCU
byte 1: byte length of payload
byte 2 to n-3: <payload bytes>
byte n-1: 2's complement checksum
byte n: "03" (ascii end of text)
Here's the exchange from a "get flash options" using the Renesas flash programmer software: To enter programming / boot mode, the debugger holds FLMD0 high and resets the MCU - this puts it in programing mode. It then send 2x "0x00" bytes to allow the MCU to sync baud rate with the debugger. And then sends commands as required.
The MCU generally responds to every command with an "06" (ascii acknowledge), and then responds again with data if required.
When attempting to write flash, we have this sequence of commands to start with: With trial and error, I have confirmed the final "0x40" command is setting up the memory address range we are going to write (in this case 0x000000 to 0x03FFFF). I don't know what any of the other commands do.
The Renesas software sends 256 byte chunks of data to write after this, with 2 header bytes "02 00", and 2 footer bytes (checksum and 0x17 - ascii "end of transmission block"). The MCU responds "06 06" on success, or "06 xx" where xx is an error code (eg. 1C when writing data to flash that's already written and didn't verify correctly afterwards).
In the end I used an Arduino Mega to act as the programmer. My source is attached here, and rather than try and explain it its probably easier to look yourself: It acts as a utility and does a few things automatically:
- Blank scans 256 byte blocks by writing all FF to them. If the MCU returns 06 06, the block must have already been blank. This is how I found blank areas of flash to write my dumper code.
- Writes the UART dumper code to the address you define (in 2 chunks, since its slightly larger than 256 bytes).
- Writes a "all zero" block to the address you define and returns result code (06 or 1C). I used this to confirm the reset vector writer was working.
- The big one: Writes the reset vector, starting at address 0x000000 and incrementing 1 NOP instruction at a time. Resetting the MCU and checking for UART output from the dumper after each write. Keeps going up to your maximum defined address, until it detects the reset vector worked and your code is running, at which point it stops.