像STM8一样编程STM32(寄存器级GPIO) [英] Programing STM32 like STM8(register level GPIO )

查看:48
本文介绍了像STM8一样编程STM32(寄存器级GPIO)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我像PD_ODR_ODR4 = 1;一样编写了STM8 GPIO,但是stm32f10x.h没有这个功能.有没有定义位的.h文件.

I programmed STM8 GPIO like PD_ODR_ODR4 = 1; but stm32f10x.h doesn't have this function.Is there any .h file that has definition for bits.

抱歉,我不知道如何更好地解释这个问题.

Sorry but I don't know how to explain this problem better.

我尝试了多个 GPIO 库.

I tried multiple GPIO libraries.

强文本

推荐答案

你在问题​​中提到了 stm32f10x.h,所以我假设它是关于 STM32F1 系列控制器的.其他系列有一些差异,但大体程序是一样的.

You mention stm32f10x.h in the question, so I'm assuming it's about the STM32F1 series of controllers. Other series have some differences, but the general procedure is the same.

GPIO 引脚排列在 16 个被调用端口的组中,每个端口都有自己的一组控制寄存器,命名为 GPIOAGPIOB 等.它们被定义为指向GPIO_TypeDef 结构.影响引脚输出的控制寄存器有 3 个.

GPIO pins are arranged in banks of 16 called ports, each having it's own set of control registers, named GPIOA, GPIOB, etc. They are defined as pointers to GPIO_TypeDef structures. There are 3 control registers that affect pin outputs.

写入 ODR 一次设置所有 16 个引脚,例如GPIOB->ODR = 0xF00F 设置引脚 B0B3B12B15> 为 1,B4B11 为 0,无论它们之前的状态如何.可以写 GPIOD->ODR |= (1<<4) 来设置 pin GPIOD4 为 1,或者 GPIOD->ODR &=~(1<<4) 重置它.

Writing ODR sets all 16 pins at once, e.g. GPIOB->ODR = 0xF00F sets pins B0 through B3 and B12 through B15 to 1, and B4 through B11 to 0, regardless of their previous state. One can write GPIOD->ODR |= (1<<4) to set pin GPIOD4 to 1, or GPIOD->ODR &= ~(1<<4) to reset it.

Writing BSRR 将写入的值视为两个位掩码.低半字是设置掩码,值为 1 的位将 ODR 中的相应位设置为 1.高半字是复位掩码,值为 1 的位将 ODR<中的相应位设置为/code> 为 0.GPIOC->BSRR = 0x000701E0 将设置引脚 C5 虽然 C8 为 1,重置 C0C2 为 0,并保留所有其他端口位.在写BSRR时尝试设置和重置相同的位,然后它会被设置为1.

Writing BSRR treats the value written as two bitmasks. The low halfword is the set mask, bits with value 1 set the corresponding bit in ODR to 1. The high halfword is the reset mask, bits with value 1 set the corresponding bit in ODR to 0. GPIOC->BSRR = 0x000701E0 would set pins C5 though C8 to 1, reset C0 through C2 to 0, and leave all other port bits alone. Trying to both set and reset the same bit when writing BSRR, then it will be set to 1.

BRR和在BSRR里写reset bitmask是一样的,即GPIOx->BRR = x 等价于 GPIOx->BSRR = (x <<16).

Writing BRR is the same as writing the reset bitmask in BSRR, i.e. GPIOx->BRR = x is equivalent to GPIOx->BSRR = (x << 16).

现在可以写一些宏了

#define GPIOD_OUT(pin, value) GPIOD->BSRR = ((0x100 + value) << pin)
#define GPIOD4_OUT(value) GPIOD_SET(4, value)

改变单个引脚,但它不像它那样灵活,例如您不能获取单个引脚的地址并将其作为变量传递.

to change single pins, but it's not as flexible as it could be, e.g. you cannot take the address of a single pin and pass it around in variables.

位带

Cortex-M 控制器(不是全部,但 STM32F1 系列有)具有此功能,可以使内部 RAM 和硬件寄存器中的单个位可寻址.0x40000000-0x400FFFFF 范围内的每一位都映射到 0x42000000-0x43FFFFFF 范围内的一个完整的 32 位字.它不适用于此地址范围之外的外围设备,例如 USB 或 NVIC.

Cortex-M controllers (not all of them, but the STM32F1 series do) have this feature to make individual bits in internal RAM and in hardware registers addressable. Each bit in the 0x40000000-0x400FFFFF range is mapped to a full 32-bit word in the 0x42000000-0x43FFFFFF range. It doesn't work with peripherals outside this address range, like USB or NVIC.

外设寄存器的位带地址可以用这个宏计算

The bit-banding address of a peripheral register can be calculated with this macro

#define BB(reg) ((uint32_t *)(PERIPH_BB_BASE + ((uint32_t)&(reg) - PERIPH_BASE) * 32U))

并且您可以将结果指针视为包含 32 个字的数组的基数,每个字对应于外围寄存器中的一个位.现在可以

and you can treat the resulting pointer as the base of an array holding 32 words, each corresponding to a single bit in the peripheral registers. Now it's possible to

#define PD_ODR_ODR4 (BB(GPIOD->ODR)[4])

并在作业中使用它.读取它将给出 0 或 1 作为其值,写入它的值将写入值的最低有效位复制到外围寄存器位.您甚至可以获取它的地址,并将其传递给一个对 pin 执行某些操作的函数.

and use it in assignments. Reading it will give 0 or 1 as its value, values written to it copy the least significant bit of the written value to the peripheral register bit. You can even take its address, and pass it to a function that does something with the pin.

位带记录在 PM0056 Cortex®-M3 编程手册中.

Bit-banding is documented in PM0056 Cortex®-M3 programming manual.

这篇关于像STM8一样编程STM32(寄存器级GPIO)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆