-
Notifications
You must be signed in to change notification settings - Fork 177
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
feature: expose C api cs_pre_trans_delay
in SpiDriver
#266
feature: expose C api cs_pre_trans_delay
in SpiDriver
#266
Conversation
c2d527d
to
f78f342
Compare
f78f342
to
0412e3b
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 for the contribution ❤️
/// Add an aditional Amount of SPI bit-cycles the cs should be activated before the transmission (0-16). | ||
/// This only works on half-duplex transactions. | ||
#[must_use] | ||
pub fn cs_pre_delay_us(mut self, delay_us: u16) -> Self { |
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.
How does it behave when not in half-duplex mode? Do we get a runtime EspError or does it just get eaten silently? If you are not sure could you test out the cases and report how it acts running?
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.
That's a very funny thing in fact. I only use the FullDuplex mode.
I am using the AI-Thinker NodeMcu ESP32-S.
And it does make a difference (small one, but there is). However it only seems to take in account the fact that its different from 0.
When I do not set it or put 0, we have no time gap between CS pull-down and the first clock (and you can see the turquoise MISO channel doesn't change its state):
When I set it to 1/16/1000, I get the same small time gap between the CS pull-down and the first clock (and MISO answers):
This is all using FullDuplex.
They say in the C API driver include that its not supported, but somewhere else if we look closely we see:
In full-duplex mode, only support cs pretrans delay = 1 and without address_bits and command_bits"
So, at least from what I see its that, even though some part of the C doc seems to say that in FullDuplex mode it doesn't work. In reality its more like a on-off switch for a 1us pre-delay in FullDuplex mode, but it does make a big difference for some SPI chips.
Also, as you can see, setting a value higher than 16 (here 1000), doesn't change the behavior compared to 16.
(And sorry for the bad pictures instead of the oscilloscope screen capture, I was lazy to remember the functionality, but I think its still is enough to demonstrate)
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.
Further reading of the doc says:
cs_ena_pretrans
is not compatible with the Command and Address phases of full-duplex transactions.
But on the field itself:
Amount of SPI bit-cycles the cs should be activated before the transmission (0-16). This only works on half-duplex transactions.
Seems like its not as clear as it could be in the doc.
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 for testing it out!
We are not using the Command & Address part currently in the driver. So that bit doesn't matter.
Did you also test the post delay? Docs don't indicate that this isn't only working in Half-Duplex.
What version of esp-idf are you using? We could annotate your finding's that a delay is happening in Full-Duplex at least in your tested esp-idf version, but with a warning that it might not be a "stable" option to do so.
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.
This week will be busy at work for reports etc... I will see if I have time, if not it will be next week!
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.
Its your PR -> your pacing 😄
i have no stress here
src/spi.rs
Outdated
@@ -233,6 +233,10 @@ pub mod config { | |||
pub duplex: Duplex, | |||
pub bit_order: BitOrder, | |||
pub cs_active_high: bool, | |||
///< Amount of SPI bit-cycles the cs should be activated before the transmission (0-16). This only works on half-duplex transactions. |
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.
If you specify more than 16 'bit cycles', how does the api react? does it take 16 than? If not sure can you test it and report back?
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.
It doesn't change anything given my test (0/1/16/1000), at least in FullDuplex Mode. It behaves like if the value was the highest available.
Further testing on other boards and configs could be interesting.
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.
Could you run a short test with half-duplex on your board and just write something and check if the delay is working overall as intended?
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.
I am doing some further testing at home on another oscilloscope and another ESP32 (its still a NodeMcu but instead of having written ESP32S on top its written ESP32-WROOM-32E). It behaves the same as the other one, but for scientific purpose I will redo all the tests.
Just to note that DMA doesn't work on Half-Duplex (I think this is normal and documented) (which I was activating using Fullduplex in my previous tests and no error was shown about it). I will post the results once I finish.
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 for further looking into it. If you want to check out the C driver path. We are using the somewhat stable driver/spi C api, though it itself goes down a bit deeper and is somewhat splitted.
- In C land they have the high level drivers for General Purpose (gpspi) and one for communicating with the internal Flash memory (sdspi). This is what lives in https://github.com/espressif/esp-idf/blob/master/components/driver/spi/include/driver/spi_master.h and neighborhood.
- This drivers are written against an C hal living in https://github.com/espressif/esp-idf/blob/master/components/hal/include/hal/spi_hal.h
- The hal itself acts on a "low level" api that is somewhat simmilar accros all chips. This is the abstraction over the register level. Here we can also find more infos about the delays itself. For example if we look at the codepath for an esp32 can be found here. https://github.com/espressif/esp-idf/blob/master/components/hal/esp32c3/include/hal/spi_ll.h#L795 . There is an interesting command that suggest the delay should not work in Full-Duplex if command and address features are utilized, but we are not using them. So by that comment it should work in Full-Duplex. Though your test seems to suggest that is not really working fully just "partially"?
It seems the compilation on the CI fails for some platforms. Wouldn't it need to change the toolchain to |
yeah its a known compiler issue currently, a fix is in the working. IF the S2 and risc-v are passing for you here its fine CI wise. |
we might revisit the complete delay topic shortly, when rust-embedded/embedded-hal#462 lands . It's currently not fully flashed out but i think the delay would be flexible to not only allow delays between transaction, it would presumably be allowing to add an delay as a first transaction. |
I was gonna say that they are 2 totally different things while only reading their discussion, but reading the actual code changes and the datasheet of device in example, you are right. (Also unrelated to this technical issue, how do you keep track of other projects pull request like this and get a good overview of the progress, status etc... I am a bit curious and even though I often use Open-Source, I didn't get the chance and time to contribute much, I would love some advice) |
We have to see how the Operation pattern itself changes the landscape. Though we always will have some esp specific api, that most likly will be different from the embedded-hal api.
Well everyone and every project is different, so i can only speak how its around here. There is not a magical switch though it comes with hanging around and doing stuff. To be a bit more concrete
|
Thanks a lot for the advice ! |
@Vollbrecht Here is my further testing: My understanding about the SPI pre-transaction timing is that:
|
Ty for all the testing and sorry for the delay! |
@vpochapuis did you have the chance to try out our new driver after the update. Because of the new transaction iter thing we currently can only delay between transfers. For Soft CS drivers this issue is trivial to fix but the other case would still relight on this underlying idf method. |
Dear @Vollbrecht I didn't have time to try it out yet! |
…elay-in-spi-driver
No just make sure that it sill does the things you want it to do, technically the last big API changes on our site should not influenced this PR to much, just to verify it works with the current API, then we can merge it. |
Thanks for telling me this! |
…elay-in-spi-driver
…elay-in-spi-driver
@ivmarkov we can also merge this. @vpochapuis sorry for keeping this dormant so long thanks for keeping it up to date. |
Its ok! thanks a lot for getting back on it! |
I saw the CI didn't pass some checks, is it normal? Or should i do anything about it? |
its fixed already on master, it was just some nightly clippy things |
…elay-in-spi-driver
Oh ok, I synced my branch just now, just in case |
…elay-in-spi-driver
…elay-in-spi-driver
@Vollbrecht Don't you have merge perms in the meantime? |
This is what I need, any update? |
Dear
esp-idf-hal
maintainers,I was trying to use the different Spi Drivers provided by the crate in order to communicate with an industrial Spi chip (AnalogDevices LTC-6813.
When I was doing so in C, i was using the
spi_device_interface_config_t
struct fieldcs_ena_pretrans
in order to successfully communicate with the chip. It seems that without a delay between the CS activation and the 1st clock tick, the slave IC doesnt understand the transaction.I found that that the
SpiDriver
exposed in the crate didn't expose this parameter, resulting in the communication with the chip to fail (less than 1us delay between the CS activation and the first clock tick).However, the SoftwareCs Driver, seemed to use it, but in fact was simulating it using its own Delay function. I tried it, but even without any delay added with the given function, the CS delay is at least 20us which is too long.
Hence, in order to let users take advantage of the
esp-idf
already existing API, I propose to expose 2 parameters:cs_ena_pretrans
andcs_ena_posttrans
with this Merge Request.Please let me know if there is any issue with this approach.
Thank you for your attention.