I am evaluating Freescale's Kinetis Design Studio for their ARM series microcontrollers. I was looking at an example as a basis to do my first "blink an LED" project. When using variables shared between my main program and an ISR such as a counter, I would typically define a volatile global in main.c and reference it as extern in the ISR. Their example shows it exactly the opposite and they also don't use the volatile keyword. I have never seen it done this way. Is there an advantage to this? BTW, my program works just fine either way.
C Global declared in ISR
570 views Asked by john8791 AtThere are 2 answers
too honest for this site
On
I do not know the codewarrior suite. However, in general, volatile tells the compiler the variable is changed outside normal control flow as given by the program code.
Historically, embedded compilers were quite forgiving about not using 'volatile' and/or implemented some strategies to support inexperienced programmers who did not know about optimizations and "forgot" volatile. This will result, however, in badly optimized code, expecially for the ARM platform (it was less of a problem with HC08&Co MCUs which had to load every variable from memory anyway).
While it might be that CW is still that tolerant, compilers which highly optimize code like gcc are not that forgiving and optimise much more radical. If you forget volatile or barriers for such compilers, you might end up with an empty main-loop, for instance.
Even if your code runs fine for now, this might change once you enable optimizations or change a minor (possibly unrelated) aspect.
Very well meant advice from an long-term embedded developer: use volatile. Use it sparsely and well thought (more details can be found here), but use it!
Related Questions in C
- How to call a C language function from x86 assembly code?
- What does: "char *argv[]" mean?
- User input sanitization program, which takes a specific amount of arguments and passes the execution to a bash script
- How to crop a BMP image in half using C
- How can I get the difference in minutes between two dates and hours?
- Why will this code compile although it defines two variables with the same name?
- Compiling eBPF program in Docker fails due to missing '__u64' type
- Why can't I use the file pointer after the first read attempt fails?
- #include Header files in C with definition too
- OpenCV2 on CLion
- What is causing the store latency in this program?
- How to refer to the filepath of test data in test sourcecode?
- 9 Digit Addresses in Hexadecimal System in MacOS
- My server TCP doesn't receive messages from the client in C
- Printing the characters obtained from the array s using printf?
Related Questions in ARM
- Jiobook flashing
- How to flush denormal numbers to zero for apple silicon?
- How to exploit Unified Memory in OpenCL with CL_MEM_ALLOC_HOST_PTR flag?
- ARM Assembly code is not executing in Vitis IDE
- Which version of ARM does the M1 chip run on?
- Vector by Scalar Division with -ffast-math
- Why veneer code generated by gcc for cortex-m0 seems 8-byte aligned?
- Getting almost random time stamp counter on ARM
- Portenta H7 Baremetal Development and a Little Guidance on Embedded System Learning Roadmap
- STM32 RTC3 Mixed Mode: Writing TR resets SSR
- Implementing Quick Sort Algorithm in Visual2 with armv7
- How can I create an Inline assembly command with a multi-variable register offset?
- Inquiry: ARM Compatibility for Puppeteer
- Confusion with thumb instructions while compiling recipe for cortexm4 CPU
- Difficulty understanding virtual LPIs in GICv3
Related Questions in EMBEDDED
- MSP430F5529 on the MSPEXP430F5529LP: UART is not actually transmitting despite seemingly correct setup. What is wrong?
- A FPGA Project Proposal where I can use both PS and PL
- Program doesn't run after DFU
- Sending struct through queue
- How to generate a VPI warpmap for polynomial distortion correction?
- How to present this example concept in UML: Using 2 LCD displays in C/C++
- CLion: Debug via St-Link
- Portenta H7 Baremetal Development and a Little Guidance on Embedded System Learning Roadmap
- STM32 RTC3 Mixed Mode: Writing TR resets SSR
- Unable to read value from gpio set as input
- Mbed TLS: in-place en-/decryption for OAEP doesn't seem to work
- Shared variable read from low priority thread in preemptive scheduling
- Own Pattern / framework for interfacing with components in C
- Performance Difference Between Global Variable and Local Variable in Embedded Systems
- Comparing analog signal from Electret mic with samples
Related Questions in CORTEX-M
- Why veneer code generated by gcc for cortex-m0 seems 8-byte aligned?
- STM32G030 refuses write to flash
- Unable to read value from gpio set as input
- Microcontroller hangs with LWIP UDP
- Can't connect ST-Link with APM32F003 through OpenOCD
- programming and debugging of different sam types
- VTOR not found in STM32F030
- Unit tests on registers with bare metal programming
- Force .bss section to be in last program header
- J-Link script to flash program in S32K144 (allow security)
- Cortex-M external interrupt occurs when executing fault handler with higher priority
- Why gcc is not using S16-S31 registers of Cortex M7?
- Issue with measuring ARM MCU interrupt latency
- What is the most efficient way to write two (for example) bits of a register using cortex-m0 instruction set?
- How to do unaligned int store on ARM Cortex M4?
Related Questions in CODEWARRIOR
- How do I use an external library path with C project in Neovim?
- Read Flash Memory using PEmicro and CodeWarrior IDE (MC9S12XET256 MCU)
- How do I account for the spaces in the code?
- How should I write my C code to have the resulting assembly use extra dsll32 and dsra32 instructions?
- Where does CodeWarrior keep rsp values?
- How to determine offset values when disassembly?
- TypeError: Cannot read property 'toUpperCase' of undefined |CodeWars|
- Need to return array of the top-3 most occurring words. (codewars )
- Sum of variabels from each for loop iterations
- Food Ranking List Challenge (TestDome)
- Receiving an exit code error on codewars when trying to manipulate a string
- Why is this code removing `False` from the array?
- Loop is stuck for some reason and then finally returns undefined
- What is the code to print text into a new line on assembly code, on HCS12 microcontroller using CodeWarrior?
- String incrementer codewars (Ruby) problem
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
The missing
volatileis an error (albeit a common one) that itself should be a quality warning. The choice of where to instantiate a global is arbitrary, but from a cohesion point of view, it makes sense to keep data that relates to the interrupt with the interrupt. That said, the use of the global data is itself an indication of dubious quality and practice.A better pattern that makes use of data encapsulation is:
interrupt.c
...
interrupt.h
main.c