Research on Self-Compiled ISP Code for P89LPC922
[Copy link]
With the continuous expansion and deepening of the application field of microcontrollers, the requirements for their various performances are gradually increasing. At present, more and more types of microcontrollers have ISP (In System Programming) function. ISP programming does not require the microcontroller to be removed from the target system, and only a limited number of pins are required to connect to the outside world, which minimizes the additional component costs and circuit board area, making the development and specific application of microcontroller products quite convenient. This article will analyze the advantages and disadvantages of the preset ISP code of P89LPC922, and further combine the actual application situation to study the scheme of self-compiled ISP code, and give a specific implementation method, which has a strong practical value for the microcontroller system composed of Ps9LPC922. 1 Analysis of the P89LPC922 preset ISP code function The P89LPC922 is a single-chip packaged microcontroller produced by Philips. It uses a high-performance processor structure and integrates many system-level functions including ISP functions. It is suitable for many occasions that require high integration and low cost, and can meet many performance requirements. When the P69LPC922 leaves the factory, the upper 512 bytes of the user code space contain a serial ISP subroutine, allowing the device to be programmed through the serial port in the circuit. When applying the ISP function, it is necessary to use 5 pins of the chip (Vdd, Vss, TxD, RxD, RST), and a connector is required to connect to the computer serial port. In order to make the device enter the ISP state, 3 accurately timed low-level pulses must be given to the reset pin at power-on. The number of pulses can only be 3. Less or more than 3 pulses will cause the device to fail to enter the ISP mode. Another way to enter the ISP mode is to use the serial port interval detection function to reset the chip and force the device to enter the ISP mode. However, this method requires corresponding function settings in the user program, and the preset ISP code of P89LPC922 does not support this method. After entering the ISP mode, although the baud rate can be automatically set for the specific characters received, the setting parameter of this baud rate is only 1 byte, and the baud rate range is subject to certain restrictions. In addition, the ISP serial port works in mode 1, and only receives records in Intel-Hex format after the baud rate is set. From the above analysis, it can be seen that although the preset ISP function of P89LPC922 is relatively complete and can meet the application of the chip development stage, if the product is finalized, especially in the actual site where it is used as the lower computer in the networking system, it will be very inconvenient to update and download the user program code, and some functions cannot even be realized at all. This is mainly reflected in the following aspects: ◇It must be powered on to enter the ISP mode, which is not suitable for remote control; ◇The reset pin needs to be used, and the corresponding control timing is strict; ◇The baud rate range is limited; ◇There is no address option, and the system networking is not supported; ◇Only Intel-Hex format is supported, and the communication efficiency is low. 2 Self-written ISP code solution P89LPC922 has a very complete hardware system, which provides users with considerable freedom to write their own ISP code. Taking the 485 bus networking system composed of P89LPC922 as an example, the functional requirements of the self-compiled ISP code are given below: ◇The hardware does not use the reset pin, only the serial port two-wire connection is used; ◇The device can enter ISP mode at any time without powering on or off; ◇Automatically determine and set the communication baud rate, there should be ten baud rates; ◇The device has an address, and this address can be queried and modified at any time; ◇It can realize the functions of erasing, writing, verifying and encrypting user codes. Among them, the host computer of the 485 bus networking system is a computer, and the slave computer is P89LPC922. In order to achieve the above functions, the following problems must be solved: ◇How to allocate code storage area; ◇Planning and processing of chip addresses; ◇ Enter ISP method; ◇Automatically determine the baud rate method; ◇Determination of serial port working mode and communication protocol; ◇ISP code encryption and configuration word determination. 2.1 Allocation of code storage area In order not to affect the user's program code writing, the self-compiled ISP code is applied to the high end of the program storage area. Of course, different self-compiled ISP codes require different sizes of storage space. Here, the address space of the self-compiled ISP code is specified as 1COOH~lFFFH, a total of 1 KB, that is, the entire 7th sector. Among them, the ISP entry address is 1FOOH.
2.2 Planning and processing of chip addresses Since the program storage space of P89LPC922 can be used as a data storage device, the last 16 bytes of the 6th sector can be converted into a data space shared by ISP and user programs, with addresses of 1BFOH~1BFFU. Among them, address lBFAH stores the chip address, and address 1BFBH stores the inverted chip address. For example, if the chip address is 0lH, then (1BFAH)=01H, (1BFBH)=0FEH. The address is inverted and then stored in order to determine the legality of the address when the user program reads it out, thereby increasing reliability.
2.3 Method of entering ISP Due to functional requirements, the reset pin cannot be used to enter the ISP mode, so the boot state can only be set to 1 and the boot vector to 1FH. In this way, each time a power-on reset or other reset occurs, the program is fixed to start execution from the address 1F00H, that is, after the reset, the chip first enters the ISP mode and executes the ISP code. After the ISP process is completed or no ISP operation is performed within the specified time (such as 2s), the program will jump to address O to execute the user program code. Before jumping to address O, that is, before executing the user program code, the serial port interval detection function is set. In this way, when executing the user program code, the serial port interval detection function can be used to enable the chip to enter the ISP mode after reset. The following is the relevant program for setting the serial port interval detection function. ANL SSTAT, #OFAH; clear the BR bit, SSTAT. 2 MOV BRGCON, #00H; turn off the baud rate generator ORL AUXRl, #40H; set the EBRR bit, and reset when the UART interval signal is detected MOV SCON, #50H; set the serial port to work mode 1 (can also be mode 2 or mode 3), allowing the serial port to receive MOV BRGRl, #05H; set the serial port baud rate to 4800 (can also be other values) MOV BRGRO, #0fOH MOV BRGCON, #03H; turn on and use the serial port baud rate generator From the above program, it can be seen that the ISP code does not affect the user program's use of the serial port. The user program can reset the serial port working mode and baud rate, and the user program will not affect the use of the serial port silence detection function to enter the ISP mode. In actual use, the host computer sends a 0 signal for 0.5s, and then restores to a 1 signal, so that the serial port can detect the interval signal, so that the chip can enter the ISP code program after reset.
2.4 Automatic determination of baud rate After the lower computer (i.e. P89LPC922) is reset, within the specified time (such as 2s), the upper computer sends a 1-byte address frame to the lower computer according to the baud string set by the user. This address frame is 85 (hexadecimal 55H). The lower computer determines and sets its own baud rate based on this address frame. First, set timer Tl to mode 1, set the 16-bit counter and clear it. Then determine whether the receiving pin RXD is at a low level. If it is at a low level, continue to determine and wait for RXD to be at a high level. In fact, this process is to determine the start bit of the address frame sent by the upper computer, and it is also an anti-interference measure. Next, start timer Tl at the falling edge of RXD, and turn off timer T1 at the next falling edge of RXD. At this time, the value of the 16-bit counter of timer T1 is the time to send 2 bits of data at the current baud rate. The clock source of the fixed timer is the frequency-divided-by-two of the independent baud rate generator clock source, so the value of the 16-bit counter of timer T1 is the value of the baud rate generator rate registers BRGR1 and BRGR0 plus 16. Of course, there is a slight deviation between the value of the 16-bit counter obtained by this method and the value determined by the theoretical baud rate. In order to make the set baud rate exactly the same as the theoretical value, the theoretical value can be calculated according to the ten types of baud rates required by the function, and then compared with the value of the 16-bit counter, and then the theoretical value is written into registers BRGR1 and BRGR0, so that the set baud rate is 100% accurate.
2.5 Determination of the serial port working mode and communication protocol For the networked system, the serial port needs to be set to mode 3. Each communication between the upper computer and the lower computer is carried out in a "command-response" manner. The upper computer sends a "command" and the lower computer responds in a "response" manner. A single data frame consists of 11 bits and the format is as follows: 1 start bit + 8 data bits + 1 identification bit + 1 stop bit , where identification bit = 1 indicates that this frame is an address frame, and identification bit = 0 indicates that this frame is a data frame. At the beginning of communication, the lower computer is in the address frame receiving mode, and the upper computer sends a 1-byte address frame as a roll call packet command. The roll call address frame range, that is, the lower computer address range, is 1~255; after receiving the roll call packet, the lower computer compares it with its own address. If it matches, the address frame receiving mode is changed to a mode that can receive both address frames and data frames, and its own address is sent to the upper computer as a 1-byte data frame response. After receiving it, the upper computer determines that it is consistent with the sent address, and the upper computer begins to send various commands in data frame mode. The lower computer sends a command to communicate with the lower computer. After the lower computer enters the ISP state, if it receives an address frame that does not match its own address and the address is not equal to 0, it will go to address 0 to execute the user program code. If the address is equal to 0, the lower computer changes the address frame receiving mode to a mode that can receive both address frames and data frames, and sends its current address as a 1-byte data frame response to the upper computer, thereby completing the lower computer address query function. Note that when the upper computer sends the 0 address, only one lower computer can be connected. According to the functional requirements, the commands sent by the upper computer to the lower computer should include address modification, sector erasure, writing user program code, reading user program code, sector encryption, and program download completion. Among them, the upper computer executes the download completion command, and after the lower computer returns the download completion response, the lower computer goes to address 0 to execute the user program code. As for the command packet format, it can be written by the user. Here is a typical command packet format: packet identification + packet address + packet command + packet length + packet data + packet check. In addition, the ISP code file is recommended to be a binary format file, which can reduce the complexity of the corresponding program.
2.6 ISP code encryption and configuration word determination After the ISP code is written, it needs to be written into P89LPC922 by the programmer. Before programming, the relevant configuration words need to be set, including user configuration byte, user confidentiality byte, boot vector and boot status. Among them, the boot vector is 1FH and the boot status is 01H. In order to prevent the user from erasing or rewriting the ISP code, the 7th sector of the user confidentiality byte should be 07H, that is, the erasing and reading and writing operations of the ISP code are prohibited. The user confidentiality byte of other sectors can be OOH, that is, no confidentiality status is set, and it is freely set by the user through the sector encryption command of the communication protocol. The user configuration byte is 0A3H, that is, the watchdog reset function is enabled, the power-off detection is enabled, the reset pin is used as an input pin, and the internal RC oscillator is used. In summary, it is possible to fully utilize the perfect hardware resources of P89LPC922 to write ISP code programs suitable for specific application environments. This will bring great convenience to the actual use of this chip, especially when it is necessary to update and download user programs. Of course, if some encryption processing is added to the communication process between the upper computer and the lower computer in specific applications, the reliability and security of the entire self-compiled ISP code program will be enhanced, and the specific application system will also be more perfect.
|