STM32程序加载---网络

一、设计流程

编写IAP加载程序,使用网口传输数据,并将.bin文件写入。单片机启动的时候按住某一个按键跳转到程序加载流程,否则跳转到应用程序入口地址执行。

 

二、程序分析

1、main程序


int main(void)
{    
    uint32_t flag = 0x00;
#ifdef SERIAL_DEBUG
    DebugComPort_Init();    
    printf("\r\n   %s", __VESION);   
#endif  

    /* Initialize Key Button mounted on STM324xG-EVAL board */       
    IAP_Key_Init();     
    FLASH_Unlock();
    FLASH_ClearFlag(FLASH_FLAG_EOP | 
                    FLASH_FLAG_OPERR | 
                    FLASH_FLAG_WRPERR | 
                    FLASH_FLAG_PGAERR | 
                    FLASH_FLAG_PGPERR |
                    FLASH_FLAG_PGSERR); 

    
    flag = *(uint32_t*)FLASH_USER_IAP_FLAG_ADDR;
    /* Test if Key push-button on STM324xG-EVAL Board is not pressed */

    FLASH_Lock();
#ifdef SERIAL_DEBUG
    printf("\r\n  >flag=0x%08x\r\n", flag);
#endif

    if((0x21212121 == flag)||!IS_ENTER_IAP())
    {
#ifdef SERIAL_DEBUG
        printf("[UR1000-IAP]#running the IAP application\r\n");
#endif   

        /* configure ethernet (GPIOs, clocks, MAC, DMA) */ 
        ETH_BSP_Config();
        
        /* Initialize the LwIP stack */
        LwIP_Init();

        /* Initialize the TFTP server */
        IAP_tftpd_init();  
    
        /* Infinite loop */
        while (1)
        {
            /* check if any packet received */
            if (ETH_CheckFrameReceived())
            { 
                /* process received ethernet packet */
                LwIP_Pkt_Handle();
            }
            /* handle periodic timers for LwIP */
            LwIP_Periodic_Handle(LocalTime);
        }   

    }  
    else
    {
        /* Key push-button not pressed: jump to user application */
#ifdef SERIAL_DEBUG
        printf("[UR1000-IAP]#jump to user application\r\n");
#endif

        /* Check if valid stack address (RAM address) then jump to user application */
        if (((*(__IO uint32_t*)USER_FLASH_FIRST_PAGE_ADDRESS) & 0x2FFE0000 ) == 0x20000000)
        {            
            /* Jump to user application */
            JumpAddress = *(__IO uint32_t*) (USER_FLASH_FIRST_PAGE_ADDRESS + 4);
#ifdef SERIAL_DEBUG					
            printf("[UR1000-IAP]#JumpAddress = 0x%08x \r\n", JumpAddress);
#endif					
            Jump_To_Application = (pFunction) JumpAddress;

            //NVIC_SetVectorTable(0x8000000, 0x8000);
            /* Initialize user application's Stack Pointer */
            __set_MSP(*(__IO uint32_t*) USER_FLASH_FIRST_PAGE_ADDRESS);
            Jump_To_Application();
        }
        else
        {
            /* Otherwise, do nothing */
            /* LED3 (RED) ON to indicate bad software (when not valid stack address) */
#ifdef SERIAL_DEBUG
            printf("[UR1000-IAP]#bad software (not valid stack address)\r\n");
#endif            
            /* do nothing */
            while(1);
        }        
    }    
}

 

你可能感兴趣的:(单片机,stm32,程序加载,TCP)