mirror of
https://github.com/ioacademy-jikim/device_driver.git
synced 2026-08-22 13:52:55 +00:00
first sample
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
LOCAL_PATH := $(call my-dir)
|
||||
include $(CLEAR_VARS)
|
||||
LOCAL_CFLAGS := -fPIE
|
||||
LOCAL_LDFLAGS := -fPIE -pie
|
||||
LOCAL_MODULE := myapp
|
||||
LOCAL_SRC_FILES := blocking_app.c
|
||||
include $(BUILD_EXECUTABLE)
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
#if 1
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define LED_RATIO _IOW( 'm', 1 , int)
|
||||
|
||||
int main()
|
||||
{
|
||||
int fd, ret;
|
||||
int ratio;
|
||||
char buff[10];
|
||||
printf("밝기 입력( 0-255 ) : ");
|
||||
scanf("%d", &ratio );
|
||||
fd = open( "/dev/mydev", O_RDONLY );
|
||||
ret = ioctl(fd, LED_RATIO, &ratio);
|
||||
if( ret < 0 )
|
||||
{
|
||||
printf("ret=%d, errno=%d\n", ret, errno );
|
||||
return 0;
|
||||
}
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
#if 0
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define LED_RATIO _IO( 'm', 1 )
|
||||
|
||||
int main()
|
||||
{
|
||||
int fd, ret;
|
||||
char buff[10];
|
||||
fd = open( "/dev/mydev", O_RDONLY );
|
||||
ret = ioctl(fd, LED_RATIO);
|
||||
if( ret < 0 )
|
||||
{
|
||||
printf("ret=%d, errno=%d\n", ret, errno );
|
||||
return 0;
|
||||
}
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
#if 1
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int fd;
|
||||
char *p;
|
||||
fd = open("/dev/mydev", O_RDWR );
|
||||
p = mmap(0, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
strcpy( p , "hello world\n");
|
||||
printf("%s\n", p );
|
||||
munmap(p, 4096);
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
#include <fcntl.h>
|
||||
int main()
|
||||
{
|
||||
int fd;
|
||||
fd = open( "/sys/devices/virtual/misc/mydev/uevent", O_WRONLY );
|
||||
write( fd, "add\n", 4 );
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
#if 1
|
||||
#include <stdio.h>
|
||||
#include <poll.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int fd, ret;
|
||||
int temp;
|
||||
char buff[1024];
|
||||
struct pollfd fds[2];
|
||||
fd = open( "/dev/mydev", O_RDONLY );
|
||||
fds[0].fd = 0 ;
|
||||
fds[0].events = POLLIN;
|
||||
fds[1].fd = fd ;
|
||||
fds[1].events = POLLIN;
|
||||
|
||||
poll( fds, 2, -1 );
|
||||
|
||||
if( fds[0].revents & POLLIN )
|
||||
{
|
||||
read( 0, buff, sizeof buff );
|
||||
buff[ret-1] = 0;
|
||||
printf("buff=%s\n", buff );
|
||||
}
|
||||
if( fds[1].revents & POLLIN )
|
||||
{
|
||||
read( fd, &temp, sizeof temp );
|
||||
printf("temp=%d\n", temp );
|
||||
}
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
#if 0
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define LED_RATIO _IO( 'm', 1 )
|
||||
|
||||
int main()
|
||||
{
|
||||
int fd, ret;
|
||||
char buff[10];
|
||||
fd = open( "/dev/mydev", O_RDONLY );
|
||||
ret = ioctl(fd, LED_RATIO);
|
||||
if( ret < 0 )
|
||||
{
|
||||
printf("ret=%d, errno=%d\n", ret, errno );
|
||||
return 0;
|
||||
}
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
int main()
|
||||
{
|
||||
int fd, ret;
|
||||
char buff[10];
|
||||
fd = open( "/dev/mydev", O_RDONLY );
|
||||
ret = read( fd, buff, sizeof buff );
|
||||
buff[ret] = 0;
|
||||
printf( "buff[%s], ret=%d\n", buff, ret );
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
int main()
|
||||
{
|
||||
int fd, ret;
|
||||
int temp;
|
||||
fd = open( "/dev/mydev", O_RDONLY );
|
||||
ret = read( fd, &temp, sizeof temp );
|
||||
printf( "temp=%d, ret=%d\n", temp, ret );
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
int main()
|
||||
{
|
||||
int fd, ret;
|
||||
char buff[] = "LED_ON";
|
||||
fd = open( "/dev/mydev", O_WRONLY );
|
||||
ret = write( fd, buff, strlen(buff) );
|
||||
printf( "ret=%d\n", ret );
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
/*
|
||||
#include <fcntl.h>
|
||||
int main()
|
||||
{
|
||||
int fd;
|
||||
int flag=128;
|
||||
fd = open( "/dev/mydev", O_WRONLY );
|
||||
write( fd, &flag, sizeof flag );
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
Reference in New Issue
Block a user