![]() The reset values are usually all zeros for those registers, with the exception of some debugging pins like A13 and A14. The ‘O’ in those register names stands for ‘Output’. The GPIO_OSPEEDR and GPIO_OTYPER settings are only used when the pin is set to ‘Output’ mode in GPIO_MODER. There are four different registers which work together to set pin functions – they are described in section 8 of the reference manual, but here are the basics for each one: With the peripheral initialized, we need to set up each individual pin – they default to ‘input mode’ with no pull-up/down resistors. If we wanted to use a pin in the GPIOA bank, we would also need to set the RCC_AHBENR_GPIOAEN bit. If try to use a peripheral without first enabling its ‘clock’ in the RCC registers, it will not work, and the GPIO pins are no exception. Those peripheral names are defined in the stm32f031圆.h file that we included earlier, and this line must be included early in the main method. For GPIOB, that looks like this: // Enable the GPIOB peripheral in 'RCC_AHBENR'. By default, power is not applied to most of the chip’s peripherals for energy efficiency, so we need to turn them on by setting the appropriate bit in the RCC “clock enable” registers. Before we use the GPIOB peripheral, we need to tell the chip to turn it on. With ST’s device files included, we can reference peripheral registers by name instead of address in our ‘main.c’ file. ![]() Pin B3 is connected to the on-board LED on ST’s “Nucleo” boards. After the includes, we can define which GPIOB pins will be used for the button and LED. The stdint.h header defines standard integer types like uint32_t, and the stm32f031圆.h file is the device header file discussed above. If it has already been defined and included, then the #ifndef will return false and the file will be ignored. If it hasn’t been processed yet, the #ifndef (‘if not defined’) will evaluate true and the file will be defined and included. The #ifndef/#define/#endif statements are a common pattern in C programming they prevent the file from being included more than once. Define GPIOB pin mappings for our LED and button. Now that we have external files to include, it makes sense to define a main.h header file for those sorts of definitions: #ifndef _VVC_MAIN_H I included it as an empty file because the standard stm32f031圆.h file depends on it. The system_stm32f0xx.h file is included by ST’s files because it is used in most of their auto-generated code, but its typical contents are not relevant to our low-level project. ![]() The stm32f0xx.h file provides general definitions shared across the entire STM32F0 line of chips, and the stm32f031圆.h file has the definitions specific to our STM32F031K6 chip. Some of the device header files – like cmsis_gcc.h and the ones starting with core_cm – are “ CMSIS” files which provide some common ARM Cortex-M definitions and things like software helpers for things like setting up hardware interrupts. These supporting files are all located in the device_headers directory in the repository that I’ll link to at the end of this post. The bad news is, the definitions are all written as confusing acronyms, so it’s helpful to have your chip’s reference manual handy to check what the jumbles of letters mean. ![]() The companies which make these chips – ST Microelectronics in this case – usually also provide free header files which have definitions for which memory addresses go to which peripherals, and which bits do what. For example, in this tutorial we will set a bit to 1 in an “Output Data Register” to pull a pin’s voltage high enough to turn on an LED, and reset the same bit to 0 to pull the pin to ground and turn the LED off. The way that we read and program these peripherals is to check and set certain values at specific memory addresses. They can do things like speak common communication protocols with other chips, run actions on timers, and send general-purpose signals to the pins connected to the chip. It has a number of hardware “Peripherals” to make it easier for you write programs which interact with the real world. The STM32 chips have a lot of functionality which is not easy to represent in the standard C language. The resistor and capacitor are both optional – they’re just a very simple form of debouncing. You can find the actual pin mappings in section 6.11 of this reference document, or they’re also printed on the informational card that comes with the board. Strangely, the B1 pin is labeled ‘D6’ on the Nucleo boards I think that ST wanted to use the same footprint and labeling as the popular Arduino Nano. The 100nF capacitor across the button should help reduce noise, one side of the button connects to ground through a jumper wire, and I put a 470Ω resistor between the other side of the button and pin B1. The green ‘LD3’ LED is attached to pin B3 on the board. ‘Nucleo’ STM32F031K6 board with a button.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |