The STC microcontroller contains two Flash blocks. The program running in one Flash can erase and reprogram the other Flash. Generally, the ISP program is stored in a Flash with a smaller capacity (Block1), and the user code is stored in a Flash with a larger capacity (Block0). The ISP program in Block1 erases and reprograms the user code in Block0. Friends who are doing STC microcontroller decryption or using it know that there is a section of about 3K code after the program storage area of the STC microcontroller. This code is the ISP program of the STC microcontroller. There is information about ISP in the data sheet of the STC microcontroller. Since this code can be upgraded, we can be sure that this area can be rewritten. Haha, these involve the technology of soft decryption of STC microcontrollers. I won’t say more. You can use your own wisdom.
The following is some information for designing your own STC ISP protocol. Many application systems require program code upgrades. If the program code is in an external Flash memory, the program code upgrade can be implemented by directly operating the external Flash. However, for some system program codes inside the microcontroller, the microcontroller is required to support the IAP (In-Application-Programming) function. This article introduces the ISP (In-System-Programming) programming method in this case, as well as its specific implementation on SST and STC microcontrollers.
1 Basic structure of ISP implementation
There are many ways to implement ISP, but they generally follow the process shown in Figure 1.
Figure 1
Among them, there are generally the following ways to judge the user ISP selection.
(1) Connect to the computer system and select by the system command
When entering the user ISP selection judgment, the microcontroller first sends specific feature data, and then waits for the command data. If the selection command sent by the computer system is received within a certain period of time, it will enter the user code upgrade, otherwise it will jump directly to the user code execution.
(2) Select by the jumper on the user board
Generally, a code upgrade selection jumper is designed using the spare port of the microcontroller. When entering the user ISP selection judgment, the microcontroller can directly determine whether to enter the user code upgrade or directly jump to the user code execution based on the status of this port.
(3) Select by the user board operation function
In the function menu or function combination of the user board, the user is allowed to select the code upgrade function, and at the same time, the corresponding flag is stored in the external memory. When the code upgrade function is selected, specific data is written in the external memory, and then the program is reset. When entering the user ISP selection judgment, the data in the external memory is judged. If it meets the conditions, it will enter the user code upgrade, otherwise it will jump directly to the user code execution.
2 IAP Program
Design At present, many microcontrollers support IAP function. Generally, these microcontrollers contain two Flash blocks. The program running in one Flash can erase and reprogram the other Flash. Generally, the ISP program is stored in a Flash with smaller capacity (Block1), and the user code is stored in a Flash with larger capacity (Block0). The ISP program in Block1 erases and reprograms the user code in Block0.
The following introduces the IAP program design by taking two 51 series compatible Flash microcontrollers as examples. One is SST89C54 and the other is STC89C516RD.
(1) SST89C54 related features
SST89C54 has 20KB (16KB+4KB) program memory inside, with unified addressing. Block0 is 0000H~3FFFH; Block1 is F000H~FFFFH. Block1 can be selected to map to the 1KB/2KB/4KB program area starting from address 0000H.
(2) STC89C516RD related features
SST89C516RD has 72KB (64KB+8KB) program memory. Block0 is 0000H~FFFFH, and Block1 can be mapped to the 8KB program area starting from address 0000H (power-on reset defaults to address mapping).
The IAP operation of SST and STC microcontrollers is almost exactly the same. The C program source code IAP.C of the IAP function is given on the website of this journal (www.dpj.com.cn). It should be noted that the selection of block0 in the Block0_erase function is different for the two microcontrollers (just the opposite).
3 Switching from ISP program to user code
In design, the ISP program is generally designed as a program that runs after power-on reset. If the user code does not need to be upgraded or after the upgrade is completed, the program must be switched to user code execution. The switching from ISP program to user code varies from microcontroller to microcontroller.
(1) The switch from SST89C54 program area Block1 to Block0 is realized
when the SST89C54 microcontroller is burned, the ISP program is written to Block1, and the burn mapping selection bit RB0/RB1 (RE-MAP[1:0]) is set. In this way, when the program is powered on and reset, Block1 is automatically mapped to the 4KB program area starting at address 0000H, and the ISP program is executed. Since Block1 is also allocated at address F000H~FFFFH, when compiling and generating the ISP program code, all address ranges are set to F000H~FFFFH. When it is necessary to switch to the user code (Block0) to run, Modify the SFCF[7] control bit VIS, cancel the 0000H address mapping of Block1, and then jump to address 0000H to execute, and then start running the user code program in Block0. The
ISP C program source code ISP.C is given on the website of this magazine. It should be noted that this program needs to create a project file in Keil-C, including
IAP.C function and STARTUP.A51, and the definition of STC should be removed in IAP.C and ISP.C. In order to set the address range to F000H~FFFFH, the program entry address in STARTUP.A51 should be changed from 0 to 0F000H, as follows:
CSEG AT 0F000H
C_STARTUP: LJMP STARTUP1.
You also need to modify the compilation options in the Target tab to set Off-chip Code memory: Start
= 0xF000; Size = 0x1000; and set the C51 tab to set Interrupt Vectors at address: 0xF000.
(2) Switching from Block1 to Block0 in the STC89C516RD program area
When the STC89C516RD microcontroller is burned, the ISP program is written to Block1. (Note: the SC0/SC1 bit is not burned). When the microcontroller is powered on and reset, the default Block1 is mapped to the 8KB program area starting at address 0000H and enters the ISP program execution. When it is necessary to switch to the user code (Block0) to run, the ISP modifies the SFCF[1] control bit SWR to generate a soft reset (Software Reset). Since SC0 and SC1 are not burned, after the program is soft reset, Block1 will no longer be mapped to the address 0000H, and the user code program in Block0 will start to run.
The ISP C program source code ISP.C is given on the website of this magazine. It should be noted that this program needs to create a project file in Keil-C, including the IAP.C function, and the definition of STC must be retained in IAP.C and ISP.C.
4 Communication protocol with computer
When upgrading user code, it is necessary to communicate with the computer. RS232 serial communication is generally used, and the data protocol adopts a simple protocol. This protocol refers to the ISP data protocol of the ADuC812 microcontroller. (ADuC812 microcontroller hardware has a built-in ISP program).
(1) Reset command (computer → microcontroller)
The computer sends a 4-byte reset command: 21H, 5AH, 00H, A6H, and the microcontroller returns reset information.
(2) Reset information (microcontroller → computer)
The reset information is 25 bytes, the first 3 bytes are the microcontroller company's characteristic characters (such as: "ADI", "SST", "STC"), and the last 1 byte is the checksum.
(3) Data packet format (computer → microcontroller)
The computer sends a data packet format: 07H, 0EH, length, data, checksum (length and data checksum).
(4) Erase
command The computer sends a data packet, which contains only 1 byte of data and the content is: character "A" or "C". After erasing the user program area, the microcontroller returns 1 byte 06H to indicate success; 05H indicates failure.
(5) Programming command
The computer sends a data packet, which contains: "W", 00H, address high byte, address low byte, program data. The microcontroller returns 1 byte 06H to indicate success; 05H indicates failure.
(6) Run user program
The computer sends a data packet, which contains only 1 byte of data and the content is: character "U". The microcontroller returns 1 byte 06H to indicate success, and then jumps to the user program to run.
This magazine website provides the C program source code Download.C for computer download software.
Conclusion
Based on the above ISP program design ideas and examples, you can modify the ISP initial code, or enrich its ISP functions (read function, password control, etc.), and try to design your own ISP program, which will definitely add a lot to your system.
Previous article:Design and implementation of DC ripple tester based on STC89C52 single chip microcomputer
Next article:Design of power-free electronic lock system using STCl2C2052
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications