Skip to content

Commit 0400fff

Browse files
committed
[stm32f412] support RTduino SPI
1 parent 71560ba commit 0400fff

File tree

7 files changed

+71
-5
lines changed

7 files changed

+71
-5
lines changed

bsp/stm32/stm32f412-st-nucleo/applications/arduino_pinout/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ I2C bus is `SCL/D15` and `SDA/D14` pins. Users can directly include the `#includ
6969

7070
### 3.2 SPI Bus
7171

72-
This board doesn't support Arduino SPI header file and functions.
72+
The SPI bus of the Nucleo board is the `CS/D10`, `MOSI/D11`, `MISO/D12` and `SCK/D13` pins printed on the board. These `D11`, `D12` and `D13` pins It is taken over by the RT-Thread SPI device framework and there is no need to directly control these pins. User needs to operate `D10` chip select pin. Directly quote `#include <SPI.h>` (Arduino official SPI header file) to use. After using the SPI function, the PWM function of `D10` and `D11` will be irreversibly disabled and converted to the SPI function.
7373

7474
### 3.3 Serial
7575

bsp/stm32/stm32f412-st-nucleo/applications/arduino_pinout/README_zh.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ I2C总线是板上丝印的 `SCL/D15` 和 `SDA/D14` 引脚,这两个引脚默
6868

6969
### 3.2 SPI总线
7070

71-
目前本BSP不支持使用Arduino的SPI功能
71+
Nucleo板的SPI总线是板上丝印的 `CS/D10``MOSI/D11``MISO/D12` 以及 `SCK/D13` 引脚,这`D11``D12` 以及 `D13` 引脚是被RT-Thread SPI设备框架接管的,不需要直接操控这些引脚。用户需要操作 `D10` 片选引脚。直接引用`#include <SPI.h>`(Arduino官方SPI头文件)即可使用。在使用SPI功能后,`D10``D11` 的PWM功能将会不可逆失效,转为SPI功能
7272

7373
### 3.3 串口
7474

bsp/stm32/stm32f412-st-nucleo/applications/arduino_pinout/pins_arduino.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
#include "pins_arduino.h"
1313
#include <drv_gpio.h>
1414

15+
#define DBG_TAG "RTduino.pins_arduino"
16+
#define DBG_LVL DBG_INFO
17+
#include <rtdbg.h>
18+
1519
/*
1620
* {Arduino Pin, RT-Thread Pin [, Device Name, Channel]}
1721
* [] means optional
@@ -50,3 +54,34 @@ const pin_map_t pin_map_table[]=
5054
{A6, RT_NULL, "adc1", RT_ADC_INTERN_CH_VREF}, /* ADC, On-Chip: internal reference voltage */
5155
{A7, RT_NULL, "adc1", RT_ADC_INTERN_CH_TEMPER}, /* ADC, On-Chip: internal temperature sensor */
5256
};
57+
58+
#ifdef RTDUINO_USING_SPI
59+
void switchToSPI(const char *bus_name)
60+
{
61+
GPIO_InitTypeDef GPIO_InitStruct = {0};
62+
63+
if(!rt_strcmp(bus_name, "spi1"))
64+
{
65+
__HAL_RCC_SPI1_CLK_ENABLE();
66+
__HAL_RCC_GPIOA_CLK_ENABLE();
67+
68+
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5);
69+
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_6);
70+
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_7);
71+
72+
/**SPI1 GPIO Configuration
73+
PA5 ------> SPI1_SCK
74+
PA6 ------> SPI1_MISO
75+
PA7 ------> SPI1_MOSI
76+
*/
77+
GPIO_InitStruct.Pin = GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
78+
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
79+
GPIO_InitStruct.Pull = GPIO_NOPULL;
80+
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
81+
GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
82+
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
83+
84+
LOG_W("D11, D12 and D13 will switch from PWM to SPI");
85+
}
86+
}
87+
#endif /* RTDUINO_USING_SPI */

bsp/stm32/stm32f412-st-nucleo/applications/arduino_pinout/pins_arduino.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,16 @@
4141
#define A6 (26)
4242
#define A7 (27)
4343

44+
#define RTDUINO_PIN_MAX_LIMIT A7 /* pin number max limit check */
45+
4446
#define F_CPU 96000000L /* CPU:96MHz */
4547

4648
#define LED_BUILTIN D17 /* Default Built-in LED */
4749

4850
/* i2c1 : PB9-SDA PB8-SCL */
4951
#define RTDUINO_DEFAULT_IIC_BUS_NAME "i2c1"
5052

53+
#define SS D10
54+
#define RTDUINO_DEFAULT_SPI_BUS_NAME "spi1"
55+
5156
#endif /* Pins_Arduino_h */

bsp/stm32/stm32f412-st-nucleo/board/CubeMX_Config/Inc/stm32f4xx_hal_conf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
/* #define HAL_SAI_MODULE_ENABLED */
6363
/* #define HAL_SD_MODULE_ENABLED */
6464
/* #define HAL_MMC_MODULE_ENABLED */
65-
/* #define HAL_SPI_MODULE_ENABLED */
65+
#define HAL_SPI_MODULE_ENABLED
6666
#define HAL_TIM_MODULE_ENABLED
6767
#define HAL_UART_MODULE_ENABLED
6868
/* #define HAL_USART_MODULE_ENABLED */

bsp/stm32/stm32f412-st-nucleo/board/Kconfig

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,13 @@ menu "Onboard Peripheral Drivers"
2727
select BSP_USING_PWM14_CH1
2828
select BSP_USING_I2C
2929
select BSP_USING_I2C1
30+
select BSP_USING_SPI
31+
select BSP_USING_SPI1
32+
select BSP_SPI1_TX_USING_DMA
33+
select BSP_SPI1_RX_USING_DMA
3034
imply RTDUINO_USING_SERVO
3135
imply RTDUINO_USING_WIRE
36+
imply RTDUINO_USING_SPI
3237
default n
3338

3439
endmenu
@@ -142,6 +147,27 @@ menu "On-chip Peripheral Drivers"
142147
endif
143148
endif
144149

150+
menuconfig BSP_USING_SPI
151+
bool "Enable SPI BUS"
152+
default n
153+
select RT_USING_SPI
154+
if BSP_USING_SPI
155+
config BSP_USING_SPI1
156+
bool "Enable SPI1 BUS"
157+
default n
158+
159+
config BSP_SPI1_TX_USING_DMA
160+
bool "Enable SPI1 TX DMA"
161+
depends on BSP_USING_SPI1
162+
default n
163+
164+
config BSP_SPI1_RX_USING_DMA
165+
bool "Enable SPI1 RX DMA"
166+
depends on BSP_USING_SPI1
167+
select BSP_SPI1_TX_USING_DMA
168+
default n
169+
endif
170+
145171
config BSP_USING_ON_CHIP_FLASH
146172
bool "Enable on-chip FLASH"
147173
default n

bsp/stm32/stm32l476-st-nucleo/applications/arduino_pinout/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ Hardware Drivers Config --->
6262

6363
### 3.1 I2C总线
6464

65-
STM32F410 Nucleo板的I2C总线是板上丝印的 `SCL/D15``SDA/D14` 引脚,这两个引脚是被RT-Thread I2C设备框架接管的,不需要直接操控这两个引脚,直接引用`#include <Wire.h>`(Arduino官方I2C头文件)即可使用。
65+
Nucleo板的I2C总线是板上丝印的 `SCL/D15``SDA/D14` 引脚,这两个引脚是被RT-Thread I2C设备框架接管的,不需要直接操控这两个引脚,直接引用`#include <Wire.h>`(Arduino官方I2C头文件)即可使用。
6666

6767
### 3.2 SPI总线
6868

69-
目前本BSP不支持使用Arduino的SPI功能
69+
Nucleo板的SPI总线是板上丝印的 `CS/D10``MOSI/D11``MISO/D12` 以及 `SCK/D13` 引脚,这`D11``D12` 以及 `D13` 引脚是被RT-Thread SPI设备框架接管的,不需要直接操控这些引脚。用户需要操作 `D10` 片选引脚。直接引用`#include <SPI.h>`(Arduino官方SPI头文件)即可使用。在使用SPI功能后,`D10``D11` 的PWM功能将会不可逆失效,转为SPI功能
7070

7171
### 3.3 串口
7272

0 commit comments

Comments
 (0)