About code efficiency
————————
The programming guidelines of the MSP430 series are conducive to improving code efficiency. It should be pointed out that these guidelines are basically proposed based on the hardware structure characteristics of the MSP430 series.
1. The execution of the bit field type is very slow, so the bit field method should be used only to save data storage space. If the user must use bit field storage, the bit masking operation of char or unsigned int can be used instead of the bit field operation.
2. Variables that are not used outside the module should be declared as static, so that these variables may be allocated in registers for temporary storage during compilation, thereby improving code efficiency.
3. Use unsigned data types as much as possible. In many cases, operations on unsigned data types are more efficient than those on signed data types, especially for division and modulus operations.
4. ANSI-C prototype functions should be used in program design. Calling ANSI functions is more efficient than calling K&S functions.
5. The MSP430 series microcontrollers have the highest efficiency in operating 16-bit data types. Such as: short, int, unsigned int types, etc. Generally speaking, using 8-bit data types can save data storage space, but it does not reduce the storage space of program code; and for the use of 32-bit data types, since the structure of the MSP320 series does not directly support it, the execution efficiency is low.
6. Scalar automatic variables (scalar auto) are usually allocated in registers, so whenever possible, auto type should be used instead of static type.
7. The first two parameters of a function are transmitted using registers, so when passing parameters to a function, it is often more efficient to use parameters than static variables.
8. Copying structures and union data types is a very expensive operation. You should try to avoid assigning structures and unions during program execution, avoid using structures and union data types as function parameters, and avoid letting functions return structures and union data types. Whenever possible, pointers to structures and unions should be used to implement the above operations.
9. Non-scalar automatic variables (structures, unions, arrays, etc.) declared in a function without initial values will have their data copied each time the function is called during program execution. If it is a constant, you can use the static const storage class to avoid this.
In addition, the method of using C program to call assembly subroutines is also an effective means to improve code efficiency.
The role of the connection command file
is
to make the program design suitable for the user's target system. Each project requires a connection command file containing the memory mapping details of the target system. Its file extension is .xcl, and its content can be browsed with a text editor.
The connection command file has basically the same structure. Take MSP430F149C.xcl as an example. The file contains a large number of comment lines to increase readability. There are not many command lines that actually work.
First, use the -c command to define the CPU type as MSP430
-cMSP430
, and then use a series of -Z commands to define the memory segments used by the compiler, and define the data, stack, information, code, interrupt vector and other functional areas respectively. The commands are as follows:
-Z(DATA)UDATA0,IDATA0,ECSTR=0200-09FF
-Z(DATA)CSTACK#0200-0A00
-Z(CODE)INFO=1000-10FF
-Z(CODE)CODE,CONST,CSTR,CDATA0,CCSTR=1100-FFDF
-Z(CODE)INTVEC=FFE0-FFFF
Then use the -e command to define the low-level input and output functions for printf and scanf that are suitable for the user's application requirements and space constraints. The commands are as follows
-e_small_write=_formatted_write
-e_medium_read=_fomatted_read
Finally, specify the loaded C library (runtime library file) to adapt to the differences between different CPUs, such as whether it contains a hardware multiplier, etc.:
cl430ksm.r43Users
can directly select the link command file suitable for the target system from the ICC430 subdirectory. However, in order to better adapt to the user's design goals, they can also edit and modify the link command file.
C430 Configuration
————
〉〉Set the header file of the target machine modelAccording
to the target machine model (such as MSP430F149), add the following statement at the beginning of the source file:
#include 'MSP430X14X.h'This
file contains the address description of the special function registers in the specific MSP430 target machine. In the subsequent code writing, the name of the register can be used directly. The content of the MSP430X14X.h file can be found in the INC subdirectory of the software.
1. Memory allocationThrough
the link command file, the address of the ROM and RAM in the hardware environment of the target system can be described. The link option specifies:
*The ROM area is used to store functions, constants and initialization values
*The RAM area is used to store stacks and variables
*The non-volatile RAM area can be described by the no_init type modifier and the memory #pragma compilation command to resident variables. The compiler places this variable in a separate no_init segment. Its address range is specified by the user in the non-volatile RAM area. The system does not initialize these variables when the program is running.
2. The default stack size
is 512Bytes, using the line command: -Z(DATA)CSTACK+200
3. Input and output
*putchar and getchar are basic functions used to implement all character-based I/O operations. In order to implement all character-based I/O functions, users must define these two functions using the tools provided by the hardware environment.
*printf and sprintf functions use a general formatting function called _formatted_write. The ANSI standard version _formatted_write
is very large, and the functions it provides are not required in many applications. In order to reduce memory overhead, two smaller versions that can be replaced are provided in the standard C library:
1) _medium_write
is the same as _formatted_write except that it does not support floating-point numbers. Therefore, using the %f, %g, %G, %e and %E specifiers will generate errors. _medium_write is much smaller than _formatted_write.
2) _small_write
is similar to _medium_write, but only supports %%, %d, %o, %c, %s, and %x specifiers for int types, and does not support bit fields and precision parameters. _small_write is about 10%-15% of _formatted_write in size.
The default version of the C430 compiler is _small_write: -e_small_write=_formatted_write
To select the full ANSI version, delete this line.
* Simplified printf
Sometimes, in order to support special output formats or non-standard output devices, special output routines must be customized. The file imwri.c provides a highly simplified printf function source file that does not include sprintf. This source program version can be modified as required by the user, and then the compiled module is added to the library to replace the original module.
* Scanf and sscanf
are similar to printf and sprintf. Scanf and sscanf use a general formatting function called _formatted_read. The ANSI standard version _formatted_read is very large, and the functions it provides are not needed in many applications. To reduce memory usage, a smaller version is also provided in the standard C library: _medium_read. It has the same functions as _formatted_read except that it does not support floating point numbers, but _medium_read is much smaller than _formatted_read.
The default version of C430 is _medium_read.
4. Hardware and memory initialization
Create the target module file cstartup.r43, and then the user should use the following commands in the connection command file to make XLINK reference the user-defined CSTARTUP module instead of the module in the library:
-A cstartup
-C library
In the Workbench, you can add the modified cstartup file to the user's project and add the -C command before the library in the connection command file.
Other keywords
——————
Target (target) Group (group) Source file (source file)
Hardware multiplier (hardware multiplier) Hardware emulator (flash emulation tool)
Software simulation debugging (simulator) Hardware breakpoint (full speed)/virtual breakpoint (virtual breakpoint) (single step)/system breakpoint
Previous article:UCOSii (I)——System Initialization
Next article:SPI Timing Analysis
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- Sentenced to 4 years! Megvii Technology driver attempted to extort 3 million yuan from the chairman but failed
- Problems and Solutions in TMS320F206 Simulation Debugging
- Disassembly of 10 yuan smart bracelet
- Last day! Microchip Live Broadcast | How to solve precision timing challenges in ADAS systems
- [Fudan Micro FM33LG0 Series Development Board Review] Basic Introduction
- MaxplusII advanced use and improvement.rar
- Talking about software architecture from the perspective of history - object-oriented programming (Part 3)
- DCDC input end inrush current problem
- Non-blocking assignment order problem
- [Raspberry Pi 4B Review] + Real-time status monitoring website server construction