2
\$\begingroup\$

I managed to connect an old PS/2 keyboard to the GPIO pins of my Raspberry Pi. I do get correct data from the keyboard which means when I press 'A' on the keyboard I get 'A' in my C code. The big issue I've got now is that I can't put a sleep delay into my code because even usleep(1) is taking too much time so I don't get correct data or even any data anymore. I tried nanosleep too but for some reason this got totally ignored by my code. Is there any agreeable way to put in a short delay? Without a delay my code is consuming 99% of CPU time.

\$\endgroup\$
3
\$\begingroup\$

Unfortunately, there's really no good way to do this. When you call sleep, a system call is made and the OS goes off and runs other programs while you sleep. But the OS scheduler has a minimum timeslice it will do that on, and that's on the order of milliseconds, not microseconds. As a result, very small sleeps will tend to either not happen at all or take far too long, which is what you're seeing.

You have two realistic options: Write a kernel module that won't suffer under the same scheduling constraints, or use an offboard processor, such as a cheap 8-bit MCU to do the decoding, sending the results back via a serial port. I would recommend the latter.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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