-
Notifications
You must be signed in to change notification settings - Fork 1k
I2C Slave : rework slave receive data sequence #306
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
Conversation
This work includes some rework of the Slave RX path, which is based on below principle: - we don't know in advance how many bytes will be sent by the I2C master so we'll be listening to bytes 1 by 1 - in order to get them one by one, we're programing the I2C with HAL_I2C_Slave_Sequential_Receive_IT and 1 byte at a time and we're using the HAL_I2C_SlaveRxCpltCallback to store the byte then programing again for next byte. - this sequence is ended when the HAL_I2C_ListenCpltCallback is called, which happens when the master ends the ongoing sequence. We can then prepare for the next one. In order to implement this mecanism, we're introduced a local counter slaveRxNbData where we store the number of received bytes, as well as a new slave mode SLAVE_MODE_LISTEN which allows for extra checks. i2c_s structure members that can be modified from main context or under interrupt context have been marked as volatile.
For STM32 families that have a DutyCycle init parameter, we need to select the Fast Mode dedicated configuration which will set the fast mode bit in CCR register.
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.
I tried an I2C communication between an Arduino Pro Mini 168 (3.3V) clone and an STM32F103C8T6 BluePill with the STM Core master branch plus PR #306, flashing the example "master_reader_writer" sketch on one side, and the "slave_sender_receiver" sketch on the other side:
-
if the BluePill is the master and the Pro Mini the slave, everything is still working OK
-
if the BluePill is the slave and the Pro Mini the master, everything is now working OK
Case 2 was not working previously:
http://www.stm32duino.com/viewtopic.php?t=3612#p45190
Thanks for the successful changes!
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.
Only some typo to fix.
Thanks
cores/arduino/stm32/twi.c
Outdated
i2c_t *obj = get_i2c_obj(hi2c); | ||
|
||
/* Previous master transaction now ended, so inform upper layer if needed | ||
* then prepare for listeing to next request */ |
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.
listening
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.
done
cores/arduino/stm32/twi.c
Outdated
void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c) | ||
{ | ||
i2c_t *obj = get_i2c_obj(hi2c); | ||
/* reset transmit buffer size */ |
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.
Please, remove one space and use capital letter for first letter.
/* Reset
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.
done
I2C Slave : rework slave receive data sequence
Description
This work includes some rework of the Slave RX path, which is based on
below principle:
so we'll be listening to bytes 1 by 1
HAL_I2C_Slave_Sequential_Receive_IT and 1 byte at a time and we're using
the HAL_I2C_SlaveRxCpltCallback to store the byte then programing again
for next byte.
which happens when the master ends the ongoing sequence. We can then
prepare for the next one.
In order to implement this mecanism, we're introduced a local counter
slaveRxNbData where we store the number of received bytes, as well as a
new slave mode SLAVE_MODE_LISTEN which allows for extra checks.
i2c_s structure members that can be modified from main context or under
interrupt context have been marked as volatile.
Addresses #217
TESTS
Example used: master_reader_writer + slave_sender_recevier
Has been tested with NUCLEO boards F411RE, L152RE, F091RC, L476_RG in various combinations 1 as master, the other as slave, with a X-NUCLEO-IKS01A1 connected on one board to provide the required pull-up resistors on the I2C lines.