STM32作为从机,打算用DMA来实现收发,配置如下:
void SPI1_DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
/* DMA1 Channel2 (triggered by SPI1 Rx event) Config */
DMA_DeInit(DMA1_Channel2);
DMA_InitStructure.DMA_PeripheralBaseAddr = SPI1_DR_Addr; //?? SPI1 ????(0x4001300C) ??(????)
// DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SRC_Buffer; //?? SRAM ????(????)
DMA_InitStructure.DMA_MemoryBaseAddr = (u16)SendD;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //???? ??-??
DMA_InitStructure.DMA_BufferSize = buffersize; //?? SPI1 ????
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //Êý¾Ý¿í¶È16λ
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //¹¤×÷ÔÚÕý³£»º´æģʽ
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel2, &DMA_InitStructure); //²»ÉèÖÃΪÄÚ´æµ½ÄÚ´æ´«Êä
DMA_ITConfig(DMA1_Channel2, DMA_IT_TC, ENABLE); //¿ªÆô½ÓÊÕÍê³ÉÖжÏ
/* Enable SPI1 DMA RX request */
SPI1->CR2 |= 1<<0; //½ÓÊÕ»º³åÇøDMAʹÄÜ
DMA_Cmd(DMA1_Channel2, ENABLE);
/* DMA1 Channel3 (triggered by SPI1 Tx event) Config */
DMA_DeInit(DMA1_Channel3);
//DMA_InitStructure.DMA_PeripheralBaseAddr = SPI1_DR_Addr; //?? ????(0x4001300C) ??(???)
DMA_InitStructure.DMA_PeripheralBaseAddr = SPI1_DR_Addr;
// DMA_InitStructure.DMA_MemoryBaseAddr = (u32)DST_Buffer; //?? SRAM ????(???)
DMA_InitStructure.DMA_MemoryBaseAddr = (u16)ReceivedD;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; //???? ??-??
DMA_InitStructure.DMA_BufferSize = buffersize; //?? SPI1Êý¾Ý¿í¶È
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Enable; //??????(??)
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //??????(??)
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //??????(??)
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //16λ
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //????,???????,?????
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; //????-?(??)
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //?????????
DMA_Init(DMA1_Channel3, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel3, DMA_IT_TC, ENABLE); //¿ªÆô·¢ËÍÍê³ÉÖжÏ
//DMA_ITConfig(DMA1_Channel3, DMA_IT_TE, ENABLE); //¿ªÆô·¢ËÍ´íÎóÖжÏ
/* Enable SPI1 DMA TX request */
SPI1->CR2 |= 1<<1; //·¢ËÍ»º³åÇøDMAʹÄÜ
DMA_Cmd(DMA1_Channel3, ENABLE); //ʹÄÜDMA
}
/*ÅäÖÃDAMÖжÏ*/
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel2_IRQn; //DMAͨµÀ2ÖжÏ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //Ö÷ÓÅÏȼ¶Îª2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //´ÓÓÅÏȼ¶Îª0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //Ö÷ÓÅÏȼ¶Îª2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //´ÓÓÅÏȼ¶Îª0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //
NVIC_Init(&NVIC_InitStructure);
为什么我的程序运行的时候不能正常收发数据呢?
每次程序都会运行到
B .
ENDP
ALIGN |