PAGES

Sunday, August 18, 2013

Talking Cogs

Parallax code award to Jim Coleman
DESIGNING WITH THE PROPELLER CHIP
COG LOADER: LOADING COGS IN A PROPELLER CHIP

ONE of the most important functions in the Parallax Propeller chip is the ability to talk between various processors. There are numerous ways to write programs to do this, some which use a fractional number of the chip's eight cores and some programs which use the full load of cores. Some examples are spartan and some are more comprehensive and involved, and some will contain embedded comments while others have none. In these examples, cores are called Cogs, the naming convention conceived by the Propeller designer, Charles "Chip" Gracey III, who is also the founder of Parallax Company.

Let's take a look at these programming examples that can talk with Cogs. First up, we honor and spotlight Jim Coleman's award, Object of the Week, issued by Parallax this week. The program is found at the Obex and can be downloaded here. The program is fantastic in documentation and includes the use of PBASIC style language for the Propeller chip. Next up is this sample found at QuickStart 5: Multiple Cogs. It loads up three cogs with tasks that all run at the same time. The code is provided below.

CON
  _clkmode = xtal1 + pll16x         'Establish speed
  _xinfreq = 5_000_000              '80Mhz
OBJ
  led: "E555_LEDEngine.spin"        'Include LED methods object
VAR
  byte Counter                      'Establish Counter Variable                                   
  long stack[90]                    'Establish working space
PUB Main
  cognew(Twinkle(16,clkfreq/50), @stack[0])    'start Twinkle cog 1
  cognew(Twinkle(19,clkfreq/150), @stack[30])  'start Twinkle cog 2
  cognew(Twinkle(22,clkfreq/100), @stack[60])  'start Twinkle cog 3
PUB Twinkle(PIN,RATE)                  'Method declaration
  repeat                               'Initiate a master loop 
    repeat Counter from 0 to 100       'Repeat loop Counter
      led.LEDBrightness(Counter, PIN)  'Adjust LED brightness
      waitcnt(RATE + cnt)              'Wait a moment     
    repeat Counter from 100 to 0       'Repeat loop Counter
      led.LEDBrightness(Counter,PIN)   'Adjust LED brightness
      waitcnt(RATE + cnt)              'Wait a moment


Next, move onto Methods & Cogs and follow the details of the Spin code shown below.

'' File: CogStartStopWithButton.spin
'' Launches methods into cogs and stops the cogs within loop structures that
'' are advanced by pushbuttons.
VAR
  long stack[60]
PUB ButtonBlinkTime | time, index, cog[6]
 repeat
   repeat index from 0 to 5
     time := ButtonTime(23)
     cog[index] := cognew(Blink(index + 4, time, 1_000_000), @stack[index * 10])
   repeat index from 5 to 0
     ButtonTime(23)
     cogstop(cog[index])
PUB Blink( pin, rate, reps)
  dira[pin]~~
  outa[pin]~
  repeat reps * 2
    waitcnt(rate/2 + cnt)
    !outa[pin]
PUB ButtonTime(pin) : delta | time1, time2
  repeat until ina[pin] == 1
  time1 := cnt
  repeat until ina[pin] == 0
  time2 := cnt
  delta := time2 - time1


PROPELLER LINK TREASURY
Parallax Inc.
http://www.parallax.com/
OBEX
http://obex.parallax.com/object/61
QuickStart 5: Multiple Cogs
http://www.parallaxsemiconductor.com/quickstart5
Propeller Education Kit Labs
http://www.parallax.com/go/pekit
Comstock's
https://www.comstocksmag.com/batteries-not-included 
Propeller <-> PC Terminal Communication (forums)
http://forums.parallaxinc.com/forums/?f=25&m=341494&g=341494#m341494
Debug LITE for the Parallax Serial Terminal (forums)
http://forums.parallaxinc.com/forums/default.aspx?f=25&m=348893
Measure Resistance and Capacitance (forums)
http://forums.parallaxinc.com/forums/default.aspx?f=25&m=335407
Transmit Square Wave Frequencies (forums)
http://forums.parallaxinc.com/forums/default.aspx?f=25&m=343747
EEPROM Datalogging and I2C (forums)
http://forums.parallaxinc.com/forums/default.aspx?f=25&m=219237
Servo Control (forums)
http://forums.parallaxinc.com/forums/?f=25&m=197069&g=197069#m197069
PEKbot (forums)
http://forums.parallaxinc.com/forums/default.aspx?f=25&p=1&m=174962
Methods & Cogs
http://nagasm.org/ASL/Propeller/printedPDF/MethodsAndCogs-v10.pdf