Application program upgrade method based on dsPIC33F series microcontroller

Publisher:JFETLatest update time:2011-04-13 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

0 Introduction
The dsPIC33F series microcontroller is a high-performance microcontroller launched by Microchip. It seamlessly integrates the high-speed computing power of the digital signal processor (DSP) with the control characteristics of the microcontroller, providing a cost-effective single-chip, single-instruction stream solution for embedded system design. It has been widely used in audio processing, video processing and industrial control. However, in actual applications, due to changes in customer needs or program bug modifications, it is often necessary to upgrade the microcontroller application. This article uses the Run-Time Serf-Programming (RTSP) feature of the dsPIC33F microcontroller to implement the upgrade function based on the microcontroller application.

1 Basic Principles
The dsPIC33F series microcontroller provides two ways to program its internal program memory: In-Circuit Serial Programming (ICSP) and Run-Time Self-Programming (RTSP). ICSP uses 5 wires to program the microcontroller, which is mainly used in product debugging or production process. It is obviously not suitable for program upgrades during product operation. RTSP is completed by using TBLRD (table read) and TBLWT (table write) instructions. Using RTSP, a block (or "row") of 64 instructions (192 B) or a single program storage word can be written to the program memory at one time, and a block (or "page") of 512 instructions (1 536 B) can be erased at one time. Since this method uses program instructions to achieve self-programming, it can be applied to program upgrades during product operation. Therefore, this article uses the RTSP method to implement the application program upgrade function based on the dsPIC33F series microcontroller. The essence of program upgrade is to update the content of the program memory, so it is necessary to first understand the internal structure of the program memory of the dsPIC33F microcontroller.
1.1 Internal structure of the program memory of the dsPIC33F microcontroller
The program memory structure of the dsPIC33F microcontroller is shown in Figure 1. In general, the content of "reset address" is all 0, and after the microcontroller is reset, it starts to execute from the address 0 of the program memory, that is, from the "GOTO instruction" position in Figure 1. A jump instruction is stored here, which directly points to the first address of the user program. By default, the instruction at this location is "GOTO 0x200", that is, the default jump to the user program with the first address of 0x200 (the "user program storage space" in Figure 1) to start execution. The dsPIC33F microcontroller has two interrupt vector tables, namely the "interrupt vector table" and the "alternate interrupt vector table", with addresses from 0x000004 to 0x0000FF and 0x000100 to 0x0001FF respectively. These two interrupt vector tables allow different interrupt service routines to handle each interrupt source. The program upgrade function is realized based on the above characteristics of the internal structure of the program memory.


1.2 Implementation process of program upgrade function
The "user program storage space" in Figure 1 is divided into two parts: the boot program and the application program, as shown in Figure 2(a). The boot program is used to guide the user program to enter the upgrade state or the normal operation state. If it enters the upgrade state, it receives new program data and updates the content of the program memory; if it enters the normal operation state, the program jumps to the application program area. The application program is a program written to realize the product function, which is the object of "program upgrade" in this article.
In actual application, the values ​​of certain storage units of the external non-volatile memory (such as E2PROM) are used as a flag for whether to perform program upgrade (hereinafter referred to as program upgrade flag). After the microcontroller is powered on or reset, the "GOTO0x400" instruction is first executed, that is, jumping to the boot program. By default, the program upgrade flag is in the "normal operation" state. The boot program calls the jump instruction to guide the user program to the application program area. If a new application version is found on the server during operation or an upgrade program command is received from the server, the microcontroller sets the program upgrade flag to the "program upgrade" state, and then the software resets the microcontroller. At this time, the program jumps to the boot program again. According to the program upgrade flag, the microcontroller enters the program upgrade state, receives the new program, and updates the content of the program memory. After the upgrade is completed, the microcontroller sets the program upgrade flag to the "normal operation" state, and then resets the software again. At this time, the microcontroller runs in the new application.
Since both the boot program and the application may use interrupts, the application uses the interrupt vector table and the boot program uses the spare interrupt vector table to execute their respective interrupt service subroutines. It is worth noting that in Figure 2 (a), the first address of the boot program is 0x000400. This is because the dsPIC33F microcontroller will erase 512 instruction spaces (i.e., 1 "block", occupying 1024 address units) when executing an erase instruction. This means that the program upgrade must be in units of 1024 (i.e., 0x400). Similarly, the first address of the application must be an integer multiple of 0x400. This article uses 0x00C800.
From the analysis of the above basic principles, it can be seen that the implementation of the program upgrade function is mainly the implementation of the boot program.

2 Implementation of the boot program
2.1 Program flow
After entering the boot program, the execution flow of the program is as follows:
(1) Initialize the circuit board, including the operating frequency of the microcontroller, peripheral interfaces, etc., and pay special attention to enabling the "alternate interrupt vector table" of the microcontroller. Because the "interrupt vector table" is enabled by default after the dsPIC33F microcontroller is reset, and it can be seen from the description of the basic principle that the boot program must use the "alternate interrupt vector table".
(2) Determine the direction of the program, and use the program upgrade flag to determine whether to jump to the application or execute the program upgrade process.
(3) If the program upgrade flag is "normal operation", call the GOTO instruction to jump to the application.
(4) If the program upgrade flag is "program upgrade", execute the program upgrade process, which is the focus of the boot program.
The program upgrade is updated by "block", and each "block" contains 1 024 storage units (in words), that is, each frame of data must contain 2 048 bytes of program code. When updating the program, it is not simply to overwrite the old program with the received new program code. Some special frames need special processing. This can be well understood by comparing the program memory structure after the application is compiled with the program memory structure in the actual application. Figure 2 (a) shows the internal structure of the program memory in the actual application; (b) shows the program memory structure after the application is compiled. By comparing these two figures, it can be seen that the programs in the intervals of "GOTO instruction", "spare interrupt vector table" and "boot program" cannot be directly overwritten. If the "GOTO instruction" is overwritten, the program will jump directly to the application after the program is upgraded, and the boot program will not be executed; if the "spare interrupt vector table" is overwritten, the interrupt service program in the boot program cannot be executed; if the content of the "boot program" interval is overwritten, the boot program will be cleared, which will obviously bring disastrous consequences. Therefore, the first frame data needs special processing. This frame data corresponds to the content of the first block of the program memory (address 0~0x3FF), including the content of "GOTO instruction", "reset address", "interrupt vector table" and "backup interrupt vector table". In order to ensure that the content of "GOTO instruction" and "backup interrupt vector table" is not modified, it is necessary to read out the original "GOTO instruction" and "backup interrupt vector table" first, replace the content of the corresponding position in the receiving data buffer, and then write it into the first block of the program storage area; for the second frame to the 50th frame (0xe800/0x400), these are the contents of the boot program, and obviously no modification can be made, so these frames of data are directly discarded after receiving; from the 51st frame to the last frame, this is the content of the application program, and it is also the content that really needs to be upgraded, so the old content can be directly overwritten. After the program is upgraded, the program update flag is restored to the "normal operation" state, and then the CPU is reset, and the new application program is entered through the boot program, thereby realizing the upgrade of the application program.
Based on the above description, the detailed process of the boot program is shown in Figure 3.


2.2 Implementation of key details
(1) Enable the "alternate interrupt vector table". Set the value of the ALTIVT control bit in the INTCON2 register. If its value is set to "1", the "alternate interrupt vector table" is enabled; if it is set to "0", the "interrupt vector table" is enabled.
(2) Setting the program's first address. In the description of the above process, it is mentioned that the first address of the boot program is 0x400, and the first address of the application is 0xc800. The setting of the program's first address can be achieved by modifying the link description file (.gld file). Taking the dsPIC33FJ256GP710 microcontroller as an example, change the boot program's first address to 0x400. The implementation steps are as follows: Open p33FJ256GP710. gld file, change "program(xr): ORIGIN=0x200, LENGTH=0x2AA00" to "pro-gram(xr): ORIGIN=0x400, LENGTH=0x2A900"; change "_ _CODE_ BASE=0x200" to "_ _CODE_BASE=0x400". The same method is used to modify the application's first address.
(3) Read/write program memory. The references provide a very complete explanation of the principle and complete source code that can be used directly. (
4) Software reset of the CPU. When it is found that the program needs to be upgraded during the running of the application or when the upgrade of the program is completed in the boot program, the CPU needs to be reset by software. This can be achieved through the "reset" instruction provided by the dsPIC33F microcontroller. The "reset" instruction can be executed at the corresponding position of the program.

3 Discussion of Several Issues in Practical Applications
3.1 Correctness of Program Upgrade
The above article only focuses on the method of program upgrade and does not discuss the correctness of program upgrade. However, in practical applications, due to the existence of transmission interference, the received new program data may be wrong. If there is no corresponding countermeasure, it will obviously affect the normal operation of the product. In order to ensure the correctness of program upgrade, the following countermeasures are commonly used:
(1) Strictly check each frame of data (such as CRC check), and then update the corresponding program storage area after the check passes; after updating the program storage area, re-read the program storage area data and compare it with the received data. Only when the data is correct can the next frame of data be received.
(2) If the hardware cost is not considered, an external data storage device can be used to update the program storage device uniformly after all new program data is completely received and checked. Finally, the data of the entire program storage device is compared with the received data one by one. If the comparison is correct, the upgrade is completed.
(3) If the application occupies a small space, the application area in the program storage device can also be divided into two parts. When the program is upgraded, these two parts are covered in turn. This operation mode allows the program memory to store both the old version and the new version of the application program at the same time. When the new version of the program does not run normally for some reason, the boot program can switch the application program to the old version.
Method (1) is the easiest to implement, but the application program can only run normally after the entire upgrade process is completely completed. If the communication is interrupted during the upgrade process for some reason, the application program will not be able to run. This is the biggest drawback of this method.
Method (2) is also easy to implement, and because it starts upgrading after all the new program data is received, it overcomes the drawbacks of method (1). However, this method requires increased hardware costs and occupies more hardware resources of the microcontroller (for interaction with external memory).
Method (3) can also overcome the drawbacks of method (1) and does not require increased hardware costs, but the implementation of this method is obviously much more complicated than the previous two methods. The above three methods each have their own advantages and disadvantages, and can be selected according to actual conditions in practical applications.
3.2 Breakpoint resume problem
In actual applications, the program upgrade may need to be restarted for some reason (such as power failure reset). At this time, if the transmission is still started from the first frame of data, it will cause a waste of time, and for some communication networks that charge by traffic (such as GPRS), it will also cause a waste of communication costs. Therefore, it is necessary to implement the "breakpoint resume" function in actual applications. This can be achieved by imitating the FTP breakpoint resume principle, that is, the microcontroller refreshes the upgrade information (including the current frame number) stored in the external non-volatile memory (such as EEPROM) every time it correctly receives and stores a frame of data. If the program upgrade is restarted, the current upgrade information will be sent to the server, and the server can start sending data from the "breakpoint", thereby realizing the "breakpoint resume" function.

4 Conclusion
The application upgrade method based on the dsPIC33F series microcontroller has been successfully applied in the development and design of products such as IPPhone and security systems, and the operation is stable and reliable. Although the program upgrade method discussed in this article is based on the dsPIC33F series of microcontrollers, its basic principles are also applicable to other microcontrollers with runtime self-programming or In Application Program (IAP) functions.

Reference address:Application program upgrade method based on dsPIC33F series microcontroller

Previous article:Design of MP3 player based on single chip microcomputer
Next article:Design of portable mine gas detection system

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号