Ver Fonte

手动加载模块,卸载模块。lsmod insmod rmmod

xuxinyi há 1 ano atrás
pai
commit
6ed8ce9988
3 ficheiros alterados com 41 adições e 120 exclusões
  1. 19 1
      开发笔记/开发笔记.md
  2. 21 119
      驱动文件/1/leds-user-defind.c
  3. 1 0
      驱动文件/Makefile

+ 19 - 1
开发笔记/开发笔记.md

@@ -148,4 +148,22 @@
         ![img_10.png](img_10.png
         交换文件,可以采用这两种方法,哪种方法好,自己选择。
         1.xshell+xfile 
-        2.vscode+samba
+        2.vscode+samba
+22.编译成模块(.ko)文件
+    obj-m+= leds-user-defind.o
+    将模块导入泰山派的linux系统中
+    adb push xx\xx.ko /data/xx.ko
+23.加载模块
+    1.进入adb shell 
+        cd /data
+    2.找到xx.ko文件
+        lsmod 查看已存在的module
+        insmod xx.ko
+    3.lsmod 查看是否加载成功
+        ![alt text](image-6.png)
+    4.查看模块打印的数据
+        dmesg | grep "hello"
+        ![alt text](image-7.png)
+    5.卸载模块
+        rmmod xx
+        ![alt text](image-8.png)

+ 21 - 119
驱动文件/1/leds-user-defind.c

@@ -1,127 +1,29 @@
 #include <linux/module.h>
 #include <linux/init.h>
-#include <linux/of.h>
-#include <linux/platform_device.h>
-#include <linux/timer.h>
 
-struct my_device_data {
-    struct timer_list my_timer;
-    struct platform_device *pdev;  // 添加指向 platform_device 的指针
-};
+static int majorNumber;                  // 设备号
+#define CLASS_NAME "my_char_class"
+static int __init helloworld_init(void) {
+ pr_info("Hello world initialization!\n");
 
-void my_timer_callback(struct timer_list *t) {
-    struct my_device_data *data = from_timer(data, t, my_timer);
-    dev_info(&data->pdev->dev, "hello world\n");  // 通过指针访问设备
-
-    // 重新启动定时器
-    mod_timer(&data->my_timer, jiffies + msecs_to_jiffies(10000));
-}
-
-static int my_device_probe(struct platform_device *pdev) {
-    struct my_device_data *data;
-    struct device_node *np = pdev->dev.of_node;
-
-    // 分配设备数据结构
-    data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
-    if (!data)
-        return -ENOMEM;
-
-    // 存储 platform_device 指针
-    data->pdev = pdev;
-
-    // 初始化定时器
-    timer_setup(&data->my_timer, my_timer_callback, 0);
-
-    // 启动定时器
-    mod_timer(&data->my_timer, jiffies + msecs_to_jiffies(10000));
-
-    // 存储设备数据指针
-    platform_set_drvdata(pdev, data);
-
-    dev_info(&pdev->dev, "设备匹配: %s\n", np->name);
-
-    return 0;
+ return 0;
 }
-
-static int my_device_remove(struct platform_device *pdev) {
-    struct my_device_data *data = platform_get_drvdata(pdev);
-
-    // 删除定时器
-    del_timer_sync(&data->my_timer);
-
-    dev_info(&pdev->dev, "设备移除\n");
-    return 0;
+static void __exit helloworld_exit(void) {
+    pr_info("Hello world exit\n");
+    mejornNumber = register_chrdev(0, "hello");
+    if (mejorNumber < 0)
+    {
+        printk(KERN_ALERT "Registering device failed with %d\n",mejorNumber );
+        return majorNumber
+    }
+    printk(KERN_INFO "I was assigned major number %d. To talk to\n",mejorNumber );
+
+    //创建设备类
+    my_class = class_create(THIS_MODULE, "hello_class");
+    
 }
-
-static const struct of_device_id my_device_ids[] = {
-    { .compatible = "mynode", },
-    { /* sentinel */ }
-};
-
-MODULE_DEVICE_TABLE(of, my_device_ids);
-
-static struct platform_driver my_device_driver = {
-    .probe = my_device_probe,
-    .remove = my_device_remove,
-    .driver = {
-        .name = "my_device_driver",
-        .of_match_table = my_device_ids,
-        .owner = THIS_MODULE,
-    },
-};
-
-module_platform_driver(my_device_driver);
-
+module_init(helloworld_init);
+module_exit(helloworld_exit);
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("John Madieu <john.madieu@gmail.com>");
-MODULE_DESCRIPTION("示例设备树驱动,每10秒打印一次hello world");
-
-
-private fun addAndUploadPhoto(path: String, context: Context, activity: Activity, ok: (String) -> Unit) {
-    "addAndUploadPhoto path:${path}".logDebug()
-    uploadTag = path
-
-    val file = File(path)
-    val requestBody = RequestBody.create(MediaType.parse("image/*"), file)
-    val body = MultipartBody.Part.createFormData("file", file.name, requestBody)
-
-    val client = OkHttpClient()
-    val url = "https://your-api-endpoint/admin/sys-file/upload"
-
-    val request = Request.Builder()
-        .url(url)
-        .post(body)
-        .build()
-
-    client.newCall(request).enqueue(object : Callback {
-        override fun onFailure(call: Call, e: IOException) {
-            activity.runOnUiThread {
-                "图片上传失败".toastShort()
-                mLoadingDialog?.dismiss()
-                mLoadingDialog = null
-            }
-        }
-
-        override fun onResponse(call: Call, response: Response) {
-            if (response.isSuccessful) {
-                val responseBody = response.body()?.string()
-                val jsonObject = JSONObject(responseBody)
-                val filePath = jsonObject.getString("filePath")
-
-                activity.runOnUiThread {
-                    "https://your-api-endpoint/$filePath".logDebug()
-                    ok.invoke("https://your-api-endpoint/$filePath")
-                    "上传成功".toastShort()
-                    mLoadingDialog?.dismiss()
-                    mLoadingDialog = null
-                }
-            } else {
-                activity.runOnUiThread {
-                    "图片上传失败".toastShort()
-                    mLoadingDialog?.dismiss()
-                    mLoadingDialog = null
-                }
-            }
-        }
-    })
-}
+MODULE_DESCRIPTION("Linux kernel module skeleton");

+ 1 - 0
驱动文件/Makefile

@@ -93,3 +93,4 @@ obj-$(CONFIG_LEDS_TRIGGERS)		+= trigger/
 
 #新增
 obj-y+= newchrled.o
+obj-m+= leds-user-defind.o