初中生
最后登录1970-1-1
在线时间 小时
注册时间2020-3-30
|
楼主 |
发表于 2021-4-7 08:28:09
|
显示全部楼层
贴一下SPI读写一个字节的函数,在函数里需要配置DMA,后面的都是调用这个函数,大家有什么更好的处理方式;这种方式效果还没有我直接不用DMA好呢
u8 SPI1_ReadWriteByte(u8 TxData)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_Cmd(DMA1_Channel2,DISABLE);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)SPI1_DR_Addr;
// Äú′æμØÖ·(òa′«êäμıäá¿μÄÖ¸Õë)
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SPI_RX_BUF;
// ·½Ïò£o′óÄú′æμ½íaéè
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
// ′«êä′óD¡
DMA_InitStructure.DMA_BufferSize = 1;
// íaéèμØÖ·2»Ôö
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
// Äú′æμØÖ·×ÔÔö
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
// íaéèêy¾Yμ¥λ
DMA_InitStructure.DMA_PeripheralDataSize =
DMA_PeripheralDataSize_Byte;
// Äú′æêy¾Yμ¥λ
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
// DMAģ꽣¬ò»′λòÕßÑ-»·Ä£ê½
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal ;
//DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
// óÅÏ輶£oÖD
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
// ½ûÖ1Äú′æμ½Äú′æμÄ′«êä
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
// ÅäÖÃDMAí¨μà
DMA_Init(DMA1_Channel2, &DMA_InitStructure);
DMA_DeInit(DMA1_Channel3);
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SPI_TX_BUF;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_Init(DMA1_Channel3, &DMA_InitStructure);
SPI_TX_BUF[0]= TxData;
DMA_ClearFlag(DMA1_FLAG_TC2);
DMA_ClearFlag(DMA1_FLAG_TC3);
DMA_Cmd (DMA1_Channel3,ENABLE);
DMA_Cmd (DMA1_Channel2,ENABLE);
SPI_I2S_DMACmd(SPI1,SPI_I2S_DMAReq_Tx, ENABLE);
SPI_I2S_DMACmd(SPI1,SPI_I2S_DMAReq_Rx, ENABLE);
while (!DMA_GetFlagStatus(DMA1_FLAG_TC2));
while (!DMA_GetFlagStatus(DMA1_FLAG_TC3));
DMA_Cmd (DMA1_Channel3,DISABLE);
DMA_Cmd (DMA1_Channel2,DISABLE);
DMA_ClearFlag(DMA1_FLAG_TC2);
DMA_ClearFlag(DMA1_FLAG_TC3);
return (SPI_RX_BUF[0]);
}
|
|