1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

can: add prototype code for testing can support

This commit is contained in:
Niklas Eiling 2020-03-02 10:49:20 +01:00 committed by Steffen Vogel
parent 5b1cda7a89
commit 9653ea5295
3 changed files with 96 additions and 0 deletions

View file

@ -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)

View file

@ -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

54
lib/nodes/can-test/main.c Normal file
View file

@ -0,0 +1,54 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <net/if.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/can.h>
#include <linux/can/raw.h>
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;
}