驅(qū)動開發(fā)-串口驅(qū)動技術(shù)_第1頁
驅(qū)動開發(fā)-串口驅(qū)動技術(shù)_第2頁
驅(qū)動開發(fā)-串口驅(qū)動技術(shù)_第3頁
驅(qū)動開發(fā)-串口驅(qū)動技術(shù)_第4頁
驅(qū)動開發(fā)-串口驅(qū)動技術(shù)_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

1、串口驅(qū)動技術(shù)一、 確定任務(wù)1、 掌握基本的字符設(shè)備的驅(qū)動程序設(shè)計。2、 掌握基本的文件操作。3、掌握在操作系統(tǒng)下串口的收發(fā)處理過程。二、 學(xué)習(xí)內(nèi)容 1、 閱讀S5PV210數(shù)據(jù)手冊,了解串口是原理和使用方法。2、 編寫讀寫串口的應(yīng)用程序。3、 編寫makefile文件。4、下載并調(diào)試讀寫串口的應(yīng)用程序。三、基礎(chǔ)知識a) 原理圖: (3) 寄存器簡介:a) 系統(tǒng)框架圖:UART0有256字節(jié)用于FIFO模式、UART1有64字節(jié)用于FIFO模式,UAR2和UZRT3有16字節(jié)用于FIFO模式。在FIFO模式下,總共有所有的字節(jié)用于FIFO寄存器;在非FIFO模式下,只有一字節(jié)用作緩沖寄存器。UL

2、CONn寄存器主要用于設(shè)置發(fā)送數(shù)據(jù)位數(shù)、起始位數(shù)、停止位數(shù)、奇偶校驗位。UCONn控制寄存器主要用于設(shè)置傳輸和接收模式的選擇、分頻值的配置、信號和中斷的使能。 UFCONn寄存器主要用于發(fā)送和接收緩沖區(qū)大小的設(shè)置、FIFO的使能。 UTRSTSTn寄存器主用用于保存串口的狀態(tài)信息。 UERSTATn寄存器主要用于保存串口的錯誤信息。 UTXHn和URXHn寄存器分別為發(fā)送寄存器和接收寄存器。 四、代碼設(shè)計 1、 RS-232原理,請讀者自行查找相關(guān)資料。2、 測試程序(詳細(xì)程序請查看附件):#include <stdio.h>#include <stdlib.h>#in

3、clude <unistd.h>#include <sys/stat.h>#include <fcntl.h>#include <sys/ioctl.h>#include <errno.h>#include <string.h>#include <termios.h>#define max(x, y) (int)(x) > (int)(y) ? x : y)#define TRUE 1#define FALSE 0static char *serial_name="/dev/s3c2410_se

4、rial3"static struct timeval timeout=3,0;static struct termios oldtio_r,oldtio_w;int speed_arr = B115200,B57600, B38400, B19200, B9600, B4800, B2400, B1200, B300,B38400, B19200, B9600, B4800,B2400, B1200, B300, ;int name_arr = 115200,57600, 38400, 19200, 9600, 4800, 2400, 1200, 300,38400, 19200,

5、 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_arri)tcflush(fd, TCIOFLUSH); cfsetispeed(&Opt, speed_arri); cfsetospeed(&Opt, speed_arri);

6、status = tcsetattr(fd, TCSANOW, &Opt); if (status != 0) perror("tcsetattr fd1"); return; tcflush(fd,TCIOFLUSH); /*brief 設(shè)置串口數(shù)據(jù)位,停止位和效驗位*param fd 類型 int 打開的串口文件句柄*param databits 類型 int 數(shù)據(jù)位 取值 為 7 或者8*param stopbits 類型 int 停止位 取值為 1 或者2*param parity 類型 int 效驗類型 取值為N,E,O,S*/int set_Parity

7、(int fd,int databits,int stopbits,int parity)struct termios options; if ( tcgetattr( fd,&options) != 0) perror("SetupSerial 1"); return(FALSE); options.c_cflag &= CSIZE; switch (databits) case 7: options.c_cflag |= CS7; break; case 8:options.c_cflag |= CS8;break;default:fprintf(std

8、err,"Unsupported data sizen");return (FALSE); switch (parity) case 'n':case 'N':options.c_cflag &= PARENB; /* Clear parity enable */options.c_iflag &= INPCK; /* Enable parity checking */options.c_iflag &= (ICRNL|IGNCR);options.c_lflag &= (ICANON );break;case

9、 'o':case 'O':options.c_cflag |= (PARODD | PARENB); /* 設(shè)置為奇效驗*/ options.c_iflag |= INPCK; /* Disnable parity checking */break;case 'e':case 'E':options.c_cflag |= PARENB; /* Enable parity */options.c_cflag &= PARODD; /* 轉(zhuǎn)換為偶效驗*/ options.c_iflag |= INPCK; /* Disnab

10、le parity checking */break;case 'S':case 's': /*as no parity*/options.c_cflag &= PARENB;options.c_cflag &= CSTOPB;break;default:fprintf(stderr,"Unsupported parityn");return (FALSE); /* 設(shè)置停止位*/ switch (stopbits) case 1: options.c_cflag &= CSTOPB;break;case 2:opti

11、ons.c_cflag |= CSTOPB;break;default:fprintf(stderr,"Unsupported stop bitsn");return (FALSE); /* Set input parity option */ if (parity != 'n') options.c_iflag |= INPCK; options.c_ccVTIME = 150; / 15 seconds 超時時間 options.c_ccVMIN = 0; /最小讀取位tcflush(fd,TCIOFLUSH); /* Update the option

12、s and do it NOW */ if (tcsetattr(fd,TCSANOW,&options) != 0) perror("SetupSerial 3");return (FALSE); return (TRUE); int OpenDev(char *Dev)intfd = open( Dev, O_RDWR | O_NOCTTY | O_NDELAY); /| O_NOCTTY | O_NDELAYif (fd <0) perror("Can't Open Serial Port");exit(EXIT_FAILUR

13、E);return fd;int serial_write(int fd)char buf="this is uart testn"int ret ;/memset(buf, 0 , sizeof(buf);/printf("write serial:n");/fgets(buf , 256, stdin); /if(!strcmp(buf,"quit") /輸入quit 退出測試程序/printf("good byen");/return FALSE;/if(tcflush(fd,TCIOFLUSH)<0)

14、 /清空串口緩沖區(qū)fprintf(stderr, "tcflush errorn");ret = write(fd , buf , strlen(buf); /寫串口if(ret <0)perror("write serial");exit(EXIT_FAILURE);/write(fd , "n0",2); /printf("write uartn");return TRUE;void serial_read(int fd )int ret ;int len= 0;char buf256;memset(bu

15、f , 0 , 256);ret = read(fd , buf , 256); /讀取串口if(ret<0)perror("read");exit(EXIT_FAILURE);#if 0while(0)ret = read(fd , buf , 256);if(ret =-1 && errno = EINTR)continue;else if(ret <0)perror("read serial");exit(EXIT_FAILURE);else if(ret =0)break;elseprintf("receiv

16、e %s n",buf);#endif/printf("%dn",ret);printf("read serial: %s n",buf);if(tcflush(fd,TCIOFLUSH)<0) /清空串口緩沖區(qū)fprintf(stderr,"flush errorn");return ;void serial_config(int fd)set_speed(fd,115200); /設(shè)置波特率baud rateif (!set_Parity(fd,8,1,'N') printf("Set P

17、arity Errorn"); exit(EXIT_FAILURE); return ;int main(void)int fd_r , fd_w;int i ;int ret ;char ch ;fd_set readfds;fd_w = OpenDev(serial_name0); /打開 串口tcgetattr(fd_w,&oldtio_w);serial_config(fd_w);/fcntl(fd_r, F_SETFL, F_UNLCK); / Non-blocking read /while(ch= getchar()!='n' && ch !=EOF); /清空輸入緩沖區(qū)while(1)if(!serial_writ

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論