Print from a Different Core

Up to now, all of our example programs that use print statements have done so from the main routine, which starts executing in cog 0.  This quick lesson shows you how to print from different cores. 

The trick is that only one core at a time can use the SimpleIDE Terminal connection, and must be closed before being opened in another.  Sort of like passing the talking stick or speaker's staff from one person in the circle to another at a meeting.   



On the Activity Board (original or WX version), the Propeller chip's P30 and P31 pins are pre-wired to the USB port for programming and communication with the SimpleIDE Terminal. Any setup with these connections will work.


Test Code

The example program prints two messages from two different cores.

  • Open Print from Other Cores.side from ...Documents\SimpleIDE\Learn\Examples\Multicore. 
  • Click the Run with Terminal button.
  • Verify you see a message in the SimpleIDE Terminal like the one shown below.


How it Works

The program includes the simpletools.h library, which also makes the simpletext library's functions available, including print, simpleterm_close, and simpleterm_open.

#include "simpletools.h"                    // Library include

void other();                               // Forward declaration
int *otherCog;                              // Global pointer for cog_run return

The main routine, which is running in cog 0, begins with a print statement that displays the text string in the SimpleIDE Terminal.  Next, simpleterm_close releases the terminal connection for this core, so it can be opened by a different core.  After that comes the cog_run function call to launch the other function in a new core, and the return value is stored in otherCog.

int main()                                  // Main function
  print("Cog 0 has the Talking Stick first... \n");   

  simpleterm_close();                       // Close SimpleIDE Terminal for this core
  otherCog = cog_run(other, 128);           // Run other function in another core

Last comes the other function definition and its code block; this is the code running in a different core. The first instruction opens the SimpleIDE Terminal for this core, so it can display the string in the print statement that follows.  After printing, the next line closes the SimpleIDE Terminal. The last line uses cog_end and the otherCog pointer to shut down the core.

void other()                                // other function definition
  simpleterm_open();                        // Open SimpleIDE Terminal for this core
  print("...and now the other cog has it. \n");                           

  simpleterm_close();                       // Close SimpleIDE Terminal
  cog_end(otherCog);                        // Shut down this core  


Did You Know?

Printing Values Takes Stack Space  — Printing a simple text string from another core doesn't require much stack space. You could reduce the 128 in the example above all the way down to 10, and it might still run .  But, if you want your launched core to check the value of global variables, use them in operations with local variables, and print the results, you are going to need an adequate stack!  We reccommend 128 as a minimum. If your custom print statements do not appear with the as desired, try increasing the stack space in that core's cog_run function call.

Making the Serial Connection Takes Time — If you find that not all of your print statements are showing up as they should, try adding pause(100); right after each instance of simpleterm_open. Your particular computer might need a moment to make the serial terminal connection.

Not Just print! See simpletext.h — There are many other functions in this library that interact with the SimpleIDE Terminal. So, like print, they can be used from one core at a time, after closing and opening the terminal as shown in this lesson.  These include printi, scan, scani, and anything starting with put or get.  See the library's documentation for details.


Try This

Let's expand this example program to print from a third core. First, we'll need to add another function (let's just name it "another") with its own print statement.  Then, we can launch it from the other function; in effect passing the Talking Stick.

  • Click Save Project As button, and rename name the project Print from Other Core (Try This).
  • Copy the other function and paste it at the end of the program. Rename it "another" and change the text string in the print statement.
  • Add a forward declaration for the another function, and a global pointer for it named *anotherCog.
  • In the other function, add a cog_run function call to launch another, storing the result in anotherCog. It needs to go after simpleterm_close, but before cog_end.
  • Finally, back in the main routine, increase the stack space in the cog_run function call, since we've made additions to the other function's code block.

Here's the SimpleIDE Terminal output:


Your Turn

Let's have our three cores access and update a global variable, perform an operation on it, and print the updated value.  You will need to:

  • Add and initialize a global volatile int variable.
  • Make sure the stack space for each cog_run function call is at least 128.
  • Add a statement to run in each core that updates the global variable.
  • Update each print statement to display the updated value of the global variable.

Here's an example with a global variable initialized to 0 and increased by one in each core.