Skip to main content

Stm32 Peripheral Drivers from Scartch : GPIO register overview

GPIO register overview

This is a brief overview of the GPIO registers. The video below will cover everything I am going to explain here with the slight difference that this text is more thought out and the video is impromptu. Nonetheless we're off to the races.

The STM32F1 series offers several GPIO ports starting from  GPIOA...through...GPIOG each port consists of 16 Pins indexed from 0-15. Depending on the specific F1 chip you have you may not have all 7 ports, for example my chip is a 48 pin package like the image below and as you can observe I do not have all Ports A through G. Make sure you google a Pinout of your specific chip for reference and save the image to keep handy. If you are using the same board I showed you off of ebay this is your pinout. 

GPIO Registers:

Name: GPIO Control Register LOW and GPIO Control Register HIGH 

Function: Used to configure the GPIO pins to input / output , speed and pull up/down internal resistors.

How to use it: The GPIO ControlRegister LOW and Control Register HIGH are the registers used to to configure the specific pin you are using.  They are identical in function so I have only included the picture of the LOW register above. As you can see above bits 0 and 1 are labeled MODE 0 , bits 2 and 3 are labeled CNF 0 . This pattern repeats through the entire register. The 0 in the label denotes the Pin it will configure, so MODE0 will configure the mode for pin 0 and CNF0 will also configure Pin0. This means that for every pin  there are 4 configuration bits:  2 for MODE and 2 for CNF , since each pin requires 4 bits this means this 32 bit register can only configure 8 pins before running out of space, this is why we have the other register the Control Register  HIGH. The difference is the LOW register will configure pins 0 - 7 and the HIGH register will configure pins 8- 15 . I hope this makes sense and if not feel free to ask questions via comments.

Name: GPIO Input Data Register 

Function: Used to read inputs on the pin.

This register is straight forward. When you set  your pin as input it is because a sensor or some other circuitry is inputting some sort of signal on your pin. This register is used to read the input. The bit and pin mapping is also straight forward. Bit 0 is equivalent to pin 0 and so on. 


Popular posts from this blog

Interface a Rotary Encoder the right way.

So whilst reading my favorite odyssey found here I stumbled onto something I never spotted before. It turns out that the general purpose timers support hardware interfacing with an incremental encoder. This means no more interrupts and no need to increment whatever variable you had. Now all you have to do is get the value from the Count register in the timer. Did I also mention it takes care of figuring out which direction you are turning it? Amazing! Lets talk about it.

Encoder Interface Mode Verbatim from the reference manual of the STM32F103 page 392 or Section 15.3.12  "Encoder interface mode acts simply as an external clock with direction selection. This 
means that the counter just counts continuously between 0 and the auto-reload value in the 
TIMx_ARR register (0 to ARR or ARR down to 0 depending on the direction). So the user 
must configure TIMx_ARR before starting. In the same way, the capture, compare, 
prescaler, trigger output features continue to work as normal."

STM32L0/F0/F3 I2C : Part 3 RX TX via DMA

Most modern microcontroller come with a peripheral called DMA which allows for an even more hands-off approach. The Direct Memory Access controller will get a tutorial of it's own in the future. However, it is so simple to use that I can easily explain the required bits in this tutorial without feeling like I will overwhelm the reader. In this iteration of the I2C series I will cover how to TX and RX date on the peripheral in conjunction with the DMA controller. 

First let me briefly explain in a high level what the DMA controller does. It allows you to transfer data in 3 ways: Peripheral to memoryMemory to peripheralMemory to memory. What does this mean? For example, when you receive data on I2C in your RXDR register, you can have the DMA controller move that data received into an array. At the end of the day your variables are just memory locations, so if you have declared an array it has an address associated with it, this is address is what the DMA considers  "Memory"…

STM32 I2C Does it suck?

Recently I have been annoyed with  bugs in the I2C implementation in ST's F1/L1/F40/F2 series, and  any microcontroller made by ST before 2012. There appears to be, what I can only describe as, a race condition when attempting to receive data in I2C interrupt(low priority)  or polling mode.

Just to be clear a race condition is defined as , verbatim from wiki: "the behavior of an electronics, software, or other system where the system's substantive behavior is dependent on the sequence or timing of other uncontrollable events. It becomes a bug when one or more of the possible behaviors is undesirable."
I was humming along trying to make my YouTube tutorials when I arrived to the I2C protocol implementation. I have used I2C with the F1 series plenty of times, however I have never had the need to receive data from a slave. However for the sake of completeness I decided to implement an RXing routine in I2C for my tutorial. Only to find statements like this in the referen…