/**
 ****************************************************************************************
 * DA1458x object loading script through debugger interface (e.g. JLink, etc).
 * The goal of this script is to load the Keils's object file to the DA1458x RAM
 * assuring that the DA1458x has been previously cleaned up.
 ****************************************************************************************
 */

// Write to DA1458x SYS_CTRL_REG -> Generate SW reset, enable debugger
_WWORD(0x50000012, 0x80a4)

// Previous command (SW reset) unhalts the processor
// Write to Cortex-M0 DHCSR Core Debug Register -> Halt Cortex-M0
_WDWORD(0xe000edf0, 0xa05f0003)

// Required sleep time (in msec) before debugger reset is executed
_sleep_(1000)

// Debugger reset (check Keil debugger settings)
// Preselected reset type (found in Options-> Debug-> Settings) is Normal (0):
//    - Normal: Resets core & peripherals via SYSRESETREQ & VECTRESET bit
RESET

// Load object file
LOAD %L

// Load stack pointer
SP = _RDWORD(0x20000000)

// Load program counter
$ = _RDWORD(0x20000004)

FUNC void disp_memlog(void)
{
    int idx;
    unsigned int min_delta;

    exec("log > Memlog.log");

    printf("\n\n*** Memory Logging Results ***\n\n");

    printf(">>> ENV HEAP <<<\n");
    printf("Used size in this HEAP  : %4d (current) - %4d (maximum)\n", mem_log[0].used_sz, mem_log[0].max_used_sz);
    printf("Used size in other HEAPs: %4d (current) - %4d (maximum)\n\n", mem_log[0].used_other_sz, mem_log[0].max_used_other_sz);

    printf(">>> DB HEAP <<<\n");
    printf("Used size in this HEAP  : %4d (current) - %4d (maximum)\n", mem_log[1].used_sz, mem_log[1].max_used_sz);
    printf("Used size in other HEAPs: %4d (current) - %4d (maximum)\n\n", mem_log[1].used_other_sz, mem_log[1].max_used_other_sz);

    printf(">>> MSG HEAP <<<\n");
    printf("Used size in this HEAP  : %4d (current) - %4d (maximum)\n", mem_log[2].used_sz, mem_log[2].max_used_sz);
    printf("Used size in other HEAPs: %4d (current) - %4d (maximum)\n\n", mem_log[2].used_other_sz, mem_log[2].max_used_other_sz);

    printf(">>> Non-Ret HEAP <<<\n");
    printf("Used size in this HEAP  : %4d (current) - %4d (maximum)\n", mem_log[3].used_sz, mem_log[3].max_used_sz);
    printf("Used size in other HEAPs: %4d (current) - %4d (maximum)\n\n", mem_log[3].used_other_sz, mem_log[3].max_used_other_sz);

    exec("log off");
} //disp_memlog() ends
