//SPI1: PA5--SCK PA7--MOSI //SPI2: PB13--SCK PB15--MOSI //SPI1--Master SPI2--Slave //SPI1发送数据到SPI1的数据寄存器 然后SPI2数据寄存器中如果有数据的时候会发出DMA请求 接着DMA把数据搬到内部寄存器SPI2_Buffer_Rx[BufferSize]中 //SPI1用DMA1_Channel3传送数据 时钟速率达到36M/s ,SPI2用DMA1_Channel4传送 最大速率达18M/s #include"stm32f10x.h" #define BufferSize 50 #define SPI2_DR_Addr 0x4000380c #define SPI1_DR_Addr 0x4001300c uint8_t Idex=0; typedef enum {FAILED=0,PASSED=!FAILED}TextStatus; uint16_tSPI1_Buffer_Tx[BufferSize]={0x0102,0x0203,0x0304,0x0405,0x0506,0x0607,0x0708,0x0809,0x090a,0x0a0b, 0x0102,0x0203,0x0304,0x0405,0x0506,0x0607,0x0708,0x0809,0x090a,0x0a0b, 0x0102,0x0203,0x0304,0x0405,0x0506,0x0607,0x0708,0x0809,0x090a,0x0a0b, 0x0102,0x0203,0x0304,0x0405,0x0506,0x0607,0x0708,0x0809,0x090a,0x0a0b, 0x0102,0x0203,0x0304,0x0405,0x0506,0x0607,0x0708,0x0809,0x090a,0x0a0b }; uint16_t SPI2_Buffer_Rx[BufferSize]; volatile TextStatus textstatus=FAILED; void RCC_Config(void); void GPIO_Config(void); void DMA_Config(void); void SPI_Config(void); TextStatus TempComp(uint16_t*temp1,uint16_t* temp2,uint16_t length); int main(void) { RCC_Config(); GPIO_Config(); DMA_Config(); SPI_Config(); while(1); } void RCC_Config(void) { //RCC_PCLK2Config(RCC_HCLK_Div2); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_SPI1,ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE); } void GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5|GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13|GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB,&GPIO_InitStructure); } void DMA_Config(void) { DMA_InitTypeDef DMA_InitStructure; DMA_DeInit(DMA1_Channel4); DMA_InitStructure.DMA_PeripheralBaseAddr=(uint32_t)SPI2_DR_Addr; DMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)SPI2_Buffer_Rx; DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable; DMA_InitStructure.DMA_BufferSize=BufferSize; DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode=DMA_Mode_Normal; DMA_InitStructure.DMA_Priority=DMA_Priority_High; DMA_InitStructure.DMA_M2M=DMA_M2M_Disable; DMA_Init(DMA1_Channel4,&DMA_InitStructure); DMA_DeInit(DMA1_Channel3); DMA_InitStructure.DMA_PeripheralBaseAddr=(uint32_t)SPI1_DR_Addr; DMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)SPI1_Buffer_Tx; DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable; DMA_InitStructure.DMA_BufferSize=BufferSize; DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode=DMA_Mode_Normal; DMA_InitStructure.DMA_Priority=DMA_Priority_High; DMA_InitStructure.DMA_M2M=DMA_M2M_Disable; DMA_Init(DMA1_Channel3,&DMA_InitStructure); DMA_Cmd(DMA1_Channel3,ENABLE); DMA_Cmd(DMA1_Channel4,ENABLE); } void SPI_Config(void) { SPI_InitTypeDef SPI_InitStructure; SPI_InitStructure.SPI_Direction=SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode=SPI_Mode_Master; SPI_InitStructure.SPI_DataSize=SPI_DataSize_16b; SPI_InitStructure.SPI_CPOL=SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA=SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS=SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_2; SPI_InitStructure.SPI_FirstBit=SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial=7; SPI_Init(SPI1,&SPI_InitStructure); SPI_InitStructure.SPI_Direction=SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode=SPI_Mode_Slave; SPI_Init(SPI2,&SPI_InitStructure); SPI_Cmd(SPI1,ENABLE); SPI_Cmd(SPI2,ENABLE); SPI_I2S_DMACmd(SPI2,SPI_I2S_DMAReq_Rx,ENABLE); SPI_I2S_DMACmd(SPI1,SPI_I2S_DMAReq_Tx,ENABLE); /* while(Idex < BufferSize) { while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET); SPI_I2S_SendData(SPI1,SPI1_Buffer_Tx[Idex++]); //SPI1只管发送数据然后 如果SPI2数据寄存器中数据满的话 会发出DMA请求接着DMA干活 while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RXNE)==RESET); SPI2_Buffer_Rx[Idex++]=SPI_I2S_ReceiveData(SPI2); } */ while(DMA_GetFlagStatus(DMA1_FLAG_TC3)==RESET); while(DMA_GetFlagStatus(DMA1_FLAG_TC4)==RESET); textstatus=TempComp(SPI1_Buffer_Tx,SPI2_Buffer_Rx,BufferSize); } TextStatus TempComp(uint16_t*temp1,uint16_t* temp2,uint16_t length) { while(length--) { if(*temp1 != *temp2) { return FAILED; } temp1++; temp2++; } return PASSED; } ——————————————
|