# MSPM33 NONMAIN images

`mk_nonmain.py` creates, changes and checks images of the NONMAIN flash
region of the TI MSPM33 (MSPM33C321A, board LP-MSPM33C321A). PEEDI programs
these images with `flash program`.

## What NONMAIN is

NONMAIN is the boot configuration of the device: one 2 KB flash sector at
`0x80101800` with two structures, each ending with a CRC-32:

| Range (offset)  | Address                 | Contents                                  |
|-----------------|-------------------------|-------------------------------------------|
| 0x000 - 0x14B   | 0x80101800 - 0x8010194B | BCR: debug access, SWD, mass erase, factory reset, passwords, flash protection, boot clock |
| 0x14C           | 0x8010194C              | CRC-32 of the BCR                         |
| 0x400 - 0x44B   | 0x80101C00 - 0x80101C4B | BSL: bootloader pins, password, options   |
| 0x44C           | 0x80101C4C              | CRC-32 of the BSL configuration           |

The rest of the sector is 0xFF. The fields are described in the TRM
(SLAU962, section 1.4.2 "NON_MAIN_Configuration Registers").

The CRC-32 is: polynomial 0xEDB88320 (reflected), initial value 0xFFFFFFFF,
no final XOR, over the bytes of the structure before the CRC. The TRM
doesn't give the algorithm; it was found with the factory defaults of the
device and gives exactly their CRCs.

The boot code checks the configuration at every reset. A wrong CRC or a
wrong configuration can make the device inaccessible: SWD disabled, or the
debug access disabled together with the factory reset, can never be undone.

## Safety in PEEDI

PEEDI programs NONMAIN only with `NONMAIN = YES` in the flash section of the
target configuration file (default NO):

    [FLASH_MSPM33]
    CHIP            = MSPM33
    NONMAIN         = YES

Before the sector is erased, PEEDI checks the image and refuses it if:

- it is not exactly the whole sector (2048 bytes at 0x80101800),
- BCR_CONFIG_ID or BSL_CONFIG_ID differs from the device,
- one of the two CRCs is wrong,
- it disables the SWD (BOOTCFG0 [31:16] != 0xAABB),
- it disables the debug access and the factory reset (BOOTCFG0 [15:0] and
  BOOTCFG4 [31:16] both 0x5522).

A restricted debug access or a disabled / password protected factory reset
only gives a warning. The sector is erased and programmed in one step, only
after the check. `flash erase` and `flash erase chip` never erase NONMAIN.

`mk_nonmain.py` makes the same checks (except the config IDs of the
device) and doesn't write a refused image.

## Step 1: read the NONMAIN of the device

Start from the configuration of the device, not from an image of another
device revision (the config IDs must match).

In a PEEDI telnet session:

    mspm33> memory read32 0x80101800 1
    mspm33> memory dump 0x80101800 0x800 tftp://192.168.3.10/nonmain_read.bin

The first `memory read32` is a dummy read: right after a reset of the
MSPM33, the first access to the configuration region returns 0.

Instead of `memory dump`, the text output of
`memory read32 0x80101800 512` can be saved into a file; the script reads
both formats.

Check the read image:

    ./mk_nonmain.py nonmain_read.bin --info

`--info` lists all fields with address, value and description, and at the
end the state of SWD, debug access, factory reset and mass erase.
"CRCs OK" means that the image is a valid configuration.

### Without the device: the built-in factory default

    ./mk_nonmain.py --default --info
    ./mk_nonmain.py --default -o nonmain_new.bin --set BOOTCFG1=0xAABB5522

`--default` starts from the factory default built into the script instead
of an input image. It was read from an MSPM33C321A after a factory reset
(config IDs 0x05000000) and gives the same image, byte for byte, as the
device's own NONMAIN.

Another device revision may have other defaults or config IDs. PEEDI
refuses an image with other config IDs than the device, but it doesn't see
other differing defaults (e.g. BSL pins or boot clock). Reading the NONMAIN
of the device (above) is the safer way; use `--default` when the device is
not at hand or its configuration is to be replaced completely.

## Step 2: change fields and write the new image

    ./mk_nonmain.py nonmain_read.bin -o nonmain_new.bin --set BOOTCFG1=0xAABB5522

- `--set NAME=VALUE` sets a field by name (`--list` shows the names). The
  option can be given more than once.
- `--set-ofs OFS=VALUE` sets a 32-bit word by its offset in the sector
  (0x000 - 0x7FC) or by its address (0x80101800 - 0x80101FFC), e.g. for a
  field without a name.
- Both CRCs are recomputed. They can't be set by hand.
- Without `-o`, the changes are only shown and checked, nothing is written.
- `-o` without changes writes the input image unchanged, with its CRCs:
  this converts a `memory read32` text dump into a binary image. A damaged
  image (wrong CRC) is not written.

For every changed field the output shows the parts which change, with
the old and the new meaning, e.g.:

    BOOTCFG1                        0xAABBAABB → 0xAABB5522
      BSL invoke pin                enabled → disabled

A meaning marked with "(?)" is assumed: the TRM doesn't give the encoding of
that field (debug hold, flash bank swap policy, bootloader mode, fast boot,
memory readout), 0xAABB = enabled and 0x5522 = disabled as for the other
fields. Values which the TRM doesn't list are shown as "not documented".

The output shows the input CRCs, the changes, the state of SWD, debug
access, factory reset and mass erase, and the result of the checks. On a
terminal it is colored; `--no-color` or the environment variable `NO_COLOR`
switch the colors off. The exit code is 0 (OK), 1 (the image has errors,
it is not written) or 2 (wrong options).

The values of the BOOTCFG fields are 0xAABB (enabled), 0x5522 (disabled)
and 0xCCDD (enabled with password) for each 16-bit half, see the TRM.

Examples:

    # list the fields
    ./mk_nonmain.py --list

    # the factory default as an image
    ./mk_nonmain.py --default -o nonmain_default.bin

    # disable the BSL invoke pin (BOOTCFG1 [15:0]) - harmless, used for the test
    ./mk_nonmain.py nonmain_read.bin -o nonmain_test.bin --set BOOTCFG1=0xAABB5522

    # a word by address
    ./mk_nonmain.py nonmain_read.bin -o nonmain_new.bin --set-ofs 0x80101C34=0x10001000

    # convert a "memory read32" text dump into a binary image
    ./mk_nonmain.py nonmain_read.txt -o nonmain_read.bin

## Step 3: program the image

Copy the image to the TFTP server and program it:

    mspm33> flash program tftp://192.168.3.10/nonmain_new.bin bin 0x80101800
    ++ info: NONMAIN image checked: BCR CRC 0x44723F2A, BSL CRC 0xAF559B73
    ++ info: erasing NONMAIN at 0x80101800
    ++ info: successfully programmed 2 KB in 0.03 sec
    mspm33> flash verify tftp://192.168.3.10/nonmain_new.bin bin 0x80101800

The new configuration is used after the next reset (`reset`). Check it:

    mspm33> reset
    mspm33> memory read32 0x80101800 1
    mspm33> memory read32 0x80101800 4

Do not reset or power off the device between the erase and the end of the
programming. If PEEDI reports "NONMAIN is not programmed completely",
program the image again before the next reset.

Comment out `NONMAIN = YES` again when it is not needed any more.

## Back to the factory defaults

- Program the image read in step 1, or
- the factory reset via the debug mailbox (needs the factory reset enabled
  in the configuration): `CORE0_LOCKOUT_RECOVERY = MSPM_FACTORY_RESET` in
  the target configuration file, connect once (it runs at every connect and
  also erases MAIN and DATA), then comment it out again.

## Tested

On LP-MSPM33C321A with PEEDI firmware 26.12.1007 (2026-12-03):

- image with BOOTCFG1 = 0xAABB5522 (BSL invoke pin disabled): programmed,
  verified, accepted by the boot code after a reset; the factory default
  programmed back, identical to the original;
- image with a wrong BCR CRC: refused, NONMAIN not changed;
- the refusal of images which disable the SWD, or the debug access together
  with the factory reset, was tested on the host only (with the check
  function of `main/apps/pgm/flash/mspm.c`), not on the device.
