AnsweredAssumed Answered

How can I use a sleep function in a static library project?

Question asked by Nhan Tran on Feb 17, 2018
Latest reply on Feb 26, 2018 by Kerry Zhou

Hello all,

 

I am programming a Morse code decoder, which translate a string message in to Morse code, on an LPC1549 board. I implement a Morse code decoder class in the file containing the main function, and everything works fine. But when I want my project a bit more well organized by moving the class to a static library project, I am stuck at the sleep/delay function (the purpose of the sleep function is to blink a led to meet the timing requirement for Morse code). The reason is that the sleep function relies on a global variable "counter" that decrements every time Systick_Handler (which is configured to trigger every 1 ms) is called. So if I move the Morse code decoder class to another project, the sleep function called inside the decoder class cannot run. The Systick_Handler and Sleep function are shown below. I kindly ask for an advice. Any help is appreciated!

 

static volatile int state;
static volatile std::atomic_int counter;

extern "C"{
void SysTick_Handler(void)
{

   static int ticks = 0;
   if(counter > 0) counter--;
   ticks++;
   if (ticks > TICKRATE_HZ) {
   ticks = 0;
   state = 1 - state;
   }
}
}
void Sleep(int ms)
{
   counter = ms;
   while(counter > 0) {
      __WFI();
   }
}

Outcomes