Sunday, July 18, 2010

Propeller 1



The Parallax Propeller is an 8.00 USD uC that
has 8 cores (processors) inside of it. each one
can run at 100+Mhz and perform about 25,000,000
instructions per second (25mips) Each core has its
own video generating hardware!

This uC is so wonderfully different that when many first
see it they go through three stages in rapid succession. These
were pointed out by a forum member at Parallax (localroger)
WTF? OMG! LOL      After this initial reaction the new initiate
is usually seen stumbling away, mumbling to himself "No
interrupts!  No interrupts at all!"

And, is this chip ever rugged! Just look at this video.

Chip Temperature Test


There is a free open-source C compiler available for the
propeller and also a fine commercial C compiler. For BASIC
fans there is a great free compiler called PropBASIC. You
can also write a lot of great code using the Propeller's built-in
language called SPIN, this tiny interpreter is a compact, elegant
marvel. But for the fastest code ever you will benefit by writing
PASM code to run in the cog ram. You can have some cogs
running PASM, some running BASIC, some running C...it's
all up to you!

Here is an image of the chip's silicon wafer.


Notice the eight 512x32 ram locations across the top.
This ram is where your assembly language will be placed.
This is the fastest memory on the uC. You can page in
more asm code from the 32kb of ram you can see at the
bottom but there is a speed hit involved. Don't worry
though as a lot can be done in the 512x32 ram directly
available to each processor.

If you are not already a Parallax Propeller user you should
go the their website and download the free propeller manual.
(right click the .PDF link and choose "save link as" to download the manual)

You should also download the free Spin Tool software.
You will find it listed on the downloads page.

There is a free Propeller simulator called Gear. You should
get a copy so that you can use it to run your asm (Propeller assembly is usually called PASM) code without the need to
even have a Propeller chip. http://sourceforge.net/projects/gear-emu/


I will add to this post later...I'm busy now setting up
a large solderless breadboard for the first assembly
program for the Propeller chip. It will be a standard
turn on an LED "Hello World" type of asm program.

The circuit I have placed on the breadboard is directly
from the Propeller manual. It includes a 5.00Mhz crystal
and a 32kb eeprom chip. The eeprom is where the code
you write is stored. When the Propeller boots up it pulls
this code into the 32kb of ram you see at the bottom of
the image above
 


 Here is the large breadboard almost done. I just need
to add the crystal and an LED and resistor to it.







This is one of those really cheap Asian import breadboards
that you get on ebay for 16.00 shipping included. I put the
5v and 3.3v regulators on the bottom beneath the terminal
blocks that you see. I used a 7905 for the 5v and a 317T
with 2 Voltage setting resistors for the 3.3 there are four
rubber feet on the bottom of the breadboard that keep the
regulators from touching whatever surface you place it on.
This cheap setup is perfect for experimenting. Later on, after
I introduce the ATmega1284p uC I will add it to this breadboard
and show how these two uC's can work together to create some
truly powerful projects. These two chips working together are
an order of magnitude more powerful than something like an
Arduino! 





I am going to start off with some very minimal PASM
programs so that I can explain exactly what each and every
line does.

So, here is the smallest one possible. All it does is enter
and stay in an endless loop. So basically it does nothing :-)







And here it is as text, so you can copy it and paste
it into the Propeller Tool IDE.

PUB Main
  cognew(@A, 0)
DAT
A jmp #A

You can actually compile this in the Propeller Tool and
output it to the eeprom. The Propeller chip will run it..but
you won't really be able to tell since it does nothing...hmm

Here is another very tiny pasm program that actually turns on
an LED connected between Pin P0 and GND. (P0 is pin 1)
See the correct pin in the upper left corner of this image?







Here is an image of this tiny PASM program running in
a Propeller chip.




Please note the resistor between the pin and GND
it is used to limit the current to the LED and is very
important! (I think I used a 1k)

You may have noticed I did not place a crystal on the
breadboard. We are running the Propeller in what is
known as RCFAST mode using its internal RC clock
oscillator. It is fast enough for these early minimal PASM
programs.

Now here is a modification of the last PASM program. This
actually blinks the LED but it is blinking so very fast that the
human eye can not discern it. Below you can see pin1 toggling
rapidly in the Logic Probe pane of the Gear simulator. This is
what it would look like if you attached an oscilloscope to pin1
and gnd. 

I would post an image of this modified program running but the
image would look identical to the last one, except the LED would
be slightly dimmer since it is off half the time.




These tiny PASM programs are really rather pointless, but
they do serve the purpose of letting a newbie actually type in
some PASM code, compile it, and write it to the eeprom so they
can watch it run on a Propeller chip. This can actually be a big
hurdle sometimes and once you get past these initial baby steps
everything becomes much easier :-)

I am expecting readers to be able to figure out from the help
docs on the Parallax site how to use things like the propeller
Tool IDE and Gear. But if you do have any problems please
post a comment and I will try to clarify things.

Now I am going to explain, in detail, every single line of these
short PASM programs. I will also point out what is missing
from them that really should be there, things like comments
and more pertinent names for loops and labels. I made things
as short and sweet as possible in the hope it would not add
to the initial confusion we all have when looking at a new asm
dialect.

I have re-written the simple PASM program that toggles pin1
(P0)  This version is more or less the way a seasoned PASM
coder would write a simple program like this.








The first line
{{FastBlink.spin}}
Is just a comment that displays the name of the program.
Anything between {}or after a ' on a line is always a
comment, meant only for humans reading the program listing.
Comments are good! use them!

The Next Line is

CON

This marks the start of a Constant Block.
This is where you declare constants that the program
will use and where you set Propeller configuration
settings. We have no constants to define in our simple
program but we do have some configuration settings to
detail.

The next line

_clkmode = xtal1 + pll16x

Tells the compiler that we are choosing to use a crystal
for our system clock and that we want the pll (phase lock
loop) inside the Propeller to multiply the crystals frequency
by 16.

This next line tells the compiler that out chosen crystal is
a 5Mhz one so after multiplying by 16 our clock frequency
will be 80Mhz

 _xinfreq = 5_000_000

The propeller executes an instruction every 4 cycles so at
80Mhz it can run up to 20,000,000 instructions a second
in each of its 8 cogs.


The next line

PUB

Declares a code section that does some work and then returns
some result value. Our simple program actually has no useful
value to return so it returns a zero. There can be several PUB
blocks within your program (Object)

cognew(@Loop,0)

This line starts a new processor (cog) and loads it with 2KB
of hub ram. The location in hub ram to fetch the code from
is located by the label "Loop" ...the compiler knows where
this 2kb of data is to be found...it always fetches exactly 2kb
starting at the location of label "Loop"

In this instance the 2kb of program data will be loaded into
cog1, cog0 is running the program we have written above.
All our program really does is cause the tiny PASM program
contained in the DAT section to be loaded into cog1 and run.
Once cog1 is running our tiny pin toggling program the SPIN
program in cog0 is finished.

Just keep in mind that this code

{{FastBlink.spin}}

CON
 _clkmode = xtal1 + pll16x
 _xinfreq = 5_000_000


PUB Main
{start a new cog to run our PASM code}

 cognew(@Loop,0)   'launch cog, fill the cog ram with data from hub ram

Is high level interpreted SPIN language running in cog0 and
that what it does is cause this code

Loop mov dira, #1  'set pin1 to be an output
           xor outa, #1  'this flips pin1 each time it runs (1 becomes 0) (0 becomes 1)
           jmp #Loop     'loop forever by jumping back to A

To be placed into the ram of cog1 and run.

Now I realize that it is hard at first to get your head around the
idea that there are actually 8 separate processors running at once.
You write a high level program using SPIN in one processor to
load other programs into other processors and start them running.
It's just a totally different world than how things are done on a
single core uC like an AVR. Once this all sinks in you will simply
gasp at the power and the possibilities! :-)

Just hang in there, enlightenment will come suddenly.

DAT

The DAT declares a data block.
This code section is where all of your PASM code goes.
Various types of data also get stored in here. This all gets
loaded to cog ram when the 2kb gets pulled in from hub memory
to the cog ram space by this command  cognew(@Loop,0)

This next line is pretty self explanatory.

org 0         'make the code located at Loop begin at cog mem address 0

It is not strictly required in this particular instance, but I
strongly suggest always using it, even in a simple PASM
example such as this.

Next we have this line.

Loop mov dira, #1  'set pin1 to be an output

dira is the direction register for port A
It is 32 bits long and holds the direction states for the 32
I/O pins of the Propeller chip. If a particular pin has a
corresponding 1 in its dira bit then it is set up as an output.
If 0 then it is an input. The I/O pins are numbered from 0-31
or P0-P31. All cogs have access to the I/O pins at all times.
Just how this works will be made clear in a future blog posting.

The label at the start of that line

loop

could actually have been placed on a line of its own, it would
have made no difference whatsoever, it's just a label that the
compiler takes notice of to designate a location in memory.

Loop
mov dira, #1  'set pin1 to be an output

Works just the same.

The # mark before the number 1 in that line is VERY
important, without it the code means something very different
than it does if it is included.

mov dira, #1  sets the dira register to
00000000_00000000_00000000_00000001

whereas
mov dira, 1 would set the dira register to whatever value
might be held in cog memory location 1.  This is a very
different effect indeed!

Next the line

xor outa, #1  'this flips pin1 each time it runs (1 becomes 0) (0 becomes 1)

This flips the value of pin1 each time the line is executed.
xor is a very handy way to accomplish bit flipping. We will
look further at it at a later time. Again the # is very important
and without it you would be xor'ing the outa register with whatever
value was in memory location one instead of the number 1.
OUTA is the output register for PortA. Writing a 1 to a bit set
as an output on portA will cause that pin to go to VCC, setting
it to zero sets the port to gnd.

Finally, our last line is

jmp #Loop     'loop forever by jumping back to Loop

This just causes our small program to continuously jmp
back up to the line designated by the label "Loop"
Again, the # is very important!

Here is a screenshot of our toggling program now
running at 20,000,000 ips and toggling pin1 (P0) millions
of times a second. Notice that the Gear simulator can't really
keep up at that rate and the display is just a band. On a real
scope you would still see a nice square wave pattern.
















Stay tuned for more Propeller assembly posts, but first a
couple of new AVR posts :-)





















1 comment:

  1. Thanks for sharing your thoughts. I truly appreciate your efforts and I am waiting for your next write ups thanks once again.

    ReplyDelete