Fast GPIO control


I have an Apalix iMX6 with Ixora dev board and writing code on VS Code with ApolloX and Docker.
I need to control GPIO pins for reading and writing really fast in my code. For example I need to make a GPIO to switch states in 20MHz frequency.

I am currently using gpiod.h for doing that with gpiod_ctxless_set_value and gpiod_ctxless_get_value but the functions are really slow. The switching which I expect to happen with 20MHz is done with 160Hz.

I need some other method for accessing and controlling the GPIOs without so much overhead or some other library which works faster.
Are there any options for that for Torizon?

Such a high frequency is absolutely impossible if you’re using a regular GPIO driver and user space application. You could try writing a custom Linux driver, but you still may not reach your goal. Could you please provide more details about your objective? Perhaps it can be solved in another way


I have a communication similar to SPI with CLK, CS pins but it has two input pins for data.
So basically on each falling edge of the CLK I need to read the data coming on these two data lines.
The problem is that unlike SPI, there are two input data pins and not only one. So in the design SPI was not used but the people designing it used some of the 8 GPIO pins provided on the Apalis iMX6 board.
This cannot be changed at this moment so I am trying to figure out how I can read data fast from these pins, because the device from which I need to read now has a minimum clk speed of 20kHz.

I looked in the pins in the datasheet but I am not sure if they can even be changed to SPI functionality. So I don’t know what could be the solution then if you say that it can’t be accomplished.

I may involve quite alot of work and I’m not sure if it is applicable to your platform.

Changing GPIO could be done via accessing the GPIO registers via /dev/mem. You mmap the specific registers into user space and than can change the GPIOs as fast as your CPU and GPIO hardware is, bypassing the kernel.

If the SoC contains a free DMA channel which can be connected to the GPIO register than it would also be possible to read GPIOs quite fast. For Rasperry PI there is a library called pigpio which samples up to 10 MHz (pigpio library). This library only works for Rasperry PI, but it may give an idea.

All of this is is quite a hack (but works quite nice)

But just to repeat: Nothing of this may be relevant for your platform.

Achieving 20KHz is probably doable. You can try modifying the SPI bitbang driver to meet your requirements. It’s likely that you’ll need to use a kernel with an PREEMPTRT patch. However, it can still be challenging to accomplish. Please refer to this thread - What are the disadvantages of bit banging SPI/I2C in embedded applications - Stack Overflow. I would recommend either using a dedicated microcontroller on your carrier board to communicate with your SPI-like peripherals and then using a regular SPI to communicate with the Colibri module, or you can use a Colibri module (imx7 or imx8X) with an additional M-core running FreeRTOS to handle this task.

Hi @Svetoslav !

I would like to add add here this relevant thread from NXP’s forum.

Best regards,

Hi @Svetoslav !

Do you have any updates regarding this topic?

Was your initial question answered?

Best regards,