Skip to content

Commit 28815b1

Browse files
author
Jamie Smith
authored
DMA SPI support for STM32 devices (#162)
* Start on STM32 DMA SPI * Update all objects.hs, add interrupt function * Initial DMA code should be ready to test out... * Fix SPI interrupt-mode IRQ handlers, add SPI::transfer_and_wait * Fix CMake error when building for STM32WL processors * Now builds on all STM devices! * Properly support STM32U5 / DMA IP v3 * Start on STM32F4 support, fix hardfault on IP v1 and v3 due to incorrect indexing * Fix Rx-only transfers, add abort code, fix incorrect channel assignments for DMA IP v1 devices * Start on STM32H7 SPI DMA * Fixes for H7: Correctly manage data cache, keep SPI ISR enabled * Implement DMA SPI header constants for all remaining STM32 families. Also add support for freeing DMA channels * Try and fix build on STM32G0 * Fix build on STM32G0 * Add SPI_32BIT_WORDS label, start on fixing SPI docs * SPI: Implement reference counting so that DMA channels get freed properly * Fix issue where SPI data could get corrupted (by TI mode turning on) depending on memory layout (if your spis pointer & 0x10 was nonzero) * Mark DMA channels as unallocated when SPI bus is freed * Simplify spi_abort_asynch() * Fix some rebase issues, fix failing to allocate DMA channel on STM32U5 * Fix DMA getting stuck on STM32F4, F7, and F2
1 parent 7e9d658 commit 28815b1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+2642
-265
lines changed

drivers/source/I2C.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ void I2C::abort_transfer(void)
245245
I2C::Result I2C::transfer_and_wait(int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, rtos::Kernel::Clock::duration_u32 timeout, bool repeated)
246246
{
247247
// Use EventFlags to suspend the thread until the transfer finishes
248-
rtos::EventFlags transferResultFlags("I2C::Result EvFlags");
248+
rtos::EventFlags transferResultFlags("I2C::transfer_and_wait EvFlags");
249249

250250
// Simple callback from the transfer that sets the EventFlags using the I2C result event
251251
event_callback_t transferCallback([&](int event) {

hal/include/hal/spi_api.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ int spi_master_write(spi_t *obj, int value);
261261
* tx_length and rx_length. The bytes written will be padded with the
262262
* value 0xff.
263263
*
264+
* Note: Even if the word size / bits per frame is not 8, \c rx_length and \c tx_length
265+
* still give lengths in bytes of input data, not numbers of words.
266+
*
264267
* @param[in] obj The SPI peripheral to use for sending
265268
* @param[in] tx_buffer Pointer to the byte-array of data to write to the device
266269
* @param[in] tx_length Number of bytes to write, may be zero

targets/TARGET_STM/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ target_sources(mbed-stm
5252
trng_api.c
5353
us_ticker.c
5454
watchdog_api.c
55+
stm_dma_utils.c
5556
)
5657

5758
target_link_libraries(mbed-stm INTERFACE mbed-cmsis-cortex-m)

targets/TARGET_STM/TARGET_STM32F0/objects.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,6 @@ struct pwmout_s {
4444
uint8_t inverted;
4545
};
4646

47-
struct spi_s {
48-
SPI_HandleTypeDef handle;
49-
IRQn_Type spiIRQ;
50-
SPIName spi;
51-
PinName pin_miso;
52-
PinName pin_mosi;
53-
PinName pin_sclk;
54-
PinName pin_ssel;
55-
#if DEVICE_SPI_ASYNCH
56-
uint32_t event;
57-
uint8_t transfer_type;
58-
#endif
59-
};
60-
6147
struct serial_s {
6248
UARTName uart;
6349
int index; // Used by irq

targets/TARGET_STM/TARGET_STM32F0/spi_device.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,7 @@
2121
// Defines the word length capability of the device where Nth bit allows for N window size
2222
#define STM32_SPI_CAPABILITY_WORD_LENGTH (0x0000FFF8)
2323

24+
// We have DMA support
25+
#define STM32_SPI_CAPABILITY_DMA 1
26+
2427
#endif
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2016-2023 STMicroelectronics
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#ifndef MBED_OS_STM_DMA_INFO_H
19+
#define MBED_OS_STM_DMA_INFO_H
20+
21+
#include "cmsis.h"
22+
#include "stm_dma_utils.h"
23+
24+
// See STM32F0 reference manual Table 26.
25+
26+
/// Mapping from SPI index to DMA link info for Tx
27+
static const DMALinkInfo SPITxDMALinks[] = {
28+
{1, 3}, // SPI1 Tx is DMA1 Channel 3
29+
{1, 5}, // SPI2 Tx is DMA1 Channel 5
30+
};
31+
32+
/// Mapping from SPI index to DMA link info for Rx
33+
static const DMALinkInfo SPIRxDMALinks[] = {
34+
{1, 2}, // SPI1 Rx is DMA1 Channel 2
35+
{1, 4}, // SPI2 Rx is DMA1 Channel 4
36+
};
37+
38+
#endif //MBED_OS_STM_DMA_INFO_H

targets/TARGET_STM/TARGET_STM32F1/objects.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,6 @@ struct serial_s {
8989
#endif
9090
};
9191

92-
struct spi_s {
93-
SPI_HandleTypeDef handle;
94-
IRQn_Type spiIRQ;
95-
SPIName spi;
96-
PinName pin_miso;
97-
PinName pin_mosi;
98-
PinName pin_sclk;
99-
PinName pin_ssel;
100-
#if DEVICE_SPI_ASYNCH
101-
uint32_t event;
102-
uint8_t transfer_type;
103-
#endif
104-
};
105-
10692
struct i2c_s {
10793
/* The 1st 2 members I2CName i2c
10894
* and I2C_HandleTypeDef handle should

targets/TARGET_STM/TARGET_STM32F1/spi_device.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,7 @@
3535
// Defines the word length capability of the device where Nth bit allows for N window size
3636
#define STM32_SPI_CAPABILITY_WORD_LENGTH (0x00008080)
3737

38+
// We have DMA support
39+
#define STM32_SPI_CAPABILITY_DMA 1
40+
3841
#endif
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2016-2023 STMicroelectronics
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#ifndef MBED_OS_STM_DMA_INFO_H
19+
#define MBED_OS_STM_DMA_INFO_H
20+
21+
#include "cmsis.h"
22+
#include "stm_dma_utils.h"
23+
24+
// See STM32F1 reference manual Tables 78 and 79.
25+
26+
/// Mapping from SPI index to DMA link info for Tx
27+
static const DMALinkInfo SPITxDMALinks[] = {
28+
{1, 3}, // SPI1 Tx is DMA1 Channel 3
29+
{1, 5}, // SPI2 Tx is DMA1 Channel 5
30+
{2, 2}, // SPI3 Tx is DMA2 Channel 2
31+
};
32+
33+
/// Mapping from SPI index to DMA link info for Rx
34+
static const DMALinkInfo SPIRxDMALinks[] = {
35+
{1, 2}, // SPI1 Rx is DMA1 Channel 2
36+
{1, 4}, // SPI2 Rx is DMA1 Channel 4
37+
{2, 1}, // SPI3 Rx is DMA2 Channel 1
38+
};
39+
40+
#endif //MBED_OS_STM_DMA_INFO_H

targets/TARGET_STM/TARGET_STM32F2/STM32Cube_FW/STM32F2xx_HAL_Driver/stm32f2xx_hal_dma_ex.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,18 @@ HAL_StatusTypeDef HAL_DMAEx_MultiBufferStart_IT(DMA_HandleTypeDef *hdma, uint32_
202202

203203
/* Enable Common interrupts*/
204204
hdma->Instance->CR |= DMA_IT_TC | DMA_IT_TE | DMA_IT_DME;
205-
hdma->Instance->FCR |= DMA_IT_FE;
205+
206+
/* Mbed CE mod: Only enable the FIFO Error interrupt if the FIFO is actually enabled.
207+
* If it's not enabled, then this interrupt can trigger spuriously from memory bus
208+
* stalls that the DMA engine encounters, and this creates random DMA failures.
209+
* Reference forum thread here:
210+
* https://community.st.com/t5/stm32-mcus-products/spi-dma-fifo-error-issue-feifx/td-p/537074
211+
* also: https://community.st.com/t5/stm32-mcus-touch-gfx-and-gui/spi-dma-error-is-occurred-when-the-other-dma-memory-to-memory-is/td-p/191590
212+
*/
213+
if(hdma->Instance->FCR & DMA_SxFCR_DMDIS)
214+
{
215+
hdma->Instance->FCR |= DMA_IT_FE;
216+
}
206217

207218
if((hdma->XferHalfCpltCallback != NULL) || (hdma->XferM1HalfCpltCallback != NULL))
208219
{

0 commit comments

Comments
 (0)