aa. How to execute code from PIC32 RAM?

PIC32 micrcontrollers have five memory management registers: BMXCON, BMXDKPBA, BMXDUDBA, BMXDUPBA, BMXPUPBA, BMXDRMSZ, BNXPFMSZ and BMXBOOTSZ.

BMXDKPBA, BMXDUDBA, BMXDUPBA and BMXPUPBA registers default to zero (0). This means that that flash RAM is used for kernel 0 and 1 program segments and the whole RAM is used for kernel 0 and kernel 1 data segments. No code may be executed from RAM.

However at least BMXDKPBA, BMXDUDBA and BMXDUPBA registers must be propetly set to allow code execution from PIC32 RAM. The simplest option is to set BMXDKPBA to the data RAM segment size and BMXDUDBA and BMXDUPBA registers to 32 kB, which is the size of PIC32MX250F128B RAM. This will cause the rest of the RAM to be treated as kernel 0 and 1 program memory.

Here is an example:

STEP 1:

BMXDKPBA = 10 * 1024 bytes = 10 kB = 0x2800 --> kernel 0 and 1 data RAM = 10 kB, followed by kernel 0 and 1 program RAM = 22 kB

BMXDUDBA = BMXDUPBA = 32 kB = 0x8000 --> user data RAM and user program RAM are not defined

This configuration is perfectly ok for the most of the PIC32 projects, because all the firmware code is executed in kernel mode.

STEP 2:

The next consideration is on how to build a program code to run from PIC32MX250F128B RAM. C compilers for PIC32 microcontrollers support linker configuration files. A configuration file must produce an executable code (abosolute or relative) that can run in the kernel RAM program segment. The resulting *.HEX file must read to PIC32MX250F128B kernel program RAM segment with:

MemoryWrite(register_address as UInt32, new_value as UInt32) as Integer

instructions. You may also set BMXCON register to 0x41 (arbitration mode 1 - default) or 0x40 (arbitration mode 0, if DMA controller is not used) to avoid memory exceptions.

STEP 3:

The executable code entry point must be determined. The easiest way to do this is to use a disassembly function from the programming environment. Look for the main function 32-bit start-up address. It must be within kernel program RAM segment (ex. 0xA000xxxx).

Use:

PIC32_Jump(addr as UInt32) as integer

function to start the program.

See the PIC32MX2X0 programming guide on the functions usage details.

A full programming example will be provided soon....

MEMORY MATRIX CONTROL REGISTERS DESCTIPTIONS:

BMXCON (R/W) = configuration register that defines arbtration mode (0,1,2) and enables various memory exceptions that occure, if a memory access is outside defined memory segments boundaries.

BMXDKPBA (R/W) = defines base address for kernel RAM program segment. Only lower 16-bits are used.The upper 16-bits are presumed to be =0x8000 for kernel program RAM segment 0 and 0xA000 for kernel program RAM segment 1 (max. value = 32 kB = 0x8000 on PIC32MX250F128B)

BMXDUDBA (R/W) = defines User data RAM segment base address (max. 32 kB = 0x8000 on PIC32MX250F128B)

BMXDUPBA (R/W) = defines User program RAM segment base address that folows the user RAM data segment (max. value = 32 kB = 0x8000 on PIC32MX250F128B)

BMXPUPBA (R/W) = defines base address for kernel flash RAM program segment. Only lower 24-bits are used.The upper 8-bits are presumed to be =0x9D for kernel program RAM segment 0 and 0xBD for kernel program RAM segment 1 (max. value = 128 kB = 0x20000 on PIC32MX250F128B)

BMXDRMSZ (read-only) = PIC32 RAM size in bytes (32 kB = 0x8000 on PIC32MX250F128B)

BNXPFMSZ (read-only) = PIC32 flash RAM size in bytes (128 kB = 0x20000 on PIC32MX250F128B)

BMXBOOTSZ (read-only) = Boot flash RAM size (C00 = 3 kB on PIC32MX250F128B)

ALSO SEE:

- PROGRAMMING GUIDE: PIC32 microcontroller programing in VB.NET

- How does PIC32MX250F128B compare to PIC18F24J50 and PIC18F25K50 (PIC32MX250F128B datasheet)?

- User functions support