LPC1768 IAP升級方法 實例
測試說明:
本實驗通過串口0對開發板進行IAP升級,本工程為引導程序,將開發板上的USB轉串口連接到電腦
插上3.2寸彩屏模塊,下載程序到開發板,彩屏顯示按鍵功能:
INTO鍵:擦除應用程序
方向鍵 ok:進行IAP升級,等待串口接收應用程序
方向鍵 down:運行應用程序
方向鍵 up:顯示菜單
當按下ok鍵等待串口接收應用程序時,電腦通過超級終端發送預先編譯好的應用程序(BIN)格式
以1K Xmodem模式進行發送給開發板,開發板接收完成后顯示 Updata Complete,按方向鍵down
開始執行應用程序
單片機源程序如下:
- /*******************************************************************************
- * Company: Wang Electronic Technology Co., Ltd.
- ******************************************************************************
- * 文件名稱:main.c
- * 功能說明:IAP引導程序
- * 版 本:V1.1
- * 作 者:jeansonm
- ********************************************************************************
- * 文件名稱:
- * 功能說明:
- * 版 本:
- * 更新作者:
- * 日 期:
- * 更新原因:
- ********************************************************************************/
- #include "LPC17xx.h"
- #include "lcd_bsp.h"
- #include "xmodem1k.h"
- #include "iap.h"
- #include <stdio.h>
- #define IMG_START_SECTOR 0x00010000 /* Sector 16 應用程序地址 */
- #define IMG_END_SECTOR 0x00037FFF /* Sector 20 */
- /* Function Prototype */
- static uint32_t load_image(uint8_t *data, uint16_t length);
- /* Character array workspace for GLCD print functions */
- #define MAX_STRING_SIZE 50
- static uint8_t string[3][MAX_STRING_SIZE];
- static uint32_t received_data = 0;
- /* State-machine variable to control application functionality */
- enum state_machine {
- READY = 0,
- MENU,
- ERASE_FLASH,
- FLASH_IMG,
- SHOW
- };
- enum state_machine cmd;
- /*********************************************************************************************************
- ** Function name: JMP_Boot
- ** Descriptions: 跳轉到應用程序
- ** input parameters: address 應用程序地址
- ** output parameters: 無
- ** Returned value: 無
- *********************************************************************************************************/
- __asm void JMP_Boot( uint32_t address ){
- LDR SP, [R0] ;Load new stack pointer address
- LDR PC, [R0, #4] ;Load new program counter address
- }
- /*********************************************************************************************************
- ** Function name: Boot
- ** Descriptions: 跳轉到應用程序
- ** input parameters: 無
- ** output parameters: 無
- ** Returned value: 無
- *********************************************************************************************************/
- void Boot( void )
- {
- SCB->VTOR = IMG_START_SECTOR & 0x1FFFFF80; //修改中斷向量表
- JMP_Boot(IMG_START_SECTOR);
- }
- /*********************************************************************************************************
- ** Function name: Screen_Fresh
- ** Descriptions: 刷新顯示
- ** input parameters: p 顯示信息
- ** output parameters: 無
- ** Returned value: 無
- *********************************************************************************************************/
- void Screen_Fresh(char *p)
- {
- LCD_Clear(Black);
- LCD_DisplayString(0, 0, "Application MENU");
- LCD_DisplayString(0, 16, "Press \"INT0\" to erase image");
- LCD_DisplayString(0, 16*2, "Press \"UP\" to print menu");
- LCD_DisplayString(0, 16*3, "Press \"CENTER\" to transfer image");
- LCD_DisplayString(0, 16*4, "Press \"DOWN\" to display image");
- LCD_DisplayString(0, 16*5, string[0]);
- LCD_DisplayString(0, 16*6, string[1]);
- LCD_DisplayString(0, 16*7, string[2]);
- LCD_DisplayString(0, 16*9, p);
- }
- int main(void)
- {
- uint32_t ints[4];
- SystemClockUpdate();
- LCD_BSP_Init(); //LCD初始化
- LCD_Clear(Black);
- LCD_SetBackColor(Black);
- LCD_SetTextColor(White);
- cmd = MENU;
- while(1)
- {
- switch(cmd)
- {
- case READY:
- if (!(LPC_GPIO2->FIOPIN & (1<<10)))/* INT0 鍵 */
- {
- Screen_Fresh("Erasing Images...");
- cmd = ERASE_FLASH;
- }
- else if(!(LPC_GPIO1->FIOPIN & (1<<29)))/* 方向鍵 up 鍵 */
- {
- cmd = MENU;
- }
- else if(!(LPC_GPIO1->FIOPIN & (1<<25)))/* 方向鍵 ok 鍵 */
- {
- Screen_Fresh("Waiting for XMODEM Xfer...");
- cmd = FLASH_IMG;
- }
- else if(!(LPC_GPIO1->FIOPIN & (1<<26)))/* 方向鍵 down 鍵 */
- {
- Screen_Fresh("Execute program");
- cmd = SHOW;
- }
- break;
- case MENU:
- /* 顯示引導程序版本 */
- if(u32IAP_ReadBootVersion (&ints[0], &ints[1]) == IAP_STA_CMD_SUCCESS)
- {
- snprintf((char *)string[0], MAX_STRING_SIZE, "Boot Code version %d.%d", ints[0], ints[1]);
- }
-
- /* 顯示器件ID */
- if(u32IAP_ReadPartID(&ints[0]) == IAP_STA_CMD_SUCCESS)
- {
- snprintf((char *)string[1], MAX_STRING_SIZE, "Part ID: %d (%#x)", ints[0], ints[0]);
- }
- /* 顯示器件序列號 */
- u32IAP_ReadSerialNumber(&ints[0], &ints[1], &ints[2], &ints[3]);
- snprintf((char *)string[2], MAX_STRING_SIZE, "Serial #: %08X:%08X:%08X:%08X", ints[0], ints[1], ints[2], ints[3]);
- Screen_Fresh("Menu");
- cmd = READY;
- break;
- case ERASE_FLASH:
- /* 擦除存儲區準備IAP升級 */
- if ((u32IAP_PrepareSectors(16, 20) == IAP_STA_CMD_SUCCESS) &&
- (u32IAP_EraseSectors (16, 20) == IAP_STA_CMD_SUCCESS))
- Screen_Fresh("Erase Done");
- else
- Screen_Fresh("Erase FAIL");
- cmd = READY;
- break;
- case FLASH_IMG:
- /* 清空接收寄存器 */
- received_data = 0;
- /* 更新APP程序 */
- vXmodem1k_Client(&load_image);
- Screen_Fresh("Updata Complete");
- cmd = READY;
- break;
- case SHOW:
- Boot();
- cmd = READY;
- break;
- }
-
- }
- }
- static uint32_t load_image(uint8_t *data, uint16_t length)
- {
- if(length > 0){
-
- /* 準備寫扇區操作 */
- if(u32IAP_PrepareSectors(16, 20) == IAP_STA_CMD_SUCCESS)
- {
- /* 將RAM內容復制到Flash */
- if(u32IAP_CopyRAMToFlash(IMG_START_SECTOR + received_data, (uint32_t)data, length) == IAP_STA_CMD_SUCCESS)
- {
- /* 比較復制內容 */
- if(u32IAP_Compare(IMG_START_SECTOR + received_data, (uint32_t)data, length, 0) == IAP_STA_CMD_SUCCESS)
- {
- received_data += length;
- return 1;
- }
- }
- }
- /* 打印失敗信息 */
- Screen_Fresh("FAIL (RESET & ERASE IMAGE)");
- return 0;
- }
- else
- return 0;
- }
復制代碼
所有資料51hei提供下載:
LPC1768寶馬開發板串口IAP升級實例.zip
(112.42 KB, 下載次數: 65)
2018-10-8 15:33 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|