Wednesday, February 20, 2013

Arduino Due Hardware SPI

The Arduino documentation for the Due board has extended library calls for hardware SPI, handling up to three devices.  The extended documentation is good but not overly complete.  In learning the device, I have found a number of things that may clarify the library in practical use.

The library for hardware SPI is SPI.h.  It defines different functions than the AVR Arduino library.  This is due to expanded capabilities described below.  If you port code from old programs, you will have to edit calls to new syntax.

Pins: Hardware SPI MISO, MOSI, and SCK pins are only on the SPI header (not to be confused with the ICSP header which is different on the Due).  See below.
What this does not show are the digital pin numbers.  They are not needed for SPI library calls but will be used later so we should know them.  The Arduino IDE has predefined variables MISO, MOSI, and SCK (something not readily known or documented).  If you use a short sketch to print them to the serial monitor, they are: MISO - Digital Pin 74, MOSI - Digital Pin 75, and SCK - Digital Pin 76.

Instead of one Chip Select (SS) pin, the Due supports three.  These are hardware Digital Pin 10 (SPI-CS0), Digital Pin 4 (SPI-CS1) and Digital 52 (SPI-CS2).  These pin values are used in the Due SPI library calls to determine which hardware SPI device you are addressing (up to three at the same time).

Clock settings: different from the AVR Arduinos as well.  The function call is:
  SPI.setClockDivider(SS, divisor);

The preset values of SPI_CLOCK_DIV2 etc. from AVR days are not defined. The divisor is an integer from 1 to 255. You calculate the clock rate as follows:  The Due system clock is 84 Megahertz.  The divisor is the number you divide into 84 MHz to get your desired clock frequency.  So common values would be:

Divisor   SPI Clock Frequency
84          1 MHz
42          2 MHz
21          4 MHz   (Default value, usually what AVR SPI bus uses)

It appears there is no way to get 8 MHz as the function takes integer divisors. (Need to double check this, very little documentation).

For now, it would appear that setting pins using pinMode and digitalWrite are not allowed.  The comment on the reference is: once SPI.begin() is called, the declared pin will not be available as a general purpose I/O pin, so do not set pinMode for the SPI lines if also using SPI library calls.

Finally, the hardware SPI library is only for 8 bit data values (send and receive).  If you need 9 bit SPI as is used on some graphical displays, the hardware library will not work.  But do not fret, there are some that have used low level calls to do software SPI which is capable of 9 bit SPI, I'll look into that next.



Monday, February 18, 2013

Arduino Due SPI Display - Nearly Defeated

I have been working over a week on trying to get an Adafruit SPI driven display to work on the Arduino Due.  No, it still does not work and I'm not happy.
The Due SPI interface has fairly incomplete documentation.  From the clock divider function call having do documentation on divisors to supposed support for more than 8 bit transfers (but zero documentation on that).

So writing an interface with native function calls is pretty much moot at this point.

On the Arduino Uno, most high performance code manipulated processor registers to get the speeds necessary for pumping out the data to a display fast enough to see in real time.  It turns out direct register calls work with Due also.  The best example is the gLCD Library by Tom Carpenter documented in this forum post.  Pretty good stuff but once you get to AVR/Uno normal and high speed, Due normal and high speed, the complexity rises exponentially.

My work has been to splice the initialization and display write code into the Adafruit HX8340B library.  Not an easy task after mastering the Due blink sketch.

First you have to make the original code Due compatible.  This involves compiler directives to detect a Due target platform.  The Due does not have some features of the originals.  The following is to move data that an AVR places in Program Memory (PROGMEM) into the more ample RAM of the Due (and the fact that PROGMEM is not handled like the AVR on the Due if at all):

#ifdef __SAM3X8E__
#define PROGMEM static
#define pgm_read_byte(x)        (*((char *)x))
#define prog_uchar uint8_t
#else
#include <avr/pgmspace.h>
#endif

This should work with #ifdef _LIB_SAM_ also but I have issues with that consistently working.

Then there is adding code for SPI again with #ifdef modifiers to put in Due code instead of AVR code where necessary.  Pretty complex.

Where I have bogged down is:
  1. Are Arduino SPI calls necessary for direct register (probably yes), so is a divisor of 21 for 4 MHz (84/21=4)?  No documentation.
  2. The gLCD library and Adafruit library have different handshakes for starting to enable the signal lines. I'd trust Adafruit did theirs right.
  3. Setting pinMode for SPI data lines causes the code to hang.  Are these needed and if so how to not have them hang.  A real debugger would help.
So at this point I'm setting the work aside.  I can post some code on Github if anyone wishes to review things.

If I get creative, I'll probably do two things:

  1. Fire up my DSO Nano scope - I haven't used it yet but I should to see what pins are doing.  An 8 bit 100 MHz logic analyzer would be preferable but I don't have one.
  2. Start at the basics - get just the Due code running - probably by starting with basic SPI oerations, then building on that.  It would be better to have a real simple SPI device to do this on that gives clear indications of operation rather than the complex display I have.  Ideas?

Excellent Arduino Pinout Diagrams

Several high quality color pinout diagrams have been posted to the Arduino forums.  These are very helpful for knowing the basic and advanced functions of Arduino boards.

The first to be posted was for the complex Arduino Due (above) by user Graynomad.  Then user Pighixxx used similar layout to do diagrams for the Uno, Mega, Esplora and more.

For readers, I link to the forum posts for various diagrams.  This allows you to get the latest version in case of revisions (the forum is keen on spotting any problems which keeps the quality of the diagrams very high).


Also with the Uno, Pighixxx laminated a printout and mounted it on a plate and mounted a real Uno which makes a great prototyping plate (probably more detailed than a beginner might want, I would say a great target would be for technical or college level classes).

One doesn't need this level of detail for their first Arduino project but for midlevel to advanced work, these are wonderful and bring together information it could take hours to research.  Thus you have more time for actual experimenting (and debugging).


Sunday, February 10, 2013

Official Esplora Examples to Get Update

Arduino IDE version 1.5.2 for Due release notes also includes notes for the unreleased 1.0.4.  In those release notes, it states that it: "Fixed a lot of Esplora examples."

Yay!  The examples currently available are not all that great.  If you want to see Esplora library calls in action, see my previous post.  Perhaps I'll find out I made some funky calls myself.

References:

Saturday, February 9, 2013

Arduino Esplora with Display Exercise Program

Everyone wants to play with new hardware.  If you have a new Arduino Esplora, grab an Adafruit 1.8" LCD breakout and load up this program.  It allows you to display sensor values and set LED and buzzer values, to see what is happening.
Snap in the display into the right-hand "display header" row of black header.  The software is available on Github at https://github.com/TheKitty/EsploraApps, file EsploraTest.ino

Select which test you would like via the Switch buttons on the right of the Esplora labelled Switch 1, Switch 2, and Switch 3.

Testing all the buttons and sensors, select Switch 1 (the bottom one):
When you press buttons or move the joystick, the appropriate word turns from white to green.  The linear potentiometer (slider) adjusts, curiously from 1023 on the left to 0 on the right (at least on mine, hope it wasn't soldered in backwards).  The microphone sound value is displayed (yell at it), as is the light sensor value (cover it to change).  Temperature is measured in Celsius and Fahrenheit (under the display above the slider, blow hot breath to raise it).  Finally, accelerometer values (X, Y, and Z axis) are displayed (suddenly move the Esplora to see changes).  When you are done, push down on the joystick (do not move it, press it down firmly, it has a switch there also) to return to the menu.

Testing the RGB LED is easy by selecting Switch 2 at the menu:
Red and Green values may be adjusted from 0 to 255 by moving the joystick up, down, left, and right.  Adjust the blue values with the linear potentiometer slider.  To exit, again press down on the joystick.

Finally, to play with the buzzer, select Switch 3 at the main menu:
Move the linear potentiometer slider to adjust the frequency.  If the sound is annoying, move to the left (low) or press Switch 1 to silence.  Again press the Joystick down to get to the menu.

The code is good in displaying how you might use the Arduino Esplora library calls.  You can access some (but not all) of the hardware directly using lower-level functions but I don't do that as this is to demonstrate the official way to use the Esplora (ok, except for the display which is NOT an official display because as of the writing of this article has not been released).

Hopefully this work will give you ideas on your own exploration with Esplora.


Tuesday, February 5, 2013

Esplora LCD Pong Game

The Arduino Esplora is blooming as a game platform. This includes use as a standalone game machine as documented in my previous posts.  Add an Adafruit 1.8 inch TFT LCD display or the official Arduino display onto the little-documented expansion header and the Esplora is a game platform rather than a controller.
Add an LCD to your Esplora and you have a standalone gaming platform
The first game on an Esplora + LCD was a platform bounce game I ported from work by R0d0t.  But I also eyed a Pong game posted to Youtube by David Rutherford.

So I contacted David and he sent me over the code the other day.  David stated the code was not textbook code - besides adding Esplora button presses, I did some reformatting and documentation.

The Arduino may be powered by a USB cable to your PC, a USB phone brick, or a phone backup battery.

So here I present the classic one-player Pong game.  It uses the Esplora right-hand buttons 2 and 4 (up and down) to allow the player to move their paddle up and down.  First player to 5 wins.  The Arduino plays a good game but can make some bonehead moves every now and then - definitely not easy to beat it.

To start a new game, you press the right button (labeled Switch 3).
Player's paddle on screen is on the left, control buttons to the right.  Excuse my photography...
You can download the code for free on Github at https://github.com/TheKitty/EsploraApps.

You can mod the game but it may not be the simplest process.  I looked into use of the joystick for paddle control and I found it sluggish.  The game may be sped up by not drawing the lines and score after every ball move.  Perhaps the gameplay calculation conditional statements could be a bit clearer.  If you do speed up the game, you should have something to govern the ball speed - I would suggest the slider below the display controlling a delay.

If you want a cleaner version of the code, the official Arduino.cc website has a version of Pong at https://www.arduino.cc/en/Tutorial/EsploraTFTPong