- Compile with full size optimization.
- Use local variables whenever possible.
- Use the smallest applicable data type. Use unsigned if applicable.
- If a non-local variable is only referenced within one function, it should be declared static.
- Collect non-local data in structures whenever natural. This increases the possibility of indirect addressing without pointer reload.
- Use pointers with offset or declare structures to access memory mapped I/O.
- Use for(;;) { } for eternal loops.
- Use do { } while(expression) if applicable.
- Use descending loop counters and pre-decrement if applicable.
- Access I/O memory directly (i.e., do not use pointers).
- Declare main as C_task if not called from anywhere in the program.
- Use macros instead of functions for tasks that generates less than 2-3 lines assembly code.
- Reduce the size of the Interrupt Vector segment (INTVEC) to what is actually needed by the application. Alternatively, concatenate all the CODE segments into one declaration and it will be done automatically.
- Code reuse is intra-modular. Collect several functions in one module (i.e., in one file) to increase code reuse factor.
- In some cases, full speed optimization results in lower code size than full size optimization. Compile on a module by module basis to investigate what gives the best result.
- Optimize C_startup to not initialize unused segments (i.e., IDATA0 or IDATA1 if all variables are tiny or small). If possible, avoid calling functions from inside the interrupt routine.
- Use the smallest possible memory model.
Five Hints to Reduce RAM Requirements
- All constants and literals should be placed in Flash by using the Flash keyword.
- Avoid using global variables if the variables are local in nature. This also saves code space. Local variables are allocated from the stack dynamically and are removed when the function goes out of scope.
- If using large functions with variables with a limited lifetime within the function, the use of subscopes can be beneficial.
- Get good estimates of the sizes of the software Stack and return Stack (Linker File).
- Do not waste space for the IDATA0 and UDATA0 segments unless you are using tiny variables (Linker File).
Checklist for Debugging Programs
- Ensure that the CSTACK segment is sufficiently large.
- Ensure that the RSTACK segment is sufficiently large.
- Ensure that the external memory interface is enabled if it should be enabled and disabled if it should be disabled.
- If a regular function and an interrupt routine are communicating through a global variable, make sure this variable is declared volatile to ensure that it is reread from RAM each time it is checked.
No comments:
Post a Comment