Fortsätt till huvudinnehåll

Inlägg

Visar inlägg med etiketten Embedded

Embedded development questions part II

I present to you, this piece of code: bool keyPressed = false; void interruptServiceRoutine() {   keyPressed = true; } int main() {   for (;;)   {     while (!keyPressed);     std::cout << "A key was pressed!";   }   return 0; } Take a minute to think about what's wrong with the code above. Ready? Ok, here are the things I came to think of during my job interview. keyPressed  is set to true in the ISR. But never (re)set to false anywhere. keyPressed  should be volatile  to ensure the compiler does not optimize it. There is a risk for key bounce  which may result in several interrupts per key press. To solve 1 and 2 the code can be modified to look like this: volatile bool keyPressed = false; void interruptServiceRoutine() {   keyPressed = true; } int main() {   for (;;)   {     while (!keyPressed);    ...

Embedded development questions part I

Let me just hit you with this. Check this code: const char* aChar_p = "Hello"; // (1) (2) bool aBoolean; // (3) int main() {   int i = 0; // (4)   bool flag = true; // (5)   return 0; } Now answer this. Where in memory will (1) be stored? What about (2), and (3), and (4), and (5)? This was another question I was faced with during my recent job interview. To be honest, I have never needed to care much about linker files and memory layout. In every project I have been in these things have already been set up long before I've entered the scene. With that said, I think it's still good knowledge to have, so let's go trough them one by one. Variables defined at this (top) level are called global. They are accessible from anywhere in the code and has a lifespan that stretches over the entire program execution. You would say it is a static variable. Since the variable is static, you might think that adding the static  keyword won't make any difference...