用stm32f407做spi的从机,片选NSS设置为硬件模式,可是NSS硬件上没有接,仍然可以收到主机发来的数据
配置代码如下:
GPIO_InitTypeDef GPIO_uInitStructure;
SPI_InitTypeDef SPI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);
GPIO_uInitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_uInitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_uInitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_uInitStructure.GPIO_PuPd = GPIO_PuPd_UP;;
GPIO_uInitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA,&GPIO_uInitStructure);
// SPI1_RES_ON;
GPIO_uInitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_uInitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_uInitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_uInitStructure.GPIO_PuPd = GPIO_PuPd_UP;;
GPIO_uInitStructure.GPIO_Speed = GPIO_Speed_100MHz; // 2M
GPIO_Init(GPIOA,&GPIO_uInitStructure);
//SPI1_CS_ON;
GPIO_uInitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6|GPIO_Pin_7;
GPIO_uInitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_uInitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_uInitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_uInitStructure.GPIO_PuPd = GPIO_PuPd_UP;;
GPIO_Init(GPIOA,&GPIO_uInitStructure);
SPI_Cmd(SPI1,DISABLE);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex ;
SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; //SPI_BaudRatePrescaler_32;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1,&SPI_InitStructure);
SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_RXNE, ENABLE);
SPI_Cmd(SPI1, ENABLE);
SPI_I2S_ClearITPendingBit(SPI1, SPI_I2S_IT_RXNE);
NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
dma_spi1_init();
DMA_ITConfig(DMA2_Stream5, DMA_IT_TC, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
|