⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart1t.c

📁 周立公smartarm2200开发板的串口例子
💻 C
字号:
#define IN_UART1#include "config.h"//#include <asm/arch/hardware.h>//#include <asm/io.h>#include     <stdio.h>      /* stand input and output... *///#include     <stdlib.h>     /* stand lib *///#include     <unistd.h>     /* UNIX stand function *///#include     <sys/types.h>  /**///#include     <sys/stat.h>   /**/#include     <fcntl.h>      /* file controle */#include     <termios.h>    /*PPSIX terminal controle */#include     <errno.h>      /* error *//************ set baud ********************/void set_speed(int fd, int speed){    int   i;    int   status;    struct termios Opt;    tcgetattr(fd, &Opt);  // get attribute of serial port    tcflush(fd, TCIOFLUSH);    cfsetispeed(&Opt, speed);    cfsetospeed(&Opt, speed);    status = tcsetattr(fd, TCSANOW, &Opt);  // set attribute    if  (status != 0)    {        perror("tcsetattr fd1");        return;    }    tcflush(fd,TCIOFLUSH);}/*********** set data format: databits,stopbits and parity **********/int set_data_format(int fd,int databits,int stopbits,int parity){    struct termios opt;    if( tcgetattr(fd, &opt)  !=  0)    {  	perror("SetupSerial 1");  	return(FALSE);    }    opt.c_cflag &= ~CSIZE;    switch (databits)    { 	        case 5:  		opt.c_cflag |= CS5;  		break;        case 6:  		opt.c_cflag |= CS6;  		break;        case 7:  		opt.c_cflag |= CS7;  		break;  	case 8:		opt.c_cflag |= CS8;		break;	default:		fprintf(stderr,"Unsupported data size\n");		return (FALSE);    }    switch (parity)    {        case 'n':        case 'N':		opt.c_cflag &= ~PARENB;   /* Clear parity enable */		opt.c_iflag &= ~INPCK;     /* Enable parity checking */		break;	case 'o':	case 'O':		opt.c_cflag |= (PARODD | PARENB);  /* parity checking */ 		opt.c_iflag |= INPCK;      /* Disnable parity checking */		break;	case 'e':	case 'E':		opt.c_cflag |= PARENB;     /* Enable parity */		opt.c_cflag &= ~PARODD;   /*  */  		opt.c_iflag |= INPCK;       /* Disnable parity checking */		break;//	case 'S'://	case 's':  /*as no parity*///		opt.c_cflag &= ~PARENB;//		opt.c_cflag &= ~CSTOPB;//		break;	default:		fprintf(stderr,"Unsupported parity\n");		return (FALSE);    }           switch (stopbits)    {        case 1:  		opt.c_cflag &= ~CSTOPB;		break;	case 2:		opt.c_cflag |= CSTOPB;		break;	default:		fprintf(stderr,"Unsupported stop bits\n");		return (FALSE);    }    /* Set input parity option */    if (parity != 'n')     	opt.c_iflag |= INPCK;    opt.c_cc[VTIME] = 100; // 10 seconds    opt.c_cc[VMIN] = 0;    tcflush(fd, TCIFLUSH); /* Update the options and do it NOW */    if (tcsetattr(fd, TCSANOW, &opt) != 0)    {  	perror("SetupSerial 3");	return (FALSE);    }     return (TRUE); }/************* main() ********************/int main(){    int fd;    int temp, PinSel0Save;    int8 rcv[32]; //   temp = inl(PINSEL0);        // long int //   PinSel0Save =  temp & (0x0f << 16); // save PINSEL0 setting //   temp &= ~(0x0f << 16);              // connect UART0 //   temp |= (0x05 << 16); //   outl(temp, PINSEL0);                // write PINSEL0         fd = open("/dev/ttyS1", O_RDWR);  // read and write    if(fd == -1)    {	printf("Can't open ttyS1!\n");	exit(0);    }    set_speed(fd,B115200);   // set baud 115200    if (set_data_format(fd,8,1,'N')== FALSE)    {         printf("Data format Error!\n");        exit(1);    }       write(fd, "Hello World!\n", 13) ;    write(fd, "Hello World!\n", 13) ;    read(fd, rcv, 8);    printf("\n");    write(fd, rcv, 8);    printf("\n");    /* disconnect UART1 */  //  temp = inl(PINSEL0);  //  temp &= ~(0x0f << 16);  //  temp |= PinSel0Save;  //  outl(temp, PINSEL0);    close(fd);    return(0);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -