C语言PIC16 serial bootloader和C#语言bootloader PC端串口通信程序

        了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序)。

  新PIC16 Bootloader

  在完成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><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

 

  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#后写的,上面提到的功能都实现了,但肯定有可以提高的地方,欢迎赐教。以下是主要的代码段。

        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 unsuccessfully\r\n");
                textBoxStatus.ForeColor = Color.Black;
                loaderReader.Close();
                loaderSerial.Close();
                btnDownload.Enabled = true;
                return;
            }

            loaderFrame = new SerialFrame();
            //if (!erase())
            //{
            //    textBoxStatus.ForeColor = Color.Red;
            //    textBoxStatus.AppendText("Erase unsuccessfully\r\n");
            //    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("\r\nDownloading hex file ...\r\n");
            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");
                            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("Error found during data programming");
                            loaderReader.Close();
                            loaderSerial.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;
                loaderSerial.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();
                loaderSerial.Close();
                btnDownload.Enabled = true;
                return;
            }
            loaderSerial.Close();
            loaderReader.Close();
            btnDownload.Enabled = true;
        }
View Code

  PhsLoader_v1.0 用户界面

  

  PhsBoot_v1.0 功能

  在PhsLoader_v1.0完成后,接着就是完成PhsBoot_v1.0。 PhsBoot_v1.0主要功能就是接收PhsLoader_v1.0传送过来的Hex记录。解读Hex记录中的启始位,命名,地址,数据和结束位,将数据烧录到指定的程序存储器的位置上,然后通过串口返回Response消息给PC端PhsLoader_v1.0。

  PhsBoot_v1.0 位置

  PhsBoot_v1.0放置在程序存储器的底部,大小为0x100程序字,编译时需设置Code offset参数为ROM_SIZE - 0x100, 例如ROM SIZE为0x2000 PIC16单片机,Code offset需设置为0x1F00 (0x2000 - 0x100)。

  

  PhsBoot_v1.0 主要代码段

  PhsBoot_v1.0 是用C语言写的,Microchip 8-bit C Compiler--XC8编译的。

    while (1)
    {
        if (PIR1bits.RCIF == 1)
        {
            RecivedByte = RCREG;
            PIR1bits.RCIF == 0;
            m_buffer[m_buffer_Index++] = RecivedByte; //receive data

            if (m_buffer_Index >= BUFFER_MAX)
            {
                if (m_buffer[0] == STX && RecivedByte == ETX)
                { //get complete cmd
                    switch (m_buffer[CMD_INDEX])
                    {
                    case WR_MEM:
                        EECON1 = PGM_WRITE;
                        WriteMem();
                        break;
                    case RUN_APP:
                        sendResponse();
                        //TXSTA = 0x02;           // reset TXSTA RCSTA before jumping to application
                        //RCSTA = 0x00;
                        #asm
                        ljmp BOOT_START
                        #endasm
                        break;
                    default:
                        //sendResponse();
                        break;
                    }
                }
                else
                { //Send data error back
                    TXREG = '?';
                    while (TXSTAbits.TRMT == 0); //wait empty
                }
                m_buffer_Index=0;
            }
        }
    }

  如何使用 

  1. 使用XC8编译PhsBoot_v1.0, 由于PhsBoot_v1.0将放在程序存储器底部,占0x100程序字,编译前需将Code Offset编译参数设到正确值。例如,某PIC16 单片机的程序存储器空间为0x2000程序字,Code Offset = 0x2000 - 0x100 = 0x1F00, 所以只需设置Code offset为1F00, 然后编译。

  2. 使用pickit3烧录PhsBoot_v1.0的Hex文件到目标板中。

  3. 拔除pickit3烧录器

  4. 连接目标板与PC的串口,打开PhsLoader_v1.0用户界面,选择COM端口,BAUD RATE。

  5. 点击PhsLoader_v1.0用户界面上的“.."按钮加载需要烧录的应用程序Hex文件。

  6. 重启目标板,接着立刻在PhsLoader_v1.0界面上点击Download按钮。如果超时未点击Download按钮,目标板会自动跳转到上次烧录的应用程序中去。

  7. 烧录完毕,再次重启目标板, 2秒后目标板开始正常运行应用程序。

  之后每次更新应用程序,只需重复步骤 4 ~ 7 就可以了。

  主要特性

  新的PIC16 serial bootloader有以下主要特性

  1. C语言写的,XC8 编译(只有一点汇编在里面)。

  2. 非常容易移植。

  3. 支持FLASH烧写, 快速,占用空间小。

  4. 可支持EEPROM烧写。

  5. 不支持CONFIG BITS/IDLOC 烧写,保持应用程序的Configuration Bits和Bootloader的一致。

 

  如果你有什么疑问,或有兴趣了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 验证信息请填bootloader 或 cnblogs)

 

  若需了解我的上一款PIC16 串口bootloader 请阅读随笔《自己用C语言写PIC16单片机的serial bootloader》

posted on 2015-10-07 12:38 GeekyGeek 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/geekygeek/p/pic16_serial_bootloader.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值