Helpful ReplyHot!Issue with incrementing and setting a global variable in XC32

Page: 12 > Showing page 1 of 2 - Powered by APG vNext Trial
Author
nschaeferle
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2017/01/07 13:01:15
  • Location: 0
  • Status: offline
2018/06/12 14:20:45 (permalink)
0

Issue with incrementing and setting a global variable in XC32

I believe there is an issue with XC32 2.10 in incrementing and declaring global variables. I am running MPLAB 4.20 with a PICkit 3 and the target device is a PIC32MK0512MCF064. I can provide more details if that is needed but I believe the route of the problem is some insidious bug with the compiler/linker. Shown below is a simple program showing three variables, two would be sufficient but i used three to further demonstrate the issue.
 
#include <xc.h>
#include <sys/attribs.h>

unsigned int x = 5;
unsigned int y = 5;
unsigned int z = 5;

int main(void)
{
 while(1)
 {
  x++;
  y++;
  z++;
 }
}

 
The problem I am having is that the first globally declared variable, x in this case, gets clobbered? after running and breakpointing in main on the line that does "z++". What I mean is that the variables will look okay at the start of the program and then x gets "stuck" at 1
 
first break -     x = 6, y = 6, z = 5
second break - x = 1, y = 7, z = 6
third break -    x = 1, y = 8, z = 7
fourth break -  x = 1, y = 9, z = 8
 
Variable x is at memory location 0x80000364. I mention this because this seems to be the only common link here. For instance if i switch the order of x and y deceleration then y gets "clobbered and stuck"
 
The disassembly (if helpful) is:
 
!int main(void)
!{
0x9D00073C: ADDIU SP, SP, -8
0x9D000740: SW FP, 4(SP)
0x9D000744: ADDU FP, SP, ZERO
! while(1)
! {
! x++;
0x9D000748: LW V0, -32764(GP)
0x9D00074C: ADDIU V0, V0, 1
0x9D000750: SW V0, -32764(GP)
! y++;
0x9D000754: LW V0, -32760(GP)
0x9D000758: ADDIU V0, V0, 1
0x9D00075C: SW V0, -32760(GP)
! z++;
0x9D000760: LW V0, -32756(GP)
0x9D000764: ADDIU V0, V0, 1
0x9D000768: SW V0, -32756(GP)
! }
0x9D00076C: J 0x9D000748
0x9D000770: NOP

 
Please help me resolve this.
post edited by nschaeferle - 2018/06/12 14:25:19
#1
nschaeferle
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2017/01/07 13:01:15
  • Location: 0
  • Status: offline
Re: Issue with incrementing and setting a global variable in XC32 2018/06/12 14:24:03 (permalink)
0
The closest related issue I have found is
 
https://www.microchip.com/forums/m960692.aspx?high=global+variable
 
however comment 6 does not resolve my issue
#2
Gort2015
Klaatu Barada Nikto
  • Total Posts : 2611
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: Issue with incrementing and setting a global variable in XC32 2018/06/12 14:32:36 (permalink)
+1 (1)
The compiler knows that nothing really happens.
Try writing each one to a SFR such as timer or uart, they will all change.
 
Clobbered, inline assembly yes but not in C.  The registers are irrelevant to a C programmer.

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#3
du00000001
Just Some Member
  • Total Posts : 1893
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: Issue with incrementing and setting a global variable in XC32 2018/06/12 14:33:50 (permalink)
0
The assembly is just looking fine.
Maybe you'll catch some other component writing to this address (e.g.setting a data breakpoint to this specific address).
Could it be the debugger is interfering?

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#4
DarioG
Allmächtig.
  • Total Posts : 53419
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: porcodioland
  • Status: online
Re: Issue with incrementing and setting a global variable in XC32 2018/06/12 14:36:32 (permalink)
+1 (1)
I'd definitely state it's a debugger "artifact"

DEUTSCHLAND TOOOOOR!!! Brandenburger springt :D

#5
nschaeferle
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2017/01/07 13:01:15
  • Location: 0
  • Status: offline
Re: Issue with incrementing and setting a global variable in XC32 2018/06/12 15:13:07 (permalink)
+1 (1)
I think Dario is right. Once again this looks like debugger behavior. I added some additional code to test this. The image attached is a example of "waiting for x to reach 9". The first break on the if statement is "debugger_issue.png" and the next break within the if statement (q++) is the "debugger_issue_2.png".
 
Additionally If i continue to run and break on this if block and not move the breakpoint to within the if block then the code never increments x or q and I can get y and z to increment forever, or at least as long as i care to click.
 
I think this proves that it is a debugger problem? If so then what is the solution to get the debugger to give me a real reflection of what the micro is doing on the breakpoint in question? I cant really begin to apply this micro to my larger project without the debugger "working"
 
 
post edited by nschaeferle - 2018/06/12 15:21:35

Attachment(s)

Attachments are not available: Download requirements not met
#6
du00000001
Just Some Member
  • Total Posts : 1893
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: Issue with incrementing and setting a global variable in XC32 2018/06/12 15:54:46 (permalink)
0
Check the debugging options!
Or simply declare the RAM to start "a bit higher". BTW: do you have a "manual" or the automatic linker file?

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#7
nschaeferle
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2017/01/07 13:01:15
  • Location: 0
  • Status: offline
Re: Issue with incrementing and setting a global variable in XC32 2018/06/12 16:05:21 (permalink)
0
I believe I have an automatic linker file. I am using "whatever came with MPLAB and XC32" as I am not experienced enough with the linker files to be tinkering with them.
 
What debugging options should i be checking? I have had similar issues to this in the past when the project had higher level optimization options but right now I am at the lowest setting (- 0 - Do not optimize. The compiler’s goal is to reduce the cost of compilation and to make debugging produce the expected results.)
#8
NKurzman
A Guy on the Net
  • Total Posts : 15947
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: Issue with incrementing and setting a global variable in XC32 2018/06/12 16:54:13 (permalink)
+2 (2)
Another I am testing the compiler with my Test code and the compiler is broke Post?
Problem
1. if your Test code does Nothing, the Compilers optimizer can just delete it.
2. some simple variables are in the Registers an MplabX does not always display them.
3. Scope and the elf file.  The compiler tries to tell the debugger when a variable exists. Its life can be short. or sometimes the communication is wrong and MPlabX thinks it is not in scope, but it is.
 
My suggestion is to start back at the original issue that lead you to test the compiler.  Debbuging the output of an optimizing compiler is a skill to learn.
#9
DarioG
Allmächtig.
  • Total Posts : 53419
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: porcodioland
  • Status: online
Re: Issue with incrementing and setting a global variable in XC32 2018/06/13 01:48:20 (permalink)
0
I always debug via leds or serial port... though I undeerstand that in some cases Debugger is (would be) handy... :)

DEUTSCHLAND TOOOOOR!!! Brandenburger springt :D

#10
Gort2015
Klaatu Barada Nikto
  • Total Posts : 2611
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: Issue with incrementing and setting a global variable in XC32 2018/06/13 02:36:04 (permalink)
+1 (1)
I also debug using the serial port.
 

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#11
rpg7
Super Member
  • Total Posts : 1355
  • Reward points : 0
  • Joined: 2003/11/07 12:47:35
  • Status: offline
Re: Issue with incrementing and setting a global variable in XC32 2018/06/13 02:55:40 (permalink)
+1 (1)
Gort2015
I also debug using the serial port.



Agreed. The debugger  can cause a lot of heartache. It is also useful to implement exception handlers.
#12
du00000001
Just Some Member
  • Total Posts : 1893
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: Issue with incrementing and setting a global variable in XC32 2018/06/13 03:09:27 (permalink)
+1 (1)
I'm using serial, DAC (where available) and - - - the debugger.
Each of these tools has its advantages and drawbacks. But my favorite is really the debugging tool - for ease of use (when it works) and 'universality'.

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#13
NKurzman
A Guy on the Net
  • Total Posts : 15947
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: Issue with incrementing and setting a global variable in XC32 2018/06/13 09:25:31 (permalink)
+1 (1)
The Debugger, LED, and serial Port are just tools in the Tool Box. You need to know what tool to use and when. And how to use the tool.  You can use a screwdriver to open a paint can.  But if you are using it to drive a nail, then may the issue is not the tool.
#14
nschaeferle
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2017/01/07 13:01:15
  • Location: 0
  • Status: offline
Re: Issue with incrementing and setting a global variable in XC32 2018/06/13 09:34:01 (permalink)
0
NKurzman, I appreciate your input but it's a little hard to understand your points without the use of proper sentence construction/grammar (comment #9) and your feedback for comment #14 isnt constructive.
 
I mean no offense. If you would like to help here then please inform me on when the In Circuit Debugger is appropriate to use, if you know? I think my main concern is having the ability to inspect registers (especially peripheral control registers) when trying to bring up peripherals on the target micro. My only tool to do this is the ICD. I never had such odd and alarming behavior in my previous experiences with dspic30f, pic32mx, pic24, and pic18
 
Lastly in regards to comment #9: optimization level is at 0 so there should be no optimization? Also I think my example is about as simple as it can get?
post edited by nschaeferle - 2018/06/13 09:36:41
#15
Jim Nickerson
User 452 _
  • Total Posts : 4722
  • Reward points : 0
  • Joined: 2003/11/07 12:35:10
  • Location: San Diego, CA
  • Status: offline
Re: Issue with incrementing and setting a global variable in XC32 2018/06/13 10:39:10 (permalink)
0
nschaeferle
NKurzman, I appreciate your input but it's a little hard to understand your points without the use of proper sentence construction/grammar (comment #9) and your feedback for comment #14 isnt constructive.
 
I mean no offense. If you would like to help here then please inform me on when the In Circuit Debugger is appropriate to use, if you know? I think my main concern is having the ability to inspect registers (especially peripheral control registers) when trying to bring up peripherals on the target micro. My only tool to do this is the ICD. I never had such odd and alarming behavior in my previous experiences with dspic30f, pic32mx, pic24, and pic18
 
Lastly in regards to comment #9: optimization level is at 0 so there should be no optimization? Also I think my example is about as simple as it can get?


nschaeferle,
Offending those trying to help you will not get you very far.
Maybe you have missed the point of a simple example.
Do make a new project with the minimal code that demonstrates the problem.
Package the project and attach to a post.
Then others ( if they are so inclined ) may run your project and maybe help you out.
#16
nschaeferle
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2017/01/07 13:01:15
  • Location: 0
  • Status: offline
Re: Issue with incrementing and setting a global variable in XC32 2018/06/13 11:06:24 (permalink)
0
Jim, I dont know how to keep a thread on track when people are not offering constructive advice. I was trying to be polite and I even noted that I am not try to offend anyone.
 
I appreciate the feedback and I think its important to continue this thread as it may serve as a good educational resource for everyone, including myself, to know when and for what the ICD is useful for. To my knowledge there is no documentation from Microchip that lists why these "why erratic debugger behaviors/artifacts occur". In other words it would be helpful if there was a user guide or something like a micro's silicon errata to point out these particular issues.
 
The project is attached. Its quite simple to get a repro. All you have to do is set a breakpoint on the if block (line 17) then cycle through that break point to see x get set to 1 (or at least the debugger thinks this is happening) then remove that breakpoint and set a break at line 19 where q++ happens. Continue to run and you will see that the q++ occurs but y and z are much "farther along" in their increment process

Attachment(s)

Attachments are not available: Download requirements not met
#17
NKurzman
A Guy on the Net
  • Total Posts : 15947
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: Issue with incrementing and setting a global variable in XC32 2018/06/13 11:26:56 (permalink)
+2 (2)
My Point to you is you started of the track because you are fighting the Optimizer and the Debugger.
You simple example does not appear to work. But it is most likely to be following the Rules, plus the limitations (or Bugs) of the PIC32 Debbugger. 
So do you want to Debug that example? Or deal with your actual issue?
 
You can make a Global Volatile.  This will make it more likely to display properly in the DEBUGGER.  remove the Volatile after you are done debugging.  IF the variable is used by both main and interrupt ( or 2 interrupts) then it MUST be Volatile.  Then There are issues with is a Variable OR Register Operation Atomic.  The PIC32 is not a PIC10-33.  It is MIPS
 
So You can Move to your actual issue, Or You can continue with attempting to Prove the Compiler can not Generate simple code.  Always a possibility, But usually Not.  And Since MplabX and its debugging has a long history of issues, It is not proof.
#18
nschaeferle
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2017/01/07 13:01:15
  • Location: 0
  • Status: offline
Re: Issue with incrementing and setting a global variable in XC32 2018/06/13 11:34:13 (permalink)
0
NKurzman, thanks for the feedback I would like to stick to the simple example as my actual problem will probably obscure the understanding of the issue I am having. I do realize this is a MIPs architecture but my experience with PIC32MX devices has not shown this problem.
 
I tried the simple example with x, y, z, and q as volatile and it has the same behavior
#19
NKurzman
A Guy on the Net
  • Total Posts : 15947
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: Issue with incrementing and setting a global variable in XC32 2018/06/13 13:05:31 (permalink) ☄ Helpfulby Jim Nickerson 2018/06/13 13:47:15
+2 (2)
unsigned int x = 5;
unsigned int y = 5;
unsigned int z = 5;

int main(void)
{
 while(1)
 {
  x++;
  y++;
  z++;
 }
}
 X , Y and Z are Not used. 
The Compiler can choose to not do the increments, since they are not used.
try assigning the results to a register, like a port. or using them some how.
#20
Page: 12 > Showing page 1 of 2 - Powered by APG vNext Trial
Jump to:
© 2018 APG vNext Trial Version 4.5