Skip to content
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 tx now working, see #99 #100

Merged
merged 3 commits into from
Jun 11, 2023
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
12 changes: 10 additions & 2 deletions libraries/Wire/src/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,15 +299,23 @@ void TwoWire::onRequestService(void* pWireObj)
TwoWire* pWire = (TwoWire*) pWireObj;
// don't bother if user hasn't registered a callback
if (pWire->user_onRequest) {
// reset tx buffer iterator vars
// reset master tx buffer iterator vars
// !!! this will kill any pending pre-master sendTo() activity
pWire->_tx_buffer.head = 0;
pWire->_tx_buffer.tail = 0;

// reset slave tx buffer iterator vars
pWire->_i2c.tx_buffer_ptr = pWire->_tx_buffer.buffer;
pWire->_i2c.tx_count = 0;

// alert user program
pWire->user_onRequest();
}

// reset slave tx buffer iterator to let interrupt transmit the buffer
pWire->_i2c.tx_buffer_ptr = pWire->_tx_buffer.buffer;
}
}

// sets function called on slave write
void TwoWire::onReceive(void (*function)(int))
{
Expand Down
24 changes: 8 additions & 16 deletions libraries/Wire/src/utility/twi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,26 +541,18 @@ void i2c_attach_slave_tx_callback(i2c_t *obj, void (*function)(void*), void* pWi
* @param length Number of bytes to read
* @return status
*/
i2c_status_enum i2c_slave_write_buffer(i2c_t *obj, uint8_t *data, uint16_t length)
{
i2c_status_enum i2c_slave_write_buffer(i2c_t *obj, uint8_t *data, uint16_t length){
struct i2c_s *obj_s = I2C_S(obj);
uint8_t i = 0;
i2c_status_enum ret = I2C_OK;
if ( (obj_s->tx_count + length) > obj->tx_rx_buffer_size)
return I2C_DATA_TOO_LONG;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think incrementing tx_count should only happen if we know that we can hold the data, otherwise we get into an inconstistent state if we increment + return error. I.e., first check if (obj_s->tx_count + length <= obj->tx_rx_buffer_size), then increment.


if (length > obj->tx_rx_buffer_size) {
ret = I2C_DATA_TOO_LONG;
} else {
/* check the communication status */
for (i = 0; i < length; i++) {
*obj_s->tx_buffer_ptr++ = *(data + i);
}
obj_s->tx_count = length;
obj_s->tx_buffer_ptr = obj_s->tx_buffer_ptr - length;
}
return ret;
uint8_t i = 0;
for (i; i < length; i++)
*obj_s->tx_buffer_ptr++ = *(data + i);
obj_s->tx_count += length;
return I2C_OK;
}


/** Check the I2C bus to see if it's busy
*
* @param obj The I2C object
Expand Down