Start a C program when BeagleBone boots

I’ve been spending some time lately trying to turn the BeagleBone into a “single-serving” system for my prototypes. Most of these prototypes require running some program whenever the system gets power. Before you continue, I strongly recommend you read my previous BeagleBone posts, if you start feeling a bit lost.

So here I have a very simple circuit… 5 LEDs connected to 5 different pins (P8_3,P8_4,P8_5,P8_11,P8_12). I would like these LEDs to display a perform a simple pattern whenever the BeagleBone is turned ON. Something like the following video.

The code for this circuit is pretty straight forward and looks like this:

To compile and run this code, download my BeagleBone_IO library, save the above code as main.c and type the following commands:

gcc -c BeagleBone_gpio.c
gcc BeagleBone_gpio.o main.c -o main
./main

What is neat about my code, is that it is able to trap a signal whenever the user types CONTROL+C and ends the program normally… after performing some generic BeagleBone GPIO cleanup.

Anyway, I would like this program to run whenever my BeagleBone starts up. BeagleBone’s default OS is Angstrom Linux which uses systemd as the initialization scheduler. I am still learning about this, but according to the wikipedia entry and also to Kezhong’s Weblog, this daemon is meant to efficiently express services dependencies, by allowing more services to be launched in parallel at system startup while reducing the need to mess around with many scripts. I used to do this on ubuntu… and it was a mess.

Anyway, I would like to declare my LED blinking program as a service. So I need to execute the following steps:

Step #1: Create a script that will call the program that you wish you launch at boot time
This means we need to create a file in the /usr/bin/ directory with any name. For example, assuming that our compiled LED program is in /home/root/main we can create an executable simple script called myScript.sh with the following lines of code.

echo “#!/bin/bash” > /usr/bin/myScript.sh
echo “/home/root/main” >> /usr/bin/myScript.sh
chmod u+x /usr/bin/myScript.sh

Step #2: Create the service file
Create the file /lib/systemd/myScript.service . Actually your service name can be anything you want… not restrained to myScript.service.

nano /lib/systemd/myScript.service

Step #3: Write the appropriate service settings
Your /lib/systemd/myScript.service can be for example:

[Unit]
Description=Run the LED pattern script
After=syslog.target network.target

[Service]
Type=simple
ExecStart=/usr/bin/myScript.sh

[Install]
WantedBy=multi-user.target

Step #4: Make a symbolic link

cd /etc/systemd/system/
ln /lib/systemd/myScript.service myScript.service

Step #5: Make systemd take notice of it, activate the service immediately and enable the service to be started on boot-up

systemctl daemon-reload
systemctl start myScript.service
systemctl enable myScript.service

After executing all commands on step 5, the LED program should immediately start. Try switching the BeagleBone power OFF & ON… and you will see the LED program.

How to terminate the script that is constantly running in the background?
If everything went okay, your script in constantly running in the background. To terminate it, you need to kill it. Use the top command and find out the process id (pid) of your looping program. As shown on the screenshot, my main pid is 13.



Listing all jobs

The following command will take care of terminating the job.

kill 113

How disable the script from starting up at the boot time?
Just type:

systemctl disable myScript.service

Thanks to Piranha at the official BeagleBoard forum for guidance.

Cracking PDF-file passwords using a BeagleBone board

I am strong proponent of customized embedded systems; a single piece of hardware aimed at solving a single problem. If we have a particular task that is time intensive, repetitive, difficult to configure and somewhat “shady”, it is probably best to execute it in hardware. Lets consider a tool for recovering passwords from PDF-files. There are several downloadable software programs out there that allow you to recover a lost password from an encrypted PDF-file. Is this the best approach? Hardly.



Cracking a pdf password using a BeagleBone board… yes, the password is rock.

In general I am very wary of installing software in my computer that I do not have access to its source code. Who knows what is happening behind the scenes. Sure, the program may be trying to find the password for my PDF-file, but at the same time it may be sending the same PDF-file to some “shady” folks together with all my computer passwords, credit card numbers and so on. Not good. Even if the password cracking program comes from a “reputable” software house, installing software always leave floating traces behind, which inevitably bloat our hard disks and slow things down.

My idea was simple: pick up a programmable embedded system with USB support and use that as my password cracking platform. This embedded system is to be completely isolated (not connected to any computer or network) and powered up from a 5V adapter. The user just has to attach a usb disk with the encrypted file, press a button and let the platform crack away.

I chose the TI’s BeagleBone; a powerful linux ready 700MHz embedded system retailing for about $85. For more information I recommend checking out the “Getting Started Guide” by Matt Richardson @ Make magazine, in addition to the official BeagleBone webpage.

Here is a video of my PDF-file password cracker in action.

For all intents and purposes, the BeagleBone is just a very portable linux computer. This means I could write all the brute-force and all I/O routines directly in C and BASH. It worked great.
In terms of performance, the BeagleBone is able to test about 7,000 words per second, whereas my macbook pro can to about 25,000. Since, I am just using a brute approach… It takes about 16 minutes to test every single 4 letter word (uppercase+lowercase+symbols), and 900 minutes to test every single 5 letter word. Obviously there is plenty of room for improvement here, such as a dictionary based option and even customized multi-core architectures with many BeagleBone CPUs. This last option is the most interesting one. While the BeagleBone prototype system costs around $85, the CPU itself costs around $5. With some clever engineering, we could potentially have a dedicated 10 core system for less than $300.

Regarding the technical details of this project look here for the LCD component and here for the digital I/O button implementation.

Controlling a HD44780 LCD on a BeagleBone (Part 2)

On the previous post I described how a HD44780 can be integrated with a BeagleBone. For this post, I created a small HD44780 library for the BeagleBone that makes the process of writing text to a LCD as easy as it was on the Arduino.

First of all you need some files. Go to the github for this project (https://github.com/nunoalves/BeagleBone-HD44780) and place the following files into your BeagleBone:

  • beagle_gpio.c
  • beagle_gpio.h
  • beagle_hd44780.c
  • beagle_hd44780.h

    These files provide all the necessary LCD I/O. In this post we will be discussing the example_03.c and example_04.c files that are also available on that same repository.

    Once the files are on your BeagleBone, make all the appropriate connections, exactly as described on the previous post.

    Instead of describing every single function in detail, its probably easier if you look at the following code (example_03.c), which performs some very basic LCD operations.

    You can compile, and run, this code with the following commands:

    gcc -c beagle_gpio.c
    gcc -c beagle_hd44780.c
    gcc beagle_gpio.o beagle_hd44780.o example_03.c
    ./a.out

    This next sample code (example_04.c) shows how to write full strings and how to address a particular LCD position.



    Running the code from example_04.c on a BeagleBone with a HD44780 compatible LCD

    You can compile, and run, this code with the following commands:

    gcc -c beagle_gpio.c
    gcc -c beagle_hd44780.c
    gcc beagle_gpio.o beagle_hd44780.o example_04.c
    ./a.out

    Have fun!

  • Controlling a HD44780 LCD on a BeagleBone (Part 1)

    Hitachi HD44780 compatible LCD screens are those common displays we see all over the place. Getting them to display some information is fairly easy, as all you have to do is send the appropriate bits to each of the LCD screen inputs.

    Basic 16x2 Character LCD - HD44780 compatible

    In this post I will describe the steps I took to integrate a Hitachi HD44780 LCD with the BeagleBone board. First of all you need a LCD display. From my Arduino days, I had a couple of them laying around my lab. In this tutorial, I used a fairly inexpensive Microtivity LCD Module 1602. Because this LCD module requires a 5V source, and the BeagleBone GPIO pins only provide 3.3V, you need to get power from other sources: either connect a 5V DC adaptor to the BeagleBone and use P9_5 which provides +5V, or use any adapter that you have in your house to deliver the appropriate voltage with a voltage regulator. If you opt for the voltage regulator route, I recommend you get a 9V adaptor and use this neat sparkfun kit, just make sure the ground of this external voltage supply is connected to the ground pin of the BeagleBone. Of course you can bypass all these problems, by getting a +3.3V LCD display, but they will be more expensive.

    BeagleBone and a HD44780 compatible LCD

    Once you have acquired the LCD and connected to an appropriate power source, use the following connections.

    LCD Pin # LCD Pin Description LCD Pin Connects to
    1 VSS Ground
    2 VDD +5V
    3 VO Attach a potentiometer here to adjust contrast
    4 Register Select (RS) BeagleBone pin P8_4
    5 Read/Write (R/W) Ground
    6 Clock Enable (E) BeagleBone pin P8_3
    7 Data Bit 0 Unconnected
    8 Data Bit 1 Unconnected
    9 Data Bit 2 Unconnected
    10 Data Bit 3 Unconnected
    11 Data Bit 4 BeagleBone pin P8_5
    12 Data Bit 5 BeagleBone pin P8_11
    13 Data Bit 6 BeagleBone pin P8_12
    14 Data Bit 7 BeagleBone pin P8_14
    15 Backlight Anode (+) +5V (or lower)
    16 Backlight Cathode (-) Ground

    Source-Code
    The code below will prompt you to enter a bitstream into the LCD screen. Keep in mind that you need the library beagle_gpio.h, or else the program will not compile. You can find all the necessary files to run the program in the following github site (https://github.com/nunoalves/BeagleBone-HD44780). You can compile, and run, this code by typing the following commands in the BeagleBone terminal:

    gcc -c beagle_gpio.c
    gcc beagle_gpio.o example_01.c
    ./a.out
    rm -f a.out

    Once the code is compiled you will be asked to type the appropriate bitstream. If you type the following bits, you will type the word “hi” on the LCD.

    Type the following bitstream to display "hi" in the LCD

    If you would like to manually type different characters look at the following table:

    HD44780 LCD character table

    … And here is a screen shot of the program output.

    Screenshot of the program manually sending a bitstream to the HD44780.

    Finally, if you would like to know more about controlling these LCD devices I suggest this website, and the article “How to use intelligent L.C.D.s – Part One” by Julyan llett, which can be found here.

    In a future post, I will release another library that will allow us to automatically display text without manually typing the entire bitstream.

    Addressing multiple BeagleBone GPIO pins in C

    Since I am in the process of writing a HD44780 compatible LCD display driver for the beagle bone, I need to have a very simple piece of C code that writes data to multiple GPIO pins at the same time. In particular I need to have a function that when given a binary value, will turn ON/OFF may GPIO pins. For example, lets say the end-user is using the following 5 GPIO pins, P8_3, P8_4, P8_5, P8_11 and P8_12. Now lets say he just wants to turn ON pins P8_4 and P8_11, while leaving the others OFF. I created a neat piece of code that takes the pin selection in a binary format, and performs the appropriate ON/OFF switching operations. The latest version of this code can be found at the following github repository, https://github.com/nunoalves/BeagleBone-GPIO-in-C.

    While this code works quite well, it is not really complete. For starters, I haven’t included all BeagleBone GPIO pins. You can include them all yourself in the function initialize_each_enabled_gpio(). Please refer to this earlier blog post for hints about how to do it. I also haven’t split this code into a different header file, so it’s its own library. I will do so, after I release the drivers for the HD44780 LCD display. Finally, this code could need some cleaning and optimization…

    Here is a screenshot of the program running in the BeagleBone.

    To compile and run this code just type the following commands in the BeagleBone terminal.

    gcc main.c
    ./a.out

    Finally, for those of you that are Doxygen fans, here is a printout of the code documentation.