C語言dsPIC / PIC24 serial bootloader和C#語言bootloader PC端串口通信程
新dsPIC/PIC24Bootloader
PhsBoot_v4.0是我最新用C語言實現的PIC bootloader, 采用串口通信,適用于dsPIC30, dsPIC33和PIC24, 并為其用C#寫了PC端通信程序PhsLoader_v4.0。PhsLoader_v4.0通過串口按照自定義的通信協定發(fā)送數據PhsBoot_v4.0, PhsBoot_v4.0接收數據,按照通信協定解讀數據,解讀出其中Hex數據,并將其燒錄到正確的位置。
通信協定
dsPIC/PIC24單片機端PhsBoot_v4.0和PC端PhsLoader_v4.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_v4.0 功能
定義好了通訊協定, 接著就按照協定去實現PhsLoader_v4.0。 PhsLoader_v4.0的具體功能包括選擇COM端口和BAUD RATE, 連接COM, 加載應用程序Hex文件,Parse 應用程序的Hex文件,一行一行解讀Hex文件,然后按照通訊協定通過串口發(fā)送Hex記錄到單片機,接收單片機發(fā)送回來的Response,發(fā)送完畢后斷開COM連接,發(fā)送期間出現問題就立馬結束發(fā)送。
PhsLoader_v4.0 主要代碼段
PhsLoader_v4.0是用C#實現的,是我在利用空余時間自學C#后寫的,上面提到的功能都實現了。
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;}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;}}}}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;}