Go Down

Topic: Fast digital I/O and software SPI with C++ templates (Read 4 times) previous topic - next topic

aleandrogreete

#10
Jul 22, 2012, 01:44 pm Last Edit: Jul 22, 2012, 01:47 pm by aleandrogreete Reason: 1
Thanks Fat16Lib! I will wait your examples! Thanks for sharing this!!!
Winezed, thanks a lot for help me, I never use templates and it's a great opportunity to study your solution!

mrTee

#11
Jul 25, 2012, 10:14 am Last Edit: Jul 25, 2012, 11:12 am by mrTee Reason: 1
Hi , thanks for this fine library! I wrote a decoder DCF77 library (for the atomic time broadcasted by the DCF77 radiostation) (see this thread) where I want to quickly read and discard small pulses. I think your library may help perform peak rejection more efficiently.

- I was wondering, though, is the read out speed limited by processor cycles, or by the hardware? Have you been able to measure that? I can imagine as a test: connect a pulse generator to pin 1, a scope to pin 2.  Read the pin 1 value and write it to 2.
- As I understand, the standard Arduino implementation needs many more instructions. Are those extra instructions in any way useful? Or, in other words, does your library also have drawbacks?

kert

#12
Aug 19, 2012, 02:33 am Last Edit: Aug 19, 2012, 02:35 am by kert Reason: 1

I was initially trying to re-create the fastDigitalWrite macros as a template library so I could add in extra features. But I hit a wall as no matter how much I can write, I really don't know that much about arduinos internals, that's where your code comes in.


This is the only thread that i found mentioning templated digitalwritefasts .. so posting here.

There is already a very cool templated fastPins header here https://github.com/felis/USB_Host_Shield_2.0/blob/master/avrpins.h

However, by default its syntax is different/object oriented, so a blink example would look like this:
Code: [Select]
#include <avrpins.h>

void setup() {
 P13::SetDirWrite();
}

void loop() {
 P13::Set();
 delay(1000);
 P13::Clear();
 delay(1000);
}


With a bit of preprocessor help, and partial template class specialization its easy to translate digitalWrite syntax to this syntax without creating any runtime overhead. Call this header digitalWriteFast3.h :

Code: [Select]

#include <avrpins.h>

template <class T, int N>
struct fast3_wrap_impl
{
   static T setmode() {
T::SetDirWrite();
}
static T write() {
T::Set();
}
};

template <class T>
struct fast3_wrap_impl<T, 0>
{
   static T setmode() {
T::SetDirInput();
}
static T write() {
T::Clear();
}
};

template <class T, int N>
void fast3_wrap_setmode()
{
   fast3_wrap_impl<T, N>::setmode();
}

template <class T, int N>
void fast3_wrap_write() {
fast3_wrap_impl<T, N>::write();
}

template <class T>
int fast3_wrap_read() {
return T::IsSet();
}

#define pinModeFast3(a, b) fast3_wrap_setmode< P##a , b>();
#define digitalWriteFast3(a,b) fast3_wrap_write< P##a , b >();
#define digitalReadFast3(a,b) fast3_wrap_read< P##a >();


After that, rewriting the loop example:
Code: [Select]

#include <digitalWriteFast3.h>

void setup() {
 pinModeFast3(13, OUTPUT);
}

void loop() {
 digitalWriteFast3( 13, HIGH );
 delay(1000);
 digitalWriteFast3( 13 , LOW ) ;
 delay(1000);
}



Feel free to modify and improve. I have not tested this beyond the simple blink, but i trust that as long avrpins.h does things correctly, it should be fairly safe to use.

I have verified that these digitalWriteFast3 calls resolve into single cbi/sbi instructions.

Maybe the author of the avrpins.h could add this template stuff in, as a user you would be able to use either syntax without thinking about it, and still get the same result.


fat16lib

#13
Aug 19, 2012, 04:25 am Last Edit: Aug 19, 2012, 04:28 am by fat16lib Reason: 1
The library in this thread didn't work well for use in other bit-bang libraries so I have rewritten it.  One of the main reasons for fast digital I/O is bit-bang for protocols like SPI.

Please see DigitalPinBeta20120804.zip http://code.google.com/p/beta-lib/downloads/list.

Also see this topic http://arduino.cc/forum/index.php/topic,117356.0.html.

I suspect the templates kert describes above will have the same problems.

I still include template classes for simple use in sketches but I base these classes on static functions with constant arguments.

These static functions are easier to use in other bit-bang libraries like a software SPI library which is included as an example in the new library.

kert

Thanks, you should add your libraries here http://arduino.cc/playground/Main/LibraryList
There were so many different fast i/o threads and half-solution libraries around .. gaah.

Anyway, i just realized my posted code is crap anyway, as you cant use variables as pin indexes, only numeric constants :)

Go Up