We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi
I have an error that shows its ugly head now and then.... The µC I'm using is C8051F122.
When I debug I can locate the problem, but the bug does not show the same place in code all times.
What I see now is that when stepping over the following line:
SMB0CN = 0x44;
The register SMB0CN actually is set to 0xC4. This sets the BUSY bit in the register.
It can be resolved by removing the µC supply, but a RESET won't do. But after some time the problem shows again.....
Does anyone have an idea????
Best Regards....
unsigned char xdata ucMyVar = 0; ucMyVar = 0xFF;
I'll assume that was just a typo...
In the watch window I see that the value of the variable still is 0 after stepping over the code....
Any ideas?
That means nothing at all. In particular that does not constitute any kind of mistake. The compiler is allowed to move code around, as long as the end result is still the right one. You'll have to step on until at least the first instruction that reads this variable before you can conclude that the compiler isn't going to write to it in time.
Yeah ucMyVar was what I meant.
What you are saying is that you can not trust the data in the watch window until the variable is used the next time?
I know that the compiler can keep it anywhere in memory, but I surely would expect the watch window to reflect the value of the variable at all times (of course only global variables)....
"What you are saying is that you can not trust the data in the watch window"
No, that's not it at all.
The watch window will accurately display what's actually in the physical RAM.
The problem is that the Compiler optimisation may mean that the physical RAM may not actually be updated at the precise point of the specific source line.
You will have to look at the generated assembler to see precisely what's actually happening.
"I know that the compiler can keep it anywhere in memory"
That's not the point - see above.
"I surely would expect the watch window to reflect the value of the variable at all times"
No - you need to spend some time understanding how optimising compilers work!
The compiler can decide to remove variables that are not used. And it can decide to store a variable (all the time or sometimes) in a register. And the variables may not live through the full function call.
Regarding I2C - you should check the state of the data line to see if any external device is driving the line. There are a number of articles you can Google for that describes how you can switch from I2C to GPIO and pin-toggle the clock line until you get the external device into a state where you can switch back to I2C and send a new start condition.
Especially if I2C communication is used to equipment on a different PCB, you need to handle situations where a spurious pulse may lock up the transfer.
but I surely would expect the watch window to reflect the value of the variable at all times
And it quite certainly does just that.
But the compiler can delay actually making the change to the value until right before it needs it. It can even optimize the entire variable out of existence. So unless you turned optimization off completely, you shouldn't put too much trust in the sequence of operations performed by the CPU being the same as that in the source code.
"the compiler can delay actually making the change to the value until right before it needs it. It can even optimize the entire variable out of existence"
Indeed.
Perhaps these articles may be helpful in understanding some of the issues around compiler optimisation:
www.8052.com/.../175943
www.8052.com/.../96661
it would be nice to know what the error that started this whole ting is - I do not see it mentioned
questions 1) where did you get the IIC code from 2) is/are the attached chip(s) IIC or SMB? 3) have you disabled the timeouts in SMB0CN 4) have you removed the capacitor on the reset pin 5) set the optimizationh to 2 - NOTHING ELSE WHEN DEBUGGING 6) the name Morten makes me guess - what nationality are you
Erik
The I2C code has existed for long has been working properly. Two slaves are connected to the I2C bus.
The timeout has been disabled in SMB0CN, and the C has not been removed.
If I lower the optimization to 2, Warnings will show under download:
for example: "Warning! An attempt was made to modify address 1FD09 which is restricted! Continue download?"
if I press "OK" the address is incremented and a new box is thrown...
Is your code so bloated that you overrun 127k with opt 2 switch to e.g. an ARM
Otherwise find out what writes to the "protection and other stuff area" and fix it. The .map file should show you what to look for.
Many do not realize that the top 1k if the f12x/f13x is not code memory but 'restricted'. If you write certain locations the whole shebang will lock up.