-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe 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(). There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -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. | ||
|
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
andHAL_I2C_SlaveTxCpltCallback