본문 바로가기
DSP, MCU/펌웨어

STM32 ] __IO , volatile 타입 한정자 (Qualifier)

by eteo 2022. 6. 23.

 

 

이에 대한 정의는 Drivers - CMSIS - Include _ core_cm4.h 파일 내에서 찾을 수 있다.

 

 

 

 

 

/* IO definitions (access restrictions to peripheral registers) */
/**
    \defgroup CMSIS_glob_defs CMSIS Global Defines

    <strong>IO Type Qualifiers</strong> are used
    \li to specify the access to peripheral variables.
    \li for automatic generation of peripheral register debug information.
*/
#ifdef __cplusplus
  #define   __I     volatile             /*!< Defines 'read only' permissions */
#else
  #define   __I     volatile const       /*!< Defines 'read only' permissions */
#endif
#define     __O     volatile             /*!< Defines 'write only' permissions */
#define     __IO    volatile             /*!< Defines 'read / write' permissions */

/* following defines should be used for structure members */
#define     __IM     volatile const      /*! Defines 'read only' structure member permissions */
#define     __OM     volatile            /*! Defines 'write only' structure member permissions */
#define     __IOM    volatile            /*! Defines 'read / write' structure member permissions */

/*@} end of group Cortex_M4 */

 

보통 프로그램 실행시 속도를 위해 CPU에서 데이터를 메모리에서 직접 읽어오지 않고, 캐시(Cache)를 통해서 읽어오게 되는데 이를 막고 직접 메모리에서 읽어오게끔 하기위해 volatile 키워드가 사용된다.

 

특히 임베디드 시스템에서 peripheral registers 변수로 쓰기위해 volatile 키워드를 사용하거나, 인터럽트 서비스 루틴(ISR)에서 값이 변경될 수 있는 전역 변수의 경우 램에서는 값이 변경 되었는데 프로세서가 캐시에서 읽어오면 문제가 생길 수 있기 때문에 volatile 키워드를 붙인다. 

 

또한 컴파일러가 dead code로 보고 무시하는 경우(컴파일러 최적화)를 막기 위해volatile 키워드를 사용한다.

 

 

 

참고 : CMSIS 란, Cortex Microcontroller Software Interface Standard의 약자로, 소프트웨어 제품들 간 호환성을 고도화 시키고 소프트웨어 이식성을 증대 시키기 위해 ARM 사에서 개발한 소프트웨어 프레임워크를 말한다.