Skip to content

drivers/soft_spi: fix soft spi transfer bug#16164

Merged
maribu merged 1 commit into
RIOT-OS:masterfrom
kex2017:master
Mar 17, 2021
Merged

drivers/soft_spi: fix soft spi transfer bug#16164
maribu merged 1 commit into
RIOT-OS:masterfrom
kex2017:master

Conversation

@kex2017

@kex2017 kex2017 commented Mar 8, 2021

Copy link
Copy Markdown

fix:fix drivers/soft_spi transfer one byte without read back bug
test:test read w25q64 id success

@jeandudey jeandudey added Area: drivers Area: Device drivers Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) labels Mar 8, 2021
@kfessel

kfessel commented Mar 8, 2021

Copy link
Copy Markdown
Contributor

I am no reviewer but to interested by the Tietel of your PR

The following might be early unnecessary optimization and i currently have no spi device to check but i would go with this:

out as a transfer register; shifting only by one (some archs (e.g.:AVR) a bad at shifting more than on step at once)

static inline uint8_t _transfer_one_byte(soft_spi_t bus, uint8_t out)
{
    int8_t bit = 0, i = 8;
    if (SOFT_SPI_MODE_1 == soft_spi_config[bus].soft_spi_mode ||
        SOFT_SPI_MODE_3 == soft_spi_config[bus].soft_spi_mode) {
        /* CPHA = 1*/
        gpio_toggle(soft_spi_config[bus].clk_pin);
    }

    do{
        bit = (out & (1 << 7)) >> 7;
        gpio_write(soft_spi_config[bus].mosi_pin, bit);

        xtimer_nanosleep(soft_spi_config[bus].soft_spi_clk);
        gpio_toggle(soft_spi_config[bus].clk_pin);

        out <<= 1; /*shift tranfer register*/

        bit = gpio_read(soft_spi_config[bus].miso_pin);
        out = bit ? out | 0x01 : out & 0xfe; /*set or delete bit 0*/

        xtimer_nanosleep(soft_spi_config[bus].soft_spi_clk);
        --i;
        if (i > 0) gpio_toggle(soft_spi_config[bus].clk_pin);
    }while(i > 0)

    if (SOFT_SPI_MODE_0 == soft_spi_config[bus].soft_spi_mode ||
        SOFT_SPI_MODE_2 == soft_spi_config[bus].soft_spi_mode) {
        /* CPHA = 0 */
        xtimer_nanosleep(soft_spi_config[bus].soft_spi_clk);
        gpio_toggle(soft_spi_config[bus].clk_pin);
    }

    return out;
}

For the read function i would just transfer 0x00 as done for hardware SPI.

@kex2017

kex2017 commented Mar 9, 2021

Copy link
Copy Markdown
Author

I am no reviewer but to interested by the Tietel of your PR

The following might be early unnecessary optimization and i currently have no spi device to check but i would go with this:

out as a transfer register; shifting only by one (some archs (e.g.:AVR) a bad at shifting more than on step at once)

static inline uint8_t _transfer_one_byte(soft_spi_t bus, uint8_t out)
{
    int8_t bit = 0, i = 8;
    if (SOFT_SPI_MODE_1 == soft_spi_config[bus].soft_spi_mode ||
        SOFT_SPI_MODE_3 == soft_spi_config[bus].soft_spi_mode) {
        /* CPHA = 1*/
        gpio_toggle(soft_spi_config[bus].clk_pin);
    }

    do{
        bit = (out & (1 << 7)) >> 7;
        gpio_write(soft_spi_config[bus].mosi_pin, bit);

        xtimer_nanosleep(soft_spi_config[bus].soft_spi_clk);
        gpio_toggle(soft_spi_config[bus].clk_pin);

        out <<= 1; /*shift tranfer register*/

        bit = gpio_read(soft_spi_config[bus].miso_pin);
        out = bit ? out | 0x01 : out & 0xfe; /*set or delete bit 0*/

        xtimer_nanosleep(soft_spi_config[bus].soft_spi_clk);
        --i;
        if (i > 0) gpio_toggle(soft_spi_config[bus].clk_pin);
    }while(i > 0)

    if (SOFT_SPI_MODE_0 == soft_spi_config[bus].soft_spi_mode ||
        SOFT_SPI_MODE_2 == soft_spi_config[bus].soft_spi_mode) {
        /* CPHA = 0 */
        xtimer_nanosleep(soft_spi_config[bus].soft_spi_clk);
        gpio_toggle(soft_spi_config[bus].clk_pin);
    }

    return out;
}

For the read function i would just transfer 0x00 as done for hardware SPI.

test on w25q64 ok

@kfessel

kfessel commented Mar 9, 2021

Copy link
Copy Markdown
Contributor

@kex2017: Thanks for testing

Even though I am not a reviewer, i would suggest:

  • do not PR from your master branch, create a new one prior PR git checkout -b <my new branch> and git push -u <your_repo> <my_new_branch>
  • keep the Headlines that you got when you started your PR (try to fit your information within them)
  • do not merge master into your PR instead someone may ask you to git rebase on <RIOT/origin>/master
  • the static test already have some issues with spelling and code formating (sorry for that; these tests don't like my writing style)
    you might want to git commit --fixup
  • If you want to restart the PR please put this one into references

@kex2017

kex2017 commented Mar 10, 2021

Copy link
Copy Markdown
Author

Thank you for your advice

@kfessel

kfessel commented Mar 10, 2021

Copy link
Copy Markdown
Contributor

@basilfx and @maribu might have interest at this PR that basicaly adds read support to soft_spi

@maribu maribu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for this fix. I wonder how this ever got merged in this state.

I have some comments regarding code style.

Comment thread drivers/soft_spi/soft_spi.c Outdated
Comment thread drivers/soft_spi/soft_spi.c Outdated
Comment thread drivers/soft_spi/soft_spi.c Outdated
Comment thread drivers/soft_spi/soft_spi.c Outdated
Comment thread drivers/soft_spi/soft_spi.c Outdated
Comment thread drivers/soft_spi/soft_spi.c Outdated
Comment thread drivers/soft_spi/soft_spi.c Outdated
Comment thread drivers/soft_spi/soft_spi.c Outdated
Comment thread drivers/soft_spi/soft_spi.c Outdated
Comment thread drivers/soft_spi/soft_spi.c Outdated
@maribu maribu added Reviewed: 1-fundamentals The fundamentals of the PR were reviewed according to the maintainer guidelines Reviewed: 2-code-design The code design of the PR was reviewed according to the maintainer guidelines Reviewed: 4-code-style The adherence to coding conventions by the PR were reviewed according to the maintainer guidelines Reviewed: 5-documentation The documentation details of the PR were reviewed according to the maintainer guidelines CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Mar 13, 2021
@maribu

maribu commented Mar 13, 2021

Copy link
Copy Markdown
Member

Code looks good to me. Let's see if the CI has some comments. Feel free to squash.

It might be that now some boards fail to build test applications or similar due to the increased ROM size. If that happens, we need to disable the final linking stage in the affected application by adding the affected board to the applications Makefile.ci - but maybe we're like and everything still builds fine.

@kex2017

kex2017 commented Mar 15, 2021

Copy link
Copy Markdown
Author

adding the affected board to the applications Makefile.ci

Maybe add the affected board to the applications Makefile.ci is a good way

@maribu

maribu commented Mar 15, 2021

Copy link
Copy Markdown
Member

adding the affected board to the applications Makefile.ci

Maybe add the affected board to the applications Makefile.ci is a good way

Not needed, all builds succeeded :-)

@maribu

maribu commented Mar 15, 2021

Copy link
Copy Markdown
Member

Tested using #16196. Please squash!

@maribu maribu added the Reviewed: 3-testing The PR was tested according to the maintainer guidelines label Mar 15, 2021
@maribu

maribu commented Mar 16, 2021

Copy link
Copy Markdown
Member

@kex2017 Could you combine all commits into the first using git rebase -i master? Then change pick to fixup for all but the first commit. Here is a random search hit for a guide on squashing for more details.

@kfessel

kfessel commented Mar 16, 2021

Copy link
Copy Markdown
Contributor

@kex2017 maybe its not so easy for you since this is your master branch you need to rebase it on upstream/master

git rebase -i origin/master if origin is the upstream RIOT remote

if you do not yet have an upstream RIOT remote you might need to create one

git remote add up https://github.com/kex2017/RIOT.git
git fetch up

and rebase on its master

git rebase -i up/master

and then mark your commits as @maribu described

@maribu maribu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ACK. Thanks for the fix. A very nice first contribution, btw!

@maribu maribu changed the title drivers/soft_spi:fix soft spi transfer bug drivers/soft_spi: fix soft spi transfer bug Mar 17, 2021
@maribu maribu merged commit 6f011c8 into RIOT-OS:master Mar 17, 2021
@kaspar030 kaspar030 added this to the Release 2021.04 milestone Apr 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: drivers Area: Device drivers CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Reviewed: 1-fundamentals The fundamentals of the PR were reviewed according to the maintainer guidelines Reviewed: 2-code-design The code design of the PR was reviewed according to the maintainer guidelines Reviewed: 3-testing The PR was tested according to the maintainer guidelines Reviewed: 4-code-style The adherence to coding conventions by the PR were reviewed according to the maintainer guidelines Reviewed: 5-documentation The documentation details of the PR were reviewed according to the maintainer guidelines Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants