dma_driver_type dma_drivers[] =
{
[eDMA1_CH01] =
{
.dma_config =
{
.irq_vector = DMA1_Channel1_IRQn,
.mpriority = 2,
.spriority = 1,
.controller = DMA1,
.stream = DMA1_Channel1,
.channel = LL_DMA_CHANNEL_1,
.clear_flags = LL_DMA_ClearFlag_TC1,
.isavtive_flags = LL_DMA_IsActiveFlag_TE1,
.isenable_it = LL_DMA_IsEnabledIT_TC,
},
.dma_sem = NULL,
},
};
void sDMA_Configuration(dma_driver_type *driver, uint8_t *p_txbuf, uint8_t *p_rxbuf)
{
LL_DMA_InitTypeDef DMA_InitStructure;
if( driver->dma_sem == NULL)
{
driver->dma_sem = osSemaphoreNew(1, 0, NULL);
}
/* Enable DMA peripheral clock */
if ( driver->dma_config.controller == DMA1 )
{
RCC->AHBENR |= RCC_AHBENR_DMA1EN;
}
else
{
RCC->AHBENR |= RCC_AHBENR_DMA2EN;
}
NVIC_SetPriority(driver->dma_config.irq_vector, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), driver->dma_config.mpriority, driver->dma_config.spriority));
NVIC_EnableIRQ(driver->dma_config.irq_vector);
LL_DMA_DeInit(driver->dma_config.controller, driver->dma_config.channel);
/* Configure DMA controller to manage TX DMA request ----------*/
DMA_InitStructure.PeriphOrM2MSrcAddress = (uint32_t)p_txbuf;
DMA_InitStructure.PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_INCREMENT;
DMA_InitStructure.MemoryOrM2MDstIncMode = LL_DMA_MEMORY_INCREMENT;
DMA_InitStructure.PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_BYTE;
DMA_InitStructure.MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_BYTE;
//DMA_InitStructure.Mode = LL_DMA_MODE_CIRCULAR;
DMA_InitStructure.Mode = LL_DMA_MODE_NORMAL;
DMA_InitStructure.Priority = LL_DMA_PRIORITY_HIGH;
DMA_InitStructure.Direction = LL_DMA_DIRECTION_MEMORY_TO_MEMORY;
DMA_InitStructure.MemoryOrM2MDstAddress = (uint32_t)p_rxbuf;
DMA_InitStructure.NbData = (uint32_t)0;
LL_DMA_Init(driver->dma_config.controller, driver->dma_config.channel, &DMA_InitStructure);
}
void sDMA_EnableStream(dma_driver_type *driver, uint16_t length)
{
LL_DMA_SetDataLength(driver->dma_config.controller, driver->dma_config.channel, length);
driver->dma_config.clear_flags(driver->dma_config.controller);
/* Enable the DMA Stream Interrupt */
LL_DMA_EnableIT_TC(driver->dma_config.controller, driver->dma_config.channel);
/* Enable the DMA TX Stream */
LL_DMA_EnableChannel(driver->dma_config.controller, driver->dma_config.channel);
osSemaphoreAcquire(driver->dma_sem, osWaitForever);
}
void sDMA_Interrupt(dma_driver_type *driver)
{
//TX Interrupt
if(driver->dma_config.isavtive_flags(driver->dma_config.controller) || \
driver->dma_config.isenable_it(driver->dma_config.controller, driver->dma_config.channel))
{
LL_DMA_DisableChannel(driver->dma_config.controller, driver->dma_config.channel);
LL_DMA_DisableIT_TC(driver->dma_config.controller, driver->dma_config.channel);
driver->dma_config.clear_flags(driver->dma_config.controller);
osSemaphoreRelease(driver->dma_sem);
}
}
可以参考 |