Skip to content

Commit a8581ac

Browse files
authored
[Ardunio]为stm32f103-blue-pill对接RTdunio软件包 (#6231)
* 对接RTdunio
1 parent 76885ca commit a8581ac

File tree

13 files changed

+295
-25
lines changed

13 files changed

+295
-25
lines changed

bsp/stm32/stm32f072-st-nucleo/applications/arduino_main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
void setup(void)
1414
{
1515
/* put your setup code here, to run once: */
16+
Serial.begin();
1617
}
1718

1819
void loop(void)
1920
{
2021
/* put your main code here, to run repeatedly: */
21-
Serial.println("Hello Arduino!\n");
22+
Serial.println("Hello Arduino!");
2223
delay(800);
2324
}

bsp/stm32/stm32f103-blue-pill/applications/SConscript

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ cwd = GetCurrentDir()
55
src = Glob('*.c')
66
CPPPATH = [cwd]
77

8+
if GetDepend(['PKG_USING_RTDUINO']) and not GetDepend(['RTDUINO_NO_SETUP_LOOP']):
9+
src += ['arduino_main.cpp']
10+
811
group = DefineGroup('Applications', src, depend = [''], CPPPATH = CPPPATH)
912

1013
list = os.listdir(cwd)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2006-2022, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2022-08-05 liYony first version
9+
*/
10+
11+
#include <Arduino.h>
12+
13+
void setup(void)
14+
{
15+
/* put your setup code here, to run once: */
16+
Serial.begin();
17+
}
18+
19+
void loop(void)
20+
{
21+
/* put your main code here, to run repeatedly: */
22+
Serial.println("Hello Arduino!");
23+
delay(800);
24+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# STM32F103 Blue-Pill开发板的Arduino生态兼容说明
2+
3+
## 1 RTduino - RT-Thread的Arduino生态兼容层
4+
5+
STM32F103 Blue-Pill开发板已经完整适配了[RTduino软件包](https://github.com/RTduino/RTduino),即RT-Thread的Arduino生态兼容层。用户可以按照Arduino的编程习惯来操作该BSP,并且可以使用大量Arduino社区丰富的库,是对RT-Thread生态的极大增强。更多信息,请参见[RTduino软件包说明文档](https://github.com/RTduino/RTduino)
6+
7+
### 1.1 如何开启针对本BSP的Arduino生态兼容层
8+
9+
Env 工具下敲入 menuconfig 命令,或者 RT-Thread Studio IDE 下选择 RT-Thread Settings:
10+
11+
```Kconfig
12+
Hardware Drivers Config --->
13+
Onboard Peripheral Drivers --->
14+
[*] Support Arduino
15+
```
16+
17+
## 2 Arduino引脚排布
18+
19+
![blue-pill-f103-pinout](blue-pill-f103-pinout.png)
20+
21+
该BSP遵照Arduino UNO板的引脚排列方式,并扩展增加了Blue-pill自身的板载资源功能引脚。详见 `pins_arduino.c`
22+
23+
| Arduino引脚编号 | STM32引脚编号 | 5V容忍 | 备注 |
24+
| --------------- | ------------- | ------ | ------------------------------------------------------------ |
25+
| 0 (D0) | -- | | 该引脚在UNO板中为串口RX引脚,不可当做普通IO |
26+
| 1 (D1) | -- | | 该引脚在UNO板中为串口TX引脚,不可当做普通IO |
27+
| 2 (D2) | PB9 || I2C1-SDA,被RT-Thread的I2C设备框架i2c1总线接管,不可当做普通IO |
28+
| 3 (D3) | PB8 || I2C1_SCL,被RT-Thread的I2C设备框架i2c1总线接管,不可当做普通IO |
29+
| 4 (D4) | PB7 || |
30+
| 5 (D5) | PB6 || |
31+
| 6 (D6) | PB5 || PWM3-CH2(定时器3发生) |
32+
| 7 (D7) | PB4 || PWM3-CH1(定时器3发生) |
33+
| 8 (D8) | PB3 || PWM2-CH2(定时器2发生) |
34+
| 9 (D9) | PA15 || PWM2-CH1(定时器2发生) |
35+
| 10 (D10) | PA12 || USB-DB,不可当做普通IO |
36+
| 11 (D11) | PA11 || USB-DM,不可当做普通IO |
37+
| 12 (D12) | PA10 || UART1-RX,被RT-Thread的UART设备框架uart1接管,不可当做普通IO |
38+
| 13 (D13) | PA9 || UART1-RX,被RT-Thread的UART设备框架uart1接管,不可当做普通IO |
39+
| 14 (D14) | PA8 || PWM1-CH1(定时器1发生) |
40+
| 15 (D15) | PB15 || |
41+
| 16 (D16) | PB14 || |
42+
| 17 (D17) | PB13 || PWM1-CH1N(定时器1发生) |
43+
| 18 (D18) | PB12 || |
44+
| 19 (D19) | PC13 || 板载LED默认引脚 |
45+
| 20 (D20) | PC14 || |
46+
| 21 (D21) | PC15 || |
47+
| 22 (D22) | PA2 || UART2-TX,被RT-Thread的UART设备框架uart2接管,不可当做普通IO |
48+
| 23 (D23) | PA3 || UART2-RX,被RT-Thread的UART设备框架uart2接管,不可当做普通IO |
49+
| 24 (D24) | PA5 || SPI-SCK, SPI功能尚未完全实现,目前仅作普通IO使用 |
50+
| 25 (D25) | PA6 || SPI-MISO,SPI功能尚未完全实现,目前仅作普通IO使用 |
51+
| 26 (D26) | PA7 || SPI-MOSI,SPI功能尚未完全实现,目前仅作普通IO使用 |
52+
| 27 (D27) | PB10 || UART3-TX,被RT-Thread的UART设备框架uart3接管,不可当做普通IO |
53+
| 28 (D28) | PB11 || UART3-RX,被RT-Thread的UART设备框架uart3接管,不可当做普通IO |
54+
| A0 | PA0 || ADC |
55+
| A1 | PA1 || ADC |
56+
| A2 | PA4 || ADC |
57+
| A3 | PB0 || ADC |
58+
| A4 | PB1 || ADC |
59+
| A5 | -- | | 芯片内部参考电压 ADC |
60+
| A6 | -- | | 芯片内部温度 ADC |
61+
62+
> 注意:
63+
>
64+
> 1. 驱动舵机和analogWrite函数要选择不同定时器发生的PWM信号引脚,由于STM32的定时器4个通道需要保持相同的频率,如果采用相同的定时器发生的PWM分别驱动舵机和analogWrite,可能会导致舵机失效。
65+
> 4. D10引脚是PWM反相位引脚(也就是常说的互补输出引脚CHxN)。但是这里不用考虑到占空比互补问题(CHx-20%,CHxN-80%),直接正常使用即可。
66+
> 4. STM32的PA15引脚默认作为JTAG下载使用,但是如果采用SWD调试时,PA15(D9)可以作为普通的IO口使用,但是需要设置一些寄存器。
67+
68+
> 参考资料
69+
>
70+
> 1. 暂无
71+
72+
## 3 I2C总线
73+
74+
STM32F103 Blue-Pill板的I2C总线是板上丝印的 `SCL/D3``SDA/D2` 引脚,这两个引脚是被RT-Thread I2C设备框架接管的,不需要直接操控这两个引脚,直接引用`#include <Wire.h>`(Arduino官方I2C头文件)即可使用。
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from building import *
2+
3+
cwd = GetCurrentDir()
4+
src = Glob('*.c') + Glob('*.cpp')
5+
inc = [cwd]
6+
7+
group = DefineGroup('Arduino', src, depend = ['PKG_USING_RTDUINO'], CPPPATH = inc)
8+
9+
Return('group')
1.09 MB
Loading
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) 2006-2022, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2022-08-05 liYony first version
9+
*/
10+
#include <Arduino.h>
11+
#include <board.h>
12+
#include "pins_arduino.h"
13+
14+
/*
15+
{Arduino Pin, RT-Thread Pin [, Device Name(PWM or ADC), Channel]}
16+
[] means optional
17+
Digital pins must NOT give the device name and channel.
18+
Analog pins MUST give the device name and channel(ADC, PWM or DAC).
19+
Arduino Pin must keep in sequence.
20+
*/
21+
const pin_map_t pin_map_table[]=
22+
{
23+
{D0}, /* RX */
24+
{D1}, /* TX */
25+
{D2}, /* I2C1-SDA */
26+
{D3}, /* I2C1-SCL */
27+
{D4, GET_PIN(B,7)},
28+
{D5, GET_PIN(B,6)},
29+
{D6, GET_PIN(B,5), "pwm3", 2},/* PWM */
30+
{D7, GET_PIN(B,4), "pwm3", 1}, /* PWM */
31+
{D8, GET_PIN(B,3), "pwm2", 2}, /* PWM */
32+
{D9, GET_PIN(A,15), "pwm2", 1}, /* PWM */
33+
{D10}, /* USB-DP */
34+
{D11}, /* USB-DM */
35+
{D12}, /* UART1-RX */
36+
{D13}, /* UART1-TX */
37+
{D14, GET_PIN(A,8), "pwm1", 1}, /* PWM */
38+
{D15, GET_PIN(B,15)},
39+
{D16, GET_PIN(B,14)},
40+
{D17, GET_PIN(B,13), "pwm1", -1}, /* PWM */
41+
{D18, GET_PIN(B,12)},
42+
{D19, GET_PIN(C,13)}, /* user led1 */
43+
{D20, GET_PIN(C,14)},
44+
{D21, GET_PIN(C,15)},
45+
{D22}, /* UART2-TX */
46+
{D23}, /* UART2-RX */
47+
/* The connection of RTdunio SPI has not been completed, *
48+
* and it can only be used as a common GPIO at present. */
49+
{D24, GET_PIN(A,5)}, /* SPI-SCK */
50+
{D25, GET_PIN(A,6)}, /* SPI-MISO */
51+
{D26, GET_PIN(A,7)}, /* SPI-MOSI */
52+
{D27}, /* UART3-TX */
53+
{D28}, /* UART3-RX */
54+
{A0, GET_PIN(A,0), "adc1", 0}, /* ADC */
55+
{A1, GET_PIN(A,1), "adc1", 1}, /* ADC */
56+
{A2, GET_PIN(A,4), "adc1", 4}, /* ADC */
57+
{A3, GET_PIN(B,0), "adc1", 8}, /* ADC */
58+
{A4, GET_PIN(B,1), "adc1", 9}, /* ADC */
59+
{A5, RT_NULL, "adc1", 17}, /* ADC, On-Chip: internal reference voltage, ADC_CHANNEL_VREFINT */
60+
{A6, RT_NULL, "adc1", 16} /* ADC, On-Chip: internal temperature sensor, ADC_CHANNEL_TEMPSENSOR */
61+
};
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (c) 2006-2022, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2022-08-05 liYony first version
9+
*/
10+
11+
#ifndef Pins_Arduino_h
12+
#define Pins_Arduino_h
13+
14+
/* pins alias. Must keep in sequence */
15+
#define D0 (0)
16+
#define D1 (1)
17+
#define D2 (2)
18+
#define D3 (3)
19+
#define D4 (4)
20+
#define D5 (5)
21+
#define D6 (6)
22+
#define D7 (7)
23+
#define D8 (8)
24+
#define D9 (9)
25+
#define D10 (10)
26+
#define D11 (11)
27+
#define D12 (12)
28+
#define D13 (13)
29+
#define D14 (14)
30+
#define D15 (15)
31+
#define D16 (16)
32+
#define D17 (17)
33+
#define D18 (18)
34+
#define D19 (19)
35+
#define D20 (20)
36+
#define D21 (21)
37+
#define D22 (22)
38+
#define D23 (23)
39+
#define D24 (24)
40+
#define D25 (25)
41+
#define D26 (26)
42+
#define D27 (27)
43+
#define D28 (28)
44+
#define A0 (29)
45+
#define A1 (30)
46+
#define A2 (31)
47+
#define A3 (32)
48+
#define A4 (33)
49+
#define A5 (34)
50+
#define A6 (35)
51+
52+
#define F_CPU 72000000L /* CPU: 72MHz */
53+
#define LED_BUILTIN D19 /* Default Built-in LED */
54+
55+
#define RTDUINO_DEFAULT_IIC_BUS_NAME "i2c1"
56+
57+
#define RTDUINO_SERIAL2_DEVICE_NAME "uart2"
58+
#define RTDUINO_SERIAL3_DEVICE_NAME "uart3"
59+
60+
#endif /* Pins_Arduino_h */

bsp/stm32/stm32f103-blue-pill/board/CubeMX_Config/CubeMX_Config.ioc

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,34 @@ Mcu.Pin12=PB0
3333
Mcu.Pin13=PB1
3434
Mcu.Pin14=PB10
3535
Mcu.Pin15=PB11
36-
Mcu.Pin16=PA8
37-
Mcu.Pin17=PA9
38-
Mcu.Pin18=PA10
39-
Mcu.Pin19=PA11
36+
Mcu.Pin16=PB13
37+
Mcu.Pin17=PA8
38+
Mcu.Pin18=PA9
39+
Mcu.Pin19=PA10
4040
Mcu.Pin2=PD0-OSC_IN
41-
Mcu.Pin20=PA12
42-
Mcu.Pin21=PA13
43-
Mcu.Pin22=PA14
44-
Mcu.Pin23=PA15
45-
Mcu.Pin24=PB3
46-
Mcu.Pin25=PB4
47-
Mcu.Pin26=PB5
48-
Mcu.Pin27=VP_ADC1_TempSens_Input
49-
Mcu.Pin28=VP_ADC1_Vref_Input
50-
Mcu.Pin29=VP_RTC_VS_RTC_Activate
41+
Mcu.Pin20=PA11
42+
Mcu.Pin21=PA12
43+
Mcu.Pin22=PA13
44+
Mcu.Pin23=PA14
45+
Mcu.Pin24=PA15
46+
Mcu.Pin25=PB3
47+
Mcu.Pin26=PB4
48+
Mcu.Pin27=PB5
49+
Mcu.Pin28=VP_ADC1_TempSens_Input
50+
Mcu.Pin29=VP_ADC1_Vref_Input
5151
Mcu.Pin3=PD1-OSC_OUT
52-
Mcu.Pin30=VP_SYS_VS_Systick
53-
Mcu.Pin31=VP_TIM1_VS_ClockSourceINT
54-
Mcu.Pin32=VP_TIM2_VS_ClockSourceINT
55-
Mcu.Pin33=VP_TIM3_VS_ClockSourceINT
52+
Mcu.Pin30=VP_RTC_VS_RTC_Activate
53+
Mcu.Pin31=VP_SYS_VS_Systick
54+
Mcu.Pin32=VP_TIM1_VS_ClockSourceINT
55+
Mcu.Pin33=VP_TIM2_VS_ClockSourceINT
56+
Mcu.Pin34=VP_TIM3_VS_ClockSourceINT
5657
Mcu.Pin4=PA0-WKUP
5758
Mcu.Pin5=PA1
5859
Mcu.Pin6=PA2
5960
Mcu.Pin7=PA3
6061
Mcu.Pin8=PA4
6162
Mcu.Pin9=PA5
62-
Mcu.PinsNb=34
63+
Mcu.PinsNb=35
6364
Mcu.ThirdPartyNb=0
6465
Mcu.UserConstants=
6566
Mcu.UserName=STM32F103C8Tx
@@ -122,6 +123,9 @@ PB10.Mode=Asynchronous
122123
PB10.Signal=USART3_TX
123124
PB11.Mode=Asynchronous
124125
PB11.Signal=USART3_RX
126+
PB13.Locked=true
127+
PB13.Mode=PWM Generation1 CH1 CH1N
128+
PB13.Signal=TIM1_CH1N
125129
PB3.Signal=S_TIM2_CH2
126130
PB4.Signal=S_TIM3_CH1
127131
PB5.Signal=S_TIM3_CH2
@@ -195,7 +199,7 @@ SH.ADCx_IN8.0=ADC1_IN8,IN8
195199
SH.ADCx_IN8.ConfNb=1
196200
SH.ADCx_IN9.0=ADC1_IN9,IN9
197201
SH.ADCx_IN9.ConfNb=1
198-
SH.S_TIM1_CH1.0=TIM1_CH1,PWM Generation1 CH1
202+
SH.S_TIM1_CH1.0=TIM1_CH1,PWM Generation1 CH1 CH1N
199203
SH.S_TIM1_CH1.ConfNb=1
200204
SH.S_TIM2_CH1_ETR.0=TIM2_CH1,PWM Generation1 CH1
201205
SH.S_TIM2_CH1_ETR.ConfNb=1
@@ -211,8 +215,8 @@ SPI1.Direction=SPI_DIRECTION_2LINES
211215
SPI1.IPParameters=VirtualType,Mode,Direction,BaudRatePrescaler,CalculateBaudRate
212216
SPI1.Mode=SPI_MODE_MASTER
213217
SPI1.VirtualType=VM_MASTER
214-
TIM1.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1
215-
TIM1.IPParameters=Channel-PWM Generation1 CH1
218+
TIM1.Channel-PWM\ Generation1\ CH1\ CH1N=TIM_CHANNEL_1
219+
TIM1.IPParameters=Channel-PWM Generation1 CH1 CH1N
216220
TIM2.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1
217221
TIM2.Channel-PWM\ Generation2\ CH2=TIM_CHANNEL_2
218222
TIM2.IPParameters=Channel-PWM Generation1 CH1,Channel-PWM Generation2 CH2

bsp/stm32/stm32f103-blue-pill/board/CubeMX_Config/Src/stm32f1xx_hal_msp.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,10 +327,17 @@ void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
327327
/* USER CODE BEGIN TIM1_MspPostInit 0 */
328328

329329
/* USER CODE END TIM1_MspPostInit 0 */
330+
__HAL_RCC_GPIOB_CLK_ENABLE();
330331
__HAL_RCC_GPIOA_CLK_ENABLE();
331332
/**TIM1 GPIO Configuration
333+
PB13 ------> TIM1_CH1N
332334
PA8 ------> TIM1_CH1
333335
*/
336+
GPIO_InitStruct.Pin = GPIO_PIN_13;
337+
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
338+
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
339+
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
340+
334341
GPIO_InitStruct.Pin = GPIO_PIN_8;
335342
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
336343
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

0 commit comments

Comments
 (0)