Sniff is a "Scratch-like" programming language that's designed to help Scratchers move gently from Scratch to more conventional languages. They can start writing programs, without having to learn a new language because Sniff is based on Scratch. They learn a little more about variables, compiling, syntax errors (!), and they can have fun controlling real hardware while they're doing it.

Tuesday, 27 January 2015

Getting started with Sniff and Propeller

Parallax are an odd company. Their products have many parallels with Arduino: They make and sell micro controllers targeted at education, with an emphasis on open source. However they've been doing it since the '90s (Arduino didn't start until 2005), and not only do they release complete systems, but they design and sell their own CPU's!

However if you're the UK/Europe you might not have come across them. That's probably because they're based in America, and as a small company their sales team don't have the resources to target overseas markets. They also have a policy of manufacturing in the USA where possible - which is very commendable if you're an American buying from an American company, but for the rest of us means that they're expensive imports.

Parallax's main CPU: the Propeller, has some impressive specs compared to other embedded controllers. It has EIGHT cores (which they call COGS), they're 32bit, and run at decent 80MHz. Each is supposed to deliver around 20Mips, so that gives us a potential 160Mips to play with. There are 32IO pins.

That sounds pretty amazing. Less impressive is the memory. Each COG has 2K of working RAM, and there's a central 32K of RAM. There's also 32K of ROM, but that's largely dedicated to code used by SPIN - Parallax's custom programming language, so we don't get much from it. 32K of core RAM doesn't sound too bad (The Uno has 2K), but the Propeller doesn't have built in eeprom like the AVR so boot code in the ROM loads your code from an external i2c eprom into the RAM to be able to run it. 32K of RAM has to hold both your code and data. To make it even worse the 32bit processor means code size is around 4 times as big as the AVR, so memory is very tight.

I've been tempted by the Propeller for a while and finally found an ebay seller who would ship me a quickstart board from America for about £20 including p&p.

It's about the size of an Arduino Uno, and again there are many similarities. It powers/programs over USB, and has the GPIO pins exposed on a header. One of the nice things here however is that the outputs are numbered 0-31, and appear in consecutive order on the header. No complex pin/port mappings to remember (I'm looking at you Raspberry PI!). Another nice touch is that there are 8 LEDS, with buffered drivers to you can go crazy flashing lights, but still use the same pins as inputs if you need to. The board runs at 3.3v, but each of the pins can source/sink a healthy 40mA (same as AVR - far more than most other CPUs), so you shouldn't have to worry too much about blowing it up.

There are no "special" pins on the Propeller. Whereas the AVR has UART's, I2C, PWM, and SPI hardware, the Propeller doesn't. One of the reasons for having those 8 cores is that you can simply code up those features and run them on a core, so you can have 8 UART's or 8 i2c busses if you want to. However there are some pins used by the boot loader: 30/31 are the serial connection, while 28/29 are i2c. In addition several of parallax's boards place an SDCard reader on pins 22-25, so it makes sense to use these for SPI where practical.

So onward to using Sniff on the board! Sniff compiles to C, and then uses the native C compiler to target specific platforms, so you'll need to install the Parallax "SimpleIDE" first. You should then be able to compile and run Sniff programs by simply typing "prop-sniff myprog.sniff". This puts the program into the eeprom, and starts it running.

One of the first things you'll probably find is that you run out of memory! You can improve things a bit by compressing the code, at the cost of a little performance: "prop-sniff cmm myprog.sniff". This uses the "compact memory model" as opposed to the "large memory model". You could also try the extended memory model, but to do that you'll need to refer to the parallax gcc documentation on configuring your board. Part of the problem is that the Propeller has no floating point hardware, so need to include maths libraries to do floating point - which Sniff depends on (its not efficient, but its easier to understand!). Simply using Sin() in increased the code size by 12K, so be careful, and check the code size. Hopefully this will improve with future versions of Propeller GCC.

Propeller-Sniff supports i2c, spi ( MosiPin=24; MisoPin=22;SckPin=23; SS for SDCARD=25) and gpio so most of the embedded examples should work. You can use PWM on any pins, but its currently limited to two channels. Serial works as expected, but is at 115200 baud.

Currently Sniff only uses a single cog to actually run your program (using 2 others for support functions), but in future release we should be able to make better use of more of the cores. Sniff is a parallel lanuage, so it will be really exciting to try and map it to a parallel chip.

Finally its really easy to build your own Propeller board! All you need is a Propeller (which comes in a 40pin DIL package!), a 5MHz Crystal , an i2c eeprom, and a few decoupling caps. You can program and power it using a 3.3 USB/ttl serial adapter: connect the DTR line to reset to synchronise when programming.

Bonus update: David Whale (@whaleygeek) just pointed me in the direction of Spinvent - purveyors of all things Propeller in the UK and Europe! Check them out! Their "demo board" looks pretty good value!

No comments:

Post a Comment