I’m trying to get I2C communication between a Daisy Seed and an Arduino Uno to work. The idea is, that the arduino asks the Daisy for some data by providing it with the number of bytes it wants. The Daisy should then respond by sending the number of bytes required. I managed to get this to work between a Blue Pill and an arduino Uno, but I want the Daisy Seed to take over from the Blue Pill. On the Daisy Seed side I’m using the code below, which alas is not working.
When I add some debug prints it becomes clear that the _i2c.ReceiveBlocking call, which should read the number of bytes the Arduino wants to receive, almost immediately returns, and the value of ‘number’ always is zero while the arduino sends e.g. a number like 32.
Any thoughts on what is wrong here and whether this approach is viable in the first place?
using namespace daisy;
uint8_t random(uint8_t dontCare) {
// Not a real random function.
static uint8_t number = 10;
return number++;
}
void request_N_Bytes_and_Crc(uint8_t testData[32], uint8_t requestedNumber) {
uint8_t crc = 0;
uint8_t random_number = random(255);
for (uint8_t i = 0; i < requestedNumber; i++) {
testData[i] = random_number;
crc = crc + random_number;
random_number++;
}
// The CRC is send as the final uint8_t.
testData[requestedNumber] = crc;
}
int main(void)
{
static DaisySeed seed;
unsigned char address = 42;
uint8_t testData[33];
seed.Configure();
seed.Init();
initLeds();
seed.StartLog(false);
System::Delay(5000);
static constexpr I2CHandle::Config _i2c_config
= {I2CHandle::Config::Peripheral::I2C_1,
{{DSY_GPIOB, 8}, // SCL op Daisy Seed Pin 12
{DSY_GPIOB, 9}}, // SDA op Daisy Seed Pin 13
I2CHandle::Config::Speed::I2C_1MHZ};
I2CHandle _i2c;
_i2c.Init(_i2c_config);
while(1) {
// Get the requested number of bytes.
uint8_t number = 0;
I2CHandle::Result i2cResult = _i2c.ReceiveBlocking(address, &number, 1, 5000);
// Generate the requested number of bytes.
request_N_Bytes_and_Crc(testData, number);
// Transmit the requested number of bytes.
i2cResult = _i2c.TransmitBlocking(address, testData, number + 1, 500);
System::Delay(1000);
}