From 66d4364b5610f74d9b97236c384866ca419f439b Mon Sep 17 00:00:00 2001 From: Wilhelm Westermark Date: Sat, 12 Nov 2016 02:27:55 +0100 Subject: [PATCH] Code for the RPi --- RPi/Makefile | 19 +++++++++++++ RPi/README.md | 10 +++++++ RPi/i2c_challenge.c | 65 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 RPi/Makefile create mode 100644 RPi/README.md create mode 100644 RPi/i2c_challenge.c diff --git a/RPi/Makefile b/RPi/Makefile new file mode 100644 index 0000000..b462983 --- /dev/null +++ b/RPi/Makefile @@ -0,0 +1,19 @@ +# +# Makefile: +################################################################################# + + +#DEBUG = -g -O0 +DEBUG = -O3 +CC = gcc +INCLUDE = -I/usr/local/include +CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe + +LDFLAGS = -L/usr/local/lib +LDLIBS = -lwiringPi -lwiringPiDev -lpthread -lm + +# Should not alter anything below this line +############################################################################### + +SRC = i2c_challenge.c \ + diff --git a/RPi/README.md b/RPi/README.md new file mode 100644 index 0000000..0875d64 --- /dev/null +++ b/RPi/README.md @@ -0,0 +1,10 @@ +This is a runnable executable that sends I²C-values to a specified slave (In this case an AVR on address 0x20) + +On the pi you need: +wiringpi +gcc + +To compile and run: +make i2c_challenge + +./i2c_challenge diff --git a/RPi/i2c_challenge.c b/RPi/i2c_challenge.c new file mode 100644 index 0000000..ea5d48d --- /dev/null +++ b/RPi/i2c_challenge.c @@ -0,0 +1,65 @@ +O/* +*i2ctest.c +* Raspberry Pi I2C test using wiringPi library. +* +*Copyright (c) Nahid Alam. +*********************************************************** +*i2ctest is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * i2ctest is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * +*********************************************************** +*/ + +#include +#include +#include + +int main (int argc, char *argv[]) +{ + int fd; + int data; + int send=1; + wiringPiSetup () ; + fd=wiringPiI2CSetup (0x20) ; /*Use i2cdetect command to find your respective device address*/ + if(fd==-1) + { + printf("Can't setup the I2C device\n"); + return -1; + } + else + { + while(data != 6) { + wiringPiI2CWrite(fd, send); + data=wiringPiI2CRead(fd); + if(data==-1) + { + printf("No data\n"); + //return -1; + } + else if(data == send*3) { + printf("Sending challenge"); + wiringPiI2CWrite(fd, 4); + } + else if(data == 6) { + printf("Read proper response \n"); + break; + } + else + { + //print data + printf("Waiting to send challenge"); + } + printf(" data=%d\n", data); + + } + } + return 0; +} +