Arduino code:
Code: Select all
// Wire Slave Sender
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Sends data as an I2C/TWI slave device
// Refer to the "Wire Master Reader" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include <Wire.h>
void setup()
{
Serial.begin(9600);
delay(1000);
Serial.println("Startup");
Wire.begin(0x60); // join i2c bus with address #2
Wire.onRequest(requestEvent); // register event
}
void loop()
{
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
Serial.println("Wrote hello");
float a = 3.14159;
Wire.write((byte *) &a,4); // respond with message of 6 bytes
Serial.println('%f',(byte *) &a);
// as expected by master
}
void float2Bytes(float val,byte* bytes_array){
// Create union of shared memory space
union {
float float_variable;
byte temp_array[4];
} u;
// Overite bytes of union with float variable
u.float_variable = val;
// Assign bytes to input array
memcpy(bytes_array, u.temp_array, 4);
}
Code: Select all
>> i2cdetect -y -r 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- UU UU UU UU -- -- -- -- -- -- -- --
60: 60 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
>> i2cdump -y 1 0x60
No size specified (using byte-data access)
0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef
00: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX BANNED
10: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX BANNED
20: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX BANNED
30: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX BANNED
40: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX BANNED
50: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX BANNED
60: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX BANNED
70: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX BANNED
80: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX BANNED
90: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX BANNED
a0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX BANNED
b0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX BANNED
c0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX BANNED
d0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX BANNED
e0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX BANNED
f0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX BANNED
>> i2cget -y 1 0x60
0xd0
I can't however seem to submit a valid read request from the adafruit beaglebone i2c library. I want to read the 4 bytes from the arduino, but I cant seem to figure out how to do the equivalent of arduino's requestFrom function.