Saturday, April 9, 2016

Use Adafruit Feather Wings as MKR1000 Shields

The MKR1000 microcontroller board from arduino.cc is a very new, very powerful board. With the board coming out of beta and now available, the pinout of the board is fixed (compared to the "pinout might change" communicated in the beta).

It will take some time for MKR1000 compatible daughterboards ("shields").

Fortunately, Adafruit makes add-on boards for their new Feather line. Feather comes with several different microcontrollers: AVR 32u4, SAMD21 M0, ESP8266 and some also come integrated with nice radios. Peripheral daughterboards (Adafruit calls "Wings") now available include LEDs, a real-time clock, motor control, relay, servo, and OLED display.

In the absence of MKR1000 shields, you can look to use Feather Wing boards.


Wings are not directly pluggable into headers placed on the MKR1000.  The pinouts are different and the M0 processors on the Feather M0 and the MKR1000 are different (SAMD21 vs. SAMW25 respectfully).

To overcome the differences, a sandwich board can be used. This would best be a custom board (think a nice purple PCB with ma1e headers on the bottom, female on the top). with the pins from the bottom switched around to make the top pins fit the Feather Wing pin expectations.

For a proof of concept, I took two Adafruit Proto Feather Wings. They were used to make the wiring changes.

Above, the MKR1000 is the bottom board furthest from the display.  It has female headers (with extra long pins for breadboard use also) soldered in (shown below).


The second board, a proto, has two sets of male pins, ones to make with the MKR1000 and male headers pointed up towards the second proto.

The second proto has male headers pointing down to the first and female headers for the Feather Wing daughterboard.

The cross connection may be done with 22 gauge wire.  A more old fashioned connection method is to use fine wire wrap wire and connect with a wire wrap tool. See Wikipedia if you're unfamiliar with wire wrapping.

The OLED Feather wing communicates via the I2C bus. To make it work, we need to cross connect the following pins:

  • Ground
  • Power / 3.3 volts
  • I2C data line (SDA)
  • I2C clock line (SCL)
  • The reset line for the display is connected to Pin 5 on the MKR1000
The code to put test text on the display:


/*
 *  MKR1000 and Adafruit Feather Wing Code
 *  Mike Barela http://21stdigitalhome.blogspot.com/
 */
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 5
Adafruit_SSD1306 display(OLED_RESET);
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
void setup() {
  
  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
  
  // Show image buffer on the display hardware.
  // Since the buffer is intialized with an Adafruit splashscreen
  // internally, this will display the splashscreen.
  display.display();
  delay(2000);
  // Clear the buffer.
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print("Connected to");
  display.setCursor(0,16);
  display.print("MKR1000!");
  display.display(); 
}
void loop() {
}
Have fun wiring additions to your MKR1000 board including using Adafruit Feather wings.

Thursday, March 31, 2016

Cross Compiling Post Mortem

My article series a few days ago on Windows cross compiling for Raspberry Pi hasn't caught fire.

Today has the announcement from the Microsoft Build conference that an upcoming Windows 10 update will include the Ubuntu user environment and ability to run Ubuntu executables.  Wow.

This means a much easier time working on development.  We'll have to wait for the Redstone upgrade, no date given, then use a Windows 10 machine.  Worth upgrading from Windows 7?

Tuesday, March 29, 2016

Cross Compiling - Compile C Programs on Your Windows Machine for a Raspberry Pi - Use

This is the final of 5 posts on setting up and using a PC cross compiler to make Raspberry Pi programs.

Following the previous posts, we have the Raspberry Pi compatible gcc compiler on our PC with library files copied over from the current Raspbian Jessie. We also have an updated Pi ready to run programs.
The make utility is often used by professionals to pull together a collection of files to build a program and there is a copy of a compatible Windows Make utility in C:\SysGCC\bin.  But most Raspberry pi programs are not that complicated!  We can use gcc to make programs with only one source file. Let's try it out. Type or copy to the file HelloWorld.c in Notepad, Wordpad (text), or other text editor:

#include <stdio.h>

int main()
{
    printf("Hello, world\n");
}

If it looks different than an Arduino sketch, it is. It is standard C, straight out of Kernighan and Ritchie. Place the file HelloWorld.c into a fresh program directory. The following Windows Command ("Batch") file helps automate the compile process - copy this to another text file called gcc.bat:

@ECHO OFF
SET PATH=%PATH%;C:\SysGCC\bin
IF %1.==. GOTO NOARG
@ECHO ON
C:\SysGCC\bin\arm-linux-gnueabihf-g++.exe %1.c -o %1
@ECHO OFF
if errorlevel 1 (
   ECHO Compile Fail - error %errorlevel%
   exit /b %errorlevel%
)
GOTO END1
:NOARG
  ECHO Must enter parameter like "C:\> gcc myfile" (no .c needed)
GOTO END1
:END1

If you installed the compiler into a different directory than C:\SysGCC, change the second and fourth lines to match.  All of this doesn't do anything fancy - it uses some command code (like a Unix shell script) to make sure you feed it a file name and that the compile worked (or not).

If you are in Windows File Explorer in the directory of your program, you'll see HelloWorld.c and gcc.bat. We will use a command line prompt to run the compiler (since it is a command program, not a graphical program). While holding the PC Shift key on the keyboard, right click the mouse in the Explorer window. Select the entry Open Command Window Here. You will have a Windows command window open in your program directory, which you can verify by typing the dir command. Compile your program by typing gcc <programname> - for our test, we'll type gcc HelloWorld (no .c should be typed).


With no error messages, your program worked!  You now have a file named HelloWorld (no extension) in the directory.  This is your Pi program.

Transferring Your Program to Your Pi

The binary executable file now needs to go from the PC to the Pi. This may be done in multiple ways but the simplest way is using the Pi Finder utility we used earlier.


Press the Upload button and select your HelloWorld file we just made in your project directory (do not select the HelloWorld.c file.  If you don't have a HelloWorld file, around 6,500 bytes, go back to compile it. When you complete the upload process, the file is in your ~/home/pi directory:


You need to execute the command chmod +x HelloWorld to tell the Pi the file is an executable (and not a data file). The file name will be in green when you type the file listing (directory) command ls.

Now you can execute your program (finally!) by typing ./HelloWorld (the preceding ./ tells Raspbian Linux we're executing the program in the current directory).  It prints Hello, world - success!

Going Further

Now we can build more complex programs, perhaps Arduino-like programs.  We'll explore these in coming blog posts.  Thanks for following this 5 post series and as always leave comments if you like, reshare with your colleagues & friends.

Other blog posts in this series:
  1. Introduction
  2. PC Setup
  3. Pi Setup
  4. Library Setup
  5. Use (this post)

Cross Compiling - Compile C Programs on Your Windows Machine for a Raspberry Pi - Library File Copy

This Part 4 of the posts on cross compiling Raspberry Pi programs on your PC.

From previous posts, we have the gcc cross compiler on the PC and a fresh Raspbian Jessie running on the Pi. The next step is to get the latest library files from the Pi over to the PC.

Fortunately those charitable folks at sysprogs have another utility to pull the correct files from your Pi to your PC.  If you installed gcc into C:\SysGCC (or substitute your own directory path), go to C:\SysGCC\TOOLS in Windows File Explorer.  Run UpdateSysroot.bat by double clicking it.  You'll get the pop-up window below:


Type in the IP address of your Pi. In the Adafruit Pi Finder, it gives you the address as 4 numbers separated by periods.You can run sudo ifconfig in a Pi terminal window to find the 4 number group IP address. Look at eth0 for wired connection, wlan0 for wireless.

Leave the other settings on their default values and press the Synchronize button. This will take several minutes and takes a few hundred megabytes of hard disk space on your PC.

Once the process is done, our setup is finally complete!  It would be a longer setup if we would have set up a full compile environment. The next post demonstrates compiling a program and using it on your Pi.

Other blog posts in this series:
  1. Introduction
  2. PC Setup
  3. Pi Setup
  4. Library Setup (this page)
  5. Use

Cross Compiling - Compile C Programs on Your Windows Machine for a Raspberry Pi - Pi Setup

This is Part 3 of the tutorial on compiling C/C++ programs for Raspberry Pi on Windows

This tutorial will set up the Raspberry Pi.  The latest version of the operating system used is Raspbian Jessie, using other OSes may change how you need to do things.

Let's look at our model again from Part 1:

We'll need the Pi to be communicating with our PC to do two things:
  1. A (probably one-time) copy of the library files the Pi uses to link in additional functionality. Using library files may be familiar from using Arduino. Instead of picking one or more libraries to use, we'll copy all the library files on the PC as space is not much of a concern on a PC file system and it isn't that large (hundreds of megabytes).
  2. We need a mechanism to copy the finished executable file from our PC to the Raspberry Pi.

Your Pi and its Rasbian Operating System

If you are starting fresh (you do not have an operating system SD card), I suggest following instructions on downloading and placing the OS image on a SD (or microSD) card. Tutorials:
Insert the card into the Raspberry Pi. Boot your Pi with a keyboard, monitor, and network connection. If you need to connect to your Pi via your PC (sometimes called running headless), I suggest running the Adafruit utility Pi Finder.
This program has several advantages:
  • You can click the Terminal button to get a command line window access to the Pi
  • The Upload button will very quickly upload files to your Pi (perfect for compiled executables!)
  • Shut down your pi the correct way

Ensure a Fresh OS

To be sure we're running the newest operating system patches (which include possible library changes), we need to freshen things up. 

Configure your Pi:
  • via the command line, use guide here
  • via the graphical interface at Menu Button -> Preferences -> Raspberry Pi Configuration
  • Allow Secure Shell (SSH) connections during configuration.
For configuration be sure to select your locale and keyboard to match where you are and what you have. Setting the time zone is good as well as expanding your file system if your card is larger than 4 GB.

Now to update your Pi's software:
  • If you're in the graphical environment, click the computer screen icon to get a command line terminal
  • upgrade your software via this Raspberry Pi Tutorial
Now everything is (hopefully) running the latest Raspbian Jessie with all updates. We can now get the compiler loaded with all the libraries on your Pi!

Other blog posts in this series:

  1. Introduction
  2. PC Setup
  3. Pi Setup
  4. Library Setup
  5. Use



Cross Compiling - Compile C Programs on Your Windows Machine for a Raspberry Pi - PC Setup

This is Part 2 of the series on cross compiling on the PC for the Raspberry Pi.

Let's set up a Windows computer to allow for Raspberry Pi cross compiling.

As stated in the original post, there are several ways to get a toolchain for cross compiling, often involving making your Windows PC more like a Linux/Unix machine. This involves more work than most people are looking for.

This effort leverages prior work to build the toolchain by sysprogs.com. They have posted a large number of cross compilers pre-built under the GNU license.

Download the latest toolchain for Raspbian Jessie (gcc 4.9.2 as of this tutorial). Run the program and you'll get the following screen:

Due to the Unix roots for cross compilers, you need to set an installation directory without spaces in any directory name.  This rules out the typical Windows Program Files directory.  I selected C:\SysGCC.

The installation takes several minutes, even on a fast PC as there are a large number of files involved. There will not be any icon for the desktop as the tools are for command line use.

That's it, your tools will be placed in the directory you specified.  Remember the location.

The cross compiler will not have the latest libraries for the Raspberry Pi that are included in the Raspbian Jessie distribution.  These are needed as they are linked in, used by the compiler to add pre-built functionality to a program at compile time. The easiest way to get a fresh set is to copy over the files from specific directories on an up to date Raspberry Pi.  This will be done in the next post.

Other blog posts in this series:

  1. Introduction
  2. PC Setup
  3. Pi Setup
  4. Library Setup
  5. Use

Monday, March 28, 2016

Cross Compiling - Compile C Programs on Your Windows Machine for a Raspberry Pi - Introduction

Cross Compiling is the process of making an executable program for one computer with the goal of running the program on another (different) computer. This can be very handy! Your desktop or laptop is probably more capable than your Raspberry Pi or other development device. Your PC has the edge with a better display, more memory, and speed.

With compiled programs like C and C++, this can be a more ideal procedure when compared to Python which is interpreted on the fly (thus no speed increase by doing compilation beforehand). Many people are familiar with C and C++ as the basis for the programming when using the Arduino development environment.

The diagram below shows how this would be accomplished.
The open source gcc compiler can generate code for many different computers. What we are looking for in this series is a Windows executable compiler which generates Raspberry Pi executables. There are many more combinations. Raspberry Pi cross compilers most often use Linux as the PC-side operating system and you can find Mac hosts as well.

Unfortunately, getting the cross compiler is not as simple as going to the Raspberry Pi website or GitHub repository (at present). The code for building the gcc toolchain (the programs that make up the cross compiler) is available but you need a native compiler (in this case Windows) and an environment that will take the source code and make the tools you want. Since gcc is a compiler that is Unix/Linux centric, you can set up an environment that helps Windows work more like *nix, the most common two being MinGW and Cygwin. This complicates the upfront work to set things up, something that we'd like to avoid at present.

The tutorial in following posts will demonstrate an effective cross compile process by using tools developed by SysProgs and Adafruit.

  1. This introduction
  2. PC Setup
  3. Pi Setup
  4. Library Setup
  5. Use