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






Wednesday, March 2, 2016

Getting Started with the Beta Arduino MKR1000

Note: No, I cannot share the pinout of the MKR1000 beta board (printed on the back of the board) per an email sent to the Hackaday.io contest winners, sorry.  The reason is the pinout might change so making shields right now would lead to heartache.

One thousand Arduino.cc MKR1000 beta boards are now being received by Makers worldwide thanks to a contest sponsored with Hackster.io and Microsoft.  Round Two of their competition is to make some great things.  Here is documentation on how to get started after the box comes in the mail.

1) Go to the e-mail you got days ago stating that you shouldn't release info (doh!).  Sign up for the MKR1000 closed forum on arduino.cc and wait up to 48 hours for the link (doh!).

2) Go to the arduino.cc download site and get the nightly build of the Arduino IDE software.  Copy it into a directory to use out of the zip file.

2a) For Windows users only: go into the drivers directory and run program dpinst-x86.exe for 32 bit Windows or more likely for modern installs dpinst-amd64.exe for 64 bit Windows (Intel or AMD, not just for AMD processors).  This will install the device drivers for the latest Arduino boards including MKR1000.

3) Run the beta Arduino IDE by running arduino.exe.

4) Go to Tools -> Board -> Boards Manager and install the Arduino SAMD Boards package version 1.6.3.  This gives you the MKR1000 as a board choice.  1.6.4 apparently dropped MKR1000.  If the closed MKR1000 forum says differently use what they say.

5) Go to Sketch -> Include Library and do a search for "101".  Install the WiFi 101 library package which supports the MKR1000 wi-fi support. Use version 0.8.0 or higher.

6) Plug in a USB cable A to Mini B from your PC to the board.  For Windows, the appropriate driver should load if you got the beta IDE driver installed ok - if the driver fails, try again to install the driver.

7) You can load the Blink sketch:

void setup() { 
  pinMode(6, OUTPUT);     // Pin 6 is onboard LED on beta MKR1000 

void loop() { 
  digitalWrite(6, HIGH);  // turn the LED on (HIGH is voltage level) 
  delay(500);             // wait for 500ms 
  digitalWrite(6, LOW);   // turn the LED off by making voltage LOW 
  delay(500);             // wait for 500ms 
}

Note the LED on the beta boards is on Digital Pin 6, not the usual Arduino Pin 13.  If all goes well the Green LED marked "L" blinks!

There are more sample programs for advanced use including web client and server under File -> Examples -> WiFi 101. Note the Wifi 101 has its LED on Pin 9 which has to be changed to Pin 6 for MKR1000.  Try the SimpleWebServerWiFi sketch with the LED ad Pin 6, very fun!  You'll need the output from the serial monitor to know the web address for the web page to change the LED state.

Resources on the Internet