Linux的串口接收中断一般都是使能的,无论是否是DMA接收。

![]()
当串口接收到数据的时候,会调用 stm32_usart_push_buffer_dma函数。在这个函数内部向用户层发送信号,然后就可以调用read方法,读取数据了。从而避免循环读取数据。

n_tty.c
- // application pid, application use ioctl method to set this value
- int app_pid_ttySTM2 = 0;
-
- typedef enum{
- IOCTL_SET_APP_PID = 0x111,
- } MODULE_CMD;
-
- static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
- unsigned int cmd, unsigned long arg)
- {
- struct n_tty_data *ldata = tty->disc_data;
- int retval;
-
- printk(KERN_INFO "gpio_XXXX_ioctl:%s cmd=0x%x,arg=0x%lx\n", __func__, cmd, arg);
-
- switch (cmd) {
- case TIOCOUTQ:
- return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
- case TIOCINQ:
- down_write(&tty->termios_rwsem);
- if (L_ICANON(tty) && !L_EXTPROC(tty))
- retval = inq_canon(ldata);
- else
- retval = read_cnt(ldata);
- up_write(&tty->termios_rwsem);
- return put_user(retval, (unsigned int __user *) arg);
- case IOCTL_SET_APP_PID:
- {
- if(tty->index == 2)
- {
- if(copy_from_user(&app_pid_ttySTM2, (int *)arg, sizeof(int)))
- {
- return -EFAULT;
- }
- }
-
- return 0;
- }
-
- default:
- return n_tty_ioctl_helper(tty, file, cmd, arg);
- }
- }
stm32-usart.c
- #define SIGETX 44
-
- static void stm32_usart_push_buffer_dma(struct uart_port *port,
- unsigned int dma_size)
- {
- struct stm32_port *stm32_port = to_stm32_port(port);
- struct tty_port *ttyport = &stm32_port->port.state->port;
- unsigned char *dma_start;
- int dma_count;
- struct kernel_siginfo info;
- struct task_struct *task = NULL;
-
- dma_start = stm32_port->rx_buf + (RX_BUF_L - stm32_port->last_res);
- dma_count = tty_insert_flip_string(ttyport, dma_start, dma_size);
-
- port->icount.rx += dma_count;
- stm32_port->last_res -= dma_count;
- if (stm32_port->last_res == 0)
- stm32_port->last_res = RX_BUF_L;
-
- if (app_pid_ttySTM2 == 0)
- {
- printk(KERN_ALERT "XXXX_NOTIFIER: gpio_XXXX_handler not set user pid\n");
- return ;
- }
-
- //Sending signal to app
- memset(&info, 0, sizeof(struct kernel_siginfo));
- info.si_signo = SIGETX;
- info.si_code = 0;
- info.si_int = 1234;
-
- printk(KERN_INFO "gpio_XXXX_handler Interrupt received from GPIO XXXX pin\n");
-
- rcu_read_lock();
- task = pid_task(find_vpid(app_pid_ttySTM2), PIDTYPE_PID);
- rcu_read_unlock();
-
- if (task == NULL) {
- printk(KERN_ALERT "XXXX_NOTIFIER: get_current failed\n");
- return;
- }
- else
- {
- printk(KERN_INFO "Sending signal to app\n");
- if(send_sig_info(SIGETX, &info, task) < 0) {
- printk(KERN_ALERT "gpio_XXXX_handler Unable to send signal\n");
- }
- }
- }
应用程序:
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
-
- #define FALSE 0
- #define TRUE 1
-
- char *recchr="We received:\"";
-
- void print_usage();
-
- int speed_arr[] = {
- B921600, B460800, B230400, B115200, B57600, B38400, B19200,
- B9600, B4800, B2400, B1200, B300,
- };
-
- int name_arr[] = {
- 921600, 460800, 230400, 115200, 57600, 38400, 19200,
- 9600, 4800, 2400, 1200, 300,
- };
-
- #define SIGETX 44
- int check = 0;
-
- typedef enum{
- IOCTL_SET_APP_PID = 0x111,
- } APP_CMD;
-
- bool readyReadFlag = FALSE;
-
- void sig_event_handler(int n, siginfo_t *info, void *unused)
- {
- printf ("sig_event_handler Received signal from kernel");
-
- if (n == SIGETX) {
- check = info->si_int;
- printf ("Received signal from kernel : Value = %u\n", check);
-
- readyReadFlag = TRUE;
- }
- }
-
- 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);
- }
-
- if (i == 12){
- printf("\tSorry, please set the correct baud rate!\n\n");
- print_usage(stderr, 1);
- }
- }
- /*
- *@brief 璁剧疆涓插彛鏁版嵁浣嶏紝鍋滄浣嶅拰鏁堥獙浣�
- *@param fd 绫诲瀷 int 鎵撳紑鐨勪覆鍙f枃浠跺彞鏌�*
- *@param databits 绫诲瀷 int 鏁版嵁浣� 鍙栧�� 涓� 7 鎴栬��8*
- *@param stopbits 绫诲瀷 int 鍋滄浣� 鍙栧�间负 1 鎴栬��2*
- *@param parity 绫诲瀷 int 鏁堥獙绫诲瀷 鍙栧�间负N,E,O,,S
- */
- int set_Parity(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(stderr,"Unsupported data size\n");
- return (FALSE);
- }
-
- switch (parity) {
- case 'n':
- case 'N':
- options.c_cflag &= ~PARENB; /* Clear parity enable */
- options.c_iflag &= ~INPCK; /* Enable parity checking */
- break;
- case 'o':
- case 'O':
- options.c_cflag |= (PARODD | PARENB); /* 璁剧疆涓哄鏁堥獙*/
- options.c_iflag |= INPCK; /* Disnable parity checking */
- break;
- case 'e':
- case 'E':
- options.c_cflag |= PARENB; /* Enable parity */
- options.c_cflag &= ~PARODD; /* 杞崲涓哄伓鏁堥獙*/
- options.c_iflag |= INPCK; /* Disnable parity checking */
- break;
- case 'S':
- case 's': /*as no parity*/
- options.c_cflag &= ~PARENB;
- options.c_cflag &= ~CSTOPB;
- break;
- default:
- fprintf(stderr,"Unsupported parity\n");
- return (FALSE);
- }
- /* 璁剧疆鍋滄浣�*/
- switch (stopbits) {
- case 1:
- options.c_cflag &= ~CSTOPB;
- break;
- case 2:
- options.c_cflag |= CSTOPB;
- break;
- default:
- fprintf(stderr,"Unsupported stop bits\n");
- return (FALSE);
- }
- /* Set input parity option */
- if (parity != 'n')
- options.c_iflag |= INPCK;
- options.c_cc[VTIME] = 150; // 15 seconds
- options.c_cc[VMIN] = 0;
-
- //options.c_lflag &= ~(ECHO | ICANON);
-
- options.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
- options.c_oflag &= ~OPOST;
- options.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
- options.c_cflag &= ~(CSIZE | PARENB);
-
- tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
- if (tcsetattr(fd,TCSANOW,&options) != 0) {
- perror("SetupSerial 3");
- return (FALSE);
- }
- return (TRUE);
- }
-
- /**
- *@breif 鎵撳紑涓插彛
- */
- int OpenDev(char *Dev)
- {
- int fd = open( Dev, O_RDWR ); //| O_NOCTTY | O_NDELAY
- if (-1 == fd) { /*璁剧疆鏁版嵁浣嶆暟*/
- perror("Can't Open Serial Port");
- return -1;
- } else
- return fd;
- }
-
-
- /* The name of this program */
- const char * program_name;
-
- /* Prints usage information for this program to STREAM (typically
- * stdout or stderr), and exit the program with EXIT_CODE. Does not
- * return.
- */
-
- void print_usage (FILE *stream, int exit_code)
- {
- fprintf(stream, "Usage: %s option [ dev... ] \n", program_name);
- fprintf(stream,
- "\t-h --help Display this usage information.\n"
- "\t-d --device The device ttyS[0-3] or ttySCMA[0-1]\n"
- "\t-b --baudrate Set the baud rate you can select\n"
- "\t [230400, 115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200, 300]\n"
- "\t-s --string Write the device data\n");
- exit(exit_code);
- }
-
- /*
- *@breif main()
- */
- int main(int argc, char *argv[])
- {
- int fd, next_option, havearg = 0;
- char *device;
- int i=0,j=0;
- int nread; /* Read the counts of data */
- char buff[512]; /* Recvice data buffer */
- pid_t pid;
-
- int app_pid;
- struct sigaction act;
-
- //char *xmit = "1234567890"; /* Default send data */
- char *xmit = "0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20"; /* Default send data */
- char xmit_data[15] = {0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25};
-
- int speed, send_mode = 0;
- const char *const short_options = "hd:s:b:m:";
-
- const struct option long_options[] = {
- { "help", 0, NULL, 'h'},
- { "device", 1, NULL, 'd'},
- { "string", 1, NULL, 's'},
- { "baudrate", 1, NULL, 'b'},
- { "send/recv mode", 0, NULL, 'm'},
- { NULL, 0, NULL, 0 }
- };
-
- /* install custom signal handler */
- sigemptyset(&act.sa_mask);
- act.sa_flags = SA_NODEFER;
- act.sa_sigaction = sig_event_handler;
- sigaction(SIGETX, &act, NULL);
-
- printf("Installed signal handler for SIGETX = %d\n", SIGETX);
-
- app_pid = getpid();
-
- program_name = argv[0];
-
- do {
- next_option = getopt_long (argc, argv, short_options, long_options, NULL);
- switch (next_option) {
- case 'h':
- print_usage (stdout, 0);
- case 'd':
- device = optarg;
- havearg = 1;
- break;
- case 'b':
- speed = atoi(optarg);
- break;
- case 's':
- xmit = optarg;
- havearg = 1;
- break;
- case 'm':
- send_mode = atoi(optarg);
- break;
- case -1:
- if (havearg) break;
- case '?':
- print_usage (stderr, 1);
- default:
- abort ();
- }
- }while(next_option != -1);
-
- sleep(1);
- fd = OpenDev(device);
-
- if (fd > 0) {
- set_speed(fd, speed);
- } else {
- fprintf(stderr, "Error opening %s: %s\n", device, strerror(errno));
- exit(1);
- }
-
- if (set_Parity(fd,8,1,'N')== FALSE) {
- fprintf(stderr, "Set Parity Error\n");
- close(fd);
- exit(1);
- }
-
- ioctl(fd, IOCTL_SET_APP_PID, &app_pid);
-
- #if 0
- pid = fork();
-
- if (pid < 0) {
- fprintf(stderr, "Error in fork!\n");
- } else if (pid == 0){
- #endif
- if (send_mode){
- while(1) {
- printf("%s SEND: %s\n",device, xmit);
- //write(fd, xmit, strlen(xmit));
- write(fd, xmit_data, sizeof(xmit_data));
- sleep(1);
- i++;
- }
- }else {
- while(1) {
- if(readyReadFlag)
- {
- printf("Start to read. \n");
- nread = read(fd, buff, sizeof(buff));
- if (nread > 0) {
- printf("RECV nread = %d\n", nread);
-
- for(int id = 0; id < nread; id++)
- {
- printf("RECV[%d]: %d\n", id, buff[id]);
- }
-
- buff[nread] = '\0';
-
- //printf("%s RECV[%d]: %s\n", device, nread, buff);
- }
-
- readyReadFlag = FALSE;
- }
-
- sleep(1);
- }
- }
- close(fd);
- exit(0);
- }
-
