找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 5478|回復(fù): 3
打印 上一主題 下一主題
收起左側(cè)

LPC1768單片機(jī)串口IAP升級實(shí)例源碼

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
#
ID:90762 發(fā)表于 2018-10-8 15:35 | 只看該作者 回帖獎(jiǎng)勵(lì) |正序?yàn)g覽 |閱讀模式
LPC1768 IAP升級方法 實(shí)例  
測試說明:
    本實(shí)驗(yàn)通過串口0對開發(fā)板進(jìn)行IAP升級,本工程為引導(dǎo)程序,將開發(fā)板上的USB轉(zhuǎn)串口連接到電腦
    插上3.2寸彩屏模塊,下載程序到開發(fā)板,彩屏顯示按鍵功能:
    INTO鍵:擦除應(yīng)用程序
    方向鍵 ok:進(jìn)行IAP升級,等待串口接收應(yīng)用程序
    方向鍵 down:運(yùn)行應(yīng)用程序
    方向鍵 up:顯示菜單
   
    當(dāng)按下ok鍵等待串口接收應(yīng)用程序時(shí),電腦通過超級終端發(fā)送預(yù)先編譯好的應(yīng)用程序(BIN)格式
    以1K Xmodem模式進(jìn)行發(fā)送給開發(fā)板,開發(fā)板接收完成后顯示 Updata Complete,按方向鍵down
    開始執(zhí)行應(yīng)用程序

單片機(jī)源程序如下:
  1. /*******************************************************************************
  2.   * Company: Wang Electronic Technology Co., Ltd.
  3.   ******************************************************************************
  4.   * 文件名稱:main.c
  5.   * 功能說明:IAP引導(dǎo)程序
  6.   * 版    本:V1.1
  7.         * 作    者:jeansonm
  8. ********************************************************************************
  9.   * 文件名稱:
  10.   * 功能說明:
  11.   * 版    本:
  12.         * 更新作者:        
  13.   * 日    期:
  14.         * 更新原因:
  15. ********************************************************************************/
  16. #include "LPC17xx.h"
  17. #include "lcd_bsp.h"
  18. #include "xmodem1k.h"
  19. #include "iap.h"
  20. #include <stdio.h>

  21. #define        IMG_START_SECTOR        0x00010000        /* Sector 16 應(yīng)用程序地址 */
  22. #define        IMG_END_SECTOR                0x00037FFF        /* Sector 20 */


  23. /*        Function Prototype */
  24. static uint32_t load_image(uint8_t *data, uint16_t length);

  25. /*        Character array workspace for GLCD print functions */
  26. #define MAX_STRING_SIZE                50
  27. static uint8_t string[3][MAX_STRING_SIZE];
  28. static uint32_t received_data = 0;

  29. /*        State-machine variable to control application functionality */
  30. enum state_machine {
  31.         READY = 0,
  32.         MENU,
  33.         ERASE_FLASH,
  34.         FLASH_IMG,
  35.         SHOW
  36. };
  37. enum state_machine         cmd;

  38. /*********************************************************************************************************
  39. ** Function name:       JMP_Boot
  40. ** Descriptions:        跳轉(zhuǎn)到應(yīng)用程序
  41. ** input parameters:    address 應(yīng)用程序地址
  42. ** output parameters:   無
  43. ** Returned value:      無
  44. *********************************************************************************************************/
  45. __asm void JMP_Boot( uint32_t address ){
  46.    LDR SP, [R0]                ;Load new stack pointer address
  47.    LDR PC, [R0, #4]        ;Load new program counter address
  48. }

  49. /*********************************************************************************************************
  50. ** Function name:       Boot
  51. ** Descriptions:        跳轉(zhuǎn)到應(yīng)用程序
  52. ** input parameters:    無
  53. ** output parameters:   無
  54. ** Returned value:      無
  55. *********************************************************************************************************/
  56. void Boot( void )
  57. {
  58.         SCB->VTOR = IMG_START_SECTOR & 0x1FFFFF80;        //修改中斷向量表
  59.         JMP_Boot(IMG_START_SECTOR);
  60. }

  61. /*********************************************************************************************************
  62. ** Function name:       Screen_Fresh
  63. ** Descriptions:        刷新顯示
  64. ** input parameters:    p 顯示信息
  65. ** output parameters:   無
  66. ** Returned value:      無
  67. *********************************************************************************************************/
  68. void Screen_Fresh(char *p)
  69. {
  70.         LCD_Clear(Black);
  71.         LCD_DisplayString(0, 0,          "Application MENU");
  72.         LCD_DisplayString(0, 16,          "Press \"INT0\" to erase image");
  73.         LCD_DisplayString(0, 16*2, "Press \"UP\" to print menu");
  74.         LCD_DisplayString(0, 16*3, "Press \"CENTER\" to transfer image");
  75.         LCD_DisplayString(0, 16*4, "Press \"DOWN\" to display image");
  76.         LCD_DisplayString(0, 16*5, string[0]);
  77.         LCD_DisplayString(0, 16*6, string[1]);
  78.         LCD_DisplayString(0, 16*7, string[2]);
  79.         LCD_DisplayString(0, 16*9, p);
  80. }
  81. int main(void)
  82. {
  83.         uint32_t ints[4];

  84.         SystemClockUpdate();

  85.         LCD_BSP_Init();                                //LCD初始化
  86.         LCD_Clear(Black);
  87.         LCD_SetBackColor(Black);
  88.         LCD_SetTextColor(White);

  89.         cmd = MENU;
  90.         while(1)
  91.         {               
  92.                 switch(cmd)
  93.                 {
  94.                         case READY:                                
  95.                                 if                (!(LPC_GPIO2->FIOPIN & (1<<10)))/*        INT0 鍵 */
  96.                                 {
  97.                                         Screen_Fresh("Erasing Images...");
  98.                                         cmd = ERASE_FLASH;                                                
  99.                                 }
  100.                                 else if(!(LPC_GPIO1->FIOPIN & (1<<29)))/*        方向鍵 up 鍵 */
  101.                                 {
  102.                                         cmd = MENU;                                
  103.                                 }
  104.                                 else if(!(LPC_GPIO1->FIOPIN & (1<<25)))/*        方向鍵 ok 鍵 */
  105.                                 {
  106.                                         Screen_Fresh("Waiting for XMODEM Xfer...");
  107.                                         cmd = FLASH_IMG;
  108.                                 }
  109.                                 else if(!(LPC_GPIO1->FIOPIN & (1<<26)))/*        方向鍵 down 鍵 */
  110.                                 {
  111.                                         Screen_Fresh("Execute program");
  112.                                         cmd = SHOW;
  113.                                 }
  114.                                 break;

  115.                         case MENU:                                
  116.                                 /*        顯示引導(dǎo)程序版本 */
  117.                                 if(u32IAP_ReadBootVersion (&ints[0], &ints[1]) == IAP_STA_CMD_SUCCESS)
  118.                                 {
  119.                                         snprintf((char *)string[0], MAX_STRING_SIZE, "Boot Code version %d.%d", ints[0], ints[1]);
  120.                                 }
  121.                         
  122.                                 /*        顯示器件ID */
  123.                                 if(u32IAP_ReadPartID(&ints[0]) == IAP_STA_CMD_SUCCESS)
  124.                                 {
  125.                                         snprintf((char *)string[1], MAX_STRING_SIZE, "Part ID: %d (%#x)", ints[0], ints[0]);
  126.                                 }

  127.                                 /*        顯示器件序列號 */
  128.                                 u32IAP_ReadSerialNumber(&ints[0], &ints[1], &ints[2], &ints[3]);
  129.                                 snprintf((char *)string[2], MAX_STRING_SIZE, "Serial #: %08X:%08X:%08X:%08X", ints[0], ints[1], ints[2], ints[3]);
  130.                                 Screen_Fresh("Menu");                                
  131.                                 cmd = READY;
  132.                                 break;

  133.                         case ERASE_FLASH:
  134.                                 /*        擦除存儲(chǔ)區(qū)準(zhǔn)備IAP升級  */
  135.                                 if ((u32IAP_PrepareSectors(16, 20) == IAP_STA_CMD_SUCCESS) &&
  136.                                     (u32IAP_EraseSectors  (16, 20) == IAP_STA_CMD_SUCCESS))
  137.                                         Screen_Fresh("Erase Done");
  138.                                 else
  139.                                         Screen_Fresh("Erase FAIL");                        
  140.                                 cmd = READY;
  141.                                 break;

  142.                         case FLASH_IMG:
  143.                                 /*        清空接收寄存器 */
  144.                                 received_data = 0;               

  145.                                 /* 更新APP程序 */
  146.                                 vXmodem1k_Client(&load_image);               
  147.                                 Screen_Fresh("Updata Complete");
  148.                                 cmd = READY;
  149.                                 break;
  150.                         case SHOW:
  151.                                 Boot();
  152.                                 cmd = READY;
  153.                                 break;
  154.                 }
  155.                
  156.         }
  157. }

  158. static uint32_t load_image(uint8_t *data, uint16_t length)
  159. {
  160.         if(length > 0){
  161.                
  162.                 /*        準(zhǔn)備寫扇區(qū)操作 */
  163.                 if(u32IAP_PrepareSectors(16, 20) == IAP_STA_CMD_SUCCESS)
  164.                 {                           
  165.                         /*        將RAM內(nèi)容復(fù)制到Flash */
  166.                         if(u32IAP_CopyRAMToFlash(IMG_START_SECTOR + received_data, (uint32_t)data, length) == IAP_STA_CMD_SUCCESS)
  167.                         {                                
  168.                                 /*        比較復(fù)制內(nèi)容 */
  169.                                 if(u32IAP_Compare(IMG_START_SECTOR + received_data, (uint32_t)data, length, 0) == IAP_STA_CMD_SUCCESS)
  170.                                 {
  171.                                         received_data +=  length;
  172.                                         return 1;
  173.                                 }
  174.                         }
  175.                 }
  176.                 /*        打印失敗信息 */
  177.                 Screen_Fresh("FAIL (RESET & ERASE IMAGE)");
  178.                 return 0;
  179.         }
  180.         else
  181.                 return 0;
  182. }
復(fù)制代碼

所有資料51hei提供下載:
LPC1768寶馬開發(fā)板串口IAP升級實(shí)例.zip (112.42 KB, 下載次數(shù): 65)


評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏2 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

板凳
ID:985511 發(fā)表于 2021-12-1 18:02 | 只看該作者
此文及后面的資料很不錯(cuò),剛好用上這個(gè)資料
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:624664 發(fā)表于 2019-10-15 19:51 | 只看該作者
來看看
回復(fù)

使用道具 舉報(bào)

樓主
ID:589817 發(fā)表于 2019-8-22 09:54 | 只看該作者
資料很好
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表