读写函数
需要重写读写函数
void spimTransferBytes(uint8_t port, uint8_t* wrBuf, uint8_t* rdBuf, uint16_t len)
{
SPI_TypeDef* SPIx;
uint8_t spiBuf = 0;
DMA_Channel_TypeDef * dmaChannel[] =
{
DMA1_Channel2, DMA1_Channel3, //SPI1 Rd, Wr DMA channel
DMA1_Channel4, DMA1_Channel5, //SPI2 Rd, Wr DMA channel
};
uint8_t dmaIndex = 0;
if(len == 0 || port >= HW_SPI_MAX)
return;
SPIx = spimGroup[port];
if(SPIx == SPI2)
dmaIndex = 2;
dmaChannel[dmaIndex]->CPAR = (uint32_t)(&SPIx->DR); //peripheral's address
dmaChannel[dmaIndex]->CMAR = (uint32_t)rdBuf; //memory's addrss
dmaChannel[dmaIndex]->CNDTR = len ; //the length of transfer
dmaChannel[dmaIndex]->CCR = (0 << 14) | //peripheral to memory
(3 << 12) | //the priority is highest.
(0 << 11) | //the width of memory transfer : 8bit
(0 << 10) |
(0 << 9) | //the width of peripheral transfer : 8bit
(0 << 8) |
(1 << 7) | //the address of memory: increase
(0 << 6) | //the address of peripheral: fixed
(0 << 5) | //not circle mode
(0 << 4) | //transfer direction: peripheral to memory
(0 << 3) | //disable the interrupt of transfer error.
(0 << 2) | //disable the interrupt of transfer half.
(0 << 1) | //disable the interrupt of transfer complete.
(0); //Disable channel.
if(rdBuf == NULL)
{
dmaChannel[dmaIndex]->CMAR = (uint32_t)&spiBuf; //memory's addrss
dmaChannel[dmaIndex]->CCR &= (~(1 << 7)); //the address of memory: fixed
}
dmaChannel[dmaIndex + 1]->CPAR = (uint32_t)(&SPIx->DR);
dmaChannel[dmaIndex + 1]->CNDTR = len;
dmaChannel[dmaIndex + 1]->CMAR = (uint32_t)wrBuf;
dmaChannel[dmaIndex + 1]->CCR = (0 << 14) |
(3 << 12) |
(0 << 11) |
(0 << 10) |
(0 << 9) |
(0 << 8) |
(1 << 7) |
(0 << 6) |
(0 << 5) |
(1 << 4) | //transfer direction: memory to peripheral
(0 << 3) |
(0 << 2) |
(0 << 1) |
(0);
if(wrBuf == NULL)
{
dmaChannel[dmaIndex + 1]->CMAR = (uint32_t)&spiBuf;
dmaChannel[dmaIndex + 1]->CCR &= (~(1 << 7));
}
SPIx->DR;
//Wait for DMA finish
dmaChannel[dmaIndex]->CCR |= 0x01;
dmaChannel[dmaIndex + 1]->CCR |= 0x01;
while(dmaChannel[dmaIndex]->CNDTR); //Because the SPI is set to FullDuplex, so only check one channel.
dmaChannel[dmaIndex]->CCR = 0x00;
dmaChannel[dmaIndex + 1]->CCR = 0x00;
}
|