Problems with using sys/io.h outb() in linux in a thread

Using OF to solve and display toolpaths for a CNC lathe and then sending the pulse to stepper motor drivers via the parallel port.

Problem - getting some problems with the program crashing.

Using debian 3.16 64 bit and OF 0.9.8

In a separate C program, using the outb() function from sys/io.h will correctly operate by sending 8-bit pulses through pins 2-9 on a parallel port breakout board. I can verify the function I wrote and nanosleep() for generating a 30khz 0 to 2.5 vdc square wave pulse train to the stepper motor controllers.

The problem is, when I use a thread in the OF program to send those pulses out, the program crashes and the dmesg output is:

[489010.960452] traps: toolpathSolver_[24298] general protection ip:41cfbd sp:7f4856709620 error:0 in toolpathSolver_debug[400000+450000]
[489107.998364] traps: toolpathSolver_[24358] general protection ip:41cfbd sp:7f10f2735620 error:0 in toolpathSolver_debug[400000+450000]
[489213.467951] traps: toolpathSolver[25051] general protection ip:4342d9 sp:7fde7bbcc970 error:0 in toolpathSolver[400000+389000]

I’ve tried running it with or without debugging symbols. The user which is running the program belongs to the lp group, which does not require sudo to access the parallel port. Still, running the OF program as root still causes a crash.

I’m fairly sure the loop below needs to be in a thread:

sendPulse(pulseCode[currentToolpathStep], pulseLength[currentToolpathStep]);

the sendPulse(short outPulse, double outDuration) function I wrote uses outb() and nanosleep() to generate the square wave needed to step the stepper motors and control direction.

sendPulse() works fine in a separate plain C program.

Using it in a thread in the OF program does not.

No good answers around the interwebs regarding what is going on.

I looked into the serial functions of OF, but that is not going to work, seeing that I’m pushing 8 bits via the parallel port.

Is there something I am missing here?

Tried adding:

attribute ((optimize(3)))

to the function declaration for sendPulse(), since it seems optimization 3 is needed for parallel port access, but still not working…

Also tried forcing O3 with pragmas in the main.cpp file. No luck there either…


Problem solved. Needed an ioperm() call in setup() and run as root.

30khz square waves coming out… like dogshit. Hiccups every second or two. Maybe there is a prioritization scheme for threads?