✨STM32G070RBT6总共有4个USART资源。
Serial1 for USART1
Serial2 for USART2
Serial3 for USART3
Serial4 for UART4
默认情况下,只有一个Serialx实例映射到通用
Serial
名。对于STM32G070RBT6而言,映射的管脚为:PA1
和PA0
。
C:\Users\Administrator\AppData\Local\Arduino15\packages\STMicroelectronics\hardware\stm32\2.3.0\variants\STM32G0xx\G070RBT\variant_generic.h
中可以找到相关定义:// Default pin used for generic 'Serial' instance
// Mandatory for Firmata
#ifndef PIN_SERIAL_RX
#define PIN_SERIAL_RX PA1
#endif
#ifndef PIN_SERIAL_TX
#define PIN_SERIAL_TX PA0
#endif
#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.begin(115200);//默认串口1的输出管脚:PA0 PA1。
// 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");
}
// RX TX
HardwareSerial Serial1(PA10, PA9);//将串口1的管脚指定到PA10,PA9引脚上
// the setup function runs once when you press reset or power the board
void setup() {
Serial1.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
Serial1.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
Serial1.println("Arduino STM32G070RBT6");
}
#define led1 PC2
#define led2 PC3
#define button1 PC0
#define button2 PC1
// RX TX
HardwareSerial Serial1(PC5, PC4);
// the setup function runs once when you press reset or power the board
void setup() {
// HardwareSerial(PA10, PA9);
Serial1.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
Serial1.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
Serial1.println("Arduino STM32G070RBT6");
}
#define led1 PC2
#define led2 PC3
#define button1 PC0
#define button2 PC1
// RX TX
HardwareSerial Serial1(PB7, PB6);
// the setup function runs once when you press reset or power the board
void setup() {
Serial1.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
Serial1.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
Serial1.println("Arduino STM32G070RBT6");
}
void setRx(uint32_t rx)
void setTx(uint32_t tx)
void setRx(PinName rx)
void setTx(PinName tx)
注意点:重载函数名相同,形参注意区分。
variant_generic.h
头文件声明了数字引脚/*----------------------------------------------------------------------------
* STM32 pins number
*----------------------------------------------------------------------------*/
#define PA0 PIN_A0
#define PA1 PIN_A1
#define PA2 PIN_A2
#define PA3 PIN_A3
#define PA4 PIN_A4
#define PA5 PIN_A5
#define PA6 PIN_A6
#define PA7 PIN_A7
#define PA8 8
#define PA9 9
#define PA10 10
#define PA11 11
#define PA12 12
#define PA13 13
#define PA14 14
#define PA15 15
#define PB0 PIN_A8
#define PB1 PIN_A9
#define PB2 PIN_A10
#define PB3 19
#define PB4 20
#define PB5 21
#define PB6 22
#define PB7 23
#define PB8 24
#define PB9 25
#define PB10 PIN_A11
#define PB11 PIN_A12
#define PB12 PIN_A13
#define PB13 29
#define PB14 30
#define PB15 31
#define PC0 32
#define PC1 33
#define PC2 34
#define PC3 35
#define PC4 PIN_A14
#define PC5 PIN_A15
#define PC6 38
#define PC7 39
#define PC8 40
#define PC9 41
#define PC10 42
#define PC11 43
#define PC12 44
#define PC13 45
#define PC14 46
#define PC15 47
#define PD0 48
#define PD1 49
#define PD2 50
#define PD3 51
#define PD4 52
#define PD5 53
#define PD6 54
#define PD8 55
#define PD9 56
#define PF0 57
#define PF1 58
#define PA9_R 59
#define PA10_R 60
// Alternate pins number
#define PA6_ALT1 (PA6 | ALT1)
#define PA7_ALT1 (PA7 | ALT1)
#define PA7_ALT2 (PA7 | ALT2)
#define PA7_ALT3 (PA7 | ALT3)
#define PA15_ALT1 (PA15 | ALT1)
#define PB0_ALT1 (PB0 | ALT1)
#define PB1_ALT1 (PB1 | ALT1)
#define PB1_ALT2 (PB1 | ALT2)
#define PB6_ALT1 (PB6 | ALT1)
#define PB13_ALT1 (PB13 | ALT1)
#define PB14_ALT1 (PB14 | ALT1)
#define PB15_ALT1 (PB15 | ALT1)
#define PB15_ALT2 (PB15 | ALT2)
#define PC4_ALT1 (PC4 | ALT1)
#define PC5_ALT1 (PC5 | ALT1)
#define PC8_ALT1 (PC8 | ALT1)
#define PC9_ALT1 (PC9 | ALT1)
#define PC10_ALT1 (PC10 | ALT1)
#define PC11_ALT1 (PC11 | ALT1)
#define NUM_DIGITAL_PINS 61
#define NUM_REMAP_PINS 2
PinNames.h
头文件定义了形参PinName
枚举类型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
#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");
}
HardwareSerial(uint32_t _rxtx)
:U(S)ART用于半双工的Tx引脚编号(PYn)HardwareSerial(PinName _rxtx)
:U(S)ART用于半双工的Tx引脚编号(PYn)HardwareSerial(uint32_t _rx, uint32_t _tx): U(S)ART用于半双工的Tx引脚编号(PYn)
HardwareSerial(PinName _rx, PinName tx): U(S)ART用于半双工的tx引脚名称(PY_n)
HardwareSerial(void *peripheral, HalfDuplexMode_t halfDuplex = HALF_DUPLEX_DISABLED):如果HALF_DUPLEX_ENABLED,在用于半双工的PeripheralPins.c中获取所请求外设的第一个Tx引脚.
enableHalfDuplexRx()
使能Rx模式下的Serial。可以使用read()
操作,但将避免执行读操作。在使用available()
之前使用.void setHalfDuplex()
:当实例未以半双工模式实例化时,启用实例的半双工模式。在这种情况下,必须在begin()
之前调用。Serial4发送字节给Serial3,比较值,然后Serial3重新发送给Serial4并比较。需要连接PA0和PB10。
HardwareSerial Serial3(PA0);
HardwareSerial Serial4(PB10);
//HardwareSerial Serial3(PA_0);
//HardwareSerial Serial4(PB_10);
//HardwareSerial Serial3(UART4, HALF_DUPLEX_ENABLED);
//HardwareSerial Serial4(USART3, HALF_DUPLEX_ENABLED);
//HardwareSerial Serial3(PA0, PA0);
//HardwareSerial Serial4(PB10, PB10);
//HardwareSerial Serial3(PA_0, PA_0);
//HardwareSerial Serial4(PB_10, PB_10);
//HardwareSerial Serial3(NC, PA_0);
//HardwareSerial Serial4(NC, PB_10);
//HardwareSerial Serial3(NUM_DIGITAL_PINS, PA0);
//HardwareSerial Serial4(NUM_DIGITAL_PINS, PB10);
static uint32_t nbTestOK = 0;
static uint32_t nbTestKO = 0;
void test_uart(int val)
{
int recval = -1;
uint32_t error = 0;
Serial4.write(val);
delay(10);
while (Serial3.available()) {
recval = Serial3.read();
}
/* Enable Serial4 to RX*/
Serial4.enableHalfDuplexRx();
if (val == recval) {
Serial3.write(val);
delay(10);
while (Serial4.available()) {
recval = Serial4.read();
}
/* Enable Serial3 to RX*/
Serial3.enableHalfDuplexRx();
if (val == recval) {
nbTestOK++;
Serial.print("Exchange: 0x");
Serial.println(recval, HEX);
} else {
error = 2;
}
}
else {
error = 1;
}
if (error) {
Serial.print("Send: 0x");
Serial.print(val, HEX);
Serial.print("\tReceived: 0x");
Serial.print(recval, HEX);
Serial.print(" --> KO <--");
Serial.println(error);
nbTestKO++;
}
}
void setup() {
Serial.begin(115200);
Serial4.begin(9600);
Serial3.begin(9600);
}
void loop() {
for (uint32_t i = 0; i <= (0xFF); i++) {
test_uart(i);
}
Serial.println("Serial Half-Duplex test done.\nResults:");
Serial.print("OK: ");
Serial.println(nbTestOK);
Serial.print("KO: ");
Serial.println(nbTestKO);
while (1);
}
https://github.com/stm32duino/wiki/wiki/API