Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
arturkow2000 committed Jul 18, 2023
1 parent d4d7921 commit adba827
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
Empty file added blog/.hugo_build.lock
Empty file.
50 changes: 49 additions & 1 deletion blog/content/post/2023-06-07-twpm-spi-fix.md
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,55 @@ re-programming DMA channel. Now, the test succeeds.
## Extending tests
TODO
I have basic code that can read and write data over SPI, but so far I have
tested only read of zeroed registers. Now, it is time to extends the tests, so
that we write random data of random lengths, then read the data back and check
whether it is as expected.
I started with something simple
```python
tpm_write(0, bytes([1,2,3,4,5,6,7,8]))
x = tpm_read(0, 8)
assert x == [1,2,3,4,5,6,7,8]
```

and failed:

![](/img/stm32-spi-failed-second-transfer.png)

It turned out that I incorrectly cleared `SPI_RXFIFO_THRESHOLD` bit, which
should be set for 8-bit frame length. This was causing RXDMA to not complete
under some circumstances, freezing the application.

Changing

```c
CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
```
to
```c
SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
```

solved the problem, however I got another one.

![](/img/stm32-spi-readback-wrong-data.png)

Wait state is properly inserted and terminated, but payload is not valid. I
split the test into two so that I can do pause the app between write and read
from the register. Peeking at the `scratch_buffer` reveals that DMA went wrong
as first three bytes were completely lost.

![](/img/stm32-scratch-state.png)

What's more, we are once again stuck polling for DMA completion (DMA is still
waiting for remaining three bytes). The issue could possibly be caused by too
high delays between restarting of DMA transfers, so I lowered SPI frequency down
to 100 KHz, but to my surprise, the result was exactly the same. I tested
different data sizes and the result is always the same (3 bytes lost).

## Summary

Expand Down
Binary file added blog/static/img/stm32-scratch-state.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit adba827

Please sign in to comment.