-
Notifications
You must be signed in to change notification settings - Fork 1k
Fix I2C slave issue #216
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
Fix I2C slave issue #216
Conversation
Fix stm32duino#212 Signed-off-by: Frederic.Pillon <[email protected]>
@@ -539,7 +539,7 @@ void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c) | |||
|
|||
if((obj->i2c_onSlaveReceive != NULL) && | |||
(obj->slaveMode == SLAVE_MODE_RECEIVE)) { | |||
nbData = I2C_TXRX_BUFFER_SIZE - obj->handle.XferCount; | |||
nbData = I2C_TXRX_BUFFER_SIZE - obj->handle.XferSize; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aren't you using HAL_I2C_SlaveRxCpltCallback and HAL_I2C_SlaveTxCpltCallback ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, only HAL_I2C_ListenCpltCallback
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. then it's not easy for me to understand the code. If this works - fine. But I think ListenCpltCallback does not mean that data has been transmitted already, so I don't understand the lines above and why XferCount is replaced by XferSize
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I think, that we missed that when @fprwi6labs do the #135.
While I2C is sequential it seems to be OK but this will need to be review to handle that properly.
Anyway this PR fix buffer allocation, so I will merge it and raise new issue to track the code review with HAL_I2C_SlaveRxCpltCallback
and HAL_I2C_SlaveTxCpltCallback
Fix stm32duino#212 Signed-off-by: Frederic.Pillon <[email protected]>
@@ -380,6 +380,13 @@ void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes) | |||
if(rxBufferIndex < rxBufferLength){ | |||
return; | |||
} | |||
|
|||
allocateRxBuffer(numBytes); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be better to do as little as possible inside the ISR? - I.e do the RX buffer allocation in begin() when slave mode is detected.
Since the current implementation of onReceiveService() never appends data to the RX buffer (it always start writing at index 0) it should be safe to allocate the buffer in begin().
At least as long as I2C_TXRX_BUFFER_SIZE <= BUFFER_LENGTH (add a static_assert for safety)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if numbytes is greater than the one allocated it need realloc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, but it will never be greater. I2C_TXRX_BUFFER_SIZE = BUFFER_LENGTH = 32 (at least for now)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
allocateRxBuffer(numBytes) can still be called in the ISR, just to make sure.
In the normal case it will do nothing if the buffer is pre-allocated in begin()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're right in case of slave receive. I will check all those things when I will review #217
Fix I2C slave issue
See #212