Control LED light equipment
Driver
Essence: create corresponding device node files for hardware devices
When creating equipment files, specify the use of equipment files.
application program
Control the hardware according to the device file usage specified by the driver
Control hardware equipment steps
1. Find the device node file corresponding to the hardware device
Two places:
-
/dev directory
Engineers familiar with the driver can use a device node file to control all the features of the hardware
-
/sys directory
Amateur engineers use a device node file to control only one feature of the hardware
Strictly speaking, the file below it is the hardware operation interface exported from Linux kernel to user space
2. Find out how to use the device file specified by the driver
LED lamp program
Device node file: / sys/class/leds
Write a value to the brightness file to control the brightness of the led lamp
led brightness value: 0 ~ 255
LED application building steps
1. Put a part first_ 1 copy to shared folder of NFS server
sudo cp -r makefile/part_1 workdir
2. Then check the Linux local mount directory and read it successfully
3. Find the file path of the LED device
4. Next, write code on Ubuntu
First create the project file directory
Create a part in a shared folder_ 1 as the project directory, add the header file directory, source file directory and Makefile file. As follows:
Write main in module1 C Documents
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> //ARM development board LED device path #define RLED_DEV_PATH "/sys/class/leds/red/brightness" #define GLED_DEV_PATH "/sys/class/leds/green/brightness" #define BLED_DEV_PATH "/sys/class/leds/blue/brightness" int main(int argc, char *argv[]) { int res = 0; int r_fd, g_fd, b_fd; printf("This is the led demon\r\n"); //Gets the device file descriptor for the red light r_fd = open(RLED_DEV_PATH, O_WRONLY); if(r_fd < 0) { printf("Fail to open %s device\r\n", RLED_DEV_PATH); } //Gets the device file descriptor for the green light g_fd = open(GLED_DEV_PATH, O_WRONLY); if(g_fd < 0) { printf("Fail to open %s device\r\n", GLED_DEV_PATH); } //Gets the device file descriptor of the blue light b_fd = open(BLED_DEV_PATH, O_WRONLY); if(b_fd < 0) { printf("Fail to open %s device\r\n", BLED_DEV_PATH); } while (1) { //red light write(r_fd, "255", 3); sleep(1); write(r_fd, "0", 1); //green light write(g_fd, "255", 3); sleep(1); write(g_fd, "0", 1); //Blue light write(b_fd, "255", 3); sleep(1); write(b_fd, "0", 1); } }
Then write the Makefile file and modify it with the previous one
ARCH ?= x86 ifeq ($(ARCH),x86) CC=gcc else #Compile on the development board CC=arm-linux-gnueabihf-gcc endif #Change the program name to led TARGET=led #Compile path BUILD_DIR=build #Source file path SRC_DIR=module1 #Header file path INC_DIR=include CFLAGS=$(patsubst %,-I%,$(INC_DIR)) INCLUDES=$(foreach dir,$(INC_DIR),$(wildcard $(dir)/*.h)) SOURCES=$(foreach dir,$(SRC_DIR),$(wildcard $(dir)/*.c)) OBJS=$(patsubst %.c,$(BUILD_DIR)/%.o,$(notdir $(SOURCES))) #Let make find files in the specified directory when the current directory cannot be found VPATH=$(SRC_DIR) $(BUILD_DIR)/$(TARGET):$(OBJS) $(CC) $^ -o $@ $(BUILD_DIR)/%.o:%.c $(INCLUDES) | creat_build $(CC) -c $< -o $@ $(CFLAGS) .PHONY:clean create_build clean: sudo rm -r $(BUILD_DIR) creat_build: sudo mkdir -p $(BUILD_DIR)
5. On MobaX, store the part in the local mount directory_ 1 copy the project to the / home/user directory of the development board and compile it. Before compiling programs on the development board, you need to install gcc and make tools in advance.
sudo apt install gcc/make