采用UDP协议实现PIC18F97J60 ethernet bootloader
???????????????????? 了解更多關(guān)于bootloader 的C語言實(shí)現(xiàn),請加我QQ: 1273623966 (驗(yàn)證信息請?zhí)?bootloader),歡迎咨詢或定制bootloader(在線升級(jí)程序)。
TCP/IP Stack
使用pic18f97j60開發(fā)過多個(gè)項(xiàng)目,項(xiàng)目中都使用了Microchip免費(fèi)提供的TCP/IP Stack實(shí)現(xiàn)遠(yuǎn)程控制。但是每次更新程序,都需要將pic18f97j60目標(biāo)板取回來重新燒錄,很不方便。既然可以實(shí)現(xiàn)遠(yuǎn)程控制,為什么不實(shí)現(xiàn)遠(yuǎn)程更新呢?這就是我的ethernet bootloader的由來。Microchip的TCP/IP Stack功能很強(qiáng)大,我決定只用它的UDP模塊來實(shí)現(xiàn)。為了實(shí)現(xiàn)遠(yuǎn)程更新,我需要寫pic18f97j60單片機(jī)端UDP協(xié)議的ethernet bootloader程序--我將其命名為PhnBoot_v1.0; 同時(shí)還需要寫PC端與bootloader交互的UDP通信程序--我將其命名為PhnLoader_v1.0。我還定義了PhnBoot_v1.0和PhnLoader_v1.0之間傳輸數(shù)據(jù)的通信協(xié)定。
通信協(xié)定
單片機(jī)端PhnBoot_v1.0和PC端PhnLoader_v1.0之間的通信數(shù)據(jù)包采用以下協(xié)定
<STX><CMD><ADDRL><ADDRH><ADDRU><LEN><DATA>...<DATA><ETX>
定義如下:
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
PhnLoader_v1.0 功能
定義好了通訊協(xié)定, 接著就按照協(xié)定去實(shí)現(xiàn)PhnLoader_v1.0。 PhnLoader_v1.0的具體功能包括選擇IP地址,端口和協(xié)議類型,目前只支持UDP協(xié)議, 創(chuàng)建UDP服務(wù)器,加載應(yīng)用程序Hex文件,Parse 應(yīng)用程序的Hex文件,一行一行解讀Hex文件,一旦收到更新請求,立刻按照通訊協(xié)定采用UDP協(xié)議發(fā)送Hex記錄到單片機(jī),接收單片機(jī)發(fā)送回來的Response,發(fā)送完畢后斷開UDP通信,發(fā)送期間出現(xiàn)問題就立馬結(jié)束發(fā)送。
PhnLoader_v1.0 主要代碼段
PhnLoader_v1.0是用C#實(shí)現(xiàn)的,是我在利用空余時(shí)間自學(xué)C#后寫的,上面提到的功能都實(shí)現(xiàn)了。
private void btnDownload_Click(object sender, EventArgs e){btnDownload.Enabled = false;pBarLoading.Visible = false;if (!this.connect()){Debug.WriteLine("Udp server building unsuccessfully");textBoxStatus.ForeColor = Color.Red;textBoxStatus.AppendText("Udp server building unsuccessfully\r\n");textBoxStatus.ForeColor = Color.Black;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 unsuccessfully\r\n");textBoxStatus.ForeColor = Color.Black;loaderReader.Close();loaderServer.Close();btnDownload.Enabled = true;return;}loaderFrame = new SerialFrame();DateTime startTime = DateTime.Now;IPEndPoint clientPoint = new IPEndPoint(IPAddress.Any, 0);if (!loaderServer.Read(readyMsg,timeSpan)){Debug.WriteLine("Error: Timeout receive ready message from bootloader");textBoxStatus.ForeColor = Color.Red;textBoxStatus.AppendText("Timeout receive ready message from bootloader\r\n");textBoxStatus.ForeColor = Color.Black;loaderServer.Close();loaderReader.Close();btnDownload.Enabled = true;return;}if (!erase()){textBoxStatus.ForeColor = Color.Red;textBoxStatus.AppendText("Erase unsuccessfully\r\n");textBoxStatus.ForeColor = Color.Black;loaderReader.Close();loaderServer.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("\r\nDownloading hex file ...\r\n");try{while (loaderReader.Peek() >= 0){pBarLoading.PerformStep();recordLine = loaderReader.ReadLine();if (recordLine.Contains(EXTEND_TOKEN) == true){if (recordLine.Contains(USER_ID_TOKEN) == true){isNextLineUserID = true;continue;}else if (recordLine.Contains(CONFIG_BITS_TOKEN) == true){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;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(J_TYPE_CONFIG_BITS_1) == true) ||(recordLine.Contains(J_TYPE_CONFIG_BITS_2) == true) ||(recordLine.Contains(J_TYPE_CONFIG_BITS_3) == true) ||(recordLine.Contains(J_TYPE_CONFIG_BITS_4) == true) ||(recordLine.Contains(J_TYPE_CONFIG_BITS_5) == true) ||(recordLine.Contains(J_TYPE_CONFIG_BITS_6) == true) ||(recordLine.Contains(J_TYPE_CONFIG_BITS_TOKEN_1) == true) ||(recordLine.Contains(J_TYPE_CONFIG_BITS_TOKEN_2) == true)) &&(Address_U == 0x010000)){if (!DownloadConfigLine(recordLine)){Debug.WriteLine("Error found during configuration bits programming");loaderReader.Close();loaderServer.Close();btnDownload.Enabled = true;return;}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");loaderReader.Close();loaderServer.Close();btnDownload.Enabled = true;return;}isNextLineConfigBits = false;}else{if (!DownloadDataLine(recordLine)){Debug.WriteLine("Error found during data programming");loaderReader.Close();loaderServer.Close();btnDownload.Enabled = true;return;}}}}catch (Exception ex){Debug.WriteLine("Error: " + ex.Message);textBoxStatus.ForeColor = Color.Red;textBoxStatus.AppendText("Downloading failed\r\n");textBoxStatus.ForeColor = Color.Black;loaderServer.Close();loaderReader.Close();btnDownload.Enabled = true;return;}textBoxStatus.AppendText("Downloading completed\r\n");if (!run()){textBoxStatus.ForeColor = Color.Red;textBoxStatus.AppendText("Jump to Application unsuccessfully\r\n");textBoxStatus.ForeColor = Color.Black;loaderReader.Close();loaderServer.Close();btnDownload.Enabled = true;return;}loaderServer.Close();loaderReader.Close();btnDownload.Enabled = true;} View CodePhnLoader_v1.0 用戶界面
PhnBoot_v1.0 功能
在PhnLoader_v1.0完成后,接著就是完成PhnBoot_v1.0。 PhnBoot_v1.0主要功能就是使用Microchip的TCP/IP Stack建立UDP Client,發(fā)送更新應(yīng)用程序請求,接收PhnLoader_v1.0傳送過來的Hex記錄。解讀Hex記錄中的啟始位,命名,地址,數(shù)據(jù)和結(jié)束位,將數(shù)據(jù)燒錄到指定的程序存儲(chǔ)器的位置上,然后通過ethernet返回Response消息給PC端PhnLoader_v1.0。
PhnBoot_v1.0 位置
PhnBoot_v1.0放置在程序存儲(chǔ)器的頭部,大小為0x2400程序字。
Interrupt Vector Remap
由于PhnBoot_v1.0位于程序存儲(chǔ)器的頭部,需要對Interrupt Vector進(jìn)行remap. 代碼如下。
#define REMAPPED_APP_HIGH_INTERRUPT_VECTOR 0x2408#define REMAPPED_APP_LOW_INTERRUPT_VECTOR 0x2418#pragma code low_vector_section=0x018void low_vector (void){_asmgoto REMAPPED_APP_LOW_INTERRUPT_VECTOR_endasm}#pragma code high_vector_section=0x08void high_vector (void){_asmgoto REMAPPED_APP_HIGH_INTERRUPT_VECTOR_endasm}PhnBoot_v1.0 主要代碼段
PhnBoot_v1.0 是用C語言寫的,Microchip 8-bit C Compiler--MCC18編譯的。
switch (GenUDPSt){case SM_HOME:ARPResolve(&Server.IPAddr);if (ARPIsResolved(&Server.IPAddr,&Server.MACAddr)){MySock = UDPOpen(ClientPort,&Server,ServerPort);if (MySock != INVALID_UDP_SOCKET){tick = 0x10000;delay = BOOT_TIMEOUT;GenUDPSt++;}}else{tick--;if (tick==0){tick = 0x10000;if (delay == 0){delay = BOOT_TIMEOUT;GenUDPSt = SM_CLOSE;}delay--;}}break;case SM_READY:if (UDPIsPutReady(MySock) > BUFFER_MAX){UDPPutString(ok);UDPFlush();GenUDPSt++;}else{tick--;if (tick==0){tick = 0x10000;if (delay == 0){delay = BOOT_TIMEOUT;GenUDPSt = SM_CLOSE;}delay--;}}break;case SM_RESPONSE:udpPackets = UDPIsGetReady(MySock);if (udpPackets >= BUFFER_MAX){UDPGetArray(line_buffer, BUFFER_MAX);UDPDiscard();if (line_buffer[0] == STX && line_buffer[BUFFER_MAX - 1] == ETX){switch (line_buffer[CMD_INDEX]){case WR_MEM:EECON1 = PGM_WRITE;WriteMem();break;case WR_CFG:if (!last_block_written&&!CFG_NUM){WriteStart();last_block_written = 1;ResetBlockBuffer();}CFG_NUM++;EECON1 = CFG_WRITE;WriteCfg();break;case ER_MEM:EECON1 = PGM_ERASE;EraseMem();break;case RUN_APP:if (!last_block_written){WriteStart();last_block_written = 1;ResetBlockBuffer();}GenUDPSt++;default:break;}if (UDPIsPutReady(MySock) >= BUFFER_MAX){UDPPutArray(line_buffer, BUFFER_MAX);UDPFlush();}}}else{tick--;if (tick==0){tick = 0x10000;if (delay == 0){delay = BOOT_TIMEOUT;GenUDPSt = SM_CLOSE;}delay--;}}break;case SM_CLOSE:while (!TXSTAbits.TRMT);TXREG='>';UDPClose(MySock);MySock = INVALID_UDP_SOCKET;_asmgoto APP_START_endasmbreak;}}如何使用
1. 使用MCC18編譯PhnBoot_v1.0,
2. 使用pickit3燒錄PhnBoot_v1.0的Hex文件到目標(biāo)板中。
3. 拔除pickit3燒錄器
4. 將目標(biāo)板與PC的接入同一局域網(wǎng),并設(shè)置PC的IP地址和目標(biāo)板的IP地址為同一網(wǎng)域,打開PhnLoader_v1.0用戶界面,選擇IP, 端口,和通信協(xié)議。
5. 點(diǎn)擊PhnLoader_v1.0用戶界面上的“.."按鈕加載需要燒錄的應(yīng)用程序Hex文件 (注意:由于PhnBoot_v1.0占用了程序存儲(chǔ)器頭部0x2400程序字,所以應(yīng)用程序編譯需要設(shè)置Code offset為0x2400)。
6. 重啟目標(biāo)板,接著立刻在PhnLoader_v1.0界面上點(diǎn)擊Download按鈕。如果超時(shí)未點(diǎn)擊Download按鈕,目標(biāo)板會(huì)自動(dòng)跳轉(zhuǎn)到上次燒錄的應(yīng)用程序中去。
7. 燒錄完畢,再次重啟目標(biāo)板, 2秒后目標(biāo)板開始正常運(yùn)行應(yīng)用程序。
之后每次更新應(yīng)用程序,只需重復(fù)步驟 4 ~ 7 就可以了。
主要特性
本PIC ethernet bootloader有以下主要特性
1. 使用了Microchip免費(fèi)的TCP/IP Stack,采用UDP協(xié)議。
2. C語言寫的,MCC18 編譯。
3. 非常容易移植。
4. 支持FLASH燒寫, 快速,占用空間小。
5. 可支持EEPROM燒寫。
6. 支持CONFIG BITS/IDLOC 燒寫。
?
如果你有什么疑問,或有興趣了解更多關(guān)于bootloader 的C語言實(shí)現(xiàn),請加我QQ:?1273623966 (驗(yàn)證信息請?zhí)?strong> bootloader 或 cnblogs)。
posted on 2015-10-17 18:22 GeekyGeek 閱讀(...) 評論(...) 編輯 收藏轉(zhuǎn)載于:https://www.cnblogs.com/geekygeek/p/pic_udp_bootloader.html
總結(jié)
以上是生活随笔為你收集整理的采用UDP协议实现PIC18F97J60 ethernet bootloader的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU2102 A计划
- 下一篇: 平铺式顺序结构