Serial port signal(IRQ) handler (using C)


 
Thread Tools Search this Thread
Top Forums Programming Serial port signal(IRQ) handler (using C)
# 1  
Old 01-14-2015
Debian Serial port signal(IRQ) handler (using C)

Hello,

I'm writing some serial(UART) handler but have stuck on few issues, maybe anyone can help to show me what I'm doing wrong.
Basically I'm intending to write serial RX signal handler.
Application receives defined packages of data over serial which contains header and payload. Handler should analyse incoming stream and upon detection of header (header is 6B in length) switches to receive payload of length defined in header, then after receiving full message packs it and sends to application for handling.
Problem I'm facing is that at random moments in signal I receive errors that "Resource temporarily unavailable" while reading from ttyOx device and I see that sometimes I miss incoming data.
Also is it possible that if while I'm handling signal one more signal arrives and it is started to be handled parallel? If it is, what are the ways to prevent it? So I would be sure that no more signals will fire on same peripheral until I will finish handling what I have on my hands now.

Serial open and init:
Code:
int open_port(int port_nr) {
    int fd; /* File descriptor for the port */
    switch (port_nr) {
    case 1:
        fd = open("/dev/ttyO1", O_RDWR | O_NOCTTY | O_NDELAY); 
        break;
    case 2:
        fd = open("/dev/ttyO2", O_RDWR | O_NOCTTY | O_NDELAY); 
        break;
    default:
        printf("Invalid Serial port selected\n");
        return (-1);
    }
    if (fd == -1) {
                /* Could not open the port.*/
        perror("open_port Failed - ");
    } else {
        switch (port_nr) {
        case 1:
            sig_act.sa_handler = ttyO1_signal_handler_IO;
            UART_port_ttyO1 = fd;
            ttyO1_TX_list = NULL;
            if (pltfrm_mbx_init(&ttyO1_Mbx, sizeof(sPubMsg), 20) < 0) {
              printf("failed to create mailbox !! \n");
              return (-1);
            }
            break;
        case 2:
            sig_act.sa_handler = ttyO2_signal_handler_IO;
            UART_port_ttyO2 = fd;
            ttyO2_TX_list = NULL;
            if (pltfrm_mbx_init(&ttyO2_Mbx, sizeof(sPubMsg), 20) < 0) {
              printf("failed to create mailbox !! \n");
              return (-1);
            }
            break;
        default:
            printf("Invalid Serial port selected\n");
            return (-1);
        }
        sig_act.sa_flags = 0;
        sig_act.sa_restorer = NULL; 
        sigaction(SIGPOLL, &sig_act, NULL);
        fcntl(fd, F_SETFL, (FNDELAY | FASYNC));
        fcntl(fd, F_SETOWN, getpid());
    }
    return (fd);
}

void setup_port(int port_nr) {
    struct termios  options;
    int             port = port_nr_to_fd(port_nr);
    if (port < 0) {
        printf("Request to set-up invalid port(%d) detected...\n", port_nr);
    }
            /* get the current options */
    tcgetattr(port, &options);
            /* Set the to 57600kbps baud rate 8N1*/
    cfsetispeed(&options, B57600);
    cfsetospeed(&options, B57600);
    options.c_cflag &= ~(PARENB | CSTOPB | CSIZE);
    options.c_cflag |= CS8;    /* Select 8 data bits */
    options.c_cflag     |= (CLOCAL | CREAD);
    options.c_lflag     &= ~(ICANON | ECHO | ECHOE | ISIG);
    options.c_oflag     &= ~OPOST;
    options.c_iflag     &= ~(IXOFF | IXON);
    options.c_cc[VMIN]  = 0;
    options.c_cc[VTIME] = 0;
            /* set the options */
    tcsetattr(port, TCSANOW, &options);
}

Signal handler basics:
Code:
static int rd_serial(int port, uint8_t *buff, int len) {
    int res;
    res = read(port, buff, len); 
    if (res == -1) {
        perror("serial_read: Failed to read /dev/ttyO2 - ");
    } 
    return res;
}

static int handle_RX_payload(int port, sMsgData *RX_msg) {
***
    rd_serial(port, pnt, (RX_msg->message_len - rx_len));
***
}

static int handle_RX_header(int port, sMsgData *RX_msg) {
    static uint8_t  header[6];
    uint8_t         byte;
    int             rx_cnt;
    do {
        rx_cnt = rd_serial(port, &byte, 1);
        if (rx_cnt > 0) {
            memcpy(header, &header[1], 5);
            header[5] = byte; 
            RX_msg->mesage_type = check_header(header);
            if (RX_msg->mesage_type != 0xFF) {  //valid header sequence found
                RX_msg->message_len = (header[3] << 8) + header[2] - 4; 
                RX_msg->payload = (uint8_t*)malloc(RX_msg->message_len);
                return STAGE_COMPLETED;
            }
        }
    } while (rx_cnt > 0); 
    return EMPTY_BUFFER;
}

static uint8_t signal_handler_IO (int port, uint8_t *rx_state, sMsgData *RX_msg) {
    uint8_t   *state = rx_state;
    int       res;
***
    res = handle_RX_header(port, RX_msg);
***
    res = handle_RX_payload(port, RX_msg);
***
}

void ttyO1_signal_handler_IO (int status) {
    static uint8_t  state = 2;
    static sMsgData ttyO1_RX_msg;
    signal_handler_IO(UART_port_ttyO1, &state, &ttyO1_RX_msg);
}

void ttyO2_signal_handler_IO (int status) {
    static uint8_t  state = 2;
    static sMsgData ttyO2_RX_msg;
    signal_handler_IO(UART_port_ttyO2, &state, &ttyO2_RX_msg);
}

Error I see in my log:serial_read: Failed to read /dev/ttyO2 - : Resource temporarily unavailable


Laurynas
# 2  
Old 01-16-2015
Well during this time I found some ways to solve more or less all problems for now, tho some solutions are just nasty workarounds, so maybe on them you can offer some other possible solutions.
Over all there was two issues, signals firing while other signal is being handled, that messed up with "private" variables (using privates in handle_RX_header() and handle_RX_payload() was one of stupidest things I have done) also because of that some data was mixed of overwritten in buffer. And other issue was that Unix was converting '0x0D' to '0x0A' so on some CRC's I have failed to detect message header.

1st problem solved by:
Code:
void ttyO2_signal_handler_IO (int status) {
//handle_RX_header() and handle_RX_payload() private variables moved to privates of each ttyO signal handler.
    static struct _sRxSignalData    ttyO2_status = {2, {0, 0, 0, 0, 0, 0}};
    static sMsgData ttyO2_RX_msg;
//mutex was used to stop signal handling while other is not finished. 
    pthread_mutex_lock(&ttyO2_mutex);
//should stop errors - probably working didn't happined after changes, not sure if becouse of mutex or this
    ioctl(UART_port_ttyO2, REAL_FIONBIO, 1);
//should stop other signals in peripheral - wasn't working
    ioctl(UART_port_ttyO2, FIOASYNC, 0);
    signal_handler_IO(UART_port_ttyO2, &ttyO2_status, &ttyO2_RX_msg);
    ioctl(UART_port_ttyO2, REAL_FIONBIO, 0);
    ioctl(UART_port_ttyO2, FIOASYNC, 1);
    pthread_mutex_unlock(&ttyO2_mutex);
}

2nd problem solved by:
Code:
void setup_port(int port_nr) {
    struct termios  options;
    int             port = port_nr_to_fd(port_nr);
    if (port < 0) {
        printf("Request to set-up invalid port(%d) detected...\n", port_nr);
    }
            /* get the current options */
    tcgetattr(port, &options);
            /* Set the to 57600kbps baud rate 8N1*/
    cfsetispeed(&options, B57600);
    cfsetospeed(&options, B57600);
    options.c_cflag &= ~(PARENB | CSTOPB | CSIZE);
    options.c_cflag |= CS8;    /* Select 8 data bits */
            /* set raw input, 1 second timeout */
    options.c_cflag     |= (CLOCAL | CREAD);
    options.c_lflag     &= ~(ICANON | ECHO | ECHOE);
    options.c_oflag     &= ~OPOST;
//mess with '\n' and '\r' characters on data receive fix, break symbol should be ok too.
    options.c_iflag     &= ~(IXOFF | IXON | IGNBRK | INLCR | IGNCR | ICRNL);
    options.c_cc[VMIN]  = 0;
    options.c_cc[VTIME] = 0;
            /* set the options */
    tcsetattr(port, TCSANOW, &options);
}

Now for 1st issue would be nice to know how can I stop signal generation when I need and continue it again, don't really like stopping signals with mutexes. Way better solution would be stop their generation during handling.

For second - maybe I missed something and with current setup serial still be making changes to received data. Or maybe there is other way to open or set up serial port that UNIX WOULD NOT CHANGE ANY INCOMING OR OUTGOING DATA.

to migurus:
sorry seems I can't reply yet - not enough post writen, so will reply here:

"***" in code means code parts specific to data analyse, basically data checking, FSM and other code parts which just analyses data stream prints log and passes info to main program.

For ACK part my protocol specification is that no ACK to garbage, so if I miss some data I miss message and consider it as garbage.

In rd_serial the /dev/ttyO2 is hardcoded just temporary, since at the given moment I'm using only this port.


Laurynas
# 3  
Old 01-16-2015
according to stty, "raw mode" is
Code:
      raw    same as -ignbrk -brkint -ignpar -parmrk  -inpck  -istrip  -inlcr
              -igncr  -icrnl   -ixon   -ixoff   -iuclc  -ixany -imaxbel -opost
              -isig -icanon

So if you handle all those flags, that will do.

Interrupts are an odd way to handle this, though. You may also need to worry about spurious interrupts too. Why not put just read() in a loop? If you have several streams competing for attention, you can use select() to wait for one of several file descriptors to become ready, so you don't block waiting for one while another needs attention. Or, you could multithread it to process different descriptors independently.
# 4  
Old 01-16-2015
You can't safely use malloc() or pthread_mutex_lock() in a signal handler. You can only use async-signal safe calls in a signal handler. A list of Linux async-signal safe calls can be found here:

signal(7) - Linux manual page
This User Gave Thanks to achenle For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

problem in doing coding of signal handler

i m unble to execute code of signal handler using a) Wait b) Waitpid (1 Reply)
Discussion started by: madhura
1 Replies

2. Shell Programming and Scripting

Perl Signal Handler

I was working on some Perl code that does signal handling and I came across this one liner and wasn't sure what it was doing. local $SIG{__DIE__} = sub {$! = 2; die $_;}; I think the first part of the anonymous subroutine is setting $! to 2, but I am not sure what the second part is doing. ... (1 Reply)
Discussion started by: SFNYC
1 Replies

3. Programming

Signal Handler Hangs

Hi, I have a problem with signal handler algorithm in linux. My code is hanging ( It is continuously looping inside the signal handler) . I am pasting my code here... Please provide me some help regarding this. I googled many places and wrote this code.. but doesnt seem to be working without... (6 Replies)
Discussion started by: sree_ec
6 Replies

4. UNIX for Dummies Questions & Answers

Doubt with irq handler.......

Hello, I have develop a driver for my hardware and now, I need to handle a IRQ but I does not work. As I can understand, to handle a irq, it is necessary to make a request_irq(). If the return value is zero, ok, no problem to handle irq. Here is a easy example of my driver: #include... (8 Replies)
Discussion started by: webquinty
8 Replies

5. Shell Programming and Scripting

Perl - Problems with Signal Handler

I have a problem with signal handlers not working. I have a long 1000 line code and somehow this code for signal handling is not working: $SIG{INT} = \&interrupt; sub interrupt { print STDERR "Caught a control c!\n"; exit; # or just about anything else you'd want to do } Any... (2 Replies)
Discussion started by: som.nitk
2 Replies

6. Programming

Runaway SIGALRM signal handler

I have written a program to demonstrate a problem I have encountered when using BSD style asynchronous input using the O_ASYNC flag in conjunction with a real time interval timer sending regular SIGALRM signals to the program. The SIGIO handler obeys all safe practices, using only an atomic update... (8 Replies)
Discussion started by: stewartw
8 Replies

7. Programming

signal handler problems

Hey guys, I am trying to write a little shell, and was writing a signal handler to handle SIGINT (I am using 'stty intr ^C' and using ctrl-C to give SIGINT). I wrote this signal handler: void handle_sigint() { write(2,"handling sigint\n",16); write(1,"\nshell% ",8); } ... (4 Replies)
Discussion started by: blowtorch
4 Replies

8. Programming

signal handler for SIGCHLD

Hi, I have an c++ application which uses the function fork and execvp(). The parent does not wait until the child ends. The parents just creates children and let them do their stuff. You can see the parent program as a batch-manager. I have added a SIGCHLD handler to the program: void... (3 Replies)
Discussion started by: jens
3 Replies

9. Solaris

serial port signal

hi i am using solaris 9 on sparc . i was wondering if there was a command to control my serial interface , as to send a signal periodically every interval of time to the input of a 555 timer . thanks for your help .... (0 Replies)
Discussion started by: ppass
0 Replies

10. Shell Programming and Scripting

shell script signal handler

AIX 4.3.3 I am trying to write a signal handler into a ksh shell script. I would like to capture the SIGTERM, SIGINT, and the SIGTSTP signals, print out a message to the terminal, and continue executing the script. I have found a way to block the signals: #! /bin/ksh SIGTERM=15 SIGINT=2... (2 Replies)
Discussion started by: jalburger
2 Replies
Login or Register to Ask a Question