当前位置:首页 > 单片机 > 单片机
[导读]  新PIC16Bootloader  在完成HyperBootloader之后(具体详见我之前的随笔),我决定重写PIC bootloader。为什么呢? HyperBootloader是由PC端的串口通信软件--超级终端来传送Hex数据的,一行一行地传送,每传送一

  新PIC16Bootloader

  在完成HyperBootloader之后(具体详见我之前的随笔),我决定重写PIC bootloader。为什么呢? HyperBootloader是由PC端的串口通信软件--超级终端来传送Hex数据的,一行一行地传送,每传送一行Delay 20ms,以等待Hyperbootloader烧录完。因为这样效率有些低,所以我决定自己写PC端的串口通信程序和PIC bootloader,为了提高效率还定义了PC端串口通信程序和PIC单片机端bootloader之间的通信协定。首先我重写PIC16 bootloader, 我要完成PIC16单片机端bootloader程序--我命其名为PhsBoot_v1.0; 我还要完成与其协同工作的PC端串口通信程序--我命其名为PhsLoader_v1.0, 为此,我花了三个月的空闲时间,自学了C#。

  通信协定

  PIC16单片机端PhsBoot_v1.0和PC端PhsLoader_v1.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 24 bits ( ADDRL , ADDRH , ADDRH)

  具体有以下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_v1.0 功能

  定义好了通讯协定, 接着就按照协定去实现PhsLoader_v1.0。 PhsLoader_v1.0的具体功能包括选择COM端口和BAUD RATE, 连接COM, 加载应用程序Hex文件,Parse 应用程序的Hex文件,一行一行解读Hex文件,然后按照通讯协定通过串口发送Hex记录到单片机,接收单片机发送回来的Response,发送完毕后断开COM连接,发送期间出现问题就立马结束发送。

  PhsLoader_v1.0 主要代码段

  PhsLoader_v1.0是用C#实现的,由于是我在利用空余时间自学C#后写的,上面提到的功能都实现了,但肯定有可以提高的地方,欢迎赐教。以下是主要的代码段。


privatevoidbtnDownload_Click(objectsender,EventArgse){btnDownload.Enabled=false;pBarLoading.Visible=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;Address_U=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){isNextLineConfigBits=true;continue;}else{constintADDR_U_START_INDEX=9;constintADDR_U_LENGTH=4;stringaddrU=recordLine.Substring(ADDR_U_START_INDEX,ADDR_U_LENGTH);Address_U=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;}isNextLineConfigBits=false;}else{//if(recordLine.Contains(J_TYPE_CONFIG_BITS_TOKEN)==true&&Address_U==0x10000)//{//continue;//}/*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;}

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭