Hi,
I am working on a 328p based system, and would like to protect the application code from reading (externally). I don't need the bootloader so I programed the 328p with an uno as an isp programmer.
I would like to set the lock bits.
I did some search for a week or so and found there are a lot information about fuses but little about the lock bits.
From what I have got, I am a little bit confused about the lock bits of this chip. In the datasheet, it said there are 8 bits and there are two bits in charge of the protection of the application part (lb1 lb2), and will form 3 modes.
But in most threads I have read using avrdue to set them, like this -Ulock:w:0x00:m -u. And some people said one need to modify the value from 0x3f to 0x0f to lock it, and others mentioned that unlock is 0xFF, lock is 0xCF.
Could some one give more details about this? Why there are two Hex number to set two bits? What's the right way to do it?
I am using a uno as isp programmer. And I would like to test if I can still read the hex file from it or not once I have set the lock bits.
What I have tried is the following:
C:\avr\bin>c:\avr/bin/avrdude -Cc:\avr/etc/avrdude.conf -v -patmega328p -cstk500v1 -PCOM8 -b19200 -e -Ulock:w:0x0f:m -u
and the system gets back as:
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:\avr/etc/avrdude.conf"
Using Port : COM8
Using Programmer : stk500v1
Overriding Baud Rate : 19200
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 : STK500
Description : Atmel STK500 Version 1.x firmware
Hardware Version: 2
Firmware Version: 1.18
Topcard : Unknown
Vtarget : 0.0 V
Varef : 0.0 V
Oscillator : Off
SCK period : 0.1 us
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.03s
avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: erasing chip
avrdude: reading input file "0x0f"
avrdude: writing lock (1 bytes):
Writing | ################################################## | 100% 0.03s
avrdude: 1 bytes of lock written
avrdude: verifying lock memory against 0x0f:
avrdude: load data lock data from input file 0x0f:
avrdude: input file 0x0f contains 1 bytes
avrdude: reading on-chip lock data:
Reading | ################################################## | 100% 0.02s
avrdude: verifying ...
avrdude: 1 bytes of lock verified
avrdude done. Thank you.
So did I set the lock bits succeefully?
After that I downloaded the blink example to the 328p chip using the same wiring and the same uno as isp prorammer in the arduino ide, and the blinking led is back on the chip again. I checked the output of the ide:
d:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude -Cd:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf -v -patmega328p -cstk500v1 -PCOM8 -b19200 -Uflash:w:C:\Users\xxxx\AppData\Local\Temp\arduino_build_996885/Blink.ino.hex:i
Looks to me there is no erase operation (-e). I assume if I had set the lock bit to lock(0f), the only way to write the flash is to erase it first...