2020-10-30 07:11 AM
Hello.
I'm trying to play a music file using PWM with STM32F1. For this, I saved the sound data in flash memory with appropriate resolution. Using Timer 2 and DMA, I transfer the data to TIM2-> CCR1 register. Sampling Frequency 8 Khz.
DMA Circular Mode works fine. As soon as I set up the timer, It write the data to the CCR1 register and I getting the pwm at the out. This way I get a constantly repeating sound.
But I have a problem. I want the system to stop after Transfer is complete. For this I am running DMA in normal mode. After the transfer is complete, the DMA TC interrupt continues to come.
My init settings and DMA interrupt routines are as follows.
void TIM_PWM_Sound_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* GPIOA Configuration: Channel 1 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* DMA1 Channel2 Config */
DMA_DeInit(DMA1_Channel2);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)TIM2_CCR1_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)sound_data;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 256;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel2, &DMA_InitStructure);
/* DMA1 Channel5 enable */
//DMA_Cmd(DMA1_Channel2, ENABLE);
/* TIM1 Peripheral Configuration --------------------------------------------*/
/* Time Base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = 35-1;//8Khz
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 256-1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* Channel 1 Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 15;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_Init(&NVIC_InitStructure);
DMA_ITConfig(DMA1_Channel2, DMA_IT_TC, ENABLE);
/* TIM2 Update DMA Request enable */
TIM_DMACmd(TIM2, TIM_DMA_Update, ENABLE);
/* TIM2 counter enable */
TIM_Cmd(TIM2, ENABLE);
}
void DMA1_Channel2_IRQHandler(void)
{
GPIOB->ODR^=GPIO_Pin_2;
TIM_Cmd(TIM2,DISABLE);
TIM2->CCR1=0;
DMA_Cmd(DMA1_Channel2, DISABLE);
/* DMA1 Channel5 enable */
DMA_ClearITPendingBit(DMA_IT_TC);
}
The system should halt after the transfer is complete. And I have to restart whenever I want. What could be the problem? What should I do to start it up again?
Best Regards
Solved! Go to Solution.
2020-10-30 02:00 PM
I don't use SPL, but I'd say you need to have
DMA_ClearITPendingBit(DMA1_IT_TC2);
at the end of the ISR.
JW
2020-10-30 02:00 PM
I don't use SPL, but I'd say you need to have
DMA_ClearITPendingBit(DMA1_IT_TC2);
at the end of the ISR.
JW
2020-10-31 03:24 AM
@Community member Thank you for the reply. Yes I made a mistake about that. Now It Goes to Transfer complete interrupt when data transfer is complete. But I have a problem like this. With DMA running in Normal Mode, I couldn't figure out how to restart it.
What should I do to restart DMA after data transfer is complete?
2020-11-01 03:01 AM
If you want to repeat the same transfer - i.e. same source and destination addresses, same content of DMA_CCR - write 0 to DMA_CCR.EN, write the required number of transfers to DMA_NDTR, and reenable channel by writing 1 to DMA_CCR.EN.
There surely are incantations in SPL to do that - as I've said, I don't use SPL. Try to review the available examples.
JW