gd32f330RBT6spi主从配置
时间: 2023-12-22 11:04:51 浏览: 112
以下是gd32f330RBT6 SPI主从配置的示例代码:
```c
#include "gd32f3x0_spi.h"
#include "gd32f3x0_gpio.h"
#define SPI0_SLAVE_PIN_CS GPIO_PIN_4
#define SPI0_SLAVE_PIN_SCK GPIO_PIN_5
#define SPI0_SLAVE_PIN_MISO GPIO_PIN_6
#define SPI0_SLAVE_PIN_MOSI GPIO_PIN_7
#define SPI0_SLAVE_GPIO GPIOB
void spi0_slave_gpio_init(void)
{
rcu_periph_clock_enable(RCU_GPIOB);
gpio_mode_set(SPI0_SLAVE_GPIO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, SPI0_SLAVE_PIN_CS);
gpio_mode_set(SPI0_SLAVE_GPIO, GPIO_MODE_AF, GPIO_PUPD_NONE, SPI0_SLAVE_PIN_SCK | SPI0_SLAVE_PIN_MISO | SPI0_SLAVE_PIN_MOSI);
gpio_output_options_set(SPI0_SLAVE_GPIO, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, SPI0_SLAVE_PIN_SCK | SPI0_SLAVE_PIN_MOSI);
gpio_af_set(SPI0_SLAVE_GPIO, GPIO_AF_0, SPI0_SLAVE_PIN_SCK | SPI0_SLAVE_PIN_MISO | SPI0_SLAVE_PIN_MOSI);
}
void spi0_slave_init(void)
{
spi_parameter_struct spi_init_struct;
rcu_periph_clock_enable(RCU_SPI0);
spi_i2s_deinit(SPI0);
spi_struct_para_init(&spi_init_struct);
spi_init_struct.device_mode = SPI_SLAVE;
spi_init_struct.trans_mode = SPI_TRANSMODE_RECEIVEONLY;
spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;
spi_init_struct.nss = SPI_NSS_SOFT;
spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
spi_init_struct.prescale = SPI_PSC_256;
spi_init(SPI0, &spi_init_struct);
spi_enable(SPI0);
}
int main(void)
{
uint8_t data;
spi0_slave_gpio_init();
spi0_slave_init();
while (1)
{
while (RESET == spi_i2s_flag_get(SPI0, SPI_FLAG_RBNE));
data = spi_i2s_data_receive(SPI0);
// 处理接收到的数据
}
}
```
阅读全文