1

Specifically in Cortex-M4 (if that matters), when doing GPIO you need to write and/or read from GPIO's memory-mapped registers.

Before diving deeper into library code that handled this for me, I thought it was pure-hardware (say I wanted to set some pin HIGH, then there's a physical wire going from whatever bit in RAM that I toggle, to that pin. This way that bit would act as a switch). But after learning about the way it's done (activating some bit to set the pin HIGH, but activating some other bit to set the pin LOW), makes it feel a bit arbitrary, almost as if there's some parsing logic involved to understand what to do when some certain bit is activated.

So which is the case? Is this still pure-hardware with some complex circuitry or is there some sort-of a "nano-controller" within the peripheral that parses registers? (which just sets the problem back one-bit, how does this "nano-controller" communicate with real-world if microcontroller requires a peripheral to do so?)

zombiesauce
  • 113
  • 4
  • 2
    I think the term you seek is called "microcode". But I do not believe the STM32 uses any. Some do though but probably complex peripherals in larger more complex application processors like the A5 from TI rather than microcontrollers. Not GPIO. – DKNguyen Sep 17 '21 at 14:20
  • 1
    If memory serves, there are some very good figures showing just what's going on in each peripheral in the STM datasheets. – Scott Seidman Sep 17 '21 at 14:25
  • There is "address decode" logic, this is a hardware circuit and does not suggest any sort of "parsing" code. – Ben Voigt Sep 17 '21 at 15:10

2 Answers2

5

That GPIO pin logic is almost certainly done in hardware.

The reason they have different registers for set/clear/toggle a pin is to prevent read-modify-write problems. It also makes the software smaller.

The IO pin register is just a flip-flop circuit, and may have clear, set, and toggle inputs. When you write to a memory mapped register there is going to be a memory decode process in hardware that ultimately generates an enable signal for each address. In your processor they very likely just hooked the enable signals for the different register locations to the set, clear, and toggle inputs on the IO pin flip flop.

The advantage of doing it this way is that setting, clearing, or toggling an IO pin is now an atomic operation (so its thread safe and also safe in the case of other peripherals accessing the port). Using a normal read-modify-write setup to change a pin state is not.

Also writing a constant to a register is going to use less code than doing a full read-modify-write.

user4574
  • 11,816
  • 17
  • 30
1

There is no easy way to separate a function of a microcontroller as hardware and software. It mostly is a combination of both. But I consider all of that as hardware. The microcontroller is doing a job by decoding a value in some registers and mapping that to outputs, or mapping inputs to a register. I consider all of that as hardware. There are some functions like computing according to inputs, filtering, and etc. that are performed in your software. However, all that computation is just another instruction in some registers.

There are multiple configurations to use a peripheral. One of them uses Direct Memory Access (DMA) and puts/reads data directly between to/from memory and peripheral. But in most cases, the hardware pins are connected to some control registers via a multiplexer. For example, take a look at this (From here):

GPIO

It shows the internal connection of a GPIO pin. As you can see, this pin could be used as an analog or digital pin. In the case of a digital output pin, the state of the pin can be changed via writing to a register.

To set a pin as an output, first you define the pin as an output pin by selecting the multiplexer to be connected to the output register. Then, to set the output value, what the controller does is mapping a value from memory to this register. For example, when you want the state of high in the output, it puts a 1 (or zero depending on the architecture) to the output register bit. From this part on, the procedure is done in pure hardware; but managing what will be on the output register, is done via an instruction set that is defined in your software.

Saadat
  • 572
  • 3
  • 8