Shop OBEX P1 Docs P2 Docs Learn Events
Invalid variable name list or ???? — Parallax Forums

Invalid variable name list or ????

pmrobertpmrobert Posts: 675
edited 2014-03-28 05:52 in Propeller 1
I am a novice with Prop GCC and SimpleIDE. While doing some experimentation with passing values between cogs I found that use of "pw" as a variable name is not reported as an error but does prevent my very simple program from running as expected. I can declare a 'pw" and all is fine until I assign it a value. If I change it's name to "pw1" the code works as expected, toggling P1 at period = 1000 usec and pulsewidth 500 usec. I have included the simplest possible code that demonstrates the observed behavior. What have I overlooked? The specifics are Win7-64 dev machine, latest SimpleIDE (0.9.45), C3 board type, compiled as LMM Main RAM, C compiler type, optimization changes made no difference.
#include "simpletools.h"                      // Include simpletools library 

// For LMM cog 160 bytes declared as int
// Add enough for stack & calcs (50 bytes declared as int 
unsigned int stack[(160 + (50 * 4)) / 4];

// Declare volatile variables that will not be optimized
// to one variable for both cogs to share.
volatile unsigned int pin;
volatile unsigned int per;
volatile unsigned int pw1;
volatile unsigned int pw;

// Function prototype
void inj_drive(void *par);

// Cog 0 starts here
int main()
{
  pin = 1;
  //pw = 500; // This does not work
  pw1 = 500; // This works
  per = 1000;  
  // Pass function pointer, optional parameter value, 
  // stack array address, and the stack's size to cogstart.
  volatile int cog = cogstart(&inj_drive, NULL, stack, sizeof(stack));

  while(1);
}  



// Cog 1 starts executing code in this function in
// parallel.  Hooraaay!
void inj_drive(void *par)
{ 
    pwm_start(per);
    pwm_set(pin,0,pw1); // This works
    //pwm_set(pin,0,pw); //This does not.
  while(1);
}

Comments

  • Dave HeinDave Hein Posts: 6,347
    edited 2014-03-27 20:32
    The pwm library object contains a function named pw. Your pw variable is being linked to this function, and I think that the statement "pw =500;" is clobbering this function. Make your pw variable static and there won't be a conflict.

    EDIT: The simpletools.h file does contain function prototypes for some of the pwm functions, but it doesn't have a prototype for the pw function. If it did have a prototype the compiler would have caught the error. The pw function in pwm.c should probably have been made static so that it wouldn't conflict with user variables.
  • pmrobertpmrobert Posts: 675
    edited 2014-03-28 05:30
    Thank you, Dave. Would I be correct in assuming that If I stick to using static declarations for global vars that can only be modified in one cog or thread I should be good to go as far as further conflicts are concerned? Then volatile, forcing memory reads and writes, should be used if there are >1 thread or cog code sections that could modify it? What can I do to prevent such silent conflicts in the future - grep for the suspected conflicted name in the sources?
    -Mike
  • Dave HeinDave Hein Posts: 6,347
    edited 2014-03-28 05:52
    If your variable is only used within one file you can make it static. If you want to reference it from another file it cannot be static. You are correct that volatile must be used if another cog or thread reads or writes a variable. The volatile keyword tells the optimizer that the variable must be stored or read every time it is accessed.

    Silent conflicts are normally avoided by using unique names and using function prototypes, or making variables and functions static that don't need to be accessed from another file. The pmw library object should have either included a function prototype for pw in simpletools.h or made the pw function static. I would suggest contacting Parallax and letting them know about the problem.
Sign In or Register to comment.