snifer

【原创】基于嵌入式系统的模块化编程体验

0
阅读(1791)

寒假很忙,今天给一个单位做了一个讲座,探讨了在嵌入式系统中模块化编程的研究,下面我就写写这个简单的项目。

这个项目就是要编写一个最基本的模块,加载模块,观察结果。从而掌握模块的基本要素及加载、卸载、查看工具的使用。

主机Ubuntu 10.10。

编写程序:

/*
 * hello.c
 *
 * Simple hello world 2.6 driver module
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */


#include <linux/module.h>
#include <linux/kernel.h>

MODULE_LICENSE ("GPL");

int __init init_module (void)
{
  printk (KERN_INFO "Hello world\n");
  return 0;
}

void __exit cleanup_module (void)
{
  printk (KERN_INFO "Goodbye world\n");
}


然后:

1、将文件夹ex1-hello-world复制到linux环境中,如:/home/linux/test

$su root

$cp ex1-hello-world  /home/linux/test –a

2、$cd /home/linux/test/ex1-hello-world

3、$make

4、通过insmod命令将模块加入内核

$insmod  insmod hello.ko

5、通过lsmod查看内核模块

$ lsmod | grep hello

6、通过rmmod删除内核中的模块

$ rmmod hello

7、查看源码,适当修改,掌握实验内容