Skip to content

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

Merged
merged 2 commits into from
Feb 2, 2018
Merged

Fix I2C slave issue #216

merged 2 commits into from
Feb 2, 2018

Conversation

fpistm
Copy link
Member

@fpistm fpistm commented Feb 2, 2018

See #212

@fpistm fpistm added the bug 🐛 Something isn't working label Feb 2, 2018
@fpistm fpistm added this to the Next release milestone Feb 2, 2018
@fpistm fpistm self-assigned this Feb 2, 2018
@fpistm fpistm requested a review from LMESTM February 2, 2018 09:22
@fpistm fpistm mentioned this pull request Feb 2, 2018
@@ -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;
Copy link
Member

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 ?

Copy link
Member Author

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

Copy link
Member

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

Copy link
Member Author

@fpistm fpistm Feb 2, 2018

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

@fpistm fpistm merged commit 2415879 into stm32duino:master Feb 2, 2018
@fpistm fpistm deleted the slave_i2c branch February 2, 2018 16:19
@@ -380,6 +380,13 @@ void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes)
if(rxBufferIndex < rxBufferLength){
return;
}

allocateRxBuffer(numBytes);
Copy link
Contributor

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)

Copy link
Member Author

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

Copy link
Contributor

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)

Copy link
Contributor

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()

Copy link
Member Author

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

benwaffle pushed a commit to benwaffle/Arduino_Core_STM32 that referenced this pull request Apr 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants