In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of Android how to develop the first driver, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this Android how to develop the first driver article. Let's take a look at it.
1. Driver program
Frist_driver.c
# include # define HELLOON 1#define HELLOOFF 0 # define HELLO_CNT 1#define HELLO_NAME "hello driver" static char readbuf; static char writebuf [100]; static char kerneldata [] = {"hello my first driver"}; static char mybuf [100] = "1234"; struct hello_driver {dev_t devid / * device number * / struct cdev cdev; / * cdev*/struct class * class; / * Class * / struct device * device; / * device * / int major; / * Primary device number * / int minor; / * Secondary device number * / atomic_t atomic_lock / * Atomic variable * /}; struct hello_driver hello_driver_dev;static ssize_t show_my_device (struct device * dev,struct device_attribute * attr, char * buf) {return sprintf (buf, "% s\ n", mybuf);} static ssize_t set_my_device (struct device * dev,struct device_attribute * attr, const char * buf, size_t len) / / echo command, the function {sprintf (mybuf, "% s", buf) will be called; return len } static DEVICE_ATTR (my_device_test, S_IWUSR | S_IRUSR, show_my_device, set_my_device); / / define a device properties file named my_device_test static int hello_driver_open (struct inode * inode, struct file * filp) {if (! atomic_dec_and_test (& hello_driver_dev.atomic_lock)) {atomic_inc (& hello_driver_dev.atomic_lock); return-EBUSY } filp- > private_data = & hello_driver_dev;return 0;} static ssize_t hello_driver_read (struct file * filp, char _ _ user * buf, size_t cnt, loff_t * offt) {int ret = 0 size_t cnt (kerneldata); ret = copy_to_user (buf,readbuf,cnt); if (ret = = 0) {printk ("kernel senddata ok!\ n");} else {printk ("kernel senddata failed\ n");} return 0 } static ssize_t hello_driver_write (struct file * filp, const char _ user * buf, size_t cnt, loff_t * offt) {int ret = 0posit = copy_from_user (writebuf,buf,cnt); if (ret = = 0) {printk ("kernel recvdata% s\ n", writebuf);} else {printk ("kernel recvdata failed\ n");} return 0 } static long hello_driver_unlocked_ioctl (struct file * filp, unsigned int cmd, unsigned long arg) {switch (cmd) {case HELLOON: printk ("Open hello driver\ n"); break;case HELLOOFF: printk ("Close hello driver\ n"); break;default:break;} return 0;} static int hello_driver_release (struct inode * inode, struct file * filp) {struct hello_driver * dev = filp- > private_data;atomic_inc (& dev- > atomic_lock); return 0 } static struct file_operations hello_driver_fops= {.owner = THIS_MODULE,.open = hello_driver_open, .read = hello_driver_read,.write = hello_driver_write,.unlocked_ioctl = hello_driver_unlocked_ioctl,.release = hello_driver_release,}; static int _ init hello_driver_init (void) {int ret;/*1. Set the atomic variable to ensure that only one application can use the driver * / atomic_set (& hello_driver_dev.atomic_lock,1); / * 2 at a time. The primary device number * / if (hello_driver_dev.major) {/ * registered device number * / hello_driver_dev.devid = MKDEV (hello_driver_dev.major,0); ret = register_chrdev_region (hello_driver_dev.devid, HELLO_CNT, HELLO_NAME); if (ret) was assigned before assigning the device number * / / * 2.1
< 0){printk("can't register major\n"); return ret; }}else/*2.2 之前未分配设备号,向内核申请设备号*/{ alloc_chrdev_region(&hello_driver_dev.devid, 0, HELLO_CNT, HELLO_NAME); }printk(KERN_ERR"hello_driver_dev major = %d, minor = %d\n", hello_driver_dev.major,hello_driver_dev.minor);hello_driver_dev.cdev.owner = THIS_MODULE;cdev_init(&hello_driver_dev.cdev,&hello_driver_fops);ret = cdev_add(&hello_driver_dev.cdev, hello_driver_dev.devid,HELLO_CNT);if(ret){printk("Error cdev_add\n"); goto fail_to_cdev_add;}hello_driver_dev.class = class_create(THIS_MODULE,HELLO_NAME);if(IS_ERR(hello_driver_dev.class)){goto fail_to_class_create;}hello_driver_dev.device = device_create(hello_driver_dev.class , NULL, hello_driver_dev.devid, NULL, HELLO_NAME);if(IS_ERR(hello_driver_dev.device)){goto fail_to_device_create;} if(sysfs_create_file(&hello_driver_dev.device ->(kobj), & dev_attr_my_device_test.attr) {/ / create a my_device_test properties file return-1 under the mytest_device device directory;} return 0 switch to fail to create a my_device_test property region (hello_driver_dev.devid, HELLO_CNT); return-1 to fail to classically create a my_device_test property Unregister_chrdev_region (hello_driver_dev.devid, HELLO_CNT); return-1; fail_to_device_create:cdev_del (& hello_driver_dev.cdev); unregister_chrdev_region (hello_driver_dev.devid, HELLO_CNT); class_destroy (hello_driver_dev.class); return-1;} static void _ exit hello_driver_exit (void) {cdev_del (& hello_driver_dev.cdev); unregister_chrdev_region (hello_driver_dev.devid, HELLO_CNT) Device_destroy (hello_driver_dev.class, hello_driver_dev.devid); class_destroy (hello_driver_dev.class);} module_init (hello_driver_init); module_exit (hello_driver_exit); MODULE_DESCRIPTION ("my hello driver"); MODULE_AUTHOR ("Kong Lingze"); MODULE_LICENSE ("GPL"); 2. Load the driver and compile to the kernel add folder
Add a new folder frist_driver in the kernel/driver directory and a file Kconfig,Makefile,frist_driver.c in frist_driver
What is in the frist_driver/Kconfig:
What is in the frist_driver/Makefile:
Add content to driver/Kconfig:
Add content to driver/Makefile:
Execute make menuconfig under kernel/driver and select First Android Driver
Source build/envsetup.sh under the Android source directory
Lunch xx xxxx (optional)
Make bootimage-J32
Test:
Execute fastboot flash boot boot.img to burn the kernel to the development board, find sys/class/hello driver/hello driver or the following directory in the development board directory, and execute the command shown in the figure
The test succeeded
3. Test with c program
First, create a new helloapp directory under the packages directory of the android source code, and create two new files, hello_app.c and Android.mk, under this directory.
Hello_app.c#include # define READ 3#define WRITE 4#define HELLO_ON 1#define HELLO_OFF 0Grady /. / app / dev/hello\ driver static unsigned char cUserData [] = {"User data"}; int main (int argc, char * * argv) {int iFd;int iRet;char * cFilename;unsigned int arg = 0ntran unsigned char cReadBuf; unsigned char cWriteBuf If (argceous nodes 3) {Printf ("Usage:%s", argv [0]);} / / Open device node cFilename = argv [1]; iFd = open (cFilename, O_RDWR); if (iFd < 0) {printf ("open% s failed\ n", cFilename); return-1;} / * read data in the driver * / if (atoi (argv [2]) = READ) {memset (cReadBuf,sizeof (cReadBuf), sizeof (cReadBuf)); iRet = read (iFd,cReadBuf,sizeof (cReadBuf)) If (iRet < 0) {printf ("read% s data failed\ n", cFilename); return-1;} else {printf ("read data is:%s\ n", cReadBuf);}} else if (atoi (argv [2]) = = WRITE) / / write data {memset (cWriteBuf,sizeof (cWriteBuf), sizeof (cWriteBuf)); memcpy (cWriteBuf,cUserData,sizeof (cUserData)); iRet = write (iFd,cWriteBuf,sizeof (cWriteBuf)) to the driver If (iRet < 0) {printf ("write% s data failed\ n", cFilename); return-1;}} else if (atoi (argv [2]) = = HELLO_ON) / / send HELLO_ON command {ioctl (iFd,HELLO_ON,arg) to the driver;} else if (atoi (argv [2]) = = HELLO_OFF) / / send HELLO_OFF command {ioctl (iFd,HELLO_OFF,arg);} iRet = close (iFd) to the driver If (iFd < 0) {printf ("close% s failed", cFilename); return-1;} return 0;} Andoird.mkLOCAL_PATH: = $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE_TAGS: = optionalLOCAL_MODULE: = hellotestLOCAL_SRC_FILES: = $(call all-subdir-c-files) include $(BUILD_EXECUTABLE) compilation
After completion, type source build\ envser.up.sh in the Android source directory, and then enter the packages/helloapp directory to execute the mm command to generate an executable file. The compiled file is in out/target/product/xxx/obj/EXECUTABLES/hellotapp_intermediates/.
Test adb shelladb push hello_app / data to push hello_app to the data directory of the android device, and chmod + x helloapp adds executable permissions. Execute. / helloapp / dev/hello driver 1
Print as follows:
Open hello driver
We can see that the test was successful.
This is the end of the article on "how to develop the first driver for Android". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to develop the first driver in Android". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.