Friday, April 7, 2017

Key points for Embedded Programming

There are some intricacies involved when you look at the core assembly level in embedded systems. People who I have seen not take a formal Embedded Course or any formal C course in general, end up coding the less efficient way. This is a generalisation but I have too at some point of time while  coding controllers,  have been coding the wrong way. The thing is developing logic is one aspect of the story and making the most of the data types, qualifiers and specifiers is also important. This is primarily because while coding embedded systems, we generally have constraints on the flash memory or RAM or the pins available. So the better you code, the better will be the optimisation and the better the controller will perform.

Take a look at this code snippet from my code here.

1) Use of static and const

This is an interrupt handler for the counting of encoder states and look at the static variable. The static and const is what I want to focus here. Some key points:
  • What's actually happening here is that, the variable count needs to compute the counts, leave the function, go to main and come back while keeping track of the counts.
  • static is used here since I want to essentially keep track of the counts, but then if I use a local variable, it will get destroyed the moment it leaves the function.
  • Having a static specifier, helps here as it tells the compiler to statically allocate a fixed size for the variable. This helps in retaining the value of variable when scope of the function gets destroyed and refrains the compiler from making copies if the variable.
  • const is a qualifier and it tells the compiler not to make any changes to the variable.
  • a global static variable tends to restrict the variable to be under scope of that file only. Meaning no other file can share the same variable.
2) Use of volatile

This is another grey area in embedded. Not many know of it and not many use this, atleast the amateur embedded programmers. volatile is again a qualifier and not a storage specifier like static. Basically the compiler keeps on making optimisation so that the code runs faster and efficiently. When it comes to the compiler, it basically converts the high level language to a machine language and in this is where it tries to optimise the code. 
So by putting a volatile qualifier you are basically telling the compiler not to optimise it. Here's a good example

3) Use of pointers

Again a topic largely neglected by intermediate and amateur embedded programmers. Basic logic is needed to get the code running but running into optimisations and increasing the controller efficiency is also needed. I'm not going to divulge into details, but basically pointers point to addresses directly. This is important because most of the time you are writing some driver and you need the address the memory location directly.
Also, the fact that it does not let the program make copies if the variable at run time.

4) Use of debuggers

When you are making large projects, you can't do a trial and error everytime to get the bugs in the code. You ought to have a good debugger to see what happens to your code line by line.
This also ensures if you have peripherals attached to your controller, you can actually check if the data incoming is correct or not. I use a gdb server setup to load and debug using the st-link on STM32.

A short video of which you can see here:

No comments:

Post a Comment

Featured Post

ROS Autonomous Navigation Bot - [Clerkbot] - Initial Tests

Finally it took me three months to fully come up with this robot and just a fun fact, it took me a month to just tune the ocean of paramet...