本帖最后由 buffalo_bill 于 2022-7-26 15:53 编辑
初始化代码:
#define SET_SPI0_NSS_HIGH gpio_bit_set(GPIOA,GPIO_PIN_4);
#define SET_SPI0_NSS_LOW gpio_bit_reset(GPIOA,GPIO_PIN_4);
nvic_irq_enable(SPI0_IRQn, 1);
rcu_periph_clock_enable(RCU_SPI0);
//rcu_periph_clock_enable(RCU_GPIOA);
/* configure SPI0 GPIO : SCK/PA5, MISO/PA6, MOSI/PA7 */
gpio_af_set(GPIOA, GPIO_AF_5, GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7);
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7);
//gpio_mode_set(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_6);
/* set GPIO PA4 as SPI0 NSS output */
gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_4);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7);
SET_SPI0_NSS_HIGH
/* deinitilize SPI and the parameters */
spi_i2s_deinit(SPI0);
spi_struct_para_init(&spi_init_struct);
/* SPI0 parameter configuration */
spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
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.clock_polarity_phase = SPI_CK_PL_HIGH_PH_1EDGE;
spi_init_struct.nss = SPI_NSS_SOFT;
spi_init_struct.prescale = SPI_PSC_256;
spi_init_struct.endian = SPI_ENDIAN_MSB;
spi_init(SPI0, &spi_init_struct);
/* configure SPI1 byte access to FIFO */
spi_fifo_access_size_config(SPI0, SPI_BYTE_ACCESS);
/* enable SPI interrupt */
// spi_i2s_interrupt_enable(SPI0, SPI_I2S_INT_TBE);
// spi_i2s_interrupt_enable(SPI0, SPI_I2S_INT_RBNE);
spi_nss_output_enable(SPI0);
/* enable SPI */
spi_enable(SPI0);
读从设备寄存器函数
SET_SPI0_NSS_LOW
while(RESET == spi_i2s_flag_get(SPI0, SPI_FLAG_TBE));
spi_i2s_data_transmit(SPI0, txData);
uint8_t ret;
while(RESET == spi_i2s_flag_get(SPI0, SPI_FLAG_RBNE));
ret = spi_i2s_data_receive(SPI0);
SET_SPI0_NSS_HIGH
=======================
CPU型号是[size=1em]GD32L23X
[size=1em]现象是时钟,片选,发送引脚都能正常发出信号,可是读到的值始终是0.
[size=1em]谢谢各位
|