Using the Arduino on the mangOH green (Part 1)
Published:  18 April 2017

Along with the two CF3 sockets, the mangOH Green board is also populated with an Arduino processor and the 0.1" pitch sockets to mount Arduino shields. This is a useful feature for interfacing any of the myriad of sensors and devices that are on existing Arduino shields.

Getting started with the Arduino on the mangOH Green

The Arduino that has been implemented on the mangOH green is an Arduino Leonardo and it has its own mini USB port for programming purposes. Note that the mini USB port does not provide power to the Arduino - you will need to power up the mangOH board either via a separate micro USB connection, or via the 2.5mm DC jack.

Note: The Arduino shield connectors are only connected to the Arduino Leonardo, NOT directly to either of the CF3 sockets on the mangOH green.

The only way to access an Arduino shield is via the onboard Arduino Leonardo.

Connecting to the Arduino

Before any code development can be done, the Arduino on the mangOH green board has to be connected to a development PC (host) running the Arduino Development IDE available from here

On a Linux PC

When plugged into a Linux PC, watching the Kernel log (/var/log/kern.log on a Ubuntu PC), the log will report something like the following:

Apr 18 14:17:07 MBP kernel: [236400.560118] usb 3-1: new full-speed USB device number 7 using ohci-pci
Apr 18 14:17:07 MBP kernel: [236400.783200] usb 3-1: New USB device found, idVendor=2341, idProduct=8036
Apr 18 14:17:07 MBP kernel: [236400.783213] usb 3-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
Apr 18 14:17:07 MBP kernel: [236400.783220] usb 3-1: Product: Arduino Leonardo
Apr 18 14:17:07 MBP kernel: [236400.783227] usb 3-1: Manufacturer: Arduino LLC
Apr 18 14:17:07 MBP kernel: [236400.796198] input: Arduino LLC Arduino Leonardo as /devices/pci0000:00/0000:00:04.0/usb3/3-1/3-1:1.2/0003:2341:8036.0007/input/input13
Apr 18 14:17:07 MBP kernel: [236400.852507] hid-generic 0003:2341:8036.0007: input,hidraw0: USB HID v1.01 Mouse [Arduino LLC Arduino Leonardo] on usb-0000:00:04.0-1/input2
Apr 18 14:17:07 MBP kernel: [236400.863878] cdc_acm 3-1:1.0: ttyACM0: USB ACM device
Apr 18 14:17:07 MBP kernel: [236400.865405] usbcore: registered new interface driver cdc_acm
Apr 18 14:17:07 MBP kernel: [236400.865409] cdc_acm: USB Abstract Control Model driver for USB modems and ISDN adapters

The important things to take away from this information are the following:

Apr 18 14:17:07 MBP kernel: [236400.783213] usb 3-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
Apr 18 14:17:07 MBP kernel: [236400.783220] usb 3-1: Product: Arduino Leonardo

which shows the Arduino is connected and correctly identified; and

Apr 18 14:17:07 MBP kernel: [236400.863878] cdc_acm 3-1:1.0: ttyACM0: USB ACM device

which indicates which serial port (required for programming) the Arduino has connected to.

On a Windows PC

Note: This is tested on a Windows 7 x64 PC. Your milage may vary on other versions.

On plugging the Arduino into the Windows PC, a 'Found new hardware' message may pop-up. This will install the appropriate driver(s) for your version of Windows as required. Clicking on the message to expand further details will show a window like this after installation is complete:

The important thing to take away from this is the COM port that the Arduino Leonardo has connected to (in this case it's COM50).

If the Arduino drivers have previously been installed on the the Windows PC, expand the Ports tree in Windows Device Manager to find out which COM port the Arduino has enumerated this time:

In the example shown above, the Arduino Leonardo has enumerated as COM50

Connecting to the Arduino from the mangOH Green/Legato

The mangOH green DV3/DV4 boards have got a permanent connection to the hardware serial port on the Arduino Leonardo. This is done using a FTDI USB to RS232 IC mounted on the mangOH green motherboard and which is permanently mapped to the Linux /dev/ttyUSB0 device. This device is available immediately after the mangOH green has booted.

A simple example - the CaSeInVeRtEr

Tip: Sometimes it is easier to do the Arduino Develoment on a stand alone Arduino board and then move it to the mangOH green. This may be the case when developing some code to interface to a shield.

Presented here is a simple example of the mangOH green using the onboard Arduino Leonardo as a slave device to perform a simple task. In this case, the Arduino will listen on the serial port and invert the case of any characters sent to it. E.g. if an A is sent by the mangOH green, the Arduino will reply with an a. Control characters (except for carriage return and line feed character) and punctuation characters will be ignored and digits will be returned as-is.

Note: Serial communications on an Arduino Leonardo is somewhat different to the way serial communications are done on other Arduino boards (in particular the Arduino Uno).

The Arduino Leonardo has two serial ports - the first is an emulated serial port on the USB programming connection. This is mapped internally to the Serial class. The hardware UART (TX/RX pins) is mapped to the Serial1 class. It is this Serial1 port that is connected to the /dev/ttyUSB0 device on the mangOH green (and connected to the shield pins)

Other Arduino boards (such as the Arduino Uno) only have one serial port - the hardware UART (TX/RX pins) - and this is shared between the programmer/serial monitor and arduino shields.

So all communications between the Arduino and mangOH green MUST be done using the Arduino Serial1 functions.

Arduino code

The Arduino code is very simple. It initializes the Serial port(s) to 57600 baud and then loops forever checking to see if there is serial data available. If there is a serial data available, the first character is read from the serial port and tested:

  • if the character is \r or \n or ` ` (space), append the character to the string

  • if the character is 'numeric' (isDigit()), append the character to the string

  • if the character is 'alpha' (isAlpha() or a-z or A-Z), invert the case of the character by XORing it with 0x20 (0010 0000). This will change A (0x41) to a (0x61) and so on.

  • otherwise, leave the string empty.

Once the tests are complete, print the string (without any trailing line feed or carriage return characters) to the serial port and loop around again.

The Arduino code can be downloaded here

installing the Arduino code

  1. download the code and extract it to a directory on the host PC. Use 7Zip on a Windows PC to open the .tgz file.

  2. make sure the mangOH board is powered on and the Arduino mini USB port is connected to the host PC

  3. open the Arduino IDE and use File->Open to navigate to the directory created in step 1 (above) and open the .ino file

  4. go to Tools->Board and select Arduino Leonardo from the drop down list

  5. go to Tools->Port and select the Serial Port that the Arduino has enumerated as (/dev/ttyACM0 (Linux) or COM50 (Windows) in the examples above - but use the values for your particular setup)

Warning: DO NOT OPEN THE SERIAL MONITOR AT THIS TIME as it will interfere with programming the Arduino over USB

  1. compile and install the application on the Arduino by using Sketch->Upload , Ctrl+U or click the 'Arrow' in the toolbar of the Arduino IDE.

  2. if the upload completes sucessfully (no errors or warnings), open the serial monitor (Tools->Serial Monitor or CTRL+SHIFT+M)

  3. set the Baud Rate to 57600 baud and the Line Ending to Carriage Return in the drop down boxes in the bottom right hand corner of the Serial Monitor.

  4. type a character in the command bar at the top of the Serial Monitor and either click Send or press Enter on the keyboard. The character should disappear and the inverse case version appear in the output pane of the Serial Monitor.

Note: Remember to close the Serial Monitor window before recompiling and uploading code to the Arduino Leonardo. If this is not done, the programmer sometimes tries to re-enumerate the USB port and the Arduino comes back on a different port as the previous port is still held open by the Serial Monitor.

mangOH Green application

Conceptually, the Legato application for the mangOH green is very similar to the Arduino code, but the implementation is somewhat more complex:

  • low level open() the serial port and get a file descriptor (FD) reference to the opened device

  • use the FD to set up the serial device (/dev/ttyUSB0) to 57600,N,8,1 using the standard linux termios() api calls

  • use the Legato le_fdMonitor() File Descriptor Monitor API to assign an event listener (callback function) to the FD - this callback will be called whenever data is available to be read on the serial port. Note that this callback function is asynchronous - it's an event , not a poll and will be called when data becomes available.

  • set up a recurring timer to fire approximately every second. This timer will send one character at a time from an incrementing character sequence (A->Z then a->z repeated forever) to the FD using the write() function.

  • and then run forever

Tip: The mangOH code in this example uses low level file functions such as open(), read() and write(). These file I/O functions DO NOT operate in the way that the more familiar fopen(), fread(), fwrite() family of functions operate.

These two different families of file operation functions are NOT interchangeable!

The mangOH code can be downloaded here

installing the mangOH code

For those in a hurry, a prebuilt WP85 update binary is provided in the tgz download bundle. Note that this is built against Legato 16.10.1 and will not run on mangOH platforms without this version of Legato.

Note: The following instructions are for a command line build and install of the application on a Linux PC. Users wishing to use Windows and/or Developer Studio will have to import the source files into Developer Studio and use the appropriate tools inside Developer Studio.

Note: This code has been tested against Legato version 16.10.1, and requires a mangOH green DV3 or DV4 board with Legato 16.10.1 installed.

The code may not compile against or behave as expected agains other hardware or other versions of Legato.

  1. download the code and extract it to a directory on the host PC. Use 7Zip on a Windows PC to open the .tgz file.

  2. make sure the mangOH micro USB port is connected to the host PC and the mangOH board is powered on and there is a network connection to 192.168.2.2 (the USB ethernet connection to the mangOH board)

  3. on the develpment PC, open a terminal/command/console window.

  4. ensure that the legato environment has been set up (either run bin/legs or . bin/configlegatoenv from the Legato install directory). This can be tested by running the following command in the terminal window:

env | grep WP85

which should return the following results indicating that the WP85 toolchain is available:

WP85_TOOLCHAIN_PREFIX=arm-poky-linux-gnueabi-
WP85_TOOLCHAIN_DIR=/opt/swi/y17-ext/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi
  1. in the terminal window, change to the directory where the application source code was extracted to

  2. make the application using the following commands in the terminal window:

make clean
make target
  1. open two ssh consoles to the mangOH board. This can be done from two new terminal windows using the following command in each window:
ssh root@192.168.2.2:

or by using a terminal application such as PuTTY

  1. in one ssh console, run the following command:
logread -f

This will scroll the mangOH system log on the screen whenever any new information is logged.

  1. on the host, run the following command:
make install

to use the host legato tools to install the application .update file to the mangOH board.

  1. in the second ssh console, run the following command:
app start arduinomaster

to start the application on the mangOH board.

At this point, there should be a continuous stream of log messages appearing in the logging ssh console indicating the character being transmitted to the Arduino and the response being read by the mangOH. There should be one pair (one transmit, one receive) roughly once per second.