Beaglebone black is a SOC using the ARM7 architecture running the Linux OS. The running kernel was adapted specifically for the embedded world. Interacting with click boards or other peripherals in real-time can be somewhat of a challenge. At times it can be very frustrating if this is your starting point in the embedded world. Advantages exist however, for this kind of development. Multi-threading and a wide choice of available languages are just a beginning. The Linux kernel contains all modules for well-known protocols ( I2C, SPI, UART ). For this discussion we are going to demonstrate how the MikroBUS cape can be used to expand the usage of the Beaglebone and also introduce the concept of the Device Tree.
With a mikroBUS Cape you can greatly extend the capabilities of your beaglebone black and have introduce new forms of interaction with the outside world. Capes have four MikroBUS sockets for adding sensors as well as a programmable EEPROM memory with a changeable I2C address. The idea behine the EEPROM on capes is to storage the cape identification information which can be used for automatic loading of a matched device tree for those capes defined in the kernel.
In this "How To" we will compile a previously made device tree for mikroBUS cape and afterwards we will load our device tree with the aid of the cape manager. Once loaded, we test with I2C protocol with an RTC click, an easy demonstration due to the kernel already having provisions for it.
Device Tree or shorter DT is a human readable data structure for describing hardware. DT was introduced to Beaglebone black with kernel 3.8 release with aims to improve earlier usage of the board-am335xevm.c. With more than 4000 lines of code needed to be edited for every new cape, that was a real challenge even for those professional kernel developers. With human readable DT files, most all serious issues are resolved. DT text files have the .dts extension and can be compiled with the DTC compiler that generates a .dtbo binary files usable by the beaglebone Cape Manager.
Since we already have BB-MIKROBUS-01-00A1.dts written by Daniel Sdei which works very surely, our task is to compile it with DTC compiler and load binary to the cape manager.
root@beaglebone:~# dtc -O dtb -o BB-MIKROBUS-01-00A1.dtbo -b 0 -@ BB-MIKROBUS-01-00A1.dts
The new compiled binary will show up in same folder with .dts . This binary needs to be copied to the /lib/firmware/ . Before enabling our device tree we must disable the HDMI port. This is because the HDMI port uses some of the pins that we also are using with cape. That will require editing of the /boot/uEnv.txt file. Add the following line to the file.
After this, we can reboot system and check if we have successfully disabled the HDMI port by reading the /sys/devices/bone_capemgr.9/slots file. A "L" letter means that device is loaded, so if your results are similar like this - with no L in front of HDMI slot, you are good.
root@beaglebone:~# cat /sys/devices/bone_capemgr.9/slots 0: 54:PF--- 1: 55:PF--- 2: 56:PF--- 3: 57:PF--- 4: ff:P-O-L Bone-LT-eMMC-2G,00A0,Texas Instrument,BB-BONE-EMMC-2G 5: ff:P-O-- Bone-Black-HDMI,00A0,Texas Instrument,BB-BONELT-HDMI
Finally, the time for loading our DT is here. First of all navigate to the /lib/firmware and then execute the following command to load our DT definition to the cape manager.
root@beaglebone:~# echo BB-MIKROBUS-01:00A1 > /sys/devices/bone_capemgr.9/slots
If we want to load and activate the DT during the boot process, add the following line to the /etc/default/capemgr and reboot the system.
After the system boot we can now check the final result
root@beaglebone:~# cat /sys/devices/bone_capemgr.9/slots 0: 54:PF--- 1: 55:PF--- 2: 56:PF--- 3: 57:PF--- 4: ff:P-O-L Bone-LT-eMMC-2G,00A0,Texas Instrument,BB-BONE-EMMC-2G 5: ff:P-O-- Bone-Black-HDMI,00A0,Texas Instrument,BB-BONELT-HDMI 8: ff:P-O-L Override Board Name,00A1,Override Manuf,BB-MIKROBUS-01
As you can see, there is a new device tree at slot 8 of cape manager. To check if we have all needed devices, we should list contents of /dev/ folder.
root@beaglebone:~# ls /dev/spi* /dev/spidev1.0 /dev/spidev1.1 root@beaglebone:~# ls /dev/i2c* /dev/i2c-0 /dev/i2c-1 root@beaglebone:~# ls /dev/ttyO* /dev/ttyO0 /dev/ttyO1 /dev/ttyO2 /dev/ttyO4
spidev1.x are SPI buses available.
i2c-x are I2C buses available.
ttyOx are UART channels available.
Test the Communication with the RTC2 click
The RTC2 click board features a well-known DS1307 serial real-time clock already supported by kernel. We will only test the communication between the click board and the OS itself. To load the kernel module for the RTC2 click execute following command.
root@beaglebone:~# echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
Let's try to read the time from RTC - to do that we should execute root@beaglebone:~# hwclock -r -f /dev/rtc1
If no errors are printed then everything is performing correctly and we have successfully loaded our device tree and our cape is ready for use. If the module is new, more than likely the time and date are incorrect.
With a properly loaded device tree you can work at developing applications that uses up to 4 different clicks at the same time. Also with mikroBUS cape you can extend your Beaglebone with more than two hundred different click boards.