atmega328p Lock bits not protecting my aplication

Hello my friends; Since yesterday I'm trying to avoid copying the contents of my ATmega328p-pu, I've spent long hours reading and researching on google. And trying to understand the Lock Bits.

I have a lot of ATmega328p, I'm using a ArduinoUNO as ISP and also I've a HIGH voltage programmer to reset any brick chip.

I'm using optiboot_atmega328.hex bootloader.

I'm using this calculator

I've set every protection mode level.

SPM = Store Program Memory (write flash)
LPM = Load Program Memory (read flash)

-U lock:w:0x03:m
-U lock:w:0x00:m
-U lock:w:0xFF:m

And a lot of configuration but nothing happens.. I always can read my sketch.hex

avrdude -c arduino -p m328p -P COM41 -b 19200 -U flash:r:sketch.hex:i

and I always check the value with

avrdude -c arduino -p m328p -P COM41 -b 19200 -U lock:r:Lockbyte:h

I'm starting to think my lock bits do not work on this chip, I've tried four chip and the same problem any protection is activated.

Really I've research each datasheet and each google link today, I feel disappointed right now. Any help Thanks a lot..

I suspect no-read lock-bit and bootloader are mutually exclusive.

Thanks for your answer, I also tried with the stock bootloader ATmegaBOOT_168_atmega328.hex and the same problem. :frowning:

Lock bits can be set to prevent an external programmer such as Arduino as ISP from reading flash memory. But if you have a program installed on the chip that is designed to read flash contents, such as a bootloader, it will be able to read flash memory. So, you need to remove the bootloader by uploading the sketch you want to protect using the programmer (do not use the bootloader and serial to upload the sketch). Then set the lock bits to protect it and see if you can read the chip contents.

Thanks for your reply @dmjlambert I never think that the application can runs without bootloader, the same way I tried to load my sketch.hex with -e command. it should erase the chip ? and I dont used -D command.

avrdude -C "C:\WinAVR-20100110\bin/avrdude.conf" -v -v -v -v -patmega328p -e -cstk500v1 -P\\.\COM41 -b19200 -F -Uflash:w:"C:\Blink.cpp.hex":i -U lock:w:0x00:m

The same result. Any protection is activated. :confused:

I have a lot of 100 atmega328p, atmega8.. I've tried in four or five chips and the same.

Thanks for your help.

willbeck:
Thanks for your reply @dmjlambert I never think that the application can runs without bootloader

I think you may be mistaken about the purpose of a bootloader. A bootloader is used to give you a method of reading and writing applications to Flash memory, as a convenient alternative to ISP programming. The bootloader has nothing to do with an application running, it simply allows you to load the application. Once the application is loaded via this convenient method, the bootloader is not used any more. Unless of course you would like to use or make available the bootloader to circumvent lock bit protection and allow reading of the application, which is what you may be doing.

Lock bits can prevent reading of the application via ISP.

You can upload an application via ISP, in which case you do not need a bootloader. The IDE menu includes an option to Upload Using Programmer.

Second purpose of a bootloader: In Arduino Land, using the Burn Bootloader function of the IDE not only writes the bootloader, but it also sets the fuse values to whatever standard the Arduino board requires for normal operation.

Trivia: there is an exception I can think of, regarding an application requiring a bootloader. Forum community member majek made a special version of Optiboot bootloader that has portions of code an application can use to use blank areas of Flash memory for data storage.

@dmjlambert Thanks my friend very good explanation, I'm now understanding more about the function of the bootloader. I always wrote the bootloader first and then my sketch by ISP and avrdude commands. It has been a long way to learn Now I understand.

Finally I'm thinking in give my product without any protection of my work.

@dmjlambert @Coding Badly

OMG right now I've finally understanding my problem. After hundreds of tests in these three days.

After each upload to the Atmega328p I was using this for read the sketch.

-U flash:r:sketch.hex:i

And It always show me the Reading % process, everything OK.
I was assuming that if the lock bits were programmed it should not allow read or write.

and Only one time I wrote the chip with the sketch that I read but It could be that at that time it was not blocked and it work perfect.

But now I understood that the resulting HEX file it a garbage File when the chip is locked. It's not work when the lock bits are set to 0x00 for example.

I'm really happy now. But really tired after very tired after doing so many tests; I think the read process Should not start when the chip is locked :sweat_smile:

thanks for being so outstanding.

Thank you for the follow-up.

How i lock My ATMEGA328p from reading again ,... please give me video or steps ,..

Thanks for helping