You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.4 KiB
51 lines
1.4 KiB
/*
|
|
*i2ctest.c
|
|
* Raspberry Pi I2C test using wiringPi library.
|
|
*
|
|
*Copyright (c) Nahid Alam. <nahid.mahfuza.alam@gmail.com>
|
|
***********************************************************
|
|
*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 <stdio.h>
|
|
#include <wiringPi.h>
|
|
#include <wiringPiI2C.h>
|
|
|
|
#define I2C_ADDR 0x20
|
|
|
|
uint8_t solve_challenge(uint8_t arg) {
|
|
return (arg >> 3) ^ (arg << 3);
|
|
}
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
int fd; // Linux filehandle to the i2c device
|
|
uint8_t challenge;
|
|
uint8_t response;
|
|
wiringPiSetup();
|
|
|
|
fd = wiringPiI2CSetup(I2C_ADDR); // Initialize the i2c protocol with the address
|
|
if (fd == -1) { // Failed to contact device
|
|
printf("Can't setup the I2C device\n");
|
|
return -1;
|
|
}
|
|
|
|
delay(1);
|
|
|
|
challenge = wiringPiI2CRead(fd);
|
|
response = solve_challenge(challenge);
|
|
wiringPiI2CWrite(fd, response);
|
|
|
|
return 0;
|
|
}
|