cancel
Showing results for 
Search instead for 
Did you mean: 

Stm32F10X+FatFS+SDIO DMA HardFault

bronek
Associate II
Posted on December 15, 2015 at 17:20

I have problem with STM32f103 FatFS library.

If I start it with

SD_SetDeviceMode(SD_POLLING_MODE)

everything works OK. But when I change it to

SD_SetDeviceMode(SD_DMA_MODE)

it is fired HardFaultHandler.

Thread #1 <
main
> (Suspended : Breakpoint) 
HardFault_Handler() at exception_handlers.c:328 0x800011c 
<
signal
handler called>() at 0xfffffff1 
0x0 
<
signal
handler called>() at 0xfffffff9 
SD_ReadBlock() at sdcard.c:916 0x800344c 
disk_read() at diskio.c:95 0x80003ec 
move_window() at ff.c:745 0x80004f6 
check_fs() at ff.c:2 184 0x800051a 
find_volume() at ff.c:2 258 0x800060a 
f_mount() at ff.c:2 441 0x8000984 
<...more frames...> 
JLinkGDBServerCL.exe 
[HardFault]
Stack frame:
R0 = 10002000
R1 = 00000001
R2 = 00002A80
R3 = 10001FFF
R12 = 0000000D
LR = FFFFFFF9
PC = 00000000
PSR = 20000041
FSR/FAR:
CFSR = 00020000
HFSR = 40000000
DFSR = 00000001
AFSR = 00000000
Misc
LR/EXC_RETURN= FFFFFFF1

Problem is fired when Reading FatFS->win. It is 512bytes buffer. I align it by

BYTE __attribute__ ((aligned)) win [512];

sdio dma reading initialization is:

static void DMA_RxConfiguration(uint32_t *BufferDST, uint32_t BufferSize)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_ClearFlag(DMA2_FLAG_TC4 | DMA2_FLAG_TE4 | DMA2_FLAG_HT4 | DMA2_FLAG_GL4);
/* DMA2 Channel4 disable */
DMA_Cmd(DMA2_Channel4, DISABLE);
/* DMA2 Channel4 Config */
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SDIO_FIFO_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)BufferDST;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = BufferSize / 4;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA2_Channel4, &DMA_InitStructure);
/* DMA2 Channel4 enable */
DMA_Cmd(DMA2_Channel4, ENABLE);
}

#stm32-fatfs-sdio-dma-hardfault
3 REPLIES 3
Posted on December 15, 2015 at 17:43

Make sure the stack is sufficiently large, and that if you DMA into the stack (local/auto variables) that the scope is valid over the entire DMA transaction.

I'd guess your DMA is trashing the stack, or other parts of the memory arena. You might want to fill unused memory, or guard regions around the DMA area, and confirm it's not exceeding its bounds.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
bronek
Associate II
Posted on December 16, 2015 at 08:39

Stack is laaaaarge

__Main_Stack_Size = 4096;

Problematic is first DMA read, data buffer is 512B, so it can't trash the stack. It is ''empty'' project, only hardware initialization and SD card mounting

NVIC_Configuration();
Status = SD_Init();
Status = SD_GetCardInfo(&SDCardInfo);
if (Status == SD_OK) // ----------------- Select Card --------------------------------
Status = SD_SelectDeselect((u32) (SDCardInfo.RCA << 16));
if (Status == SD_OK) // Set Device Transfer Mode to DMA
Status = SD_SetDeviceMode(SD_DMA_MODE); // SD_POLLING_MODE SD_DMA_MODE SD_INTERRUPT_MODE
fr = f_mount(&FatFs, ''0:'', 1); 

When main() function started, stack pointer is 0x20009fe8 Before HardFault is stack 0x20009cd8 In HardFault handler stack is 0x20009c98 I think stack is OK there are registers on HardFault_Handler (before HardFault_Handler_C)

General Registers General Purpose and FPU Register Group 
r0 268496896 
r1 512 
r2 0 
r3 1110443404 
r4 42 
r5 512 
r6 536874840 
r7 536874840 
r8 512 
r9 9 
r10 3372728174 
r11 3342186858 
r12 13 
sp 0x20009c98 
lr 4294967281 
pc 0x800011c <
HardFault_Handler
> 
xpsr 1627389955 
MSP 536911000 
PSP 626494284 
PRIMASK 0 
BASEPRI 0 
FAULTMASK 0 
CONTROL 0 

stm32forum
Associate II
Posted on December 17, 2015 at 23:14

__process_stack_size big enough?