AnsweredAssumed Answered

STM32L051: clear PWM output on COMP2 trigger.

Question asked by van_hest.bart on Aug 26, 2015
Latest reply on Aug 28, 2015 by Amel N
I am quite new to STM32 and try to do my second application using an STM32L051K8T6 where I need a PWM signal that is aborted when current rises above a treshold. It didn't take me too long to get this working on the STM32F0DISCOVERY board (software development/proof of concept when the final hardware wasn't ready yet), but somehow I can't get it to work on the L051.

The toolchain I use is Keil uVision5 (STM Cortex M0/M0+ license), initial setup code is generated with STM32CubeMX, and I am debugging the target board through the STM32F0Discovery builtin STLink v2 interface.

Little more information about the application:
- I currently have all the buses and core running from the MSI oscillator at 4.194MHz.
- COMP2 uses 1/4 internal VREF on the INM input and the voltage on PA3 on the INP input and is used in Fast mode.
- TIM2 is used as the PWM generator in PWM mode 1 using an up counter with output inverted. Channel1 is used as PWM output exiting the chip at PA5
- When the signal on PA3 rises above 1/4 internal VREF the PWM cycle should be aborted, exacly like described in document RM0377 (STM32L0x1 reference manual) section 16.3.11

Results so far:
- COMP2 is working; I can see bit 30 of COMP2_CSR changing and I am getting interrupts.
- PWM itself is working. Signal waveforms on the scope look as expected. 
- PWM cycle abort is NOT working.

The code used to initialise TIM2 and COMP, as generated by STM32CubeMX:

01.htim2.Instance = TIM2;
02.  htim2.Init.Prescaler = 41;
03.  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
04.  htim2.Init.Period = 9;
05.  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
06.  HAL_TIM_Base_Init(&htim2);
07. 
08. 
09.  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
10.  HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig);
11. 
12. 
13.  HAL_TIM_PWM_Init(&htim2);
14. 
15. 
16.  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
17.  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
18.  HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig);
19. 
20. 
21.  sConfigOC.OCMode = TIM_OCMODE_PWM1;
22.  sConfigOC.Pulse = 9;
23.  sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
24.  sConfigOC.OCFastMode = TIM_OCFAST_ENABLE;
25.  HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1);
26. 
27. 
28.  sClearInputConfig.ClearInputState = ENABLE;
29.  sClearInputConfig.ClearInputSource = TIM_CLEARINPUTSOURCE_ETR;
30.  sClearInputConfig.ClearInputPolarity = TIM_CLEARINPUTPOLARITY_NONINVERTED;
31.  sClearInputConfig.ClearInputPrescaler = TIM_CLEARINPUTPRESCALER_DIV1;
32.  sClearInputConfig.ClearInputFilter = 0;
33.  HAL_TIM_ConfigOCrefClear(&htim2, &sClearInputConfig, TIM_CHANNEL_1);
34. 
35. 
36.  HAL_TIMEx_RemapConfig(&htim2, TIM2_ETR_COMP2_OUT);


01.hcomp2.Instance = COMP2;
02.hcomp2.Init.InvertingInput = COMP_INVERTINGINPUT_1_4VREFINT;
03.hcomp2.Init.NonInvertingInput = COMP_NONINVERTINGINPUT_IO1;
04.hcomp2.Init.LPTIMConnection = COMP_LPTIMCONNECTION_DISABLED;
05.hcomp2.Init.OutputPol = COMP_OUTPUTPOL_NONINVERTED;
06.hcomp2.Init.Mode = COMP_MODE_HIGHSPEED;
07.hcomp2.Init.WindowMode = COMP_WINDOWMODE_DISABLED;
08.hcomp2.Init.TriggerMode = COMP_TRIGGERMODE_IT_RISING;
09.HAL_COMP_Init(&hcomp2);

Both TIM2 and COMP peripherals are started in IT mode using HAL_COMP_Start_IT and HAL_TIM_PWM_Start_IT.

When looking at the registers, I see no strange things.
TIM2->CCMR1.OC1CE is enabled.
TIM2->SMCR.ETPS is 0x00
TIM2->SMCR.ECE is 0
TIM2->SMCR.ETF is 0x00
TIM2->SMCR.ETP is 0
TIM2->OR.ETR_RMP is 0x06

etcetera.

I also checked the code sample A.9.10 in RM0377, but that one contains an error: that sample tries to set the OCCS bit in TIM2->SMCR. This bit is not available on STM32L0x1 devices...

Can anyone point me in the right direction?

Outcomes