新手入门
- 积分
- 3
- 金钱
- 3
- 注册时间
- 2023-4-27
- 在线时间
- 0 小时
|
1金钱
1.SPI通过硬件片选的方式自动收发数据,不加DMA是没有问题的,加DMA收发数据失败;
2.DMA配置为0,1,2,3通道,分别是内存->SPI1;SPI1->内存;SPI2->内存;内存->SPI2;
3.一调用发送函数,每次进DMA1_Steream2中断中,写了两天没信心了。。
配置及收发函数如下,跪求大佬指导;
- <blockquote>#define SPI1_DMA_TX_STREAM DMA1_Stream0
复制代码
//SPI_DMA 发送/接收;
void SPI_TransmitReceive(uint8_t Axis, uint8_t *pTxData, uint8_t *pRxData,uint16_t Size)
{
if((pTxData ==NULL) || (pRxData ==NULL) || (Size ==0UL))return ;
volatile SPI_TypeDef* pSpi = Axises[Axis];
uint8_t TxCnt = Size;uint8_t RxCnt = Size;
uint32_t i=0;
//禁止DMA1传输
DMA1_Stream0->CR &= ~DMA_SxCR_EN; DMA1_Stream1->CR &= ~DMA_SxCR_EN; DMA1_Stream2->CR &= ~DMA_SxCR_EN; DMA1_Stream3->CR &= ~DMA_SxCR_EN;
SPI1_DMA_TX_STREAM->NDTR = 0x00000000;
SPI1_DMA_TX_STREAM->NDTR = BUFFER ;
SPI2_DMA_TX_STREAM->NDTR = 0x00000000;
SPI2_DMA_TX_STREAM->NDTR = BUFFER ;
SPI2_DMA_TX_STREAM->NDTR = 0x00000000;
SPI2_DMA_TX_STREAM->NDTR = BUFFER ;
SPI2_DMA_RX_STREAM->NDTR = 0x00000000; //传输长度
SPI2_DMA_RX_STREAM->NDTR = BUFFER ;
//SPI1 ->Stream0 存储器地址 外设地址
DMA1_Stream0->M0AR = *(__IO uint32_t*)pTxData; DMA1_Stream0->PAR = pSpi->TXDR;
//SPI1 ->Stream1 外设地址 存储器地址
DMA1_Stream1->PAR = *(uint32_t*)pSpi->RXDR; DMA1_Stream1->M0AR = *(__IO uint32_t*)pRxData;
//SPI2 ->Stream2 存储器地址 外设地址
DMA1_Stream2->M0AR = *(__IO uint32_t*)pTxData; DMA1_Stream2->PAR = pSpi->TXDR;
//SPI2 ->Stream3 外设地址 存储器地址
DMA1_Stream3->PAR = *(uint32_t*)pSpi->RXDR; DMA1_Stream3->M0AR = *(__IO uint32_t*)pRxData;
DMA1_Stream0->CR |= DMA_SxCR_TCIE; DMA1_Stream1->CR |= DMA_SxCR_TCIE;DMA1_Stream2->CR |= DMA_SxCR_TCIE;DMA1_Stream3->CR |= DMA_SxCR_TCIE;
// pSpi->CR2 = 5; //TSIZE
/* Enable EOT, DXP, UDR, OVR, FRE, MODF and TSERF interrupts */
// pSpi->IER |= SPI_IER_EOTIE | SPI_IER_DXPIE | SPI_IER_UDRIE | SPI_IER_OVRIE | SPI_IER_TIFREIE |SPI_IER_MODFIE |SPI_IER_TSERFIE;
pSpi->RXDR;
//主机发送打开
pSpi->CR1 |= SPI_CR1_CSTART;
//使能DMA1传输 //开始传输
while((pSpi->SR & SPI_SR_TXP) == 0);
DMA1_Stream0->CR |= DMA_SxCR_EN;
DMA1_Stream1->CR |= DMA_SxCR_EN;
DMA1_Stream2->CR |= DMA_SxCR_EN;
DMA1_Stream3->CR |= DMA_SxCR_EN;
while((DMA1->LISR & DMA_LISR_TCIF0 )==0 || (DMA1->LISR & DMA_LISR_TCIF1 )==0 || (DMA1->LISR & DMA_LISR_TCIF2 )==0
|| (DMA1->LISR & DMA_LISR_TCIF3 )==0 ){};
//DMA1_Stream0->NDTR -= 1; DMA1_Stream1->NDTR |= 1;DMA1_Stream2->NDTR -= 1;DMA1_Stream3->NDTR -= 1; //传输长度
// while((TxCnt >0)||(RxCnt > 0))
// {
// if((pSpi->SR & SPI_SR_TXP) && (TxCnt > 0))
// {
// *((__IO uint8_t *)pSpi->TXDR) = *((const uint8_t *)pTxData);
// TxCnt--;
// }
// if((pSpi->SR & SPI_SR_RXP) && (RxCnt > 0))
// {
// *(( uint8_t *)pRxData) = *((__IO uint8_t *)pSpi->RXDR);
// RxCnt--;
// }
// if(i > 29999)break;
// i++;
// }
pSpi->IFCR = (SPI_IFCR_EOTC | SPI_IFCR_TXTFC) ;
}
uint32_t XX_DMA_WR(uint8_t Axis, uint8_t Addr, uint32_t Value)
{
uint8_t TDate[5] = {0}; uint8_t RDate[5] = {0};
TDate[0] = Addr | 0x80;
TDate[1] = Value >> 24 ;
TDate[2] = (Value >> 16) & 0xFF ;
TDate[3] = (Value >> 8) & 0xFF;
TDate[4] = Value & 0xFF ;
SPI_TransmitReceive(Axis, TDate, RDate,5);
Value |= RDate[1] << 24;
Value |= RDate[2] << 16;
Value |= RDate[3] << 8;
Value |= RDate[4];
return Value;
}
|
|