Hi, everyone !
When I wait a status bit in a register, can I use "while" statement. the following is a example.
I saw a lot of "while" statements in driver code even in menufacturer (NXP、TI) demo code. So, does it has no bad effect on an application ?
You should consider the case when the flag that you are waiting for is never set.
For example, COP watchdog or timer can be used to escape from the infinite loop.
I hope it helps.
The register must have been declared 'volatile' so the compiler will be forced to re-read the register in the loop.
And of course the bit should be set by hardware. If you cannot guarantee that, add a timeout counter in the loop and exit the loop when the hardware does not set the flag in time. See the code a few lines higher, delayT is the timeout counter.
Thanks for your kindly reply ! Diana Batrlova Arie de Muijnck
I know the COP watchdog and timer interrupt can deal with waitting timeout, but I'm thinking about some no interrupt, no watchdog conditions, such as initialization following power-up.
I thought about two methods shown below, these two methods are not convenient. The method 1 uses delayT, the method 2 uses macro DELAY based hardware counter. Do you have some better suggention ?
Diana has left for her vacation so I'll put a few notes.
Hmmm, it is really up to you which method you will use. I usually prefer the simplest method which can be easily investigated after a months later when you forget a lot about ideas you had during solution. Only think also about any asynchronous process, for example, an interrupt which can be launched and served during loop or something that directs program flow somewhere else. Of course, it should not affect process if it only extends the loop cycle, however always think whether no background process is able to affect your polling method of bit status test.
I have used, usually (similar to your method 1):
static unsigned long cnt;
cnt = some value;
While( ( ! tested_bit ) && cnt ) // repeat while tested bit is not set and loop period is not exceeded
If( ! cnt ) report_error( WaitingLoopPeriodExceeded ) ;
HW counter, if I have understand it correctly, can be a problem if there is another interrupt which can cause loss of correct flag about time period overflow.
Retrieving data ...