我有一个项目,我从串行端口接收数据,然后发送响应。我注意到我的系统(NanoPi NEO核心2)的延迟为1毫秒。接收中断时,我希望将其至少减少到0.3毫秒。
我想知道是否正确设置了串行端口选项或是否缺少某些内容。我有一个数字示波器,当中断发生时,我启用了一个GPIO来测量调用该中断所花费的时间。
#include <stdint.h>
#include <fcntl.h> //Used for UART
#include <termios.h> //Used for UART
#include <sys/signal.h> //Used for UART interrupt
#include <wiringPi.h> //GPIO for debug
#include <linux/serial.h> //Serial struct
#define TIMING_UART_PIN 4
int uart_port;
#define SERIALPORT "/dev/ttyS1" //Physical UART
void usartRX_interrupt (int status); /* definition of signal handler */
void init_UART(void){
printf("Init UART\n");
uart_port = open(SERIALPORT, O_RDWR | O_NOCTTY | O_NDELAY);
fcntl(uart_port, F_SETFL, FNDELAY);
fcntl(uart_port, F_SETOWN, getpid());
fcntl(uart_port, F_SETFL, O_ASYNC );
struct termios options;
tcgetattr(uart_port, &options);
options.c_cflag = B38400 |PARENB | CS8 | CLOCAL | CREAD; //UART PORT 38400kb/s even parity 8 bits
cfsetispeed(&options,38400);
cfsetospeed(&options,38400);
options.c_iflag = IGNPAR | INPCK; //Ignore characters with parity error
options.c_oflag = 0;
options.c_lflag = 0;
tcsetattr(uart_port, TCSANOW, &options); //set options for UART
//Set Interrupt handler for UART RX
struct sigaction saio;
saio.sa_handler = usartRX_interrupt; //Handler for UART
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction(SIGIO,&saio,NULL);
//Trying to set low latency
struct serial_struct kernel_serial_settings;
ioctl(uart_port, TIOCGSERIAL, &kernel_serial_settings);
kernel_serial_settings.flags |= ASYNC_LOW_LATENCY;
ioctl(uart_port, TIOCSSERIAL, &kernel_serial_settings);
}
int main(int argc, char** argv) {
wiringPiSetup();
pinMode(TIMING_UART_PIN,OUTPUT);
digitalWrite(TIMING_UART_PIN,LOW);
init_UART();
while (1)
{
usleep(1);
}
return 0;
}
void usartRX_interrupt (int status)
{
uint8_t bufferData;
digitalWrite(TIMING_UART_PIN,HIGH); //Debugging pin
read( uart_port, &bufferData , sizeof bufferData ); //read data
digitalWrite(TIMING_UART_PIN,LOW);
}