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);elseretval = 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 44static 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 appmemset(&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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <getopt.h>
#include <string.h>
#include <signal.h>
#include <pthread.h>
#include <stdbool.h>#define FALSE 0
#define TRUE 1char *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 secondsoptions.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_NDELAYif (-1 == fd) { /*璁剧疆鏁版嵁浣嶆暟*/perror("Can't Open Serial Port");return -1;} elsereturn 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 0pid = fork();if (pid < 0) {fprintf(stderr, "Error in fork!\n");} else if (pid == 0){
#endifif (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);
}

Linux:TTY串口接收中断相关推荐

  1. Linux tty串口测试程序

    在程序中,很容易配置串口的属性,这些属性定义在结构体struct termios中. 关于termios的详细介绍,可以另行查资料,或者参考:详解linux下的串口通讯开发:http://blog.i ...

  2. linux设备驱动,tty串口编程 如何查看linux下串口是否可用?串口名称等

    如何查看linux下串口是否可用?串口名称等? http://zhidao.baidu.com/question/419148559.html 查看串口是否可用,可以对串口发送数据比如对com1口,e ...

  3. linux 控制虚拟串口,linux虚拟串口控制器实现-适用于无开发板学习tty driver-好向圈...

    在前面几章,我们介绍了tty子系统的框架.数据结构.tty驱动的注册与注销等内容,本章我们借助 之前学习的内容开发一个虚拟的串口控制器驱动,以便我们理解开发串口驱动的步骤及驱动开发实践. 本次实现的虚 ...

  4. linux内核串口接入pps,Linux串口(serial、uart)驱动程序设计

    一.核心数据结构 串口驱动有3个核心数据结构,它们都定义在 1.uart_driver uart_driver包含了串口设备名.串口驱动名.主次设备号.串口控制台(可选)等信息,还封装了tty_dri ...

  5. linux tty结构体,linux tty驱动架构分析

    再看Linux tty驱动过程中发现linux的驱动构架中,面向对象的思想已经根深蒂固.就比如这串口驱动,代码中经常有一些貌似和串口无关的代码,比如,tty_register_driver等.但我们却 ...

  6. 树莓派slam_SLAM+语音机器人DIY系列:(五)树莓派3开发环境搭建——6.树莓派USB与tty串口号绑定...

    摘要 通过前面一系列的铺垫,相信大家对整个miiboo机器人的DIY有了一个清晰整体的认识.接下来就正式进入机器人大脑(嵌入式主板:树莓派3)的开发.本章将从树莓派3的开发环境搭建入手,为后续ros开 ...

  7. read接收不全linux,linux下串口读写有关问题 read 一次读不全(5)

    当前位置:我的异常网» Linux/Unix » linux下串口读写有关问题 read 一次读不全 linux下串口读写有关问题 read 一次读不全(5) www.myexceptions.net ...

  8. linux虚拟串口及远程访问

    1. 虚拟终端概念 linux中有很多终端,如下简单介绍下各种终端或串口的概念. 1.1 tty:终端设备的统称 tty是Teletype或TeletypeWriter的缩写,中文翻译为电传打字机.电 ...

  9. linux内核串口调试,linux 串口调试方法

    linux 串口调试方法 作者:syhdjf 发布于:2015-4-8 16:41 最近项目上用到linux下的串口,与下级模块的通信出了些问题,所以写了个小程序想要测试下串口,物理连接是PC端串口调 ...

最新文章

  1. 简单两步使用node发送qq邮件
  2. 堆、栈、方法区、静态代码块---Java
  3. 【读书笔记《Android游戏编程之从零开始》】17.游戏开发基础(游戏适屏的简述和作用、让游戏主角动起来)...
  4. python-opencv3 kmeans图像分类
  5. 求最大连续子序列和——解法1 – 暴力出奇迹||解法2 – 分治
  6. 预测数值型数据:回归源码分析(1)
  7. Nginx服务系列——代理
  8. 删除的时候提示“该项目不在C:\User\桌面 中
  9. [MySql]默认密码的查找与修改
  10. NEU 1683: H-Index
  11. 常见NoSQL数据库概述
  12. 袖珍电子书,雄心壮志永不变
  13. Python整理PEER所下载的地震波源数据——提取地震波至txt+生成地震波反应谱
  14. 如何让iframe背景色透明
  15. ICQ官方中文版 v10.0.12161.0
  16. pci规划的三个原则_PCI规划应遵循什么原则? - 51学通信网络课堂 - 通信人值得信赖的在线交流学习平台 - Powered By EduSoho...
  17. Mysql数据库轻松学06—数据分析师常用:数据查询语言DQL之单表查询
  18. 什么是迭代(迭代法)
  19. VR虚拟现实心理脱敏训练系统整体解决方案
  20. Reverses the digits of an integer mathmatically

热门文章

  1. 使用卡尔曼滤波和扩展卡尔曼滤波进行毫米波雷达和激光雷达数据融合示例
  2. 美军如何在不可信设备上安全访问国防部网络?
  3. okcoin 爱沙尼亚_我如何成为爱沙尼亚的电子居民
  4. 不要问程序员什么是“对象”,也不要给他介绍“对象”
  5. 2021年R2移动式压力容器充装考试题及R2移动式压力容器充装考试试卷
  6. 翻译:Panda3D Manual/V. Programming with Panda/E. Camera Control
  7. 解决 编译错误 对‘sem_init’未定义的引用 collect2: error: ld returned 1 exit status builtin: recipe for
  8. 工作居住证 TO 户口
  9. 马毅:低维模型与深度模型的殊途同归
  10. ARKit 又添新实例:iPhone 上看 Falcon 9 火箭回收