# MSPM33 program in the flash

Test program for the LP-MSPM33C321A LaunchPad: it runs from the MAIN flash,
blinks the red LED (PA0, jumper J6) and counts in SRAM. It shows that a
program written with `flash program` boots from the flash and runs, with
and without PEEDI.

| File               | Contents                                                       |
|--------------------|----------------------------------------------------------------|
| `startup.c`        | vector table, reset handler: copy `.data`, clear `.bss`, `main()` |
| `main.c`           | LED on PA0, counter and state in SRAM                          |
| `mspm33_flash.ld`  | code at 0x10000000 (flash), data and stack at 0x30000000 (SRAM) |
| `makefile`         | build with `cross/arm-ronetix-eabi-11.2.0`, `make install` copies the image to `/tftpboot` |

## Addresses

The MSPM33 maps the flash and the SRAM twice (TRM SLAU962 1.3, datasheet
Table 8-2):

| Memory | Non-secure alias | Secure alias (used here) |
|--------|------------------|--------------------------|
| Flash  | 0x00000000       | 0x10000000               |
| SRAM   | 0x20000000       | 0x30000000               |

The program is linked at 0x10000000, the MAIN flash address of PEEDI and of
TI's OpenOCD. After a reset the CPU is in the secure state; the boot ROM
starts the application with the vector table at the start of the flash and
sets SCB VTOR = 0x10000000 and GSC VTOR_S (0x40047B80) = 0x10000001.

## Build

    make            # mspm33_flash.elf, mspm33_flash.bin, mspm33_flash.map
    make install    # copy mspm33_flash.bin to /tftpboot

## Target configuration

`etc/cortex-m/mspm33.cfg`, with the lockout recovery **disabled**:
`CORE0_LOCKOUT_RECOVERY = MSPM_MASS_ERASE` erases the flash at every reset,
also the program which was just written.

    ;CORE0_LOCKOUT_RECOVERY  = MSPM_MASS_ERASE

On the LaunchPad: J101 jumpers removed (PEEDI on J103), J6 inserted (red LED).

## Program and run

    mspm33> flash erase 0x10000000 0x800
    mspm33> flash program tftp://192.168.3.10/mspm33_flash.bin bin 0x10000000
    mspm33> flash verify tftp://192.168.3.10/mspm33_flash.bin bin 0x10000000
    mspm33> reset
    mspm33> go

`reset` stops the core at the reset vector and returns when the core is
connected:

    ++ info: core 0: vector catch, PC=0x10000042, CPSR=0xF9000000
    ++ info: core 0: connected

With a firmware before 26.12.1014, `reset` returned before the core was
connected and a `go` right after it gave "unable to start CORE0". Without
PEEDI (power on, or the reset button) the program starts by itself.

## Check

The state is in `test` at 0x30000004 (see `mspm33_flash.map`):

| Address    | Field          | Expected                          |
|------------|----------------|-----------------------------------|
| 0x30000000 | `data_pattern` | 0xA5A55A5A (`.data` from the flash) |
| 0x30000004 | `test.magic`   | 0x600DF00D: `main()` was reached  |
| 0x30000008 | `test.data_ok` | 1: `.data` was copied             |
| 0x3000000C | `test.counter` | increases                         |
| 0x30000010 | `test.toggles` | increases at every LED toggle (about 2 per second) |
| 0x30000014 | `test.vtor`    | 0x10000000: SCB VTOR seen by the program |

    mspm33> memory read32 0x30000000 6
    0x30000000: A5A55A5A 600DF00D 00000001 008E4F36
    0x30000010: 00000004 10000000
    mspm33> halt
    CORE0 -> Cortex-M33 - stopped by user request
             PC=0x10000110, CPSR=0x29000000
    mspm33> memory read32 0x40047B80 2
    0x40047B80: 10000001 01000001

A PC in 0x10000xxx shows that the program runs from the flash. A fault stops
in `default_handler` (0x10000040).

## Tested

On LP-MSPM33C321A with PEEDI firmware 26.12.1007 (2026-12-03): programmed,
verified; the program started by itself after the hardware reset (state in
SRAM set before PEEDI connected) and after the reset with vector catch
(`go`); the LED blinks; VTOR set by the boot ROM.

## Reset without NRST

The boot ROM sets the secure vector table (GSC VTOR_S) only after a BOOTRST
(NRST, power on). With an empty flash it points to the SRAM (0x30000000). A
reset of the core only (SYSRESETREQ, `RESET_TIME = 0`) doesn't run the boot
ROM: after programming, the CPU would start from the old VTOR_S, not the new
program. Therefore PEEDI (from firmware 26.12.1009) sets VTOR_S = 0x10000001
and SCB VTOR = 0x10000000 after programming the start of the MAIN flash, as
TI's OpenOCD does. Tested with `RESET_TIME = 0`: VTOR_S was 0x30000000 before
`flash program`, 0x10000001 after it, and the reset stopped at the reset
handler 0x10000042.

## Debug in the STOP mode

`make STOP=1` builds a variant which enters the STOP mode after 6 LED toggles
(SYSCTL PMODECFG = STOP, SCR.SLEEPDEEP, WFI) and counts the WFI in
`test.stop` (0x30000018). In STOP the CPU debug access (AHB-AP) is not
available (TRM 33.2.2), unless the PWR-AP keeps it:

    CORE0_LOW_POWER_DEBUG   = MSPM

Tested with PEEDI firmware 26.12.1010:

- without the parameter: after 6 toggles `memory read32 0x30000004 6` gives
  "can not read target memory", `halt` gives "unable to stop CORE0";
- with the parameter: the memory reads work in STOP (`test.stop` = 1,
  `test.toggles` = 6, the counter stops) and `halt` stops the core in the
  WFI loop.

## Pin configuration

The PINCM registers of the IOMUX start at offset 4 (TRM Table 9-3): PINCM1
(PA0) is at 0x400CC004. The pin table of the datasheet shows 0x400CC000 for
PINCM1, which is wrong: a write there is ignored, the GPIO toggles but the
pin stays disconnected and the LED doesn't blink.

## TI examples

The LaunchPad user's guide (SLAU969) refers to the `gpio_toggle_output`
example of the MSPM33 SDK (DriverLib, No RTOS). TI's Trusted Firmware-M port
runs its secure image from the flash at 0x10000000, as this program does.
