Not possible to select SPI3 DMA RX1 on STM32 F2, F4, F7, H7
- Dominant language
- C
- Stars
- 4k
- Forks
- 1.7k
- Avg merge
- 1d 17h
- Merged PRs (30d)
- 237
Description
### Short story
- There is a check in SPI driver which checks if we defined SPI channels
- `0` in this check means **not defined**, so in case of `0` driver doesn't enable DMA
- SPI3 DMA RX1 is unlucky to be `0`
### Long story
`DMAMAP_SPI3_RX_1` is defined in [arch/arm/src/stm32/hardware/stm32_dma_v2.h#L406](https://github.com/apache/incubator-nuttx/blob/master/arch/arm/src/stm32/hardware/stm32_dma_v2.h#L406) and evaluates to `0`:
```
#define STM32_DMA_MAP(d,s,c) ((d) << 6 | (s) << 3 | (c))
```
```
#define DMAMAP_SPI3_RX_1 STM32_DMA_MAP(DMA1,DMA_STREAM0,DMA_CHAN0)
```
```
#define DMA1 (0)
...
#define DMA_STREAM0 (0)
...
#define DMA_CHAN0 (0)
```
In [arch/arm/src/stm32/stm32_spi.c#L2062](https://github.com/apache/incubator-nuttx/blob/master/arch/arm/src/stm32/stm32_spi.c#L2062), this commit [cfc5b59](https://github.com/apache/incubator-nuttx/commit/cfc5b596364ddbe163009c69309ababe62309c98) to NuttX 7.27 from @dagar (I am not complaining, just sending notification) added a check if DMA channels are defined, before enabling DMA for SPI.
```
if (priv->rxch && priv->txch)
```
Type of `rxch` is `uint8_t` and `0` is used to indicate no channel being assigned.
It is fine, except if we are unlucky, and try to use SPI3 DMA1, Stream 0, Channel 0 `DMAMAP_SPI3_RX_1`, for example:
`board.h`
```
#define DMACHAN_SPI3_RX DMAMAP_SPI3_RX_1 /* DMA1, Stream 0, Channel 0 */
#define DMACHAN_SPI3_TX DMAMAP_SPI3_TX_1 /* DMA1, Stream 5, Channel 0 */
```
Not sure how to fix it nicely. Zero shouldn't be used to detect lack of channel if `0` is valid channel identifier.
Contributor guide
Research direction
Start with arch/arm/src/stm32/stm32_spi.c around line 2062 and trace rxch and txch back to the DMACHAN_SPI3_* definitions in board.h. Compare that check with the DMA map macros in arch/arm/src/stm32/hardware/stm32_dma_v2.h around line 406; done means SPI3 RX1 remains selectable when its valid mapping evaluates to zero, while an unassigned channel is still distinguished.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c
- Domain
- embedded-iot
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100