strange fuse/lockbits

hello,
recently I discovered a function that reads and "prints" the fuse/lockbits

#include <avr/boot.h>;

void setup()
{
    Serial.begin(115200);

    cli();
    uint8_t lowBits      = boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS);
    uint8_t highBits     = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS);
    uint8_t extendedBits = boot_lock_fuse_bits_get(GET_EXTENDED_FUSE_BITS);
    uint8_t lockBits     = boot_lock_fuse_bits_get(GET_LOCK_BITS);
    sei();

    Serial.print("Low:  0x");
    Serial.println(lowBits, HEX);
    Serial.print("High: 0x");
    Serial.println(highBits, HEX);
    Serial.print("Ext:  0x");
    Serial.println(extendedBits, HEX);
    Serial.print("Lock: 0x");
    Serial.println(lockBits, HEX);
}

void loop() 
{
}

in the boards.txt the values are

unlock : 0X3F

low:0xFF
lock:0x0F
Hi:0xD8
Ex:0xFD

Now, I uploaded the sketch as it is on a mega2560 and get:

Low: 0xFF
High: 0xD8
Ext: 0xFD
Lock: 0xFF

Any idea why the lock is different?

Have you done a Tools > Burn Bootloader on your Mega 2560 so you're certain the fuses are actually set to the values in boards.txt?

When I run the sketch I get this:
Low: 0xFF
High: 0xD8
Ext: 0xFD
Lock: 0xCF

So you'll note that the lock bits still don't match but if you check what the difference is:
boards.txt:

sketch output:

you can see the only difference is the unused bit 6 and 7.

There is some weirdness in how these unused fuse bits are handled. The AVRDUDE tool used by the Arduino IDE to set the fuses used to handle them one way, then changed in v6.3 to handling them the other way because supposedly that is more correct. This caused a lot of breakage so Arduino modified their version of AVRDUDE to still be compatible with the old fuse values. So I believe this causes AVRDUDE to actually write 0xCF even though the board definition says 0x0F.

More info:

this doesnt happen here.

I canged lockbits to 0x03, the sketch IS locked and still get 0xFF

hmmm it seems there is a problem somewhere

from beginning
I want to lock my application section. For this, I change boards.txt (mega main section) lockbits to 0x0C. Then upload a blink sketch using programmer "arduino as isp" upload ends ok.

Disconnent programmer, connect target mega to usb and try to "simple" upload a sketch. The procedure stops with "connection timeout" (from this i "understood" it is not possible to program, so conclude lockbits ok). (after that I found the function that reads lockbits and tried it and see that there is something wrong)

restore value in boards.txt, Reconnect programmer, burn bootloader go back and verify that now target can get sketch through "upload"

Qs:
is 0x0C correct in order to get "further programming and verification disabled"?
Is procedure correct (change boards.txt, upload using pogrammer (arduino as isp))?

thank you

demkat1:
Is procedure correct (change boards.txt, upload using pogrammer (arduino as isp))?

No, the procedure is not correct. Only Tools > Burn Bootloader sets the fuses. Upload Using Programmer does not set fuses.

You also need to restart the Arduino IDE after changing boards.txt.

You also need to be sure you are editing the right boards.txt. You may have multiple copies on your computer. Turn on File > Preferences > Show verbose output during: > upload and check the output of the Burn Bootloader command to be sure the correct fuse settings were used.

demkat1:
Then upload a blink sketch using programmer "arduino as isp" upload ends ok.

Disconnent programmer, connect target mega to usb and try to "simple" upload a sketch. The procedure stops with "connection timeout" (from this i "understood" it is not possible to program, so conclude lockbits ok)

The conclusion is not correct. "Upload using programmer" deletes the bootloader. So any subsequent attempt with a USB connection will fail, regardless of the fuse settings

Thank you both

it is driving me crazy.

use 1.8.1
Take a mega2560 board. Have an uno "as isp". Burn bootloader. ok, led blinks.
disconnect programmer go to ide,
upload sketch to read fuse and lockbits (see above). ok it works. it gives me E=FD, H=D8, L=FF, lock=CF, as it is on boards.txt (CF=0F unused bits 6,7).

connect "as isp", go to dos window and type

...\avrdude -P COM3 -b 19200 -C ...\avrdude.conf -c arduino -p m2560 -Ulock:w:0x0C:m

ok, writen, verified
disconnect programmer go to ide, sketch ok, it gives me lock=CC.

upload sketch
Sure, why not , "done uploading" and new sketch is "on board"

demkat1:
it is driving me crazy.

use 1.8.1
Take a mega2560 board. Have an uno "as isp". Burn bootloader. ok, led blinks.
disconnect programmer go to ide,
upload sketch to read fuse and lockbits (see above). ok it works. it gives me E=FD, H=D8, L=FF, lock=CF, as it is on boards.txt (CF=0F unused bits 6,7).

connect "as isp", go to dos window and type

...\avrdude -P COM3 -b 19200 -C ...\avrdude.conf -c arduino -p m2560 -Ulock:w:0x0C:m

ok, writen, verified
disconnect programmer go to ide, sketch ok, it gives me lock=CC.

upload sketch
Sure, why not , "done uploading" and new sketch is "on board"

nobody can say why
"further programming and verification disabled" is not working?
why the chip is not "locked"???

0C should mean serial and parallel programming are disabled (but erase will still work, and erases the lock bits as well.) (0bxxxxxx00) and application section access to the bootloader area is disabled (0bxx00xxxx).
But the bootloader can still access the application section (0bxxxx11xx), which is what you’re seeing, right?

The docs are laid out rather horribly!

(Bootloader programingm isn’t either serial programming or parallel programming as they are using the terms. It’s the bootloader deciding to do self programming. It’s actually a pretty useful state to be in, if you bootloader control secure access to some important stuff...)

westfw:
0C should mean serial and parallel programming are disabled (but erase will still work, and erases the lock bits as well.) (0bxxxxxx00) and application section access to the bootloader area is disabled (0bxx00xxxx).
But the bootloader can still access the application section (0bxxxx11xx), which is what you’re seeing, right?

So, you mean instead of 0C, use 00?

(I can try it on monday, but since it is "scrubing me", pls be so kind and answer with a Y or N
thank you

So, you mean instead of 0C, use 00?

To "lock" an arduino, I'd leave the bootloader unprogrammed, and set the Lock bits to 0 (the contents of the boot lock bits would be irrelevant at that point.)
The sequence would look like (using a programmer connected to the ISP port):

  1. "Burn Bootloader" - to set fuses appropriately for the "board."
  2. "Upload using programmer" (erases bootloader, uploads sketch, leaves fuses.) (Hmm. There's some chance that newer IDEs will upload the "sketch and the bootloader combined" image. Keep an eye on it...)
  3. Change lock bits to 0 using avrdude.

thank you
over and out