Tips-and-Tricks

Version 9 (Anthony Rowe, 06/08/2007 02:32 pm)

1 1 Anthony Rowe
2 1 Anthony Rowe
== Nano-RK Tips and Tricks ==
3 1 Anthony Rowe
Many of these suggestions are generally good practice for any resource constained development.
4 1 Anthony Rowe
5 4 Anthony Rowe
'''Don't Allocate Large Data Structures Inside Functions'''
6 2 Anthony Rowe
 * Allocating large data structures in functions puts them on the stack.  Typically your task's stack is only 128 bytes, so putting things like a network buffer on the stack will almost certainly cause a stack overflow. Instead, make large data structures global.
7 1 Anthony Rowe
8 1 Anthony Rowe
'''Avoid Recursive Function Calls'''
9 1 Anthony Rowe
 * Recursive function calls are also heavy when it comes to stack consumption.  Try to avoid them whenever possible
10 1 Anthony Rowe
11 8 Anthony Rowe
'''Use "inline" For Speed And To Save Stack Space'''
12 3 Anthony Rowe
  * When it seems fit, use "inline" to avoid pushing things onto the stack for small functions.
13 3 Anthony Rowe
14 4 Anthony Rowe
'''Be Very Careful With Dynamic Memory'''
15 3 Anthony Rowe
 * Malloc does work, but if you frequently malloc and free memory you will create fragmentation.  Eventually this fragmentation can lead to malloc failing.  This is especially bad when you run your application for a few minutes and it seems fine, but then fails after a few hours.  In general, avoid using malloc whenever possible. If you must, use it in situations where the code can safely continue if allocation fails.
16 5 Anthony Rowe
17 5 Anthony Rowe
'''Use nrk_kprintf() Whenever Possible'''
18 5 Anthony Rowe
 * Strings used in normal printf() are stored in RAM!  nrk_kprintf() stores them in FLASH memory using the PSTR() macro.  Only use regular printf() when the string is dynamic (i.e. you use %d to print variables etc).  
19 9 Anthony Rowe
20 9 Anthony Rowe
'''How much memory am I using?'''
21 9 Anthony Rowe
 * After you compile, you should get a printout that tells you the size of your .data, .text and .bss sections.  .data is the amount of RAM that your program uses that is defined at startup to a particular value.  This means that it has to be stored in both RAM and ROM. .text is the amount of code your program uses.  .bss is the amound of zeroed out RAM your program uses.  Since the memory is set to 0 by default, you do not need to store a copy of it in ROM like with the .data section.  So your total RAM is data+bss while your FLASH usage is data+text.  In Nano-RK the stacks for applications are stored in static memory, so they appear in the bss section.  The kernel stack by default does not, so you should account for the RAM usage as data+bss+128bytes to be safe.   In the following example the RAM usage is 220+1021+128 = 1369 and the FLASH is 17258+220 = 17478.  A FireFly v2.2 node has 8192 bytes of RAM and 131072 bytes of FLASH.
22 9 Anthony Rowe
{{{
23 9 Anthony Rowe
Size after:
24 9 Anthony Rowe
main.elf  :
25 9 Anthony Rowe
section     size      addr
26 9 Anthony Rowe
.data        220   8388864
27 9 Anthony Rowe
.text      17258         0
28 9 Anthony Rowe
.bss        1021   8389084
29 9 Anthony Rowe
.stab      41268         0
30 9 Anthony Rowe
.stabstr   16934         0
31 9 Anthony Rowe
Total      76701
32 9 Anthony Rowe
}}}