• 采用UDP协议的PIC32MZ ethernet bootloader


             了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序)。

      经过千辛万苦,今天终于完工PIC32MZ EC Starter Kit的ethernet bootloader项目。我将整个项目, 命名为PhnBootloader。它分为两个部分。第一个部分是PC 端的host程序PhnLoader_v3.0。另一部分是单片机端boot程序PhnBoot_v3.0,是从Microchip AN1388 ethernet bootloader移植过来的。整个移植过程碰到很多困难,主要是由于ethernet bootloader使用到Microchip 的TCP/IP Stack,但是TCP/IP Stack不支持PIC32MZ。 这里我列出移植需要注意的地方:

    1. PIC32MZ默认enable cache.

    2. PIC32MZ ethernet module ports mapping

    3. PIC32MZ EC Starter Kit的external PHY的LEDs和Start Kit的LED1,LED2复用

    4. PIC32MZ EC Starter Kit的ethernet module有些port复用AN功能。

    5. ethernet bootloader download完application后,跳转到application前 ,注意要清零所有使用到的register,包括disable interrupt。

      PhnLoader_v3.0和PhnBoot_v3.0之间采用UDP协议通信,通信数据包格式采用以下协定:

    <STX><CMD><ADDRL><ADDRH><ADDRU><ADDRM><LEN><DATA>...<DATA><ETX>

    STX - Start of packet indicator
    ETX - End of packet indicator
    LEN - The length of true data
    DATA - General data 16 bytes, only first LEN of datas are true
    CMD - Base command
    ADDR - Address up to 32 bits  ( ADDRL , ADDRH , ADDRH, ADDRM)

      具体有以下Base command:

    RD-VER:  0x00 -- Read Version Information (最终版本未实现)
    RD_MEM: 0x01 -- Read Program Memory (最终版本未实现)
    ER_MEM: 0x03 -- Erase Program Memory
    WR_MEM: 0x02 -- Write Program Memory
    WR_CFG: 0x04 -- Write Configuration Registers (最终版本未实现)

      PhnLoader_v3.0

      PhnLoader_v3.0 是PC端host 程序。

          运行环境:Window + .NET Framework 4.5

          开发语言:C#

          通信类型:UDP Server

          IP 地址: 192.168.1.105

          端口号:  8720

          UI 如下图:

      C#源代码主要部分:

                try
                {
                    while (loaderReader.Peek() >= 0)
                    {
                        pBarLoading.PerformStep();
                        recordLine = loaderReader.ReadLine();
    
                        if (recordLine.Contains(EXTEND_TOKEN) == true)
                        {
                            if (recordLine.Contains(USER_ID_TOKEN) == true)
                            {
                                isNextLineUserID = true;
                                continue;
                            }
                            //else if (recordLine.Contains(CONFIG_BITS_TOKEN) == true)
                            //{
                            //    const int ADDR_U_START_INDEX = 9;
                            //    const int ADDR_U_LENGTH = 4;
                            //    string addrU = recordLine.Substring(ADDR_U_START_INDEX, ADDR_U_LENGTH);
                            //    loaderUpperAddr = Convert.ToInt32(addrU, 16) << 16;
                            //    isNextLineConfigBits = true;
                            //    continue;
                            //}
                            else if (recordLine.Contains(PIC32_CONFIG_BITS_TOKEN) == true)
                            {
                                const int ADDR_U_START_INDEX = 9;
                                const int ADDR_U_LENGTH = 4;
                                string addrU = recordLine.Substring(ADDR_U_START_INDEX, ADDR_U_LENGTH);
                                loaderUpperAddr = Convert.ToInt32(addrU, 16) << 16;
                                isNextLineConfigBits = true;
                                continue;
                            }
                            else
                            {
                                const int ADDR_U_START_INDEX = 9;
                                const int ADDR_U_LENGTH = 4;
                                string addrU = recordLine.Substring(ADDR_U_START_INDEX, ADDR_U_LENGTH);
                                loaderUpperAddr = Convert.ToInt32(addrU, 16) << 16;
                                continue;
                            }
                        }
                        else if (recordLine.Contains(END_OF_HEX_FILE_TOKEN) == true)
                        {
                            break;
                        }
                        if (isNextLineUserID)
                        {
                            isNextLineUserID = false;
                            // do nothing;
                        }
                        else if (isNextLineConfigBits)
                        {
                            //if (!DownloadConfigLine(recordLine))
                            //{
                            //    Debug.WriteLine("Error found during configuration bits programming");
                            //    loaderReader.Close();
                            //    loaderServer.Close();
                            //    btnDownload.Enabled = true;
                            //    return;
                            //}
                            DownloadConfigLine(recordLine);
                            isNextLineConfigBits = false;
                        }
                        else
                        {
                            if (!DownloadDataLine(recordLine))
                            {
                                Debug.WriteLine("Error found during data programming");
                                loaderReader.Close();
                                loaderServer.Close();
                                btnDownload.Enabled = true;
                                return;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Error: " + ex.Message);
                    textBoxStatus.ForeColor = Color.Red;
                    textBoxStatus.AppendText("Downloading failed
    ");
                    textBoxStatus.ForeColor = Color.Black;
                    loaderServer.Close();
                    loaderReader.Close();
                    btnDownload.Enabled = true;
                    return;
                }

      PhnBoot_v3.0

      PhnBoot_v3.0 是单片机端Boot 程序。

          运行环境:PIC32MZ EC Starter Kit

          开发语言:C 语言 (Microchip XC32编译)

          通信类型:UDP Client

          IP 地址: 192.168.1.123

          端口号:  8740

      主要代码段:

            switch (GenUDPSt)
            {
            case SM_HOME:
                ARPResolve(&ServerNode.IPAddr);
                startTime = TickGet();
                GenUDPSt++;
                break;
            case SM_ARP:
                if (TickGet() - startTime < 5*TICK_SECOND)
                {
                    if (ARPIsResolved(&ServerNode.IPAddr,&ServerNode.MACAddr))
                    {
                        MySock = UDPOpen(LOCAL_CLIENT_PORT,&ServerNode,REMOTE_SERVER_PORT);
                        startTime = TickGet();
                        GenUDPSt++; 
                    }
                }
                else
                {
                    GenUDPSt = SM_HOME;
                }
                break;
            case SM_UDPOPEN:
                if (TickGet() - startTime < TICK_SECOND)
                {
                    if (MySock != INVALID_UDP_SOCKET)
                    {
                        startTime = TickGet();
                        GenUDPSt++;
                    }
                }
                else
                {
                    GenUDPSt = SM_HOME;
                }
                break;
            case SM_UDPPUT:
                if (TickGet() - startTime < 5*TICK_SECOND)
                {
                    if (UDPIsPutReady(MySock) > BUFFER_MAX)
                    {
                        UDPPutString(ok);
                        UDPFlush();
                        GenUDPSt++;
                    }
                }
                else
                {
                    GenUDPSt = SM_CLOSE;
                }
                break;
            case SM_UDPGET:
                if (TickGet() - startTime < 5*TICK_SECOND)
                {
                    udpPackets = UDPIsGetReady(MySock);
                    if (udpPackets >= BUFFER_MAX)
                    {
                       UDPGetArray(frameBuffer, BUFFER_MAX);
                       UDPDiscard();
                       if (frameBuffer[0] == STX && frameBuffer[BUFFER_MAX - 1] == ETX)
                       {
                        Uart_Putc('.');
                        for(i=0;i<BUFFER_MAX;i++)
                        {
                            printf("%02x",frameBuffer[i]);
                        }
                        printf("%02x",frameBuffer[CMD_INDEX]);
                        printf("
    ");
                        switch (frameBuffer[CMD_INDEX])
                        {
                        case WR_MEM:
                            //printf("<");
                            SourceAddr.byte.LB = frameBuffer[ADDRL_INDEX];
                            SourceAddr.byte.HB = frameBuffer[ADDRH_INDEX];
                            SourceAddr.byte.UB = frameBuffer[ADDRU_INDEX];
                            SourceAddr.byte.MB = frameBuffer[ADDRM_INDEX];
                            int ix;
                            for (ix=0;ix<frameBuffer[LEN_INDEX];)
                            {
                            pData.byte.LB = frameBuffer[LEN_INDEX+1+ix++];
                            pData.byte.HB = frameBuffer[LEN_INDEX+1+ix++];
                            pData.byte.UB = frameBuffer[LEN_INDEX+1+ix++];
                            pData.byte.MB = frameBuffer[LEN_INDEX+1+ix++];
                            NVMWriteWord(SourceAddr.Val, pData.Val);
                            SourceAddr.Val += 4;
                            }
                            break;
                         case WR_CFG:
                            break;
                         case ER_MEM:
                            for (EraseAddr.Val = ERASE_FLASH_BASE_ADDRESS; EraseAddr.Val < ERASE_FLASH_END_ADDRESS;)
                            {
                                NVMErasePage(EraseAddr.Val);
                                EraseAddr.Val += PAGE_SIZE;
                            }
                            break;
                         case RUN_APP:
                            GenUDPSt = SM_CLOSE;
                            break;
                         default:
                            break;
                        } 
                        startTime = TickGet();
                        if (GenUDPSt == SM_UDPGET) GenUDPSt = SM_RESPONCE;
                      }
                    }
                }
                else
                {  
                    GenUDPSt = SM_CLOSE;
                }
                break;
            case SM_RESPONCE:
                if (TickGet() - startTime < 10*TICK_SECOND)
                {
                    if (UDPIsPutReady(MySock) >= BUFFER_MAX)
                    {
                        UDPPutArray(frameBuffer, BUFFER_MAX);
                        UDPFlush();
                    }
                    startTime = TickGet();
                    GenUDPSt = SM_UDPGET;
                }
                else
                {
                    printf("xx");      // for debug
                    GenUDPSt = SM_CLOSE;
                }
                break;
            case SM_CLOSE:
                printf("done
    ");        // for debug
                UDPClose(MySock);
                MySock = INVALID_UDP_SOCKET;
                GenUDPSt = SM_HOME;
                TRISCSET = 0x2000; // RC13 Input
                LATCCLR = 0x6000;  // Clear LATC 
                LATHCLR = (1<<2);
                TRISHSET = (1<<2);
                INTDisableInterrupts();
                T1CON = 0;
                ETHCON1 = 0;
                ETHCON2 = 0;
                CoreT_DelayMs(1);
                U1MODE = 0x0;
                U1STA = 0x0;
                (*((void(*)(void))USER_APP_RESET_ADDRESS))();
                break;
            }

      如何使用PhnLoader_v3.0和PhnBoot_v3.0

          1. 使用XC32编译PhnBoot_v3.0(编译前,需先修改linker script)。

      2. 使用pickit3烧录PhnBoot_v3.0的Hex文件到目标板中。

      3. 拔除pickit3烧录器

      4. 连接目标板和PC到同一局域网,设置PC的IP地址,打开PhnLoader_v3.0,选择UDP协议,端口号,IP地址。

      5. 点击PhnLoader_v3.0用户界面上的“.."按钮加载需要烧录的应用程序Hex文件(编译前,需先修改linker script)

      6. 重启目标板,等到LED3 灯亮了,立刻在PhnLoader_v3.0界面上点击Download按钮。如果超时未点击Download按钮,会自动跳转到上次烧录的应用程序中去。

      7. 烧录完毕,再次重启目标板, 数秒后目标板开始正常运行应用程序。

      之后每次更新应用程序,只需重复步骤 4 ~ 7 就可以了。注意,整个烧录过程未能实现User ID/Configuration Bits的烧写,所以需要保持应用程序的Configuration Bits和Bootloader的Configuration Bits一致。

  • 相关阅读:
    mysql dbrd脑裂问题
    iOS 跳转至AppStore评分页面
    UIButton的titleLabe setAttributeSting 首次不起作用
    IOS ScrollView放大缩小点击位置并居中
    UIScrollView 性能优化
    iOS UIButton单双击处理响应不同的方法
    正则表达式过滤手机号
    iOS8通讯录之联系人增删查,多号码增删操作
    CoreData 添加新字段
    一张图教你搞定Mac App Store 应用安装包存储路径
  • 原文地址:https://www.cnblogs.com/geekygeek/p/pic32mz_udp_ethernetbootloader.html
Copyright © 2020-2023  润新知