How to improve the execution efficiency of MSP430 C language code

Publisher:WanderlustGlowLatest update time:2018-05-03 Source: eefocusKeywords:MSP430 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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

Keywords:MSP430 Reference address:How to improve the execution efficiency of MSP430 C language code

Previous article:UCOSii (I)——System Initialization
Next article:SPI Timing Analysis

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号