Navigating Challenges in Embedded Systems with C Programming
on
For many years, the foundation of microcontroller programming has been Embedded C. In the area of embedded systems, it is the glue that holds hardware and software together. However, working with Embedded C is not always simple, as many engineers would confirm. Even seasoned professionals might fall victim to a number of frequent blunders and hazards. Our expert in this episode, Chris Rose, is the Director of Electric Innovation, an electronic and electrical design company. We discuss the realm of Embedded C.
Understanding the Difference between C and Embedded C
The first question on our agenda is understanding the difference between C and Embedded C. Chris explains that while C is a general-purpose programming language, Embedded C is essentially C with a specific mindset. Embedded C is intricately linked to the hardware of the microcontroller being used. This distinction is crucial for embedded software developers, as they work in an environment where every bit and clock cycle counts.The Landscape of C Standardization
Chris then goes on to highlight a few distinctions, mostly between hosted and freestanding C implementations. He points out that using compiler extensions or other workarounds is necessary because the standard C language does not have an official mechanism for handling interrupts or interacting with hardware. Chris also discusses the current status of the C language standardization, pointing out that while C99 is still a popular option because of superior support, C17 or C18 are the versions that are now in use. He offers observations on the accessibility and readability of C standards papers, which are frequently extremely detailed and written.The Role of Coding Standards
Chris emphasizes the importance of coding standards in embedded systems development. He recommends using coding standards like MISRA or Barr-C to maintain code quality, ensure consistency, and reduce the likelihood of bugs or errors. These standards provide rules and guidelines that help developers write clean, readable, and maintainable code. A suggestion is coding standards to help manage global variables, enforce coding style and best practices, and improve code quality. Adhering to such standards not only makes the code more reliable but also simplifies the debugging and maintenance process, which is crucial in embedded systems where reliability is paramount.Global Variables: When Are They Acceptable?
Chris discusses global variables in embedded systems and emphasizes the need to use them judiciously. He mentions that global variables can make code less readable and harder to maintain, and they can lead to unintended side effects or bugs. In most cases, it's better to use local variables within functions to encapsulate data and minimize global variable usage. He advises following coding standards like MISRA or Barr-C to restrict or manage global variables, keeping them to a minimum while ensuring they are well-documented and utilized only when necessary for inter-function communication or persistent data storage. This approach helps improve code maintainability and reduces the risk of bugs in embedded systems.Handling Floating-Point Variables in Embedded Systems
In embedded systems, handling floating-point variables has historically been avoided due to their potential slowness and non-deterministic behavior, but with more powerful microcontrollers equipped with hardware support for floating-point operations, this approach is changing. It is explained that the compiler's ability to leverage the floating-point unit (FPU) varies depending on the compiler, and with modern microcontrollers boasting higher clock speeds, the performance impact of floating-point operations has become less significant. However, the effectiveness of using the FPU still depends on the compiler's support and the need for specific compiler settings or pragmas to enable FPU usage.The Future of Embedded Programming
The episode ends on an intriguing note about the future of embedded programming. Is C still relevant, or are we on the cusp of a transition to a more modern language like Rust?Chris acknowledges that alternatives like Rust are emerging. He recommends that students consider learning Rust in addition to C and C++. He expresses concerns that Rust could potentially face similar challenges of non-standardization that C encountered due to the multitude of compiler variations.
For those interested in the full episode and further information, jump over to Elektor TV - Industry or watch below:
Hey - I have a great story...
Great! We'd love to hear it. Feel free to get in touch to learn how to share your news with us. We'll try to squeeze it in somewhere, whether a product launch, new technology, breakthrough, or event. Simply contact Stuart Cording via email, on Twitter, or LinkedIn.
Discussion (0 comments)