Skip to content

Commit 63ca7a2

Browse files
author
Krzysztof Blazewicz
committed
stmhal/dma: precalculate register base and bitshift on handle init
Current version of HAL drivers optimize IRQ handler by using precalculated DMA register address and stream bitshift instead of calculating it on every interrupt. Since we skip call to `HAL_DMA_Init` on reused DMA, fields StreamBaseAddress and StreamIndex of DMA handle are not initialized and thus leads to SegFault in `DMA_IRQHandler`. HAL_DMA_Init is a big routine and we do not need to call it on each use of DMA (ex.: series of I2C operations) and DMA_CalcBaseAndBitshift is really small and releasing it increases code size by only 8 bytes.
1 parent 0280b2c commit 63ca7a2

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

stmhal/dma.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,13 @@ void dma_init(DMA_HandleTypeDef *dma, const dma_descr_t *dma_descr, void *data){
434434
HAL_DMA_DeInit(dma);
435435
HAL_DMA_Init(dma);
436436
HAL_NVIC_SetPriority(dma_irqn[dma_id], IRQ_PRI_DMA, IRQ_SUBPRI_DMA);
437+
} else {
438+
// only necessary initialization
439+
#if defined(MCU_SERIES_F4)
440+
// calculate DMA base address and bitshift to be used in IRQ handler
441+
extern uint32_t DMA_CalcBaseAndBitshift(DMA_HandleTypeDef *hdma);
442+
DMA_CalcBaseAndBitshift(dma);
443+
#endif
437444
}
438445

439446
HAL_NVIC_EnableIRQ(dma_irqn[dma_id]);

stmhal/hal/f4/src/stm32f4xx_hal_dma.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ typedef struct
152152
* @{
153153
*/
154154
static void DMA_SetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength);
155-
static uint32_t DMA_CalcBaseAndBitshift(DMA_HandleTypeDef *hdma);
155+
uint32_t DMA_CalcBaseAndBitshift(DMA_HandleTypeDef *hdma);
156156
static HAL_StatusTypeDef DMA_CheckFifoParam(DMA_HandleTypeDef *hdma);
157157

158158
/**
@@ -1188,7 +1188,7 @@ static void DMA_SetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t
11881188
* the configuration information for the specified DMA Stream.
11891189
* @retval Stream base address
11901190
*/
1191-
static uint32_t DMA_CalcBaseAndBitshift(DMA_HandleTypeDef *hdma)
1191+
uint32_t DMA_CalcBaseAndBitshift(DMA_HandleTypeDef *hdma)
11921192
{
11931193
uint32_t stream_number = (((uint32_t)hdma->Instance & 0xFFU) - 16U) / 24U;
11941194

0 commit comments

Comments
 (0)