diff --git a/Makefile b/Makefile index 6f9c00de301f3c7e9598273287045b5148ffb2a6..56ae406856a198ddfb55a4b527e5a2bf4184ccf8 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,7 @@ compile_UART : g++ -o uart uart.cpp -lwiringPi compile_spi : - g++ -o spi spi.cpp -l wiringPi + g++ -o spi spi.cpp compile_encoder : g++ -o encoder encoder.cpp -l wiringPi diff --git a/motor b/motor deleted file mode 100755 index 3362d38843b55f93301e17d0e1e0810d601aeb94..0000000000000000000000000000000000000000 Binary files a/motor and /dev/null differ diff --git a/motor.c b/motor.c index 3e8fcaf8bb48589e3fd563c84c1a0275a002040c..c5398867f7d7c376249e3b7ce06e5e219be92603 100644 --- a/motor.c +++ b/motor.c @@ -1,3 +1,4 @@ + #include <stdio.h> #include <stdlib.h> diff --git a/spi b/spi deleted file mode 100755 index 0f019bce4b79cf28883b6f2dae4dff47546dc392..0000000000000000000000000000000000000000 Binary files a/spi and /dev/null differ diff --git a/spi.cpp b/spi.cpp index 8f4114b55bb303107a0f0208fe0a673336e2e209..3eee1f1ef8ded98ee65cf27123b76483b7a89f80 100644 --- a/spi.cpp +++ b/spi.cpp @@ -1,38 +1,73 @@ #include <stdlib.h> #include <stdio.h> -#include <wiringPi.h> -#include <wiringPiSPI.h> +#include <string.h> +#include <unistd.h> -#include <iostream> +#include <net/if.h> +#include <sys/socket.h> +#include <sys/ioctl.h> + +#include <linux/can.h> +#include <linux/can/raw.h> -using namespace std; -const int CHANNEL_CAN = 1; int main(int argc, char const *argv[]) { - int result, fd; - unsigned char buff[30]; + int fd; + struct sockaddr_can addr; + struct ifreq ifr; + struct can_frame frame; + + printf("CAN TRY\n"); - fd = wiringPiSPISetup(CHANNEL_CAN, 500000); - if (fd == -1) { - printf("Error setup\n"); + fd = socket(PF_CAN, SOCK_RAW, CAN_RAW); + if (fd < 0) { + perror("error socket"); exit(EXIT_FAILURE); } - - int reg = 0x40; - - int GPIO[] = {0b00000001, 0b00000010, 0b00001000, 0b00010000}; - buff[0] = reg; - for (int i = 0; i < 4; i++) { - buff[1] = GPIO[i]; - printf("buff %i : %x %x\n",i, buff[0], buff[1]); - result = wiringPiSPIDataRW(CHANNEL_CAN, buff, 2); - printf("buff %i : %x %x\n",i, buff[0], buff[1]); - buff[0] = reg; - delay(1500); + + strcpy(ifr.ifr_name, "can0"); + ioctl(fd, SIOCGIFINDEX, &ifr); + + memset(&addr, 0, sizeof(addr)); + addr.can_family = AF_CAN; + addr.can_ifindex = ifr.ifr_ifindex; + + if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { + perror("error bind"); + exit(EXIT_FAILURE); + } + + frame.can_id = 0x02; + frame.can_dlc = 1; + frame.data[0] = 0b011111111; + + + if (write(fd, &frame, sizeof(struct can_frame)) != sizeof(struct can_frame)) { + perror("error write"); + exit(EXIT_FAILURE); } + // int nbytes; + // struct can_frame readframe; + + + // nbytes = read(fd, &readframe, sizeof(struct can_frame)); + + // if (nbytes < 0) { + // perror("reading"); + // exit(EXIT_FAILURE); + // } + + // printf("0x%x [%d]\n", readframe.can_id, readframe.can_dlc); + // for (int i = 0; i < frame.can_dlc; i++) + // { + // printf("0x\n", readframe.data[i]); + // } + + // printf("\r"); + return 0; }