Programming embedded methods: Arrays and pointer arithmetic

This second installment of the “Programming embedded methods” sequence covers arrays and pointer arithmetic, however clearly, with an embedded twist.

Welcome to the second installment of the “Programming embedded methods” sequence. As we speak’s topic, lined in lesson 7, is arrays and pointer arithmetic, however clearly, with an embedded twist.

Lesson 7 – Arrays and pointer arithmetic

The motivating instance for this lesson is the GPIO (Basic-Objective Enter/Output) peripherals within the TivaC microcontroller, which you should program to be able to management the composite LED on the LaunchPad board. The TivaC GPIO design is somewhat uncommon as a result of it permits impartial addressing of each potential mixture of the 8-bits within the GPIO group. This elaborate design, referred to as “bit masking by way of handle strains” within the TivaC Datasheetrequires 256 separate addresses (and presents a superb case for making use of arrays in C).

What Is the Downside?

The primary apparent query is why would the {hardware} designers of the TivaC GPIO go into such bother. In spite of everything, they might merely deal with the 8-bits in a GPIO group like a byte in RAM, which you would then manipulate within the standard method. In reality, they already present it. For instance, right here is the code from the earlier lesson 6 that units the bit similar to the crimson LED, leaving all different bits within the GPIO_PORTF group unchanged:

GPIO_PORTF_DATA_R |= LED_RED;

I suppose that is how most of us are used to doing this. However it seems that such a “standard” code represents a read-modify-write sequences. The sequence could be problematic, particularly within the load-store CPU structure like ARM, the place the code compiles into three directions:

  • Load the earlier worth of the GPIO knowledge right into a CPU register,
  • Apply bitwise OR to set the bit within the CPU register,
  • Retailer the consequence from the CPU register to the GPIO knowledge.

A sequence like that may be bother whether it is interrupted, and the interrupt (or a separate RTOS job) additionally modifies the identical GPIO_PORTF group, though maybe different bits in it.

Atomic Entry

The TivaC {hardware} designers have been clearly conscious of such concurrency hazards, and due to this fact they devised a strategy to replace the person GPIO bits with out the dreadful read-modify-write sequence. As a substitute, not solely each GPIO bit, however even each mixture of the 8 GPIO bits, could be up to date with one STR instruction. A single instruction is of course atomic and eliminates concurrency hazards.

Past Bit-Banding

Some YouTube viewers commented that the TivaC GPIO design is simply one other instance of “bit-appeal” of the peripheral reminiscence area within the Cortex-M4. Bit-banding is predicated on an identical thought of ​​reminiscence aliasing, the place every bit within the main area has a separate handle within the secondary, aliased area. However for bit-banding of the 8 GPIO bits, you would want solely 8 addresses. TivaC GPIO goes past that by offering 256 addresses, which let you atomically set any mixture of the 8 GPIO bits.

Finish Notes

An essential takeaway from this lesson is that peripheral registers aren’t reminiscence, though the CPU accesses them utilizing the identical load and retailer directions for accessing reminiscence. For instance, writing to many of the 256 GPIO registers doesn’t learn again as the identical worth. Some peripheral registers can’t be learn in any respect (write-only registers), which by no means occurs with “actual” reminiscence!

Within the subsequent lesson, you’ll study features and see how they work on the low degree. Keep tuned!


Dr. Miro M. Samek is the creator of the open supply QP real-time embedded frameworks and the freeware QM graphical model-based design instrument. He’s additionally the founder and CEO of Quantum Leaps — the supplier of contemporary embedded software program based mostly on lively objects and hierarchical state machines in addition to instruments for visible modeling, computerized code era, and unit testing of deeply embedded software program. Miro teaches the favored YouTube “Fashionable Embedded Programs Programming” video course on which this text sequence is predicated.

Associated Contents:

For extra Embedded, subscribe to Embedded’s weekly e-mail e-newsletter.