用示波器去抓波形的时候发现片选会在时钟和MOSI前面就拉高片选信号,此时我的函数发送数据都还没开始执行片选就拉高了,主要的问题还是时序不同步,请教下大家这是什么问题导致的具体的配置如下
#include "./flash/spi.h"
void spi_gpio_init(void)
{
GPIO_InitType GPIO_InitStructure;
RCC_Pclk2_Config(RCC_HCLK_DIV2);
RCC_APB2_Peripheral_Clock_Enable(RCC_APB2_PERIPH_SPI2);
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_Mode = GPIO_MODE_AF_PP;
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_Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.GPIO_Pull = GPIO_PULL_UP;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF6_SPI2;
GPIO_Peripheral_Initialize(GPIOA, &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_gpio_cs_init(void)
{
GPIO_InitType GPIO_InitStructure;
GPIO_InitStructure.Pin = SPI_CS;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT_PP;
GPIO_InitStructure.GPIO_Pull = GPIO_PULL_UP;
GPIO_InitStructure.GPIO_Current = GPIO_DS_4MA;
GPIO_InitStructure.GPIO_Slew_Rate = GPIO_SLEW_RATE_FAST;
GPIO_Peripheral_Initialize(GPIOA, &GPIO_InitStructure);
}
void spi_init(void)
{
SPI_InitType SPI_InitStructure;
spi_gpio_init();
spi_gpio_cs_init();
SPI_I2S_Reset(SPI2);
SPI_Initializes_Structure(&SPI_InitStructure);
SPI_InitStructure.DataDirection = SPI_DIR_DOUBLELINE_FULLDUPLEX;
SPI_InitStructure.SpiMode = SPI_MODE_MASTER;
SPI_InitStructure.DataLen = SPI_DATA_SIZE_8BITS;
SPI_InitStructure.CLKPOL = SPI_CLKPOL_HIGH;
SPI_InitStructure.CLKPHA = SPI_CLKPHA_SECOND_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_256;
SPI_InitStructure.FirstBit = SPI_FB_MSB;
//SPI_InitStructure.CRCPoly = 7;
SPI_Initializes(SPI2, &SPI_InitStructure);
SPI_SS_Output_Enable(SPI2);
/* Enable SPI_MASTER */
SPI_Set_Nss_Level(SPI2,SPI_NSS_HIGH);
SPI_ON(SPI2);
SPI_CS_HIGH();
}
uint8_t spi_tramsit_receive(uint8_t data)
{
uint8_t brxbuff;
// printf("data = %2X\n",(uint16_t)data);
SPI_I2S_Data_Transmit(SPI2,data);
while(SPI_I2S_Flag_Status_Get(SPI2, SPI_I2S_FLAG_TE) == SET);
brxbuff = SPI_I2S_Data_Get(SPI2);
while(SPI_I2S_Flag_Status_Get(SPI2, SPI_I2S_FLAG_BUSY) != SET) ;
return brxbuff;
}
**/
下面主要是main函数中的内容和宏定义
#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
uint32_t spi_read_id(void)
{
uint32_t data = 0 ;
uint8_t receiveid[3] = {0x00};
uint8_t id = 0;
SPI_CS_LOW();
spi_tramsit_receive(0x01);
receiveid[0] = spi_tramsit_receive(0X01);
receiveid[1] = spi_tramsit_receive(0X01);
receiveid[2] = spi_tramsit_receive(0X01);
SPI_CS_HIGH();
data = receiveid[0] <<16 |receiveid[1]<<8 | receiveid[2];
return id ;
}
int main(void)
{
uint8_t data = 0xAA;
uint32_t id = 0;
uart_init();
spi_flash_init();
while(1)
{
id = spi_read_id();
printf("id = %d",id);
SysTick_Delay_Ms(1000);
}
}
|