请教各位前辈,STM32F103 的spi问题 STM作为从机(NXP 的LPC2294作为主机与之通讯)(急)
我在调STM32F103 的SPI 遇到问题,调了好久没有调出来,我用的是周立功的LPC2294的MCU 与STM32f103进行两个MCU之间SPI通讯,Lpc2294作为主机,STM作为从机,LPC2294作为主机SPI调好了,时钟,片选,MOSI 脚都有数据输出了,可STM收不到数据。
STM32的库中关于SPI的配置都是将他设为主机,没有找到将其作为从机用的,按手册将其设置为从机可是调不通。我不知道是硬件问题还是软件没有配对,硬件电路我是将两个MCU之间的脚直接相连(NSS两脚相连,CLK两片脚相连,MOSI两片脚相连,MISO两片脚相连),中间没有任何电路,不知是否可以?
求救了。
此帖出自
小平头技术问答
#define BufferSize 32
u8 SPI1_Buffer_Tx[BufferSize] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,
0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10,0x11,0x12,
0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,
0x1C,0x1D,0x1E,0x1F,0x20};
u8 SPI2_Buffer_Tx[BufferSize] = {0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,
0x5A,0x5B,0x5C,0x5D,0x5E,0x5F,0x60,0x61,0x62,
0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x6B,
0x6C,0x6D,0x6E,0x6F,0x70};
u8 SPI1_Buffer_Rx[BufferSize], SPI2_Buffer_Rx[BufferSize];
u8 Tx_Idx = 0, Rx_Idx = 0, k = 0;
SPI_InitTypeDef SPI_InitStructure;
void RCC_Configuration(void);
void NVIC_Configuration(void);
void SPI_Configuration(void);
void GPIO_Configuration(void);
int main(void)
{
#ifdef debug
debug();
#endif
RCC_Configuration();
NVIC_Configuration();
SPI_Configuration();
GPIO_Configuration();
while(1)
{
while(Tx_Idx<BufferSize) //BufferSize==32
{
/* Wait for SPI1 Tx buffer empty */
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)==RESET);
/* 数据发送寄存器为空 */
SPI_I2S_SendData(SPI2, SPI2_Buffer_Tx[Tx_Idx]);
/* SPI2主模式发送 */
SPI_I2S_SendData(SPI1, SPI1_Buffer_Tx[Tx_Idx++]);
/* Wait for SPI2 data reception */
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE)==RESET);
/* Read SPI2 received data */
SPI2_Buffer_Rx[Rx_Idx] = SPI_I2S_ReceiveData(SPI2);
/* Wait for SPI1 data reception */
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)==RESET);
/* Read SPI1 received data */
SPI1_Buffer_Rx[Rx_Idx++] = SPI_I2S_ReceiveData(SPI1);
}
/* 2nd phase: SPI1 Slave and SPI2 Master */
/* SPI1 Re-configuration ---------------------------------------------------*/
SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
SPI_Init(SPI1, &SPI_InitStructure);
/* SPI2 Re-configuration ---------------------------------------------------*/
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_Init(SPI2, &SPI_InitStructure);
/* Reset Tx_Idx, Rx_Idx indexes and receive tables values */
Tx_Idx=0;
Rx_Idx=0;
for(k=0; k<BufferSize; k++) SPI2_Buffer_Rx[k]=0;
for(k=0; k<BufferSize; k++) SPI1_Buffer_Rx[k]=0;
/* Transfer procedure */
while(Tx_Idx<BufferSize) //BufferSize==32
{
/* 检测指定的SPI标志位设置是否 */
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE)==RESET);
/* 通过SPI1发送一个数据 */
SPI_I2S_SendData(SPI1, SPI1_Buffer_Tx[Tx_Idx]);
/* 通过SPI2发送一个数据*/
SPI_I2S_SendData(SPI2, SPI2_Buffer_Tx[Tx_Idx++]);
/* 检测指定的SPI标志位设置是否 */
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)==RESET);
/* Buffer_Rx[Rx_Tdx]接受SPI2的数据 */
SPI1_Buffer_Rx[Rx_Idx] = SPI_I2S_ReceiveData(SPI1);
/* Wait for SPI2 data reception */
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE)==RESET);
/* Buffer_Rx[Rx_Tdx]接受SPI1的数据*/
SPI2_Buffer_Rx[Rx_Idx++] = SPI_I2S_ReceiveData(SPI2);
}
/* Check the corectness of written dada */
while(1)
{
}
}
}
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK/2 */
RCC_PCLK2Config(RCC_HCLK_Div2);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_SPI1,ENABLE);
/* SPI2 Periph clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
}
void NVIC_Configuration(void)
{
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
}
void SPI_Configuration(void)
{
SPI_Cmd(SPI1, DISABLE); //先禁能,才能改变MODE模式
SPI_Cmd(SPI2, DISABLE);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //两线全双工
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //主模式
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //8位
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //CPOL=0时钟悬空低
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //CPHA=1 数据在第二个沿捕获
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //或hard模式
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; //4分频
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB; //高位在前
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC7(效验位7)
SPI_Init(SPI1, &SPI_InitStructure); //SPI1为主模式
/* SPI2 Config -------------------------------------------------------------*/
SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
SPI_Init(SPI2, &SPI_InitStructure); //SPI2为从模式
/* Enable SPI1 */
SPI_Cmd(SPI1, ENABLE); //SPI1使能
/* Enable SPI2 */
SPI_Cmd(SPI2, ENABLE); //SPI2使能
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure SPI1 pins: SCK, MISO and MOSI ---------------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure SPI2 pins: SCK, MISO and MOSI ---------------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
一周热门 更多>