Arduino cannot set lock_bits and fuses from IDE

I want to set the lock bit and fuses from inside the Arduino IDE, using an external programmer (Sparkfun's Pocket AVR Programmer).

For this example, let's focus only on the lockbit. I am using Arduino Pro Mini.
Inside the boards.txt file, I have located the Pro Mini section and more specifically this line:

pro.bootloader.lock_bits=0x0F

I have changed this to:

pro.bootloader.lock_bits=0x00

What I did, is I changed the lock bit, so that the flash in unreadable.
However, when I read the flash without the lock bit set and when I read it with the lock bit set, it is exactly the same. This means that the setting did not work.

Is there a way to make this work? I cannot understand why this does not work for me. Obviously, I select the Upload Using External Programmer option.

These are all the settings for this Arduino model:

pro.name=Arduino Pro or Pro Mini

pro.upload.tool=avrdude
pro.upload.protocol=arduino

pro.bootloader.tool=avrdude
pro.bootloader.unlock_bits=0x3F
pro.bootloader.lock_bits=0x00


pro.build.board=AVR_PRO
pro.build.core=arduino
pro.build.variant=eightanaloginputs

and you changed tools.avrdude.program.pattern in platform.txt

Hello! Dejavu.
These are the contents of the given parameter in platform.txt:

tools.avrdude.program.pattern="{cmd.path}" "-C{config.path}" {program.verbose} {program.verify} -p{build.mcu} -c{protocol} {program.extra_params} "-Uflash:w:{build.path}/{build.project_name}.hex:i" -e -Ulock:w:{bootloader.unlock_bits}:m -Uefuse:w:{bootloader.extended_fuses}:m -Uhfuse:w:{bootloader.high_fuses}:m -Ulfuse:w:{bootloader.low_fuses}:m

Then this:

tools.avrdude.program.pattern="{cmd.path}" "-C{config.path}" {program.verbose} {program.verify} -p{build.mcu} -c{protocol} {program.extra_params} "-Uflash:w:{build.path}/{build.project_name}.hex:i" -e -Ulock:w:{bootloader.lock_bits}:m

And this...

tools.avrdude.program.pattern="{cmd.path}" "-C{config.path}" {program.verbose} {program.verify} -p{build.mcu} -c{protocol} {program.extra_params} "-Uflash:w:{build.path}/{build.project_name}.hex:i" -e -Ulock:w:{pro.bootloader.lock_bits}:m

what are the 3 versions?

Different things that i tried. Nothing worked. Tried every combination basically.

I don't think the IDE will try to change the lock fuses when you upload. It sets the fuses when you burn a fresh bootloader.

I think that to lock the FLASH with your sketch installed you will need to learn how to use 'avrdude' outside the IDE.

I would start by turning on "verbose messages during upload" and burning a bootloader. That will show you the 'avrdude' commands used to upload the bootloader and set fuses. Start with the commands to set fuses and get them to work outside the IDE.

@johnwasser it is upload with programmer and the avrdude command executed by the IDE is modified to have the options for fuses.

the command is
avrdude -avrdude.conf -v -patmega328p -cusbtiny -Uflash:w:Blink.ino.hex:i -Ulock:w:0x3F:m -Uefuse:w:0xFD:m -Uhfuse:w:0xDA:m -Ulfuse:w:0xFF:m

@nikolakos what prints avrdude when executing the command

What exactly does this command do?
You want me to execute this and report the results?
I am asking because the only thing i want to do, is modify the lock bit to 0x00.

Using this command for example:
avrdude -c usbtiny -p m328p -U lock:w:0x00:m

in order to write the lock_bit, I get:


avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: reading input file "0x00"
avrdude: writing lock (1 bytes):

Writing |                                                    | 0% 0.00s ***failed;
Writing | ################################################## | 100% 0.04s

avrdude: 1 bytes of lock written
avrdude: verifying lock memory against 0x00:

Reading | ################################################## | 100% 0.00s

avrdude: WARNING: ignoring mismatch in unused bits of "lock"
         (0xc0 != 0x00). To prevent this warning set unused bits
         to 1 when writing (double check with your datasheet first).
avrdude: 1 bytes of lock verified

avrdude done.  Thank you.

I have tried every different combination in the platforms.txt file. What frustrates me the most is that it does not work, although it should be working!
I don't know why nothing works. In any combination, I am still able to read the flash, which should not be possible, which means setting the lock bit failed.

the command in my comment is one of the commands in your comment

I didn't try these values specifically.
If you mean the first command, it is the first one i tried. Later, i removed all the fuses parts and kept the lock bits only.

Let's look at the Arduino IDE output.
When I use tools.avrdude.program.pattern="{cmd.path}" "-C{config.path}" {program.verbose} {program.verify} -p{build.mcu} -c{protocol} {program.extra_params} "-Uflash:w:{build.path}/{build.project_name}.hex:i" -Ulock:w:{pro.bootloader.unlock_bits}:m

Then I get:

avrdude: Version 6.3-20190619
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "C:\Users\Nikos Stolis\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf"

         Using Port                    : usb
         Using Programmer              : usbtiny
avrdude: usbdev_open(): Found USBtinyISP, bus:device: bus-0:\\.\libusb0-0001--0x1781-0x0c9f
         AVR Part                      : ATmega328P
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PC2
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    20     4    0 no       1024    4      0  3600  3600 0xff 0xff
           flash         65     6   128    0 yes     32768  128    256  4500  4500 0xff 0xff
           lfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           lock           0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : USBtiny
         Description     : USBtiny simple USB programmer, https://learn.adafruit.com/usbtinyisp
avrdude: programmer operation not supported

avrdude: Using SCK period of 10 usec
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: Using SCK period of 10 usec
avrdude: reading input file "C:\Users\NIKOSS~1\AppData\Local\Temp\arduino_build_984948/Blink.ino.hex"
avrdude: writing flash (924 bytes):

Writing | ################################################## | 100% 1.05s

avrdude: 924 bytes of flash written
avrdude: verifying flash memory against C:\Users\NIKOSS~1\AppData\Local\Temp\arduino_build_984948/Blink.ino.hex:
avrdude: load data flash data from input file C:\Users\NIKOSS~1\AppData\Local\Temp\arduino_build_984948/Blink.ino.hex:
avrdude: input file C:\Users\NIKOSS~1\AppData\Local\Temp\arduino_build_984948/Blink.ino.hex contains 924 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.84s

avrdude: verifying ...
avrdude: 924 bytes of flash verified

avrdude done.  Thank you.


One thing that sticks out is this:

avrdude: programmer operation not supported

Maybe some option in the System wide configuration file?

you have to restart the IDE to apply changes in platform.txt

copy here the executed command too

I figured that out... I was restarting the IDE the whole time.
I just realized that before the output, you get the command that is being used. I will try a new set of experiments and will post here.

Ok. I reached a point - thanks to your help - where I realize that the generated command is not the correct.
This is the command it generates:

C:\Users\RandomUzer\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/bin/avrdude -CC:\Users\RandomUzer\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf -v -patmega328p -cusbtiny -Uflash:w:C:\Users\RAND~1\AppData\Local\Temp\arduino_build_481636/Blink.ino.hex:i 

These are four different permutations of parameters that i used:

tools.avrdude.program.pattern="{cmd.path}" "-C{config.path}" {program.verbose} {program.verify} -p{build.mcu} -c{protocol} {program.extra_params} "-Uflash:w:{build.path}/{build.project_name}.hex:i" -Ulock:w:{bootloader.lock_bits}:m

tools.avrdude.program.pattern="{cmd.path}" "-C{config.path}" {program.verbose} {program.verify} -p{build.mcu} -c{protocol} {program.extra_params} "-Uflash:w:{build.path}/{build.project_name}.hex:i" -Ulock:w:{pro.bootloader.lock_bits}:m


tools.avrdude.program.pattern="{cmd.path}" "-C{config.path}" {program.verbose} {program.verify} -p{build.mcu} -c{protocol} {program.extra_params} "-Uflash:w:{build.path}/{build.project_name}.hex:i" "-Ulock:w:{bootloader.lock_bits}:m"

tools.avrdude.program.pattern="{cmd.path}" "-C{config.path}" {program.verbose} {program.verify} -p{build.mcu} -c{protocol} {program.extra_params} "-Uflash:w:{build.path}/{build.project_name}.hex:i" "-Ulock:w:{pro.bootloader.lock_bits}:m"

Their differences are that I used both bootloader.lock_bits and pro.bootloader.lock_bits, as well as trying these with and without " " in the second parameter.
Somehow, the last parameter is never injected in the actual command.

What is really frustrating you is you do not know what you are doing, so you are throwing garbage into the process trying to make sense out of nonsense.

Fuses can be set from the IDE, but editing any of the .txt control files is not smart. With just a little research and use of avrdude, you can do things correctly. Use an UNO configured as a programmer, or buy one, search for Arduino ICSP programmer.
The UNO sketch is in the examples for the IDE, or use Nick Gammon's:
arduino_sketches/Atmega_Board_Programmer.ino at master · nickgammon/arduino_sketches · GitHub

Command line script for Windows:
Copy 'n Paste Arduino Firmware - Community / Exhibition / Gallery - Arduino Forum

More:
Gammon Forum : Electronics : Microprocessors : Atmega bootloader programmer

I know that I do not know what I am doing, this is why I am in a forum asking for help.
I also don't know why you are being so aggressive and rude for asking a question... This is your issue.

I already know how to do this process with AVRDude. But thank you anyway.

@Juraj when I tried chaining commands together, adding more parameters to the AVRDude command, it worked.

avrdude -c usbtiny -p m328p -U flash:w:MyProg.ino.hex -U lock:w:0x00:m

This works. It both uploads the code to the arduino and sets up the lock bit.
I am not sure why the IDE fails to inject the second parameter to the actual command.

So still, no progress from the Arduino IDE...