在使用GD32450V-START的SPI进行主从操作时,从机接收到的数据全是FF,不知道是哪里出了问题。
#include <stdio.h>
#include "gd32f4xx.h"
#include "systick.h"
#include "gd32f4xx_it.h"
#include "gd32f450v_start.h"
void USART1_Config(void);
void SPI_MasterInit(void);
void SPI_SlaverInit(void);
void SPI_MasterSlaveFullduplex(void);
int main(){
systick_config();
USART1_Config();
SPI_MasterInit();
SPI_SlaverInit();
while(1){
SPI_MasterSlaveFullduplex();
}
}
void USART1_Config(void)
{
rcu_periph_clock_enable (RCU_GPIOA);
rcu_periph_clock_enable (RCU_USART1);
/* connect port to USART1_Tx */
gpio_af_set(GPIOA, GPIO_AF_7, GPIO_PIN_2);
/* connect port to USART1_Rx */
gpio_af_set(GPIOA, GPIO_AF_7, GPIO_PIN_3);
/* configure USART Tx as alternate function push-pull */
gpio_mode_set (GPIOA,GPIO_MODE_AF, GPIO_PUPD_PULLUP ,GPIO_PIN_2);
gpio_output_options_set (GPIOA, GPIO_OTYPE_PP , GPIO_OSPEED_50MHZ , GPIO_PIN_2);
/* configure USART Rx as alternate function push-pull */
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP ,GPIO_PIN_3);
usart_deinit(USART1);
usart_baudrate_set (USART1 , 9600U);
usart_word_length_set (USART1 , 8);
usart_stop_bit_set (USART1, USART_STB_1BIT);
usart_parity_config (USART1, USART_PM_NONE);
usart_hardware_flow_rts_config(USART1,USART_RTS_DISABLE);
usart_transmit_config(USART1,USART_TRANSMIT_ENABLE);
usart_receive_config(USART1,USART_RECEIVE_ENABLE);
usart_enable(USART1);
}
void SPI_MasterInit(void){
spi_parameter_struct spi_init_struct = {0};
spi_struct_para_init(&spi_init_struct);
/*初始化时钟 */
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_SPI0);
/* A7: MOSI; A4:NSS; A5:SCK */
gpio_af_set (GPIOA,GPIO_AF_5,GPIO_PIN_7);
gpio_af_set (GPIOA,GPIO_AF_5,GPIO_PIN_4);
gpio_af_set (GPIOA,GPIO_AF_5,GPIO_PIN_5);
/* SPI0 GPIO_OUTPUT_SET */
gpio_mode_set (GPIOA,GPIO_MODE_AF,GPIO_PUPD_NONE,GPIO_PIN_7);
gpio_output_options_set(GPIOA,GPIO_OTYPE_PP ,GPIO_OSPEED_25MHZ,GPIO_PIN_7);
gpio_mode_set (GPIOA,GPIO_MODE_AF,GPIO_PUPD_NONE,GPIO_PIN_5);
gpio_output_options_set(GPIOA,GPIO_OTYPE_PP ,GPIO_OSPEED_25MHZ,GPIO_PIN_5);
gpio_mode_set (GPIOA,GPIO_MODE_AF,GPIO_PUPD_NONE,GPIO_PIN_4);
gpio_output_options_set(GPIOA,GPIO_OTYPE_PP ,GPIO_OSPEED_25MHZ,GPIO_PIN_4);
/* enable SPI0 */
spi_init_struct.trans_mode = SPI_TRANSMODE_BDTRANSMIT;
spi_init_struct.device_mode = SPI_MASTER;
spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;
spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
spi_init_struct.nss = SPI_NSS_HARD;
spi_init_struct.prescale = SPI_PSC_16;
spi_init_struct.endian = SPI_ENDIAN_MSB;
spi_init(SPI0, &spi_init_struct);
gpio_bit_set(GPIOA,GPIO_PIN_4);
spi_nss_output_enable(SPI0);
spi_enable(SPI0);
}
void SPI_SlaverInit(void){
spi_parameter_struct spi_init_struct = {0};
spi_struct_para_init(&spi_init_struct);
/* Initialize GPIO */
rcu_periph_clock_enable(RCU_GPIOE);
/* E4:NSS;E2:SCK ; E5:MISO;E6:MOSI */
gpio_af_set (GPIOE,GPIO_AF_5,GPIO_PIN_2);
gpio_af_set (GPIOE,GPIO_AF_5,GPIO_PIN_4);
gpio_af_set (GPIOE,GPIO_AF_5,GPIO_PIN_6);
/* SPI2_SCK GPIO_INPUT_SET */
gpio_mode_set (GPIOE,GPIO_MODE_AF,GPIO_PUPD_NONE,GPIO_PIN_2);
/* SPI2_MOSI GPIO_INPUT_SET */
gpio_mode_set (GPIOE,GPIO_MODE_AF,GPIO_PUPD_NONE,GPIO_PIN_4);
/* MISO GPIO_INPUT_SET */
gpio_mode_set (GPIOE,GPIO_MODE_AF,GPIO_PUPD_NONE,GPIO_PIN_6);
/* enable SPI3 */
rcu_periph_clock_enable(RCU_SPI3);
spi_init_struct.trans_mode = SPI_TRANSMODE_BDRECEIVE;
spi_init_struct.device_mode = SPI_SLAVE;
spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;
spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
spi_init_struct.nss = SPI_NSS_HARD;
spi_init_struct.prescale = SPI_PSC_16;
spi_init_struct.endian = SPI_ENDIAN_MSB;
spi_init(SPI3, &spi_init_struct);
spi_enable(SPI3);
}
void SPI_MasterSlaveFullduplex(void)
{
uint32_t slave_rx_buf;
rcu_periph_clock_enable (RCU_GPIOA);
rcu_periph_clock_enable (RCU_GPIOE);
gpio_bit_reset(GPIOE,GPIO_PIN_4);
spi_i2s_data_transmit(SPI3, 0x10);
while(SET!=spi_i2s_flag_get(SPI3,SPI_FLAG_RBNE));
slave_rx_buf = spi_i2s_data_receive(SPI3);
printf("Slave receive: ");
printf("%x ", slave_rx_buf);
}
int fputc(int ch, FILE *f)
{
usart_data_transmit(USART1, (uint8_t)ch);
while (usart_flag_get(USART1, USART_FLAG_TBE) == RESET);
return (ch);
}
int fgetc(FILE *f)
{
while (usart_flag_get(USART1, USART_FLAG_RBNE) == RESET);
return (int)usart_data_receive(USART1);
}