Intermittent CM4 FAULT! Bus Fault! in floating point app

Makes sense. I had removed heap_4.c.

BTW, I should note that I have newlib version 3.3, so I get this warning when I compile heap_useNewlib_NXP.c:

This wrapper was verified for newlib versions 2.5 - 3.1; please ensure newlib's external requirements for malloc-family are unchanged!

I reread Dave’s instructions, and revised my linker script slightly:

MY_HEAP_SIZE = 0x7000; /* 28 kB */

.heap (NOLOAD):
{
    __HeapBase = .;
    __end__ = .;
    end = __end__;
    KEEP(*(.heap*))
    . = ALIGN(8);
    . = . + MY_HEAP_SIZE;
    . = ALIGN(8);
    __HeapLimit = .;
} > ram

/* (int)&HEAP_SIZE; // that's (&__HeapLimit)-(&__HeapBase) */
HEAP_SIZE = __HeapLimit - __HeapBase;

With configUSE_NEWLIB_REENTRANT 1 I end up at Call Stack:

0 ??() ?????? ?????? 0x584E5F62 (All) 
1 __sfvwrite_r() ?????? ?????? 0x1009C840 (All) 
2 _puts_r() ?????? ?????? 0x1009DD32 (All) 
3 Cy_SysLib_ProcessingFault() main_cm4.c 372 0x10090ACC (All) 
4 Cy_SysLib_FaultHandler(const uint32_t * faultStackAddr = <optimized out>) Generated_Source\PSoC6\pdl\drivers\peripheral\syslib\cy_syslib.c 444 0x100969EE (All) 
5 UsageFault_Handler() gcc/startup_psoc6_01_cm4.S 455 0x1008034E (All) 
6 <signal handler called>() ?????? ?????? 0xFFFFFFE1 (All) 
7 ??() ?????? ?????? 0x584E5F62 (All) 
8 __sflush_r() ?????? ?????? 0x1009C2D8 (All) 
9 ??() ?????? ?????? 0x77AFFA9C (All) 

0x1009C2D8 is in here:

0x1009C2CA E00A     b.n	1009c2e2 <__sflush_r+0xce>
0x1009C2CC 6A6C     ldr	r4, [r5, #24]	; 0x24
0x1009C2CE 69E9     ldr	r1, [r5, #1c]
0x1009C2D0 4633     mov	r3, r6
0x1009C2D2 463A     mov	r2, r7
0x1009C2D4 4640     mov	r0, r8
0x1009C2D6 47A0     blx	r4
0x1009C2D8 1E04     subs	r4, r0, #0
0x1009C2DA EBA60604 sub.w	r6, r6, r4
0x1009C2DE 4427     add	r7, r4
0x1009C2E0 DD17     ble.n	1009c312 <__sflush_r+0xfe>

Registers:

r    0=,0x080304E8,r1=,0x4E657375,r2=,0x100B0188,r3=,0x0000000F,r4=,0x100B00E1,
    r5=,0x584E5F62,r6=,0x100B0188,r7=,0x0000000F,r8=,0x0802FA20,r9=,0x080304E8,
    r10=,0x0802FA34,r11=,0x7FFFFC00,r12=,0xFFFFFFFF,sp=,0x0802F9EC,lr=,0x1009C841,
    pc=,0x584E5F62,xpsr=,0x20070003,msp=,0x0802F9EC,psp=,0x08026F18,

lr=,0x1009C841, is pointing in here:

0x1009C838 D04F     beq.n	1009c8da <__sfvwrite_r+0xe6>
0x1009C83A 69E1     ldr	r1, [r4, #1c]
0x1009C83C 6A65     ldr	r5, [r4, #24]	; 0x24
0x1009C83E 47A8     blx	r5
0x1009C840 2800     cmp	r0, #0
0x1009C842 DD5B     ble.n	1009c8fc <__sfvwrite_r+0x108>
0x1009C844 F8D83008 ldr.w	r3, [r8, #8]
0x1009C848 1A1B     subs	r3, r3, r0
0x1009C84A 4406     add	r6, r0
0x1009C84C 1A3F     subs	r7, r7, r0
0x1009C84E F8C83008 str.w	r3, [r8, #8]
0x1009C852 2B00     cmp	r3, #0
0x1009C854 D1E9     bne.n	1009c82a <__sfvwrite_r+0x36>

Better if you could put a breakpoint in before the fault. Start stepping through printf %f to see where the fault occurs. Once you hit the fault and know where it occurs, then do the whole thing again but capture the stack and context prior to the fault. (I’m assuming the fault occurs the first call to printf %f).

As it is, your fault handler appears to be making calls that potentially cause other faults.

If I comment out the start of UITask:

//        UITaskHandle = xTaskCreateStatic(
//                      UITask,           /* Function that implements the task. */
//                      "UITask",         /* Text name for the task. */
//                      sizeof xStack/sizeof xStack[0],  /* Number of indexes in the xStack array. */
//                      NULL,             /* Parameter passed into the task. */
//                      3,                /* Priority at which the task is created. */
//                      xStack,           /* Array to use as the task's stack. */
//                      &xTaskBuffer );   /* Variable to hold the task's data structure. */        
//        configASSERT(UITaskHandle);

I get

0 vApplicationMallocFailedHook() main_cm4.c 319 0x1008F17C (All) 
1 _sbrk_r(struct _reent * pReent = <optimized out>, int incr = 8192) ..\..\FreeRTOS_helpers\heap_useNewlib_NXP.c 149 0x1008EE70 (All) 
2 _malloc_r() ?????? ?????? 0x100970E8 (All) 
3 FF_CreateIOManger(FF_CreationParameters_t * pxParameters = 0x8031bd8 <xStack.17225+2940>, FF_CreationParameters_t * pxParameters@entry = 0x8031bd8 <xStack.17225+2940>, FF_Error_t * pError = 0x8031bd4 <xStack.17225+2936>, FF_Error_t * pError@entry = 0x8031bd4 <xStack.17225+2936>) ff_ioman.c 147 0x10086078 (All) 
4 disk_init(sd_card_t * pSD = 0x80242d0 <sd_cards+64>) ff_sddisk.c 137 0x10081DC4 (All) 
5 FF_SDDiskInit(const char * pcName = 0x100a9e57 "SysSDCrd0", const char * pcName@entry = 0x100a9e57 "SysSDCrd0") ff_sddisk.c 162 0x10081E4A (All) 
6 mount(FF_Disk_t ** ppxDisk = 0x8024510 <SysSDCrds+8>, FF_Disk_t ** ppxDisk@entry = 0x8024508 <SysSDCrds>, const char * const devName = 0x100a9e57 "SysSDCrd0", const char * const path = 0x100a9e56 "/SysSDCrd0") ff_utils.c 86 0x1008B8FA (All) 
7 SysSDCrd0Task(void * arg = <optimized out>) data_logging.c 1143 0x1009091E (All) 
8 ??() ..\FreeRTOS\FreeRTOS\Source\portable\GCC\ARM_CM4F\port.c 703 0x1008E464 (All) 

where it is trying to malloc 8 * 512 = 4k bytes.

But, again, runs OK with #define configUSE_NEWLIB_REENTRANT 0.

Maybe I am just running out of heap.

EDIT: In cy8c6xx7_cm4_dual.ld I increased

MY_HEAP_SIZE = 0x7000;

to

MY_HEAP_SIZE = 0x8000;

and now it is running with #define configUSE_NEWLIB_REENTRANT 1.

I am new to ld files, but it seems to me I should be able to do something like

__HeapLimit = __StackLimit - 1;

to tell it I want to use all available RAM.

Also, I guess I need to break out Puncover or something and see if I can improve my stack usage situation.

EDIT 2: It died again:

0 ??() ?????? ?????? 0x3830FFF0 (All) 
1 __sflush_r() ?????? ?????? 0x1009C272 (All) 
2 _fflush_r() ?????? ?????? 0x1009C368 (All) 
3 Cy_SysLib_ProcessingFault() main_cm4.c 371 0x10090AC6 (All) 
4 Cy_SysLib_FaultHandler(const uint32_t * faultStackAddr = <optimized out>) Generated_Source\PSoC6\pdl\drivers\peripheral\syslib\cy_syslib.c 444 0x100969EE (All) 
5 UsageFault_Handler() gcc/startup_psoc6_01_cm4.S 455 0x1008034E (All) 
6 <signal handler called>() ?????? ?????? 0xFFFFFFE1 (All) 
7 ??() ?????? ?????? 0x3830FFF0 (All) 
8 __sflush_r() ?????? ?????? 0x1009C272 (All) 
9 ??() ?????? ?????? 0xF7DFE7B6 (All) 

r0=,0x0803D110,r1=,0xD94608BF,r2=,0x00602169,r3=,0x00000000,r4=,0x3830FFF0,r5=,0x100A4CD3,
r6=,0x00000000,r7=,0x20000000,r8=,0x0803D110,r9=,0x0803D110,r10=,0x08046000,r11=,0x0803D464,
r12=,0xFFFFFFFF,sp=,0x0803CFD4,lr=,0x1009C273,pc=,0x3830FFF0,xpsr=,0x400F0003,msp=,0x0803CFD4,
psp=,0x08026F28,

|0x1009C024 D909     bls.n|1009c03a <__udivmoddi4+0x12e>|
|0x1009C026 2D00     cmp|r5, #0|
|0x1009C028 F00080B1 beq.w|1009c18e <__udivmoddi4+0x282>|

3 posts were split to a new topic: Cortex M4 hard fault when using floating point unit

That could work, but you might still want to keep MY_HEAP_SIZE up to date so you’ll get build errors if there’s not enough heap. Either way, your map file tells you how much space is available between __HeapLimit and __StackLimit so you can make sure you’re assigning RAM to these two resources appropriately. Remember the “stack” we’re talking about here is the interrupt stack aka MSP stack.

Ah, good point. I guess I mean “.bss”.

It goes against my superstitions to say so, but it looks like the “Intermittent CM4 FAULT! Bus Fault!” problem that started this thread has been solved by setting

#define configUSE_NEWLIB_REENTRANT 1

in FreeRTOSConfig.h. Dave’s page explains the reasons better than I can.

I want to thank you all for your help; I don’t think I ever would have figured this out on my own. It has also been a great learning experience.

1 Like