Lecture 1 - Intro to STM32
![[CPE 316 Intro GPIO RCC STM32.pptx]]
Overview
We'll be programming a Nucleoboard, containing 2 ARM core CPUs, with 80 MHz operation and ... (blah blah blah, look up STM32 Microcontroller STM32L476 on your own for more details).
This class is where CPE's really shine. All your hardware background (Computer Architecture included) and software (data structures, intro to programming) come together. This is the most "CPE" class.
Reading datasheets will be the biggest takeaways from this class. The STM32 has like 270 pages, mostly of electrical characteristics, mechanical tolerances, etc. There's a lot of traces (HW) to know about, and SW things too. There's an API for the board, but the goal of the class is to get into the nitty gritty to try to understand what's going on under the hood.
Takeaways During Lecture
- The biggest help you'll get is to 'aim' you towards more relevant information, in contrast to less helpful info.
- We covered the syllabus via this link
- Live Calendar link is here
GPIO
We're dealing with a 3.3V chip. When we write 1 to a pin, it'll go to 3.3V, and 0 to 0V. Consider these voltages for external thingies. If we read the pin, we need 3.3V to get a 1, otherwise a 0.
We don't want to have too many pins, as otherwise it'll make the PCB itself way too complicated. That's why certain pins are multifunctional (multiplexed). Certain pins can be an input IO, output IO, alternative function, or analog mode, which we look at later. Today we just look at input and output mode. Since there's 4 modes, we need two bits to say this mode.
If you look at the board, there's two pins per board, so we set these pins to their corresponding values when we want a certain mode. Recall from CPE 333, there's Memory-Mapped IO, where you write to an address that really goes to a multiplexor to the outside world. This is MMIO (Memory-Mapped IO). It could be at 0x400000000
or some big number like that. This is still how the industry does it. You can find the memory map of our peripherals on ![[CPE 316 Intro GPIO RCC STM32.pptx]] on slide 16. Peripherals start at 0x400000000
. But you don't have to memorize this, as the tools take care of this for you.
Pins are in PORTs of (up to) 16 pins (PA0, PA1, ... PA15) in LSB to MSB order. Likewise there's port B, C, D, E, ..., up to H. Clearly we don't have that number of pins on our board (we have like 64). Each of these PORTs have as:
- MODR: mode register (input, output, alternative function, analog)
- PUPDR: pull up/down register (off, PU, PD). If a pin is in an undefined state (not necessarily 0 or 1), we can put a pull up resistor on the pin to make it
- IDR: input data register
- ODR: output data register
- BSRR: set/reset register (write only), sets or clears a bit
- BRR: reset register (write only), clears a bit
- These have to do with semaphores and race conditions, which we will see later.
- AFRL, AFRH, ASCR, LCKR, OTYPER, OSPEEDR, ... (see the others in the manual!!!)'
How do we know all of this? We go to our handy reference:
[[rm0351-stm32l47xxx-stm32l48xxx-stm32l49xxx-and-stm32l4axxx-advanced-armbased-32bit-mcus-stmicroelectronics.pdf]]
We can traverse the table of contents and find that we have our GPIO info in chapter 8. Section 5 have info on our registers, so we check that out.
[[rm0351-stm32l47xxx-stm32l48xxx-stm32l49xxx-and-stm32l4axxx-advanced-armbased-32bit-mcus-stmicroelectronics.pdf#page=304]]
We can read more information on this. Notice that if we want port A to have a pull down for bit 1, we set each rw
data bit to it's corresponding value.
Memory Map
This can be found on page 312-313. To write to these MMIO, we use use ARM assembly to write to mapped addresses. If you check out slide 22, notice that we can rewrite line 9 to:
// GPIOA is a pointer to a struct, so we need to dereference
GPIOA->MODER &= ~(0b110000000000);
// GPIO_MODER_MODE5 === some hex number shown above.
// All this does is clear the bits
All the definitions are given in the "main.h"
file that we include, where the preprocessor just replaces them with the numbers in question.
But why is Pin 13 of port C so important? It's connected to the Blue button on the board. Hence why we care when it's pressed.
Furthermore, the const
keyword in C means read-only, NOT that it's unchanging. The values of the registers themselves may change, so if, say, we pushed the button, some of the IDR values may change, even if the register itself is const
.
All things related to the microcontroller are in structures. We can access them like objects without methods.
The problem is that the header files of information is like 18000 lines, that we include. You should look at the file to find the values of these constants. They give the __IO
to indicate an input/output register type (which itself is defined in more detail). We can chase this forever, but you get how to read deeper and deeper down the rabbit hole for whatever purposes you have.
From now till Wednesday, do the following:
On Wednesday, we'll dive more into the GPIOs, as well as the clocking circuits on the inside.