How to create your own command

Version 3

    Sometimes we have to set certain things in the Linux environment which take a lot of steps. For example if you would like to manually set Edison’s serial port, you would have to go to the Intel® Edison Kit for Arduino* Hardware Guide and check table three in order to know how to configure the board’s muxing. If we were to do so, we would have to enter all the following commands:

     

    echo -n "130" > /sys/class/gpio/export
    echo -n "248" > /sys/class/gpio/export
    echo -n "216" > /sys/class/gpio/export
    echo -n "214" > /sys/class/gpio/export
    echo low > /sys/class/gpio/gpio214/direction
    echo low > /sys/class/gpio/gpio248/direction
    echo in > /sys/class/gpio/gpio216/direction
    echo mode1 > /sys/kernel/debug/gpio_debug/gpio130/current_pinmux
    echo in > /sys/class/gpio/gpio130/direction
    echo high > /sys/class/gpio/gpio214/direction
    echo -n "131" > /sys/class/gpio/export
    echo -n "249" > /sys/class/gpio/export
    echo -n "217" > /sys/class/gpio/export
    echo -n "214" > /sys/class/gpio/export
    echo low > /sys/class/gpio/gpio214/direction
    echo high > /sys/class/gpio/gpio249/direction
    echo in > /sys/class/gpio/gpio217/direction
    echo mode1 > /sys/kernel/debug/gpio_debug/gpio131/current_pinmux
    echo out > /sys/class/gpio/gpio131/direction
    echo high > /sys/class/gpio/gpio214/direction
    stty -F /dev/ttyMFD1 9600
    

     

    That is a total of 21 lines, just to configure the serial port and its baud rate. So, right now you must be thinking, that it is not possible that such a simple process has to be so tedious. And you would be right. The advantage of having Linux as an embedded OS in our Edison is that you have all the possibilities you would have on a Linux PC. Creating a custom command in Linux is very simple and it would make setting up the serial port much less tedious.

    The first thing we’ll need for our command is a script. In this case we’ll use a shell scrip, however this is not the only option we have available. The script we’ll use will set up the serial port and its baud rate. We’ll use the same commands as above:

     

    #!/bin/sh
    echo -n "130" > /sys/class/gpio/export
    echo -n "248" > /sys/class/gpio/export
    echo -n "216" > /sys/class/gpio/export
    echo -n "214" > /sys/class/gpio/export
    echo low > /sys/class/gpio/gpio214/direction
    echo low > /sys/class/gpio/gpio248/direction
    echo in > /sys/class/gpio/gpio216/direction
    echo mode1 > /sys/kernel/debug/gpio_debug/gpio130/current_pinmux
    echo in > /sys/class/gpio/gpio130/direction
    echo high > /sys/class/gpio/gpio214/direction
    sleep 1
    echo -n "131" > /sys/class/gpio/export
    echo -n "249" > /sys/class/gpio/export
    echo -n "217" > /sys/class/gpio/export
    echo -n "214" > /sys/class/gpio/export
    echo low > /sys/class/gpio/gpio214/direction
    echo high > /sys/class/gpio/gpio249/direction
    echo in > /sys/class/gpio/gpio217/direction
    echo mode1 > /sys/kernel/debug/gpio_debug/gpio131/current_pinmux
    echo out > /sys/class/gpio/gpio131/direction
    echo high > /sys/class/gpio/gpio214/direction
    stty -F /dev/ttyMFD1 9600
    echo "UART configuration complete"
    

     

    In this case we’ll simply name this file uart. It’s important to point out that the name of this file will be the name of the command we create.

    Once the file is created we need to give executable rights to it. This can be achieved with the command chmod +x uart. When your command is complete and it has executable rights it is ready to be moved. The reason why we need to move the file is because a command should be able to be ran from anywhere on Edison’s file system. So, now you simply have to run the command mv uart /bin/.

    Now you should be able to run your custom command on your board.