Programming/C Programming
volatile keyword
decdream
2013. 7. 17. 11:55
"비휘발성" 이라는 의미로,
컴파일러에게 임의의 최적화를 금지할때 쓰는 키워드 이다.
#define LCD_RAM (*((volatile unsigned short *) 0x60020000)) /* RS = 1 */
이런식으로 address 에 직접 access 할때,
저장공간을 컴파일러가 최적화 하여, r3 나 r5 로 변경해 버리면 곤란할 것이다.
또한, HW 에 의해서 지속적으로 변경되는 데이터를 담고 있다면,
이또한 read 할때마다, 값이 update 되어야 하는데
컴파일러 최적화는 이를 방해하는 요소가 된다.
이러한 것들을 피할때 volatile keyword 를 사용한다.
단, 추가 첨언을 하자면..
The rule of thumb in Linux kernel programming is that if you feel tempted to use the “volatile” keyword, you’re doing something wrong: The correct way to access hardware registers is with iowrite32(), ioread32() and other io-something functions and macros. All device drivers demonstrate this.
리눅스 프로그래밍에서 volatile 을 사용해야 할것 같다면, 무언가 잘못 구현한 것이다.
iowrite32(), ioread32() 등의 메크로를 사용해야 한다.