Accessing I/O Pins in Secondary Cogs, SimpleIDE
evil_aaronm
Posts: 11
This is the program I'm trying to run on my QuickStart. The array of functions works; starting a cog works. What doesn't work is turning on and off the appropriate pins in the supporting cogs. I'd like the main loop to run in primary cog - cog 0, typically - and a lighting routine to run in the supporting cog, until a new lighting routine is selected and the supporting cog is told to stop. If I run the lighting routines in the main cog, they work just fine. If I run the same code in a cog, it doesn't set the pins high or low. I used high() and low() and OUTA and DIRA; it doesn't want to work.
The pins in question are connected to an 8-channel relay controller that has an LED per channel whenever the relay is activated or turned off. The relays are getting nothing, when the lighting routines are run from cogs. Any suggestions what I'm doing wrong? I did see some other references, primarily to OUTA and DIRA, but I tried those, as you can see, and it didn't seem to help. Thanks for any assistance. As mentioned, I tried a number of things, so excuse the commented code here and there.
Aaron
The pins in question are connected to an 8-channel relay controller that has an LED per channel whenever the relay is activated or turned off. The relays are getting nothing, when the lighting routines are run from cogs. Any suggestions what I'm doing wrong? I did see some other references, primarily to OUTA and DIRA, but I tried those, as you can see, and it didn't seem to help. Thanks for any assistance. As mentioned, I tried a number of things, so excuse the commented code here and there.
Aaron
/** * This is the main LightController program file. */ #include "simpletools.h" // Include simple tools #define MAXBUTTONS 8 #define MAXLIGHTS 8 // just happens to match the number of buttons; doesn't have to #define BASELED 16 // pin 16 is the first output #define BASESWITCH 8 #define STACKSIZE 180 //#define STACKSIZE (sizeof long * (40 + 25)) #define COGS 7 // Globals -- if any //int buttons[MAXBUTTONS]; //static volatile int routineGo = 0; //unsigned int stack[COGS][STACKSIZE]; // needed for cog tasks unsigned int stack[STACKSIZE]; // needed for cog tasks static volatile int gInt = 0; // Declarations int init( void * ); void allOff( void * ); void allOn( void * ); void leftToRight( void * ); void cogFlashLight( void * ); void initialize( ) { int i; for ( i = 0; i < MAXBUTTONS; i++ ) low( BASELED + i); void *par; allOff( par ); } void allOff( void *par ) { int i; gInt += 1; //print( "allOff...\n" ); for ( i = 0; i < MAXLIGHTS; i++ ) { high( BASESWITCH + i ); // turn relay OFF (backwards) } } void allOn( void *par ) { int i; gInt += 1; //print( "allOn...\n" ); for ( i = 0; i < MAXLIGHTS; i++ ) { low( BASESWITCH + i ); // turn relay ON (backwards) } } void leftToRight( void *par ) { uint32_t m_mask; int i; //print( "In leftToRight...\n" ); //while( 1 ){ pause( 100 ); gInt += 1; for ( i = 0; i < MAXLIGHTS; i++ ) { //high( BASELED + i ); //low( BASESWITCH + i ); // turn light on (backwards) m_mask = (1 << (BASESWITCH + i)); OUTA &= ~m_mask; // low DIRA |= m_mask; pause( 100 ); // 10/th of a sec //low( BASELED + i ); //high( BASESWITCH + i ); // light off (backwards) OUTA |= m_mask; // high DIRA |= m_mask; pause( 100 ); } //} } void doNothing( void *par ) { while( 1 ) { pause( 1000 ); } } int main() // Main function { //print("\n\nHello!\n"); // takes up a core, so be warned int i; int routine = MAXBUTTONS - 1; // initially allOff int switched = 0; int cogRunning = 0; void (*funcList[MAXBUTTONS])(void *) = { allOn, leftToRight, doNothing, doNothing, doNothing, doNothing, doNothing, allOff }; void *par = NULL; //initialize(); //uint32_t m_mask; for ( i = 0; i < MAXBUTTONS; i++ ) { /* m_mask = (1 << i); OUTA &= ~m_mask; // should be "low" */ set_direction( BASESWITCH + i, 1 ); // output } cogstart( &allOff, par, stack, sizeof(stack) ); high( BASELED + routine ); // turn on allOff led while ( 1 ) { switched = 0; // reset on each loop; switches are changed if == 1 for ( i = 0; i < MAXBUTTONS; i++ ) { if ( input(i) ) { low( BASELED + routine ); routine = i; high( BASELED + routine ); switched = 1; pause( 350 ); // de-bounce break; // stop checking buttons } } if ( switched ) { // if any buttons were pressed if ( cogRunning != 0 ) cogstop( cogRunning ); cogRunning = 0; pause( 100 ); // wait for routine to stop /* allOff( par ); // turn all lights off, first if ( routine == 0 || routine == MAXBUTTONS -1 ) funcList[routine]( par ); // call selected function; no cogs else { //leftToRight( par ); */ cogRunning = cogstart( funcList[routine], par, stack, sizeof(stack) ); // } //printf( "gInt: %d\n", gInt ); } } // while( 1 ) return 0; // should never get here }
Comments
It won't work if yo set a bit to output in one cog and then try to drive that output from another cog.
Without looking to hard at your code I'm guessing that might be the problem.
Do have a look at the Propeller architectural block diagram in the manual to see why this is so. Specifically you will see that pins are driven by the ANDing of DIRA and OUTA bits from a COG.