19.2.9.4.3 Programming External Memories

Programming external memories requires the use of applets, which are memory programming algorithms running in the target internal SRAM.

Applets permit ROM code size reduction, no hard-coded memory identification tables, and give the possibility to add support for several types of external memories.

Applet binaries are ciphered and bundled into the Secure SAM-BA tool. They cannot be modified by the user and only Microchip can provide them.

Depending on the memory and applet implementation, the number of commands available can differ from one applet to another.

The first step is to send the ciphered applet to the target. This is done through the Send Applet command:

(Device to PC) >> SAPT,0,9870,0,01#

(Device to PC) << CACK,00000000,00009870#

(PC to device) >> <applet_binary.cip>

(Device to PC) << CACK,00000000,00000000#

In the example above, Secure SAM-BA requests sending an applet of size 0x9870 and that is acknowledged by the monitor. Secure SAM-BA sends the applet ciphered binary file (applet_binary.cip), and after checking the signature and deciphering the applet in SRAM, the monitor sends the status (0x0: successful).

Now that the applet is in SRAM, before executing its code, its mailbox must be filled. The mailbox is the 32-word buffer, at the beginning of the applet area, which allows exchanging commands and arguments with the applet.

To do so, the Send Applet Mailbox command must be issued.

The mailbox is not ciphered, and is automatically written at the correct address by the ROM code.

(PC to device) >> SMBX,0,80,0,01#

(Device to PC) << CACK,00000000,00000080#

(PC to device) >> <applet_init_mailbox.bin>

(Device to PC) << CACK,00000000,00000000#

Now the applet program can be run with the Execute Applet command:

(PC to device) >> EAPP,0,0,0,00#

(Device to PC) << ASTA,00000000,00000000#

The monitor replies with a status of the applet execution (0x0: successful).

During this step, the ROM code has retrieved the information that will be used when receiving the next Write File command:
  • A memory has been successfully detected/initialized.
  • The address and the size of the data buffer: where the data exchanged can be stored (the final binary to be programmed into the external memory can be bigger than the buffer size).

Now a Send File command can be issued, with the size:

(PC to device) >> SFIL,0,5000,0,01#

(Device to PC) << CACK,00000000,00004000#

(PC to device) >> <first 0x4000 bytes of the file to be programmed>

(Device to PC) << CACK,00000000,00001000#

(PC to device) >> <next file chunk of 0x1000 bytes>

(Device to PC) << CACK,00000000,00000000#