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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cores/arduino/stm32/twi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

if(nbData != 0) {
obj->i2c_onSlaveReceive(obj->i2cTxRxBuffer, nbData);
}
Expand Down
9 changes: 8 additions & 1 deletion libraries/Wire/src/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

// error if no memory block available to allocate the buffer
if(rxBuffer == nullptr){
Error_Handler();
}

// copy twi rx buffer into local read buffer
// this enables new reads to happen in parallel
memcpy(rxBuffer, inBytes, numBytes);
Expand Down Expand Up @@ -423,7 +430,7 @@ void TwoWire::onRequest( void (*function)(void) )
* @note Minimum allocated size is BUFFER_LENGTH)
* @param length: number of bytes to allocate
*/
inline void TwoWire::allocateRxBuffer(size_t length)
void TwoWire::allocateRxBuffer(size_t length)
{
if(rxBufferAllocated < length) {
// By default we allocate BUFFER_LENGTH bytes. It is the min size of the buffer.
Expand Down
2 changes: 1 addition & 1 deletion libraries/Wire/src/Wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class TwoWire : public Stream
static void onRequestService(void);
static void onReceiveService(uint8_t*, int);

void allocateRxBuffer(size_t length);
static void allocateRxBuffer(size_t length);
void allocateTxBuffer(size_t length);

void resetRxBuffer(void);
Expand Down