This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

HELP! I cannot change the value of a variable???

nrf52840, S140.

So for the past 1 or 2 weeks I have been fighting with this problem:

Now it turned out that the connection handle may actually be INVALID, it's 0!

So I debugged the wind out of the whole thing and found that I cannot change the value of the active connection handle"m_active_conn_handle", which I use to pass as the active connection handle to a variety of functions including for example sd_ble_hvx().

Here is the program:

else if (p_ble_evt->header.evt_id == BLE_GAP_EVT_CONNECTED)
    {
		uint32_t* error_code = &m_active_conn_handle;
		m_active_conn_handle = (uint16_t)p_ble_evt->evt.gap_evt.conn_handle;
		//m_active_conn_handle = 0x1234;
	
		
		error_code = sd_ble_gatts_sys_attr_set(p_ble_evt->evt.gatts_evt.conn_handle, NULL, 0, 0);

 
    }

I set a breakpoint at:

m_active_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;

and then pressed F10. Instead of the right value (0x8e4), the m_active_conn_handle got set to 0x0000.

This is really strange? Making an uneducated guess, is this caused by allocating m_active_conn_handle at the wrong address, like some sort of read only RAM region? Or stack region? Or is this caused by RAM_BASE_ADDRESS not configured properly?

Also, a lot of error code won't show unless I play some "tricks", for example:

err_code = sd_ble_gatts_hvx(m_active_conn_handle, &hvx_params);

the err_code will not show when breakpointed, it will simply display "out of scope" in keil's watch window but if I write:

err_code_address = & err_code;

below it, and set the breakpoint there, the err_code_address will NOT display the address of err_code, still "out of scope", but err_code will now properly display if you move your mouse cursor over it!

All of these lead me to believe I have set certain memory address wrong, but then again, that's just my uneducated guess, here is my config in the magic wand:

image description image description

Also, when calling

error_code = sd_ble_enable(&ble_enable,&USER_APP_RAM_BASE);

I'm using USER_APP_RAM_BASE = 0, which I know is a really, really bad idea but I can't modify its value to anything other than that either, for the same reason I can't modify the active connection handle's value.

Can someone please tell me what the whirlwind is going on??? Please help! Many thanks!

edit @endnode can you help me??

Edit: Quick answer: turn off optimization, and the problems "unable to change value, values not properly being displayed etc. are all gone.

  • @RK thank you! I'll look up the assembly and see what I can find. I hate going there... I can't remember all those instructions. Will get back with something new, meanwhile anyone reading this please help!

  • @RK I tried to decrypt the assembly but got stopped at the first instruction: LDRH, here is the screen cap, the pertinent first line is highlighted using bring yellow stripe: screen cap

    My even less educated guess:

    load halfword length of data from address stored in r0 + 0x04 into r0;

    I don't think that makes a lot of sense, but anyway, what does that mean exactly? My google-fu isn't high enough to get the exact meaning of ldrh, I managed to find ldr under a different using case.

    Also, does the rest of the code look out of the normal to you?

  • @Mitch996, to be honest I don't understand why you are wasting time investigating handle. You were told that zero is valid value. Invalid one is:

    #define BLE_CONN_HANDLE_INVALID 0xFFFF
    

    In your another thread about sd_ble_gatts_hvx, did you check if notification enabled?

  • assembly looks fine to me, gets the 16bit value offset at +4 which is the connection handle, stores it into your variable and calls the service call with that value in r0 and r1, r2 and r3 zeroed. My guess would be if you single stepped one line from where you are on that picture, r0 would be zero, which is the connection handle.

    But as I've said already - that looks like optimized assembly to me. Code compiled optimized really doesn't work very well in debuggers, even if it has debug information (which that does). That is probably why you're having issues stepping it. Unless Keil compiles code in a very unusual way.

    Are you sure you are compiling the code not only with debug information but with zero optimisation?

  • @RK Hello, I enabled the "DEBUG" macro but regrettably I'm using O3 optimization. Guess I need to turn it off...

Related