Ejemplo n.º 1
0
int context_switch(PCB* p_pcb_old) {
	PROC_STATE_E state;
	
	state = gp_current_process->m_state;

	if (state == NEW) {
		if (gp_current_process != p_pcb_old && p_pcb_old->m_state != NEW) {
			if (p_pcb_old->m_state != BLOCKED_ON_RECEIVE &&
					p_pcb_old->m_state != BLOCKED_ON_MEMORY) {
				p_pcb_old->m_state = READY;
			}
			p_pcb_old->mp_sp = (U32*) __get_MSP();
		}
		gp_current_process->m_state = RUN;
		__set_MSP((U32) gp_current_process->mp_sp);
		__rte();  // pop exception stack frame from the stack for a new processes
	}
	
	if (gp_current_process != p_pcb_old) {
		if (state == READY){
			if (p_pcb_old->m_state != BLOCKED_ON_MEMORY && p_pcb_old->m_state != BLOCKED_ON_RECEIVE) {
				p_pcb_old->m_state = READY;
			}
			p_pcb_old->mp_sp = (U32*) __get_MSP(); // save the old process's sp
			gp_current_process->m_state = RUN;
			__set_MSP((U32) gp_current_process->mp_sp); //switch to the new proc's stack    
		} else {
			gp_current_process = p_pcb_old; // revert back to the old proc on error
			return RTX_ERR;
		} 
	}
	return RTX_OK;
}	
Ejemplo n.º 2
0
/*@brief: switch out old pcb (p_pcb_old), run the new pcb (gp_current_process)
 *@param: p_pcb_old, the old pcb that was in RUN
 *@return: RTX_OK upon success
 *         RTX_ERR upon failure
 *PRE:  p_pcb_old and gp_current_process are pointing to valid PCBs.
 *POST: if gp_current_process was NULL, then it gets set to pcbs[0].
 *      No other effect on other global variables.
 */
int process_switch(PCB *p_pcb_old) 
{
	PROC_STATE_E state;
	
	state = gp_current_process->m_state;

	if (state == NEW) {
		if (gp_current_process != p_pcb_old && p_pcb_old->m_state != NEW) {
			p_pcb_old->m_state = RDY;
			p_pcb_old->mp_sp = (U32 *) __get_MSP();
		}
		gp_current_process->m_state = RUN;
		__set_MSP((U32) gp_current_process->mp_sp);
		__rte();  // pop exception stack frame from the stack for a new processes
	} 
	
	/* The following will only execute if the if block above is FALSE */

	if (gp_current_process != p_pcb_old) {
		if (state == RDY){ 		
			p_pcb_old->m_state = RDY; 
			p_pcb_old->mp_sp = (U32 *) __get_MSP(); // save the old process's sp
			gp_current_process->m_state = RUN;
			__set_MSP((U32) gp_current_process->mp_sp); //switch to the new proc's stack    
		} else {
			gp_current_process = p_pcb_old; // revert back to the old proc on error
			return RTX_ERR;
		} 
	}
	return RTX_OK;
}
Ejemplo n.º 3
0
int main(void)
{
  
	uint32_t JumpAddress = 0;
	
	SysTick_Init();

	memcpy((uint8*)&tag_info.net_flag_set,  (void*)STORE_SYS_INFO_PAGE_ADDR, sizeof(tag_flash_info_t));
#ifdef QRCODE
	/*check if qrcode can be gernerate*/
	if(tag_info.qr_set_flag == SYS_PARA_MAGIC_NUM && tag_info.qr_crc != cal_crc16(&tag_info.qrcode[0], 63))
	{
		if (((*(__IO uint32_t*)APP_ERCODE_ADDRESS) & 0x2FFE0000 ) == 0x20000000)
    { 
      /* Jump to user application */
      JumpAddress = *(__IO uint32_t*) (APP_ERCODE_ADDRESS + 4);
      Jump_To_Application = (pFunction) JumpAddress;
      
      /* Initialize user application's Stack Pointer */
      __set_MSP(*(__IO uint32_t*) APP_ERCODE_ADDRESS);
      
      /* Jump to application */
      Jump_To_Application();
    }
  }
#endif
	
	/*check if soft update flag is set*/
	if(SYS_PARA_MAGIC_NUM == tag_info.update_software_flag)
	{
		if(tag_info.software_crc == cal_crc16((uint8*)APP_ERCODE_ADDRESS, 0x400*29))
		{
		 if(1 == program_move(tag_info.software_crc))
		 {
				while(1)
				{
					//error update
				}
		 }
		}
	}
	
	if (((*(__IO uint32_t*)APPLICATION_ADDRESS) & 0x2FFE0000 ) == 0x20000000)
    { 
      /* Jump to user application */
      JumpAddress = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4);
      Jump_To_Application = (pFunction) JumpAddress;
      
      /* Initialize user application's Stack Pointer */
      __set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS);
      
      /* Jump to application */
      Jump_To_Application();
    }
		
}
Ejemplo n.º 4
0
/**
  * @brief  Main program.
  * @param  None
  * @retval : None
  */
int main (void)
{

  
    /* Configures the different system clocks */
    RCC_Configuration();
    /* Configures the different GPIO ports */
    GPIO_Configuration();
   
    /* Test if Key push-button on STM3210x-EVAL Board is pressed */
    value_push_button = Push_Button_Read();
    if (value_push_button == 0x00)
    {
        /* If Key is pressed ,Execute the IAP i2c routine*/
        iap_i2c();
    }
    /* Keep the user application running */
    if (value_push_button == 0x01)
    {
        /*The user code is called in iap_i2c function when the value of opcode is equal to OPC_USRCD */
        /* Jump to user application */
        JumpAddress = *(__IO uint32_t*) (USER_START_ADDRESS +4);
        Jump_To_Application = (pFunction) JumpAddress;
        /* Initialize user application's Stack Pointer */
        __set_MSP(* ( __IO uint32_t* ) USER_START_ADDRESS);
        Jump_To_Application();
    }

	return (0);



}
Ejemplo n.º 5
0
//! @brief Exits bootloader and jumps to the user application.
static void jump_to_application(uint32_t applicationAddress, uint32_t stackPointer)
{
#if BL_FEATURE_OTFAD_MODULE
    quadspi_cache_clear();
    oftfad_resume_as_needed();
#endif

    shutdown_cleanup(kShutdownType_Shutdown);

    // Create the function call to the user application.
    // Static variables are needed since changed the stack pointer out from under the compiler
    // we need to ensure the values we are using are not stored on the previous stack
    static uint32_t s_stackPointer = 0;
    s_stackPointer = stackPointer;
    static void (*farewellBootloader)(void) = 0;
    farewellBootloader = (void (*)(void))applicationAddress;

    // Set the VTOR to the application vector table address.
    SCB->VTOR = (uint32_t)APP_VECTOR_TABLE;

    // Set stack pointers to the application stack pointer.
    __set_MSP(s_stackPointer);
    __set_PSP(s_stackPointer);

   // while (1); /* SGF REMOVE */
    // Jump to the application.
    farewellBootloader();
    // Dummy fcuntion call, should never go to this fcuntion call
    shutdown_cleanup(kShutdownType_Shutdown);
}
Ejemplo n.º 6
0
void jump_to_app()
{
    const struct pios_board_info *bdinfo = &pios_board_info_blob;

    PIOS_LED_On(PIOS_LED_HEARTBEAT);
    // Look at cm3_vectors struct in startup. In a fw image the first uint32_t contains the address of the top of irqstack
    uint32_t fwIrqStackBase = (*(__IO uint32_t *)bdinfo->fw_base) & 0xFFFE0000;
    // Check for the two possible irqstack locations (sram or core coupled sram)
    if (fwIrqStackBase == 0x20000000 || fwIrqStackBase == 0x10000000) {
        /* Jump to user application */
        FLASH_Lock();
        RCC_APB2PeriphResetCmd(0xffffffff, ENABLE);
        RCC_APB1PeriphResetCmd(0xffffffff, ENABLE);
        RCC_APB2PeriphResetCmd(0xffffffff, DISABLE);
        RCC_APB1PeriphResetCmd(0xffffffff, DISABLE);
		
#ifdef PIOS_INCLUDE_USB
        PIOS_USBHOOK_Deactivate();
#endif

        JumpAddress = *(__IO uint32_t *)(bdinfo->fw_base + 4);
        Jump_To_Application = (pFunction)JumpAddress;
        /* Initialize user application's Stack Pointer */
        __set_MSP(*(__IO uint32_t *)bdinfo->fw_base);
        Jump_To_Application();
    } else {
        DeviceState = failed_jump;
        return;
    }
}
Ejemplo n.º 7
0
static inline void StartApplication(uint32_t start_addr)
{ 
    uint32_t reset_vector_table;
    reset_handler_t reset_handler;

    /* Set MSP to the application MSP */
    __set_MSP(*((uint32_t *)start_addr));

    /* Load the address of the reset handler placed after __initial_sp */
    reset_vector_table = *((uint32_t *)(start_addr + 4));
    reset_handler = (void *) reset_vector_table;

    /* Check if application is running in thread mode, and if not exit the current ISR */
    uint32_t isr_vector_num = (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk);

    if (isr_vector_num > 0) {
        __asm volatile( 
            "   LDR   R4, =0xFFFFFFFF       \n"
            "   LDR   R5, =0xFFFFFFFF       \n"
            "   LDR   R6, [R0, #0x00000004] \n"
            "   LDR   R7, =0x21000000       \n"
            "   PUSH  {r4-r7}               \n"
            "   LDR   R4, =0x00000000       \n"
            "   LDR   R5, =0x00000000       \n"
            "   LDR   R6, =0x00000000       \n"
            "   LDR   R7, =0x00000000       \n"
            "   PUSH  {r4-r7}               \n"
            "   LDR   R0, =0xFFFFFFF9       \n"
            "   BX    R0                    \n"
            "   .ALIGN                      \n"
        );
    }
Ejemplo n.º 8
0
// Activate the bootloader without BOOT* pins.
STATIC NORETURN mp_obj_t machine_bootloader(void) {
    // rocky ignore: pyb_usb_dev_deinit();
    storage_flush();

    // rocky ignore: HAL_RCC_DeInit();
    // rocky ignore: HAL_DeInit();

#if defined(MCU_SERIES_F7)
    // arm-none-eabi-gcc 4.9.0 does not correctly inline this
    // MSP function, so we write it out explicitly here.
    //__set_MSP(*((uint32_t*) 0x1FF00000));
    __ASM volatile ("movw r3, #0x0000\nmovt r3, #0x1FF0\nldr r3, [r3, #0]\nMSR msp, r3\n" : : : "r3", "sp");

    ((void (*)(void)) *((uint32_t*) 0x1FF00004))();
#else
    // __HAL_REMAPMEMORY_SYSTEMFLASH();

    // arm-none-eabi-gcc 4.9.0 does not correctly inline this
    // MSP function, so we write it out explicitly here.
    //__set_MSP(*((uint32_t*) 0x00000000));
    // __ASM volatile ("movs r3, #0\nldr r3, [r3, #0]\nMSR msp, r3\n" : : : "r3", "sp");
		__set_MSP(*((uint32_t*) 0x00000000));
    ((void (*)(void)) *((uint32_t*) 0x00000004))();
#endif

    while (1);
}
Ejemplo n.º 9
0
/**
  * @brief  calls Stm32F1xx on-chip bootloader for flashing
  * @param  None
  * @retval None - shall not ever return
  */
void enter_bootloader(void) {
	USART_DeInit(USART1);
	RCC_DeInit();
	SysTick->CTRL = 0;
	SysTick->LOAD = 0;
	SysTick->VAL = 0;

	__ASM volatile (
			"MOVS r4,#1\n"
			"MSR primask, r4\n"
			"LDR r4,=#0x1FFFF000\n"
			"LDR r3,[r4]\n"
			"MSR msp,r3\n"
			"LDR r3,[r4, #4]\n"
			"blx r3\n"
	);
	 // the above ASM does the same but is much shorter
	// and does not need core_cm3.c (for the __set_XXXX())
	#if 0
	__set_PRIMASK(1);
	// changing stack point would invalidate all local var on stack!
	#define SYSMEM ((uint32_t*)0x1FFFF000) /*specific to STM32F1xx*/
	register const uint32_t SP = SYSMEM[0];
	__set_MSP(SYSMEM[0]/*sp addr*/);
	register const uint32_t BL = SYSMEM[1];
	typedef void (*BLFUNC)();
	((BLFUNC)SYSMEM[1]/*bootloader addr*/)();
	#endif
}
Ejemplo n.º 10
0
int main()
{
	pFunction Jump_To_Application;
	uint32_t JumpAddress;

	SystemInit();		/* 配置系统时钟为72M */	
	NVIC_Configuration();

	watchdog_init();
	delay_init(72);	//72M
	w25x64_init();

	InitSerial();
	Signel_led_init();
	TIM_init();
	SerialPutString("BootLoader Runing...\n");
	SerialPutString("BootLoader for ");
	SerialPutString(SYSTEM_NAME);
	SerialPutString("\n");
	ymodem();

	iap();
	SerialPutString("Load App!\n");

	TIM_Cmd(TIM2,DISABLE);
	if (((*(__IO uint32_t*)APP_LOCATION) & 0x2FFE0000 ) == 0x20000000)
	{
		JumpAddress = *(__IO uint32_t*) (APP_LOCATION + 4);
		Jump_To_Application = (pFunction) JumpAddress;
		__set_MSP(*(__IO uint32_t*) APP_LOCATION);
		Jump_To_Application();
	}
	return 0;
}
Ejemplo n.º 11
0
/* This function resets some microcontroller peripherals to reset
 hardware configuration to ensure that the In-System Programming module
 will work properly. ISP is normally called from reset and assumes some reset
 configuration settings for the MCU.
 Some of the peripheral configurations may be redundant in your specific
 project.
 #define SystemFrequency (SystemCoreClock*LPC_SYSCTL->SYSAHBCLKDIV)
 */
void ReinvokeISP(void) {
	uint32_t command[5];
	uint32_t result[4];

	/* make sure 32-bit Timer 1 is turned on before calling ISP */
	LPC_SYSCTL->SYSAHBCLKCTRL |= 0x00400;
	/* make sure GPIO clock is turned on before calling ISP */
	LPC_SYSCTL->SYSAHBCLKCTRL |= 0x00040;
	/* make sure IO configuration clock is turned on before calling ISP */
	LPC_SYSCTL->SYSAHBCLKCTRL |= 0x10000;
	/* make sure AHB clock divider is 1:1 */
	LPC_SYSCTL->SYSAHBCLKDIV = 1;

	/* Send Reinvoke ISP command to ISP entry point*/
	command[0] = 57;

	/* Set stack pointer to ROM value (reset default) This must be the last
	 piece of code executed before calling ISP, because most C expressions
	 and function returns will fail after the stack pointer is changed. */
	__set_MSP(*((uint32_t *) 0x1FFF0000)); /* inline asm function */

	/* Invoke ISP. We call "iap_entry" to invoke ISP because the ISP entry is done
	 through the same command interface as IAP. */
	iap_entry(command, result);
	// Not supposed to come back!
}
Ejemplo n.º 12
0
/* This function resets some microcontroller peripherals to reset
 * hardware configuration to ensure that the USB In-System Programming module
 * will work properly. It is normally called from reset and assumes some reset
 * configuration settings for the MCU.
 * Some of the peripheral configurations may be redundant in your specific
 * project.
 */
void ReinvokeISP(void)
{
    /* make sure USB clock is turned on before calling ISP */
    LPC_SYSCON->SYSAHBCLKCTRL |= 0x04000;
    /* make sure 32-bit Timer 1 is turned on before calling ISP */
    LPC_SYSCON->SYSAHBCLKCTRL |= 0x00400;
    /* make sure GPIO clock is turned on before calling ISP */
    LPC_SYSCON->SYSAHBCLKCTRL |= 0x00040;
    /* make sure IO configuration clock is turned on before calling ISP */
    LPC_SYSCON->SYSAHBCLKCTRL |= 0x10000;
    /* make sure AHB clock divider is 1:1 */
    LPC_SYSCON->SYSAHBCLKDIV = 1;
    /* Send Reinvoke ISP command to ISP entry point*/
    command[0] = 57;
    init_msdstate();					 /* Initialize Storage state machine */
    /* Set stack pointer to ROM value (reset default) This must be the last
     * piece of code executed before calling ISP, because most C expressions
     * and function returns will fail after the stack pointer is changed.
     * In addition ensure the CONTROL register is set to 0 so the MSP is
     * used rather than the PSP.
     */
    __set_MSP(*((volatile uint32_t *)0x00000000));
    __set_CONTROL(0);
    /* Enter ISP. We call "iap_entry" to enter ISP because the ISP entry is done
     * through the same command interface as IAP.
     */
    iap_entry(command, result);
    // Not supposed to come back!
}
Ejemplo n.º 13
0
static void isr_reset(void)
{
    // remove compiler warning
    (void)g_pfnVectors;

    /**
     * The hyperload bootloader sets the MSP/PSP upon a true reset, which is when the
     * LPC17xx (Cortex-M3) sets the values of the stack pointer.  But since we are
     * booting after a bootloader, we have to manually setup the stack pointers ourselves.
     */
    do {
        const uint32_t topOfStack = (uint32_t) &_vStackTop;
        __set_PSP(topOfStack);
        __set_MSP(topOfStack);
    } while(0);

    do {
        // Copy data from FLASH to RAM
        unsigned int LoadAddr, ExeAddr, SectionLen;
        unsigned int *SectionTableAddr;

        // Load base address of Global Section Table
        SectionTableAddr = &__data_section_table;

        // Copy the data sections from flash to SRAM.
        while (SectionTableAddr < &__data_section_table_end)
        {
            LoadAddr = *SectionTableAddr++;
            ExeAddr = *SectionTableAddr++;
            SectionLen = *SectionTableAddr++;
            data_init(LoadAddr, ExeAddr, SectionLen);
        }

        // At this point, SectionTableAddr = &__bss_section_table;
        // Zero fill the bss segment
        while (SectionTableAddr < &__bss_section_table_end)
        {
            ExeAddr = *SectionTableAddr++;
            SectionLen = *SectionTableAddr++;
            bss_init(ExeAddr, SectionLen);
        }
    } while (0) ;

    #if defined (__cplusplus)
        __libc_init_array();    // Call C++ library initialization
    #endif

    do {
        low_level_init();   // Initialize minimal system, such as Clock & UART
        high_level_init();  // Initialize high level board specific features
        main();             // Finally call main()
    } while(0);

    // In case main() exits:
    uart0_init(SYS_CFG_UART0_BPS);
    u0_dbg_put("main() should never exit on this system\n");
    while (1) {
        ;
    }
}
Ejemplo n.º 14
0
/*******************************************************************************
* Function Name  : main.
* Description    : main routine.
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
int main(void)
{
	DFU_Button_Config();

	/* Check if the Key push-button on STM3210x-EVAL Board is pressed */
	if (DFU_Button_Read())
	{ /* Test if user code is programmed starting from address 0x8003000 */
		if (((*(__IO uint32_t*)ApplicationAddress) & 0x2FFE0000 ) == 0x20000000)
		{ /* Jump to user application */

		JumpAddress = *(__IO uint32_t*) (ApplicationAddress + 4);
		Jump_To_Application = (pFunction) JumpAddress;
		/* Initialize user application's Stack Pointer */
		__set_MSP(*(__IO uint32_t*) ApplicationAddress);
		Jump_To_Application();
		}
	} /* Otherwise enters DFU mode to allow user to program his application */

	/* Enter DFU mode */
	DeviceState = STATE_dfuERROR;
	DeviceStatus[0] = STATUS_ERRFIRMWARE;
	DeviceStatus[4] = DeviceState;

	Set_System();
	Set_USBClock();

	USB_Init();  
  
	/* turn on LED */
	GPIOB->BSRR = GPIO_Pin_5;	

	/* Main loop */
	while (1)
	{}
}
Ejemplo n.º 15
0
Archivo: kos.c Proyecto: puchinya/kos
void kos_init_regs(void)
{
	uint32_t msp = __get_MSP();
	__set_PSP(msp);
	__set_MSP((uint32_t)g_kos_isr_stk + g_kos_isr_stksz);
	__set_CONTROL(2);
}
Ejemplo n.º 16
0
/*******************************************************************************
 * 名    称: IAP_JumpTo()
 * 功    能: 跳转到应用程序段
 * 入口参数:
 * 出口参数: 无
 * 作   者: 无名沈
 * 创建日期: 2014-04-23
 * 修    改: 
 * 修改日期: 
 *******************************************************************************/
void IAP_JumpTo(u32 appAddr)
{    
    u32     JumpAddress = 0;
    u8      cpu_sr;
        
    /***********************************************
    * 描述: 保存程序地址
    */
    IAP_SetAppAddr(appAddr);
    /***********************************************
    * 描述: 关中断,防止值被中断修改
    */
    CPU_CRITICAL_ENTER();
    /***********************************************
    * 描述: 外设恢复默认,避免进入应用程序后影响程序正常运行
    */
    IAP_DevDeInit();  
    /***********************************************
    * 描述: 获取应用入口及初始化堆栈指针
    */
    JumpAddress   =*(volatile u32*) (appAddr + 4); // 地址+4为PC地址
    pApp          = (pFunction)JumpAddress;         // 函数指针指向APP
    __set_MSP       (*(volatile u32*) appAddr);    // 初始化主堆栈指针(MSP)
    __set_PSP       (*(volatile u32*) appAddr);    // 初始化进程堆栈指针(PSP)
    __set_CONTROL   (0);                            // 清零CONTROL
    /***********************************************
    * 描述: 跳转到APP程序
    */
    pApp();
    
    CPU_CRITICAL_EXIT();
}
Ejemplo n.º 17
0
int main(void)
{
  uint32_t bootStatus;
  
  /* MCU Configuration----------------------------------------------------------*/
  
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
  
  /* Configure the system clock */
  SystemClock_Config();
  
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  
  /* Turn on led Init */
  HAL_GPIO_WritePin(GPIOE, GPIO_PIN_12|GPIO_PIN_13, GPIO_PIN_RESET);
  
  /* Wait for timeout */
  bootStatus = bootWait(5000);
  
  /* Turn off led */
  HAL_GPIO_WritePin(GPIOE, GPIO_PIN_12|GPIO_PIN_13, GPIO_PIN_SET);
  
  /* Check for Result */
  if(bootStatus == 0)
  {
    /* Test if user code is programmed starting from address USBD_DFU_APP_DEFAULT_ADD */
    if(((*(__IO uint32_t*)USBD_DFU_APP_DEFAULT_ADD) & 0x2FFE0000 ) == 0x20000000)
    {
      /* Jump to user application */
      JumpAddress = *(__IO uint32_t*) (USBD_DFU_APP_DEFAULT_ADD + 4);
      JumpToApplication = (pFunction) JumpAddress;
      
      /* Initialize user application's Stack Pointer */
      __set_MSP(*(__IO uint32_t*) USBD_DFU_APP_DEFAULT_ADD);
      JumpToApplication();
    }
    
    /* load application failed */
    while (1)
    {
      HAL_Delay(50);
      HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_12|GPIO_PIN_13);
    }
  }
  
  /* Init Device Library,Add Supported Class and Start the library*/
  USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS);
  USBD_RegisterClass(&hUsbDeviceFS, &USBD_DFU);
  USBD_DFU_RegisterMedia(&hUsbDeviceFS, &USBD_DFU_fops_FS);
  USBD_Start(&hUsbDeviceFS);
  
  /* Infinite loop */
  while (1)
  {
    HAL_Delay(500);
    HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_13);
  }
}
Ejemplo n.º 18
0
int main(void){
   GPIO_InitTypeDef GPIO_InitDef;
   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
   GPIO_InitDef.GPIO_Pin = GPIO_Pin_13;
   GPIO_InitDef.GPIO_Mode = GPIO_Mode_IN;
   GPIO_InitDef.GPIO_OType = GPIO_OType_PP;
   GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_UP;
   GPIO_InitDef.GPIO_Speed = GPIO_Speed_2MHz;
   GPIO_Init(GPIOA, &GPIO_InitDef);
   uint32_t pin = !GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_13);
   GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_NOPULL;
   GPIO_Init(GPIOA, &GPIO_InitDef);
   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, DISABLE);

   void (*SysMemBootJump)(void);
   if ( (*((unsigned long *)0x2001C000) == 0xDEADBEEF) || pin || !app_ok()) {//Memory map, datasheet
      *((unsigned long *)0x2001C000) =  0xCAFEFEED; //Reset bootloader trigger
      __set_MSP(0x20001000);
 	   //Point the PC to the System Memory reset vector (+4)
 	   //AN2606
      //Table 64. Bootloader device-dependent parameters
      SysMemBootJump = (void (*)(void)) (*((uint32_t *) 0x1FFF0004));
      SysMemBootJump();
      while (1);
   }else{
      uint32_t  stack = ((const uint32_t *)APP_START)[0];
      uint32_t  entry = ((const uint32_t *)APP_START)[1];
      asm volatile(
         "msr    msp, %0        \n\t"
         "bx     %1             \n\t"
         : : "r" (stack), "r" (entry)
      );
      while(1);
   }
}
Ejemplo n.º 19
0
Archivo: main.c Proyecto: 0x00f/stm32
/**
  * @brief  main
  *         Main routine for IAP application
  * @param  None
  * @retval int
  */
int main(void)
{
  /* STM32 evalboard user initialization */
  BSP_Init();

  /* Flash unlock */
  FLASH_If_FlashUnlock();
  
  /* Test if User button on the Discovery kit is pressed */
  if (STM_EVAL_PBGetState(BUTTON_USER) == Bit_RESET)
  {
    /* Check Vector Table: Test if user code is programmed starting from address 
       "APPLICATION_ADDRESS" */
    if (((*(__IO uint32_t*)APPLICATION_ADDRESS) & 0x2FFE0000 ) == 0x20000000)
    {
      /* Jump to user application */
      JumpAddress = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4);
      Jump_To_Application = (pFunction) JumpAddress;
      /* Initialize user application's Stack Pointer */
      __set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS);
      Jump_To_Application();
    }
  }
  
  /* Init Host Library */
  USBH_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USB_Host, &USBH_MSC_cb, &USR_Callbacks);
    
  while (1)
  {
    /* Host Task handler */
    USBH_Process(&USB_OTG_Core, &USB_Host);
  }
}
Ejemplo n.º 20
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f10x_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f10x.c file
     */     

  /* FSMC NOR configuration  **************************************************/
  /* Enable the FSMC Clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);

  /* Configure FSMC Bank1 NOR/SRAM2 */
  NOR_Init();

  /* Jump to code loaded in NOR memory and execute it *************************/
  JumpAddress = *(__IO uint32_t*) (ApplicationAddress + 4);
  Jump_To_Application = (pFunction) JumpAddress;

  /* Initialize user application's Stack Pointer */
  __set_MSP(*(__IO uint32_t*) ApplicationAddress); 

  Jump_To_Application();

  while (1)
  {
  }
}
Ejemplo n.º 21
0
static void tt_use_PSP(unsigned int irq_stack_addr)
{
	__set_PSP(__get_MSP());
	__set_MSP(irq_stack_addr);
	__set_CONTROL(2);
	__ISB();
}
Ejemplo n.º 22
0
Archivo: main.c Proyecto: aithon/aithon
void startProgram(void)
{
   /* Jump to user application */
   funcPtr userAppStart = (funcPtr) (*(__IO uint32_t*) (APPLICATION_START_ADDRESS + 4));
   /* Initialize user application's Stack Pointer */
   __set_MSP(*(__IO uint32_t*) APPLICATION_START_ADDRESS);
   userAppStart();
}
Ejemplo n.º 23
0
WEAK_FUNC (void ResetISR (void)) {


    /* reset the main SP to clean up any leftovers from the boot rom. */
    __set_MSP( (uint32_t) __vector_table[0].__ptr );

    /* Unlock the PWRMOD register by writing the two keys to the PWRKEY register */
    pADI_PWR->PWRKEY = PWRKEY_VALUE_KEY1;
    pADI_PWR->PWRKEY = PWRKEY_VALUE_KEY2;

    /* set the RAM0_RET bit so the entire 16k of SRAM Bank0 is hibernate-preserved */
    pADI_PWR->PWRMOD |= (1 << BITP_PWR_PWRMOD_RAM0_RET);

#ifdef RELOCATE_IVT
    int i;
    uint8_t *pSrc, *pDst;

    /* copy the IVT (avoid use of memcpy here so it does not become locked into flash) */
    for (i=0, pSrc=(uint8_t*)__vector_table, pDst=(uint8_t*)__relocated_vector_table; i<SIZEOF_IVT; i++)
    	*pDst++ = *pSrc++;
#endif

#ifdef __GNUC__

  unsigned long *pulSrc, *pulDest;

    // Copy initialised data from flash into RAM
    pulSrc = &_etext;
    for(pulDest = &_data; pulDest < &_edata; )
    {
        *pulDest++ = *pulSrc++;
    }

    // Clear the bss segment
    for(pulDest = &_bss; pulDest < &_ebss; )
    {
        *pulDest++ = 0;
    }
    // Call application main directly.
    main();

#elif __ICCARM__

    // Call IAR system startup
    __iar_init_core();
    __iar_init_vfp();
    __cmain();

#else

    // Call application main directly.
    main();

#endif

    // Stick here if main returns
    while(1);
}
Ejemplo n.º 24
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* Configure Key Button */
  BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO); 
  
  /* Unlock the Flash to enable the flash control register access */ 
  FLASH_If_FlashUnlock();
  
  /* Test if User button on the STM324x9I_EVAL is pressed */
  if (BSP_PB_GetState(BUTTON_KEY) != GPIO_PIN_RESET)
  {
    /* Check Vector Table: Test if user code is programmed starting from address 
    "APPLICATION_ADDRESS" */
    if ((((*(__IO uint32_t*)APPLICATION_ADDRESS) & 0xFF000000 ) == 0x20000000) || \
      (((*(__IO uint32_t*)APPLICATION_ADDRESS) & 0xFF000000 ) == 0x10000000))
    {
      /* Jump to user application */
      JumpAddress = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4);
      Jump_To_Application = (pFunction) JumpAddress;
      /* Initialize user application's Stack Pointer */
      __set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS);
      Jump_To_Application();
    }
  }
  
  /* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch, instruction and Data caches
       - Configure the Systick to generate an interrupt each 1 msec
       - Set NVIC Group Priority to 4
       - Global MSP (MCU Support Package) initialization
     */
  HAL_Init();
  
  /* Configure the system clock to 168 MHz */
  SystemClock_Config();
  
  /* Init FW upgrade Application */
  FW_InitApplication();
  
  /* Init Host Library */
  USBH_Init(&hUSBHost, USBH_UserProcess, 0);
  
  /* Add Supported Class */
  USBH_RegisterClass(&hUSBHost, USBH_MSC_CLASS);
  
  /* Start Host Process */
  USBH_Start(&hUSBHost);
  
  /* Run Application (Blocking mode)*/
  while (1)
  {
    /* USB Host Background task */
    USBH_Process(&hUSBHost);
    
    /* FW Menu Process */
    FW_UPGRADE_Process();
  } 
}
Ejemplo n.º 25
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
void main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
  this is done through SystemInit() function which is called from startup
  file (startup_stm32f10x_xx.s) before to branch to application main.
  To reconfigure the default setting of SystemInit() function, refer to
  system_stm32f10x.c file
  */
  
  /* Identify the board to distinguish the SDK motherboard from the SDK dongle */
  SdkEvalIdentification();
  
  /* Declare and read the DFU flag on E2Prom */
  uint8_t cDfuFlag = SdkDfuReadDfuFlag();

  /* Initialize the Key push-button */
  SdkEvalPushButtonInit(BUTTON_SCM_PS, BUTTON_MODE_GPIO);


  /* Check if the Key push-button on is pressed */
   if (!(SdkEvalPushButtonGetState(BUTTON_SCM_PS) == RESET || cDfuFlag))
  { /* Test if user code is programmed starting from address 0x8003000 */

    if (((*(__IO uint32_t*)ApplicationAddress) & 0x2FFE0000 ) == 0x20000000)
    { /* Jump to user application */
      JumpAddress = *(__IO uint32_t*) (ApplicationAddress + 4);
      Jump_To_Application = (pFunction) JumpAddress;
      /* Initialize user application's Stack Pointer */
      __set_MSP(*((__IO uint32_t*) ApplicationAddress));
      
      Jump_To_Application();
    }
  } /*!< Otherwise enters DFU mode to allow user to program his application */

  /* Reset the DFU flag on E2Prom */
  SdkDfuResetDfuFlag();

  /* Enter DFU mode */
  DeviceState = STATE_dfuERROR;
  DeviceStatus[0] = STATUS_ERRFIRMWARE;
  DeviceStatus[4] = DeviceState;

  /* DFU initialization */
  SdkEvalDFUInit();

  /* LED2 initialization */
  SdkEvalLedInit(LED2);

  /* Main loop */
  while (1)
  {
    /* LED2 toggling */
    SdkEvalLedToggle(LED2);
    
    /* pause */
    for(volatile uint32_t i = 0 ; i<0x3FFFFF ; i++);
  }
}
Ejemplo n.º 26
0
static void
jump_application(void) {
    typedef void (*func_t)(void);
    func_t entry = (func_t)user_vtor[1];
    if (entry != (void*)0xFFFFFFFF) {
        __set_MSP(user_vtor[0]);
        entry();
    }
}
Ejemplo n.º 27
0
void JumpToApp(void)
{
		JumpAddress = *(vu32*) (Download_ApplicationAddress + 4);  //the address value
		/* Jump to user application */
		Jump_To_Application = (pFunction) JumpAddress;
		/* Initialize user application's Stack Pointer */
		__set_MSP(*(vu32*) Download_ApplicationAddress);
		Jump_To_Application();
}
Ejemplo n.º 28
0
/**
 * @brief release_processor(). 
 * @return -1 on error and zero on success
 * POST: gp_current_process gets updated
 */
int k_release_processor(void)
{
	 volatile int pid;
	 volatile proc_state_t state;
	 pcb_t * p_pcb_old = NULL;

	 pid = scheduler();
	 if (gp_current_process == NULL) {
	     return -1;  
	 }

	 p_pcb_old = gp_current_process;


	 if (pid == 1) {
	     gp_current_process = &pcb1;
	 } else if (pid ==2){
	     gp_current_process = &pcb2;
	 } else {
	     return -1;
	 }					 

	 state = gp_current_process->m_state;

     if (state == NEW) {
	     if (p_pcb_old->m_state != NEW) {
		     p_pcb_old->m_state = RDY;
			 p_pcb_old->mp_sp = (uint32_t *) __get_MSP();
		 }
		 gp_current_process->m_state = RUN;
		 __set_MSP((uint32_t) gp_current_process->mp_sp);
		 __rte();  // pop exception stack frame from the stack for new processes
	 } else if (state == RDY){     
		 p_pcb_old->m_state = RDY; 
		 p_pcb_old->mp_sp = (uint32_t *) __get_MSP(); // save the old process's sp
		 
		 gp_current_process->m_state = RUN;
		 __set_MSP((uint32_t) gp_current_process->mp_sp); //switch to the new proc's stack		
	 } else {
	     gp_current_process = p_pcb_old; // revert back to the old proc on error
	     return -1;
	 }	 	 
	 return 0;
}
Ejemplo n.º 29
0
/**
  * @brief  Program entry point
  * @param  None
  * @retval None
  */
int main(void)
{
  /* Counter for the LED toggling delay */
  __IO uint32_t i = 0; 
  
  
  /*!< At this stage the microcontroller clock setting is already configured, 
  this is done through SystemInit() function which is called from startup
  file (startup_stm32fxxx_xx.s) before to branch to application main.
  To reconfigure the default setting of SystemInit() function, refer to
  system_stm32fxxx.c file
  */  
   
  /* Configure "DFU enter" button */
  STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_GPIO);

  /* Check if the Key push-button on Devkit407 Board is pressed */
  if (STM_EVAL_PBGetState(BUTTON_USER) != 0x00)
  { /* Test if user code is programmed starting from address 0x800C000 */
    if (((*(__IO uint32_t*)APP_DEFAULT_ADD) & 0x2FFE0000 ) == 0x20000000)
    { /* Jump to user application */
      JumpAddress = *(__IO uint32_t*) (APP_DEFAULT_ADD + 4);
      Jump_To_Application = (pFunction) JumpAddress;
      /* Initialize user application's Stack Pointer */
      __set_MSP(*(__IO uint32_t*) APP_DEFAULT_ADD);
      Jump_To_Application();
    }
  } /* Otherwise enters DFU mode to allow user to program his application */

  
  USBD_Init(&USB_OTG_dev,
#ifdef USE_USB_OTG_HS 
            USB_OTG_HS_CORE_ID,
#else            
            USB_OTG_FS_CORE_ID,
#endif 
            &USR_desc, 
            &DFU_cb, 
            &USR_cb);
  
  /* Setup SysTick Timer for 10 msec interrupts 
     This interrupt is used to display the current state of the DFU core */
  if (SysTick_Config(SystemCoreClock / 100)) { 
    /* Capture error */ 
    while (1);
  } 
  
  /* Main loop */
  while (1)
  {
    if (i++ == 0x100000) {
      STM_EVAL_LEDToggle(LED4);
      i = 0;
    }
  }
} 
Ejemplo n.º 30
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  pFunction JumpToApplication;
  uint32_t JumpAddress;
  
  /* STM32F3xx HAL library initialization:
       - Configure the Flash prefetch
       - Systick timer is configured by default as source of time base, but user 
         can eventually implement his proper time base source (a general purpose 
         timer for example or other time source), keeping in mind that Time base 
         duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
         handled in milliseconds basis.
       - Set NVIC Group Priority to 4
       - Low Level Initialization
     */
  HAL_Init();
  
  /* Configure the system clock to get correspondent USB clock source */
  SystemClock_Config();
  
  /* Configure USER Button */
  BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO);
  
  /* Check if the KEY Button is pressed */
  if (BSP_PB_GetState(BUTTON_KEY) != 0x00)
  {
    /* Test if user code is programmed starting from address 0x08003000 */
    if (((*(__IO uint32_t*)USBD_DFU_APP_DEFAULT_ADD) & 0x2FFE0000 ) == 0x20000000)
    {
      /* Jump to user application */
      JumpAddress = *(__IO uint32_t*) (USBD_DFU_APP_DEFAULT_ADD + 4);
      JumpToApplication = (pFunction) JumpAddress;
      
      /* Initialize user application's Stack Pointer */
      __set_MSP(*(__IO uint32_t*) USBD_DFU_APP_DEFAULT_ADD);
      JumpToApplication();
    }
  }
  
  /* Otherwise enters DFU mode to allow user to program his application */
  /* Init Device Library */
  USBD_Init(&USBD_Device, &DFU_Desc, 0);
  
  /* Register the DFU class */
  USBD_RegisterClass(&USBD_Device, &USBD_DFU);
  
  /* Add DFU Media interface */
  USBD_DFU_RegisterMedia(&USBD_Device, &USBD_DFU_Flash_fops);
  
  /* Start Device Process */
  USBD_Start(&USBD_Device);
  /* In an infinite loop */
  while (1)
  {
  }
}