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: Implicitly copy buffer into RAM if needed when using embedded hal traits #165

Merged
merged 7 commits into from
Jul 5, 2020

Conversation

kalkyl
Copy link
Contributor

@kalkyl kalkyl commented Jun 24, 2020

Issue #150

@kalkyl kalkyl changed the title Spim/Twim: Implicitly copy buffer into RAM if needed when using embedded hal traits Twim: Implicitly copy buffer into RAM if needed when using embedded hal traits Jun 24, 2020
@Yatekii
Copy link
Contributor

Yatekii commented Jun 24, 2020

Could you be so kind and run cargo fmt such that the CI is happy?

Copy link
Contributor

@Yatekii Yatekii left a comment

Choose a reason for hiding this comment

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

Looks very good to me :) I added two questions about minor things.

unsafe { w.maxcnt().bits(rx_buffer.len() as _) });

// Chunk write data
let wr_buffer = &mut [0; FORCE_COPY_BUFFER_SIZE][..];
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let wr_buffer = &mut [0; FORCE_COPY_BUFFER_SIZE][..];
let wr_buffer = &mut [0; FORCE_COPY_BUFFER_SIZE.min(EASY_DMA_SIZE)][..];

I think we should take the minimum here too? Or ensure elseplace that FORCE_COPY_BUFFER_SIZE is always <= EASY_DMA_SIZE. Maybe we should do that where we define FORCE_COPY_BUFFER_SIZE even?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, i don't know how to do that min check in const though?
I guess ensuring FORCE_COPY_BUFFER_SIZE <= EASY_DMA_SIZE where we define FORCE_COPY_BUFFER_SIZE is the best solution, then we can just use FORCE_COPY_BUFFER_SIZE for our buffer and chunk sizes?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, I think we should use .min() at both places or at no place in this code then :) Otherwise it's just confusing I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Totally agree, so i will use FORCE_COPY_BUFFER_SIZE in both places then :)

How can i assert FORCE_COPY_BUFFER_SIZE <= EASY_DMA_SIZE when they are defined in "top-level"?

Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure we can ensure this at compile time other than manually making sure of this and maybe add a comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The only hacky way i can think of right now is not pretty and not zero cost, something like:
const _CHECK_FORCE_COPY_BUFFER_SIZE: usize = EASY_DMA_SIZE - FORCE_COPY_BUFFER_SIZE;
// ERROR: FORCE_COPY_BUFFER_SIZE must be <= EASY_DMA_SIZE
..that would fail with "attempt to subtract with overflow" :)
But i guess just a comment should be enough right?

Copy link
Contributor

Choose a reason for hiding this comment

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

Wont it just optimize that out and thus be zero cost?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh yes true!

self.0.events_lastrx.write(|w| w); // reset event

// Stop read operation
self.0.tasks_stop.write(|w|
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't it stop automatically when the requested data size was received?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, according to the manual:

"Note that the TWI master does not stop by itself when the RAM buffer is full, or when an error occurs. The STOP task must be issued, through the use of a local or PPI shortcut, or in software as part of the error handler."

So i guess an alternative could be to add a PPI shortcut instead, like:
self.0.shorts.modify(|_r, w| w.lastrx_stop().enabled());

What do you prefer?
Thanks for your comments :)

Copy link
Contributor

Choose a reason for hiding this comment

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

ok, this seems odd to me tbh, but I guess if the manual states this, it must be true :)

I think the short is cleaner but it also occupies a short slot which could be used by the user for another job. So I think we should leave it as is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool! :)

@jonas-schievink jonas-schievink merged commit 7b58f2b into nrf-rs:master Jul 5, 2020
@kalkyl kalkyl deleted the twim_copy branch September 4, 2020 13:40
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.

3 participants