energia / Energia

Fork of Arduino for the Texas Instruments LaunchPad's
http://energia.nu
Other
794 stars 672 forks source link

StellarPad: sbi() + cbi() 4.6x Faster than digitalWrite() #246

Closed rei-vilo closed 11 years ago

rei-vilo commented 11 years ago

sbi() and sbi() are 4.6x faster than digitalWrite() on the LaunchPad Stellaris.

Is there a way to improve the implementation of digitalWrite()?

Results are:

Stellaris on embedXcode

digitalWrdigitalWrite() -() sbi() Comparison Times in ms digitalWrite() 1500 cbi() sbi() 325

Stellaris on Energia

digitalWrite() - cbi() sbi() Comparison Times in ms digitalWrite() 1500 cbi() sbi() 400

sbi() and sbi() require functions that are not implemented with the MSP430, namely portBASERegister and GPIOPinWrite.

The measures were provided by the following basic sketch, compiled with Energia or embedXcode:

// Core library for code-sense
#include "Energia.h"

#define LOOPS 1000000
#define _pin RED_LED
#define portOutputRegister(x) (regtype)portBASERegister(x)
#define cbi(reg, mask) GPIOPinWrite(reg, mask, 0)
#define sbi(reg, mask) GPIOPinWrite(reg, mask, mask)
typedef volatile uint32_t regtype;
typedef uint8_t regsize;

uint32_t chrono;
regtype _port;
regsize _bit;

void setup()
{
  Serial.begin(9600);
  delay(10);

  // put your setup code here, to run once:
  Serial.println("digitalWrite() - cbi() sbi() Comparison");
  Serial.println("Times in ms");

  Serial.print("digitalWrite() ");
  pinMode(_pin, OUTPUT);

  chrono = millis();
  for (uint32_t i=0; i<LOOPS; i++) {
    digitalWrite(_pin, HIGH);
    digitalWrite(_pin, LOW);
  }
  chrono = millis() - chrono;

  Serial.println(chrono, DEC);
  delay(1000);

  Serial.print("cbi() sbi()    ");
  _port   = portOutputRegister(digitalPinToPort(_pin));
  _bit    = digitalPinToBitMask(_pin);

  chrono = millis();
  for (uint32_t i=0; i<LOOPS; i++) {
    sbi(_port, _bit);
    cbi(_port, _bit);
  }
  chrono = millis() - chrono;

  Serial.println(chrono, DEC);
}

void loop()
{
    ;
}
cosmok82 commented 11 years ago

Do you have some details on cbi sbi and analogRead to set a fast reader too? In particular, about the prescaler setting, How I have to perform the settings for a specific value?

rei-vilo commented 11 years ago

@cosmok82

This is an issue tracker, not a discussion forum.

The issue is solely about digitalWrite(), not analogRead().

For discussion, please consider the thread http://forum.stellarisiti.com/topic/650-stellarpad-sbi-sbi-46x-faster-digitalwrite/ at the Stellarisiti forum.

cosmok82 commented 11 years ago

Could you read the use of cbi sbi here bit.ly/10KmoiD ? If I'm not mistaken the use made ​​of it is to read an analog signal and not to write one! But I'm not very expert in AVR code (in case of arduino's specifics... especially as written in the xoscillo code), I could be wrong.

rei-vilo commented 11 years ago

See #256