1

I run into an issue with querying a uint32_t with an atmega328p master from an attiny85 master.

I have put both codes below.

I did run the equivalent code for just sending a uint8_t and all works perfectly this way. So the hardware is well connected and working and it confirms there is something with my code.

What happen here is the master seems to be blocked at the requestFrom() function call. I can not establish why this is. It does output the serial sent in the setup() part but outputs nothing else after that.

Could anyone help?

MASTER code

#include <Wire.h>
#define SLAVE_ADDR 0x04
void setup() {
 Serial.begin(115200);
 Serial.println("Master");
 Wire.begin(); // Master mode
 //Wire.setClock(100000);
}
void loop() {
 uint32_t receivedValue = 0;
 Wire.requestFrom(SLAVE_ADDR, (uint8_t)4); // Request 4 bytes
 if (Wire.available() == 4) {
 uint8_t b0 = Wire.read();
 uint8_t b1 = Wire.read();
 uint8_t b2 = Wire.read();
 uint8_t b3 = Wire.read();
 // Assemble uint32_t (must match byte order from slave)
 receivedValue = ((uint32_t)b3 << 24) | ((uint32_t)b2 << 16) |
 ((uint32_t)b1 << 8) | (uint32_t)b0;
 Serial.print("Received: ");
 Serial.println(receivedValue);
 }
 Serial.println(Wire.available());
 delay(1000);
}

SLAVE Code

#include <TinyWireS.h>
#define I2C_SLAVE_ADDR 0x4
volatile uint32_t sensorValue = 0;
void setup() {
 TinyWireS.begin(I2C_SLAVE_ADDR);
 TinyWireS.onRequest(requestEvent);
}
void loop() {
 sensorValue++;
 TinyWireS_stop_check();
}
void requestEvent() {
 uint32_t val = sensorValue;
 volatile uint8_t outBuf[4]; // buffer for outgoing data
 outBuf[0] = (uint8_t)(val & 0xFF);
 outBuf[1] = (uint8_t)((val >> 8) & 0xFF);
 outBuf[2] = (uint8_t)((val >> 16) & 0xFF);
 outBuf[3] = (uint8_t)((val >> 24) & 0xFF);
 for (uint8_t i = 0; i < 4; i++) {
 TinyWireS.send(outBuf[i]);
 }
}
asked Aug 16 at 15:23
1
  • I run into an issue ... what is the issue? Commented Aug 16 at 21:33

1 Answer 1

0

I found the issue. The requestEvent must only send one byte at a time.

Here is my new slave code that works.

#include <TinyWireS.h>
#define I2C_SLAVE_ADDR 0x4
volatile uint32_t sensorValue = 0;
volatile uint8_t outBuf[4]; // Buffer for the 4 bytes
volatile uint8_t pos = 0; // Current position in buffer
void setup() {
 TinyWireS.begin(I2C_SLAVE_ADDR);
 TinyWireS.onRequest(requestEvent);
}
void loop() {
 sensorValue++;
 TinyWireS_stop_check();
}
void requestEvent() {
 if (pos == 0) { // Pack fresh value at start of transaction
 uint32_t val = sensorValue;
 outBuf[0] = (uint8_t)(val & 0xFF); // LSB
 outBuf[1] = (uint8_t)((val >> 8) & 0xFF);
 outBuf[2] = (uint8_t)((val >> 16) & 0xFF);
 outBuf[3] = (uint8_t)((val >> 24) & 0xFF); // MSB
 }
 TinyWireS.send(outBuf[pos]); // Send one byte
 pos = (pos + 1) % 4; // Increment; wraps around if >4 bytes requested
}
answered Aug 16 at 15:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.