login

Author Topic: Cannot compile existing Arduino project.  (Read 2770 times)

0 Members and 1 Guest are viewing this topic.

TeslaFan

Cannot compile existing Arduino project.
« on: November 03, 2011, 03:39:14 PM »
Hello!

Saw "Wiring" at Sparkfun and thought I'd see if I could switch to it from Arduino 22.

It tanks on this line of code, there's nothing special about it... I call strcpy_P dozens of times. This time is neither the first nor last. s_table[38] exists and will fit into pstr_text.

Code: [Select]
strcpy_P(pstr_text,(char*)pgm_read_word(&(s_table[38]))); //"com%dtype"

The error I get is:

Code: [Select]
CommManager.cpp:921: error: error: can't find a register in class 'BASE_POINTER_REGS' while reloading 'asm'
Then I get a list of:
Code: [Select]
CommManager.cpp:921: error: error: 'asm' operand has impossible constraints
that correspond to subsequent calls to strcpy_P but... not to ALL of them. And there are no syntax errors that I can see.

I could post more code, or even the entire program, but it's fairly large. The problem seems centered around PROGMEM, but doesn't start until after about 32 calls to strcpy_P.

bhagman

Re: Cannot compile existing Arduino project.
« Reply #1 on: November 03, 2011, 05:04:25 PM »
You might want to move over to Constant Types... this will make your code more portable too.

http://wiring.org.co/reference/

I'm guessing from the little bit I see, that you have a set of flash strings you want to use.

You can use ConstantString or, if you want to inline strings, just Constant.

http://wiring.org.co/reference/ConstantString.html

I'd have to see more of your code to see why it would be failing - that line of code doesn't look like it should cause a problem, but it might have to do with the #includes - I'm not sure.

TeslaFan

Re: Cannot compile existing Arduino project.
« Reply #2 on: November 04, 2011, 01:09:12 PM »
Ok, I replaced all the calls to strcpy_P to use the Constant data type.

That worked great with Serial.println(Constant("text to Print");

but fails with any form of strcpy() or sprintf()

Apparently, casting the Constant() as a (const char*) just returns a pointer to the object, not to the string it references.

If I can't use strcpy() or sprintf() with a text string stored in FLASH, then I'm dead in the water.

How do I use Constant() with strcpy() and sprintf()?

bhagman

Re: Cannot compile existing Arduino project.
« Reply #3 on: November 04, 2011, 03:30:09 PM »
Right.

We need to define more functions to work with ConstantTypes.

Try this:

Code: [Select]
void constantscpy(char *dest, ConstantString src)
{
  int i;
  for (i = 0; i < src.length(); i++)
    dest[i] = src[i];
  dest[i] = 0;
}

void setup()
{
  ConstantString s[3] = { Constant("One"),
                          Constant("Two"),
                          Constant("Three") };

  char d[40];

  Serial.begin(9600);
 
  constantscpy(d, s[2]);
 
  Serial.println(d);
}

void loop()
{

}

Also, you'll note that you can not globally define the ConstantString array.  You really shouldn't anyway.  :)

Try that in your code and let me know how it goes.


You could also go the route of using Strings.  It would be interesting to see how well it would work for you.

TeslaFan

Re: Cannot compile existing Arduino project.
« Reply #4 on: November 04, 2011, 06:36:15 PM »
thanks! That worked fine.

I like how the string definition gets placed where it's used, but I don't like that it's probably instantiating and destroying the wrapper object for the string every time it executes that function. Please tell me that ConstantString is totally static and doesn't dynamically construct anything.

I like how one IDE can support multiple architectures, but it seems to compile a lot slower. That might be an issue with 3rd party boards that have large libraries to support the board's unique features.

bhagman

Re: Cannot compile existing Arduino project.
« Reply #5 on: November 05, 2011, 04:12:57 AM »
ConstantString is constant. :)  Nothing dynamic at all.

We're working on the compile process now.  It should be much faster.

TeslaFan

Re: Cannot compile existing Arduino project.
« Reply #6 on: November 05, 2011, 05:30:39 PM »
Back to the original issue, looking at avr/pgmspace.h in both Arduino and Wiring, I notice that you're using a newer one, but the implementation for strcpy_P is the same. But I also noted that pgm_read_word() returns a variable cast as const.

So, changing:
Code: [Select]
strcpy_P(pstr_text,(char*)pgm_read_word(&(s_table[38]))); //"com%dtype"
in each instance where I used it to:

Code: [Select]
strcpy_P(pstr_text,(const char*)pgm_read_word(&(s_table[38]))); //"com%dtype"
fixed the issue nicely. This project is a library for a custom board based on the Arduino, so I want it to be compilable with any AVR environment, not just Wiring.

I also notice that my program is bigger (14K) with Wiring as opposed to 12.9K under Arduino. The available RAM is also lower, 697 bytes after the library is initialized in Wiring, as opposed to 752 bytes remaining after the same in Arduino.

Keep up the good work!