Saturday, December 21, 2013

Beaglebone Black GPIO - 8x8 LED Matrix with C Program - Part3

STEP-4 : Setting up the Start-up Script and make active at boot-up

  • In order to work with device tree overlay what we have created in previous step, we need to write a start-up script.
  • The C code we are going to use is have some issues with GPIO ports like "Bus Error" when the compiled file is executed.
  • Based on this website the issue got resolved when we export one pin on each GPIO port. So this we need to add it on start-up script.
  • This start-up script will be executed every time the Beaglebone Black is started.
  •  Make a start-up script with the following steps:
       root@beaglebone:# vi /usr/bin/init_boot.sh
   
        Copy and Pase following lines in "init_boot.sh" file - Download

 #!/bin/bash
echo 5 > /sys/class/gpio/export

echo 33 > /sys/class/gpio/export

echo 65 > /sys/class/gpio/export

echo 115 > /sys/class/gpio/export

export SLOTS=/sys/devices/bone_capemgr.8/slots

echo GPIO-Test > $SLOTS

  • To save and exit the file press "Esc" type ":wq" and Enter.
  • Now start-up script is ready to make active at booting process.
  • To make start-up script active at booting process, make a service with the following lines:
      root@beaglebone:# vi /lib/systemd/init_boot.service

[Unit]
Description=Enable GPIO Pins
After=syslog.target network.target

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

[Install]
WantedBy=multi-user.target


  • To save and exit the file press "Esc" type ":wq" and Enter.
  • Make a symbolic link

    root@beaglebone:# cd /etc/systemd/system/
    
root@beaglebone:# ln /lib/systemd/init_boot.service init_boot.service

  • Make systemd take notice of it, activate the service immediately and enable the service to be started on boot-up
   root@beaglebone:# systemctl daemon-reload
  
root@beaglebone:# systemctl start init_boot.service   
   root@beaglebone:# systemctl enable init_boot.service
  • Now the start-up script is active.
  • To check whether start-up script is active restart the beaglebone black.
  • Run following commands:
    To check GPIO pins 5, 33, 65 & 115 are exported:
   root@beaglebone:# ls -al /sys/class/gpio

root@beaglebone:# ls -al /sys/class/gpio/total 0
drwxr-xr-x  2 root root    0 Jan  1 05:00 .
drwxr-xr-x 48 root root    0 Jan  1 05:00 ..
--w-------  1 root root 4096 Jan  1 05:00 export

lrwxrwxrwx  1 root root    0 Jan  1 11:30 gpio115 -> ../../devices/virtual/gpio/gpio115
lrwxrwxrwx  1 root root    0 Jan  1 11:30 gpio33 -> ../../devices/virtual/gpio/gpio33
lrwxrwxrwx  1 root root    0 Jan  1 11:30 gpio5 -> ../../devices/virtual/gpio/gpio5
lrwxrwxrwx  1 root root    0 Jan  1 11:30 gpio65 -> ../../devices/virtual/gpio/gpio65
lrwxrwxrwx  1 root root    0 Jan  1 05:00 gpiochip0 -> ../../devices/virtual/gpio/gpiochip0
lrwxrwxrwx  1 root root    0 Jan  1 05:00 gpiochip32 -> ../../devices/virtual/gpio/gpiochip32
lrwxrwxrwx  1 root root    0 Jan  1 05:00 gpiochip64 -> ../../devices/virtual/gpio/gpiochip64
lrwxrwxrwx  1 root root    0 Jan  1 05:00 gpiochip96 -> ../../devices/virtual/gpio/gpiochip96
--w-------  1 root root 4096 Jan  1 05:00 unexport

   To check the device tree overlay is loaded:
   root@beaglebone:# cat /sys/devices/bone_capemgr.8/slots

 root@beaglebone:/lib/firmware# cat $SLOTS
 0: 54:PF---
 1: 55:PF---
 2: 56:PF---
 3: 57:PF---
 4: ff:P-O-- Bone-LT-eMMC-2G,00A0,Texas Instrument,BB-BONE-EMMC-2G
 5: ff:P-O-- Bone-Black-HDMI,00A0,Texas Instrument,BB-BONELT-HDMI
 6: ff:P-O-- Bone-Black-HDMIN,00A0,Texas Instrument,BB-BONELT-HDMIN
 7: ff:P-O-L Override Board Name,00A0,Override Manuf,GPIO-Test

  • It is loaded on 7th slot. 
STEP-5 : Proceed for C code
  • Download C Coded from Here

Beaglebone Black GPIO - 8x8 LED Matrix with C Program - Part2


STEP-3 : Setting the Pin mux and enabling the pullup or pull-down resistors on Port_8
  • With the help of Device tree overlay we can set the pin mux and enable the pullup or pulldown resistors.
  • Make a device tree overlay file with the following code:
//---------File Name:GPIO-Test.dts---------------
/dts-v1/;
/plugin/;

/{
       compatible = "ti,beaglebone", "ti,beaglebone-black";
       part-number = "GPIO-Test";
       version = "00A0";

       fragment@0 {
             target = <&am33xx_pinmux>;
           
             __overlay__ {
                  pinctrl_test: GPIO_Test {
            pinctrl-single,pins = <

                0x018 0x07 // P8_3 38 OUTPUT MODE7 pulldown
                0x01c 0x07 // P8_4 39 OUTPUT MODE7 pulldown
                0x008 0x07 // P8_5 34 OUTPUT MODE7 pulldown
                0x00c 0x07 // P8_6 35 OUTPUT MODE7 pulldown
                0x090 0x07 // P8_7 66 OUTPUT MODE7 pulldown
                0x094 0x07 // P8_8 67 OUTPUT MODE7 pulldown
                0x09c 0x07 // P8_9 69 OUTPUT MODE7 pulldown
                0x098 0x07 // P8_10 68 OUTPUT MODE7 pulldown
              
                0x034 0x17 // P8_11 45 OUTPUT MODE7 pullup
                0x030 0x17 // P8_12 44 OUTPUT MODE7 pullup
                0x024 0x17 // P8_13 23 OUTPUT MODE7 pullup
                0x028 0x17 // P8_14 26 OUTPUT MODE7 pullup
                0x03c 0x17 // P8_15 47 OUTPUT MODE7 pullup
                0x038 0x17 // P8_16 46 OUTPUT MODE7 pullup
                0x02c 0x17 // P8_17 27 OUTPUT MODE7 pullup
                0x08c 0x17 // P8_18 65 OUTPUT MODE7 pullup
              
              
                      
                    /* OUTPUT  GPIO(mode7) 0x07 pulldown, 0x17 pullup, 0x?f no pullup/down */
                   /* INPUT   GPIO(mode7) 0x27 pulldown, 0x37 pullup, 0x?f no pullup/down */

            >;
          };
             };
       };

       fragment@1 {
        target = <&ocp>;
        __overlay__ {
            test_helper: helper {
                compatible = "bone-pinmux-helper";
                pinctrl-names = "default";
                pinctrl-0 = <&pinctrl_test>;
                status = "okay";
            };
        };
    };
};



  • Save file with is name "GPIO-Test.dts",
  • Compiling the overlay from .dts to .dtbo  
       root@beaglebone:# dtc -O dtb -o GPIO-Test-00A0.dtbo -b 0 -@ GPIO-Test.dts

  • Now you need to copy it to /lib/firmware so it can be found.
      root@beaglebone:# cp GPIO-Test-00A0.dtbo /lib/firmware
  • We need to define SLOTS to make it active
      root@beaglebone:# echo SLOTS=/sys/devices/bone_capemgr.8/slots
  •  Finally to make it active in SLOTS
      root@beaglebone:# echo GPIO-Test > $SLOTS
  • You can check with following command
 root@beaglebone:/lib/firmware# cat $SLOTS
 0: 54:PF---
 1: 55:PF---
 2: 56:PF---
 3: 57:PF---
 4: ff:P-O-- Bone-LT-eMMC-2G,00A0,Texas Instrument,BB-BONE-EMMC-2G
 5: ff:P-O-- Bone-Black-HDMI,00A0,Texas Instrument,BB-BONELT-HDMI
 6: ff:P-O-- Bone-Black-HDMIN,00A0,Texas Instrument,BB-BONELT-HDMIN
 7: ff:P-O-L Override Board Name,00A0,Override Manuf,GPIO-Test

  • It is loaded on 7th slot.
  • You can download HERE the file created by me
  • You can modify and make your own device tree overlay with this file "GPIO-Test.dts" (Open file in text editor like)
                                                     Further info  See Part3 - To be Continued