Difference between revisions of "Const and volatile type qualifiers"
From Teknologisk videncenter
m (→Example 1) |
m (→Example 1) |
||
Line 15: | Line 15: | ||
*uartreg |= 0x1 // *uartreg's contents is OR'et to 0x1. New contents = 0x80000001 | *uartreg |= 0x1 // *uartreg's contents is OR'et to 0x1. New contents = 0x80000001 | ||
</source> | </source> | ||
+ | ==Example 2== | ||
+ | {| | ||
+ | | | ||
+ | |- | ||
+ | |From the Cortex M [[STM32F107VC]]s [http://www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/REFERENCE_MANUAL/CD00171190.pdf Reference manual] - See picture below - that the size of the embedded Flash can be seen in KB from the '''Flash size register''' at memory location 0x1FFFF7E0. | ||
+ | |- | ||
+ | |[[image:STM32flash.png|left|500px|thum|[[STM32F107VC]] Flash size register]] | ||
+ | |- | ||
+ | | | ||
+ | ===Reading the Flash size register=== | ||
+ | <source lang=c> | ||
+ | uint16_t volatile * const flashsize = (uint16_t *) 0x1ffff7e0; | ||
+ | printf("Flashsize: %i KB\n\r", (int) *flashsize ); | ||
+ | </source> | ||
+ | On the [[STM32F107VC]] the output to stdout would be '''Flashsize: 256 KB''' | ||
+ | |- | ||
+ | |} | ||
=Links= | =Links= |
Revision as of 08:29, 17 February 2012
Contents
const
const means that something is not modifiable, so a data object that is declared with const as a part of its type specification must not be assigned to in any way during the run of a program.
volatile
The use of volatile ensures that the compiler always carries out the memory accesses, rather than optimizing them out (for example if the access is in a loop).
Memorymapped access to a register
Example 1
In the example below the register 32 bit register located in memory at address 0x40000 can be accessed through the uartreg pointer.
unsigned int volatile * const uartreg = (unsigned int *) 0x40000;
*uartreg = 0x80000000; // Assign 80000000 to the 32 bit register located at memeory address 0x40000
...some code...
*uartreg |= 0x1 // *uartreg's contents is OR'et to 0x1. New contents = 0x80000001
Example 2
From the Cortex M STM32F107VCs Reference manual - See picture below - that the size of the embedded Flash can be seen in KB from the Flash size register at memory location 0x1FFFF7E0. |
Reading the Flash size register uint16_t volatile * const flashsize = (uint16_t *) 0x1ffff7e0;
printf("Flashsize: %i KB\n\r", (int) *flashsize );
On the STM32F107VC the output to stdout would be Flashsize: 256 KB |