cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103 + PWM+ DMA + HAL?

asrock70
Associate II
Posted on November 26, 2015 at 17:38

I have STM32F103 working on 8MHz.

If I InitPWM on Ch1 Tim3

void MX_TIM3_Init(void) 
{ 
TIM_ClockConfigTypeDef sClockSourceConfig; 
TIM_MasterConfigTypeDef sMasterConfig; 
TIM_OC_InitTypeDef sConfigOC; 
htim3.Instance = TIM3; 
htim3.Init.Prescaler = 0; 
htim3.Init.CounterMode = TIM_COUNTERMODE_UP; 
htim3.Init.Period = 9; 
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; 
HAL_TIM_Base_Init(&htim3); 
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; 
HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig); 
HAL_TIM_PWM_Init(&htim3); 
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; 
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; 
HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig); 
sConfigOC.OCMode = TIM_OCMODE_PWM1; 
sConfigOC.Pulse = 1; 
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; 
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; 
HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1); 
}

and star ti with

HAL_TIM_PWM_Start_IT(&htim3, TIM_CHANNEL_1);

all working OK and output generate 125ns pulse on 800kHz (1.25us) If replace Start_IT with DMA version

uint32_t pData[1]={6}; 
HAL_TIM_PWM_Start_DMA(&htim3, TIM_CHANNEL_1,pData,1); 

all working OK and output generate 70ns pulse on 800kHz (1.25us),but if use

uint32_t pData[16]={3,3,3,3,3,3,3,3,0,0,0,0,0,0,0,0}; 
HAL_TIM_PWM_Start_DMA(&htim3, TIM_CHANNEL_1,pData,16);

this genreate 375ns its OK but frequency is 400kHz(2,5us) If I trace and look on reg CCR1 I see 3,0,3,0,3,0,3,0,3,0,3,0,3,0,3,0

What's

wrong

,

what's the problem

?

#he-chose-poorly
4 REPLIES 4
Posted on November 27, 2015 at 12:03

void MX_TIM3_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
TIM_OC_InitTypeDef sConfigOC;
htim3.Instance = TIM3;
htim3.Init.Prescaler = 0;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = (72 / 8) - 1;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
HAL_TIM_Base_Init(&htim3);
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig);
HAL_TIM_PWM_Init(&htim3);
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig);
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 1;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1);
}
uint16_t pData[16]={3,3,3,3,3,3,3,3,0,0,0,0,0,0,0,0}; // Half-Words
HAL_TIM_PWM_Start_DMA(&htim3, TIM_CHANNEL_1,pData,16);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on November 27, 2015 at 12:13

Perhaps related?

[DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Java/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Java/STM32F1%20PWM%20wit%20DMA&FolderCTID=0x01200200770978C69A1141439FE559EB459D758000F9A0E3A95BA69146A17C2E80209ADC21&TopicsView=https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Java/AllItems.aspx&currentviews=19]https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Java/Flat.aspx?RootFolder=%2fpublic%2fSTe2ecommunities%2fmcu%2fLists%2fSTM32Java%2fSTM32F1%20PWM%20wit%20DMA&FolderCTID=0x01200200770978C69A1141439FE559EB459D758000F9A0E3A95BA69146A17C2E80209ADC21&TopicsView=https%3A%2F%2Fmy.st.com%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2FSTM32Java%2FAllItems.aspx¤tviews=19

The joys of using HAL I guess

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
asrock70
Associate II
Posted on November 28, 2015 at 16:47

Thanks you . With half word you have tue,

According to my

htim3.Init.Period = (clock of timer / output frequency)-1 

htim3.Init.Period = ( 8MHz/ 800kHz)-1  htim3.Init.Period = 9 is correctly.  However, I foundanother problém.  /* TIM3 init function */

void MX_TIM3_Init(void)

{ TIM_ClockConfigTypeDef sClockSourceConfig;

TIM_MasterConfigTypeDef sMasterConfig;

TIM_OC_InitTypeDef sConfigOC; htim3.Instance = TIM3;

htim3.Init.Prescaler = 0;

htim3.Init.CounterMode = TIM_COUNTERMODE_UP;

htim3.Init.Period = 9;

htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

HAL_TIM_Base_Init(&htim3); sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig); HAL_TIM_PWM_Init(&htim3); sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig); sConfigOC.OCMode = TIM_OCMODE_PWM1;

sConfigOC.Pulse = 0;

sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;

sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;

HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1); } uint16_t pData[25]={3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,6,6,6,6,6,6,6,6,0};

HAL_TIM_PWM_Start_DMA(&htim3, TIM_CHANNEL_1,(uint32_t*)pData,25);   I thing, this send 16x Log1 375ns /Log0 875ns + 8x Log1 750ns / Log0 500ns  but hw logic analyser show this 0690X00000605JvQAI.png   

Timing

is OK, but problem is in

the number of pulses

instead of 16 short have 19 short pulses and

instead of8long have 9 long pulses

Which

could be the

problem?

Posted on November 28, 2015 at 18:24

Try running the buses/CPU faster, and ensure the pattern buffer is in RAM

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..