diff --git a/lib/nodes/can-test/Makefile b/lib/nodes/can-test/Makefile new file mode 100644 index 000000000..900a75b7e --- /dev/null +++ b/lib/nodes/can-test/Makefile @@ -0,0 +1,35 @@ +CC_PATH=/opt/Xilinx/SDK/2019.1/gnu/aarch64/lin/aarch64-linux/bin +CC_PREFIX=aarch64-linux-gnu- +CPATH=$(CC_PATH)/include +LD_LIBRARY_PATH=$(CC_PATH)/lib64:$(CC_PATH)/lib + +CC=$(CC_PATH)/$(CC_PREFIX)gcc +LD=$(CC) + +CC_FLAGS= +LD_FLAGS= + +OBJ_FILES=main.o +MISC_FILES=create_vcan.sh +FPGA_IP=137.226.133.231 + +BINARY=can + +.PHONY: all clean deploy + +all : $(BINARY) + +$(BINARY) : $(OBJ_FILES) + $(LD) $(CC_FLAGS) -o $@ $^ $(LD_FLAGS) + +%.o : %.c + $(CC) $(CC_FLAGS) -c -o $@ $< + +clean: + rm -f $(BINARY) $(OBJ_FILES) + +deploy: $(BINARY) + scp $(BINARY) root@$(FPGA_IP):~ + scp $(MISC_FILES) root@$(FPGA_IP):~ + ssh root@$(FPGA_IP) /home/root/create_vcan.sh + ssh root@$(FPGA_IP) /home/root/$(BINARY) diff --git a/lib/nodes/can-test/create_vcan.sh b/lib/nodes/can-test/create_vcan.sh new file mode 100755 index 000000000..b464d1aa7 --- /dev/null +++ b/lib/nodes/can-test/create_vcan.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +# source: https://en.wikipedia.org/wiki/SocketCAN + +ip link add dev vcan0 type vcan +ip link set up vcan0 +ip link show vcan0 diff --git a/lib/nodes/can-test/main.c b/lib/nodes/can-test/main.c new file mode 100644 index 000000000..bbf719cb3 --- /dev/null +++ b/lib/nodes/can-test/main.c @@ -0,0 +1,54 @@ +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include + +int +main(void) +{ + int s; + int nbytes; + struct sockaddr_can addr; + struct can_frame frame; + struct ifreq ifr; + + const char *ifname = "can0"; + + if((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) { + perror("Error while opening socket"); + return -1; + } + + strcpy(ifr.ifr_name, ifname); + ioctl(s, SIOCGIFINDEX, &ifr); + + addr.can_family = AF_CAN; + addr.can_ifindex = ifr.ifr_ifindex; + + printf("%s at index %d\n", ifname, ifr.ifr_ifindex); + + if(bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) { + perror("Error in socket bind"); + return -2; + } + + frame.can_id = 0x123; + frame.can_dlc = 2; + frame.data[0] = 0x11; + frame.data[1] = 0x22; + + nbytes = write(s, &frame, sizeof(struct can_frame)); + + printf("Wrote %d bytes\n", nbytes); + + return 0; +} +