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.

  • @alex if the code is built debug then it's guaranteed that after each operation the values are written to the debug area on the stack, so you don't need to play tricks to read them. If the code is built release mode, then all bets are off about what it's doing.

  • @RK:

    they are just wrong, entirely. There's no read only ram

    No there are not, I'm being driven literally nuts on this.

    no wrong address anything.

    I'm not gonna be so confident, there could be a memory leak somewhere and it's usually difficult to find, also I have no idea how reliable nrfgo studio is when downloading my APP and SD.

    compilers are compilers

    You are right, but I was supposed to be on the look out for stack allocations, perhaps in the .s file, but I have no clue how to do that.

  • @RK:

    the code you post is entirely bizzare. why are you setting error_code to a pointer to the address of your variable?

    Oh that... I was supposed to sort that out, it was used to test something, but I pasted it here anyway, I'm on the brink of insanity, I've done everything I am suppsoed to do, yet nothing works out.

    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.

    That's weird because the connection handle, passed from another struct, should be 0x8e5 (sorry about the 0x8e4 value, I remembered wrong)

    I don't understand your comment about USER_APP_RAM_BASE either,

    to call the fucntion sd_ble_enable, I'm suppsoed to pass in the valid starting address of app RAM starting address. I looked it up it should be 0x20007000 or something, but

  • I can't seem to change APP_RAM_BASE to anything, and the current value, acquired by breakpointing at the very line which calls sd_ble_enable, is 0, that's not supposed to happen, yet it can advertise.

    Take a break , start fresh and work out what's wrong.

    I wish I could. I'm pulling my hair out on this one, god why must everything be so hard!

    so you don't need to play tricks to read them.

    I wish that was the case! But all I see was "out of the scope" in the watchx window of keil. I'm at my wit's end. Dammit.

  • A connection handle of 0x8e5, or 0x8e4, makes no sense. Connection handles start at 0 and go up (that's not guaranteed but that's how it actually works in practice with this softdevice).

    So it sounds to me like your actual issues are these

    1. you've convinced yourself that something to do with a connection handle is the cause of your bug and that's caused you to head into a different piece of functional code and start questioning it.

    2. keil sounds like a total POS. Are you actually compiling with debug and full debugging symbols (don't ask me how you do that in keil I don't use it) If you aren't the values you're seeing are probably not correct.

    If all else fails you could always drop down to the assembly and see what's in the registers and that will tell you what's really being passed to where.

    still think the assignment of 0 to the connection handle sounds completely right.

Related