/******************************************************************************
*  Simpel LPC11Uxx blinky program for MicroCore64 board (by Paul Seerden)
*
*  1. Use default SysTick handler to toggle LED1 (P1_26) every 200 msec
*  2. Check input P0.16, if low, remap vector table to SRAM and change SysTick
*     handler vector. New handler will toggle LED2 (P1_27) every 400 msec
*  3. Use flash scatter file to make sure that startup code (including vectors)
*     is located at 0x0 and all other code at 0x00000400 (if not remap of
*     vectors will overlap user code in flash).
*******************************************************************************/
#include <LPC11Uxx.h>                          // LPC11Uxx definitions

#define SYSTICKVECT  (*((volatile unsigned long *) 0x1000003C))

void SysTick_Handler(void)
{
  static uint8_t cnt1 = 0;

    if (++cnt1 > 20)                           // 20 * 10 msec = 200 msec
    {
        cnt1 = 0;
        LPC_GPIO->B1[26] ^= 1;                 // Toggle LED1 (pin P1_26)
    }
}

void New_SysTick_Handler(void)
{
  static uint8_t cnt1 = 0;

    if (++cnt1 > 40)                           // 40 * 10 msec = 400 msec
    {
        cnt1 = 0;
        LPC_GPIO->B1[27] ^= 1;                 // Toggle LED2 (pin P1_27)
    }
}

int main(void)
{
    LPC_GPIO->DIR[1] |= (1 << 26);             // configure P1.26 (LED1) as output
    LPC_GPIO->DIR[1] |= (1 << 27);             // configure P1.27 (LED2) as output
    SysTick_Config(SystemCoreClock/100);       // generate IRQ each 10 ms

    while (1)                                  // loop forever
    {
        if (!(LPC_GPIO->PIN[0] & (1 << 16)))   // P0.16 low?
        {
            __disable_irq();

            SYSTICKVECT = (unsigned long) &New_SysTick_Handler;

            LPC_SYSCON->SYSMEMREMAP = 1;       // re-map interrupt vectors to SRAM

            __enable_irq();
        }
    }
}
