-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I2C_slave hangs because of tx_buffer_ptr is never set back and *tx_buffer_ptr gets out of bounds.. #99
Comments
A lot of this code has been taken as-is from Gigadevice without ever being written or looked at in detail by me -- it's more than plausible that we have a bug in the I2C slave implementation, or that calling the API in an unforseen manner causes these issues. I'll appreciate a PR on this matter if you can. If not, a small minimal sketch that reproduces the problem will also enable me to look into it. |
I have never made a pull request yet but would love to. I will fork this repo and try to get a working i2c_slave code. |
Okay here my changes :-) Then i can use the
and twi.cpp#L544 ->
I can upload test code for ESP32_S2_Mini (master) <-> GD32F130C6/C8 (slave) to a github repo if requested. ESP32_S2_Mini (master):
GD32F130C6/C8 (slave) :
That was a send+request at 20ms. 5ms also work. The tx buffer is only 32 bytes long. @maxgerhardt , feel free to simply replace my new two functions with the old non-functional ones :-) 01:20 , a good night from Germany :-) |
Okay here my pull request: #100 :-) |
i2c slave tx now working, see #99
Fixed per #100 |
@maxgerhardt i have uploaded the i2c test code to: https://github.com/RoboDurden/GD32_I2C_Slave |
Sorry @maxgerhardt for causing this issue. Try to get a GD32F130C8 become an i2c slave (SimpleFOC..) but it hangs after several requestEvent() with
Wire.write(aiBuffer,30); // hanging after: 1 -> 41 calls , 2 -> 21 calls , 10 -> 17 calls , 20 -> 12 calls , 30 -> 7 calls , 40 -> 0 calls
The buffer pointer gets initialized in the TwoWire constructor Wire.cpp#L42
_i2c.tx_buffer_ptr = _tx_buffer.buffer;
but is never reset yet incremented when a byte is transmitted in twi.cpp#L650
I also do not understand the validation in i2c_slave_write_buffer(...) in twi.cpp#L550
This function can easily be called multiple times with Wire.write(..) within the requestEvent() event function.
So already without reseting tx_buffer_ptr, it can easily overflow with multiple calls per event.
That event function is called in Wire.cpp#L297 and
But i do not see this head and tail to have any effect on the tx_buffer_ptr
I fear, that the code has to be rewritten completely.
And as i guess the tx_buffer should be a cyclic buffer, using a pointer is aweful.
why not make it readable like
aiTx_Buffer[iWritePos % buffSize]
???And if
head
is the position that should be sent next:And the i2c_slave_write_buffer function adds new data to the
tail
:If the i2c-slave code indeed has never been tested, i could make these changes. I have already spent the entire day on debugging this.
But first i would like to know if you would be happy to add to this repo :-)
And for sure i will be happy if everything with the code is fine and me simply being to stupid to use it porperly :-))
Greetings from Germany, Roland and :-)
The text was updated successfully, but these errors were encountered: