Using Arduino code on Spark Core

In an earlier post Spark Core Weather Station I presented code that was reading weather sensors and sent the data over USB to a PC terminal. However the approach I took there to combine all code into a single file is not very practical. So now that the Spark IDE supports multiple project files we can just include Arduino library files.
Screenshot 2014-04-16 20.49.13
So here are some hints how to get Arduino library code and sketches to compile in the Spark framework. I use a TMP102 I2C temperature sensor to demonstrate the porting. Below is an example Sketch that reads the temperature values and sends them to the serial interface:

//////////////////////////////////////////////////////////////////
//Released under the MIT License - Please reuse change and share
//Simple code for the TMP102, simply prints ambient temperature via serial
//////////////////////////////////////////////////////////////////
#include <Wire.h>
int tmp102Address = 0x48;
void setup(){
  Serial.begin(9600);
  Wire.begin();
}
void loop(){
  float celsius = getTemperature();
  Serial.print("Celsius: ");
  Serial.println(celsius);
  float fahrenheit = (1.8 * celsius) + 32;
  Serial.print("Fahrenheit: ");
  Serial.println(fahrenheit);
  delay(200); //just here to slow down the output. You can remove this
}
float getTemperature(){
  Wire.requestFrom(tmp102Address,2);
  byte MSB = Wire.read();
  byte LSB = Wire.read();
  //it's a 12bit int, using two's compliment for negative
  int TemperatureSum = ((MSB << 8) | LSB) >> 4;
  float celsius = TemperatureSum*0.0625;
  return celsius;
}

The main reason this code fails to compile is the Arduino Wire.h library include statement fails on the cloud based Spark Core IDE. The TMP102 sketch code use I2C. So it includes the Arduino Wire Library with the following line:

#include <Wire.h>

This triggers an error in the Spark IDE. To solve this problem I created a new library named Wire.h with the following content:

#include "spark_wiring.h"
#include "spark_wiring_interrupts.h"
#include "spark_wiring_usartserial.h"
#include "spark_wiring_spi.h"
#include "spark_wiring_i2c.h"
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "debug.h"
#include "math.h"
#include "limits.h"
#include "stdint.h"
#include "spark_utilities.h"
extern "C" {
#include "usb_conf.h"
#include "usb_lib.h"
#include "usb_desc.h"
#include "usb_pwr.h"
#include "usb_prop.h"
#include "sst25vf_spi.h"
}
#include <sys/types.h>

With this library all we have to do is to uncomment or delete the original include statement. By adding Wire.h file the IDE automatically adds a line like this to the source code:

// This #include statement was automatically added by the Spark IDE.
#include "Wire.h"

As long as the Arduino source follows basic guidelines for portable code I did not face much difficulties to use existing Arduino sources on the Spark Core.

RedBear BLE Module

Bluetooth Low Energy (BLE) transceiver board. BLE is a new protocol introduced in the 4.0 revision of the Bluetooth standard. It is a wireless personal area network (PAN) technology aimed at novel applications in the healthcare, fitness, security, and home entertainment industries.  BLE is not backward-compatible with the previous Bluetooth protocol. However it uses the same 2.4 GHz frequency bands but a simpler modulation scheme. The picture below shows the RedBearLab BLE Mini Board.MKRBL2-2T
The module is a combo of a RBT01 BLE module featuring the Ti CC2540 Single chip Bluetooth (SoC) and a breakout board that offers a micro USB connector and a 3.3Volt UART connectors. The main reason this board caught my attention is the fact that it also has solder points for additional GPIOs on the back of the board. These GPIOs can be custom programed. So it should be possible to hook up  I2C or SPI sensors to the board and with a bit of software be able to monitor them.
Using such a BLE board  is not exactly an IoT solution as the connectivity to the cloud would have to be implemented with for example a Wireless enhanced Gallileo. Also the BLE’s range is rather limited. However when it comes to power consumption BLE has a leg up as it was specifically designed for very low power.

Spark Core compiler toolchain under cygwin

There are good instructions how to install the local toolchain to compile the Spark Core firmware. I don’t want to replicate them here. Go to the Spark Core Github and check the Readme.md or search for a tutorial.
The purpose of this page is to show the steps and pitfalls when installing this toolchain under cygwin on a Windows 7 64 bit computer.
I assume you have at least the base cygwin installation on your machine. For instructions please head over to cygwin.org:
Screenshot 2014-04-07 22.05.40
Make sure you install git and any other goodies you like under cygwin.

  1. GCC for ARM Cortex processors – ARM cross compiler tool chain for Windows
  2. Make – Windows version of gmake
  3. Device Firmware Upgrade (DFU) Utilities – Utility to download the firmware to the Spark Core
  4. Zatig – USB driver for firmware downloads

Now install gcc for ARM and gnu make for Windows. Yes, cygwin also includes gmake. However there is a problem with it around dependencies. The MINGW GCC compiler uses Windows path notation in the *.d dependencies files that gmake under cygwin chokes on. So if you, the second time you try to compile, get an error like this:

obj/src/application.o.d:1: *** multiple target patterns. Stop.

it is because we use MINGW GCC under cygwin instead of a native cygwin compiler. You can also checkout a related posts on the Spark Community Board. It is now time to install the firmware. Pull the following three repositories from Github:

git clone https://github.com/spark/core-firmware.git
git clone https://github.com/spark/core-common-lib.git
git clone https://github.com/spark/core-communication-lib.git

These repositories contain all the Spark Core firmware. Once the source code is downloaded, go to the build directory in the firmware folder and start the compile:

cd core-firmware/build
make

If everything went smooth you should now have a core-firmware.bin file.  Run the Zatig program to install the USB driver. The moment has come to flash the firmware into the Spark Core. For this, push the left button to reset the core while holding down the right button. Release the reset button and wait until the RGB-LED is flashing yellow. You can now download the firmware with the following command:

make program-dfu

Screenshot 2014-04-08 03.03.57
Note the DFU utility always indicates an error “Error during download get_status” This is normal and as long as you see “File downloaded successfully” everything is fine.

Windows for Galileo

Wintel
According to HotHardware.com there are signs that Microsoft will start supporting Galileo with a new “Windows on Devices” version that targets IoT and other smart devices.  Why is this noteworthy?  Well, this would in fact mean that the PC-era “Wintel” team is entering the Maker scene with their newly paired product offerings supporting an Arduino Maker platform.
This is certainly a welcome move as it broadens the choice of platforms and products Makers have to use in their project.