Forum > Embedded - AVR

Volatile

(1/1)

dseligo:
In C, when you use variable in interrupt routine, you have to declare variable with 'volatile' (so it always load/save variable in SRAM):

--- Code: C  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---volatile int test;
Is something similar needed in Free pascal and if it is, how to do it?

RayoGlauco:
Maybe here is something interesting: https://forum.lazarus.freepascal.org/index.php/topic,45299.15.html

Thaddy:
Yes there is something available in trunk/main: https://wiki.freepascal.org/FPC_New_Features_Trunk#Support_for_.22volatile.22_intrinsic

It comes with a link to an example:
https://gitlab.com/freepascal.org/fpc/source/-/blob/main/tests/test/tmt1.pp

You need trunk/main, it is not in 3.2.2 or fixes 3.2.3

Also note the remark that in the future it may be done through an attribute.

dseligo:
Thank you all, I'll test it.

ccrause:

--- Quote from: dseligo on October 05, 2022, 10:32:55 am ---In C, when you use variable in interrupt routine, you have to declare variable with 'volatile' (so it always load/save variable in SRAM):

--- Code: C  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---volatile int test;
Is something similar needed in Free pascal and if it is, how to do it?

--- End quote ---
It is not (yet?) required, since FPC will reload global variables. As Thaddy mentioned, volatile is available in the development branch, so eventually it will/may be added to a stable release.  At that time it may be prudent to wrap data access with the volatile intrinsic (when necessary) to guard against overly aggressive compiler optimization interfering with the programmer's assumptions.

Navigation

[0] Message Index

Go to full version