STM32G070RBT6基于Arduino框架GPIO输入输出模式
⛳STM32G070RBT6 GPIO输入输出
在LQFP-64封装当中,可用的IO引脚数量59个,可以说相当多。在Arduino框架下开发,GPIO输入输出引脚模式:
INPUT
(输入模式/浮空输入=INPUT_FLOATING
),INPUT_PULLUP
(输入上拉模式),INPUT_PULLDOWN
(输入下拉模式)orOUTPUT
(输出模式),INPUT_ANALOG
(模拟输入),还有就是OUTPUT_OPEN_DRAIN
(开漏输出)。
- 📋相关输入输出定义在文件:
C:\Users\Administrator\AppData\Local\Arduino15\packages\STMicroelectronics\hardware\stm32\2.3.0\cores\arduino\wiring_constants.h
头文件当中。
📖GPIO相关函数解析
🔖具体的相关函数定义在:
C:\Users\Administrator\AppData\Local\Arduino15\packages\STMicroelectronics\hardware\stm32\2.3.0\cores\arduino\wiring_digital.h
文件当中。
/**
* \brief Configures the specified pin to behave either as an input or an output.
* 设置GPIO引脚输入输出模式
* \param dwPin The number of the pin whose mode you wish to set
* \param dwMode Either INPUT, INPUT_PULLUP, INPUT_PULLDOWN or OUTPUT
*/
extern void pinMode(uint32_t dwPin, uint32_t dwMode) ;
/**
* \brief Write a HIGH or a LOW value to a digital pin.
* 写入对应的电平状态到指定GPIO引脚上
* If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the
* corresponding value: 3.3V for HIGH, 0V (ground) for LOW.
*
* \param dwPin the pin number
* \param dwVal HIGH or LOW
*/
extern void digitalWrite(uint32_t dwPin, uint32_t dwVal) ;
/**
* \brief Reads the value from a specified digital pin, either HIGH or LOW.
* 读取指定GPIO引脚状态
* \param ulPin The number of the digital pin you want to read (int)
*
* \return HIGH or LOW
*/
extern int digitalRead(uint32_t ulPin) ;
/**
* \brief Toggle the value from a specified digital pin.
* GPIO引脚状态翻转
* \param ulPin The number of the digital pin you want to toggle (int)
*/
extern void digitalToggle(uint32_t ulPin) ;
🌿通过上面的相关函数,我们可以了解到,和AVR单片机功能函数上基本一致,但是新增了一个状态翻转函数。以前的话需要
digitalWrite(uint32_t dwPin, !digitalRead(uint32_t dwPin)) ;
这样写才能实现。
📗GPIO高速模式
🔖具体的相关函数定义在:
C:\Users\Administrator\AppData\Local\Arduino15\packages\STMicroelectronics\hardware\stm32\2.3.0\cores\arduino\stm32\digital_io.h
文件当中。
- 📢在使用时,注意不要和普通模式的函数的形参混淆,否则编译会报错。
/**
* @brief This function set a value to an IO
* @param port : one of the gpio port
* @param pin : one of the gpio pin
* @param val : 0 to set to low, any other value to set to high
* @retval None
*/
static inline void digital_io_write(GPIO_TypeDef *port, uint32_t pin, uint32_t val)
{
if (val) {
LL_GPIO_SetOutputPin(port, pin);
} else {
LL_GPIO_ResetOutputPin(port, pin);
}
}
/**
* @brief This function read the value of an IO
* @param port : one of the gpio port
* @param pin : one of the gpio pin
* @retval The pin state (LOW or HIGH)
*/
static inline uint32_t digital_io_read(GPIO_TypeDef *port, uint32_t pin)
{
return LL_GPIO_IsInputPinSet(port, pin);
}
/**
* @brief This function toggle value of an IO
* @param port : one of the gpio port
* @param pin : one of the gpio pin
* @retval None
*/
static inline void digital_io_toggle(GPIO_TypeDef *port, uint32_t pin)
{
LL_GPIO_TogglePin(port, pin);
}
/**
* @brief This function set a value to an IO
* @param pn : Pin name
* @param val : 0 to set to low, any other value to set to high
* @retval None
*/
static inline void digitalWriteFast(PinName pn, uint32_t ulVal)
{
digital_io_write(get_GPIO_Port(STM_PORT(pn)), STM_LL_GPIO_PIN(pn), ulVal);
}
/**
* @brief This function read the value of an IO
* @param pn : Pin name
* @retval The pin state (LOW or HIGH)
*/
static inline int digitalReadFast(PinName pn)
{
uint8_t level = 0;
level = digital_io_read(get_GPIO_Port(STM_PORT(pn)), STM_LL_GPIO_PIN(pn));
return (level) ? HIGH : LOW;
}
/**
* @brief This function toggle value of an IO
* @param port : one of the gpio port
* @param pin : one of the gpio pin
* @retval None
*/
static inline void digitalToggleFast(PinName pn)
{
digital_io_toggle(get_GPIO_Port(STM_PORT(pn)), STM_LL_GPIO_PIN(pn));
}
高速模式中,内联函数形参-引脚变量为
枚举类型
,定义在同目录下的PinNames.h
头文件当中。
typedef enum {
// Not connected
NC = 0xFFFFFFFF,
// Pin name definition
PA_0 = (PortA << 4) + 0x00,
PA_1 = (PortA << 4) + 0x01,
PA_2 = (PortA << 4) + 0x02,
PA_3 = (PortA << 4) + 0x03,
PA_4 = (PortA << 4) + 0x04,
PA_5 = (PortA << 4) + 0x05,
PA_6 = (PortA << 4) + 0x06,
PA_7 = (PortA << 4) + 0x07,
PA_8 = (PortA << 4) + 0x08,
PA_9 = (PortA << 4) + 0x09,
PA_10 = (PortA << 4) + 0x0A,
PA_11 = (PortA << 4) + 0x0B,
PA_12 = (PortA << 4) + 0x0C,
PA_13 = (PortA << 4) + 0x0D,
PA_14 = (PortA << 4) + 0x0E,
PA_15 = (PortA << 4) + 0x0F,
PB_0 = (PortB << 4) + 0x00,
PB_1 = (PortB << 4) + 0x01,
PB_2 = (PortB << 4) + 0x02,
PB_3 = (PortB << 4) + 0x03,
PB_4 = (PortB << 4) + 0x04,
PB_5 = (PortB << 4) + 0x05,
PB_6 = (PortB << 4) + 0x06,
PB_7 = (PortB << 4) + 0x07,
PB_8 = (PortB << 4) + 0x08,
PB_9 = (PortB << 4) + 0x09,
PB_10 = (PortB << 4) + 0x0A,
PB_11 = (PortB << 4) + 0x0B,
PB_12 = (PortB << 4) + 0x0C,
PB_13 = (PortB << 4) + 0x0D,
PB_14 = (PortB << 4) + 0x0E,
PB_15 = (PortB << 4) + 0x0F,
#if defined GPIOC_BASE
PC_0 = (PortC << 4) + 0x00,
PC_1 = (PortC << 4) + 0x01,
PC_2 = (PortC << 4) + 0x02,
PC_3 = (PortC << 4) + 0x03,
PC_4 = (PortC << 4) + 0x04,
PC_5 = (PortC << 4) + 0x05,
PC_6 = (PortC << 4) + 0x06,
PC_7 = (PortC << 4) + 0x07,
PC_8 = (PortC << 4) + 0x08,
PC_9 = (PortC << 4) + 0x09,
PC_10 = (PortC << 4) + 0x0A,
PC_11 = (PortC << 4) + 0x0B,
PC_12 = (PortC << 4) + 0x0C,
PC_13 = (PortC << 4) + 0x0D,
PC_14 = (PortC << 4) + 0x0E,
PC_15 = (PortC << 4) + 0x0F,
#endif
#if defined GPIOD_BASE
PD_0 = (PortD << 4) + 0x00,
PD_1 = (PortD << 4) + 0x01,
PD_2 = (PortD << 4) + 0x02,
PD_3 = (PortD << 4) + 0x03,
PD_4 = (PortD << 4) + 0x04,
PD_5 = (PortD << 4) + 0x05,
PD_6 = (PortD << 4) + 0x06,
PD_7 = (PortD << 4) + 0x07,
PD_8 = (PortD << 4) + 0x08,
PD_9 = (PortD << 4) + 0x09,
PD_10 = (PortD << 4) + 0x0A,
PD_11 = (PortD << 4) + 0x0B,
PD_12 = (PortD << 4) + 0x0C,
PD_13 = (PortD << 4) + 0x0D,
PD_14 = (PortD << 4) + 0x0E,
PD_15 = (PortD << 4) + 0x0F,
#endif
#if defined GPIOE_BASE
PE_0 = (PortE << 4) + 0x00,
PE_1 = (PortE << 4) + 0x01,
PE_2 = (PortE << 4) + 0x02,
PE_3 = (PortE << 4) + 0x03,
PE_4 = (PortE << 4) + 0x04,
PE_5 = (PortE << 4) + 0x05,
PE_6 = (PortE << 4) + 0x06,
PE_7 = (PortE << 4) + 0x07,
PE_8 = (PortE << 4) + 0x08,
PE_9 = (PortE << 4) + 0x09,
PE_10 = (PortE << 4) + 0x0A,
PE_11 = (PortE << 4) + 0x0B,
PE_12 = (PortE << 4) + 0x0C,
PE_13 = (PortE << 4) + 0x0D,
PE_14 = (PortE << 4) + 0x0E,
PE_15 = (PortE << 4) + 0x0F,
#endif
#if defined GPIOF_BASE
PF_0 = (PortF << 4) + 0x00,
PF_1 = (PortF << 4) + 0x01,
PF_2 = (PortF << 4) + 0x02,
PF_3 = (PortF << 4) + 0x03,
PF_4 = (PortF << 4) + 0x04,
PF_5 = (PortF << 4) + 0x05,
PF_6 = (PortF << 4) + 0x06,
PF_7 = (PortF << 4) + 0x07,
PF_8 = (PortF << 4) + 0x08,
PF_9 = (PortF << 4) + 0x09,
PF_10 = (PortF << 4) + 0x0A,
PF_11 = (PortF << 4) + 0x0B,
PF_12 = (PortF << 4) + 0x0C,
PF_13 = (PortF << 4) + 0x0D,
PF_14 = (PortF << 4) + 0x0E,
PF_15 = (PortF << 4) + 0x0F,
#endif
#if defined GPIOG_BASE
PG_0 = (PortG << 4) + 0x00,
PG_1 = (PortG << 4) + 0x01,
PG_2 = (PortG << 4) + 0x02,
PG_3 = (PortG << 4) + 0x03,
PG_4 = (PortG << 4) + 0x04,
PG_5 = (PortG << 4) + 0x05,
PG_6 = (PortG << 4) + 0x06,
PG_7 = (PortG << 4) + 0x07,
PG_8 = (PortG << 4) + 0x08,
PG_9 = (PortG << 4) + 0x09,
PG_10 = (PortG << 4) + 0x0A,
PG_11 = (PortG << 4) + 0x0B,
PG_12 = (PortG << 4) + 0x0C,
PG_13 = (PortG << 4) + 0x0D,
PG_14 = (PortG << 4) + 0x0E,
PG_15 = (PortG << 4) + 0x0F,
#endif
#if defined GPIOH_BASE
PH_0 = (PortH << 4) + 0x00,
PH_1 = (PortH << 4) + 0x01,
PH_2 = (PortH << 4) + 0x02,
PH_3 = (PortH << 4) + 0x03,
PH_4 = (PortH << 4) + 0x04,
PH_5 = (PortH << 4) + 0x05,
PH_6 = (PortH << 4) + 0x06,
PH_7 = (PortH << 4) + 0x07,
PH_8 = (PortH << 4) + 0x08,
PH_9 = (PortH << 4) + 0x09,
PH_10 = (PortH << 4) + 0x0A,
PH_11 = (PortH << 4) + 0x0B,
PH_12 = (PortH << 4) + 0x0C,
PH_13 = (PortH << 4) + 0x0D,
PH_14 = (PortH << 4) + 0x0E,
PH_15 = (PortH << 4) + 0x0F,
#endif
#if defined GPIOI_BASE
PI_0 = (PortI << 4) + 0x00,
PI_1 = (PortI << 4) + 0x01,
PI_2 = (PortI << 4) + 0x02,
PI_3 = (PortI << 4) + 0x03,
PI_4 = (PortI << 4) + 0x04,
PI_5 = (PortI << 4) + 0x05,
PI_6 = (PortI << 4) + 0x06,
PI_7 = (PortI << 4) + 0x07,
PI_8 = (PortI << 4) + 0x08,
PI_9 = (PortI << 4) + 0x09,
PI_10 = (PortI << 4) + 0x0A,
PI_11 = (PortI << 4) + 0x0B,
PI_12 = (PortI << 4) + 0x0C,
PI_13 = (PortI << 4) + 0x0D,
PI_14 = (PortI << 4) + 0x0E,
PI_15 = (PortI << 4) + 0x0F,
#endif
#if defined GPIOJ_BASE
PJ_0 = (PortJ << 4) + 0x00,
PJ_1 = (PortJ << 4) + 0x01,
PJ_2 = (PortJ << 4) + 0x02,
PJ_3 = (PortJ << 4) + 0x03,
PJ_4 = (PortJ << 4) + 0x04,
PJ_5 = (PortJ << 4) + 0x05,
PJ_6 = (PortJ << 4) + 0x06,
PJ_7 = (PortJ << 4) + 0x07,
PJ_8 = (PortJ << 4) + 0x08,
PJ_9 = (PortJ << 4) + 0x09,
PJ_10 = (PortJ << 4) + 0x0A,
PJ_11 = (PortJ << 4) + 0x0B,
PJ_12 = (PortJ << 4) + 0x0C,
PJ_13 = (PortJ << 4) + 0x0D,
PJ_14 = (PortJ << 4) + 0x0E,
PJ_15 = (PortJ << 4) + 0x0F,
#endif
#if defined GPIOK_BASE
PK_0 = (PortK << 4) + 0x00,
PK_1 = (PortK << 4) + 0x01,
PK_2 = (PortK << 4) + 0x02,
PK_3 = (PortK << 4) + 0x03,
PK_4 = (PortK << 4) + 0x04,
PK_5 = (PortK << 4) + 0x05,
PK_6 = (PortK << 4) + 0x06,
PK_7 = (PortK << 4) + 0x07,
PK_8 = (PortK << 4) + 0x08,
PK_9 = (PortK << 4) + 0x09,
PK_10 = (PortK << 4) + 0x0A,
PK_11 = (PortK << 4) + 0x0B,
PK_12 = (PortK << 4) + 0x0C,
PK_13 = (PortK << 4) + 0x0D,
PK_14 = (PortK << 4) + 0x0E,
PK_15 = (PortK << 4) + 0x0F,
#endif
#if defined GPIOZ_BASE
PZ_0 = (PortZ << 4) + 0x00,
PZ_1 = (PortZ << 4) + 0x01,
PZ_2 = (PortZ << 4) + 0x02,
PZ_3 = (PortZ << 4) + 0x03,
PZ_4 = (PortZ << 4) + 0x04,
PZ_5 = (PortZ << 4) + 0x05,
PZ_6 = (PortZ << 4) + 0x06,
PZ_7 = (PortZ << 4) + 0x07,
PZ_8 = (PortZ << 4) + 0x08,
PZ_9 = (PortZ << 4) + 0x09,
PZ_10 = (PortZ << 4) + 0x0A,
PZ_11 = (PortZ << 4) + 0x0B,
PZ_12 = (PortZ << 4) + 0x0C,
PZ_13 = (PortZ << 4) + 0x0D,
PZ_14 = (PortZ << 4) + 0x0E,
PZ_15 = (PortZ << 4) + 0x0F,
#endif
📚跑个示例代码
//#include <digital_io.h>
#define led1 PC2
#define led2 PC3
#define button1 PC0
#define button2 PC1
// the setup function runs once when you press reset or power the board
void setup() {
Serial.setRx(PA10); // using pin name PY_n
Serial.setTx(PA9); // using pin number PYn
Serial.begin(115200);
// initialize digital pin LED_BUILTIN as an output.
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
}
// the loop function runs over and over again forever
void loop() {
digitalToggleFast(PC_2); // turn the LED on (HIGH is the voltage level)
digitalToggle(led2);
delay(1000); // wait for a second
Serial.println("Perseverance51");
digitalWriteFast(PC_2, !digitalReadFast(PC_2)) ; // turn the LED off by making the voltage LOW
digitalWrite(led2, !digitalRead(led2)) ;
delay(1000); // wait for a second
Serial.println("Arduino STM32G070RBT6");
}