-
Notifications
You must be signed in to change notification settings - Fork 67
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
Fix spi initialization #35
Conversation
Glad you caught this, and it's good to see the conversation over on #36! I don't have a |
Ok it seems to compile for the stm32f373, so that is a great sign. 👍 Yeah, the I would like to expand the example to confirm, that data transmission is possible. Maybe I have time for this today. |
I'm not quite getting it. (so feel free to skip this post, if you're busy - I'd be happy for the clarification though) The doc says:
spi.cr1.write(
// start of closure - the reset value is being loaded (in this case, it's 0)
|w| {
// after setting the master bit,
// br() returns a _BRW struct, which is stored in w for further modification.
w.mstr().set_bit().br();
unsafe { w.bits(br); }
// end of closure
// it modified the reset value (0) such that
// * the mstr bit is set and
// * the br part is set according to the br variable
}
// end of write - the modified reset value is written.
) however (if I understand your comment correctly) this seems not to be the case, but the following is happening: spi.cr1.write(
// start of closure
|w| {
// first finished expression statement within the closure.
// it started with loading the reset value, modified it and wrote it at the end of the expression.
// the _BRW struct is stored in w for further modification
w.mstr().set_bit().br();
// however, the reset value is loded again, the br part is being modified
// and then everything is written to the register (again)
unsafe { w.bits(br); }
}
) This is odd. Next @Sh3Rm4n wrote
|
Well, I have to correct myself: I thought, that I did test to compile the hal with version
That's how I have observed it, while debugging the code. At first, I thought that it would work like you described in the first example, however it apparently does not. I could confirm the behavior, while deubgging the code build with the I tried to look for |
Ready for review, as I've added an example, and confirmed that it is working for the stm32f303 discovery board. I find the spi |
Ok, I now found out what the real issue was. unsafe {
w.bits(br);
} The This is an easy fix: diff --git a/src/spi.rs b/src/spi.rs
index 28e59ed..e422b4a 100644
--- a/src/spi.rs
+++ b/src/spi.rs
@@ -175,12 +175,9 @@ macro_rules! hal {
.cpol()
.bit(mode.polarity == Polarity::IdleHigh)
.mstr()
- .set_bit()
- .br();
+ .set_bit();
- unsafe {
- w.bits(br);
- }
+ w.br().bits(br);
w.spe()
.set_bit()
--
2.24.0 So every thing before is practically discarded, the bit's after are set, which can be seen in my initial comment. I've looked so many times on the code and just realized it now 😅 So the docs are right, @strom-und-spiele 😀 |
That's reassuring to hear.
So obviously the first block is not reassigning a struct to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So far as I can see, this doesn't cause any regression. I ran a couple examples over on proton-c
.
I tried to setup and use PyCortexMDebug, but I couldn't get it to install on my system.
Nice! If I remeber correctly, i just cloned the PyCortexMDebug Called
and sourced source <path_to>/PyCortexMDebug/cmdebug/svd_gdb.py However, any debugger, which can read SVD files would work fine. I can not This one looks really promising, but I did not test it yet. |
b76b6d2
to
cce32db
Compare
I've updated the gdb example, as the breakpoint has to beset at I reconfirmed that the spi initialization is still working. |
bbf6abf
to
142c7c8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks to your setup instructions I was able to test and reproduce given the steps. Looks great! I rechecked for any regressions as well: everything works as expected.
@Sh3Rm4n feel free to merge this PR as well |
I noticed, that the SPI is not working on my STM Discovery Board.
With some debugging, I first found out, that the SPI is for example not initialized as master.
I used PyCortexMDebug, to see if
the registers are correctly set in combination with the svd file provided by
stm32-rs.
I did provide a minimal example, to reproduce these steps. Ideally, this example
should be extended, to be helpful for newcomers and not just a configuration
example, without using the SPI. Something like connecting MOSI and MISO to echo
the output and do
assert_eq!
or similar.Reproduction Steps:
openocd.cfg
:openocd.gdb
Output:
The fixed version outputs:
Explanation
As we can see, the
mstr
bit is not set.This is because, the register method was split up in multiple writes in
stm32f3xx-hal/src/spi.rs
Line 180 in 4fd3ae6
This was introduced in 96a03cd.
@dfrankland, i can't test if this is introduces a regression for the
stm32f373
, as i only have the discovery board available.https://docs.rs/svd2rust/0.16.1/svd2rust/#write
So, when
w
is split up, every untouched bit is reset. AsMSTR
is set at thebeginning, it will be reset with the following write invocations.