March 24, 2013

Stack usage for ARM Cortex-M

I'll present how I used one of the methods to measure stack usage, not using any plugin, just debugger and memory window.

Methods to use:
1. IDE plugins (usually compiler and linker do the job)
2. Fill pattern (add small assembly routine in startup)
3. Take an address of the stack pointer in the main function, and in the most nested function (for a simple application usable)


IAR stack plugin [www.iar.com]
This document is targeted to IAR stack plugin. Although this did not work for the MCU I debugged.

Stack and Heap [www.iar.com]
What's stack, heap, methods explained briefly how to calculate stack usage in IAR. Methods applicable for other IDE's as well, not limited to IAR only.

Stack use in C and C++ [www.keil.com]
How to obtain stack usage in KEIL using callgraph (option --callgraph).
Here's callgraph output which I found on the internet, not needed to share mine:
Keil callgraph - an example [ftp.analog.com/pub/MicroConverter]

Using indirect calls (function pointers) make this method unreliable, same for IAR stack plugin, where it can be added #pragma calls, but what if an application using another library, that's too much changes to do, more simple approach is in a following paragraph presented, a filling memory for a stack with a pattern and show how low it gets.

KEIL stack initialize routine [www.keil.com/forum]
Stack initialization routine, which I only accustomed to Thumb-2. Here's the code used on Cortex-M0. Label __user_initial_stackheap is located in assembly file for your board. Just run your application for a while and check the memory window to obtain the lowest address of the last character of your pattern (in our case it's AA (char values)). Calculate the subtraction which gives your application stack usage.

__user_initial_stackheap
    LDR     R0, =Stack_Mem
    LDR     R1, =Stack_Size           
    LDR     R2, =0xAAAAAAAA
fill             
    STR     R2, [R0]
    ADDS    R0,#4
    SUBS    R1,#4                     
    BNE     fill

This also can be added in assembly file which is used in IAR or any other IDE, just replace Stack_Mem symbol (beginning of the stack) and Stack_Size (size of the stack).

March 19, 2013

Singleton design pattern C++ (bookmark)

I am still waiting for my GoF book to arrive, thus I have to read some articles.. This time I was curious about singleton design pattern, there's a user type object in my application which has to be only one. To implement simple singleton, I chose Scott Mayers Singleton (from Effective C++).

C++ Singleton Implementation [vladislavonline.com]
Here's comparison between GoF implementation and Scott Mayer's.

Several C++ singleton implementations [silviuardelean.ro]
4 approaches presented, but scott mayers is slighty different, because copy and assignment are defined, which is not the original version.

Anyway, the best reference is the book Modern C++ Design. Chapter 6 contains information about implementation of Singleton, how to handle destroying it and also multi threaded application concern.
In case you don't have a copy of the book, this presentation uses sources from it, check it out:
Singleton C++ [www10.informatik.uni-erlangen.de]

March 17, 2013

Effective C++ in an embedded environment (book review)

There's only a preview of this presentation created by Scott Mayer, first 30 pages are accessible online.

The beginning is targeted to the virtual functions (virtual tables and their pointers). Virtual methods, multiply inheritance. Follows C++ features comparison with C with no-cast, small-cost and what can surprise C programmer if they are not aware of "small" details. More topics discussed in the book are C callbacks, code bloat (templates, inlining), TR1 and Modeling Memory-Mapped IO.

Almost at the end is Further information chapter where is quite long list of articles (links, books..) used for each topic in the book. That serves as references if something is still not clear.

Consequently, the book (more of a presentation than a book) gave me some insights in C++ which I did not consider before. As an example, I am currently implementing C++ application for one C library which generates callbacks. The section dedicated to the callbacks helped me a lot.

References:
Effective C++ in an embedded environment [www.artima.com]

March 13, 2013

KEIL and bkpt 0xab

This was particular my inattentiveness this time. Once I created new function which returns object created on the HEAP (using C++ and KEIL on Freescale KL25), program stopped working. It got stucked at instruction BKPT 0xab. I thought somehow semihost got enabled, did recheck if it was not enabled.

After I checked all options offered in KEIL, I started to think this must be related to the heap. The problem was in startup assembly file where HEAP was initialized to 0.

I have attached picture from disassembly:

March 3, 2013

Error detection checksums (bookmark)

This post will be describing checksums only.
How to find out the message you received is same as the one which was sent? Checksums or CRC? there are at least dozen approaches (let's name few : additive, XOR, one's complement, two's complement, fletcher's  and many more).

One library I have been using implemented additive checksum. Is this the right one to implement in embedded world? The answer is provided in the next link:

The Effectiveness of Checksums for Embedded Networks [ece.cmu.edu/~koopman]
One to start with, thesis dedicated to the checksums.

Which Error Detection Code Should You Use? [betterembsw.blogspot.com]
This one sums it all briefly.

Additive checksums [barrgroup.com]
I usually find an article on barrgroup which forces me to look for more than just that topic.

I am going to rewrite that additive checksum to the one's complement solution. Few more instructions executed once in a while are worth it. The implementation is in the barrgroup's article down the page. There's mistake though, sum should be 32bits in order to get properly carry bits.

February 28, 2013

Kernel coding standard

Kernel coding standard published on kernel.org differs from others by style which is used there. It made me smiled during reading, following lines are copied from it :

"However, if you have a complex function, and you suspect that a
less-than-gifted first-year high-school student might not even
understand what the function is all about"

Overall, there are many "weak" points highlighted there which should be considered.

Coding style [kernel.org]

February 17, 2013

Reciprocal multiplication

I have posted here links which were dedicated to the multiplication and division. I had to get back to it today because was not sure what does constant mean in one assembly code (0x199A).

Reciprocal Multiplication [homepage.cs.uiowa.edu/~jones]
Here was the answer to that constant, why to use fixed point arithmetic on dividing, also explanation why 0xCCCD is used to calculate 1/10 of a value.

I am glad it brought up the topic back and I had to revise it.

February 6, 2013

Sublime Text 2 for C/C++ development

I've decided to use VIM for my development. Although I installed it long time ago, I have not gave it a shot. On the other hand, I started using Sublime. The beginning was like notepad++ feeling, what does sublime offer more, I marveled. That's been itching my conscience and therefore I watched few tutorials and opened Sublime Text Cheat Sheet. Since that moment, I've pleasantly been using it with much more effectivity. Here I present most used shortcuts which makes my development faster ( take it as is, because I am still learning).

This document covers a lot, but I recommend to watch tutorials
Tutplus.com Sublime [tutsplus.com]

After you complete it, check this and write down (memorize) which you consider important.
Sublime Text Cheat [pastebin.com]

Plugins make sublime powerful, I use so far these - Linter (with cppcheck), CodeIntel, Bracket Highlighter.

The only problem I had, was to set up cppcheck in Linter, paste this into user settings for Linter (path to cppcheck)
{

"sublimelinter": "load-save",

"sublimelinter_executable_map": {

"c" : "C:/Program Files/Cppcheck/cppcheck.exe",

"c++" : "C:/Program Files/Cppcheck/cppcheck.exe"

},

"sublimelinter_gutter_marks": true,

"sublimelinter_popup_errors_on_save": true,

"sublimelinter": "save-only"

}


Happy coding and don't use a mouse, use a keyboard with shortcuts!