Hi all
I am facing a really anoying hardfault when calling
cJSON_CreateObject(...)
from the cJSON lib while LWIP TCP server is up and running. If LWIP is not polled, the json creation code works just fine. My hardware ist LPC54018 (baremetal).
Thanks for any help...
Ron
LPC54018_Project.axf [LPC54018 (cortex-m4)] Thread #1 1 (Suspended : Signal : SIGSTOP:Stopped (signal)) HardFault_Handler() at semihost_hardfault.c:61 0x10016c60 <signal handler called>() at 0xffffffe9 ENET_SetMacControl() at fsl_enet.c:397 0x1001010a global_hooks() at 0x2 Active faults Bus Fault (BFSR) IMPRECISERR (2) Imprecise data bus error Hard Fault (HFSR) FORCED (30) Indicates a forced hard fault, generated by escalation of a fault with configurable priority that cannot be handled, either because of priority or because it is disabled Fault Status Registers IPSR 0x00000003 Exception Status Register (Hard Fault) CFSR 0x00000400 Configurable fault Status Register MMSR 0x00000000 Memory Manage fault Status Register BFSR 0x00000004 Bus fault Status Register UFSR 0x00000000 User fault Status Register HFSR 0x40000000 Hard fault Status Register DFSR 0x00000000 Debug fault Status Register MMAR 0xE000EDF8 Memory Manage fault Address Register BFAR 0xE000EDF8 Bus fault Address Register AFSR 0x00000000 Auxiliary fault Status Register Stacked Registers (MSP LR/EXC_RETURN=0xffffffe9) R0 0x00000028 R1 0x00020FDE R2 0x10010101 R3 0x00000011 R12 0x00000011 LR 0x10016073 cJSON_New_Item() @ \LPC54018_Project\source\cjson\cJSON.c line 205 PC 0x1001010A ENET_SetMacControl() @ \LPC54018_Project\lwip\port\fsl_enet.c line 397 PSR 0x01000000 MSP 0x2FEE0
Solved! Go to Solution.
Hi
After some more research I discovered that somehow the global hook function pointer for malloc inside the cJSON lib got overwritten. I do not know why, yet. Calling the cJSON_init(NULL) right before calling CJSON_CreateObject() fixes the problem for now.
BR
Ron
Hi
After some more research I discovered that somehow the global hook function pointer for malloc inside the cJSON lib got overwritten. I do not know why, yet. Calling the cJSON_init(NULL) right before calling CJSON_CreateObject() fixes the problem for now.
BR
Ron
Hi,
I suggest you call the ENET_Init() in fsl_enet.c firstly, which call the ENET_SetMacControl().
In other words, before you write the ethernet module register, you have to enable the ethernet module clock with the line CLOCK_EnableClock(s_enetClock[instance]);
Otherwise, the hardfault error will be triggered.
Hope it can help you
BR
XiangJun Rong
void ENET_Init(ENET_Type *base, const enet_config_t *config, uint8_t *macAddr, uint32_t refclkSrc_Hz)
{
assert(config);
uint32_t instance = ENET_GetInstance(base);
#if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
/* Ungate ENET clock. */
CLOCK_EnableClock(s_enetClock[instance]);
#endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
/* System configure fistly. */
ENET_SetSYSControl(config->miiMode);
/* Initializes the ENET DMA with basic function. */
ENET_SetDMAControl(base, config);
/* Initializes the ENET MTL with basic function. */
ENET_SetMTL(base, config);
/* Initializes the ENET MAC with basic function. */
ENET_SetMacControl(base, config, macAddr);
#ifdef ENET_PTP1588FEATURE_REQUIRED
ENET_SetPtp1588(base, config, refclkSrc_Hz);
#endif /* ENET_PTP1588FEATURE_REQUIRED */
}
Hi
Thanks for your answer but my LWIP implementation works just fine. It comes to the hardfault when i am trying to create a cJson object while LWIP is being polled.
I attached a minimized code snippet.
The first call of cJSON_CreateObject(...) work fine but not the 2nd inside the while.
I really hope, someone can help me here...
Thanks in advance!
int main(void) {
/* Init board hardware. */
BOARD_InitBootPins();
BOARD_InitBootClocks();
BOARD_InitBootPeripherals();
systick_init();
//init LWIP
mdioHandle.resource.csrClock_Hz = EXAMPLE_CLOCK_FREQ;
/* Network interface variables */
ip4_addr_t ipaddr, netmask, gw;
/* Set network address variables */
IP4_ADDR(&gw, 192,168,80,1);
IP4_ADDR(&ipaddr, 192,168,80,80);
IP4_ADDR(&netmask, 255,255,255,0);
/* The lwIP single-threaded core: initialize the network stack */
lwip_init();
ethernetif_config_t enet_config = {
.phyHandle = &phyHandle,
.macAddress = configMAC_ADDR,
.non_dma_memory = non_dma_memory,
};
netif_add(&netif, &ipaddr, &netmask, &gw, &enet_config, ethernetif0_init, ethernet_input);
netif_set_default(&netif);
netif_set_up(&netif);
ping_init();
//this call works just fine!
cJSON *json = cJSON_CreateObject();
cJSON_Delete(json);
struct timer t;
timer_set(&t, 2000);
while(1) {
/* Poll the driver, get any outstanding frames */
ethernetif_input(&netif);
if(timer_expired(&t))
{
//this call will end in a hardfault
cJSON *json = cJSON_CreateObject();
cJSON_Delete(json);
timer_restart(&t);
}
...
}
return 0 ;
}