DIP62: Volatile type qualifier for unoptimizable variables in embedded programming

Walter Bright via Digitalmars-d digitalmars-d at puremagic.com
Tue Jul 15 23:44:26 PDT 2014


On 7/15/2014 9:30 PM, Mike wrote:
> On Tuesday, 15 July 2014 at 19:48:23 UTC, Walter Bright wrote:
>
>> The one thing peek/poke doesn't offer is transitivity. C/C++ don't offer
>> volatile transitivity either. I'm not at all sure that anyone builds a data
>> structure in memory-mapped registers, so I'm not convinced this is a need.
>
> Here's an example from the STM32F4 ARM Cortex-M peripheral library:
>
> #define __IO volatile
>
> /**
>    * @brief DMA Controller
>    */
>
> typedef struct
> {
>    __IO uint32_t CR;     /*!< DMA stream x configuration register     */
>    __IO uint32_t NDTR;   /*!< DMA stream x number of data register     */
>    __IO uint32_t PAR;    /*!< DMA stream x peripheral address register */
>    __IO uint32_t M0AR;   /*!< DMA stream x memory 0 address register   */
>    __IO uint32_t M1AR;   /*!< DMA stream x memory 1 address register   */
>    __IO uint32_t FCR;    /*!< DMA stream x FIFO control register     */
> } DMA_Stream_TypeDef;
>
> typedef struct
> {
>    __IO uint32_t LISR;   /*!< DMA low interrupt status register,    Address
> offset: 0x00 */
>    __IO uint32_t HISR;   /*!< DMA high interrupt status register,    Address
> offset: 0x04 */
>    __IO uint32_t LIFCR;  /*!< DMA low interrupt flag clear register,  Address
> offset: 0x08 */
>    __IO uint32_t HIFCR;  /*!< DMA high interrupt flag clear register, Address
> offset: 0x0C */
> } DMA_TypeDef;
>
> There are several hundreds lines of this stuff in the library. It is used to
> model register banks.  Each register bank roughly corresponds to one of the
> embedded peripherals.

There are no pointers to pointers there, so there is no transitivity issue. A 
single volatile reference to the whole struct will suffice.



More information about the Digitalmars-d mailing list