Loading...
 

Zephyr project on STM32

   Zephyr Workbench, a VSCode extension to manage Zephyr on STM32.
It enables users to easily create, develop, and debug Zephyr applications.
Main features:
  • Install host dependencies.
  • Import toolchain and SDK.
  • Create, configure, build and manage apps.
  • Debug STM32.
You can directly download it from the VSCode marketplace
For more details, visit the Zephyr Workbench

System Workbench for STM32


NO PWM Whatsoever - CubeMX DOES NOT include the HAL_TIM_PWM_Start functions

Hi Everyone,

Use of STM32F103C8T6 64K flash 20K RAM (no DAC peripheral). Tried several STM32F103C8T6 chips but no sigar.

I just want to be able to configure a PWM signal solely by using CubeMX.

At the end of the day the PWM signal will be fed to a PWM/DC converter to become an analog signal fluctuating in the audible range however the carrying PWM signal frequency itself being say 48kHz.

Please no standard peripheral library solutions. ST uses the HAL library in CubeMX and I want to stick to CubeMX and the use of the HAL library, nothing else. Pitty ST started throwing different libraries for same ARM chips. It’s already enough complicated.

Prescaler: I tried different values ranging up to 65535 but to no avail, dead LED’s and no PWM signal nowhere. Different sConfigOC.Pulse (pulse width) values but even that has no effect.

The STM32 timers are everything but intuitively. It should be straightforward. CubeMX PWM configuration should give a signal whatsoever without having to manually change anything in the code but it doesn’t.

Screenshots attached.

Advices welcome.

Thx,
Ben

Hi Ben
Thank you. Without realizing it you have just solved a problem for me by posting your 2nd picture showing your clock tree. It helped because it shows the the TIM peripherals are clocked at 2x the APBx rate of other peripherals.I had to double my prescaler divider without knowing why - now I do know why - thanks.

I Have Just got PWM Output working for channel-1 of General purpose TIM10 of a STM32F407VGt6 to drive a standard Futaba FPS148 servo. I am Using HAL version 1.4.4 in STM32 Cube F4 version 1.11.0. Consequently I will look at your work and see if I can spot any subspace anomalies.

In the meanwhile find attached a working TIM register snapshot. And my initialization code.

For what its worth I had exactly the same NO PWM problem and found that the TIM10->CCMR1(output) register had not been configured - it was 0 when it should have been 0x68. The reason was because the “HOW TO USE” instructions at the top of the stm32f4xx_hal_tim.c had a bullet list of instructions which I WRONGLY assumed contained ONE instruction per bullet - I had missed the 2nd instruction on one bullet to call function HAL_TIM_PWM_ConfigChannel(). It is possible others have missed that too.

To set the PWM level I load TIM10->CCR1 with a number between 1260 and 9660 (centered around 5460) because the servo PWM pulse is only allowed to be high for the first .3ms to 2.3ms of a 15.6ms period (in my application anyway. Officially/generically/typically it’s 1 to 2ms in a 20ms period, says Wiki)


Hi Bob1,

I suspect CubeMX didn’t include HAL_TIM_PWM_Start to activate the PWM signal.

Greets,
Ben

Yes - that would fail.

Please let us know what solves it.

Thank you.



Yes Bob1,

I only had to add HAL_TIM_PWM_Start four times for each output. Well that’s solved then.

I again used CubeMX to generate the code necessary to configure one timer for PWM. With the STM32F103C8T6 only TIMER 2,3 and 4 can be configured for PWM. CubeMX did everything except it did not start the PWM hence HAL_TIM_PWM_Start I had to manually add in main.c. I guess there’s also a HAL_TIM_PWM_Stop somewhere to be found otherwise I wouldn’t know why ST created this extra mandatory function (to then forget about it in CubeMX).

Your PWM_SERVO_wkg1.png screenshot was useful in seeing the necessities.

Thx,
Ben


Well done Ben, nice work, glad you are sorted. And thank you again.

Additional conclusion: The PWM for what I intended to use it won’t be possible because of the limited frequency range.

A/D resolution needs to be 16 bit, thus the full counter range from 0 to 65535. 72 Mhz counter clock speed ( prescaler and divider both being zero) already brings the max PWM frequency to 72 Mhz / 65536 = approximately 1 khz sample frequency. Dead end. Nevertheless it was intersting.

Greets!

Just as well you don’t need all 32bits of a 32bit timer.

I am also not able to generate PWM on STM32F4 Dicovery. I have tried TIM5 channel 2 and now TIM2 channel. All this code is generated by Cube MX. I am just trying to get any type of PWM out of the STM, it can always be configured and tweeked later. Can anyone point out what I am missing??

I see the green LED blinking on PD12 but the PA1 is all dead. It gives a straight line on the oscilloscope.


pininit() and togglepin() are my own functions.




/**
******************************************************************************
* @file main.c
* @author Ac6
* @version V1.0
* @date 01-December-2013
* @brief Default main function.
******************************************************************************

  • /

  1. include “stm32f4xx.h”
  2. include “stm32f4_discovery.h”
  3. include “compact_functions.h”




static void SystemClock_Config(void);
static void MX_TIM2_Init(void);


TIM_HandleTypeDef htim2;




int main(void)
{
HAL_Init();

SystemClock_Config();

pininit_output(GPIOD, 12);
pininit_alt_pp(GPIOA, 1);

MX_TIM2_Init();

__HAL_RCC_TIM2_CLK_ENABLE();
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);

for(;;)
{
togglepin(GPIOD, 12);
delay_ms(2000);
}
}




/** System Clock Configuration

  • /

void SystemClock_Config(void)
{

RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;

__PWR_CLK_ENABLE();

__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
HAL_RCC_OscConfig(&RCC_OscInitStruct);

RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1
|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);

HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}




void MX_TIM2_Init(void)
{

TIM_MasterConfigTypeDef sMasterConfig;
TIM_OC_InitTypeDef sConfigOC;

htim2.Instance = TIM2;
htim2.Init.Prescaler = 0;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 1024;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
HAL_TIM_PWM_Init(&htim2);

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig);

sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 384;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2);
}


void pininit_alt_pp(GPIO_TypeDef *GPIOx, uint32_t pinno)
{
pininit(GPIOx, pinno, GPIO_MODE_AF_PP,GPIO_SPEED_FREQ_LOW,GPIO_NOPULL);
}


My bad, I wasn’t specifying the source GPIO_AF1_TIM2 of the alternate function in pininit().

But I am still amazed why do I have to include the macro _HAL_RCC_TIM2_CLK_ENABLE(); in the main function. Shouldn’t the HAL initilization functions call it? Even the Cube MX doesn’t take care of it. And if I remove it from the code, the timer doesn’t run.

Also, the Cube MX doesn’t generate the pin initialization code for the PA1, i.e. the PWM channel 2 of TIM2. Does anyone know why?

 

Newest Forum Posts

  1. Монтаж камина с грилем в Москве - установка и барбекю by KpddomErorb, 2025-05-10 18:28
  2. SPI on Nucleo_STMH533RE by royjamil, 2025-05-04 20:13
  3. SPI on Nucleo_STMH533RE by higginsa1, 2025-03-25 07:37
  4. SPI on Nucleo_STMH533RE by royjamil, 2025-03-23 11:31
  5. SPI on Nucleo_STMH533RE by higginsa1, 2025-03-23 09:33
  6. Configuring DMA for ADC in SW? by sam.hodgson, 2025-03-04 12:58
  7. Build a project in "release" mode by info@creosrl.it, 2025-02-20 18:12
  8. Build a project in "release" mode by info@creosrl.it, 2025-02-20 17:05
  9. Build a project in "release" mode by tang, 2025-02-20 10:36
  10. Build a project in "release" mode by info@creosrl.it, 2025-02-19 17:35

Last-Modified Blogs