Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ devices.i2c:
- CONFIG_BSP_HW_I2C1_PIN_PB12_PB13=y
- CONFIG_BSP_HW_I2C1_CLK=100


devices.spi:
kconfig:
- CONFIG_BSP_USING_SPI=y
- CONFIG_BSP_USING_SPI0=y


devices.wlan:
kconfig:
- CONFIG_BSP_USING_WLAN=y
Expand Down Expand Up @@ -61,4 +68,4 @@ peripheral.at24c02:
- CONFIG_BSP_USING_HW_I2C=y
- CONFIG_BSP_USING_HW_I2C0=y
- CONFIG_BSP_HW_I2C0_PIN_PA2_PA3=y
- CONFIG_BSP_HW_I2C0_CLK=100
- CONFIG_BSP_HW_I2C0_CLK=100
25 changes: 25 additions & 0 deletions bsp/gd32/risc-v/gd32vw553h-eval/board/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ menu "On-chip Peripheral Drivers"
bool "Enable PWM16"
default n
endif

menuconfig BSP_USING_HW_I2C
bool "Enable Hardware I2C"
default n
Expand Down Expand Up @@ -165,6 +166,26 @@ menu "On-chip Peripheral Drivers"
range 10 1000
endif


menuconfig BSP_USING_SPI
bool "Enable SPI BUS"
default n
select RT_USING_SPI
if BSP_USING_SPI
config BSP_USING_SPI0
bool "Enable SPI0 BUS"
default n

config BSP_SPI0_TX_USING_DMA
bool "Enable SPI0 TX DMA"
depends on BSP_USING_SPI0
default n

config BSP_SPI0_RX_USING_DMA
bool "Enable SPI0 RX DMA"
depends on BSP_USING_SPI0
select BSP_SPI0_TX_USING_DMA

menuconfig BSP_USING_HWTIMER
bool "Enable timer"
default n
Expand All @@ -187,6 +208,7 @@ menu "On-chip Peripheral Drivers"
default n
config BSP_USING_HWTIMER16
bool "Enable TIM16"

default n
endif

Expand Down Expand Up @@ -219,7 +241,10 @@ menu "Board extended module Drivers"

endif


endmenu


endmenu

endmenu
83 changes: 70 additions & 13 deletions bsp/gd32/risc-v/libraries/gd32_drivers/drv_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Change Logs:
* Date Author Notes
* 2022-06-04 BruceOu first implementation
* 2026-01-24 Unknown Add GD32VW553h
*/
#include "drv_spi.h"

Expand All @@ -29,6 +30,19 @@ static struct rt_spi_bus spi_bus2;
static const struct gd32_spi spi_bus_obj[] = {

#ifdef BSP_USING_SPI0
#if defined (SOC_SERIES_GD32VW55x)
{
SPI,
"spi0",
RCU_SPI,
RCU_GPIOA,
&spi_bus0,
GPIOA,
GPIO_PIN_9,
GPIO_PIN_10,
GPIO_PIN_11,
}
#else
{
SPI0,
"spi0",
Expand All @@ -40,6 +54,7 @@ static const struct gd32_spi spi_bus_obj[] = {
GPIO_PIN_6,
GPIO_PIN_7,
}
#endif
#endif /* BSP_USING_SPI0 */

#ifdef BSP_USING_SPI1
Expand Down Expand Up @@ -92,16 +107,22 @@ static void gd32_spi_init(struct gd32_spi *gd32_spi)
rcu_periph_clock_enable(gd32_spi->spi_clk);
rcu_periph_clock_enable(gd32_spi->gpio_clk);

#if defined (SOC_SERIES_GD32VW55x)
/* configure SPI GPIO: SCK, MISO, MOSI */
gpio_af_set(gd32_spi->spi_port, GPIO_AF_0, gd32_spi->sck_pin | gd32_spi->miso_pin | gd32_spi->mosi_pin);

gpio_mode_set(gd32_spi->spi_port, GPIO_MODE_AF, GPIO_PUPD_NONE, gd32_spi->sck_pin | gd32_spi->miso_pin | gd32_spi->mosi_pin);
gpio_output_options_set(gd32_spi->spi_port, GPIO_OTYPE_PP, GPIO_OSPEED_25MHZ, gd32_spi->sck_pin | gd32_spi->miso_pin | gd32_spi->mosi_pin);
#else
/* Init SPI SCK MOSI */
gpio_init(gd32_spi->spi_port, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, gd32_spi->sck_pin | gd32_spi->mosi_pin);

/* Init SPI MISO */
gpio_init(gd32_spi->spi_port, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, gd32_spi->miso_pin);

#endif
}

static rt_err_t spi_configure(struct rt_spi_device* device,
struct rt_spi_configuration* configuration)
struct rt_spi_configuration* configuration)
{
struct rt_spi_bus * spi_bus = (struct rt_spi_bus *)device->bus;
struct gd32_spi *spi_device = (struct gd32_spi *)spi_bus->parent.user_data;
Expand Down Expand Up @@ -140,6 +161,9 @@ static rt_err_t spi_configure(struct rt_spi_device* device,
LOG_D("CK_APB2 freq: %d\n", rcu_clock_freq_get(CK_APB2));
LOG_D("max freq: %d\n", max_hz);

#if defined (SOC_SERIES_GD32VW55x)
spi_src = CK_APB2;
#else
if (spi_periph == SPI1 || spi_periph == SPI2)
{
spi_src = CK_APB1;
Expand All @@ -148,6 +172,9 @@ static rt_err_t spi_configure(struct rt_spi_device* device,
{
spi_src = CK_APB2;
}
#endif


spi_apb_clock = rcu_clock_freq_get(spi_src);

if(max_hz >= spi_apb_clock/2)
Expand Down Expand Up @@ -215,12 +242,19 @@ static rt_err_t spi_configure(struct rt_spi_device* device,
spi_init_struct.device_mode = SPI_MASTER;
spi_init_struct.nss = SPI_NSS_SOFT;

#if defined (SOC_SERIES_GD32VW55x)
spi_crc_off();
/* init SPI */
spi_init(&spi_init_struct);
/* Enable SPI_MASTER */
spi_enable();
#else
spi_crc_off(spi_periph);

/* init SPI */
spi_init(spi_periph, &spi_init_struct);
/* Enable SPI_MASTER */
spi_enable(spi_periph);
#endif

return RT_EOK;
};
Expand Down Expand Up @@ -261,16 +295,27 @@ static rt_uint32_t spixfer(struct rt_spi_device* device, struct rt_spi_message*
data = *send_ptr++;
}

// Todo: replace register read/write by gd32f4 lib
//Wait until the transmit buffer is empty
#if defined (SOC_SERIES_GD32VW55x)
/* Wait until the transmit buffer is empty */
while(RESET == spi_flag_get(SPI_FLAG_TBE));
/* Send the byte */
spi_data_transmit(data);

/* Wait until a data is received */
while(RESET == spi_flag_get(SPI_FLAG_RBNE));
/* Get the received data */
data = spi_data_receive();
#else
/* Wait until the transmit buffer is empty */
while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_TBE));
// Send the byte
/* Send the byte */
spi_i2s_data_transmit(spi_periph, data);

//Wait until a data is received
/* Wait until a data is received */
while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_RBNE));
// Get the received data
/* Get the received data */
data = spi_i2s_data_receive(spi_periph);
#endif

if(recv_ptr != RT_NULL)
{
Expand All @@ -294,15 +339,27 @@ static rt_uint32_t spixfer(struct rt_spi_device* device, struct rt_spi_message*
data = *send_ptr++;
}

//Wait until the transmit buffer is empty
#if defined (SOC_SERIES_GD32VW55x)
/* Wait until the transmit buffer is empty */
while(RESET == spi_flag_get(SPI_FLAG_TBE));
/* Send the byte */
spi_data_transmit(data);

/* Wait until a data is received */
while(RESET == spi_flag_get(SPI_FLAG_RBNE));
/* Get the received data */
data = spi_data_receive();
#else
/* Wait until the transmit buffer is empty */
while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_TBE));
// Send the byte
/* Send the byte */
spi_i2s_data_transmit(spi_periph, data);

//Wait until a data is received
/* Wait until a data is received */
while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_RBNE));
// Get the received data
/* Get the received data */
data = spi_i2s_data_receive(spi_periph);
#endif

if(recv_ptr != RT_NULL)
{
Expand Down
Loading