当前位置:网站首页>Bootloader implementation of PIC MCU

Bootloader implementation of PIC MCU

2022-07-05 07:59:00 feiyingzaishi

Reprinted from :http://blog.sina.com.cn/s/blog_77b8b43b0102v9ut.html

lead said  

  PIC Single chip microcomputer Of BootLoader It belongs to the kind that needs to write its own program , You can write various functions according to your own needs BootLoader The program to . at present , More common BootLoader The program has Microchip The company's C18 Version and famous PICC Compiler manufacturer HI-TECH company ( hereinafter referred to as HI-TECH) Of PICC18 edition . They are similar in that they all occupy Systematic 0x00~0x1FF Program space . Microchip The version of is written in Pure Exchange , and HI-TECH The version of is using C language-written . 

   stay PIC Series MCU , Only PIC18 Series and PIC16F87X A series of single chip computers IAP function , To be able to use BootLoader function . I'll just introduce PIC18 Of BootLoader,PIC16F87X Of BootLoader Similar to it . 

1 BootLoader How the program works  

  BootLoader Is a boot program , Power on the MCU / After reset, run before the user program . After running, judge whether it is necessary to enter the upgrade state . If you don't need to upgrade , Just run Flash Original program in ; If you need to upgrade , First erase the old program , Then receive the user program from the serial port , Write at the same time Flash in . When receiving data , Verification is required , Ensure that the received data is correct , Avoid writing the wrong program into the chip . 

  PIC18 There is only one single chip microcomputer Flash, Can be in Flash Erase anywhere in ( You can even put BootLoader Self erasure ); and 51 Single chip computers are generally two Flash, Only from one piece Flash Write another piece on it Flash, Instead of erasing yourself . 

  BootLoader In principle, a program can be placed anywhere in the whole program space , But for simplicity and convenience 、 It has versatility and minimizes the impact on user programs , Used from 0x00 The beginning of a program space . 

  BootLoader Programs can obtain data in many ways , Including serial port 、 In parallel 、I2C、SPI、USB etc. ; But from the practical point of view , Using serial port is undoubtedly the most convenient . 

   In order to make the user program get the maximum program space , Need BootLoader The program should be as short as possible . ad locum ,Microchip and HI-TECH All will BootLoader The program is designed to be less than 200H Bytes (100H A word ). Use 0x00~0x1FF Another important reason for this area is , This address space has a special write protection feature . 

  PIC18 Although the single chip microcomputer has multiple interrupt sources , But only two-level interrupts are supported , There are two interrupt vectors , They are located in 0x08 and 0x18; and BootLoader The program takes up 0x00~0x1FF Space , This means that you need to reposition the interrupt vector , Make the new interrupt vector point to the user's interrupt program , Only in this way can the interrupt program of the user program run normally . 

2 How to use the program  

  (1) HI-TECH Of BootLoader Program  

   stay HI-TECH Of PICC18 compiler Of examples Under the folder , There is one Bootldr A folder , It's inside HI-TECH Of BootLoader Program . This is a complete program , You can compile directly , The compiled HEX It can be downloaded to the chip with a programmer

  (2) Requirements of user system  

   If needed BootLoader, The user is required to have one in the system RS232 Serial port can communicate with computer . 

  (3) The configuration of the program  

   Here are the main parameters of the program , If they are not set correctly , Will affect the use of the program . 

  VERB0: Redundancy mode , There are more tips , But it will take up more program space , It is not recommended that . 
  BOOT_TIMEOUT: Waiting time out ,0~9s( No matter how long it is ), The default is 5s. 
  BAUD: Baud rate of serial communication , The default is 9 600. 
  FOSC: Clock frequency of user system , The default is 4 MHz. 
  NINE: Whether the communication uses the 9 Digit bit , Default not to use . 
  FILL_BYTE: Filling data used in program space erasure , The default value is 0xFF. 
  PROG_START: The starting position of the user program , The default is 0x200
. 
   Other parameters can be ignored , Modify it when necessary , They do not affect communication and downloading . 

  (4) Compilation of program  

   There are two ways to BootLoader The program compiles : 

  ① Command line mode . When using the command line , The typical usage is : 
  picc18 -8f452 bootldr.c -o -zg -noerrata 
   The last parameter above -noerrata yes PIC18Fxx2 Required by series single chip microcomputer , For other models , You don't have to join ; And the parameters -18f452 It is the model of the designated single chip microcomputer , If it's another model , Change it to the actual model . 


  ② Use MPLAB IDE. Use MPLAB IDE when , First, create a new project file in the normal way , Select the project file directory , Then join HI-TECH Of Samples\Bootldr Files under directory bootldr.c and bootldr.h. When establishing engineering documents , choice MCU The model of is the actual model , choice C Compiler is HI-TECH Of PICC18 compiler . 

  (5) Configuration of user program  

   Use HI-TECH Version of BootLoader, The modification of user program is very few and simple , Just modify the offset of the user program , There is no need to modify any program code or make any special configuration . because BootLoader The program needs to occupy 0~1FFH Space , So the user program needs to start from 200H Start running at , That is a Set the offset of the program to 200H. The method is in MPLAB IDE in , Select from the menu Project→Build Options...→Project, stay PICC-18 Linker Under the page mark Specify offset for ROM(ROM Code offset ) Enter the offset in 200( Note that the input here is already hexadecimal , There's no need to switch ), Pictured 1 Shown . 

     
                      chart 1 

  (6) Program configuration word  

   For each PIC Single chip microcomputer Chip , You need to set the correct program configuration word before it can run normally . Although in BootLoader The configuration word can be modified in , But it's not safe , It's not convenient . In general , After the program configuration word is set, it does not need to be modified , therefore Configuration word is burning BootLoader The program is written into the single chip microcomputer chip together , In the future, only use BootLoader To upgrade ( burning ) Program , This will not damage the configuration word of the chip . 

  (7) Debug user program  

   At ordinary times , When writing and debugging user programs , It's still the same as the normal way , SCM does not contain BootLoader Program , Also do not set the offset ( Or set to 0), For ease of use ICD2 Wait for the simulator to simulate the program . After the program is debugged , Then modify the offset of the program to 200H, And recompile the program , Generate end users HEX Code . Only in this way can the compiled program be used BootLoader Download . 

  (8) Download user program  
     
   Use HI-TECH Of BootLoader Program , When downloading user program to MCU , You don't need a special download program , Just use Windows The self-contained HyperTerminal program is ok . The communication parameters of the super terminal need to be set to and BootLoader The procedure is the same , Including baud rate 、 check 、 Data bits 、 Stop bit, etc . 

   First, connect the serial port cable , Then start the HyperTerminal program , Then reset the MCU ( SCM should have written BootLoader Program ). At this time , A countdown counter will be displayed in the window of the HyperTerminal , The initial value of the counter is the above BOOT_TIMEOUT Parameters . The counter refreshes every second . When the counter is 0 when , Will run the previous program . If any data is input from the HyperTerminal during this period ( Just press any key , Or the single chip microcomputer receives any data from the serial port ), The counter will stop counting , Enter upgrade state . At this time BootLoader The program will first erase the old program space , Then a colon will appear on the screen “:”, Prompt to wait for the user program to download . At this time, you can select transfer from the menu of the HyperTerminal → Send a text file , Choose the compiled HEX File can . If the download is successful , A small bracket will be displayed in the window of the HyperTerminal “)”, Prompt that the download is complete , At the same time, the user program will automatically start running . Use BootLoader When the download , Because the speed of serial port is relatively slow ( Compared with programmer ), So you need to wait for a while . The specific time is related to the size of the user program . If there is an error in the download , The single chip microcomputer will reset automatically , Enter countdown state , Repeat the process above . Using HyperTerminal is relatively simple , No special download software is required ; But lack of interaction , No process instructions , If there are errors in the download process, you can't stop . 

3 BootLoader Program improvement  

  (1) HI-TECH Of BootLoader Defects in the program  

   although HI-TECH Version of BootLoader The program is already very convenient , Have all the necessary basic elements ; But there are some defects in the program , Even a major hidden danger , It is not suitable for direct use in practical projects . Here are the main problems  

  ①BootLoader It is marked by receiving any data from the serial port BootLoader State of . Get into BootLoader Post state ,BootLoader The first thing a program does is erase the space of the previous program . If in actual use , The single chip microcomputer is reset for some unexpected reason , And run after reset BootLoader When there is any data on the serial port ( For example, the interference signal or the system is in the state of serial port communication ), It will cause the loss of user programs .

  ② BootLoader The watchdog is not used in the program . If the upgrade fails or the program crashes during the upgrade , Will not be able to return to the original upgrade state . This is not a big problem for the direct serial connection , But using remote upgrade is a fatal problem . 
  ③ If data error occurs during writing user program , It will reset , At this time, the user program has been partially written . If it runs after reset BootLoader When the program does not receive a signal , Will start the user program . In this way, incomplete user programs may cause running failures and unpredictable results . If the watchdog is opened in the user program , At this time, the watchdog may crash without being started , This is a serious problem in remote upgrade . 
   
   ④ BootLoader Program It is allowed to write EEPROM And chip configuration word . Although this increases flexibility , But it's not safe . If the configuration word is accidentally set incorrectly , After downloading, the whole chip will not work properly . At this time, you need to use the programmer to modify the configuration word . 

   Only try to overcome the defects mentioned above , Only then BootLoader The program is applied to the actual system . 

  (2) Yes HI-TECH Of BootLoader Program improvement  

   In response to the above questions , Yes HI-TECH Of BootLoader Some modifications have been made , Removed some rarely used functions and unsafe functions , At the same time, the entry BootLoader Condition for judging the state . In addition to using enhanced serial port data recognition , The method of level detection is also added , Used to determine whether to enter BootLoader state . 

   ① For the problem in the first item above , Modify it to recognize a specific string before entering BootLoader Upgrade status of . The content and length of a specific string can be defined by the user ( The length cannot exceed 12 byte . In general ,12 The identification string of bytes should be long enough ). You can use any data ( Include 0). 

   ② Added specific pin level judgment , stay BootLoader After program running , Judge whether the voltage on a particular pin is the predetermined voltage , This determines whether it is necessary to enter BootLoader Upgrade status . Pin and predetermined voltage ( high / low ) You can set it up freely . This way is safer , But it is not suitable for remote upgrade . 

   ③ Added watchdog option , Can be set to use / Don't use a watchdog . Generally, the overflow time of the watchdog is set at 0.5~2s More appropriate . 

   ④ Added programming response . One line is successfully received at each HEX After the data , Send a response byte , Used for error detection during programming .PC The download program of the client can judge whether there is an error in the download process according to this byte . 

   ⑤ For the above 3(1) in ③ The problem of , It can be solved in this way . Will be compiled HEX The file is slightly modified manually , take 0x200~0x220( Suppose the user program is from 0x200 At the beginning ) The data of the address segment moves from the beginning of the file to the end of the file . such 0x200 The code at will be written into the MCU at the end . If a failure occurs during writing , Even if BootLoader Started the user program , It will also reset because there is no code in this area , Instead of running a partially written user program . But this method needs to be right HEX Only with a certain understanding of the structure of the document . A simple judgment HEX The method of data address is ,HEX The first character on each line of the file is a colon “:”, The... After the colon 3、4、5、6 These four numbers represent the address of this line of data , It is expressed in hexadecimal . Such as :“:100200...” It means the address is 0x200. Under normal circumstances , The compiled HEX The file data is arranged in the order of addresses from low to high . 

   The improved program adds some parameters , They are : 
  BOOT_SIGNAL—— Use the MCU pin level trigger mode to enter BootLoader; 
  BOOT_SIGNAL_PORT—— Define the pin used to detect the level trigger starting mode ; 
  BOOT_SIGNAL_LEVEL—— Define the detection level 1= High level trigger ,0= Low level trigger ; 
  BOOT_TIME_DELAY—— Use timeout mode to enter BootLoader, This parameter and the above BOOT_SIGNAL Not at the same time  Use ; 
  USE_EXTEND_HEX—— Whether to receive extended HEX Code , Not using it can save code , It is not recommended that ; 
  USEWDT—— Whether in BootLoader Use a watchdog in , It is recommended to use ; 
  CONFIRM_TIME—— Number of bytes synchronized online , Use multiple bytes for synchronization in timeout mode ; 
  CONFIRM_DATA—— User definable online data ; 
  USE_ECHOBACK—— Whether to respond when programming , Use can increase the security of downloading , It is recommended to use .

   Used BOOT_SIGNAL After the way , The parts related to timeout mode no longer work . At this time, it can be defined to use any pin to determine whether it needs to enter BootLoader. In the use of BOOT_TIME_DELAY( Timeout mode ) when , Added some related content , Such as CONFIRM_DATA, You can use any specific string to confirm whether you need to enter BootLoader state , Added BootLoader The security of the program , Avoid being disturbed and entering the program upgrade state by mistake . 

  (3) Other suggestions and methods for improvement  

   ① HI-TECH Of BootLoader The program receives standard HEX file . This is not safe enough in many cases , It is not conducive to the encryption of the program , Easy to disassemble and crack . It can be done to HEX Encrypt , Become data that cannot be viewed directly . 

   ② Using HyperTerminal to download programs is slow , meanwhile , If there is an error in the download , The HyperTerminal program cannot be found and stopped in time , Instead, you can't stop until you send the file . At this time BootLoader The program will repeatedly enter BootLoader state , There is certain damage to the single chip microcomputer . It is best to write a special download program on the computer side by yourself , It can not only improve the download speed , It can also improve the success rate of downloading . 

   ③ Use RS422/485 The way . Sometimes , It's not that RS232 A serial port , It is RS422/RS485 A serial port . They are actually similar , There are only some differences in the interface mode .RS422/RS485 Need to control sending , So in BootLoader Add a sending control to the program . 

   ④ stay BootLoader in , Setting the baud rate very high is not very useful , It does not improve the overall download speed , And can only speed up the speed of data transmission . Because the whole download is divided into data communication ( The data transfer ) and Flash write in / erase ( Data waiting ) Two parts . Program code Flash The erasing and writing speed of space is relatively slow ( The typical value is 3~4 ms), It's easy to lose data if it's too fast , Cause download failure . Use 9600 bps The baud rate of , transmission 1 The data of bytes is about 1 ms, Receive a buffer 8 Bytes need about 8 ms, Greater than the write delay , So there is no need to delay ; When the communication rate exceeds 9600 bps when , receive 8 The time of byte buffer may be less than the write time , Need to delay in communication . The actual test results are : Use 9600 bps Than using 14 400 bps Time slow 50%, Use 57 600 bps Than 9600 bps Double the speed , While using 115 200 bps Time and 57 600 bps There is hardly any difference . If you use HyperTerminal to download , There is no need to set a high baud rate . Because when using the most commonly used three wire communication ( No control signal ), The super terminal adopts a more conservative way to send data , Itself is relatively slow . 
  
Conclusion  

   A good BootLoader The program should have good maintainability and can handle exceptions correctly , It will not cause damage and collapse of the system due to accidents . 

   Can be in http://shaoziyang.logchina.com/blog/article_156363.936124.html Download the improved BootLoader Program ( This program has been used in practical projects for a long time , Very stable ), And a replacement HyperTerminal PIC18 Dedicated download program ( The download speed of this program is much faster than that of HyperTerminal , You can define baud rate and online string by yourself , have BootLoader Zone code protection function and download error detection function ). 

                  reference  

1 Microchip.PIC18F1220/1320/2320/6620/6621 Data manual  
2 Microchip Application instructions AN851:A Flash Bootloader for PIC16 and PIC18 Devices 
3 HI-TECH Of PICC18(ver8.35pl2) The software BootLoader Program code and program description  

 

原网站

版权声明
本文为[feiyingzaishi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140548093762.html