STM32 SPI通信.docx
《STM32 SPI通信.docx》由会员分享,可在线阅读,更多相关《STM32 SPI通信.docx(45页珍藏版)》请在冰豆网上搜索。
STM32SPI通信
/********************(C)COPYRIGHT2007STMicroelectronics********************
*FileName:
main.c
*Author:
MCDApplicationTeam
*Version:
V1.0
*Date:
10/08/2007
*Description:
Mainprogrambody
********************************************************************************
*THEPRESENTSOFTWAREWHICHISFORGUIDANCEONLYAIMSATPROVIDINGCUSTOMERS
*WITHCODINGINFORMATIONREGARDINGTHEIRPRODUCTSINORDERFORTHEMTOSAVETIME.
*ASARESULT,STMICROELECTRONICSSHALLNOTBEHELDLIABLEFORANYDIRECT,
*INDIRECTORCONSEQUENTIALDAMAGESWITHRESPECTTOANYCLAIMSARISINGFROMTHE
*CONTENTOFSUCHSOFTWAREAND/ORTHEUSEMADEBYCUSTOMERSOFTHECODING
*INFORMATIONCONTAINEDHEREININCONNECTIONWITHTHEIRPRODUCTS.
*******************************************************************************/
/*Includes------------------------------------------------------------------*/
#include"stm32f10x_lib.h"
/*Localincludes------------------------------------------------------------*/
/*Privatetypedef-----------------------------------------------------------*/
typedefenum{FAILED=0,PASSED=!
FAILED}TestStatus;
/*Privatedefine------------------------------------------------------------*/
#defineBufferSize32
/*Privatemacro-------------------------------------------------------------*/
/*Privatevariables---------------------------------------------------------*/
SPI_InitTypeDefSPI_InitStructure;
u8SPI1_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};
u8SPI2_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};
u8SPI1_Buffer_Rx[BufferSize],SPI2_Buffer_Rx[BufferSize];
u8Tx_Idx=0,Rx_Idx=0,k=0;
volatileTestStatusTransferStatus1=FAILED,TransferStatus2=FAILED;
volatileTestStatusTransferStatus3=FAILED,TransferStatus4=FAILED;
ErrorStatusHSEStartUpStatus;
/*Privatefunctions---------------------------------------------------------*/
voidRCC_Configuration(void);
voidGPIO_Configuration(void);
voidNVIC_Configuration(void);
TestStatusBuffercmp(u8*pBuffer1,u8*pBuffer2,u16BufferLength);
/*******************************************************************************
*FunctionName:
main
*Description:
Mainprogram
*Input:
None
*Output:
None
*Return:
None
*******************************************************************************/
intmain(void)
{
#ifdefDEBUG
debug();
#endif
/*Systemclocksconfiguration---------------------------------------------*/
RCC_Configuration();
/*NVICconfiguration------------------------------------------------------*/
NVIC_Configuration();
/*GPIOconfiguration------------------------------------------------------*/
GPIO_Configuration();
/*1stphase:
SPI1MasterandSPI2Slave*/
/*SPI1Config-------------------------------------------------------------*/
SPI_InitStructure.SPI_Direction=SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode=SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize=SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL=SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA=SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS=SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_4;
SPI_InitStructure.SPI_FirstBit=SPI_FirstBit_LSB;
SPI_InitStructure.SPI_CRCPolynomial=7;
SPI_Init(SPI1,&SPI_InitStructure);
/*SPI2Config-------------------------------------------------------------*/
SPI_InitStructure.SPI_Mode=SPI_Mode_Slave;
SPI_Init(SPI2,&SPI_InitStructure);
/*EnableSPI1*/
SPI_Cmd(SPI1,ENABLE);
/*EnableSPI2*/
SPI_Cmd(SPI2,ENABLE);
/*Transferprocedure*/
while(Tx_Idx{
/*WaitforSPI1Txbufferempty*/
while(SPI_GetFlagStatus(SPI1,SPI_FLAG_TXE)==RESET);
/*SendSPI2data*/
SPI_SendData(SPI2,SPI2_Buffer_Tx[Tx_Idx]);
/*SendSPI1data*/
SPI_SendData(SPI1,SPI1_Buffer_Tx[Tx_Idx++]);
/*WaitforSPI2datareception*/
while(SPI_GetFlagStatus(SPI2,SPI_FLAG_RXNE)==RESET);
/*ReadSPI2receiveddata*/
SPI2_Buffer_Rx[Rx_Idx]=SPI_ReceiveData(SPI2);
/*WaitforSPI1datareception*/
while(SPI_GetFlagStatus(SPI1,SPI_FLAG_RXNE)==RESET);
/*ReadSPI1receiveddata*/
SPI1_Buffer_Rx[Rx_Idx++]=SPI_ReceiveData(SPI1);
}
/*Checkthecorectnessofwrittendada*/
TransferStatus1=Buffercmp(SPI2_Buffer_Rx,SPI1_Buffer_Tx,BufferSize);
TransferStatus2=Buffercmp(SPI1_Buffer_Rx,SPI2_Buffer_Tx,BufferSize);
/*TransferStatus1,TransferStatus2=PASSED,ifthetransmittedandreceiveddata
areequal*/
/*TransferStatus1,TransferStatus2=FAILED,ifthetransmittedandreceiveddata
aredifferent*/
/*2ndphase:
SPI1SlaveandSPI2Master*/
/*SPI1Re-configuration---------------------------------------------------*/
SPI_InitStructure.SPI_Mode=SPI_Mode_Slave;
SPI_Init(SPI1,&SPI_InitStructure);
/*SPI2Re-configuration---------------------------------------------------*/
SPI_InitStructure.SPI_Mode=SPI_Mode_Master;
SPI_Init(SPI2,&SPI_InitStructure);
/*ResetTx_Idx,Rx_Idxindexesandreceivetablesvalues*/
Tx_Idx=0;
Rx_Idx=0;
for(k=0;kfor(k=0;k/*Transferprocedure*/
while(Tx_Idx{
/*WaitforSPI2Txbufferempty*/
while(SPI_GetFlagStatus(SPI2,SPI_FLAG_TXE)==RESET);
/*SendSPI1data*/
SPI_SendData(SPI1,SPI1_Buffer_Tx[Tx_Idx]);
/*SendSPI2data*/
SPI_SendData(SPI2,SPI2_Buffer_Tx[Tx_Idx++]);
/*WaitforSPI1datareception*/
while(SPI_GetFlagStatus(SPI1,SPI_FLAG_RXNE)==RESET);
/*ReadSPI1receiveddata*/
SPI1_Buffer_Rx[Rx_Idx]=SPI_ReceiveData(SPI1);
/*WaitforSPI2datareception*/
while(SPI_GetFlagStatus(SPI2,SPI_FLAG_RXNE)==RESET);
/*ReadSPI2receiveddata*/
SPI2_Buffer_Rx[Rx_Idx++]=SPI_ReceiveData(SPI2);
}
/*Checkthecorectnessofwrittendada*/
TransferStatus3=Buffercmp(SPI2_Buffer_Rx,SPI1_Buffer_Tx,BufferSize);
TransferStatus4=Buffercmp(SPI1_Buffer_Rx,SPI2_Buffer_Tx,BufferSize);
/*TransferStatus3,TransferStatus4=PASSED,ifthetransmittedandreceiveddata
areequal*/
/*TransferStatus3,TransferStatus4=FAILED,ifthetransmittedandreceiveddata
aredifferent*/
while
(1)
{
}
}
/*******************************************************************************
*FunctionName:
RCC_Configuration
*Description:
Configuresthedifferentsystemclocks.
*Input:
None
*Output:
None
*Return:
None
*******************************************************************************/
voidRCC_Configuration(void)
{
/*RCCsystemreset(fordebugpurpose)*/
RCC_DeInit();
/*EnableHSE*/
RCC_HSEConfig(RCC_HSE_ON);
/*WaittillHSEisready*/
HSEStartUpStatus=RCC_WaitForHSEStartUp();
if(HSEStartUpStatus==SUCCESS)
{
/*EnablePrefetchBuffer*/
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/*Flash2waitstate*/
FLASH_SetLatency(FLASH_Latency_2);
/*HCLK=SYSCLK*/
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/*PCLK2=HCLK/2*/
RCC_PCLK2Config(RCC_HCLK_Div2);
/*PCLK1=HCLK/2*/
RCC_PCLK1Config(RCC_HCLK_Div2);
/*PLLCLK=8MHz*9=72MHz*/
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);
/*EnablePLL*/
RCC_PLLCmd(ENABLE);
/*WaittillPLLisready*/
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET)
{
}
/*SelectPLLassystemclocksource*/
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/*WaittillPLLisusedassystemclocksource*/
while(RCC_GetSYSCLKSource()!
=0x08)
{
}
}
/*Enableperipheralclocks--------------------------------------------------*/
/*GPIOA,GPIOBandSPI1clockenable*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|
RCC_APB2Periph_SPI1,ENABLE);
/*SPI2Periphclockenable*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
}
/*******************************************************************************
*FunctionName:
GPIO_Configuration
*Description:
ConfiguresthedifferentGPIOports.
*Input:
None
*Output:
None
*Return:
None
*******************************************************************************/
voidGPIO_Configuration(void)
{
GPIO_InitTypeDefGPIO_InitStructure;
/*ConfigureSPI1pins:
SCK,MISOandMOSI---------------------------------*/
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);
/*ConfigureSPI2pins:
SCK,MISOandMOSI---------------------------------*/
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
/*******************************************************************************
*FunctionName:
NVIC_Configuration
*Description:
ConfiguresNVICandVectorTablebaselocation.
*Input:
None
*Output:
None
*Return:
None
*******************************************************************************/
voidNVIC_Configuration(void)
{
#ifdefVECT_TAB_RAM
/*SettheVectorTablebaselocationat0x20000000*/
NVIC_SetVectorTable(NVIC_VectTab_RAM,0x0);
#else/*VECT_TAB_FLASH*/
/*SettheVectorTablebaselocationat0x08000000*/
NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);
#endif
}
/*******************************************************************************
*FunctionName:
Buffercmp
*Description:
Comparestwobuffers.
*Input:
-pBuffer1,pBuffer2:
bufferstobecompared.
*:
-BufferLength:
buffer