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.