C語言PIC32 serial bootloader和C#語言bootloader PC端串口通信程序
今天介紹下我新完成的為Microchip的32位單片機(jī)PIC32MZ2048ECH144開發(fā)的UART bootloader程序。整個(gè)工程分兩部分,第一部分是單片機(jī)端用XC32編譯的bootloader程序PhsBoot_v5.0,另一部分是PC端用C#編譯的bootloader通訊程序PhsLoader_v5.0。兩者之間采用固定的協(xié)定通信合作,如下。
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 (最終版本未實(shí)現(xiàn))
RD_MEM: 0x01 -- Read Program Memory (最終版本未實(shí)現(xiàn))
ER_MEM: 0x03 -- Erase Program Memory
WR_MEM: 0x02 -- Write Program Memory
WR_CFG: 0x04 -- Write Configuration Registers (最終版本未實(shí)現(xiàn))
PhsLoader_v5.0
PhsLoader_v5.0是PC端的程序,負(fù)責(zé)打開串口,加載應(yīng)用程序hex文件,并按照上面介紹的協(xié)定進(jìn)行組包然后發(fā)送出去。PhsLoader_v5.0是用C#開發(fā)的包含UI的程序,UI如下圖。
PhsLoader_v5.0 的主要實(shí)現(xiàn)代碼如下
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;}