Become a leader in the IoT community!

New DevHeads get a 320-point leaderboard boost when joining the DevHeads IoT Integration Community. In addition to learning and advising, active community leaders are rewarded with community recognition and free tech stuff. Start your Legendary Collaboration now!

Step 1 of 5

CREATE YOUR PROFILE *Required

Change Email
OR
Step 2 of 5

WHAT BRINGS YOU TO DEVHEADS? *Choose 1 or more

Collaboration & Work 🤝
Learn & Grow 📚
Contribute Experience & Expertise 🔧
Step 3 of 5

WHAT'S YOUR INTEREST OR EXPERTISE? *Choose 1 or more

Hardware & Design 💡
Embedded Software 💻
Edge Networking
Step 4 of 5

Personalize your profile

Step 5 of 5

Read & agree to our COMMUNITY RULES

  1. We want this server to be a welcoming space! Treat everyone with respect. Absolutely no harassment, witch hunting, sexism, racism, or hate speech will be tolerated.
  2. If you see something against the rules or something that makes you feel unsafe, let staff know by messaging @admin in the "support-tickets" tab in the Live DevChat menu.
  3. No age-restricted, obscene or NSFW content. This includes text, images, or links featuring nudity, sex, hard violence, or other graphically disturbing content.
  4. No spam. This includes DMing fellow members.
  5. You must be over the age of 18 years old to participate in our community.
  6. Our community uses Answer Overflow to index content on the web. By posting in this channel your messages will be indexed on the worldwide web to help others find answers.
  7. You agree to our Terms of Service (https://www.devheads.io/terms-of-service/) and Privacy Policy (https://www.devheads.io/privacy-policy)
By clicking "Finish", you have read and agreed to the our Terms of Service and Privacy Policy.

Struggled with a Bug on My Energy Meter Project

Hello everyone, Good day.
I have been battling a bug for almost 24 hours on my Energy meter project and discovered the bug had to do with the “volatile” keyword. So basically there were some variables I declared to be used as counters, I observed that there were times when the system could transmit data over UART and then blink my LED once the counter reached a particular value. Something like
if(timer2_count > 50)
{

}
The condition becomes true and the block of code gets executed. Other times the block of code does not execute. After doing some research and consulting chat GPT, I discovered that the volatile keyword was not used, which was the issue. Funny enough, while I was using STM32 cube ide I never experienced such an issue, but it became an issue while using vs code. I just declared the variable using the volatile keyword, which has been consistent. Has anyone had such an issue before? I”‘d like to hear about our experiences with keywords like volatile

  1. 32bitSaviour#0000

    Compilers do have the liberty to rearrange code how they see fit in order to produce optimal executables. Some of these changes related to volatile would be the compiler changing, for example, the order of access of a variable or caching the value of a variable such that for successive reads the value is only fetched for the first read and the cache is used for any subsequent read. Now this sounds neat but there are cases where it becomes undesirable. Imagine a register in your UART changes but the variable you used to track it is cached.

    To avoid such nastiness, volatile is used to direct the compiler not to cache the values of the variable. It will generate code to take the value of the given volatile variable from the memory every time it encounters it. This mechanism is used because at any time the value can be modified by the OS or any interrupt. So using volatile will ensure accessing the value afresh every time.

  2. ZacckOsiemo#0000

    Since you started can you break down volatile pointers and volatile values inside pointers, such as cases where one uses volatile twice.

  3. 32bitSaviour#0000

    Certainly.
    1. So volatile values are used when the those values can change unexpectedly so we do not want the compiler to make any assumptions about the value of the variable.
    “`volatile uint8_t counter = 0;

    /// isr updates counter somewhere else

    
    2. Non-volatile pointer to volatile values. We can use this to read memory-mapped peripheral registers. We know that the pointer will not change but the values stored at that location change and compilers should not be allowed to make any assumptions and optimize things away.
    `uint32_t volatile *UART_BASE = (uint32_t *) 0x7200005E;`
    
    3. Volatile pointer to non-volatile values. A pointer can change but the value in the memory remains unchanged.
    `uint32_t *volatile ptr = data`. This can be useful when the pointer address might change in a ISR but the data itself is safe to assume as not changing.
    
    4. Volatile pointer to volatile values. Both the pointer and the value are assumed to change unexpectedly. This can be useful when working in embedded environments where for example dynamic memory may change during runtime.
    

    volatile int val = 0;
    volatile *int volatile ptr = val;
    “`

  4. Afuevu#0000

    Intreasting, thank you

CONTRIBUTE TO THIS THREAD

Browse other questions tagged