Pages: [1] 2   Go Down
Author Topic: New fast digital I/O and Software SPI libraries  (Read 8296 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Edison Member
*
Karma: 63
Posts: 1631
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I posted new fast I/O libraries as DigitalPinBeta20120804.zip http://code.google.com/p/beta-lib/downloads/list.

The libraries support standard 168/328 Arduino, Mega, Leonardo, Teensy, Teensy++, and Sanguino.

The DigitalPin class provides very fast inline functions. DigitalPin is a template class and pin numbers must be specified at compile time. 

For 328 pins and low address Mega pins read(), toggle(), and write() execute in two cycles or 125 ns for a 16 MHz CPU.  This is about thirty times faster than digitalWrite() which executes in about 4 usec.

The main member functions for the DigitalPin class are:
Code:
void config (bool mode, bool level);
void high ();
void low ();
void mode (bool pinMode);
bool read ();
void toggle ();
void write (bool value);

The library also contains these static inline functions similar to digitalRead()/digitalWrite().  Pin number must be a constant.
Code:
static bool fastDigitalRead (uint8_t pin);
static void fastDigitalToggle (uint8_t pin);
static void fastDigitalWrite (uint8_t pin, bool level);
static void fastPinConfig (uint8_t pin, bool mode, bool level);
static void fastPinMode (uint8_t pin, bool mode);

There is also a Software SPI class that runs at about 2 MHz.  It is a template class with compile time pin numbers and SPI mode.  Modes 0 - 3 are supported MSB first.  LSB first would be easy to implement.

The member functions are:
Code:
void begin ();
uint8_t receive ();
void send (uint8_t data);
uint8_t transfer (uint8_t txData);

There is also a class, DigitalIO, with run-time pin numbers.  It is much slower than the  above DigitalPin class but is 3 - 4 times faster than the Arduino digitalRead()/digitalWrite() functions.
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 69
Posts: 2166
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Looks good, I have been using your previous version in one of my libraries for quite some time now.

Is the main difference ( apart from your new SPI compatibility ) the fact that the underlying implementation no longer uses a template interface. It looks a lot less complex without the non-type template declarations, however at first glance it seems mostly the same code within.

Its interesting to see we both separated the port manipulators into non-member functions and left the class as a simple wrapper. My motivation for this was to have a macro which uses __builtin_constant_p to decide weather to use Arduino standard digitalWrite or its static counterpart. The macro I tried failed, so being quite unfamiliar with __builtin_constant_p and why it failed rather than returning a value has left me with more reading to do. Maybe you have a definitive answer for this.

The DigitalIO class is nice. A clear speed / size trade-off, something I would be happy to pay in a dynamic but critical routine.
Logged


0
Offline Offline
Edison Member
*
Karma: 63
Posts: 1631
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't like macro implementations and macros are not needed to use to use digitalWrite() or another function when the pin number is not a constant.

I produce an error when pin number is not a constant because most professional embedded programming standards consider having a function change execution speed by a factor of 30  when an argument is a constant to be poor practice.

You could do this to have a fast/slow digital write:
Code:
static inline __attribute__((always_inline))
void fastSlowWrite(uint8_t pin, bool value) {
  if (__builtin_constant_p(pin)) {
    fastBitWriteSafe(pinMap[pin].port, pinMap[pin].bit, value);
  } else {
    digitalWrite(pin, value);
  }
}

This compiles as 692 bytes and write is fast.
Code:
#include <DigitalPin.h>
const uint8_t PIN = 13;
void setup() {
  pinMode(PIN, OUTPUT);
  fastSlowWrite(PIN, 1);
}
void loop() {}
This compiles as 872 bytes and write is slow.
Code:
#include <DigitalPin.h>
uint8_t var = 13;
void setup() {
  pinMode(var, OUTPUT);
  fastSlowWrite(var, 1);
}
void loop() {}

I made DigitalPin a wrapper since it insures the static and class functions behave the same.  I almost used separate implementations since the template insures the pin number is a constant an much of the static function stuff is not needed.
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 69
Posts: 2166
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, I agree macros shouldn't be provided for client use, I just used one to do a quick test.

Looking at your function I realise my error was not due to __builtin_constant_p, but the value passed to it. My fast digitalWrite is a template function and the compiler cannot instantiate it when using a variable pin.

As for combining the two being bad practice, would it not be suitable if users of the code are made fully aware of the behaviour. As already existing functionality is being combined I see it more of an extension or more flexible to users preference. On the other hand, some things I prefer a more explicit approach.

If distributed code is written using the function you posted above, it would not tie the end user into one particular mode.
Logged


0
Offline Offline
Edison Member
*
Karma: 63
Posts: 1631
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Embedded programming standards don't have statements like:
Quote
would it not be suitable if users of the code are made fully aware of the behavior.

To often one user's code is another user's library and stuff like this gets lost and tragedies happen.

Nothing really matters with Arduino since it is a case study in bad practice and I often follow with bad practice in my Arduino libraries and programs since they are just toys.  I have some limits with Arduino and a factor of 30 in execution speed was too much.

Here is a typical coding standard for a critical system, The F35 Joint Strike Fighter http://www.jsf.mil/downloads/documents/JSF_AV_C++_Coding_Standards_Rev_C.doc.
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 69
Posts: 2166
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Embedded programming standards don't have statements like:
Quote
would it not be suitable if users of the code are made fully aware of the behavior.

Agreed, they probably don't. But what they should have is technical documentation specifying all the client needs to know. Reading the documentation is hopefully part of most design standards, certainly in mine anyway. I assume you are pointing out that this expectation isn't always met on large scale systems or projects involving many people.

And as much as I want to argue I guess I can't overcome the fact that someone will eventually skip the important information, causing a bad day.

I'll download libre office so I can view that doc. Might be some good insights in there.
Logged


nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8501
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Nothing really matters with Arduino since it is a case study in bad practice
smiley

Here's a PDF of the JSF document for those who don't want to download a DOC

http://www.robgray.com/temp/JSF-AV-rules.pdf

and the similar JPL standard

http://www.robgray.com/temp/JPL_Coding_Standard_C.pdf

As an example of the Arduino code, from digitalWrite()

Code:
uint8_t port = digitalPinToPort(pin);
...
if (port == NOT_A_PIN) return;

digitalPinToPort() is just a macro that indexes into an array, there is no bounds checking and any non-zero value satisfies the if test. So

Code:
pin = 1000;
...
digitalWrite(pin, HIGH);

Probably actually works (I don't have any hardware to test it right now) but would almost certainly not do what you expect.

To be fair for what the Arduino is this may be a reasonable trade off between reliability and speed, but we often get "Can I use an Arduino in a commercial product?" style questions and while legally you can, in good conscience I don't think we should advise in the affirmative on technical grounds.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And what's wrong with the technical ground of Arduino?
Logged

first attempts in Arduino support

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8501
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm referring to the lack of defensive coding techniques used in the standard Arduino libraries. My digitalWrite(1000, HIGH); example should not be allowed to execute at run time. The error should be trapped. And IIRC there are many more examples.

I appreciate that error trapping make the program slower and larger and it's not required for the average Arduino user anyway so I'm not having a go at the decisions that were presumably made in this regard when the code was written with the "wearable LED/installation art" audience in mind.

But I don't believe the Arduino libraries should be used in a commercial or otherwise critical application.

______
Rob
« Last Edit: August 13, 2012, 06:35:33 pm by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

SF Bay Area (USA)
Online Online
Tesla Member
***
Karma: 132
Posts: 6746
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I assure you that there are many commercial projects that use coding standards no more rigid than the Arduino core.
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8501
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I have no doubt of such  smiley-eek

_____
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not an expert on the matter, but I thought that the boundary conditions of the Arduino, mainly limited CPU power and limited memory, make it less suitable for very defensive coding and more suitable for (mild) programming by contract.

While it certainly does not meet the requirements of keeping an Joint Strike Fighter in the air and other life-and-death critical applications, think it is usable in may other commercial environments. Besides, there is the enterprise code a programmer would like to write, and there is the code he writes under time constraints, changing requirements etc. As said, compared to some commercial code, the Arduino libraries aren't always that bad  smiley

Of course, this is all pretty off-topic...
« Last Edit: August 22, 2012, 04:07:13 pm by mrTee » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

fat16lib,
This library is just the idea that I needed for my project as I have been trying to make my own bit-blast SPI protocol, as I need to use pins other than 50,51,52,53 for my SPI calls.
I have my own crude bit-blast working, but I would like to take advantage of other device libraries, for example the MCP23S17 library, that are written for the SPI library, but I want to shift SPI over to pins 32, 34,36,38 on my Mega2560.

However, I'm having problems getting any signal from the GPIO pins on my Mega2560 using the SoftSPI library, or demo.
The below modified pin assigned demo compiles just fine with no errors, but when I probe pins 38 (CLK) or pins 36,34 I see nothing.  Any ideas?

Also, I have a basic question about the SS line...
The standard SPI library includes the use of SS pin to select/deselect the slave, but I don't see the use of SS in this library, or example.  Should the spi.transfer() calls be bounded calls to pull SS LOW,HIGH?
Why was the use of SS not included?

Code:
// scope test for development - assumes 328 processor
#include <DigitalPin.h>
#include <SoftSPI.h>

const uint8_t SOFT_SPI_MISO_PIN = 36;
const uint8_t SOFT_SPI_MOSI_PIN = 34;
const uint8_t SOFT_SPI_SCK_PIN  = 38;
const uint8_t SPI_MODE = 0;

SoftSPI<SOFT_SPI_MISO_PIN, SOFT_SPI_MOSI_PIN, SOFT_SPI_SCK_PIN, SPI_MODE> spi;

int test;
void setup() {
  Serial.begin(9600);
  spi.begin();
  while(1) {
    Serial.println("Enter:");
    Serial.println("R - Receive");
    Serial.println("S - Send");
    Serial.println("T - Transfer");
    while ((test = Serial.read()) <= 0) {}
    test = toupper(test);
    if (strchr("RST", test)) break;
    Serial.println("Invalid entry");
  };
  Serial.print("Starting test ");
  Serial.println((char)test);
}
void loop() {
  if (test == 'S') spi.send(0X55);
  if (test == 'R') {
    Serial.println(spi.receive(), HEX);
  }
  if (test == 'T') {
    Serial.println(spi.transfer(0XAA), HEX);
  }
  delay(10);
}
Logged

0
Offline Offline
Edison Member
*
Karma: 63
Posts: 1631
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How are you looking at the pins?

If you connect pin 34 to pin 36 and run the "T - Transfer" command does "AA" print?

If you set pin 36 to GND, does the "R - Receive" command print 0?  If pin 36 is set to 5V, "R - Receive" should print FF.

SS is needed for hardware SPI to work.  SS must be set to an output in master mode so it is often used as chip select.

For software SPI in master mode no SS signal is needed.  You can use any pin for chip select.
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17293
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm referring to the lack of defensive coding techniques used in the standard Arduino libraries. My digitalWrite(1000, HIGH); example should not be allowed to execute at run time. The error should be trapped. And IIRC there are many more examples.

I appreciate that error trapping make the program slower and larger and it's not required for the average Arduino user anyway so I'm not having a go at the decisions that were presumably made in this regard when the code was written with the "wearable LED/installation art" audience in mind.

But I don't believe the Arduino libraries should be used in a commercial or otherwise critical application.

______
Rob

Could you explain that better, as I don't see the case. The function is really  digitalWrite(int_variable, HIGH); so the compiler can have no knowlege of what the run time variable value might become. And any run time check would seem to mean some kind of actual 'overhead' or 'supervisory' code running, which is not a C/C++ property as far as I know, and would just suck up more resources from my poor overworked underpaid AVR 8 bitter? Can't it just be on me to not make such stuipid errors in the first case?

OK, on rereading your post, you are pretty clear you aren't saying it's something aimed or needed by me, just commerical or safety critical type applications.  Never mind. smiley-wink

Lefty
« Last Edit: October 04, 2012, 11:01:26 pm by retrolefty » Logged

Pages: [1] 2   Go Up
Jump to: