Now and then I get the question about mapping Cosa Pin classes and member functions to Arduino/Wiring functions. There is actually an implementation of this in the sketch CosaBenchmarkPins.ino.
inline void pinMode(Board::DigitalPin pin, uint8_t mode)
{
IOPin::set_mode(pin, (IOPin::Mode) mode);
}
inline int digitalRead(Board::DigitalPin pin)
{
return (InputPin::read(pin));
}
inline void digitalWrite(Board::DigitalPin pin, uint8_t value)
{
OutputPin::write(pin, value);
}
inline void digitalToggle(Board::DigitalPin pin)
{
OutputPin::toggle(pin);
}
inline int analogRead(Board::AnalogPin pin)
{
return (AnalogPin::sample(pin));
}
The benchmark shows the performance when using Cosa's strongly data typed board/pin definitions. Below are the results for the adaptation functions (for Arduino 1.5.8, Arduino Nano, ATmega328P, 16MHz). The result is X2-X10 faster than the standard Arduino core.
175:void loop():info:Measure the time to perform an input pin read
194:void loop():measure:digitalRead(D7):63 ns
199:void loop():info:Measure the time to perform an output pin toggle
268:void loop():measure:digitalWrite(D8, 1/0):252 ns
274:void loop():measure:digitalToggle(D8):126 ns
280:void loop():info:Measure the time to perform input pin read/output pin write
320:void loop():measure:digitalWrite(D8, !digitalRead(D7)):566 ns
325:void loop():measure:digitalRead(D7)/digitalWrite(D8,0/1):346 ns
333:void loop():info:Measure the time to perform 8-bit serial data transfer
441:void loop():measure:digitalWrite(D9/D10):9 us
450:void loop():measure:digitalWrite(D9)/digitalToggle(D10):8 us
459:void loop():info:Measure the time to read analog pin
483:void loop():measure:analogRead(A0):112 us
For more details see the Cosa Benchmarks and Board definitions.
Cheers!