[SOLVED] Code cannot be executed correctly

This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

  • [SOLVED] Code cannot be executed correctly

    Hi,

    I have a variable named g_ullSystemTick, define like this:

    Source Code

    1. volatile uint64_t g_ullSystemTick;

    This variable increase very 1ms in the timmer ISR.Then i want measure the time my code cost use g_ullSystemTick, there's my code:

    Source Code

    1. volatile uint32_t g_ulMaxCenterSendWaitTime = 0, g_ulMaxCenterSendExcuteTime = 0;
    2. volatile uint64_t g_ullStartTick = 0, g_ullIntervalTick = 0;
    3. void function(void)
    4. {
    5. //some code
    6. while (1)
    7. {
    8. g_ullStartTick = g_ullSystemTick;
    9. while (!m_conn_tx_packet_count[pars->peripheral_id])
    10. {
    11. if ((pars->timeout) && (!s_ulOperateTimeoutCounter))
    12. {
    13. break;
    14. }
    15. if ((!g_tBleInterface.ble_connectsta[pars->peripheral_id]) || (m_conn_handle_nus_c[pars->peripheral_id] == BLE_CONN_HANDLE_INVALID))
    16. {
    17. return false;
    18. }
    19. }
    20. g_ullIntervalTick = g_ullSystemTick - g_ullStartTick;
    21. if (g_ullIntervalTick > g_ulMaxCenterSendWaitTime)
    22. {
    23. g_ulMaxCenterSendWaitTime = g_ullIntervalTick;
    24. }
    25. g_ullStartTick = g_ullSystemTick;
    26. if (ble_nus_c_string_send(p_ble_nus_c, &txbuf[send_index], factlen) == NRF_SUCCESS)
    27. {
    28. g_ullIntervalTick = g_ullSystemTick - g_ullStartTick;
    29. if (g_ullIntervalTick > g_ulMaxCenterSendExcuteTime)
    30. {
    31. g_ulMaxCenterSendExcuteTime = g_ullIntervalTick;
    32. g_ucMaxTimeCmd = cmd_par->cmd;
    33. }
    34. if (m_conn_tx_packet_count[pars->peripheral_id])
    35. {
    36. m_conn_tx_packet_count[pars->peripheral_id]--;
    37. }
    38. break;
    39. }
    40. else if (!m_conn_tx_packet_count[pars->peripheral_id])
    41. {
    42. return false;
    43. }
    44. }
    45. }
    Display All


    I debug this program, found g_ulMaxCenterSendWaitTime always be 0, and I know that's clearly wrong,it's definitely not going to be 0. So i set a breakpoint at

    Source Code

    1. if (g_ullIntervalTick > g_ulMaxCenterSendWaitTime)
    , and i found g_ullSystemTick(the value is 1220) is larger than g_ullStartTick(the value is 1200),but g_ullIntervalTick still equal 0!I don't know what went wrong.

    The variables is global variable and use 'volatile' qualifier, and only this function use there variables. My project setting is, Code > Code Generation > Optimization Level set to Debug,Code > Code Generation > Debugging Level set to level-3.

    I run this code in keil MDK,very thing is OK.

    I don't dare use SES now because i don't know when there will be weird problems even my code work well in keil MDK before.

    Is there any way to solve the problem?

    The post was edited 2 times, last by hiqrf3 ().

  • Helllo Ian,

    Thank you for your inquiry.
    We see no issues with your code and such an issue is not known to us.
    Could you compare the disassembly of ES and MDK to see where differences are?
    Does the variable get used in the disassembly?

    Best regards,
    Nino
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.
  • The attachment is the screenshots~
    Images
    • 屏幕快照 2017-11-08 16.46.52.png

      622.01 kB, 2,560×1,600, viewed 553 times
    • 屏幕快照 2017-11-08 16.47.59.png

      800.74 kB, 2,560×1,600, viewed 571 times
  • The attachment is screenshots when use keil MDK
    Images
    • 屏幕快照 2017-11-08 16.56.22.png

      384.51 kB, 2,560×1,600, viewed 544 times
    • 屏幕快照 2017-11-08 16.59.23.png

      407.52 kB, 2,560×1,600, viewed 531 times
  • It seems like the compiler think r0,r1 is the value of g_ullSystemTick, but they are the value of g_ullIntervalTick actually
    Images
    • 屏幕快照 2017-11-08 17.44.12.png

      808.94 kB, 2,560×1,600, viewed 563 times
  • Hello Ian,

    Thank you for providing the screenshots.
    As you correctly deducted r0 and r1 seem to be loading the "wrong" value.
    From the screenshots you provided it is not possible to see when r0 an r1 get loaded.
    Could you provide us with the complete disassembly for that function?

    Best regards,
    Nino
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.
  • Hello Ian,

    Simply copy paste the disassembly to a text file or similar.

    Best regards,
    Nino
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.
  • First of all, i wanna say that it's really difficult to get a complete disassembly, because i can only visit it in the Disassembly window when i debug the program, and then the Disassembly window can not show the complete disassembly of a whole file or function,it's fragmented and discontinuous,so i must set breakpoint at different place in the C file and switch to the Disassembly window when the breakpoint be trigged to splicing the disassembly code.Is there some simple way to get a complete disassembly?

    After i do that, I think I found what the problem is. My raw C code is like this:

    Source Code

    1. if (factlen)
    2. {
    3. while (1)
    4. {
    5. g_ullStartTick = g_ullSystemTick;
    6. // s_ulSendTimeoutCounter = MAX_SEND_WAIT_TIME;
    7. while (!m_conn_tx_packet_count[pars->peripheral_id])
    8. {
    9. if ((pars->timeout) && (!s_ulOperateTimeoutCounter))
    10. {
    11. g_ullIntervalTick = g_ullSystemTick - g_ullStartTick;
    12. if (g_ullIntervalTick > g_ulMaxCenterSendWaitTime)
    13. {
    14. g_ulMaxCenterSendWaitTime = g_ullIntervalTick;
    15. }
    16. break;
    17. }
    18. }
    19. ...other code...
    20. }
    Display All


    when the code

    Source Code

    1. g_ullStartTick = g_ullSystemTick;


    is executed,the disassembly code is like this:

    Brainfuck Source Code

    1. if (factlen)
    2. {
    3. while (1)
    4. {
    5. g_ullStartTick = g_ullSystemTick;
    6. 4B31 ldr r3, =0x200038E8 <g_ullSystemTick>
    7. 6818 ldr r0, [r3]
    8. 6859 ldr r1, [r3, #4]
    9. 4B30 ldr r3, =0x20003E20 <g_ullStartTick>
    10. 6018 str r0, [r3]
    11. 6059 str r1, [r3, #4]
    12. --- communication.c -- 293 ---------------------------------
    13. // s_ulSendTimeoutCounter = MAX_SEND_WAIT_TIME;
    14. while (!m_conn_tx_packet_count[pars->peripheral_id])
    15. E071 b 0x0002F6C0
    Display All


    the compiler load 'g_ullSystemTick' to r0 and r1,and then jump to address 0x0002f6c0, the code at this address is like this:

    Brainfuck Source Code

    1. --- communication.c -- 295 ---------------------------------
    2. while (!m_conn_tx_packet_count[pars->peripheral_id])
    3. 7823 ldrb r3, [r4]
    4. 4AB5 ldr r2, =0x20004398 <m_conn_tx_packet_count>
    5. 5CD2 ldrb r2, [r2, r3]
    6. 2A00 cmp r2, #0
    7. D12E bne 0x0002F728
    8. --- communication.c -- 296 ---------------------------------
    9. {
    10. if ((pars->timeout) && (!s_ulOperateTimeoutCounter))
    11. 79E2 ldrb r2, [r4, #7]
    12. 4694 mov r12, r2
    13. 7A22 ldrb r2, [r4, #8]
    14. 0212 lsls r2, r2, #8
    15. 4666 mov r6, r12
    16. 4316 orrs r6, r2
    17. 7A62 ldrb r2, [r4, #9]
    18. 0412 lsls r2, r2, #16
    19. 4316 orrs r6, r2
    20. 7AA2 ldrb r2, [r4, #10]
    21. 0612 lsls r2, r2, #24
    22. 4332 orrs r2, r6
    23. D0DD beq 0x0002F6A0
    24. --- communication.c -- 297 ---------------------------------
    25. if ((pars->timeout) && (!s_ulOperateTimeoutCounter))
    26. 4AAD ldr r2, =0x20003E6C <s_ulOperateTimeoutCounter>
    27. 6812 ldr r2, [r2]
    28. 2A00 cmp r2, #0
    29. D1D9 bne 0x0002F6A0
    30. --- communication.c -- 298 ---------------------------------
    31. {
    32. g_ullIntervalTick = g_ullSystemTick - g_ullStartTick;
    33. 4BAC ldr r3, =0x20003E20 <g_ullStartTick>
    34. 681A ldr r2, [r3]
    35. 685B ldr r3, [r3, #4]
    36. 0006 movs r6, r0
    37. 000F movs r7, r1
    38. 1AB6 subs r6, r6, r2
    39. 419F sbcs r7, r3
    40. 0032 movs r2, r6
    41. 003B movs r3, r7
    42. 4EA9 ldr r6, =0x20003E18 <g_ullIntervalTick>
    43. 6032 str r2, [r6]
    44. 6073 str r3, [r6, #4]
    Display All


    you can see that when the code 'g_ullIntervalTick = g_ullSystemTick - g_ullStartTick;' be executed,the compiler still use r0 and r1 as the value of 'g_ullSystemTick',but at this moment the value of 'g_ullSystemTick' may have been updated by the timmer ISR, and the compiler still use the old value.

    The variable 'g_ullSystemTick' is global variable and use 'volatile' qualifier, shouldn't the compiler update the value very time it is used? How can I solve this problem, and how can I avoid similar problem in the future?
  • Oh, I found what caused this problem finally, that is, I define 'g_ullSystemTick' in main.c like this:

    Source Code

    1. volatile uint64_t g_ullSystemTick = 0;


    but I declare it in communication.c like this:

    Source Code

    1. extern uint64_t g_ullSystemTick;


    I missed 'volatile' here and I think that's OK because I used it when i define the function... :huh: :huh:

    I used to do that when i use MDK and the program work well,so I think the declare is not important...

    By the way, is MDK not rigorous enough?
  • One more thing, I asked this question before,that's "In OS X, some shortcut key not work.For example, the short key "option+left" for 'Back" funcion in text editor and "option + right" for "Forward"." When in editor window, I can click the button on the left top of the editor for the "Back" and "Forward" function, Bug when debugging the program, there's no buttons on the editor window,I can not also find "Back" and "Forward" item in the menu, and the shortcut not work, so I can't use there function any more, I have to manually locate where I want to be,It's a bad experience and I hope you can improve it.

    I use other softwares in OS X,ther's shortcut works well,for example,I use "NetEasy Music Player" software,it use "command + left" and "command + right" to switch songs, and Android studio's shortcut all also work well,There are many similar examples. Maybe your can try to find out why the shortcut not work in OS X instead "Unfortunately there is not much we can do about it as some shortcuts on MACOS are system bound and cannot be disabled or changed.", and maybe you can reference other software's shortcut.
  • Hello Ian,

    Ok let me try to find all your questions.

    Is there some simple way to get a complete disassembly?

    In Embedded Studio you can simply select all Ctrl + a and copy paste it into a new file.

    Alternatively you an load up your application in Ozone and there you have a export option for the disassembly window when you right click it.

    but I declare it in communication.c like this:

    extern uint64_t g_ullSystemTick;


    This was crucial information and this whole thread could have been avoided if you would have provided us with the correct data.
    When we ask for the code example do not alter it otherwise things like this can happen.

    By the way, is MDK not rigorous enough?


    Compilers like the ones from MDK tend to be more forgiving for the sake of usability.
    But that way errors like this can get into the code which will lead to errors when using compilers that are strictly following C coding standards.
    Embedded Studio currently uses GCC as the compiler which is known to be very strict when enforcing C rules.

    One more thing, I asked this question before,that's "In OS X, some shortcut key not work.For example, the short key "option+left" for 'Back" funcion in text editor and "option + right" for "Forward".

    This issue should be fixed in the latest Embedded Studio release. Which version are you using currently?

    Best regards,
    Nino
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.
  • Hi Ian,


    The version of ES i used is V3.30, the version of OS X of my MacBook pro is 10.13.1, the shortcut still not work~


    Ok this is weird. I will forward this to the ES developers.
    Sorry for any inconveniences caused.

    Edit: This issue was not fixed yet in the current release version because it was already out when we got your request.
    Sorry this was wrong information on my part.
    This fix should be available with the V3.30a.

    Best regards,
    Nino
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.