2008年4月14日 星期一

在PXA270開發板,於【Step by Step講授嵌入式開發板Linux驅動程式實務班】上課時完成的 Linux Device Driver 範例

在 ARM PXA270 開發板上, OS 是 Linux 2.6 的 Linux Device Driver。
編譯後的Driver 是 myLED.ko ,User AP 是
myU1.exe

User space side 的程式碼:


/* myU1.c */
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

#include <linux/ioctl.h>

#include "modC_inc.h"

int main()
{
static struct ioctlPara *pioctlParaVar;

int fd = open( "/dev/myChar1" , O_RDWR ) ;
pioctlParaVar = (struct ioctlPara *)malloc( sizeof( struct ioctlPara ) );
strcpy( pioctlParaVar->writeStr, "Hello Kernel by User space" );

ioctl(fd, my_IOC_RW , (void *)pioctlParaVar );

ioctl(fd, my_IOC_W , (void *)pioctlParaVar );

ioctl(fd, my_IOC_R , (void *)pioctlParaVar );

printf( "pioctlParaVar->writeStr=%s , pioctlParaVar->readStr=%s \r\n",
pioctlParaVar->writeStr, pioctlParaVar->readStr );

free(pioctlParaVar);

close( fd );
}

由於 Device Driver 程式碼比較長,僅貼上 file operations 函數的 ioctl 程式碼:



操作過程:

~/usb$ls
myLED.ko myU1.exe mydrv.ko mydrv.o test1.exe
~/usb$insmod ./myLED.ko
Using ./myLED.ko
Hello, world
world
howmany=1
~/usb$./myU1.exe
LED_open
my_IOC_RW Hello Kernel by User space
my_IOC_W Hello Kernel by User space
my_IOC_R
pioctlParaVar->writeStr=Hello Kernel by User space , pioctlParaVLED_release
ar->readStr=Hello Kernel by User space
~/usb$
~/usb$lsmod
Module Size Used by Not tainted
myLED 3980 0 - Live 0xbf000000
~/usb$rmmod myLED
Goodbye, cruel world
~/usb$

執行畫面如下:



沒有留言:

張貼留言

FPGA Verilog 的學習經驗,提供給要入門的新手

今天簡單說說 FPGA Verilog 的學習經驗,提供給要入門的新手: 1.對自己寫的FPGA Verilog程式,所生成的數位電路要心中有數。 這一點個人認為很重要,就正如寫 C語言,心中要能生成對應的組合語言一樣,我是這樣要求自己的。 雖然 FPGA Verilog語言...