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

STM32: make i2c_salve_read return the number of bytes read #14659

Merged
merged 2 commits into from
Jun 10, 2021

Conversation

pennam
Copy link
Contributor

@pennam pennam commented May 14, 2021

Summary of changes

If no timeout happens during communication i2c_slave_read(i2c_t *obj, char *data, int length) returned value is always equal to length parameter even if the master sends a lower number of bytes. Subsequently mbed-os read API will return always success.

The proposed solution is the same implemented here https://github.com/stm32duino/Arduino_Core_STM32/blob/655587065fbdd6324655fee0b13d268507e23a40/libraries/Wire/src/utility/twi.c#L1014

With this changes if the master writes 5 bytes and we read less than 5 bytes slave.read() will return success, same happens if we read exactly 5 bytes, but if we want to read 5 bytes and master writes only 4, slave.read() will return an error

Impact of changes

All ST targets

Migration actions required

Documentation


Pull request type

[X] Patch update (Bug fix / Target update / Docs update / Test update / Refactor)
[] Feature update (New feature / Functionality change / New API)
[] Major update (Breaking change E.g. Return code change / API behaviour change)

Test results

I've tested the changes on PORTENTA target using this code and works as described above.

#include "mbed.h"

// Blinking rate in milliseconds
#define BLINKING_RATE     500ms
#define BYTES_TO_READ     6

Thread thread;
I2CSlave slave(PH_8,PH_7);
I2C      master(PB_7,PB_6);

void slaveThread()
{
    char buf[10] = {0};

    slave.address(0xA0);
    while (1) {
        int i = slave.receive();
        switch (i) {
            case I2CSlave::ReadAddressed:
                break;
            case I2CSlave::WriteGeneral:
                break;
            case I2CSlave::WriteAddressed:
                int ret = slave.read(buf, BYTES_TO_READ);
                if(ret == 0) {
                    printf("Read A:");
                    for(int i = 0; i < BYTES_TO_READ; i++) {
                        printf(" %d",buf[i]);
                    }
                    printf("\n");
                } else {
                    printf("Read A: Error\n");
                }
                break;
        }
        for (int i = 0; i < 10; i++) {
            buf[i] = 0;    // Clear buffer
        }
    }
}

int main()
{
    // Initialise the digital pin LED1 as an output
    DigitalOut led(LED1);

    printf("Start slave thread\n");
    thread.start(slaveThread);

    while (1) {
        led = !led;

        printf("Master write\n");

        char cmd[5] = {0,1,2,3,4};
        master.write(0xA0, cmd, 5);

        ThisThread::sleep_for(BLINKING_RATE);
    }
}
[] No Tests required for this change (E.g docs only update)
[] Covered by existing mbed-os tests (Greentea or Unittest)
[] Tests / results supplied as part of this PR

Reviewers

@facchinm @jeromecoutant @0xc0170


@ciarmcom ciarmcom added the release-type: patch Indentifies a PR as containing just a patch label May 14, 2021
@ciarmcom ciarmcom requested review from 0xc0170, jeromecoutant and a team May 14, 2021 15:00
@ciarmcom
Copy link
Member

@pennam, thank you for your changes.
@0xc0170 @jeromecoutant @ARMmbed/mbed-os-maintainers please review.

@0xc0170 0xc0170 changed the title [STM32] make i2c_salve_read return the number of bytes read STM32: make i2c_salve_read return the number of bytes read May 18, 2021
Copy link
Contributor

@0xc0170 0xc0170 left a comment

Choose a reason for hiding this comment

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

Please add info from description to the commit message (what is being fixed and why)

Also keep the style consistent in i2c_api.c file (if ( - space)

0xc0170
0xc0170 previously approved these changes May 21, 2021
@mergify
Copy link

mergify bot commented May 24, 2021

This PR cannot be merged due to conflicts. Please rebase to resolve them.

@mergify mergify bot removed the needs: CI label May 24, 2021
…ad API return an error if less bytes are readed
@mergify mergify bot dismissed 0xc0170’s stale review May 25, 2021 06:47

Pull request has been modified.

@pennam
Copy link
Contributor Author

pennam commented May 25, 2021

rebased to resolve conflict in targets/TARGET_STM/i2c_api.c

0xc0170
0xc0170 previously approved these changes May 31, 2021
@0xc0170
Copy link
Contributor

0xc0170 commented May 31, 2021

CI started

@mergify mergify bot added the needs: work label May 31, 2021
@mergify mergify bot removed the needs: CI label May 31, 2021
@mbed-ci
Copy link

mbed-ci commented May 31, 2021

Jenkins CI Test : ❌ FAILED

Build Number: 1 | 🔒 Jenkins CI Job | 🌐 Logs & Artifacts

CLICK for Detailed Summary

jobs Status
jenkins-ci/mbed-os-ci_unittests ✔️
jenkins-ci/mbed-os-ci_cmake-cloud-example-GCC_ARM
jenkins-ci/mbed-os-ci_build-cloud-example-GCC_ARM
jenkins-ci/mbed-os-ci_cmake-cloud-example-ARM
jenkins-ci/mbed-os-ci_build-cloud-example-ARM
jenkins-ci/mbed-os-ci_build-greentea-ARM
jenkins-ci/mbed-os-ci_cmake-example-ARM
jenkins-ci/mbed-os-ci_cmake-example-GCC_ARM
jenkins-ci/mbed-os-ci_build-example-GCC_ARM
jenkins-ci/mbed-os-ci_build-example-ARM
jenkins-ci/mbed-os-ci_build-greentea-GCC_ARM

@pennam
Copy link
Contributor Author

pennam commented May 31, 2021

Need to update objects.h file of all ST platforms to PASS ci. @jeromecoutant what's your opinion on this changes? If you are OK with them i will fix the other targets. Thanks

@jeromecoutant
Copy link
Collaborator

Approved !

@mergify mergify bot dismissed 0xc0170’s stale review June 3, 2021 11:37

Pull request has been modified.

@pennam
Copy link
Contributor Author

pennam commented Jun 3, 2021

CI should be fixed now

@adbridge
Copy link
Contributor

adbridge commented Jun 9, 2021

@0xc0170 looks like you added the label but didn't actually start the CI ?

@0xc0170
Copy link
Contributor

0xc0170 commented Jun 9, 2021

@0xc0170 looks like you added the label but didn't actually start the CI ?

Correct, I had to wait until 5.15 job completed (the breakage in 5.15 to be cleared). Starting all master jobs now

@0xc0170
Copy link
Contributor

0xc0170 commented Jun 9, 2021

CI started

@mbed-ci
Copy link

mbed-ci commented Jun 9, 2021

Jenkins CI Test : ✔️ SUCCESS

Build Number: 2 | 🔒 Jenkins CI Job | 🌐 Logs & Artifacts

CLICK for Detailed Summary

jobs Status
jenkins-ci/mbed-os-ci_build-cloud-example-ARM ✔️
jenkins-ci/mbed-os-ci_unittests ✔️
jenkins-ci/mbed-os-ci_cmake-cloud-example-GCC_ARM ✔️
jenkins-ci/mbed-os-ci_cmake-cloud-example-ARM ✔️
jenkins-ci/mbed-os-ci_build-cloud-example-GCC_ARM ✔️
jenkins-ci/mbed-os-ci_cmake-example-ARM ✔️
jenkins-ci/mbed-os-ci_build-example-GCC_ARM ✔️
jenkins-ci/mbed-os-ci_build-example-ARM ✔️
jenkins-ci/mbed-os-ci_cmake-example-GCC_ARM ✔️
jenkins-ci/mbed-os-ci_build-greentea-ARM ✔️
jenkins-ci/mbed-os-ci_build-greentea-GCC_ARM ✔️
jenkins-ci/mbed-os-ci_greentea-test ✔️

@0xc0170 0xc0170 merged commit b74f62c into ARMmbed:master Jun 10, 2021
@mergify mergify bot removed the ready for merge label Jun 10, 2021
@mbedmain mbedmain added release-version: 6.12.0 Release-pending and removed release-type: patch Indentifies a PR as containing just a patch Release-pending labels Jun 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants