December 6, 2022

ENACALCULATOARE

Develop Technology For The Connected World

Programming embedded systems: Arrays and pointer arithmetic

3 min read

This next installment of the “Programming embedded systems” series handles arrays and pointer arithmetic, but definitely, with an embedded twist.

Welcome to the second installment of the “Programming embedded systems” collection. Today’s subject, lined in lesson 7, is arrays and pointer arithmetic, but obviously, with an embedded twist.

Lesson 7 – Arrays and pointer arithmetic
https://www.youtube.com/observe?v=pQs8vp7JOSk

The motivating illustration for this lesson is the GPIO (General-Purpose Enter/Output) peripheral in the TivaC microcontroller, which you have to have to system in purchase to manage the composite LED on the LaunchPad board. The TivaC GPIO design and style is instead strange simply because it permits unbiased addressing of each and every achievable mix of the 8-bits in the GPIO team. This elaborate design, termed “bit masking as a result of handle lines” in the TivaC Datasheet, calls for 256 different addresses (and features an superb situation for implementing arrays in C).

What Is the Challenge?

The very first apparent question is why would the components designers of the TivaC GPIO go into these types of difficulties. After all, they could only address the 8-bits in a GPIO group like a byte in RAM, which you could then manipulate in the usual way. In truth, they currently provide it. For example, right here is the code from the preceding lesson 6 that sets the little bit corresponding to the pink LED, leaving all other bits in the GPIO_PORTF group unchanged:

GPIO_PORTF_Info_R |= LED_Purple

I suppose this is how most of us are utilized to carrying out this. But it turns out that this sort of a “usual” code signifies a read through-modify-publish sequence. The sequence can be problematic, primarily in the load-store CPU architecture like ARM, exactly where the code compiles into 3 instructions:

  • Load the earlier benefit of the GPIO details into a CPU sign-up,
  • Implement bitwise OR to set the little bit in the CPU sign up,
  • Shop the final result from the CPU sign-up to the GPIO details.

A sequence like that can be issues if it is interrupted, and the interrupt (or a independent RTOS activity) also modifies the very same GPIO_PORTF team, despite the fact that maybe other bits in it.

Atomic Entry

The TivaC hardware designers had been clearly mindful of these concurrency dangers, and consequently they devised a way to update the personal GPIO bits without the dreadful examine-modify-create sequence. Alternatively, not only each individual GPIO little bit, but even each individual combination of the 8 GPIO bits, can be up to date with 1 STR instruction. A solitary instruction is normally atomic and eliminates concurrency hazards.

Outside of Bit-Banding

Some YouTube viewers commented that the TivaC GPIO style is just one more instance of “bit-banding” of the peripheral memory area in the Cortex-M4. Bit-banding is centered on a identical notion of memory aliasing, the place just about every little bit in the primary area has a different deal with in the secondary, aliased region. But for bit-banding of the 8 GPIO bits, you would have to have only 8 addresses. TivaC GPIO goes over and above that by providing 256 addresses, which allow you to atomically set any mixture of the 8 GPIO bits.

Conclusion Notes

An critical takeaway from this lesson is that peripheral registers are not memory, even though the CPU accesses them making use of the very same load and retail outlet directions for accessing memory. For case in point, writing to most of the 256 GPIO registers does not browse back as the exact same benefit. Some peripheral registers cannot be examine at all (write-only registers), which under no circumstances occurs with “real” memory!

In the future lesson, you will understand about functions and see how they operate at the minimal stage. Continue to be tuned!


Dr. Miro M. Samek is the creator of the open supply QP serious-time embedded frameworks and the freeware QM graphical model-primarily based style software. He is also the founder and CEO of Quantum Leaps — the supplier of contemporary embedded application based on energetic objects and hierarchical condition devices as perfectly as applications for visible modeling, automatic code generation, and unit screening of deeply embedded software program. Miro teaches the well-liked YouTube “Modern Embedded Devices Programming” video clip course on which this post sequence is primarily based.

Linked Contents:

For a lot more Embedded, subscribe to Embedded’s weekly email e-newsletter.



Copyright © All rights reserved. | Newsphere by AF themes.