#include "drv_clk.h"
#include "drv_flash.h"


#include "stm32f4xx.h"

void flash_jump(uint32_t dest_addr, NoArgCall call)
{
//#if (STM32F10X_CL | STM32F10X_MD | STM32F10X_HD)
    if (((*(__IO uint32_t*)dest_addr) & 0x2FFE0000 ) == 0x20000000)
    {    
        __disable_irq();
		call();
        SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;

        
        __set_MSP(*(__IO uint32_t*) dest_addr);
        //SCB->VTOR = dest_addr;
        ((NoArgCall) (*(__IO uint32_t*)(dest_addr + 4)))();
    }
//#endif
}

void flash_jump_new(uint32_t dest_addr, NoArgCall call)
{
    uint32_t sp_addr = *(__IO uint32_t*)dest_addr;
    uint32_t app_addr = *(__IO uint32_t*)(dest_addr + 4);
    if ((sp_addr != 0xFFFFFFFF) 
     && (app_addr != 0xFFFFFFFF)
     && ((sp_addr & 0xF0000000) == 0x20000000)
     && ((app_addr & 0x0F000000) == 0x08000000))
    {    
        __disable_irq();
		call();
        SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;

        __set_MSP(*(__IO uint32_t*) dest_addr);
        ((NoArgCall) (*(__IO uint32_t*)(dest_addr + 4)))();
    }
}