彙總地址:https://blog.csdn.net/chichi123137/article/details/80946381
#include <linux/module.h>
#include <linux/ioport.h>
#include <linux/io.h>
#include <linux/platform_device.h>
#include <linux/init.h>
#include <linux/serial_core.h>
#include <linux/serial.h>
#include <asm/irq.h>
#include <mach/hardware.h>
#include <plat/regs-serial.h>
#include <mach/regs-gpio.h>
#include <asm/uaccess.h>
/*
*編寫框架
*1 入口函數
* 1.1 註冊字符設備(存在於/dev目錄下)
* 1.2 創建一個類
* 1.3 在這個類下創建一個設備(存在於/class目錄下,用來代替mknod命令)
* 1.4 映射物理基地址爲虛擬地址,一個控制寄存器地址,一個數據寄存器地址
*2 出口函數
* 2.1 卸載字符設備
* 2.2 卸載class下的設備
* 2.3 銷燬這個類
* 2.4 取消地址映射
*3 構造file_operations
* 3.1 實現first_drv_open函數
* 配置GPF0,1,2,3爲輸出模式(用來點燈)
* 3.2 實現first_drv_write函數
* 拷貝用戶空間數據
* 如果用戶空間傳來1,輸出低電平,點燈
* 如果用戶空間傳來0,輸出高電平,滅燈
*/
static struct class *firstdrv_class;
static struct class_device *firstdrv_class_dev;
volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;
static int first_drv_open(struct inode *inode, struct file *file)
{
//printk("first_drv_open\n");
/* 配置GPF0,1,2,3爲輸出 */
*gpfcon &= ~((0x3<<(0*2)) | (0x3<<(1*2)) | (0x3<<(2*2)) | (0x3<<(3*2)));
*gpfcon |= ((0x1<<(0*2)) | (0x1<<(1*2)) | (0x1<<(2*2)) | (0x1<<(3*2)));
return 0;
}
static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
int val;
//printk("first_drv_write\n");
copy_from_user(&val, buf, count); // copy_to_user();
if (val == 1)
{
// 點燈
*gpfdat &= ~((1<<0) | (1<<1) | (1<<2) | (1<<3));
}
else
{
// 滅燈
*gpfdat |= (1<<0) | (1<<1) | (1<<2) | (1<<3);
}
return 0;
}
// 定義操作函數結構體
static struct file_operations first_drv_fops = {
.owner = THIS_MODULE, /* 這是一個宏,推向編譯模塊時自動創建的__this_module變量 */
.open = first_drv_open,
.write = first_drv_write,
};
int major;
//入口函數
static int first_drv_init(void)
{
/*
* 1 註冊字符設備(存在於/dev目錄下)
* 2 創建一個類
* 3 在這個類下創建一個設備(存在於/class目錄下,用來代替mknod命令)
* 4 映射物理基地址爲虛擬地址,一個控制寄存器地址,一個數據寄存器地址
*/
major = register_chrdev(0, "first_drv", &first_drv_fops); // 註冊, 告訴內核
firstdrv_class = class_create(THIS_MODULE, "firstdrv");
firstdrv_class_dev = device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
return 0;
}
//出口函數
static void first_drv_exit(void)
{
/* 主要是和出口函數的反向操作
* 1 卸載字符設備
* 2 卸載class下的設備
* 3 銷燬這個類
* 4 取消地址映射
*/
unregister_chrdev(major, "first_drv"); // 卸載
/* 2.6.31 上只有device_create 其他版本內核可能有class_device_create */
device_unregister(firstdrv_class_dev);
class_destroy(firstdrv_class);
iounmap(gpfcon);
}
//修飾first_drv_init函數爲入口函數
//修飾first_drv_exit函數爲出口函數
module_init(first_drv_init);
module_exit(first_drv_exit);
//使得本模塊爲支持GPL協議
MODULE_LICENSE("GPL");
上面是led驅動程序
下面爲led測試程序
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
/* firstdrvtest on
* firstdrvtest off
*/
int main(int argc, char **argv)
{
int fd;
int val = 1;
fd = open("/dev/xyz", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
}
if (argc != 2)
{
printf("Usage :\n");
printf("%s <on|off>\n", argv[0]);
return 0;
}
if (strcmp(argv[1], "on") == 0)
{
val = 1;
}
else
{
val = 0;
}
write(fd, &val, 4);
return 0;
}
makefile如下
KERN_DIR = /home/linux/tools/linux-2.6.31
all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
obj-m += driver_module.o
整體目錄結構如下:
1 在menuconfig中配置好內核源碼的目標系統爲s3c2440,在makefile中配置好交叉編譯器爲arm-linux-
2 在pc上將驅動程序編譯生成.ko
3 在pc上將測試程序編譯生成elf可執行文件
4 掛載nfs,這樣就可以在開發板上看到pc端的.ko文件和測試文件
mount -t nfs -o nolock,vers=2 192.168.0.103:/home/linux/nfs_root /mnt
執行結果如下:
加載模塊後,執行./firstdrvtest on 命令,燈亮,執行./firstdrvtest off命令,燈滅
如何根據原理圖查詢寄存器
1, 先看主板的原理圖:Tx2440底板 V3.pdf
再看核心板的原理圖:COREBOARD .pdf
查詢 s3c2440 手冊GPF的寄存器地址,主要關注控制寄存器和數據寄存器
控制寄存器用來控制是輸入、輸出還是中斷模式
數據寄存器用來收發數據
TX2440A用的GPF0,1,2,3,只要配置成輸出模式,根據用戶輸入往數據寄存器寫數據就ok了。
查詢如何設置GPF引腳爲輸出引腳