|
SPI read data via DMA fail?
[复制链接]
各位高手大家好~ 有個SPI DMA的問題請大家。 小弟使用開發平台為STM32F103ZE。 目前若不用DMA的SPI是正常可以使用的。 但目前SPI傳的速度不夠快,若照SPI 6Mbps傳送4kB data應該約花5.3 ms, 但是目前我從波形上量到的時間約是12~13ms, 因為我的應用上需要更快的速度, 而Slave最快的SPI速度只有支援到 6 Mbps,所以ST的速度也沒辦法再快了@@ 而目前的沒有用DMA的話,就是每傳送一個byte就要等RX/TX flag,這樣的效率十在不好, 下列是我照開發手冊上做的SPI DMA範例,但目前仍無法正常從Slave端拿到正確的data, 不知道有沒有哪位高人可以指點我一下,謝謝。
void API_SPI_DMA_Read_Bytes(u16 *pu16Buffer, u16 u16Len) { SPI_InitTypeDef SPI_InitStructure; DMA_InitTypeDef DMA_InitStructure; DMA_Cmd(SPI_DMA_CHANNEL, DISABLE); //DMA_DeInit(SPI_DMA_CHANNEL); DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&SPI1->DR;//SPI1_DR_Base; DMA_InitStructure.DMA_MemoryBaseAddr = (u32)pu16Buffer; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = (u32)u16Len/2; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_MemoryDataSize_HalfWord; 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(SPI_DMA_CHANNEL, &DMA_InitStructure);
SPI_InitStructure->SPI_Direction = SPI_Direction_2Lines_RxOnly;//SPI_Direction_1Line_Rx;
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_NSS_Hard; SPI_InitStructure->SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; // 6 Mbps SPI_InitStructure->SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure->SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure);
SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Rx, ENABLE);
/* Enable SPI1 */ SPI_Cmd(SPI1, ENABLE);
// SPI_DMA_CHANNEL = DMA1_Channel2 DMA_Cmd(SPI_DMA_CHANNEL, ENABLE);
/* Chip Select low */ SPI_CS_LOW(); while (u16Len>0) { //while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); //SPI_I2S_ReceiveData(SPI1); API_SPI_SendByte(0); u16Len -= 2; } // SPI_DMA_RX_FLAG = DMA1_FLAG_TC2 while(!DMA_GetFlagStatus(SPI_DMA_RX_FLAG));
/* Chip Select high */ SPI_CS_HIGH(); }
|
|