USB入门:driver+usb read.c+usb write.c
driver.c
#include<linux/init.h>
#include<linux/kernel.h>
#include<linux/module.h>
#include<linux/fs.h>
#include<linux/miscdevice.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <linux/slab.h>
MODULE_LICENSE("Dual BSD/GPL");
static int misc_open(struct inode *nd,struct file *filp)
{
//printk(KERN_ALERT"misc_open success\n");
return 0;
}
static ssize_t misc_read(struct file *filp, char __user *buf, size_t count,loff_t *ppos)
{
printk(KERN_ALERT"In the misc_read() function!\n");
char data;
int i=0;
printk("read ppos =%d\n",(unsigned int)*ppos);
data='A';
for(i=0;i<count;i++){
if(copy_to_user(buf+i,&data,sizeof(data))){
return -EFAULT;
}
printk("read data=%c\n",data);
}
//*ppos+=count;
printk("read buf=%s\n",buf);
return 0;
}
static ssize_t misc_write(struct file *filp, const char __user *buf, size_t count,loff_t *ppos)
{
printk("in the write..\n");
printk("write ppos=%d\n",(unsigned int)*ppos);
char data;
int i;
for(i=0;i<count;i++){
printk("buf=%c\n",*(buf+i));
if(copy_from_user(&data,buf+i,sizeof(data)))
{
return -EFAULT;
}
printk("data=%c\n",data);
}
//*ppos+=count;
return 0;
}
struct file_operations misc_ops =
{
.owner = THIS_MODULE,
.open = misc_open,
.read = misc_read,
.write = misc_write,
};
struct miscdevice misc=
{
.minor = MISC_DYNAMIC_MINOR,
.name = "my_misc_dev",
.fops = &misc_ops,
};
static int zx_init(void)
{
int ret;
ret = misc_register(&misc);
printk(KERN_ALERT"\t initialized %s!\n",(0==ret)?"successed":"failed");
return ret;
}
static void zx_exit(void)
{
misc_deregister(&misc);
printk(KERN_ALERT"Removing misc_dev...!\n");
}
module_init(zx_init);
module_exit(zx_exit);
usb read.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/signal.h>
#include <sys/types.h>
#define MAX 100
#define FALSE -1
#define TRUE 0
void set_speed(int fd, int speed);
int set_Parity(int fd);
int OpenDev(char *Dev);
void main(void){
int fd_usb;
int nread;
char buff[MAX] = {0};
char *usb_dev = "/dev/ttyS1";
fd_usb = OpenDev(usb_dev);
set_speed(fd_usb, 115200);
if(set_Parity(fd_usb) == FALSE){
printf("Set Parity Error\n");
return ;
}
while(1){
//sleep(1);
if((nread = read(fd_usb, buff, sizeof(buff)-1)) > 0){
buff[nread+1] = '\0';
printf( "n = %d \n", nread);
printf( "read data =%s \n", buff);
}
stpcpy(buff,"");
sleep(1);
}
close(fd_usb);
return ;
}
int OpenDev(char *Dev){
int fd = open(Dev, O_RDWR);
if (fd == -1){
perror("Can't Open Serial Port");
return -1;
}else{
return fd;
}
}
int speed_arr[] = {B115200,B38400, B19200, B9600, B4800, B2400, B1200, B300,
B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {115200,38400, 19200, 9600, 4800, 2400, 1200, 300, 38400,
19200, 9600, 4800, 2400, 1200, 300, };
void set_speed(int fd, int speed){
int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt);
for(i= 0; i < sizeof(speed_arr) / sizeof(int); i++){
if (speed == name_arr[i]) {
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);
if (status != 0){
perror("tcsetattr fd1");
return;
}
tcflush(fd,TCIOFLUSH);
}
}
}
int set_Parity(int fd){
struct termios options;
if(tcgetattr(fd, &options) != 0){
perror("SetupSerial 1");
return(FALSE);
}
//Enable the receiver and set local mode
options.c_cflag |= (CLOCAL | CREAD);
//Select 8 data bits, 1 stop bit and no parity bit
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
//Disable hardware flow control
options.c_cflag &= ~CRTSCTS;
//Choosing raw input
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
//Disable software flow control
options.c_iflag &= ~(IXON | IXOFF | IXANY);
//Choosing raw output
options.c_oflag &= ~OPOST;
//Set read timeouts
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 10;
if(tcsetattr(fd, TCSANOW, &options) != 0){
perror("Setup Serial 3");
return (FALSE);
}
return (TRUE);
}
usb write.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/signal.h>
#include <sys/types.h>
#define MAX 100
#define FALSE -1
#define TRUE 0
void set_speed(int fd, int speed);
int set_Parity(int fd);
int OpenDev(char *Dev);
void main(void){
int fd_usb;
int fd_misc;
int nwrite;
char buff[MAX] = "ssweiqqmmiioobgjklptyrewepsswei";
char *usb_dev = "/dev/ttyS0";
char *misc_dev = "/dev/my_misc_dev";
fd_misc = OpenDev(misc_dev);
fd_usb = OpenDev(usb_dev);
set_speed(fd_usb, 115200);
if(set_Parity(fd_usb) == FALSE){
printf("Set Parity Error\n");
return ;
}
while(1){
//read data from misc device
read(fd_misc, buff, sizeof(buff)-1);
printf("read from misc device:%s\n",buff);
//write to /dev/ttyS0
if((nwrite = write(fd_usb, buff, sizeof(buff)-1)) > 0){
printf("write Len= %d\n",nwrite);
}
sleep(1);
}
close(fd_usb);
close(fd_misc);
return ;
}
int OpenDev(char *Dev){
int fd = open(Dev, O_RDWR);
if (fd == -1){
perror("Can't Open Serial Port");
return -1;
}else{
return fd;
}
}
int speed_arr[] = {B115200,B38400, B19200, B9600, B4800, B2400, B1200, B300,
B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {115200,38400, 19200, 9600, 4800, 2400, 1200, 300, 38400,
19200, 9600, 4800, 2400, 1200, 300, };
void set_speed(int fd, int speed){
int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt);
for(i= 0; i < sizeof(speed_arr) / sizeof(int); i++){
if (speed == name_arr[i]) {
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);
if (status != 0){
perror("tcsetattr fd1");
return;
}
tcflush(fd, TCIOFLUSH);
}
}
}
int set_Parity(int fd){
struct termios options;
if(tcgetattr(fd, &options) != 0){
perror("SetupSerial 1");
return(FALSE);
}
//Enable the receiver and set local mode
options.c_cflag |= (CLOCAL | CREAD);
//Select 8 data bits, 1 stop bit and no parity bit
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
//Disable hardware flow control
options.c_cflag &= ~CRTSCTS;
//Choosing raw input
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
//Disable software flow control
options.c_iflag &= ~(IXON | IXOFF | IXANY);
//Choosing raw output
options.c_oflag &= ~OPOST;
//Set read timeouts
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 10;
if(tcsetattr(fd,TCSANOW,&options) != 0){
perror("SetupSerial 3");
return (FALSE);
}
return (TRUE);
}
第二组:
driver:
misc.c
#include<linux/init.h>
#include<linux/kernel.h>
#include<linux/module.h>
#include<linux/fs.h>
#include<linux/miscdevice.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <linux/slab.h>
MODULE_LICENSE("Dual BSD/GPL");
static int misc_open(struct inode *nd,struct file *filp)
{
return 0;
}
static ssize_t misc_read(struct file *filp, char __user *buf, size_t count,loff_t *ppos)
{
printk(KERN_ALERT"In the misc_read() function!\n");
char data;
int i=0;
printk("read ppos =%d\n",(unsigned int)*ppos);
data='A';
for(i=0;i<count;i++){
if(copy_to_user(buf+i,&data,sizeof(data))){
return -EFAULT;
}
printk("read data=%c\n",data);
}
printk("read buf=%s\n",buf);
return 0;
}
static ssize_t misc_write(struct file *filp, const char __user *buf, size_t count,loff_t *ppos)
{
printk("in the write..\n");
printk("write ppos=%d\n",(unsigned int)*ppos);
char data;
int i;
for(i=0;i<count;i++){
printk("buf=%c\n",*(buf+i));
if(copy_from_user(&data,buf+i,sizeof(data)))
{
return -EFAULT;
}
printk("data=%c\n",data);
}
return 0;
}
struct file_operations misc_ops =
{
.owner = THIS_MODULE,
.open = misc_open,
.read = misc_read,
.write = misc_write,
};
struct miscdevice misc=
{
.minor = MISC_DYNAMIC_MINOR,
.name = "my_misc_dev",
.fops = &misc_ops,
};
static int zx_init(void)
{
int ret;
ret = misc_register(&misc);
printk(KERN_ALERT"\t initialized %s!\n",(0==ret)?"successed":"failed");
return ret;
}
static void zx_exit(void)
{
misc_deregister(&misc);
printk(KERN_ALERT"Removing misc_dev...!\n");
}
module_init(zx_init);
module_exit(zx_exit);
usb.c
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include<stdlib.h>
#include<signal.h>
#include<sys/stat.h>
#include <sys/time.h>
#include <sys/signal.h>
#define MAX 100
/*
* 'open_port()' - Open serial port /dev/ttyS0
* Returns the file descriptor on success or -1 on error.
*/
void open_usb();
void open_misc();
char w_buf[MAX] = {0};
char r_buf[MAX] = {0};
void main(){
//strcmp(w_buf,"abcdefghijklmnop");
//read data from misc device
//open_misc();
strcpy(r_buf,"abcdefghijklmnop");
//write data to usb device
open_usb();
return ;
}
void open_usb(){
int fd; /* File descriptor for the port */
struct termios options;
int status;
int n;
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if(fd != -1){
printf("open /dev/ttyS0 succeed!\n");
//set blocking
fcntl(fd, F_SETFL, 0);
//set baud rates
tcgetattr(fd, &options);
/*
* Set the baud rates to 115200
*/
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
/*
* Enable the receiver and set local mode
*/
options.c_cflag |= (CLOCAL | CREAD);
/*
* Select 8 data bits, 1 stop bit and no parity bit
*/
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
/*
* Disable hardware flow control
*/
options.c_cflag &= ~CRTSCTS;
/*
* Choosing standard input
*/
options.c_lflag |= (ICANON | ECHO | ECHOE);
/*
* Disable software flow control
*/
options.c_iflag &= ~(IXON | IXOFF | IXANY);
/*
* Choosing raw output
*/
options.c_oflag &= ~OPOST;
/*
* Set read timeouts
*/
options.c_cc[VMIN] = 8;
options.c_cc[VTIME] = 10;
if(strlen(r_buf) > 1){
while(1){
n = write(fd, r_buf, sizeof(r_buf)-1);
if(n < 0){
//fputs("write() fo 4 bytes failed!\n", stderr);
printf("write to dev/ttyS1 failed!\n");
}else
printf("write to dev/ttyS1 device:%s\n",r_buf);
sleep(1);
}
}else
printf("r_buf = ""!\n");
status = tcsetattr(fd, TCSADRAIN, &options);
if (status != 0){
printf("tcsetattr failed!\n");
return;
}
}else{
//perror("open_port: Unable to open /dev/ttyS0 -");
printf("/dev/ttyS0 open failed!\n");
}
}
void open_misc(){
int fd;
int flag;
stpcpy(r_buf,"");
fd = open("/dev/my_misc_dev",O_RDWR | O_NOCTTY | O_NDELAY);
if(fd != -1){
fcntl(fd, F_SETFL, 0);
printf("open misc device succeed!\n");
read(fd, r_buf, sizeof(r_buf)-1);
printf("read from misc device:%s\n",r_buf);
}else{
printf("Misc device open failure\n");
}
close(fd);
}