-
Notifications
You must be signed in to change notification settings - Fork 2k
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
drivers/spi: updated SPI driver interface #3216
Conversation
@@ -11,13 +11,9 @@ | |||
* @ingroup driver_periph | |||
* @brief Low-level SPI peripheral driver |
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.
... interface
c535956
to
7b94d6b
Compare
changed interface slightly, so that |
@@ -1263,13 +1237,13 @@ int spi_transfer_regs(spi_t dev, uint8_t reg, char *out, char *in, unsigned int | |||
/* Default: send idle data */ | |||
byte_out = (uint8_t)SPI_IDLE_DATA; | |||
|
|||
for (i = 0; i < (int)length; i++) { | |||
for (i = 0; i < (int)len; i++) { |
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.
you can remove the (int)
cast since both len
and i
are size_t
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.
true, will fix.
I like this improvement. Could we somehow prepare the SPI interface for hardware CS support too while we are doing API changes? I don't really know what the best API model would be here, the CS parameter must be somehow conveyed to the SPI driver, either through a function argument or through some global variable (e.g. a driver struct or similar) |
Lets take it step by step I would say. This improvement is basically the base for further work (as not it is possible, to define a 'whatever-you-like-struct' as SPI device identifier on a per-board basis. With the CS I see a different difficulty: this is how we handle CS inside device drivers. I would hate to see constructs involving ifdefs on weather to call gpio_set/clear manually or not... |
Hm, wait: I might have an idea: We could introduce another parameter for the transfer functions: void spi_transfer_bytes(spi_t dev, gpio_t cs, ...); Now the spi driver can deicide itself, if it just ignores the given parameter (and uses internal HW CS lines instead). Even both is possible, as you could implement your CPUs spi driver in a way, that it will use some HW CS pin in case the parameter 'GPIO_UNDEF' is given, and use SW CS. Using an additional parameter has the advantage, that the SPI driver does not need to keep state. Because keeping say an instance struct for each device that is run on a SPI bus, it would lead to redundant data that can be saved this way. What do you say? |
I completely agree, which is why I would like to open the discussion on how to handle CS lines within the device drivers.
This is similar to what we have been using in Contiki for a while. You need to add one more parameter though, |
If we update the SPI API this way it will also be possible to implement DMA transfers for SPI transfers on platforms where that is supported (e.g. Kinetis) |
About the parameter For DMA we wouldn't have to change anything, this is already perfectly feasible with todays API (just nobody found the time to implement it for any platform, yet). But if this kind of change would make it easier - even better! |
I think I had one idea more: How about we define a new type for the additional spi transfer function parameter, maybe struct {
...
spi_cs_t cs;
...
} dev_t; which you can initialize by either dev_init(..., (spi_cs_t)GPIO(x,y), ...); or dev_init(..., SPI_CS_HW(x), ...); What do you think? |
@haukepetersen I don't see a way around the Your idea for the CS parameter and using special values for HW CS is fine by me, that gives us the flexibility to use both GPIO software CS, and HW CS. But it still requires a continue parameter to tell the SPI bus driver whether to de-assert the CS pin after the transaction or not. |
@gebart is there a special sensor you are thinking about when proposing the |
it is possible to find many more examples if you look for it, both in implemented drivers and future device drivers. edit: e.g. https://github.com/RIOT-OS/RIOT/blob/master/drivers/nvram_spi/nvram-spi.c#L194 |
@gebart thanks! It's been a while writing such a driver. My first idea was to use the transfer_regs function instead of calling transfer_re and transfer_bytes. Anyway, this could lead to ugly results (if that's at least possible). Guess I have to think about it a bit. |
@PeterKietzmann Some devices reset their internal communication state (e.g an address pointer to an SRAM location), when the CS is de-asserted, so it will have to be handled in a single @PeterKietzmann Do you agree that a continue parameter is a good addition to the API after seeing these examples? |
I agree that it is a good parameter when introducing the HW CS capability. Also I see that HW CS seems to be needed as this topic occurs periodically. I just don't like the idea of having this parameter in the API but using the external CS line without the need of it. That's why I have to think about it a while or become friends with that idea :-) |
Read Hauke's reply above on moving the gpio pin to a function argument of
|
Ok, that would force us to use a |
Yes
|
rebased, though the PR needs some love... |
did some more changes (everything pretty much WIP):
|
needs rebase |
not only that :-) Can't promise when I get to it, hopefully soon! |
Probably not before Monday, right? |
definitely not... |
How about Tuesday ;-)? |
Some Tuesday. It is still on my list, but it seems there is more important stuff coming up all the time... |
any news on this PR? |
still trying to rework the concept for shared buses. I think I found a suitable solution, but need to investigate some more first. Expect to have something for the next release at the latest... |
should we close this in favour of #4780? |
Makes sense. I left this open for some readon, but I can't remember it anymore, so I can't have been important :-) |
The main idea behind this API change is to adapt it to a slightly changed paradigm regarding error handling: The concept is to throw errors on initialization, but to not check all parameters everytime afterwards anymore. This leads to more compact and efficient code while not taking much away in terms of error checking.
On top of this, most implementations are implemented in a way, that there return values were quite useless anyway...
This PR is just the first step in 'modernizing' the existing SPI implementations...
NOTE: This needs some testing on all affected platforms once we agree on the changes...