__setup ()
{
__var tmp;
    // Disable the PLL.
    __writeMemory32(0x0000001F, 0x40048080, "Memory");  // AHBCLKCTRL = 0x1F;
    __writeMemory32(0x00000000, 0x40048040, "Memory");  // SYSPLLCLKSEL = 0;
    __writeMemory32(0x00000000, 0x40048044, "Memory");  // SYSPLLUEN = 0;   
    __writeMemory32(0x00000001, 0x40048044, "Memory");  // SYSPLLUEN = 1;
    __writeMemory32(0x00000000, 0x40048070, "Memory");  // MAINCLKSEL = 0;
    __writeMemory32(0x00000000, 0x40048074, "Memory");  // MAINLUEN = 0;
    __writeMemory32(0x00000001, 0x40048074, "Memory");  // MAINLUEN = 1;
    __writeMemory32(0x00000001, 0x40048078, "Memory");  // SYSAHBCLKDIV = 1;
    __writeMemory32(0x00000002, 0x40048000, "Memory");  // SYSMEMREMAP = 2
    __readMemory32( 0x00000000, "Memory");
}

execConfigureTraceETM()
{
    __var position;
    __var master;
    __var watermark;
    __var _flow;

// <e0.31> Trace: MTB (Micro Trace Buffer)
//   <o0.0..4> Buffer Size
//     <4=>  256B
//     <5=>  512B
//     <6=>   1kB
//     <7=>   2kB
//     <8=>   4kB
//   <o1>   Buffer Position
//     <i> Buffer position in RAM. Must be a multiple of the buffer size.
//   <o2.0> Stop Trace when buffer is full
//   <o2.1> Stop Target when buffer is full
// </e>
    master     = 0x80000005;
    position   = 0x10000E00;
    _flow      = 0x00000000;

    position  &= 0xFFFFFFF8;              // Mask POSITION.POINTER field
    watermark  = position + ((16 << (master & 0x1F)) - 32);
    _flow     |= watermark;

    __writeMemory32(0x00000000, 0x14000004, "Memory");  // MASTER
    __writeMemory32(position, 0x14000000, "Memory");    // POSITION
    __writeMemory32(_flow, 0x14000008, "Memory");       // FLOW
    __writeMemory32(master, 0x14000004, "Memory");      // MASTER
}

execUserFlashInit()
{   
    __message "------- Prepare for flashloader -------";
    __setup();
}

execUserPreload()
{   
    __message "------- Prepare for debug -------";
    __setup();
}
