C语言PIC18 serial bootloader和C#语言bootloader PC端串口通信程序
扫描二维码
随时随地手机看文章
新PIC18 Bootloader
PhsBoot_v3.0是我最新用C语言实现的PIC bootloader, 采用串口通信,适用于PIC18, 并为其用C#写了PC端通信程序PhsLoader_v3.0。PhsLoader_v3.0通过串口按照自定义的通信协定发送数据PhsBoot_v3.0, PhsBoot_v3.0接收数据,按照通信协定解读数据,解读出其中Hex数据,并将其烧录到正确的位置。
通信协定
PIC18单片机端PhsBoot_v3.0和PC端PhsLoader_v3.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_v3.0 功能
定义好了通讯协定, 接着就按照协定去实现PhsLoader_v3.0。 PhsLoader_v3.0的具体功能包括选择COM端口和BAUD RATE, 连接COM, 加载应用程序Hex文件,Parse 应用程序的Hex文件,一行一行解读Hex文件,然后按照通讯协定通过串口发送Hex记录到单片机,接收单片机发送回来的Response,发送完毕后断开COM连接,发送期间出现问题就立马结束发送。
PhsLoader_v3.0 主要代码段
PhsLoader_v3.0是用C#实现的,是我在利用空余时间自学C#后写的,上面提到的功能都实现了。
private void btnDownload_Click(object sender, EventArgs e)
{
btnDownload.Enabled = false;
pBarLoading.Visible = false;
if (!this.connect())
{
btnDownload.Enabled = true;
return;
}
try
{
loaderReader = new StreamReader(textBoxFile.Text);
}
catch (Exception ex)
{
Debug.WriteLine("Error: " + ex.Message);
textBoxStatus.ForeColor = Color.Red;
textBoxStatus.AppendText("Read hex file unsuccessfullyrn");
textBoxStatus.ForeColor = Color.Black;
loaderReader.Close();
loaderSerial.Close();
btnDownload.Enabled = true;
return;
}
loaderFrame = new SerialFrame();
if (!erase())
{
textBoxStatus.ForeColor = Color.Red;
textBoxStatus.AppendText("Erase unsuccessfullyrn");
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;
string recordLine;
Address_U = 0;
bool isNextLineUserID = false;
bool isNextLineConfigBits = false;
textBoxStatus.AppendText("rnDownloading hex file ...rn");
try
{
while (loaderReader.Peek() >= 0)
{
pBarLoading.PerformStep();
recordLine = loaderReader.ReadLine();
//if (recordLine.Contains(USER_ID_TOKEN) == true)
//{
// isNextLineUserID = true;
// continue;
//}
//else if (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;
}
else if (recordLine.Contains(CONFIG_BITS_TOKEN) == true)
{
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);
Address_U = 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");