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

TWIM improvements. #242

Merged
merged 4 commits into from
Oct 7, 2020
Merged

TWIM improvements. #242

merged 4 commits into from
Oct 7, 2020

Conversation

Dirbaio
Copy link
Member

@Dirbaio Dirbaio commented Sep 30, 2020

  • Derive Copy, Clone, Eq, PartialEq for Error
  • Disallow zero-length buffers, fixes Disallow zero-length buffers in TWIM #208
  • Handle all errors. Previously only AddressNack was handled, and the transfer would hang if another error ocurred.
  • remove chunking in copy_write_then_read. The previous implementation would send a TWI start condition for each chunk, so the slave chip would see each as a separate transaction. This is not equivalent to write_then_read, which was the original goal of adding chunking. It seems TWIM has no way to send multiple buffers in a single transaction, therefore the ability to chunk is removed.

There are some issues left with TWIM:

  • There's no way for the user use SUSPEND manually if they want to do a more complex repeated start sequence. Nordic's lib has a "hold" flag that suspends instead of stopping so you can do any combination of reads and writes.

    If we do this it could be used for implementing write_then_read and copy_write_then_read to reduce code duplication

    This is probably left for a future PR :)

old, no longer apply

- `copy_write_then_read` splits written data into 1024 byte chunks and txs each individually. This actually causes a repeated start condition for each chunk, instead of a single start condition at the beginning like it would happen with `write_then_read`.

This should be at least documented, but I think in practice the chunking is useless because devices will take data after a repeated start as a new transaction (expecting you to send command/address bytes again).

Maybe we should remove the chunking?

  • copy_write_then_read should use SUSPEND. It seems that once LASTRX/LASTTX happens it's not OK to leave TWIM hanging, you must immediately STOP, SUSPEND, or STARTTX/STARTRX. This can be a problem if using TWIM from low priority code. Nordic says here:

    If a more complex repeated start sequence is needed and the TWI firmware drive is serviced in a low priority interrupt it may be necessary to use the SUSPEND task and SUSPENDED event to guarantee that the correct tasks are generated at the correct time.

    This was a problem before with write_then_read too, but this PR changes it to use SHORTS.

@Dirbaio Dirbaio marked this pull request as ready for review September 30, 2020 18:13
Copy link
Contributor

@jonas-schievink jonas-schievink left a comment

Choose a reason for hiding this comment

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

Nice! Just left a few nits.

nrf-hal-common/src/twim.rs Outdated Show resolved Hide resolved
nrf-hal-common/src/twim.rs Outdated Show resolved Hide resolved
nrf-hal-common/src/twim.rs Outdated Show resolved Hide resolved
nrf-hal-common/src/twim.rs Outdated Show resolved Hide resolved
nrf-hal-common/src/twim.rs Outdated Show resolved Hide resolved
nrf-hal-common/src/twim.rs Outdated Show resolved Hide resolved
nrf-hal-common/src/twim.rs Outdated Show resolved Hide resolved
nrf-hal-common/src/twim.rs Outdated Show resolved Hide resolved
Code for setting DMA buffers is deduplicated into fns set_tx_buffer, set_rx_buffer.

Fixes nrf-rs#208
Previously only AddressNack was handled, and the transfer would
hang if another error ocurred.
The previous implementation would send a TWI start condition for each chunk,
so the slave chip would see each as a separate transaction. This is not equivalent
to `write_then_read`, which was the original goal of adding chunking.

It seems TWIM has no way to send multiple buffers in a single transaction, therefore
the ability to chunk is removed.
@Dirbaio
Copy link
Member Author

Dirbaio commented Oct 7, 2020

All nits addressed (commits amended).

@jonas-schievink jonas-schievink merged commit 8c91438 into nrf-rs:master Oct 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Disallow zero-length buffers in TWIM
2 participants