/*
STM32F030R8有两个SPI接口:
SPI1:PB3、PB4、PB5
SPI2:PB13、PB14、PB15
*/
/* 选择SPIx */
#if 0
#define SPIx SPI1
#define SPIx_CLK RCC_APB2Periph_SPI1
#define SPIx_IRQn SPI1_IRQn
#define SPIx_IRQHandler SPI1_IRQHandler
#define SPIx_SCK_PIN GPIO_Pin_3
#define SPIx_SCK_GPIO_PORT GPIOB
#define SPIx_SCK_GPIO_CLK RCC_AHBPeriph_GPIOB
#define SPIx_SCK_SOURCE GPIO_PinSource3
#define SPIx_SCK_AF GPIO_AF_0
#define SPIx_MISO_PIN GPIO_Pin_4
#define SPIx_MISO_GPIO_PORT GPIOB
#define SPIx_MISO_GPIO_CLK RCC_AHBPeriph_GPIOB
#define SPIx_MISO_SOURCE GPIO_PinSource4
#define SPIx_MISO_AF GPIO_AF_0
#define SPIx_MOSI_PIN GPIO_Pin_5
#define SPIx_MOSI_GPIO_PORT GPIOB
#define SPIx_MOSI_GPIO_CLK RCC_AHBPeriph_GPIOB
#define SPIx_MOSI_SOURCE GPIO_PinSource5
#define SPIx_MOSI_AF GPIO_AF_0
#else
#define SPIx SPI2
#define SPIx_CLK RCC_APB1Periph_SPI2
#define SPIx_IRQn SPI2_IRQn
#define SPIx_IRQHandler SPI2_IRQHandler
#define SPIx_SCK_PIN GPIO_Pin_13
#define SPIx_SCK_GPIO_PORT GPIOB
#define SPIx_SCK_GPIO_CLK RCC_AHBPeriph_GPIOB
#define SPIx_SCK_SOURCE GPIO_PinSource13
#define SPIx_SCK_AF GPIO_AF_0
#define SPIx_MISO_PIN GPIO_Pin_14
#define SPIx_MISO_GPIO_PORT GPIOB
#define SPIx_MISO_GPIO_CLK RCC_AHBPeriph_GPIOB
#define SPIx_MISO_SOURCE GPIO_PinSource14
#define SPIx_MISO_AF GPIO_AF_0
#define SPIx_MOSI_PIN GPIO_Pin_15
#define SPIx_MOSI_GPIO_PORT GPIOB
#define SPIx_MOSI_GPIO_CLK RCC_AHBPeriph_GPIOB
#define SPIx_MOSI_SOURCE GPIO_PinSource15
#define SPIx_MOSI_AF GPIO_AF_0
#endif
/* SPI数据收发变量 */
#define BUFFERSIZE 4
uint8_t TxBuffer[BUFFERSIZE] = {0,1,2,3};
uint8_t RxBuffer [BUFFERSIZE];
__IO uint8_t Rx_Idx = 0x00;
__IO uint8_t Tx_Idx = 0x00;
/* SPI配置,并直接while(1) */
static void SPI_Config(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(SPIx_CLK, ENABLE);
RCC_AHBPeriphClockCmd(SPIx_SCK_GPIO_CLK | SPIx_MISO_GPIO_CLK | SPIx_MOSI_GPIO_CLK, ENABLE);
GPIO_PinAFConfig(SPIx_SCK_GPIO_PORT, SPIx_SCK_SOURCE, SPIx_SCK_AF);
GPIO_PinAFConfig(SPIx_MOSI_GPIO_PORT, SPIx_MOSI_SOURCE, SPIx_MOSI_AF);
GPIO_PinAFConfig(SPIx_MISO_GPIO_PORT, SPIx_MISO_SOURCE, SPIx_MISO_AF);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_InitStructure.GPIO_Pin = SPIx_SCK_PIN;
GPIO_Init(SPIx_SCK_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = SPIx_MOSI_PIN;
GPIO_Init(SPIx_MOSI_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = SPIx_MISO_PIN;
GPIO_Init(SPIx_MISO_GPIO_PORT, &GPIO_InitStructure);
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_Init(SPIx, &SPI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = SPIx_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
SPI_RxFIFOThresholdConfig(SPIx, SPI_RxFIFOThreshold_QF);
SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_RXNE, ENABLE);
SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_ERR, ENABLE);
SPI_Cmd(SPIx, ENABLE);
while(1)
{
SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_TXE, ENABLE);
while(SPI_GetTransmissionFIFOStatus(SPIx) != SPI_TransmissionFIFOStatus_Empty){}
while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_BSY) == SET){}
while(SPI_GetReceptionFIFOStatus(SPIx) != SPI_ReceptionFIFOStatus_Empty){}
}
}
求助:使用SPI1正常,使用SPI2无法进入中断,一直未找到原因。有遇到类似情况的大侠多多出手相助,O(∩_∩)O谢谢
|