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.

  • Search through your code, may be you have m_active_conn_handle defined twice, one is global as should be and another is static in one of the files

  • Hi Alex, that's is a possibility, but won't the compiler give me warnings or errors for that? Anyway, I'm doing the search :) will get back later.

  • Hi Alex, I used 2 different utilities, and I can confirm that it is not possible. :)

  • As for error_code variable, it is local and if not used then it may not even be in stack, it may be for a short period in CPU register that's why you may not see it in IDE. Define it as global and find it's address in map file, same for m_active_conn_handle. In that case it will be easier to debug.

  • Let's start with your uneducated guesses, they are just wrong, entirely. There's no read only ram and no wrong address anything. memory is memory, compilers are compilers and things are where they are supposed to be.

    the code you post is entirely bizzare. why are you setting error_code to a pointer to the address of your variable? That's just weird. And what's wrong with m_active_conn_handle being set to zero? That sounds like a perfectly valid and very likely connection handle to me. Why do you think 0x8e4 is the correct value?

    I don't understand your comment about USER_APP_RAM_BASE either,

    You appear to have debugged yourself into a flap. Take a break , start fresh and work out what's wrong.

Related