cogs, threads, I need some help
spinbob
Posts: 27
I'm just stuck here trying a bunch of things and nothing works. I have 8 spin files that I converted to C using spin2cpp.exe and the code doesn't run correctly. I need some clarification. Does the converter convert code and it will run? This text is from a post I posted in General topics.
****
First a few details. Program written in spin, converted to C with spin2cpp. 12K of code compiles without errors. Our unit (custom PCB) has a display on a I2C bus that I use for feedback as to code running. Here is the problem:
In the Main() I do this:
Call a spin_start function in another C file to init a new cog to gather data. In the new cog I init a couple variables then sit in a while(1) loop reading the A/D and storing values. This is where the trouble lies. If I remove the while loop then the rest of the code in my Main works fine but with the while loop in place the unit hangs up, no display. All this code runs fine in it's spin form.
****
Can someone give me a rock solid example of how to init a new cog and run the code using C in Simple IDE. Is it treads, cogs, asm....
Thank you.
****
First a few details. Program written in spin, converted to C with spin2cpp. 12K of code compiles without errors. Our unit (custom PCB) has a display on a I2C bus that I use for feedback as to code running. Here is the problem:
In the Main() I do this:
Call a spin_start function in another C file to init a new cog to gather data. In the new cog I init a couple variables then sit in a while(1) loop reading the A/D and storing values. This is where the trouble lies. If I remove the while loop then the rest of the code in my Main works fine but with the while loop in place the unit hangs up, no display. All this code runs fine in it's spin form.
****
Can someone give me a rock solid example of how to init a new cog and run the code using C in Simple IDE. Is it treads, cogs, asm....
Thank you.
Comments
I would recommend avoiding pthread functions unless there is a good reason to use them. With pthreads we need to worry too much about yielding time like on single core processors and functions like waitcnt don't play very nicely with pthreads. That being said, pthreads with CMM/LMM memory models can do SMP processing on Propeller which is an advantage in the right hands.
int stacksize1 = sizeof(_thread_state_t)+sizeof(int)*25; int *stack1 = (int*) malloc(stacksize1);
I modified it to run three cogs and it just ran them all in one I think.
When I added :
int stacksize1 = sizeof(_thread_state_t)+sizeof(int)*25;
int *stack1 = (int*) malloc(stacksize1);
int stacksize2 = sizeof(_thread_state_t)+sizeof(int)*25;
int *stack2 = (int*) malloc(stacksize2);
int stacksize3 = sizeof(_thread_state_t)+sizeof(int)*25;
int *stack3 = (int*) malloc(stacksize3);
It worked I believe in three seperate cogs.
Can someone point me in the right direction to understand the stack
and how each cog relates to it? Maybe in the forums.
I am wondering if this is poor coding and maybe I am using more
memory than need be, etc.
Full code below:
(ps I don't know if the milliseconds are right in my debug, I just guessed based on clkfreq/20 being 50....
It is possible that all cogs in your example are using wait_time=0, and that would make it look like one cog is doing all the work.
All three leds on pin 13..14 are flashing at different rates.
I shortened the wait time to:
wait_time1 = CLKFREQ/1000; wait_time2 = CLKFREQ/2000;
wait_time3 = CLKFREQ/4000;
and put a scope on the pins (attached).
I tried putting some debug in the loop:
for(;;) {
OUTA |= (1 << pin); // set pin high
nextcnt = waitcnt2(nextcnt, wait_time);
OUTA &= ~(1 << pin); // set pin low
nextcnt = waitcnt2(nextcnt, wait_time);
}
I suppose that is the way I need to prove
that different cogs are actually running different
pieces of code.
But it didn't work. Will try again.
I am trying to figure out the relation ship between
starting a cog, the cog id and the memory allocation that is
in the code. I don't see anything stating what
cog id to start, and all the allocation statements look identical.
I was hoping there was some explanation of this inter-working
that I could find.
Programming and Customizing the Multicore Propeller Microcontroller.
hopefully that will shed some light on this...
I ran the blinkwithcogs.spin demo. In SPIN they all run at the same
time on three leds.
When I ran it through spin2cpp, it works but it runs them sequentially.
One cog and one led run, then stop and then the next cog and led run.
I don't see in the code, why the c version doesn't start all the cogs
at the same time.
This appears to be a case where spin2cpp fails. The blinkers are not getting enough stack because of the methodology. Your first post's example is better, although wrong as I mentioned in my previous post.
Would you also tell me why the wait time needs to be specified before the cogstart.
Thanks for your help earlier.
So that the value is guaranteed to be correct before the function starts? The main program runs in one COG, and thus one statement/instruction generally precedes another unless they are run in different cogs. A new COG that depends on some data should have the data set first.