misaz / Nuvoton8051ProgrammingLib

Nuvoton 8051 Serie MCU Programming library
MIT License
14 stars 3 forks source link

Erase Config Register Bits to Remove Chip Lock #2

Open Javy-He opened 9 months ago

Javy-He commented 9 months ago

Thank you for your extensive documentation! May I clarify if the Nuvoton8051_EraseCFG() function you included in Nuvoton8051.c would be able to clear a chip lock bit set in the config register? I'm looking to recycle a bunch of W79E2051 units with flash that is completely read/write protected by the config register.

I tried including the function in your AVR WriteFlash example program like so:

int main(void) {
    Nuvoton8051_Init();
    Nuvoton8051_EntryMode();

        Nuvoton8051_EraseCFG();
    Nuvoton8051_MassErase();

    Nuvoton8051_ExitMode();

    DDRB |= 1;
    PORTB &= ~(1);

    while (1) {
    }
}

but I'm still getting FF values for DeviceInfo and ReadFlash. Apologies if I'm using it incorrectly

I checked the N76E003 datasheet which details when the chip lock is enabled:

Whole Flash Memory is locked. Their contents read through a ICP programmer will be all blank (FFH). Programming to Flash Memory is invalid ... once the chip is locked, the CONFIG bytes cannot be erased or programmed individually. The only way to disable chip lock is execute “whole chip erase”.

The W79E2051 works the same way, but relies on just ICP programming without an In-Application Programmer.

I'd greatly appreciate any advice, thanks!

misaz commented 9 months ago

I checked documentation of W79E2051 and I am not sure if it has the same interface as N76E003. Nuvoton refers different programming tools used, nomenclature is different and registers like CONFIG bytes has totaly different layout.

But maybe even better, for W79E2051, Nuvoton provide package named Nuvoton_8051_ISP-ICP_Programmer_v7.15 (download from https://www.nuvoton.com/products/microcontrollers/8bit-8051-mcus/low-pin-count-8051-series/w79e2051/?group=Software&tab=2) which contains GUI for software which can control W79E2051 just using USB-to-UART which you most probably already have. N76E003 interface is more complicated and can't be implemented using simple USB-to-UART converter, so for N76E003, project like this Nuvoton8051ProgrammingLib is needed.

I testest with N76E003, that Nuvoton8051_EraseCFG do not work when chip is locked, but Nuvoton8051_MassErase work and read-only commands like Nuvoton8051_ReadCFG, Nuvoton8051_ReadCID, Nuvoton8051_ReadDID and Nuvoton8051_ReadPID also work when MCU is locked.

Nuvoton8051_MassErase unlocks the MCU (but erases all it's memories as well). If read commands like Nuvoton8051_ReadCID and Nuvoton8051_ReadDID returns 0x00 or 0xFF, them most probably chip use different programming interface, or chip is unpowered, or connection is wrong, or chip is dead, or ....

Javy-He commented 8 months ago

@misaz Thanks for taking the time to explain, I wasn't sure if the hardware ID read commands are supposed to work while the MCU is locked.

Unfortunately it appears the W79E2051 doesn't have the ISP feature that allows for programming over UART, so I'm stuck with only ICP.

I think you're right about the programming interface being different, hopefully I can try porting your library to read the hardware IDs on the W79E2051.

Cheers!

misaz commented 8 months ago

I did not speak about ISP. I spoke only about ICP. ICP controller in W79E2051 is most probably different than ICP controller in N76E003. ICP and UART are usualy incompatible, but some vendors design their ICP controllers that packet format is compatible with UART packet format (ie. fixed start bit + fixed length data + fixed stop bit). In case of N76E003 ICP bus packet format is different and thus can't be interfaced over serial, but in case of W79E2051 it seems that ICP bus packet format is compatible.

This pattern is also visible in non-Nuvoton world. For example, newer AVR MCU has UPDI interface for programming MCUs and packet format of this protocol is compatible with UART (has fixed start bit + fixed length data + fixed stop bit), but for example, ARM SWD is not (start bit has inverse polarity, data are dynamic length and much longer than UART controller supports and have no stop bit). For this reason, AVR MCUs can be programmed over UART (see serialupdi project), but ARM MCUs can't and require more complex adapters.

I recommend downloading linked package Nuvoton_8051_ISP-ICP_Programmer_v7.15 and reading bundled documentation. It can save you lot of time.

Javy-He commented 8 months ago

@misaz Thank you for clarifying, I was not aware that the W79E2051 ICP packet format was compatible with UART.

I do often use the SerialUPDI tool, since it's convenient for use with my CH340 USB-Serial adapter that I have readily available.

But if I understand correctly, the Nuvoton_8051_ISP-ICP_Programmer_v7.15 tool requires their proprietary PL-2303 based device to use the ICP function, otherwise the GUI raises an error that the "Programmer is not found".

I combed through the MCU datasheet and all bundled documentation in the Nuvoton_8051_ISP-ICP_Programmer_v7.15 package but am struggling to find ICP-related details to find a solution to use with a generic USB-Serial adapter.

May I ask how you identified the UART compatibility of the ICP bus packet format? I apologize if I'm overlooking anything in the MCU datasheet or package documentation.

Really appreciate your time!

misaz commented 8 months ago

PL-2303 is similar chip to CH340, but it is manufactured by different vendor, requires different drivers and has different VID:PID.

Compatibility with UART I deduced just because Nuvoton say, that their ICP tools is based on USB-to-UART converter (PL-2303). They say it in section 3.1 Installing the Driver in 8051 ICP Programmer, v7.15pdf which is in the refered package.

This ICP Programmer has the USB-to-Serial bridge chip (PL-2303) built inside. When connected to host, it will appear as a USB-to-Serial COM port in the System\Hardware\Device Manager. Before starting to use this programmer, the user has to install the driver if the PL-2303 driver has never been installed in this host. The user can also find this driver in the folder [(1) Driver].

I do not think it it locked to PL-2303. I tested it witn both PL-2303 and CP2102 which I have. With CP2102 it works exactly the same as PL-2303. But I do not have W79E2051, so I can't test it with chip connected. Without chip connected it still prints "Programmer is not found!" but it take longer and transmit two bytes over serial. Both bytes are 0xC1 transmitted at baudrate 115200.

Javy-He commented 8 months ago

Interesting, I thought that unlike ISP and UART, the ICP interface is a synchronous protocol and requires a dedicated clock signal pin like on Nuvoton's proprietary programmer?

I tried each of the programmer tool functions in ICP mode with my CH340, but did not observe any transmissions on the TXD line.

In any case, I would like to ask if you might recall how you determined the necessary 24 bit values sent to the RST and DAT pins during Nuvoton8051_EntryMode(), and why they are 24-bit in size?

misaz commented 8 months ago

Thank you for reply. I do not have CH340, so I can't test it, but with both PL-2303 and CP2102 it transmits byte on UART.

Details about development were discussed here: #1