Monday, December 11th 2017, 10:08am UTC+1

You are not logged in.

  • Login
  • Register

Dear visitor, welcome to SEGGER Forum. If this is your first visit here, please read the Help. It explains how this page works. You must be registered before you can use all the page's features. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

hiqrf3

Beginner

Date of registration: Oct 23rd 2017

Posts: 14

1

Wednesday, November 8th 2017, 8:53am

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
        volatile uint32_t g_ulMaxCenterSendWaitTime = 0, g_ulMaxCenterSendExcuteTime = 0;
        volatile uint64_t g_ullStartTick = 0, g_ullIntervalTick = 0;

        void function(void)
        {
              //some code
            
            while (1)
            {
                g_ullStartTick = g_ullSystemTick;
                
                while (!m_conn_tx_packet_count[pars->peripheral_id])
                {
                     if ((pars->timeout) && (!s_ulOperateTimeoutCounter))
                     {
                        break;
                     }
                     
                     if ((!g_tBleInterface.ble_connectsta[pars->peripheral_id]) || (m_conn_handle_nus_c[pars->peripheral_id] == BLE_CONN_HANDLE_INVALID))
                     {
                         return false;
                     }
                }
                 
                g_ullIntervalTick = g_ullSystemTick - g_ullStartTick;
                if (g_ullIntervalTick > g_ulMaxCenterSendWaitTime)
                {
                    g_ulMaxCenterSendWaitTime = g_ullIntervalTick;
                }
                
                g_ullStartTick = g_ullSystemTick;
                
                if (ble_nus_c_string_send(p_ble_nus_c, &txbuf[send_index], factlen) == NRF_SUCCESS)
                {
                    g_ullIntervalTick = g_ullSystemTick - g_ullStartTick;
                    if (g_ullIntervalTick > g_ulMaxCenterSendExcuteTime)
                    {
                        g_ulMaxCenterSendExcuteTime = g_ullIntervalTick;
                        g_ucMaxTimeCmd = cmd_par->cmd;
                    }
                
                    if (m_conn_tx_packet_count[pars->peripheral_id])
                    {
                        m_conn_tx_packet_count[pars->peripheral_id]--;
                    }
                    
                    break;
                }
                else if (!m_conn_tx_packet_count[pars->peripheral_id])
                {
                    return false;
                }
            }
        }
        


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?

This post has been edited 2 times, last edit by "hiqrf3" (Nov 8th 2017, 9:31am)


SEGGER - Nino

Super Moderator

Date of registration: Jan 2nd 2017

Posts: 434

2

Wednesday, November 8th 2017, 9:34am

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

hiqrf3

Beginner

Date of registration: Oct 23rd 2017

Posts: 14

3

Wednesday, November 8th 2017, 9:50am

The attachment is the screenshots~
hiqrf3 has attached the following images:
  • 屏幕快照 2017-11-08 16.46.52.png
  • 屏幕快照 2017-11-08 16.47.59.png

hiqrf3

Beginner

Date of registration: Oct 23rd 2017

Posts: 14

4

Wednesday, November 8th 2017, 10:00am

The attachment is screenshots when use keil MDK
hiqrf3 has attached the following images:
  • 屏幕快照 2017-11-08 16.56.22.png
  • 屏幕快照 2017-11-08 16.59.23.png

hiqrf3

Beginner

Date of registration: Oct 23rd 2017

Posts: 14

5

Wednesday, November 8th 2017, 10:50am

It seems like the compiler think r0,r1 is the value of g_ullSystemTick, but they are the value of g_ullIntervalTick actually
hiqrf3 has attached the following image:
  • 屏幕快照 2017-11-08 17.44.12.png

SEGGER - Nino

Super Moderator

Date of registration: Jan 2nd 2017

Posts: 434

6

Wednesday, November 8th 2017, 12:03pm

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

hiqrf3

Beginner

Date of registration: Oct 23rd 2017

Posts: 14

7

Wednesday, November 8th 2017, 12:55pm

How can i get the complete disassembly? I'am in home now,and the computer left in the company, so I can only upload it tomorrow (UTC+8 time)

SEGGER - Nino

Super Moderator

Date of registration: Jan 2nd 2017

Posts: 434

8

Wednesday, November 8th 2017, 1:18pm

Hello Ian,

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

Best regards,
Nino

hiqrf3

Beginner

Date of registration: Oct 23rd 2017

Posts: 14

9

Thursday, November 9th 2017, 3:25am

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

            }


when the code

Source code

1
g_ullStartTick = g_ullSystemTick;


is executed,the disassembly code is like this:

Source code

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


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

Source code

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


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?

hiqrf3

Beginner

Date of registration: Oct 23rd 2017

Posts: 14

10

Thursday, November 9th 2017, 3:51am

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?

hiqrf3

Beginner

Date of registration: Oct 23rd 2017

Posts: 14

11

Thursday, November 9th 2017, 7:42am

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.

SEGGER - Nino

Super Moderator

Date of registration: Jan 2nd 2017

Posts: 434

12

Friday, November 10th 2017, 9:16am

Hello Ian,

Ok let me try to find all your questions.

Quoted

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.

Quoted

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.

Quoted

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.

Quoted

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

hiqrf3

Beginner

Date of registration: Oct 23rd 2017

Posts: 14

13

Friday, November 10th 2017, 9:49am

It's my mistake, I'm not provided the correct data.

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~

SEGGER - Nino

Super Moderator

Date of registration: Jan 2nd 2017

Posts: 434

14

Friday, November 10th 2017, 9:51am

Hi Ian,

Quoted


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