21ic问答首页 - GD32F103C8T6 SPI做从机可以接收数据,但是不能发送数据出去
GD32F103C8T6 SPI做从机可以接收数据,但是不能发送数据出去
4908641532022-01-15
GD32F103C8T6 SPI做从机,中断可以接收数据,但是不能发送数据出去,使用的模式是全双工,有遇到过这种吗?我用另外一个GD32F205的芯片,做SPI从机就是可以同时收发的,这个芯片就是发不出来,我对了一下官方的例程,没有发现代码问题在哪,代码如下
/**
* @brief SPI_FLASH初始化
* @param 无
* @retval 无
*/
void BSP_SPI_Init(void)
{
spi_parameter_struct spi_init_struct;
#ifndef SPI_MODE_MASTER
/* configure SPI1 NVIC */
nvic_irq_enable(SPI1_IRQn,1,3);
#endif
/* SPI clock enable */
rcu_periph_clock_enable(RCU_GPIOB);
rcu_periph_clock_enable(RCU_SPI1);
/* SPI1 GPIO config */
#ifdef SPI_MODE_MASTER
//MASTER
//CS
gpio_init(SPI1_CS_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SPI1_CS_PIN);
//SCK MOSI
gpio_init(SPI1_SCK_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, SPI1_SCK_PIN);
gpio_init(SPI1_MOSI_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, SPI1_MOSI_PIN);
//MISO
gpio_init(SPI1_MISO_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, SPI1_MISO_PIN);
#else
//SLAVE
//CS
gpio_init(SPI1_CS_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, SPI1_CS_PIN);
//SCK MOSI
gpio_init(SPI1_SCK_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, SPI1_SCK_PIN);
gpio_init(SPI1_MOSI_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, SPI1_MOSI_PIN);
//MISO
gpio_init(SPI1_MISO_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, SPI1_MISO_PIN);
#endif
/* chip select invalid */
gpio_bit_write(SPI1_CS_PORT, SPI1_CS_PIN, SET);
/* deinitilize SPI and the parameters */
spi_i2s_deinit(SPI1);
spi_struct_para_init(&spi_init_struct);
/* SPI1 parameter config */
spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
#ifdef SPI_MODE_MASTER
spi_init_struct.device_mode = SPI_MASTER;
spi_init_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_2EDGE;
#else
spi_init_struct.device_mode = SPI_SLAVE;
spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
#endif
spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;
spi_init_struct.nss = SPI_NSS_SOFT;
spi_init_struct.prescale = SPI_PSC_64;//1M
spi_init_struct.endian = SPI_ENDIAN_MSB;
spi_init(SPI1, &spi_init_struct);
#ifndef SPI_MODE_MASTER
/* SPI int enable */
spi_i2s_interrupt_enable(SPI1, SPI_I2S_INT_RBNE);
#endif
/* SPI enable */
spi_enable(SPI1);
}
#ifndef SPI_MODE_MASTER
/*!
\brief this function handles SPI1 Handler exception
\param[in] none
\param[out] none
\retval none
*/
void SPI1_IRQHandler(void)
{
if(RESET != spi_i2s_interrupt_flag_get(SPI1,SPI_I2S_INT_FLAG_RBNE))
{
usart_interrupt_flag_clear(SPI1,SPI_FLAG_RBNE);
// received data
System.com.spi_rx[System.com.spi_rev] = spi_i2s_data_receive(SPI1); //保存数据
System.com.spi_rev++;
spi_i2s_data_transmit(SPI1, System.com.spi_tx[System.com.spi_rev]); //?
if(System.com.spi_rev > 4)
{
System.com.spi_tx_len = System.com.spi_rx[3] * 256 + System.com.spi_rx[4] + 5;
if(System.com.spi_rev > System.com.spi_tx_len)
//if(System.com.spi_rev > 22)
{
System.com.spi_rev = 0;
System.com.spi_flag |= 0x80;
spi_i2s_data_transmit(SPI1, PACKET_SYNC);
}
}
}
}
#endif
/**
* @brief SPI_FLASH初始化
* @param 无
* @retval 无
*/
void BSP_SPI_Init(void)
{
spi_parameter_struct spi_init_struct;
#ifndef SPI_MODE_MASTER
/* configure SPI1 NVIC */
nvic_irq_enable(SPI1_IRQn,1,3);
#endif
/* SPI clock enable */
rcu_periph_clock_enable(RCU_GPIOB);
rcu_periph_clock_enable(RCU_SPI1);
/* SPI1 GPIO config */
#ifdef SPI_MODE_MASTER
//MASTER
//CS
gpio_init(SPI1_CS_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SPI1_CS_PIN);
//SCK MOSI
gpio_init(SPI1_SCK_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, SPI1_SCK_PIN);
gpio_init(SPI1_MOSI_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, SPI1_MOSI_PIN);
//MISO
gpio_init(SPI1_MISO_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, SPI1_MISO_PIN);
#else
//SLAVE
//CS
gpio_init(SPI1_CS_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, SPI1_CS_PIN);
//SCK MOSI
gpio_init(SPI1_SCK_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, SPI1_SCK_PIN);
gpio_init(SPI1_MOSI_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, SPI1_MOSI_PIN);
//MISO
gpio_init(SPI1_MISO_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, SPI1_MISO_PIN);
#endif
/* chip select invalid */
gpio_bit_write(SPI1_CS_PORT, SPI1_CS_PIN, SET);
/* deinitilize SPI and the parameters */
spi_i2s_deinit(SPI1);
spi_struct_para_init(&spi_init_struct);
/* SPI1 parameter config */
spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
#ifdef SPI_MODE_MASTER
spi_init_struct.device_mode = SPI_MASTER;
spi_init_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_2EDGE;
#else
spi_init_struct.device_mode = SPI_SLAVE;
spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
#endif
spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;
spi_init_struct.nss = SPI_NSS_SOFT;
spi_init_struct.prescale = SPI_PSC_64;//1M
spi_init_struct.endian = SPI_ENDIAN_MSB;
spi_init(SPI1, &spi_init_struct);
#ifndef SPI_MODE_MASTER
/* SPI int enable */
spi_i2s_interrupt_enable(SPI1, SPI_I2S_INT_RBNE);
#endif
/* SPI enable */
spi_enable(SPI1);
}
#ifndef SPI_MODE_MASTER
/*!
\brief this function handles SPI1 Handler exception
\param[in] none
\param[out] none
\retval none
*/
void SPI1_IRQHandler(void)
{
if(RESET != spi_i2s_interrupt_flag_get(SPI1,SPI_I2S_INT_FLAG_RBNE))
{
usart_interrupt_flag_clear(SPI1,SPI_FLAG_RBNE);
// received data
System.com.spi_rx[System.com.spi_rev] = spi_i2s_data_receive(SPI1); //保存数据
System.com.spi_rev++;
spi_i2s_data_transmit(SPI1, System.com.spi_tx[System.com.spi_rev]); //?
if(System.com.spi_rev > 4)
{
System.com.spi_tx_len = System.com.spi_rx[3] * 256 + System.com.spi_rx[4] + 5;
if(System.com.spi_rev > System.com.spi_tx_len)
//if(System.com.spi_rev > 22)
{
System.com.spi_rev = 0;
System.com.spi_flag |= 0x80;
spi_i2s_data_transmit(SPI1, PACKET_SYNC);
}
}
}
}
#endif
赞0
评论
2022-01-25
赞0
评论
2022-01-25
赞0
评论
2022-01-24
您需要登录后才可以回复 登录 | 注册