Research on Key Technologies of DSP Hybrid Programming[Copy link]
DSP (digital signal processor) has been widely used in communication, aerospace, industrial control, medical, national defense, automobile and other fields due to its high-speed digital signal processing function, strong real-time performance, low power consumption, high integration and other embedded microcomputer features. TMS320LF240xA DSP (hereinafter referred to as LF240xA) is a high-performance 16-bit digital signal processor launched by TI of the United States. It has the characteristics of fast operation speed and rich peripherals integrated on chip, so it is also called DSP controller. The application fields are mainly industrial measurement and control, motor control, household appliances and consumer electronics. The software development process of LF240xA can use both assembly language and C language. The code execution efficiency of assembly language is high and the running speed is fast. It can directly operate the registers and give full play to the hardware performance of DSP controller; but its development workload is large and the program readability and portability are poor. Unlike assembly language, C language is highly readable, simple to program, and easy to debug, making it suitable for writing programs with complex structures and algorithms. However, for control, using C language to develop programs also has obvious disadvantages: first, C language code is redundant, which reduces execution efficiency and does not meet the requirements of certain control fields with high real-time requirements; second, C language cannot implement certain low-level operations. In the specific software development process, assembly language and C language can be combined for programming to give full play to their respective advantages. This can not only meet the real-time requirements but also realize the required functions, while taking into account the readability and programming efficiency of the program. For this reason, understanding and mastering the mixed programming technology of C language and assembly language is of great significance for DSP software development. 1 Preparation Before performing mixed programming, you must first create a basic operating environment. This basic environment includes the allocation of storage space, the definition of DSP register mapping addresses, and the definition of interrupt vectors. (1) Allocation of storage space Command files (*.cmd) are used to implement the allocation of program memory and data memory space. One is the target memory definition (defined by the MEMORY command), and the other is the location of each segment (defined by the SECTIONS command). (2) Definition of register mapping address Header files (*.h) are used to define the mapping addresses of the internal registers of the DSP controller used, as well as user-defined constants, registers, etc. Usually, the header file is at the beginning of the main program and is called using the assembly indicators ".include" and ".copy". There are two ways to define header files. The first is to define various registers in the form of pointer variables, that is, in the following form: volatile unsigned int T1CMPR Ox7402 The second is to define the names of various registers in the form of macro definitions: # define T1CMPR Ox7402 (3) Definition of interrupt vectors LF240xA provides multiple interrupts, such as INT1~INT6, TRAP, etc. The reset interrupt vector (c_int0) is a special interrupt defined in the real-time operation support library (rts2xx.1ib). Its function is to perform software stack operations first, then initialize global variables, and finally call the main program main(). So when reset (Reset), the program jumps to c_int0 for corresponding processing. The interrupt vector table is a simple jump instruction table that connects the interrupt subroutines used in the main program with the corresponding interrupt level types. Each interrupt vector in the table occupies 2 words and is declared in the command file to 0000h~003Fh of the program memory space. 2 General methods of mixed programming There are generally three methods for mixed programming of LF240xA: 1. Manually modify and optimize the assembly program formed after compiling the C language program; 2. Directly embed assembly statements in the C language program; 3. Write C language programs and assembly language programs separately, and then compile them independently into target code modules, and then link them. The first programming method requires extreme familiarity with both assembly and C languages, and this programming method has a relatively large negative impact on the readability and extensibility of the program, so it is generally not recommended. The second method is suitable for situations where the frequency of statement execution is very high and the efficiency of C programming and assembly programming is very different, such as entering a general interrupt subroutine for interrupts. The third method is one of the most commonly used methods of mixed programming. In this method, both the C language program and the assembly language program can use the functions and variables defined by the other party. The following focuses on the latter two methods. 2.1 Embedding assembly language in C language program C language program supports asm instruction, so this instruction can be used to directly embed assembly statements into C language programs. Some control bits in LF240xA that cannot be operated by C language can be implemented in this way. This method only requires adding double quotes on both sides of the assembly statement and enclosing it in parentheses, and adding the asm keyword in front, that is, "asm ("assembly statement");". It should be noted that the assembly statement cannot be adjacent to the previous double quote, and they must be preceded by a space, tab or label. For example, the interrupt instruction SETC INTM in assembly language is embedded in C language as "asm("SETCINTM");". Although this method is simple to operate, the assembly code is likely to destroy the original C language environment, resulting in unpredictable results. Therefore, it is only recommended to use a small amount of system initialization at the beginning of the program. It is not recommended to use this method when embedding multiple assembly language sentences to implement a complete function in C language. 2.2 Mutual calls between C language and assembly language programs (1) C language program calls assembly function The assembly function called in the C language program appears in the form of a program label in the assembly language. The program label is defined as an operand using .global and is preceded by an underscore "_". The assembly function can also use the accumulator to pass the return value to the C language program. LF240xA has 8 auxiliary registers (AR0~AR7) available for use. In the C language environment, these registers have clear division of labor. ①AR0: Frame Pointer (FP). LF240xA only provides a hardware stack of 8 words, which cannot meet the needs. Therefore, the C environment defines a special memory space as the software stack. The function of the software stack is to allocate local variables, pass function parameters, save the status of the processor, save temporary results, etc. AR0 points to the beginning of the local data space of the function in the software stack. ②AR1: Stack Pointer (SP) of the software stack. AR1 is a special pointer pointing to the top of the software stack. ③AR2: Local Variable Pointer (LVP). AR2 stores the offset of local variables and addresses local variables together with AR0 (FP). ④AR6, AR7: Register variables. Variables modified with register in C language programs are stored in AR6 and AR7. ⑤AR3~AR5: User-defined. There is no special agreement for AR3~AR5, and the user can freely decide their use. At the entry of the assembly program, it is assumed that ARP has been set to AR1, which is automatically done by the C compiler. When a C language program calls an assembly function, the assembly function program must follow the following specifications: ① Pop the return address from the hardware stack and push it into the software stack; ② Push the C program's data structure pointer FP into the stack; ③ If the assembler changes AR6 or AR7, they also need to be pushed into the stack; ④ Allocate local data structures; ⑤ Execute the actual task code of the assembler; ⑥ If the assembler has a return value, put this return value into the accumulator; ⑦ Set ARP to AR1; ⑧Deallocate local data structures; ⑨If AR6 and AR7 have been saved, restore their values from the software stack; ⑩Restore FP from the software stack; ⑾Push the return address stored in the software stack onto the hardware stack; 378678[/attach]