我按照官方例程修改了ping脚和软件片选,调试过程测认为应该是SPI都没初始化成功,麻烦各位帮忙看下是哪部分造成SPI初始化没成功,判断没初始化成功是因为我再main里一直调发送接口,但是实测没有波形下面是spi.c的配置
#include "./flash/spi.h"
void spi_gpio_init(void)
{
GPIO_InitType GPIO_InitStructure;
RCC_Pclk2_Config(RCC_HCLK_DIV2);
RCC_AHB_Peripheral_Clock_Enable(RCC_AHB_PERIPH_GPIOA|RCC_AHB_PERIPH_GPIOB);
//RCC_APB2_Peripheral_Clock_Enable(RCC_APB2_PERIPH_AFIO);
GPIO_Structure_Initialize(&GPIO_InitStructure);
// SPI_MISO PA9 AF1 ||SPI_CLK PA10 AF6 ||SPI_MOSI PB1 AF2 ||SPI_CS PA15 ||
GPIO_InitStructure.Pin = SPI_MISO;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.GPIO_Slew_Rate = GPIO_SLEW_RATE_FAST;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF1_SPI2;
GPIO_Peripheral_Initialize(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.Pin = SPI_MOSI;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF2_SPI2;
GPIO_Peripheral_Initialize(GPIOB, &GPIO_InitStructure);
/* If CLKPOL is HIGH, set SPI_SCK GPIO to GPIO_PULL_UP, otherwise set to GPIO_PULL_DOWN */
GPIO_InitStructure.Pin = SPI_CLK;
GPIO_InitStructure.GPIO_Pull = GPIO_PULL_DOWN;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF6_SPI2;
GPIO_Peripheral_Initialize(GPIOA, &GPIO_InitStructure);
GPIO_Structure_Initialize(&GPIO_InitStructure);
GPIO_InitStructure.Pin = SPI_CS;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT_PP;
GPIO_InitStructure.GPIO_Current = GPIO_DS_4MA;
GPIO_Peripheral_Initialize(GPIOA, &GPIO_InitStructure);
}
void spi_init(void)
{
SPI_InitType SPI_InitStructure;
RCC_APB2_Peripheral_Clock_Enable(RCC_APB2_PERIPH_SPI2);
spi_gpio_init();
SPI_Initializes_Structure(&SPI_InitStructure);
SPI_InitStructure.DataDirection = SPI_DIR_SINGLELINE_TX | SPI_DIR_SINGLELINE_RX;
SPI_InitStructure.SpiMode = SPI_MODE_MASTER;
SPI_InitStructure.DataLen = SPI_DATA_SIZE_8BITS;
SPI_InitStructure.CLKPOL = SPI_CLKPOL_LOW;
SPI_InitStructure.CLKPHA = SPI_CLKPHA_FIRST_EDGE;
SPI_InitStructure.NSS = SPI_NSS_SOFT;
/* It is recommended that the SPI master mode of the C version chips should not exceed 18MHz */
SPI_InitStructure.BaudRatePres = SPI_BR_PRESCALER_8;
SPI_InitStructure.FirstBit = SPI_FB_MSB;
SPI_InitStructure.CRCPoly = 7;
SPI_Initializes(SPI2, &SPI_InitStructure);
SPI_SS_Output_Enable(SPI2);
/* Enable SPI_MASTER */
SPI_ON(SPI2);
SPI_CS_HIGH();
}
uint8_t spi_transmit(uint8_t data)
{
uint8_t brxbuff;
SPI_I2S_Data_Transmit(SPI2, data);
while(SPI_I2S_Flag_Status_Get(SPI2, SPI_I2S_FLAG_TE) == RESET);
brxbuff = SPI_I2S_Data_Get(SPI2);
while(SPI_I2S_Flag_Status_Get(SPI2, SPI_I2S_FLAG_BUSY) != RESET) ;
return brxbuff;
}
spi.h的配置是
#ifndef SPI_H_
#define SPI_H_
#include "n32g430.h"
#define SPI_CS_HIGH() GPIO_Pins_Set(GPIOA,GPIO_PIN_15);
#define SPI_CS_LOW() GPIO_Pins_Reset(GPIOA,GPIO_PIN_15);
//PA
#define SPI_MISO GPIO_PIN_9
#define SPI_CLK GPIO_PIN_10
#define SPI_CS GPIO_PIN_15
//PB
#define SPI_MOSI GPIO_PIN_1
void spi_init(void);
uint8_t spi_transmit(uint8_t data);
#endif // ! SPI_H_
|