|
|
View previous topic :: View next topic |
Author |
Message |
bmitchell2407
Joined: 11 May 2018 Posts: 1
|
volatile pointer to volatile data |
Posted: Fri May 11, 2018 3:24 pm |
|
|
Hi, new to the forum here.
It seems that CCS cannot create a volatile pointer to volatile data as in:
Code: |
volatile int8 * volatile p;
|
I think this is correct standard C code, but the compiler doesn't like this. I want to use this because I have a pointer used in an ISR where the data changes and the pointer changes.
Has anyone else tried to do this?
Thanks! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat May 12, 2018 2:15 am |
|
|
bmitchell2407 wrote: | volatile int8 * volatile p; |
I agree it doesn't compile with vs. 5.078 (PCH compiler).
If you post a very small but complete test program we could see
if it's actually necessary with CCS. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Sat May 12, 2018 8:45 am |
|
|
'Volatile' has almost no effect in CCS code. The PIC12, 16 & 18, don't have any temporary registers (only W), so everything is always read each time it is used from the physical memory. This differs from other processors (and the PIC24/30 etc.), which have 'working' registers. Volatile is meant to ensure that the code will not store things into temporary registers, but instead always read from the memory. Now I haven't looked for the DsPIC's where volatile might have an effect, but on the standard PIC's it doesn't.... |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Sun May 13, 2018 10:22 pm |
|
|
I agree with this:
Ttelmah wrote: | 'Volatile' has almost no effect in CCS code. |
But not completely with this:
Ttelmah wrote: | The PIC12, 16 & 18, don't have any temporary registers (only W), so everything is always read each time it is used from the physical memory. This differs from other processors (and the PIC24/30 etc.), which have 'working' registers. |
The PIC18 does have other temporary registers, such as the PRODH and PRODL registers. I have used these as working registers in the past. I also consider the address space in the access bank for the to be fair game for temporary registers _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Mon May 14, 2018 1:08 am |
|
|
What 'volatile' is meant to prevent is the situation where (for instance) a port value is read and tested, with a loop till it is zero, which once optimised by the compiler, does not reload the physical port value after the first test. However CCS does not do this. If a value is tested, it will be retested every time round the loop.
Being 'globally volatile', is potentially inefficient. However the choices in the PIC are quite limited in this regard. Areas that would gain from being non volatile are the PROD registers (as pointed out above), but little else. The compiler won't sit assuming that "I tested that a moment ago, it can't have changed". If asked to test, it will test.
There are issues that the programmer has to deal with (such as multi byte values being updated between operations by an interrupt), but these are separate.
A 'classic' volatile demo, is;
Code: |
int a=0;
while (a==0)
;
|
Which with a 'non volatile' variable 'a' can become optimised to looping for ever without testing a again (after all it is zero....). However with 'a' declared as volatile the loop has to load 'a' and actually test it. So if the value does get changed in an interrupt for example, the code will exit the loop. CCS does this by default. |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
Re: volatile pointer to volatile data |
Posted: Mon May 14, 2018 4:37 am |
|
|
bmitchell2407 wrote: | It seems that CCS cannot create a volatile pointer to volatile data as in:
Code: |
volatile int8 * volatile p;
|
I think this is correct standard C code, but the compiler doesn't like this. I want to use this because I have a pointer used in an ISR where the data changes and the pointer changes. |
I do not know why the compiler rejects this syntax. We are just users - this is a self-help forum - and are not CCS and do not have any knowledge of or control over their compiler implmentation.
However, I have never used such a syntax, mainly because its very rare that it would ever be of practical use, except in C syntax exercises. Volatile pointers are common: I/O buffer pointers for example. In that case, the fact that the pointer changes"under" the code indicates that the data itself also changes. For this the volatile pointer syntax is fine:
but also, in the case of CCS code, unnecessary for the reasons Ttelmah outlined. Hence this is rarely seen in CCS code, even if seen as "correct" from a purist C perspective. Or more to the point, volatile tends to be used to solve problems, but if, as in CCS C, the particular problems its used to solve don't happen, then volatile tends not to be used, even if it is "correct" C. Even where volatile could be useful, for example to solve non-atomicity issues such as int16/32s changed in an ISR on PIC 18s, people tend to get round the issue in other ways, such as the "read twice" method, or even, arguably more appropriate to the hardware but also hardware specific, by critical section locking, but then this particular issue is hardware specific. I don't know if volatile actually works in that scenario, as I tend toward critical sections.
The volatile pointer to volatile data scenario would be for situations where the pointer can change and the data can change *without* a change in the pointer, i.e. the two were independant, which is rare.
So, its rare to see volatile in CCS code, and while its somewhat saddening that it doesn't compile, seeing two volatiles in one declaration is exceptionally rare to the point of being endangered. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Mon May 14, 2018 6:54 am |
|
|
Where volatile become massively important is on systems that can cache large amounts of what they are actually doing.
This is the best post I know about volatile, especially the reference for where it comes from:
<https://blog.regehr.org/archives/28>
Note the comment about a simple C. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|