采用TCP协议的PIC32MZ ethernet bootloader
了解更多关于bootloader 的C语言实现,请加我Q扣: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序)。
趁热打铁,在上一PIC32MZ UDP ethernet bootloader的基础上我又完成了采用TCP协议的PIC32MZ ethernet bootloader。两款ethernet bootloader基本架构都一样,数据包格式也一样。TCP ethernet bootloader 也分两部分,我将他们命名为PhnLoader_v4.0和PhnBoot_v4.0。
PhnLoader_v4.0
PhnLoader_v4.0 是PC端host 程序。
运行环境:Window + .NET Framework 4.5
开发语言:C#
通信类型:TCP Server
IP 地址: 192.168.1.105
端口号: 8720
UI 如下:
主要代码如下:
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; } 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\r\n"); textBoxStatus.ForeColor = Color.Black; loaderServer.Close(); loaderReader.Close(); btnDownload.Enabled = true; return; } textBoxStatus.AppendText("Downloading completed\r\n");
PhnBoot_v4.0
PhnBoot_v4.0 是单片机端Boot 程序。
运行环境:PIC32MZ EC Starter Kit
开发语言:C 语言 (Microchip XC32 free)
通信类型:TCP Client
IP 地址: 192.168.1.123
端口号: 随机生成
主要代码段:
switch (GenTCPSt) { case SM_HOME: ARPResolve(&ServerNode.IPAddr); startTime = TickGet(); GenTCPSt++; break; case SM_ARPed: if (TickGet() - startTime < 4*TICK_SECOND) { if (ARPIsResolved(&ServerNode.IPAddr,&ServerNode.MACAddr)) { MySock = TCPOpen((DWORD)&ServerNode,TCP_OPEN_NODE_INFO,REMOTE_SERVER_PORT,0); startTime = TickGet(); GenTCPSt++; } } else { GenTCPSt = SM_HOME; } break; case SM_OPENed: if (TickGet() - startTime < TICK_SECOND) { if (MySock != INVALID_SOCKET) { startTime = TickGet(); GenTCPSt++; } } else { GenTCPSt = SM_HOME; } break; case SM_TCPPUT: if (TickGet() - startTime < 4*TICK_SECOND) { if (TCPIsConnected(MySock)) { TCPPutString(MySock,ok); TCPFlush(MySock); GenTCPSt++; } } else { GenTCPSt = SM_CLOSE; } break; case SM_TCPGET: if (TickGet() - startTime < 10*TICK_SECOND) { tcpPackets = TCPIsGetReady(MySock); if (tcpPackets >= BUFFER_MAX) { TCPGetArray(MySock,frameBuffer,BUFFER_MAX); if (!TCPIsGetReady(MySock)) TCPDiscard(MySock); //take care of packet splicing if (frameBuffer[0] == STX && frameBuffer[BUFFER_MAX - 1] == ETX) { // for(i=0;i<BUFFER_MAX;i++) // { // printf("%02x",frameBuffer[i]); // } // printf("\r\n"); 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: GenTCPSt = SM_CLOSE; break; default: break; } startTime = TickGet(); if (GenTCPSt == SM_TCPGET) GenTCPSt = SM_RESPONCE; } } } else { GenTCPSt = SM_CLOSE; } break; case SM_RESPONCE: if (TickGet() - startTime < 10*TICK_SECOND) { if (TCPIsPutReady(MySock) >= BUFFER_MAX) { Uart_Putc('.'); TCPPutArray(MySock, frameBuffer, BUFFER_MAX); TCPFlush(MySock); } startTime = TickGet(); GenTCPSt = SM_TCPGET; } else { printf("xx"); // for debug GenTCPSt = SM_CLOSE; } break; case SM_CLOSE: printf("done\r\n"); // for debug TCPDisconnect(MySock); MySock = INVALID_SOCKET; GenTCPSt = SM_HOME; TRISCSET = 0x2000; // RC13 Input LATCCLR = 0x6000; // Clear LATC LATHCLR = (1<<2); TRISHSET = (1<<2); INTDisableInterrupts(); T1CON = 0; IEC0CLR = _IEC0_T1IE_MASK; ETHCON1 = 0; ETHCON2 = 0; CoreT_DelayMs(1); U1MODE = 0x0; U1STA = 0x0; (*((void(*)(void))USER_APP_RESET_ADDRESS))(); break; } }
如何使用PhnLoader_v4.0和PhnBoot_v4.0
1. 使用XC32编译PhnBoot_v4.0(编译前,需先修改linker script, 需要设置Optimization level为1)。
2. 使用pickit3烧录PhnBoot_v4.0的Hex文件到目标板中。
3. 拔除pickit3烧录器
4. 连接目标板和PC到同一局域网,设置PC的IP地址,打开PhnLoader_v4.0,选择TCP协议,端口号,IP地址。
5. 点击PhnLoader_v4.0用户界面上的“.."按钮加载需要烧录的应用程序Hex文件(编译前,需先修改linker script)
6. 重启目标板,等到LED3 灯亮了,立刻在PhnLoader_v4.0界面上点击Download按钮。如果超时未点击Download按钮,会自动跳转到上次烧录的应用程序中去。
7. 烧录完毕,再次重启目标板, 数秒后目标板开始正常运行应用程序。
之后每次更新应用程序,只需重复步骤 4 ~ 7 就可以了。注意,整个烧录过程未能实现User ID/Configuration Bits的烧写,所以需要保持应用程序的Configuration Bits和Bootloader的Configuration Bits一致。