This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Hard Fault in custom file for nRF9160

I'm trying to use some code from the sample files and given libraries to create my own custom program. Upon startup, this is what I get:

***** Booting Zephyr OS v1.14.99-ncs1 *****
Flash r
egion           Domain          Permissions
00 0x00000 0x08000      Secure          rwxl
01 0x08000 0x10000      Non-Secure      rwxl
02 0x10000 0x18000      Non-Secure      rwxl
03 0x18000 0x20000      Non-Secure      rwxl
04 0x20000 0x28000      Non-Secure      rwxl
05 0x28000 0x30000      Non-Secure      rwxl
06 0x30000 0x38000      Non-Secure      rwxl
07 0x38000 0x40000      N
on-Secure       rwxl
08 0x40000 0x48000      Non-Secure      rwxl
09 0x48000 0x50000      Non-Secure      rwxl
10 0x50000 0x58000      Non-Secure      rwxl
11 0x58000 0x60000      Non-Secure      rwxl
12 0x60000 0x68000      Non-Secure      rwxl
13 0x68000 0x70000      Non-Secure      rwxl
14 0x70000 0x78000      Non-Secure      rwxl
15 0x78000 0x80000      Non-Secure      rwxl
16 0x80000 0x88000      Non-Secure      rwxl
17 0x88000 0x90000      Non-Secure      rwxl
18 0x90000 0x98000      Non-Secure      rwxl
19 0x98000 0xa0000      Non-Secure      rwxl
20 0xa0000 0xa8000      Non-Secure      rwxl
21 0xa8000 0xb0000      Non-Secure      rwxl
22 0xb0000 0xb8000      Non-Secure      rwxl
23 0xb8000 0xc0000      Non-Secure      rwxl
24 0xc0000 0xc8000      Non-Secure      rwxl
25 0xc8000 0xd0000      Non-Secure      rwxl
26 0xd0000 0xd8000      Non-Secure      rwxl
27 0xd8000 0xe0000      Non-Secure      rwxl
28 0xe0000 0xe8000      Non-Secure      rwxl
29 0xe8000 0xf0000      Non-Secure      rwxl
30 0xf0000 0xf8000      Non-Secure      rwxl
31 0xf8000 0x100000     Non-Secure      rwxl

SRAM region             Domain          Permissions
00 0x00000 0x02000      Secure          rwxl
01 0x02000 0x04000      Secure          rwxl
02 0x04000 0x06000      Secure          rwxl
03 0x06000 0x08000      Secure          rwxl
04 0x08000 0x0a000      Secure          rwxl
05 0x0a000 0x0c000      Secure          rwxl
06 0x0c000 0x0e000      Secure          rwxl
07 0x0e000 0x10000      Secure          rwxl
08 0x10000 0x12000      Non-Secure      rwxl
09 0x12000 0x14000      Non-Secure      rwxl
10 0x14000 0x16000      Non-Secure      rwxl
11 0x16000 0x18000      Non-Secure      rwxl
12 0x18000 0x1a000      Non-Secure      rwxl
13 0x1a000 0x1c000      Non-Secure      rwxl
14 0x1c000 0x1e000      Non-Secure      rwxl
15 0x1e000 0x20000      Non-Secure      rwxl
16 0x20000 0x22000      Non-Secure      rwxl
17 0x22000 0x24000      Non-Secure      rwxl
18 0x24000 0x26000      Non-Secure      rwxl
19 0x26000 0x28000      Non-Secure      rwxl
20 0x28000 0x2a000      Non-Secure      rwxl
21 0x2a000 0x2c000      Non-Secure      rwxl
22 0x2c000 0x2e000      Non-Secure      rwxl
23 0x2e000 0x30000      Non-Secure      rwxl
24 0x30000 0x32000      Non-Secure      rwxl
25 0x32000 0x34000      Non-Secure      rwxl
26 0x34000 0x36000      Non-Secure      rwxl
27 0x36000 0x38000      Non-Secure      rwxl
28 0x38000 0x3a000      Non-Secure      rwxl
29 0x3a000 0x3c000      Non-Secure      rwxl
30 0x3c000 0x3e000      Non-Secure      rwxl
31 0x3e000 0x40000      Non-Secure      rwxl

Peripheral              Domain          Status
00 NRF_P0               Non-Secure      OK
01 NRF_CLOCK            Non-Secure      OK
02 NRF_RTC1             Non-Secure      OK
03 NRF_NVMC             Non-Secure      OK
04 NRF_UARTE1           Non-Secure      OK
05 NRF_UARTE2           Secure          SKIP
06 NRF_IPC              Non-Secure      OK
07 NRF_VMC              Non-Secure      OK
08 NRF_FPU              Non-Secure      OK
09 NRF_EGU1             Non-Secure      OK
10 NRF_EGU2             Non-Secure      OK
11 NRF_TWIM2            Non-Secure      OK
12 NRF_SPIM3            Non-Secure      OK
13 NRF_TIMER0           Non-Secure      OK
14 NRF_TIMER1           Non-Secure      OK
15 NRF_TIMER2           Non-Secure      OK
16 NRF_SAADC            Non-Secure      OK
17 NRF_GPIOTE1          Non-Secure      OK

SPM: NS image at 0x8000
SPM: NS MSP at 0x200236e0
SPM: NS reset vector at 0xf96d
SPM: prepare to jump to Non-Secure image.

***** Booting Zephyr OS v1.14.99-ncs1 *****
Initializing GPS
Socket created
GPS Initialized
Exception occurred in Secure State
***** HARD FAULT *****

 Fault escalation (see below)
***** BUS FAULT *****
  Precise data bus error
  BFAR Address: 0x50008120
***** Hardware exception *****
Current thread ID = 0x20020298
Faulting instruction address = 0x19184
Fatal fault in ISR! Spinning...

Here is the only function called by main():

void test_gps(){

    nrf_gnss_data_frame_t gps_data;
    GPS_Init(&gps_data); //issue is here

//    GPS_Enable(&gps_data);
//    nvs_setup();

    while(1){
//        GPS_State_Machine(&gps_data, fs);
    }

}

Here is GPS_Init:

void GPS_Init(nrf_gnss_data_frame_t* gps_data){
    
    printk("Initializing GPS");
    do_uart_output = true;
    loc_id = 1;
    gps_update_rate = 1000;

    int rv = init_app();
    if(rv) printk("error");

    t_last_location_recorded = k_uptime_get();
    GPS_Update_Rate(gps_update_rate);
    printk("GPS Initialized");
}

and here is init_app:

int init_app(void)
{
	u16_t fix_retry     = 0;
	u16_t fix_interval  = 1;
	u16_t nmea_mask     = NRF_CONFIG_NMEA_GSV_MASK |
			      NRF_CONFIG_NMEA_GSA_MASK |
			      NRF_CONFIG_NMEA_GLL_MASK |
			      NRF_CONFIG_NMEA_GGA_MASK |
			      NRF_CONFIG_NMEA_RMC_MASK;
	int   retval;

	if (enable_gps() != 0) {
		printk("Failed to enable GPS\n");
		return -1;
	}

	gps_socket = nrf_socket(NRF_AF_LOCAL, NRF_SOCK_DGRAM, NRF_PROTO_GNSS);

	if (gps_socket >= 0) {
		printk("Socket created\n");
	} else {
		printk("Could not init socket (err: %d)\n", gps_socket);
		return -1;
	}

	retval = nrf_setsockopt(gps_socket,
				NRF_SOL_GNSS,
				NRF_SO_GNSS_FIX_RETRY,
				&fix_retry,
				sizeof(uint16_t));

	if (retval != 0) {
		printk("Failed to set fix retry value\n");
		return -1;
	}

	retval = nrf_setsockopt(gps_socket,
				NRF_SOL_GNSS,
				NRF_SO_GNSS_FIX_INTERVAL,
				&fix_interval,
				sizeof(uint16_t));

	if (retval != 0) {
		printk("Failed to set fix interval value\n");
		return -1;
	}

	retval = nrf_setsockopt(gps_socket,
				NRF_SOL_GNSS,
				NRF_SO_GNSS_NMEA_MASK,
				&nmea_mask,
				sizeof(uint16_t));

	if (retval != 0) {
		printk("Failed to set nmea mask\n");
		return -1;
	}


//        delay(5000);


	retval = nrf_setsockopt(gps_socket,     // This call causes problems
				NRF_SOL_GNSS,
				NRF_SO_GNSS_START,
				NULL,
				0);

	if (retval != 0) {
		printk("Failed to start GPS\n");
		return -1;
	}

	return 0;
}

About 2 seconds after "GPS Initialized" is printed, the error pops up.

I also noticed when commenting out this line:

	retval = nrf_setsockopt(gps_socket,     // This call yields problems
				NRF_SOL_GNSS,
				NRF_SO_GNSS_START,
				NULL,
				0);

	if (retval != 0) {
		printk("Failed to start GPS\n");
		return -1;
	}

the error does not occur.

Everything builds without errors. I've tried building the project & spm together with the merged.hex and building them separately with .elf files, and both ways yield the Hard Fault. What are the steps to resolve this issue?

Thanks

  • Have you based this setup on the gps example located here: ncs_0_4_0\nrf\samples\nrf9160\gps? I noticed that your nrf_setsockopt() call is slightly different to the reference code:

    	retval = nrf_setsockopt(fd,
    				NRF_SOL_GNSS,
    				NRF_SO_GNSS_FIX_RETRY,
    				&fix_retry,
    				sizeof(uint16_t));

    Not sure if this is the reason for the error, but it may be worth a try.

  • Hi Bjorn,

    This is based on that sample code, yes. I believe what you have here is lines 26-30 in my init_app. In my code, I have renamed fd to gps_socket. The lines

    	retval = nrf_setsockopt(gps_socket,     // This call yields problems
    				NRF_SOL_GNSS,
    				NRF_SO_GNSS_START,
    				NULL,
    				0);

    refer to the last call to nrf_setsockopt in init_app.

  • I've figured out the issue and have fixed the hard fault.  However, I'm not entirely sure why it's happening.  I've traced it all the way back to the process_gps_data() function from the gps sample code and am able to recreate the problem in the gps sample.  

    Looking at the gps sample:

    k_sleep() is called within the primary while() loop at the very bottom of main().  In the sample code it's set to 500ms, if you increase it beyond 1000ms a hard fault is thrown.  Why is this?  I've tried commenting everything out and it seems that process_gps_data() must be called every 1000ms or less to avoid a fault.  I've tried changing the fix_interval variable in the init_app() from 1 to 2 but it does not seem to have any effect. Any ideas why?

  • I have noticed this issue too when attempting to manipulate the scanning interval from 0.5s to 1s. Seems to be very little that can be done to change this scan frequency in main.c without hard faulting. I have seen in a previous dev zone post that CONFIG_GPS_SIM_TRIGGER_TIMER_MSEC was used in the asset tracker application to set an event but that was for that sample using a simulated GPS measurement. devzone.nordicsemi.com/.../192445

    We could really do with more documentation on the GPS socket, I was surprised when I checked the socket ID of the nrf_socket command and saw it was socket ID 1232491587 Sweat smile

    I believe it is causing a hard fault in my own custom code that uses an on-board switch to cycle between GPS and NB-IoT. Sometimes the code works for a while, it eventually hard faults only when switching to GPS after the first scanning line appears. I believe it is due to this issue of when the switch was thrown in relation to this behind the scenes scan refresh rate. As for the interval timer, I haven't tested it's manipulation but believe this is for time between successful fixes, not scan rate frequency.

  • Any update on why process_gps_data() must be called every 1000ms or a hard fault occurs?

Related