Understanding the STM32F0's GPIO
Understanding the STM32F0's GPIO
This is the first part of the GPIO tutorial for the STM32F0Discovery.
The tutorial will include the following topics:
- Understanding the Structure of the GPIO Registers on the STM32F0 Microcontroller
- Understanding how to access the registers
- Setting and clearing output pins
The STM32F051 microcontroller on the STM32F0Discovery board
has 5 general purpose input/output (GPIO) ports named Port A, B, C, D and F.
Each port can have up to 16 pins, and each port has associated with it the following set of registers:
- GPIO port mode register (GPIOx_MODER)
- GPIO port output type register (GPIOx_OTYPER)
- GPIO port output speed register (GPIOx_OSPEEDR)
- GPIO port pull-up/pull-down register (GPIOx_PUPDR)
- GPIO port input data register (GPIOx _IDR)
- GPIO port outp ut data register (GPIOx_ODR)
- GPIO port bit set/reset register (GPIOx _BSRR)
- GPIO port configuration lock register (GPIOx_LCKR)
- GPIO alternate function low register (GPI Ox_AFRL)
- GPIO alternate function high register (GPIOx_AFRH)
- GPIO Port bit reset register (GPIOx_BRR)
where the 'x' in each register name acronym represents the port i.e.
the GPIOx_MODER associated with port A is called GPIOA_MODER.
Let's take a closer look at each register:
GPIO port mode register (GPIOx_MODER)
This is a 32-bit register where each set of two consecutive bits represent the mode of a single I/O pin.
For example bits 0 and 1 of the MODER register associated with GPIOC (GPIOC_MODER),
represent the mode of GPIO pin PC0 and bits 26 and 27 of the same register represent the mode of GPIO pin PC13.
These two bits can be set to:
- '00'-> input mode, which allows the GPIO pin to be used as an input pin,
- '01'-> Output mode, which allows the GPIO pin to be used as an output pin,
- '11'-> Analog mode, which allows the GPIO pin to be used as an Analog input pin and finally,
- '10'-> Alternate function mode which allow the GPIO pins to be used by peripherals such as the UART, SPI e.t.c.
It is important to note that if a pin's MODE is set to alternate function,
any GPIO settings for that pin in the GPIO registers will be overridden by the peripheral.
GPIO port output type register (GPIOx_OTYPER)
This is a 16-bit register where each bit denotes the 'type' of a single pin in the register.
This register sets the type of output pins to either push-pull or open drain.
For example if pin PC7 is configured as an output pin,
clearing bit 7 (or leaving its state at zero) of the OTYPER register associated with GPIOC (GPIOC_TYPER),
will set the output type of the GPIO output pin PC7 to "Push-Pull".
- GPIO port output speed register (GPIOx_OSPEEDR)
This is a 32-bit register where each set of two bits represent the speed of a single output pin.
For example bits 0 and 1 of the OSPEEDR register associated with port C (GPIOC_OSPEEDR),
represent the speed setting of the output pin PC0 and bits 26 and 27 of the same register
represent the speed setting of the output pin PC13. These two bits can be set to:
- 'x0': 2MHz Low speed
- '01':10MHz Medium speed
- '11': 50MHz High speed
So why have a speed setting on I/O ?
To save power.
On the 2MHz setting the GPIO would consume less current than on the 50MHz setting
I'd imagine but would have relatively longer rise/fall time specs.
The User Manual for the STM32F0 claims that the the output pins fastest toggle speed is every two clock cycles.
Assuming a maximum operation speed of 48MHz, the fastest toggle speed for the GPIO on the STM32f0 is 24 MHZ,
which means the highest frequency square wave that can be produced by the GPIO is 12MHz.
NOTE:
A quick look at the default startup code found in"system_stm32f0xx.c"
will identify that the microcontroller is indeed operating at a maximum speed of 48MHz at startup.
In a future entry I will demonstrate how this speed can be changed.
- GPIO port pull-up/pull-down register (GPIOx_PUPDR)
The GPIOx_PUPDR registers configures the internal pull-ups and pull-down resistors on each I/O pin.
The internal pull-up/down resistors can be configured on GPIO pins set as input or output
(though I'd imagine they'd be more popular on input pins).
The Pullup/down resistors have a typical value of 40KOhms but can range from 30-50Kohms.
Again each two consecutive bits represent the internal pull-up/down resistor setting for each pin within a single port.
- GPIO port input data register (GPIOx _IDR)
This is a 16-bit read-only register. Each bit represents the input value on a corresponding pin.
Reading a '0' in bit 8 of this GPIOC _IDR register indicates that the voltage on PC8 is 0V (GND).
While reading a '1' in bit 8 of this GPIOC _IDR register indicates that the voltage on PC8 is 3.3V (VDD)
GPIO port output data register (GPIOx_ODR)
This is a 16-bit read/write register. Each bit represents the output value on a corresponding pin.
Writing a '0' in bit 8 of this GPIOC _ODR register indicates that the voltage on PC8 is driven by the micro to 0V (GND).
While writing a '1' in bit 8 of this GPIOC _ODR register indicates that the voltage on PC8 is driven by the micro to 3.3V (VDD).
Writing to the ODR register is good if you want to write to the entire port.e.g.
GPIOC->ODR = 0xF0FE
The above statement changes the state of every pin on the GPIOC peripheral from its previous (and now discarded) state,
to the one indicated by the statement; 0xF0FE (0b1111000011111110).
However if you want to set only a single pin;
lets say PC8 without affecting the state of the rest of the pins on GPIOC,
you have to perform a read-modify-write (RMW) access.
To set pin PC8 independent of all other pins on GPIOC (RMW) you could use:
GPIOC->ODR |= 0x00000100; //( 0b00000000000000000000000100000000)
To clear pin PC8 independent of all other pins on GPIOC (RMW) you could use:
GPIOC->ODR &= ~(0x00000100); //( 0b00000000000000000000000100000000)
This works just fine, but you have to read the ODR register,
OR (|) or AND(&) (modify) it with a mask
and then write it back to the ODR register.
This means that at the assembly language level, at least three instructions
are used to set/clear an I/O which can significantly slow down toggling speed.
A better way would be to use the BSRR register and the BRR registers for setting and clearing pins.
They enable 'atomic' access that allows the I/O pin to be clear/set in as short a time as possible.
GPIO port bit set/reset register (GPIOx _BSRR)
Note: If both BSx and BRx are set, BSx has priority.
As mentioned in the preceding paragraph the BSRR register allows us to set/clear a particular pin (or groups of pins)
while preserving the state of the rest of the pins on a GPIO peripheral atomically
i.e. a fast as possible, without having to resort to the slower read-modify-write (RMW) accesses.
The least significant 16 bits are used to atomically set pin values to VDD
whereas the most significant 16 bits are used to atomically clear pin values to GND.
So if I wanted to set PC8 independent of all other pins on GPIOC I could use:
GPIOC->BSRR = 0x00000100;//( 0b00000000000000000000000100000000)
or
GPIOC->BSRR = (1<<8);
To clear pin PC8 independent of all other pins on GPIOC you could use:
GPIOC->BSRR = 0x01000000; //( 0b00000001000000000000000000000000)
or
GPIOC->BSRR = (1<<24);
Notice how in both scenarios a simple assignment operator '=' (atomic)
was used rather than an '|=' or an '&=' which denote RMW accesses.
Furthermore, note that to clear the pin value of PC8 to GND,
I had to set the 24th bit in the BSRR (8th bit of the most significant 16 bits).
While to set the pin value of PC8 to VDD, I had to set the 8th bit in the BSRR.
The awkwardness of atomic clearing being mapped to the most significant 16-bits of the BSRR register
is compensated for by the inclusion of the BRR register.
The BRR register maps the most significant 16-bits of the BSRR register into itself.
So to clear pin PC8 independent of all other pins on GPIOC you could use:
GPIOC->BRR = (1<<8);
Finally there are three more registers;
the GPIOx_AFRH, GPIOx_AFRL, and the GPIOx_LCKR registers.
The first two allow GPIO pins to be used for alternate functions.
There is a really neat pin muxing mechanism that allows each GPIO to be mapped
to multiple alternate functions depending on how these two registers are set.
I will spend more time on the AFRL/AFRH registers in future entries.
The last GPIOx_LCKR register can be used once GPIO
is configured to 'lock' the configuration so that it does not change until the micro is reset.
I encourage you to look up these three registers in the user manual.
Understanding the STM32F0's GPIO的更多相关文章
- STM32f10xxx 之 GPIO口配置
背景 配置stm32f103使其完成PWM输出的过程中,在配置GPIO口的时候,按照习惯配置GPIO口的speed为50MHZ,突然就意识到,为什么大部分例程习惯配置为50MHZ,而不是其它值,即有了 ...
- STM32F0单片机基于Hal库温控智能风扇
一.项目概述 设计采用STM32F0系列单片机做主控芯片,通过DHT11采集温湿度,将温度显示在OLED 屏幕上.根据温度的不同,利用STM32对风扇进行调速,总体硬件设计如下图所示 1.效果展示 2 ...
- [转]: stm328种GPIO模式
[原创]:这段时间开始研究stm32,今天撸着一段代码一直追,追到了GPIO口模式的枚举类型这里,遂去网上查看这8种模式到底是什么,网上一查,看到了一个答案被很多博主转载或者原创,那我也就不重复废话了 ...
- 基於tiny4412的Linux內核移植--- 中斷和GPIO學習(3)
作者 彭東林 pengdonglin137@163.com 平臺 tiny4412 ADK Linux-4.4.4 u-boot使用的U-Boot 2010.12,是友善自帶的,爲支持設備樹和uIma ...
- 基於tiny4412的Linux內核移植--- 中斷和GPIO學習(2)
作者 彭東林 pengdonglin137@163.com 平臺 tiny4412 ADK Linux-4.4.4 u-boot使用的U-Boot 2010.12,是友善自帶的,爲支持設備樹和uIma ...
- 基於tiny4412的Linux內核移植--- 中斷和GPIO學習(1)
作者 彭東林 pengdonglin137@163.com 平臺 tiny4412 ADK Linux-4.4.4 u-boot使用的U-Boot 2010.12,是友善自帶的,爲支持設備樹和uIma ...
- android gpio口控制
android gpio口控制 GPIO口控制方式是在jni层控制的方式实现高低电平输出,类似linux的控制句柄方式,在linux系统下将每个设备看作一个文件,android系统是基于linux内 ...
- STM32F412应用开发笔记之二:基本GPIO控制
NUCLEO-F412ZG板子上的元器件并没有完全焊接,除去ST-LINK部分和电源部分后,还有用一个USB主机接口,三个LED灯和两个按钮,不过很多功能引脚都已经引到了插针.查看原理图可发现,由原理 ...
- 通过数组和枚举简化GPIO操作编码
在工作中,经常遇到大量使用GPIO作为数字量输入输出来控制设备或采集状态,每次定义操作不同的GPIO针脚既麻烦又容易出错,于是就想要简化操作过程.对于数字量输入来说就是采集对应针脚的状态:而输出则是根 ...
随机推荐
- 训练赛第二场E题 Cottage Village
题目大意:在一条X轴上,有若干个正方形,并且保证这些正方形的中心都在X轴上,然后输入n个正方形的中心的X坐标,和正方形的边长,现在要再插入一个正方形,要求是,新插入的正方形至少要有一条边与原来的正方形 ...
- HDU 1728 逃离迷宫 BFS题
题目描述:输入一个m*n的地图,地图上有两种点,一种是 . 表示这个点是空地,是可以走的,另一种是 * ,表示是墙,是不能走的,然后输入一个起点和一个终点,另外有一个k输入,现在要你确定能否在转k次弯 ...
- MD5小彩虹表
为方便日常查询,需要一个MD5小彩虹表,当然网上有比较多的这样的查询站点,但感觉最近使用起来十分不便. 因此,编写一个小程序,用来查询一些经常出现的MD5,也即弱口令MD5查询.采用python3编写 ...
- JQuery插件autocomplete使用说明文档
项目中有时会用到ajax自动补全查询,就像Google的搜索框中那样,输入汉字或者字母的首个字母,则包含这个汉字或者字母的相关条目会显示出来供用户选择,该插件就是实现这样的功能的.autocomple ...
- MVC Form验证 登陆和退出Cookies的设定和消除
红色部分为重点 1.webconfig配置 <system.web>节点下添加 <authentication mode="Forms"> <for ...
- 打包部署到tomcat
部署到tomcat的方法 注意:在eclipse 或 idea 上需要引入外部tomcat 1.将程序打成war包启动tomcat 2.将target 文件下内容压缩城zip,发布到tomcat RO ...
- Oracle学习笔记:外连接(+)的用法
Oracle中常用 left join 和 right join 来进行外连接,同时,oracle也支持 (+) 的特殊用法,也是表示外连接,并且总是放在非主表的一方. 例如: 左外连接: selec ...
- linux pwd指令C实现
linux pwd指令C实现 研究实现pwd所需的系统调用 我们可以通过man命令和grep命令来获取我们所需要的系统调用函数信息 man chdir Linux pwd命令用于显示工作目录. 执行p ...
- c++ primer 学习杂记1
读到p483 公有,私有,受保护的继承. 1.关于基类成员在派生类中的访问级别: 1) 无论何种继承方式, 派生类都无法访问基类中的private成员. 2) 派生类可以限制,而不能放松对所继承成员的 ...
- 关于NOIP2018初赛
题面 这次PJ初赛有点傻了,可能是因为兴华水土不服吧(在这荒度了六年级的光阴). 选择题 DDDBBAAAABABBBB 第四题 当时懵了,我啥也不知道,于是就开始蒙 A.LAN B.WAN C.MA ...