Is it possible to compile-in a setting to the 8051's lock byte? I don't want to have to clear this byte at run-time or manually modify my .hex image.
Yes. This can be accomplished easily when using the Keil C51 toolchain, using either BL51 or LX51.
First, you must create your lock byte setting somewhere in code. For example, you may create create a file named "lockbyte.c" and place the following code in it:
#include <si_toolchain.h> const uint8_t code lockbyte = 0x00;
This will create a byte in flash that is initialized to zero, but it will not be located in the right place by default. In order to move it to the flash lock byte address, you will need to use linker directives. This location will be listed in the device's reference manual. For example, the EFM8BB1F8 devices have their lock byte located at flash address 0x1FFF. The linker directives can be modified by navigating to Project -> Properties -> C/C++ Build -> Settings within Simplicity Studio, then navigating to the Keil 8051 Linker > Miscellaneous window. Linker settings can be added to the [Additional Flags] field.
If you're using LX51, you will add the following to your Linker [Additional Flags] field:
SEGMENTS (?CO?LOCKBYTE(C:0x1FFF)) OVERLAY (* ! ?CO?LOCKBYTE)
This is for the normal case, when REMOVEUNSED is selected in the linker. This option removes all unused or uncalled segments. The OVERLAY directive says that it should be avoided by the overlay, and should not be determined if it is used or not. This forces it to be placed in flash, even if the variable is never called.
If REMOVEUNSED is not used, you can omit the OVERLAY directive.
If you're using LX51 and a device with code banking, then specify the bank and address. For example, use the following flags to place the lockbyte in bank 3 at address 0x1FFFF:
SEGMENTS (?CO?LOCKBYTE(B3:0x1FFFF)) OVERLAY (* ! ?CO?LOCKBYTE)
If the REMOVEUNUSED flag is used, then the overlay directive is required to exclude the lock byte from the overlay process. Otherwise, the linker will remove the lockbyte value from code memory.
For BL51, use the following: