Lab_interaccio/2016/SNIFFER_I2C/SNIFFER_I2C.ino

96 lines
2.3 KiB
Arduino
Raw Permalink Normal View History

2025-02-25 21:29:42 +01:00
/******************************************************
Arduino I2C Sniffer
2011 J. M. De Cristofaro
CC-BY-SA 3.0
this code is unsupported -- use at your own risk!
connect one digital pin each to SCL and SDA
use pins on PORTD (Arduino UNO/Duemilanove pins #0 - 7)
do not connect to serial pins (0 & 1)!
connect GND on Arduino to GND of I2C bus
*******************************************************/
/** DESCRIPTION **/
/*******************************************************
this code runs a "one-shot" capture, meaning that
it only captures data once and then dumps it to the
serial port.
it starts capturing when it detects the SDA line has
gone low. it does not check the SCL line (as a proper
start condition detector would).
you can adjust the capture window to suit you needs.
a value of 300 is long enough to catch one byte sent
over i2c at the 100 kbit/s standard rate. the sample
data is good enough to show you the sequence of events,
but little else.
if you need very specific, time-aligned data, you should
use a logic analyzer or sampling oscilloscope.
*******************************************************/
// assign pin values here
const char sda_pin = 2;
const char scl_pin = 7;
// sampling window
const int data_size = 250;
// array to contain sampled data
boolean captured_data_scl[data_size];
boolean captured_data_sda[data_size];
// housekeeping booleans
boolean captured;
boolean dumped;
void setup()
{
SerialUSB.begin(250000);
pinMode(sda_pin, INPUT);
pinMode(scl_pin, INPUT);
captured = false;
dumped = false;
attachInterrupt(scl_pin, blink1, RISING);
SerialUSB.println("Good to go, chief!");
}
// main loop: waits for SDA to go low, then
// samples the data, then formats and dumps it
// to the serial port.
volatile byte state = LOW;
volatile byte val[data_size];
volatile byte x=0;
volatile byte y=0;
void loop()
{
// digitalWrite(13, state);
if (y==data_size)
{
for (int i=0; i<data_size; i++)
{
SerialUSB.print("0x");
SerialUSB.print(val[i], HEX);
SerialUSB.print(", ");
}
x = 0;
}
}
void blink1() {
// state = !state;
bitWrite(val[y], x, digitalRead(sda_pin));
x++;
if (x==8)
{
y++;
x=0;
}
}