Skip to content

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

Merged
merged 3 commits into from
Sep 4, 2018

Conversation

LMESTM
Copy link
Member

@LMESTM LMESTM commented Aug 13, 2018

Description

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.

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.

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.
Copy link

@Squonk42 Squonk42 left a 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:

  1. if the BluePill is the master and the Pro Mini the slave, everything is still working OK

  2. 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!

@fpistm fpistm self-requested a review September 3, 2018 16:03
@fpistm fpistm added this to the 1.3.1 milestone Sep 3, 2018
@fpistm fpistm added bug 🐛 Something isn't working review on going fix 🩹 Bug fix labels Sep 3, 2018
Copy link
Member

@fpistm fpistm left a 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

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 */
Copy link
Member

Choose a reason for hiding this comment

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

listening

Copy link
Member Author

Choose a reason for hiding this comment

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

done

void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c)
{
i2c_t *obj = get_i2c_obj(hi2c);
/* reset transmit buffer size */
Copy link
Member

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

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@fpistm fpistm merged commit cc094f5 into stm32duino:master Sep 4, 2018
benwaffle pushed a commit to benwaffle/Arduino_Core_STM32 that referenced this pull request Apr 10, 2019
I2C Slave : rework slave receive data sequence
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working fix 🩹 Bug fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants