fopetesl
Posts: 44
Joined: Tue Oct 20, 2015 8:08 am

Constructing an unattended mS timer?

Tue Jan 26, 2016 6:28 pm

RPBi2
SPI link to ADXL345 accelerometer.
Using C I need to read three axes every 555uS and write to 'disk', so the processor is going to be pretty busy doing just that?
I also need to be running an interval timer to flash a LED every 500mS or so.
I could use delay() with a couple of variables to set up the 500mS timer but that will take some of the processor's cycles adding overhead to read delay() and manipulate the associated variables.

To read the system clock looks even worse since (I think) I would have to use a long long to read the clock and manipulate the result in a similar way to delay().

I cannot see interrupts being any faster so am I missing a simple function to achieve what I want?

Or will I need to go back to writing in Assembler to achieve the speed?

Any help appreciated.

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Constructing an unattended mS timer?

Tue Jan 26, 2016 6:46 pm

You may be able to do what you want on the Pi but I wouldn't consider it a beginners type task.

For the SPI part see viewtopic.php?f=44&t=71089

fopetesl
Posts: 44
Joined: Tue Oct 20, 2015 8:08 am

Re: Constructing an unattended mS timer?

Wed Jan 27, 2016 8:55 am

joan wrote:You may be able to do what you want on the Pi but I wouldn't consider it a beginners type task.

For the SPI part see viewtopic.php?f=44&t=71089
With your help earlier the SPI part now works a treat :)

I looked at your link.
Very interesting and looked like it might suit but there's a but .. the code link http://abyz.co.uk/rpi/pigpio/misc/adc/pispi.c is a 404 :(

With my level of C experience it's going to be hard work to emulate.

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Constructing an unattended mS timer?

Wed Jan 27, 2016 11:19 am

The directory no longer wants to serve up .c files. I have no idea why.

I've put the latest code in http://abyz.co.uk/rpi/pigpio/misc/adc/pispi.zip

fopetesl
Posts: 44
Joined: Tue Oct 20, 2015 8:08 am

Re: Constructing an unattended mS timer?

Wed Jan 27, 2016 12:40 pm

joan wrote:The directory no longer wants to serve up .c files. I have no idea why.

I've put the latest code in http://abyz.co.uk/rpi/pigpio/misc/adc/pispi.zip
Got it :) Thank you.

Looking at your code

Code: Select all

 while (sample < samples)
   {
      cb = rawWaveCB();
      now_reading = (cb-1) / cbs_per_reading;
      while (now_reading != last_reading)
      {
         getReading(ADCS, miso, (last_reading%buffer)*12, 0, 12, rx);
         for (i=0; i<ADCS; i++) val[i] = (rx[i*2]*16) + (rx[(i*2)+1]/16);
         printf("%8d %4d %4d\n", sample, val[0], val[1]);
         if (++last_reading >= buffer) last_reading = 0;
         sample++;
      }
      usleep(100);
   }
I've either lost it or the while(..) loop is the 40uS/reading?
So, assuming I haven't understood, how would you halve the time between readings as you suggest you might try?

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Constructing an unattended mS timer?

Wed Jan 27, 2016 1:02 pm

The timing of the reads is controlled by the DMA waveform (it actually talks to the SPI device) which copies the read SPI bits to a cyclic memory buffer. The part of the code you are looking at is extracting the data from that cyclic buffer as fast as possible (so earlier reading don't get overwritten).

Return to “Beginners”