Take the 2-minute tour ×
Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It's 100% free, no registration required.

Is there a simple way to use the registers as an interrupt trigger? I can make the application run in a new pid and poll for serial input using fork() I just need to read data, no write needed This is the "read" code I am using

fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
fcntl(fd, F_SETFL, 0);
char buf[256];
n = read(fd, (void*)buf, 255);

I am not even sure if I have framed the question correctly Could it look something like this?

int  com_data=0;

void main(void)
{
 if (com_data){
  --do something--
 }
}

char getcom()
    {
    retrieve byte from rxbuffer;
    return(byte);
    }

interrupt routine
    {
    com_data=1;
    }
share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

No "interrupt" needed. The "if(com_data)" would be achieved by using poll(2) to test if data is available on fd.

share|improve this answer
    
I do want to use an interrupt, it would be more efficient –  peterretief Nov 13 '12 at 7:32
    
@peterretief: poll(2) system call is using an interrupt for you. It will put your task to sleep until that interrupt wakes it which will make this function call return. So it is efficient and it is the way userspace programs are usually written. –  Krzysztof Adamski Nov 13 '12 at 8:01
    
@KrzysztofAdamski - but that only works if main() doesn't need to do anything else?! –  Andrew Nov 13 '12 at 8:56
    
@andrew I though of using fork() in main? –  peterretief Nov 13 '12 at 11:58
    
@Andrew poll(2) with a timeout of zero will return immediately and indicate whether data is ready to be read. –  Jim Paris Nov 13 '12 at 17:01
show 3 more comments

I was going to post this as a comment rather than an answer, but perhaps standalone is better.

It's a long time since I wrote interrupt driver drivers under Linux, so my memory is rusty... these days I'm more native.

There is an article in the Linux How To which talks about using interrupts for serial I/O. Sadly it is a bit RTFM (for setserial), so may not be overly helpful, but may give a pointer or two.

For example, ttyS0 normally uses IRQ number 4 known as IRQ4 (or IRQ 4). A list of them and more will be found in "man setserial" (search for "Configuring Serial Ports").

Further help on setting up interrupt handlers can be found on Linux Device Drivers, another site in my bookmarks.

However, you don't need to use interrupts, if your can manage polling adequately. You just need to configure the port to return (without delay) if no data is available. Replace fcntl(fd, F_SETFL, 0); with fcntl(fd, F_SETFL, FNDELAY);

The call to read() will return immediately with n=0 if no data is available.

share|improve this answer
1  
This answer is misleading in many ways. One, the device in question here is a USB device, so you will not have a single IRQ like a traditional PC com port; at best you'd have to use the USB controller's interrupt and actually speak to the HCI to find out if your previously scheduled URB has completed. Two, the asker is running in userspace; only a kernel driver can have an interrupt handler, but in this case the driver of course already exists and is loaded and working. An additional handler would do nothing. Three, fnctl is unneeded, O_NDELAY was already specified to open. –  Jim Paris Nov 13 '12 at 17:09
    
Also, using just read gives no way to find out if the fd has data available, without also reading that data. poll, on the other hand, would let you test whether data is available (equivalent to the asker's if (com_data) statement) without actually reading it. –  Jim Paris Nov 13 '12 at 17:12
1  
@JimParis I really do appreciate your help, to complicate matters even further the USB device is temporary, waiting for the GPIO level shifter. I will report back on what worked for me –  peterretief Nov 14 '12 at 8:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.