[DemoCode下载] MG51使用UART0实现ISP

[复制链接]
1135|2
 楼主| 598330983 发表于 2025-5-26 17:18 | 显示全部楼层 |阅读模式
ISP_UART0.rar (105.24 KB, 下载次数: 1)
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* SPDX-License-Identifier: Apache-2.0                                                                     */
  4. /* Copyright(c) 2023 Nuvoton Technology Corp. All rights reserved.                                         */
  5. /*                                                                                                         */
  6. /*---------------------------------------------------------------------------------------------------------*/



  7. //***********************************************************************************************************
  8. //  File Function: ML51 UART0 ISP demo code
  9. //***********************************************************************************************************
  10. #include "MG51_SDCC.H"
  11. #include "isp_uart0.h"

  12. //#define  isp_with_wdt
  13. unsigned int __xdata start_address,u16_addr;

  14. void Serial_ISR(void) __interrupt (4)
  15. {
  16.     PUSH_SFRS;

  17.     if (RI == 1)
  18.     {
  19.         uart_rcvbuf[bufhead++] =  SBUF;
  20.         clr_SCON_RI;                                         // Clear RI (Receive Interrupt).
  21.     }
  22.     if (TI == 1)
  23.     {
  24.         clr_SCON_TI;                                         // Clear TI (Transmit Interrupt).
  25.     }
  26.     if (bufhead == 1)
  27.     {
  28.         g_timer1Over = 0;
  29.         g_timer1Counter = 90; //for check uart timeout using
  30.     }
  31.     if (bufhead == 64)
  32.     {
  33.         bUartDataReady = TRUE;
  34.         g_timer1Counter = 0;
  35.         g_timer1Over = 0;
  36.         bufhead = 0;
  37.     }
  38.     POP_SFRS;
  39. }

  40. void Timer0_ISR(void) __interrupt (1)
  41. {
  42.     PUSH_SFRS;
  43.     if (g_timer0Counter)
  44.     {
  45.         g_timer0Counter--;
  46.         if (!g_timer0Counter)
  47.         {
  48.             g_timer0Over = 1;
  49.         }
  50.     }
  51.     if (g_timer1Counter)
  52.     {
  53.         g_timer1Counter--;
  54.         if (!g_timer1Counter)
  55.         {
  56.             g_timer1Over = 1;
  57.         }
  58.     }
  59.     POP_SFRS;
  60. }

  61. /************************************************************************************************************
  62. *    Main function
  63. ************************************************************************************************************/
  64. void main (void)
  65. {

  66.   set_CHPCON_IAPEN;
  67.   MODIFY_HIRC_24();
  68. #ifdef  isp_with_wdt
  69.   TA=0x55;TA=0xAA;WDCON=0x07;
  70. #endif
  71. //uart initial for ISP programmer GUI, always use 115200 baudrate
  72.   UART0_ini_115200_24MHz();
  73.   TM0_ini();

  74.   g_timer0Over=0;
  75.   g_timer0Counter=Timer0Out_Counter;
  76.   g_programflag=0;

  77. while(1)
  78. {
  79.         if(bUartDataReady == TRUE)
  80.         {
  81.           EA=0; //DISABLE ALL INTERRUPT
  82.           if(g_programflag==1)
  83.           {
  84.             for(count=8;count<64;count++)
  85.             {
  86. //              g_timer0Counter=Timer0Out_Counter;
  87.               IAPCN = BYTE_PROGRAM_AP;          //program byte
  88.               IAPAL = flash_address&0xff;
  89.               IAPAH = (flash_address>>8)&0xff;
  90.               IAPFD=uart_rcvbuf[count];
  91. #ifdef isp_with_wdt
  92.               set_IAPTRG_IAPGO_WDCLR;
  93. #else
  94.               set_IAPTRG_IAPGO;
  95. #endif
  96.          
  97.               IAPCN = BYTE_READ_AP;              //program byte verify
  98.               if(IAPFD!=uart_rcvbuf[count])
  99.               while(1);                          
  100.               if (CHPCON==0x43)              //if error flag set, program error stop ISP
  101.               while(1);
  102.               
  103.               g_totalchecksum=g_totalchecksum+uart_rcvbuf[count];
  104.               flash_address++;
  105.   
  106.               if(flash_address==AP_size)
  107.               {
  108.                  g_programflag=0;
  109.                  g_timer0Over =1;
  110.                  goto END_2;
  111.               }
  112.             }
  113. END_2:
  114.             Package_checksum();
  115.             uart_txbuf[8]=g_totalchecksum&0xff;
  116.             uart_txbuf[9]=(g_totalchecksum>>8)&0xff;
  117.             Send_64byte_To_UART0();
  118.           }
  119.             
  120.           switch(uart_rcvbuf[0])
  121.           {
  122.             case CMD_CONNECT:
  123.             case CMD_SYNC_PACKNO:
  124.             {
  125.               Package_checksum();
  126.               Send_64byte_To_UART0();   
  127.               g_timer0Counter=0; //clear timer 0 for no reset
  128.               g_timer0Over=0;
  129.               break;
  130.             }

  131.             case CMD_GET_FWVER:
  132.             {
  133.               Package_checksum();
  134.               uart_txbuf[8]=FW_VERSION;
  135.               Send_64byte_To_UART0();
  136.               break;
  137.             }
  138.             
  139.             case CMD_RUN_APROM:
  140.             {
  141.               goto _APROM;
  142.               break;
  143.             }

  144.             //please for ISP programmer GUI, ID always use following rule to transmit.
  145.             case CMD_GET_DEVICEID:
  146.             {
  147.               READ_ID();
  148.               Package_checksum();
  149.               uart_txbuf[8]=DID_lowB;  
  150.               uart_txbuf[9]=DID_highB;  
  151.               uart_txbuf[10]=PID_lowB;  
  152.               uart_txbuf[11]=PID_highB;  
  153.               Send_64byte_To_UART0();  
  154.               break;
  155.             }

  156.             case CMD_ERASE_ALL:
  157.             {
  158.               set_IAPUEN_APUEN;
  159.               IAPFD = 0xFF;          //Erase must set IAPFD = 0xFF
  160.               IAPCN = PAGE_ERASE_AP;
  161.               for(flash_address=0x0000;flash_address<APROM_SIZE/PAGE_SIZE;flash_address++)
  162.               {        
  163.                 IAPAL = LOBYTE(flash_address*PAGE_SIZE);
  164.                 IAPAH = HIBYTE(flash_address*PAGE_SIZE);
  165. #ifdef isp_with_wdt
  166.               set_IAPTRG_IAPGO_WDCLR;
  167. #else
  168.               set_IAPTRG_IAPGO;
  169. #endif
  170.               }
  171.               Package_checksum();
  172.               Send_64byte_To_UART0();  
  173.               break;
  174.             }

  175.             case CMD_READ_CONFIG:
  176.             {
  177.               READ_CONFIG();
  178.               Package_checksum();
  179.               uart_txbuf[8]=CONF0;
  180.               uart_txbuf[9]=CONF1;
  181.               uart_txbuf[10]=CONF2;
  182.               uart_txbuf[11]=0xff;
  183.               uart_txbuf[12]=CONF4;
  184.               uart_txbuf[13]=0xff;
  185.               uart_txbuf[14]=0xff;
  186.               uart_txbuf[15]=0xff;
  187.               Send_64byte_To_UART0();
  188.             break;
  189.             }
  190.             
  191.             case CMD_UPDATE_CONFIG:
  192.             {
  193.               recv_CONF0 = uart_rcvbuf[8];
  194.               recv_CONF1 = uart_rcvbuf[9];
  195.               recv_CONF2 = uart_rcvbuf[10];
  196.               recv_CONF4 = uart_rcvbuf[12];

  197.               set_IAPUEN_CFUEN;                  /*Erase CONFIG */
  198.               IAPCN = PAGE_ERASE_CONFIG;
  199.               IAPAL = 0x00;
  200.               IAPAH = 0x00;
  201.               IAPFD = 0xFF;
  202. #ifdef isp_with_wdt
  203.               set_IAPTRG_IAPGO_WDCLR;
  204. #else
  205.               set_IAPTRG_IAPGO;
  206. #endif
  207.               IAPCN = BYTE_PROGRAM_CONFIG;        /*Program CONFIG*/
  208.               IAPFD = recv_CONF0;
  209. #ifdef isp_with_wdt
  210.               set_IAPTRG_IAPGO_WDCLR;
  211. #else
  212.               set_IAPTRG_IAPGO;
  213. #endif
  214.               IAPFD = recv_CONF1;
  215.               IAPAL = 0x01;
  216. #ifdef isp_with_wdt
  217.               set_IAPTRG_IAPGO_WDCLR;
  218. #else
  219.               set_IAPTRG_IAPGO;
  220. #endif
  221.               IAPAL = 0x02;
  222.               IAPFD = recv_CONF2;
  223.   #ifdef isp_with_wdt
  224.               set_IAPTRG_IAPGO_WDCLR;
  225. #else
  226.               set_IAPTRG_IAPGO;
  227. #endif
  228.               IAPAL = 0x04;
  229.               IAPFD = recv_CONF4;
  230. #ifdef isp_with_wdt
  231.               set_IAPTRG_IAPGO_WDCLR;
  232. #else
  233.               set_IAPTRG_IAPGO;
  234. #endif
  235.               clr_IAPUEN_CFUEN;

  236.               READ_CONFIG();                        /*Read new CONFIG*/  
  237.               Package_checksum();
  238.               uart_txbuf[8]=CONF0;
  239.               uart_txbuf[9]=CONF1;
  240.               uart_txbuf[10]=CONF2;
  241.               uart_txbuf[11]=0xff;
  242.               uart_txbuf[12]=CONF4;
  243.               uart_txbuf[13]=0xff;
  244.               uart_txbuf[14]=0xff;
  245.               uart_txbuf[15]=0xff;
  246.               Send_64byte_To_UART0();
  247.               break;
  248.             }

  249.             case CMD_UPDATE_APROM:
  250.             {
  251. //              g_timer0Counter=Timer0Out_Counter;
  252.               set_IAPUEN_APUEN;
  253.               IAPFD = 0xFF;          //Erase must set IAPFD = 0xFF
  254.               IAPCN = PAGE_ERASE_AP;
  255.               
  256.               start_address = 0;
  257.               start_address = uart_rcvbuf[8];
  258.               start_address |= ((uart_rcvbuf[9]<<8)&0xFF00);
  259.               AP_size = 0;
  260.               AP_size = uart_rcvbuf[12];
  261.               AP_size |= ((uart_rcvbuf[13]<<8)&0xFF00);

  262.               u16_addr = start_address + AP_size;
  263.               flash_address = (start_address&0xFF00);

  264.               while(flash_address< u16_addr)
  265.               {
  266.                 IAPAL = LOBYTE(flash_address);
  267.                 IAPAH = HIBYTE(flash_address);
  268. #ifdef isp_with_wdt
  269.                 set_IAPTRG_IAPGO_WDCLR;
  270. #else
  271.                 set_IAPTRG_IAPGO;
  272. #endif
  273.                 flash_address += PAGE_SIZE;
  274.               }
  275.               
  276.               g_totalchecksum = 0;
  277.               flash_address = start_address;
  278.               g_programflag = 1;

  279.               for(count=16;count<64;count++)
  280.               {
  281.                 IAPCN = BYTE_PROGRAM_AP;
  282.                 IAPAL = flash_address&0xff;
  283.                 IAPAH = (flash_address>>8)&0xff;
  284.                 IAPFD = uart_rcvbuf[count];
  285. #ifdef isp_with_wdt
  286.               set_IAPTRG_IAPGO_WDCLR;
  287. #else
  288.               set_IAPTRG_IAPGO;
  289. #endif
  290.                 IAPCN = BYTE_READ_AP;                //program byte verify
  291.                 set_IAPTRG_IAPGO;

  292.                 if(IAPFD!=uart_rcvbuf[count])
  293.                 while(1);
  294.                 if (CHPCON==0x43)                //if error flag set, program error stop ISP
  295.                 while(1);
  296.                
  297.                 g_totalchecksum=g_totalchecksum+uart_rcvbuf[count];
  298.                 flash_address++;

  299.                 if(flash_address==AP_size)
  300.                 {
  301.                   g_programflag=0;
  302.                    goto END_1;
  303.                 }
  304.               }
  305. END_1:               
  306.               Package_checksum();
  307.               uart_txbuf[8]=g_totalchecksum&0xff;
  308.               uart_txbuf[9]=(g_totalchecksum>>8)&0xff;
  309.               Send_64byte_To_UART0();  
  310.               break;
  311.             }
  312.           }  
  313.           bUartDataReady = FALSE;
  314.           bufhead = 0;

  315.           EA=1;
  316.       }
  317.       /*For connect timer out   */
  318.       if(g_timer0Over==1)
  319.       {
  320.         CALL_NOP;
  321.         goto _APROM;
  322.       }
  323.       
  324.       /*for uart time out or buffer error  */
  325.        if(g_timer1Over==1)
  326.       {
  327.        if((bufhead<64)&&(bufhead>0)||(bufhead>64))
  328.          {
  329.              bufhead=0;
  330.          }
  331.       }  
  332. }   

  333. _APROM:
  334.     EA=0;
  335.     MODIFY_HIRC_16();
  336.     clr_CHPCON_IAPEN;
  337.     TA = 0xAA; TA = 0x55; CHPCON = 0x80;                   //software reset enable boot from APROM
  338.     /* Trap the CPU */
  339.     while(1);  
  340. }




旧时光放映机 发表于 2025-5-28 09:15 | 显示全部楼层
看起来你分享了一个MG51的ISP程序代码,这是一个很好的资源,可以帮助那些需要通过UART0接口进行ISP编程的人。
穷得掉渣大侠 发表于 2025-5-28 18:33 | 显示全部楼层
看起来你已经上传了ISP_UART0.rar,包含了MG51使用UART0实现ISP的示例代码。如果有任何问题或者需要进一步的帮助,请随时提问。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

267

主题

5575

帖子

22

粉丝
快速回复 在线客服 返回列表 返回顶部