C语言PIC32 serial bootloader和C#语言bootloader PC端串口通信程序
扫描二维码
随时随地手机看文章
今天介绍下我新完成的为Microchip的32位单片机PIC32MZ2048ECH144开发的UART bootloader程序。整个工程分两部分,第一部分是单片机端用XC32编译的bootloader程序PhsBoot_v5.0,另一部分是PC端用C#编译的bootloader通讯程序PhsLoader_v5.0。两者之间采用固定的协定通信合作,如下。
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 (最终版本未实现)
PhsLoader_v5.0
PhsLoader_v5.0是PC端的程序,负责打开串口,加载应用程序hex文件,并按照上面介绍的协定进行组包然后发送出去。PhsLoader_v5.0是用C#开发的包含UI的程序,UI如下图。
PhsLoader_v5.0 的主要实现代码如下
privatevoidbtnLoad_Click(objectsender,EventArgse){btnDownload.Enabled=false;textBoxStatus.AppendText("rnLoadinghexfile...rn");OpenFileDialogopenDialog=newOpenFileDialog();openDialog.Filter="Hexfiles(*.hex)|*.hex";if(openDialog.ShowDialog()==DialogResult.OK){textBoxFile.Text=openDialog.FileName;string[]pathArray=textBoxFile.Text.Split('\');intpathDeep=pathArray.Count();textBoxStatus.AppendText("Hexfile:"+pathArray[pathDeep-1]+"rn");textBoxStatus.AppendText("Loadingcompletedrn");HexParseloaderParse=newHexParse();textBoxStatus.AppendText("Parsinghexfile...rn");if(loaderParse.IsHexFile(textBoxFile.Text)){btnDownload.Enabled=true;loaderLines=loaderParse.HexLines;textBoxStatus.AppendText("Parsingcompletedrn");}else{textBoxStatus.ForeColor=Color.Red;textBoxStatus.AppendText("Parsingfailedrn");textBoxStatus.ForeColor=Color.Black;}}else{textBoxStatus.ForeColor=Color.Red;textBoxStatus.AppendText("Loadingfailedrnrn");textBoxStatus.ForeColor=Color.Black;}}privatevoidbtnDownload_Click(objectsender,EventArgse){btnDownload.Enabled=false;if(!this.connect()){btnDownload.Enabled=true;return;}try{loaderReader=newStreamReader(textBoxFile.Text);}catch(Exceptionex){Debug.WriteLine("Error:"+ex.Message);textBoxStatus.ForeColor=Color.Red;textBoxStatus.AppendText("Readhexfileunsuccessfullyrn");textBoxStatus.ForeColor=Color.Black;loaderReader.Close();loaderSerial.Close();btnDownload.Enabled=true;return;}loaderFrame=newSerialFrame();if(!erase()){textBoxStatus.ForeColor=Color.Red;textBoxStatus.AppendText("Eraseunsuccessfullyrn");textBoxStatus.ForeColor=Color.Black;loaderReader.Close();loaderSerial.Close();btnDownload.Enabled=true;return;}pBarLoading.Refresh();pBarLoading.Visible=true;pBarLoading.Value=0;pBarLoading.Maximum=loaderLines;pBarLoading.Step=1;stringrecordLine;loaderUpperAddr=0;boolisNextLineUserID=false;boolisNextLineConfigBits=false;textBoxStatus.AppendText("rnDownloadinghexfile...rn");try{while(loaderReader.Peek()>=0){pBarLoading.PerformStep();recordLine=loaderReader.ReadLine();//if(recordLine.Contains(USER_ID_TOKEN)==true)//{//isNextLineUserID=true;//continue;//}//elseif(recordLine.Contains(CONFIG_BITS_TOKEN)==true)//{//isNextLineConfigBits=true;//continue;//}if(recordLine.Contains(EXTEND_TOKEN)==true){if(recordLine.Contains(USER_ID_TOKEN)==true){isNextLineUserID=true;continue;}elseif(recordLine.Contains(CONFIG_BITS_TOKEN)==true){constintADDR_U_START_INDEX=9;constintADDR_U_LENGTH=4;stringaddrU=recordLine.Substring(ADDR_U_START_INDEX,ADDR_U_LENGTH);loaderUpperAddr=Convert.ToInt32(addrU,16)<<16;isNextLineConfigBits=true;continue;}elseif(recordLine.Contains(DSPIC_CONFIG_BITS_TOKEN)==true){constintADDR_U_START_INDEX=9;constintADDR_U_LENGTH=4;stringaddrU=recordLine.Substring(ADDR_U_START_INDEX,ADDR_U_LENGTH);loaderUpperAddr=Convert.ToInt32(addrU,16)<<16;isNextLineConfigBits=true;continue;}elseif(recordLine.Contains(PIC32_CONFIG_BITS_TOKEN)==true){constintADDR_U_START_INDEX=9;constintADDR_U_LENGTH=4;stringaddrU=recordLine.Substring(ADDR_U_START_INDEX,ADDR_U_LENGTH);loaderUpperAddr=Convert.ToInt32(addrU,16)<<16;isNextLineConfigBits=true;continue;}else{constintADDR_U_START_INDEX=9;constintADDR_U_LENGTH=4;stringaddrU=recordLine.Substring(ADDR_U_START_INDEX,ADDR_U_LENGTH);loaderUpperAddr=Convert.ToInt32(addrU,16)<<16;continue;}}elseif(recordLine.Contains(END_OF_HEX_FILE_TOKEN)==true){break;}if(isNextLineUserID){isNextLineUserID=false;//donothing;}elseif(isNextLineConfigBits){//if(!DownloadConfigLine(recordLine))//{//Debug.WriteLine("Errorfoundduringconfigurationbitsprogramming");//loaderReader.Close();//loaderSerial.Close();//btnDownload.Enabled=true;//return;//}DownloadConfigLine(recordLine);isNextLineConfigBits=false;}else{if(!DownloadDataLine(recordLine)){Debug.WriteLine("Errorfoundduringdataprogramming");loaderReader.Close();loaderSerial.Close();btnDownload.Enabled=true;return;}//DownloadDataLine(recordLine);}}}catch(Exceptionex){Debug.WriteLine("Error:"+ex.Message);textBoxStatus.ForeColor=Color.Red;textBoxStatus.AppendText("Downloadingfailedrn");textBoxStatus.ForeColor=Color.Black;loaderSerial.Close();loaderReader.Close();btnDownload.Enabled=true;return;}textBoxStatus.AppendText("Downloadingcompletedrn");if(!run()){textBoxStatus.ForeColor=Color.Red;textBoxStatus.AppendText("JumptoApplicationunsuccessfullyrn");textBoxStatus.ForeColor=Color.Black;loaderReader.Close();loaderSerial.Close();btnDownload.Enabled=true;return;}loaderSerial.Close();loaderReader.Close();btnDownload.Enabled=true;}