下面是初始化和发送函数,难道GD的库要在发送和接收时分别切换一次模式,切换发送和接收模式。
#define CURRENT_SPI SPI1
#define READ_MODE spi_disable(CURRENT_SPI); \
spi_bidirectional_transfer_config(CURRENT_SPI, SPI_BIDIRECTIONAL_RECEIVE); \
spi_enable(CURRENT_SPI);
#define WRITE_MODE spi_disable(CURRENT_SPI); \
spi_bidirectional_transfer_config(CURRENT_SPI, SPI_BIDIRECTIONAL_TRANSMIT); \
spi_enable(CURRENT_SPI);
void spi_config(void)
{
rcu_periph_clock_enable(RCU_GPIOB);
//rcu_periph_clock_enable(RCU_AF);
gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13 | GPIO_PIN_15);//MOSI 和 CLK
//gpio_init(GPIOB, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_12);//片选引脚cs
rcu_periph_clock_enable(RCU_SPI1);
spi_i2s_deinit(CURRENT_SPI);
spi_struct_para_init(&spi2); // 初始化spi_init_struct结构体,设置默认参数
spi2.device_mode = SPI_MASTER;
spi2.trans_mode = SPI_TRANSMODE_BDTRANSMIT;
spi2.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
spi2.frame_size = SPI_FRAMESIZE_8BIT;
spi2.nss = SPI_NSS_SOFT;
spi2.prescale = SPI_PSC_64;
spi2.endian = SPI_ENDIAN_MSB;
spi_init(CURRENT_SPI, &spi2); // 使用配置好的spi2结构体初始化SPI2
spi_crc_polynomial_set(CURRENT_SPI, 10);
spi_crc_on(CURRENT_SPI);
spi_enable(CURRENT_SPI);
//spi_nss_output_enable(CURRENT_SPI);
}
void Well_Hal_spi_write_byte(uint8_t btye)
{
// SPI_I2S_SendData(&hspi2,&btye,1,1000);
while (spi_i2s_flag_get(CURRENT_SPI, SPI_FLAG_TBE) == RESET);
spi_i2s_data_transmit(CURRENT_SPI, btye);
while(spi_i2s_flag_get(CURRENT_SPI, SPI_FLAG_TRANS));
}
|