Comments (10)
Switching between master and slave mode is an interesting question. I never tried it.
Simultaneous operation of master and slave mode is impossible because of the shared buffers. That is why it is important to stop the slave mode before using master mode, otherwise the interrupt routine will overwrite the data in the buffer.
Theoretically, it should work like this:
Wire.onRequest(requestEvent);
Wire.onReceive(receiveEvent);
// master mode
Wire.begin()
Wire.beginTransmission()
Wire.write(slave_addr);
Wire.write(data1);
Wire.endTransmission()
// slave mode
Wire.begin(this_slave_addr) // this enables the slave interrupts
// doing the slave stuff and decide to switch the mode
Wire.end() // this disables the slave interrupts
// master mode
Wire.begin()
Wire.beginTransmission()
Wire.write(slave_addr);
Wire.write(data2);
Wire.endTransmission()
Please report if this is working. If not, please provide a minimal example and describe the problem in detail.
from usiwire.
Hi,
Thanks for getting back to me so fast :) I made a small example with an ATTiny85 connected to a VEML6070 light sensor and an Arduino Pro-Mini via I2C bus.
I made an absulute minimal code for both the Pro-Mini and the ATTiny. It can be found here: https://github.com/x821938/test_usi_i2c_master_slave_switch
The Attiny starts as a slave. If it receives the command byte 'L'. It:
- Ends slave mode
- Becomes a master and gets light data from VEML6070 UV sensor
- Enters slave mode again
The Pro-Mini waits a little after sending the 'L' command and then pull the light data from the ATTiny slave.
If I comment out the part of the Pro-Mini code where I send the 'L' command, then the ATTiny slave nicely talks back to me. Just sending zeroes (obviousely, because it never reads from sensor). But as soon as the ATTiny has switched to master mode, I get 255 values from it. (because of missing error handling - but I am trying to keep the code minimal for demonstration purposes)
Likewise I have also tested the ATTiny talking to the lightsensor - without ever enabling slave mode. Via the visual feedback led, I can prove that it also works fine by itself.
I hope the example makes sense.
Best regards,
Alex
from usiwire.
Hi,
the example code is clear to me. But I did not yet get, why you get 255.
Could it be, that the ATTiny did not release the SDA line correctly, e.g., does Pro-Mini read also 255 from the light sensor.
Did you can check the voltage of the SDA line? A call of readFromLightSensor();
in setup()
of the Pro-Mini can also help to investigate this problem.
from usiwire.
Here is output from my datalogger when attiny behaves good and wrong.
If that can help.
from usiwire.
Here is the output on my Pro-Mini:
Sending master a command: L
Giving him a little time to finish his job
We got light value from I2C slave: 255
We got light value directly from sensor: 0
Sending master a command: L
Giving him a little time to finish his job
We got light value from I2C slave: 255
We got light value directly from sensor: 0
Sending master a command: L
Giving him a little time to finish his job
We got light value from I2C slave: 255
We got light value directly from sensor: 6
When Wire.requestFrom fails to get data, it always return 255. I think the attiny releases the SDA fine. The Pro-mini reads the value fine. See output.
from usiwire.
Anoher dump.
from usiwire.
Hi again,
I found the mistake in your library :)
You forget to set values in the begin method for the slave.... When I change it to this, it works:
void USIWire::begin(uint8_t address) {
BufferIndex = 0;
BufferLength = 0;
transmitting = 0;
USI_TWI_Slave_Initialise(address);
}
If you only run slave mode it works because the variables get set in the inital declaration.
Best regards,
Alex
from usiwire.
Thank you for the information.
The missing reset of the variables in USIWire::begin
are definitely a bug. Please feel free to submit a pull request.
But, I did not understand, why this cause the problem with the 255 value. If the variables are not set to 0 during WIRE.begin()
, then BufferIndex
and BufferLength
differ from 0 after the call of Wire.requestFrom()
. This leads to a not correctly working Wire.read()
in slave mode. For your code on the ATTiny this means:
- after a reset and receiving the first 'L',
readFromLightSensor()
should work as expected and read out the sensor and write is intolightValue
. - since
readFromLightSensor()
callsWire.requestFrom()
,Wire.read()
will return -1 in slave mode andreadFromLightSensor()
will never be called again.
The dumps of your logic analyser show that the USIWire work correctly in slave mode, e.g., changes of the SDA signal for ACK.
Does the ATTiny still report 255 with you patch? If yes, can you provide a dump that show the 'L' request and the subsequent master mode of the ATTiny?
from usiwire.
When the attiny failed to switch from master to slave, it never answered when the pro mini was polling for data. This means that SDA was high all the time the the master was communicating. Because I didn't have error handling on my pro mini, the received value will be 255 because it's latching in binary ones on each SCL clock pulse.
from usiwire.
Closed with #6 .
from usiwire.
Related Issues (13)
- To large error HOT 1
- USIWire compile error HOT 4
- Two Attiny85s - NACK after sending 16 byte HOT 1
- Enhancement: Provide Callback on entering Interrupt Routine HOT 1
- Way to large sketch
- ATtiny85 hold time
- Bundling USIWire with MegaCore HOT 3
- test failure HOT 2
- Is SPI compatible? HOT 1
- Library Clarification HOT 6
- change pins Attiny85 HOT 1
- I2C speed for 1Mhz internal oscillator HOT 15
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from usiwire.