[ell-i-developers] Re: ELL-i-Arduino Delay functions and interrupt disable

  • From: Pekka Nikander <pekka.nikander@xxxxxx>
  • To: ell-i-developers@xxxxxxxxxxxxx
  • Date: Fri, 21 Feb 2014 08:24:55 +0200

> [...] so it uses the function delayMicroseconds() from
> Arduino to read fast but not continuously, and this function is not be
> tested in our environment according to Pekka. Can someone test if this
> function is doing what it should? Probably the problem is here.

I think it is best that if you check it, with oscilloscope.  Just create a 
simple sketch that toggles an I/O with delayMicroseconds in between, and 
measure if the time is right with different values.

It is also a good idea to have a look at the source code in Ell-i/Arduino.git, 
in hardware/ell-i/stm32/cores/arduino/wiring_time.h.  (Grep is your friend.)  
Based on my quick look, it is no wonder if it does not work correctly.

> I don´t know if that works in our environment, if it does, please tell
> me how many nanoseconds each nop should take. They say that one cycle
> at 16 Mhz is 62.5 nanoseconds. I don´t know what is the frequency of
> our processor at the moment, so it would be interesting to know.

Relying on nops is a very bad practise.  The compile time frequency is 
available in F_CPU define.  However, on the ST Cortex-M0 it is possible to 
change the frequency on the fly...

A much better approach would be to use one of the timer/counters to read the 
pin at fixed periods, independent of the CPU.  For that there is the Arduino 
shiftIn API.  I seem to have implemented it using delay(), but it has never 
been tested.  A good exercise would be to rewrite the shiftIn API with a 

> noInterrupts() and interrupts() that is a more portable version

They are defined in wiring_constants.h.  They should work.

In general, at this staged of the development it is always a good idea to read 
the source code.


Other related posts: