BBB Board lesson Four: Simple LED control experiment
Learn BBB board the ultimate goal is to imagine the microcontroller as the control, but the control of the idea is completely different (Linux control hardware device is the virtual file of the relevant equipment, read and write), the study of a few days the head is big or no progress, many blog posts on the web, there is no unified statement, It is estimated that the years of the BBB board are different and that the installed system versions are different. Finally select exemplary, starting from the most basic single command control IO interface.
reach target: Outputs 0 or 1 from Gpio ports P8.12 and P8.11 pins
This lesson uses two commands, Cat and Echo, the specific way to use many online, the simplest understanding: cat used to view the file (variable) value, and Echo to write the file (variable) value.
The Operation IO Port is a series of read and write operations to the files in the/sys/class/gpio directory, because the Gpio drivers are loaded by default, so you do not need to load the device tree (the machine trees, later learned) to implement the operation.
Simply stated, the/sys directory contains the system kernel and driver-related content, it is a virtual file system, the file system directory structure is essentially the kernel object model tree. We can read and write the files here to achieve the operation of the device. As to what files are in each device, which files can be manipulated, or what the device driver defines.
System boot after the/sys/class/gpio directory is only the default several files directory, BBB board a total of more than 100 available Gpio port and not a list out, Linux under the control IO port is read and write Io file, so first you have to find the corresponding Gpio file, In fact, through the echo command to the required control of the GPIO port is set to a usable state,/sys/class/gpio directory can display the relevant files (simple understanding is the original port control file stealth, through the echo command to show it)
1. Using P8.12 pin to control LED light
Run echo > Export in the/sys/class/gpio directory, meaning P8.12 is set to available status
This "44" number is through the "BBB Pin function speed Check table", find P8.12 corresponding gpio.no is 44
After executing this command and then querying the current directory file, a gpio44 directory will appear:
Ready a small light, two lines, universal board (no also nothing, can connect the light on the line), I use the P8.12 port, P8.2 negative side.
List the 7 files in the gpio44 directory with Dir, where "direction" is the file (parameter) that sets the port as input in or out, "value" is the port value 0 or 1 (low or high), we can read and write these two files directly.
Use the cat command to view the current value, and then modify it to the desired value by using the Echo command, noting that the command uses formatting. Because the purpose of this simple experiment is to achieve indicator lights and lights out operation, "direction" to set to out, through the command catdirection view to the original value of in, so use the command echo out > Direction set to Out.
The diode light can then be lit or switched off by changing the value of the values.
Input: Echo 1 >value light on
Input: Echo 0 >value light off
The process feels very simple, but it is difficult to understand. Can only understand the character Terminal Mode command operation, understand the control principle can be in C programming freely.
2. Using P8.11 pin to control LED light
Run echo > Export in the/sys/class/gpio directory, meaning P8.11 is set to available status
This "45" number is also through the "BBB Pin function speed Check table", find P8.11 corresponding gpio.no is 45
After executing this command and then querying the current directory file, a gpio45 directory will appear:
The control process is the same, it is no longer written. Take a few pictures and look at it.
3, through the experiment we must understand several points:
A Controlling the Gpio port output 0 or 1 of the BBB board is to modify the value of the values file under the corresponding gpioxxx in the/sys/class/gpio directory
B Changing the input and output mode of the Gpio port is to modify the value of the direction file under the corresponding gpioxxx in the/sys/class/gpio directory to be in or out
C The entire BBB board hardware Device control by reading and writing the corresponding Linux device virtual files to achieve, so learning BBB board file Structure system is very important
D Know the correct use of the BBB Pin function quick Check table
E. As in this experiment, the GPIO port driver is loaded because the system is loading by default, so it does not require the device tree to be loaded, but other interfaces such as ADCs and UART must first load the relevant device tree before operation. I am also vague about this device tree, and I will discuss it later.
F. Learn how to use the Cat and Echo commands flexibly