Export_store: invalid GPIO

Dear Community,

I built an extra board with a few LEDs which I can turn on and off using linux commands on minicom. I have written an C-Program in order to control de LEDs for example, when a CAN device has been activated.

#include <stdlib.h>		//allows use of system() -> int system(const char *command);
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>

#include <linux/can.h>
#include <linux/can/raw.h>

int main(void)
{
	//set up the can1 with the bitrate 250000
	char command[100];

	strcpy(command, "ip link set can1 up type can bitrate 250000");
	system(command);

	printf("\nCAN1 should be up and running\n");

	//create a socket
	int s = socket(PF_CAN, SOCK_RAW, CAN_RAW); // @suppress("Symbol is not resolved")
	//locate the interface wished to be used
	struct ifreq ifr;
	strcpy(ifr.ifr_name, "can1");
	ioctl(s, SIOCGIFINDEX, &ifr);

	//select the CAN interface and bind the socket to it
	struct sockaddr_can addr;
	addr.can_family = AF_CAN;
	addr.can_ifindex = ifr.ifr_ifindex;
	if(bind(s, (struct sockaddr*)&addr, sizeof(addr)) < 0)
	{
		//error
		printf("Error binding the can interface to the socket\n");
	}
	else
	{
		int fd;
		//export GPIO
		fd = open("/sys/class/gpio/export", O_WRONLY);
		write(fd, "323", 2);
		close(fd);

		//configure as output
		fd = open("/sys/class/gpio/gpio323/direction", O_WRONLY);
		write(fd, "out", 3);
		close(fd);

		//turn on green LED5
		fd = open("/sys/class/gpio/gpio323/value", O_WRONLY | O_SYNC);
		write(fd, "1", 1);
		close(fd);
	}
	struct can_frame frame;

	//read message from CAN bus
	int bytes_read = read(s, &frame, sizeof(struct can_frame));

	printf("Frame ID = %X, Frame DLC = %d, Frame Data = %X %X \n", frame.can_id, frame.can_dlc, frame.data[0], frame.data[1]);

	return 0;

}

The compilation works fine and the CAN is activated, but I get the following output regarding the GPIO that controls the LED:

[ 3278.438443] export_store: invalid GPIO 32

Notice that it says the GPIO 32 is not valid, when the GPIO that I want to use is the number 323.
I have tried a similar code on my Colibri iMX6 and it worked perfectly.

Thank you for your help.

Kind regards.

Hi @cae30989

Thanks for writing to the Toradex Community!

I have tried a similar code on my Colibri iMX6 and it worked perfectly.

How did you compile the code for iMX8DX? Which Toolchain did you use?

Best regards,
Jaski

Hi @jaski.tx,

I used the cross toolchain “gcc-arm-8.2-2019.01-x86_64-aarch64-linux-gnu” that I downloaded from here. I used Eclipse. The CAN does go up and it shows that something was sent, the only thing not working is the GPIO. I tried with some of the other GPIOS but it’s like it only takes the first two numbers that I give.

Kind regards

HI @cae30989

In this code snippet, the number of characters to write should be 3 and not 2.

//export GPIO
fd = open("/sys/class/gpio/export", O_WRONLY);
write(fd, "323", 3 (not 2) );

Best regards,
Jaski

Hi @jaski.tx,

thank you very much for your help.

You are welcome.

Best regards,
Jaski