Skip to content

When to use modify() vs write()? #325

Answered by adamgreig
wpd asked this question in Q&A
Discussion options

You must be logged in to vote

Thanks for starting the first discussion!

The read/modify/write methods are documented on svd2rust, but the gist is:

  • write() starts from the register's reset value (which might not be zero), then changes just the fields you specify, and writes the result
  • modify() starts with a read of the register, then changes the fields you specify and writes the result
  • the lesser-used write_with_zero() starts from 0, changes your fields, writes.

So, whenever you only want to change a few fields, you have to use modify(), otherwise you'll wipe out any changes to the other fields.

In the book's case, it's fine to use write() since we are happy to set all the other pins (0-7) to 0 and just turn on 8-15.…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@wpd
Comment options

Answer selected by wpd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants