当前位置: 技术问答>linux和unix
linux下驱动求助
来源: 互联网 发布时间:2016-12-25
本文导语: 各位大侠们帮帮忙啊!我最近在看linux下的驱动,我的任务是编写串口驱动,是想开串口接收数据,用的是ARM11开发板。目的是使得开发板从串口接收另外一块板子的数据!开发板自带的资料中我也找到了驱动那块的...
各位大侠们帮帮忙啊!我最近在看linux下的驱动,我的任务是编写串口驱动,是想开串口接收数据,用的是ARM11开发板。目的是使得开发板从串口接收另外一块板子的数据!开发板自带的资料中我也找到了驱动那块的代码,但是我还是有点不知道怎么下手,请大家多多帮忙!提点意见也好。
|
想理解代码??慢慢理解 设备、驱动、总线三者关系。
|
别急,慢慢来,一点一点的吃懂就行!坚持下去!!
|
我的串口驱动代码给你参考,串口都在/dev 目录下,把他的参数设置完成后就可以像文件一样操作了!
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define TIME 1 //second
char *usage="Usage: ./d901_test_uart [files] n"
" files:n"
" ttymxc0 UART 1n"
" ttymxc1 UART 2n"
" ttymxc2 UART 3n";
void config_serial_port(int fd)
{
struct termios options;
/* config serial port */
tcgetattr( fd,&options);
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
options.c_cflag &= ~CSIZE; /* Mask the character size bits */
options.c_cflag |= CS8; /* Select 8 data bits */
options.c_cflag &= ~PARENB;//mode:8N1
options.c_cflag &= ~CSTOPB;
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~CRTSCTS; /*disable hardware flow control*/
options.c_iflag &= ~(IXON | IXOFF | IXANY); /*disable software flow control */
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
options.c_oflag &= ~OPOST; /*Output*/
options.c_cc[VTIME] = 20;//timeout = 2seconds
options.c_cc[VMIN] = 0;
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &options);
}
int main(int argc, char *argv[])
{
int fd;
int nwrite;
int nread;
int i;
unsigned char buf[32];
char dev[16] = "/dev/";
unsigned char cmd[2] = {0xaa,0xbb};
if (argc != 3) {
printf("%sn", usage);
}
strcat(dev, argv[1]);
/*open uart device*/
fd = open(dev, O_RDWR | O_NOCTTY);
if(fd == -1) {
printf("Can't open %s device.n", dev);
return -1;
} else {
printf("Open %s seccessful!n", dev);
}
config_serial_port(fd);
while(1) {
if ((nwrite = write(fd, cmd, 2))
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define TIME 1 //second
char *usage="Usage: ./d901_test_uart [files] n"
" files:n"
" ttymxc0 UART 1n"
" ttymxc1 UART 2n"
" ttymxc2 UART 3n";
void config_serial_port(int fd)
{
struct termios options;
/* config serial port */
tcgetattr( fd,&options);
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
options.c_cflag &= ~CSIZE; /* Mask the character size bits */
options.c_cflag |= CS8; /* Select 8 data bits */
options.c_cflag &= ~PARENB;//mode:8N1
options.c_cflag &= ~CSTOPB;
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~CRTSCTS; /*disable hardware flow control*/
options.c_iflag &= ~(IXON | IXOFF | IXANY); /*disable software flow control */
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
options.c_oflag &= ~OPOST; /*Output*/
options.c_cc[VTIME] = 20;//timeout = 2seconds
options.c_cc[VMIN] = 0;
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &options);
}
int main(int argc, char *argv[])
{
int fd;
int nwrite;
int nread;
int i;
unsigned char buf[32];
char dev[16] = "/dev/";
unsigned char cmd[2] = {0xaa,0xbb};
if (argc != 3) {
printf("%sn", usage);
}
strcat(dev, argv[1]);
/*open uart device*/
fd = open(dev, O_RDWR | O_NOCTTY);
if(fd == -1) {
printf("Can't open %s device.n", dev);
return -1;
} else {
printf("Open %s seccessful!n", dev);
}
config_serial_port(fd);
while(1) {
if ((nwrite = write(fd, cmd, 2))