# Sequential programming and waits

**URL:** <https://forum.sierrawireless.com/t/sequential-programming-and-waits/3815>\
**Category:** Documentation feedback\
**Created:** [November 3, 2009, 12:17pm UTC](https://forum.sierrawireless.com/t/sequential-programming-and-waits/3815 "2009-11-03T12:17:25Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![blackyblack](https://avatars.discourse-cdn.com/v4/letter/b/2acd7d/32.png) [@blackyblack](https://forum.sierrawireless.com/u/blackyblack)\
**Post date:** [November 3, 2009, 12:17pm UTC](https://forum.sierrawireless.com/t/sequential-programming-and-waits/3815/1 "2009-11-03T12:17:25Z")

</div>

I’m used to write programs for wavecom modules as tied together timers. It looks to me like the only way to make pauses between some actions. Is it right? Now I’m using SPI exchange with external processor and I have to organize process as:

1. Write SPI data
2. Pause for a while (10 ms is enough)
3. Read SPI data

Now with wavecom’s timers I’ve got a very messy code which looks like timer to write data, timer called from within write for delay, timer called from delay to read data. Or a bit better option to have an FSM like timer calling itself and using STEP variable to pass from one step of algorithm to another.  
I cannot use WHILE loops to make a pause from read to write since it blocks the entire module and I do not see a way to give execution to OS while staying in the while loop.

What can I do to make my program look better and what is the way to organize short and long delays in OpenAT?

---

<div class="post-metadata">

**Author:** ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://forum.sierrawireless.com/u/awneil)\
**Post date:** [November 3, 2009, 12:29pm UTC](https://forum.sierrawireless.com/t/sequential-programming-and-waits/3815/2 "2009-11-03T12:29:12Z")

</div>

Definitely the FSM!

but note that there is now adl\_ctxSleep() - see: [viewtopic.php?f=53&t=3890&p=15211&hilit=adl\_ctxSleep#p15211](http://www.wavecom.com/modules/movie/scenes/forums/viewtopic.php?f=53&t=3890&p=15211&hilit=adl_ctxSleep#p15211)

---

<div class="post-metadata">

**Author:** ![blackyblack](https://avatars.discourse-cdn.com/v4/letter/b/2acd7d/32.png) [@blackyblack](https://forum.sierrawireless.com/u/blackyblack)\
**Post date:** [November 3, 2009, 1:05pm UTC](https://forum.sierrawireless.com/t/sequential-programming-and-waits/3815/3 "2009-11-03T13:05:16Z")

</div>

I’m not sure that adl\_ctxSleep will help but thank you anyway.

---

<div class="post-metadata">

**Author:** ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://forum.sierrawireless.com/u/awneil)\
**Post date:** [November 3, 2009, 2:18pm UTC](https://forum.sierrawireless.com/t/sequential-programming-and-waits/3815/4 "2009-11-03T14:18:06Z")

</div>

> [@blackyblack](#):
>
> I’m not sure that adl\_ctxSleep will help

Not knowing your code, neither am I - but it may be worth a look.

It does allow you to have inline waits, though - without tripping the watchdog (because it allows the rest of the system to continue - unlike a loop).

---

<div class="post-metadata">

**Author:** ![Madouc](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@Madouc](https://forum.sierrawireless.com/u/Madouc)\
**Post date:** [November 3, 2009, 6:30pm UTC](https://forum.sierrawireless.com/t/sequential-programming-and-waits/3815/5 "2009-11-03T18:30:40Z")

</div>

> [@awneil](#):
>
> > [@blackyblack](#):
> >
> > I’m not sure that adl\_ctxSleep will help
> 
> Not knowing your code, neither am I - but it may be worth a look.
> 
> It does allow you to have inline waits, though - without tripping the watchdog (because it allows the rest of the system to continue - unlike a loop).

and is also using the same 18ms system granularity

---

<div class="post-metadata">

**Author:** ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://forum.sierrawireless.com/u/awneil)\
**Post date:** [November 3, 2009, 7:03pm UTC](https://forum.sierrawireless.com/t/sequential-programming-and-waits/3815/6 "2009-11-03T19:03:35Z")

</div>

> [@Madouc](#):
>
> using the same 18ms system granularity

Is that a problem for you?

---

<div class="post-metadata">

**Author:** ![yuantuh](https://avatars.discourse-cdn.com/v4/letter/y/e47c2d/32.png) [@yuantuh](https://forum.sierrawireless.com/u/yuantuh)\
**Post date:** [November 3, 2009, 10:59pm UTC](https://forum.sierrawireless.com/t/sequential-programming-and-waits/3815/7 "2009-11-03T22:59:41Z")

</div>

You can use endless while loop as you wish if your OAT app use multi-tasks. It is not a good idea to use fixed delay for SPI bus. SPI concerns how many cycles to interface external devices, say SD card, AT45XX flash, etc. For simplisity, you may implement software SPI by yourself by using any 3 GPIO pins. However, software SPI is slow and it may be less than 1Mb/s compareing with hardware SPI 13Mb/s.

You may have several solutions to solve your issues as follows:

1. Use alarm clock for single task app: TCU timer can achieve 1ms resolution. So you can write your own alarm clock to trigger events at predefined interval (your case is 10ms). Please use high level interrupt to implement TCU timer.
2. Use endless while loop for multi-task app. Wavecom have multi-task timers example, but you may need TCU timer also because ADL timer only have 18.5ms resolution.
3. You may use RTC to figure out time elapse, say 10ms, in while loop, and then trigger events you want

---

<div class="post-metadata">

**Author:** ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://forum.sierrawireless.com/u/awneil)\
**Post date:** [November 4, 2009, 12:27am UTC](https://forum.sierrawireless.com/t/sequential-programming-and-waits/3815/8 "2009-11-04T00:27:48Z")

</div>

> [@yuantuh](#):
>
> You can use endless while loop as you wish if your OAT app use multi-tasks.

No, you can’t: if any task “hangs” in a loop - deliberately or otherwise - it will trip the watchdog.

> [@](#):
>
> It is not a good idea to use fixed delay for SPI bus. SPI concerns how many cycles to interface external devices, say SD card, AT45XX flash, etc.

The OP was talking about delays between SPI operations - not timing the SPI transfer itself!

---

<div class="post-metadata">

**Author:** ![yuantuh](https://avatars.discourse-cdn.com/v4/letter/y/e47c2d/32.png) [@yuantuh](https://forum.sierrawireless.com/u/yuantuh)\
**Post date:** [November 4, 2009, 4:00am UTC](https://forum.sierrawireless.com/t/sequential-programming-and-waits/3815/9 "2009-11-04T04:00:06Z")

</div>

I have three endless while loop subtasks and never trigger watchdog. Of course, your have to put some pause before going to next loop. The following pseudo code is one of subtasks in my OAT app.

void RTasks::AudioTask()  
{  
while(1) {  
PauseLittleTime();  
MenuStatus menuStatus = GetMenuStatus();  
switch (menuStatus) {  
case MS\_IDLE: DoIdle(); break;  
case MS\_STOP: DoStop(); break;  
case MS\_START: DoStart(); break;  
case MS\_VOICECALL: DoVoiceCall(); break;  
case MS\_SPEAKER: DoSpeaker(); break;  
}  
}  
}
