Praming the PIC microcontroller's bootloader belongs to the kind of writer, which can write the BootLoader program that needs to be written according to your needs. Currently, a common Bootloader program has a PICC18 version of Microchip's C18 version and a famous PICC compiler manufacturer Hi-Tech (hereinafter referred to as Hi-Tech). The same is to occupy the system's 0x00 ~ 0x1ff program space. Microchip version is written in purely, and Hi-Tech version is written in C language. In the PIC series microcontroller, only the SM18 series and the PIC16F87x series have IAP functions to use the bootloader function. Only the BootLoader of PIC18 is introduced, and the BootLoader of PIC16F87x is similar to it. 1 BootLoader program works Bootloader is a boot program that runs before the user program before the single-chip / reset. After running, it is judged whether or not it needs to enter the upgrade state. If you don't need to upgrade, run directly in the original programs; if you need to upgrade, first erase the old program, then receive the user program from the serial port while writing to flash. When receiving data, you need to check, ensuring that the received data is correct, avoid writing the wrong program into the chip. The PIC18 microcontroller has only one flash, which can be erased anywhere in Flash (can even erase the bootloader itself); and 51 microcontrollers are generally two flash, only able to write another flash from a flash, and cannot be erased itself. The BootLoader program can be in principle anywhere in the entire program space, but for simple and convenient, genericity and minimize the impact on the user program, use a program space starting from 0x00. Bootloader program can get data in a variety of ways, including serial port, parallel port, I2C, SPI, USB, etc .; but from actual use, it is undoubtedly most convenient to use serial port. In order to make the user program get the maximum program space, the bootloader program is required as short as possible. Here, both Microchip and Hi-Tech are designed to be less than 200h bytes (100H). An important reason for using 0x00 ~ 0x1ff This area is that this address has a special write protection feature. Although the PIC18 microcontroller has multiple interrupt sources, only two interruptions are supported, and there are two interrupt vectors, which are located in 0x08 and 0x18; and the bootloader program takes up 0x00 ~ 0x1ff, which means that the interrupt vector is required to make the new The interrupt vector points to the user's interrupt program to ensure that the interrupt program of the user program is running. 2 Method for Upr Program (1) Hi-Tech's Bootloader program has a bootldr subfolder under the Examples folder of Hi-Tech's PICC18 compiler, which is the bootloader program of Hi-Tech. This is a complete program that can be compiled directly, and the compiled HEX can be downloaded to the chip with the programmer. (2) Requirements of user systems If you need to use bootloader, you can ask a RS232 serial port in the user system to communicate with your computer. (3) The configuration of the program is the main parameters of the program, and if they are set incorrect, it will affect the use of the program.
VERB0: Redundant mode, there are more tips, but will take up more program space, it is recommended to use it. Boot_timeout: Waiting for timeout, 0 ~ 9s (no meaning again), default is 5s. BAUD: The baud rate of serial port communication is 9 600. FOSC: The user system clock frequency, default is 4 MHz. NINE: Whether the communication uses the 9th bit of data bits by default. Fill_byte: The fill data used when the program space erase, the default value is 0xFF. PROG_START: The starting position of the user program, the default is 0x200. Other parameters can be used first, and they need to be modified as needed, they do not affect communication and download. (4) Compilation of the program has two methods compile the bootloader program: 1 command line mode. When using the command line, the typical usage is: PICC18 -8F452 bootldr.c--z-o er er o 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是 是Is the model of the specified microcontroller, if it is another model, modify the model used to actually use. 2 Use MPLAB IDE. When using the MPLAB IDE, first create a new project file in normal way, select the project file directory, then join the file bootldr.c and bootldr.h in the Hi-Tech Samples / BootLDR directory. When establishing an engineering document, select the model number of the MCU to actually use, select the C compiler as a PICC18 compiler of Hi-Tech. (5) The configuration of the user program uses the HI-Tech version of Bootloader, which is very small and simple to modify the user program, and only need to modify the offset of the user program, and do not need to modify any program code or any Special configuration. Because the bootloader program needs to occupy a space of 0 to 1ffh, the user program needs to start running from 200h, that is, the offset of the setup program is 200h. Method is to select Project → Build Options ... → Project from the menu, enter the offset 200 in the Specify Offset for ROM (ROM code offset) under the PICC-18 Linker page (note here The input is already hexadecimal, no reproduction), as shown in Figure 1. Figure 1 (6) Program Configuration For each PIC microcontroller chip, you need to set the correct program configuration word to function properly. Although in bootloader can modify the configuration word, this is not safe, it is inconvenient. In general, the program configuration word is set to be modified, so the configuration word writes the microcontroller chip together when you burn the bootloader program. After using bootloader to upgrade (burn) the program, this will not destroy the chip. The configuration word. (7) When debugging the user program, when writing and debugging the user program, or in normal ways, the single-chip does not include the bootloader program, nor setting offsets (or set to 0), to facilitate the use of ICD2 and other emulator Program simulation.
After the program is adjusted, the program's offset is modified as 200h, and recompile the program to generate the final user HEX code. This program can be downloaded using BootLoader. (8) Download the user program Use the Hi-Tech's bootloader program. When downloading the user program to the single-chip microcomputer, you can only need a special download program, you only need to use the super terminal program that comes with Windows. The communication parameters of the hyper end need to be set to the BootLoader program, including baud rate, check, data bit, stop bit, and the like. First connect the serial line, then start the super terminal program, then reset the microcontroller (the BootLoader program should have been written in the microcontroller). At this time, an countdown counter is displayed in the window of the hyper terminal, and the initial value of the counter is the Boot_Timeout parameter above. The counter is refreshed every second. When the counter is 0, the previous program will be run. If any data is input from the hyperh terminal during this period (that is, press one button, or the microcontroller receives any data from the serial port), the counter stops counting, enter the upgrade state. At this time, the bootloader program will first erase the old program space, and then a colon is displayed on the screen: ":" Tip Wait to download the user program. At this time, you can select the transfer from the hyperh terminal to send text files, select the compiled HEX file. If the download is successful, a small bracket ")" is displayed in the window of the hyper terminal, and the prompt download has been completed, and the user program will automatically start running. When using the bootloader download, because the speed of the serial port is relatively slow (compared to the programmer), you need to wait for a while. The specific time is related to the size of the user program. If an error occurs in the download, the single chip automatically resets, enters the countdown state, repeat the above process. Using the super terminal is relatively simple, there is no need special download software; however, there is no interactivity, no process indication, if there is an error in the download process, it cannot stop. 3 Bootloader Program Improvement (1) The defects in the bootloader program in Hi-Tech Although the Hi-Tech version of the BootLoader program is very convenient, all the necessary basic elements; but there are some defects in the program, even very Major hidden dangers are not suitable for direct use in actual engineering. Below is a major existing problem 1bootloader is to enter the bootloader status with any data to receive any data from the serial port. After entering the BootLoader state, the first thing to do by the bootloader program is to erase the space for previous programs. If in actual use, a single chip is reset because some unexpected cause is reset, and there is any data on the serial port when it runs bootloader after reset, which will cause loss of user programs. 2 There is no watchdog in the bootloader program. If the upgrade failed or the upgrade process, the program crash will not be able to return to the initial upgrade state. This is a fatal issue for direct serial connectivity. 3 If a data error occurs during the write user program, it will be reset, and the user program has been partially written. If the BootLoader program is run after the reset does not receive the signal, the user program is started. Such a hidden user program may result in running faults and unpredictable results. If the watchdog is open in the user program, there may be a phenomenon that the watchdog is not started and the crash, which is a serious problem in the remote upgrade. 4 The BootLoader program allows you to write EEPROM and chip configuration.
Although this increases flexibility, this is not safe. If the configuration word is not carefully set, the entire chip will not function properly after downloading. At this time, you need to reuse the programmer to modify the configuration word. Only by trying to overcome the defects mentioned above to apply the BootLoader program to the actual system. (2) Improvement of Hi-Tech's BootLoader program for the above mentioned questions, made some modifications to Hi-Tech's bootloader, delete some of the very few features and unsafe features, and modify the entered bootloader The condition of the status. In addition to using enhanced serial port data recognition, it also increases the method of level detection to determine whether to enter the BootLoader state. 1 For the issues in the first item, modify to identify a specific string to enter the bootloader upgrade state. The content and length of a particular string can be defined by the user (the length cannot exceed 12 bytes. Under normal circumstances, the 12-byte identification string should be long enough). Any data (including 0) can be used. 2 Increase a specific pin level judgment, after the bootloader program is run, it is determined whether the voltage on a particular pin is a predetermined voltage, thereby deciding whether to enter the BootLoader upgrade state. Pins and predetermined voltages (high / low) can be freely set. This approach is safe, but it is not suitable for remote upgrade. 3 Increase the watchdog option, you can set up / do not use the watchdog. Generally, the overflow time of the door dog is set at 0.5 to 2 s. 4 Added programming response. After each successful reception of HEX data, a response byte is issued for error detection when programming. The PC side download program can determine if the download process is wrong based on this byte. 5 For problems in 3 (1) above, you can solve this. Manually modify the compiled HEX file, the data of 0x200 ~ 0x220 (assuming the user program is starting from 0x200) address segment from the end of the file end. Such a code at 0x200 will be written in the microcontroller. If a fault occurs during the write, after the reset, even if the bootloader launches the user program, it will reset because there is no code in this area, and the user program that is written will not be run. However, this method needs to have a certain understanding of the structure of the HEX file. A simple judgment method of the HEX data address is that the first character of each line of the HEX file is a colon ":": "The four numbers after the colon will represent the address of this line of data, Hexadecimal expression. As: ": 100200 ..." means the address is 0x200. Under normal circumstances, the compiled HEX file data is arranged in the order of the address from low to high.
After the improved program adds a part of the parameters, they are: boot_signal - Using the single-chip lead level triggering to enter bootloader; boot_signal_port - define the pin trigger startup mode detection pin; boot_signal_level - definition detection Level 1 = High level trigger, 0 = low level trigger; boot_time_delay - Use timeout to enter bootloader, this parameter and the above boot_signal cannot be used at the same time; USE_EXTEND_HEX - Do not receive extended HEX code, no Use can save code, it is recommended to use; usewdt - Whether to use a watchdog in bootloader, it is recommended to use; confirm_time-online synchronization byte number, synchronize in the timeout mode; Defineable online data; Use_echoback - whether to respond when programming, use the security when downloading, is recommended. After using the boot_signal method, the part associated with the timeout method will no longer work. At this point, you can define any pin to determine if you need to enter the bootloader. When using boot_time_delay (timeout mode), some related content, such as confirm_data, you can use any specific string to confirm whether you need to enter the bootloader status, add the security of the bootloader program, avoid interference and mistake Program upgrade status. (3) Other improved advice and methods 1 Hi-Tech's BootLoader program is received by standard HEX files. This is not safe enough to have a lot of time, which is not conducive to the program's encryption, which is easy to be disassembled and cracked. The HEX can be encrypted and the data that cannot be directly viewed. 2 Use the super terminal to download the program slower, and if there is an error in the download, the hyper terminal program cannot be stopped in time, but always send the file to be done. At this time, the bootloader program will repeatedly enter the BootLoader state, and there is a certain damage to the single chip. It is best to write a computer-side dedicated download program, which can not only improve the speed of download, but also improve the success rate of downloading. 3 Use the RS422 / 485 way. Sometimes, it is not the RS232 serial port, but the RS422 / RS485 serial port. They are actually similar, just in the interface mode. RS422 / RS485 needs to be controlled, so adding a send control in the bootloader program. 4 In bootloader, the baud rate is set high and does not use too much place. It does not improve the speed of downloading the overall, but can only speed up the speed of data transmission. Because the entire download is divided into data communication (data transmission) and Flash write / erase (data waiting) two parts. The erasing and writing speed of the program code Flash space is slow (typ of 3 ~ 4 ms), it is too fast to lose data, resulting in download failure. When using the baud rate of 9600 BPS, the data transmitted by 1 byte is approximately 1 ms. The 8-byte of the buffer is received, which is more than 8 ms, which is greater than the write delay, so it does not need to be delayed; when the communication rate exceeds 9600 bps When receiving the time of the 8-byte buffer may be less than the write time, it is necessary to delay in the communication.