-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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: Add SPI device support for Apollo3 SoCs #77384
base: main
Are you sure you want to change the base?
drivers: spi: Add SPI device support for Apollo3 SoCs #77384
Conversation
a4d6135
to
792578b
Compare
The following west manifest projects have been modified in this Pull Request:
Note: This message is automatically posted and updated by the Manifest GitHub Action. |
d6b30c0
to
7452a47
Compare
2737c1c
to
d340fc3
Compare
d0bf482
to
daec21e
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.
I would change SPIM to SPIC and SPIS to SPIT if we have time.
Otherwise, the code works and looks great!
@tbursztyka could you please take another look at this PR? |
drivers/spi/spi_ambiq_spis.c
Outdated
int ret = 0; | ||
uint32_t chunk, num_written, num_read = 0; | ||
|
||
while (1) { |
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.
The logic in this loop does not seem right.
A SPI transaction is not "First I send all, then I read all". If you send 1mb, but the controller has an rx internal buffer of 1kb... you'll get into trouble. I don't know what this LRAM is made of, but I doubt it's limitless.
Also, is calling am_hal_ios_fifo_write() with a NULL as a 2nd parameter going to work? Because it can be NULL. spi_buf->len can be > 0 with spi_buf->tx_buf being NULL. Meaning it's up to the driver (or HAL if it's clever enough) to send dummy bytes instead. And it can be the same in rx so the memcpy below is definitely going to blow up.
Have you tried running tests/drivers/spi/spi_loopback ?
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.
Thank you for the comments, let me briefly introduce how our SPIS works.
Ambiq's SPIS (we called it IOS which is I/O Slave) is very specific designed, basically it contains 1KBytes share memory (Local RAM) and the SPI interface, it cannot "send" data out on its own, all operations have to be initiated by the host, so if the slave need to "send" 1mb to the host, we first trigger a GPIO interrupt to the host and then fill as much data to the FIFO, and once the host received the GPIO intrerrupt, it will first read the FIFO counter register FIFOCTR of the slave to know how much data it need to read at that time, after it read the data, the slave will start another iteration to "send" the rest data. In short there must be some kind of handshake before any data transfer.
And Apollo3 SPIS hardware does not support dummy bytes send, that's why I added spi_context_tx_buf_on and spi_context_rx_buf_on check there, NULL buffers should not be passed to HAL.
As our SPIS is actually totally different design compared with SPIM, I don't think it can run the spi_loopback test, we tested the driver with our local testcases.
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 missed the fact it is a spiS controller, nevermind. (as well as I mixed up tx/rx_buf_on with tx/rx_on -_-' )
spi_controller_peripheral should be the test to use. (if you have 2 SPI controllers - once acting as the host, the other one as the peripheral - on one board it's quite trivial to set up)
You can't avoid managing the dummy bytes in the driver: if the tx_buf is NULL but tx_len is > 0, it means it should tx dummy bytes of tx_len amount. Or else user code won't be easily portable on your driver. You could allocate a small static array of dummy bytes, and you loop on writing these relevantly to the necessary amount (if the dummy bytes array has a length of 16, if the user needs to tx 17 dummy bytes: it loops twice, once with 16, the next time with 1. For instance.)
Same on rx side, don't you need to "consume" all bytes that have been sent to you? Even the user does not provide any buffer for it I mean. (I does not have to be dummy bytes, it can be any bytes)
the spi buf set is of scatter/gather type, so it can be a complicated setup with various tx/rx, sometimes with no actual buffer but just a length there. That's why I find it weird that you do not loop until it has went through all the set (basically the goto end; in the rx block).
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 added dummy bytes read/write support, please help to review again, thank you!
Indeed, this is in fact a rule: |
So it should be controller/peripheral. Thanks! |
d4a63e5
daec21e
to
d4a63e5
Compare
This commit adds spi device support for Apollo3 SoCs. Signed-off-by: Hao Luo <[email protected]>
d4a63e5
to
a9e0bda
Compare
I changed to use controller and device. |
|
||
compatible: "ambiq,spid" | ||
|
||
include: [spi-controller.yaml, pinctrl-device.yaml, ambiq-pwrcfg.yaml] |
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.
Should the ambiq,spid.yaml have the spi-controller.yaml or the spi-device.yaml?
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 created this by reference to what nrf-spis.yaml includes; and changing to spi-device.yaml will cause lots of builld errors, I guess the spi-device.yaml is used for the external device connected to the soc, but not for the soc itself works in device mode.
This commit adds spi device support for Apollo3 SoCs.