After completing the majority of the key mechanism for my vintage toy synthesiser, which I covered in my last blog post, I thought it was about time I cracked open the BeagleBone Black board and attempted to connect the key mech to it. Setting up the BBB for my preferred development language and environment, as well as getting the Arduino-to-BBB comms working, was a bit more complex than I thought it would be, nevertheless I have now got the BBB receiving key interaction data from the keyboard.


This blog post covers the following main things:

  1. Setting up the BBB to be tethered to a computer
  2. Installing a BBB-compatible ARM cross-compiler on OS X
  3. A method for developing C/C++ based software for the BBB, from writing code to testing compiled binaries
  4. Enabling all UART/serial ports on the BBB, and writing software that reads from a connected serial device


My Preferred Development Languages and Environment


Professionally, and as a hobbyist, I mainly develop software using the C and C++ languages, which is what I plan to use when developing the BBB software for the vintage toy synthesiser.


When it comes to developing software for Linux-based single-board computers such as the BBB, my preferred way of doing it is using a cross-compiler that allows me to develop and compile the software on my main computer running OS X, and then using something such as Secure Copy (scp) to transfers the binaries onto the target hardware. The majority of this blog post talks about the tools and methods used to get this environment set up.


Tethering the BBB to a Computer


As per the official BBB Getting Started guide, the most common way to use and and develop on the BBB is to connect it to a computer and use the network-over-USB access. This is done using the following simple steps:

  1. Connect the BBB to your computer via USB
  2. Wait for a new mass storage device to appear
  3. On the mass storage device, open START.htm
  4. Follow the provided instructions to install the needed drivers


Once that has been done, possibly followed by a needed computer restart, you can now access your BBB through the IP address. My preferred access method is to use Secure Shell (ssh) through a command line interface (CLI), using the command:


ssh -l root


BBB-Compatible ARM Cross-Compiler for OS X


It is entirely possible to develop software for the BBB directly on the board by accessing it over a network. However there are a couple of pitfalls here:

  1. You're stuck using CLI programs which are not everyones preferred method of interacting with a computer, especially when it comes to text editing
  2. When compiling your software you're limited to the power of the BBB which is probably not as great as your personal computer, making the process a lot slower
  3. You'll be developing using Linux, which may not be your preferred OS to use


The way around this is to develop the software on your personal computer, where you have a GUI and greater CPU/RAM specs, and then transfer it over to the BBB afterwards. The main obstacle in doing that though is the fact that the processor type and OS of your personal computer (most probably Windows or OS X running on an Intel or AMD processor) is probably different from that of the BBB (Linux running on an ARM processor), so you need to use a compiler/toolchain that will run on one type of system (the host) but build software to be run on another type of system (the target). This is known as a cross-compiler.


The toolchain needed for cross compiling for the BBB is arm-linux-gnueabihf; which compared to the more-commonly used arm-linux-gnueabi has hardware FPU (Floating Point Unit) support which is needed for compiling for the BBB target. The arm-linux-gnueabihf cross-compiler toolchain is officially available for both Linux and Windows as a GCC-based compiler released by Linaro. For OS X there is an unofficial (but working) version of the Linaro toolchain available from here - this is the cross-compiler that I have installed and started using.

My Preferred Development Method


Now that I have my cross-compiler installed, I can start developing software for the BBB using my preferred environment and tools.


This is my preferred development method, from writing code to testing the compiled binaries:

  1. I write my code on OS X using a programming text editor - my personal favourites are Xcode and Sublime Text
  2. I compile my code using a Terminalwindow with the one of the following command:

    For C code:

    /usr/local/linaro/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc [source file] -o [compiled binary name]


    For C++ code:

    /usr/local/linaro/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-g++ [source file] -o [compiled binary name]

  3. With the same Terminal window I copy the compiled binaries to the BBB using scp:

    scp [binary file] root@[destination directory]

  4. Using a second Terminal Window which has a running ssh session logged into the BBB (see the Tethering...section above) I test running the binaries using the following command:

    ./[binary file]


Eventually I will want my BBB software to start on boot, but I'll talk about that in a later blog post.


Connecting the Key Mech Arduino via Serial


Apart from the obvious Hello World program, the first application I have developed for the BBB is a simple program that reads serial data coming from the pianos key mech Arduino, displaying read bytes to the console.


The BBB has six on-board serial ports - one that is coupled to the boards serial console, and five UART ports that can be found on the boards expansion headers. By default only the serial console port is enabled, so to use any of the other UARTs you must allow them to be enabled at boot. I did this by following the "Section 1" steps on this tutorial. Note that on my BBB the uEnv.txt file was in the /boot/ directory, not /boot/uboot/ as the tutorial suggests.


Once this had been done, I connected the Arduino Pro Mini to the BBB using the following connections:

  1. Arduino TX pin to BBB P9_26 pin (UART1 RX), for sending serial data from Arduino to BBB
  2. Arduino GND pin to BBB P9_01 pin (a DGND pin), for allowing the Arduino to be powered by the BBB
  3. Arduino RAW pin to BBB P9_03 pin (a VDD_3V3 pin), for powering the Arduino using the BBB


arduino to beaglebone black serial

The key mechanisms Arduino Pro Mini connected to the BeagleBone Black via the UART1 port


Lastly I developed a small piece of code that opens the UART1 device file (/dev/ttyO0) and displays any byte it reads from it. You can see this code on my projects GitHub repo here, as well as below:


#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <termios.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>

#define KEYBOARD_SERIAL_PATH "/dev/ttyO1"

int main (void) 
  printf ("Running test_key_mech_input (v2)...\n");

  int keyboard_fd;
  uint8_t keyboard_input_buf[1] = {0};
  //Set up serial connection
  printf ("Setting up key mech serial connection...\n");

  struct termios tty_attributes;
  // open UART1 device file for read/write
  keyboard_fd = open (KEYBOARD_SERIAL_PATH, O_RDWR);
  //if can't open file
  if (keyboard_fd < 0)
    //show error and exit
    return (-1);
  tcgetattr (keyboard_fd, &tty_attributes);
  cfmakeraw (&tty_attributes);
  // setup bauds (key mech Arduino uses 38400)
  cfsetispeed (&tty_attributes, B38400);
  cfsetospeed (&tty_attributes, B38400);
  // apply changes now
  tcsetattr (keyboard_fd, TCSANOW, &tty_attributes);
  // set it to blocking
  fcntl (keyboard_fd, F_SETFL, 0);

  //Enter main loop, and just read any data that comes in over the serial port
  printf ("Starting reading data from key mech...\n");
  while (true)
    //attempt to read a byte from the serial device file
    int ret = read (keyboard_fd, keyboard_input_buf, 1);

    //if read something
    if (ret != -1)
      //display the read byte
      printf ("Byte read from keyboard: %d\n", keyboard_input_buf[0]);
   } //if (ret)
  } ///while (true)

  return 0;



Next Steps


Now that I have got the BBB up and running the next step is to start the development of the sound synthesis engine. This will involve developing some software that creates a simple controllable tone, as well as configuring the BBB to output the audio via one of its audio outputs.