2020-01-10 12:31 AM
Help find the answer. I want to run TIM2 in trigger mode. If you connect ETR1 to Vdd, then the current at input ETR1 will be 45 mA. If you connect it to Vdd through a resistor, then the timer does not work. Where can I find a diagram of the correct connection of the trigger source? Why is there so much current in this mode?
2020-01-10 12:43 AM
> If you connect ETR1 to Vdd, then the current at input ETR1 will be 45 mA
Something is terribly wrong.
Do you have CH1 set as input (i.e. capture)? Read out and check/post the content of relevant GPIO and TIM registers.
What board is this? Isn't there something external connected to the same pin?
I don't use Cube/CubeMX.
JW
2020-01-10 12:58 AM
No, nothing else is connected to this contact. And if you use this pin as a simple GPIO input, then the current will be normal.
2020-01-10 12:58 AM
45 mA is way too much for any I/O pin to handle, see absolute maximum ratings in the datasheet. Consider your MCU damaged, and get a replacement. This is most certainly a hardware problem, unless PA0 is somehow set as output. The setup looks right to me, but I don't trust CubeMX anymore even to get the pin setup right, so check the GPIOA registers against the reference manual.
Check the board for defects, bad soldering, etc. Use a genuine debug adatpter instead of cheap aftermarket clones if your Vdd is lower than 3.3V.
2020-01-10 01:01 AM
As I've said above, you have to set CH1 to input - note that PA0 is shared between ETR and CH1.
You have TIMx_CCMR1.CC1S=0b00, i.e. output (compare). Maybe it would be enough not to enable it by setting TIMx_CCER.CC1E.
JW
2020-01-10 02:31 AM
Thanks! Everything worked with this setting.
But is there an appnote where you can read about setting up timers in CubeMx.
2020-01-10 02:46 AM
So the cube has enabled CH1 output when it shouldn't (or is the code touching TIM2 CH1 elsewhere?). Lets fix that, using the timer without HAL.
TIM2->SMCR =
TIM_SMCR_SMS_1|TIM_SMCR_SMS_2| // slave mode selection trigger mode
TIM_SMCR_TS_0|TIM_SMCR_TS_1|TIM_SMCR_TS_2| // trigger selection external trigger (ETRF)
0;
TIM2->CCMR1 =
TIM_CCMR1_OC2M_2|TIM_CCMR1_OC2M_1|TIM_CCMR1_OC2M_0| // OC 2 mode PWM 2
TIM_CCMR1_OC2PE| // preload enable - do you really need that?
0;
TIM2->CCER = TIM_CCER_CC2E; // channel 2 output enable
TIM2->PSC = 48000 - 1;
TIM2->ARR = 2000; // timer period, didn't you mean 2000 - 1 ?
TIM2->CCR2 = 1000; // duty cycle
TIM2->EGR = TIM_EGR_UG; // activate preloaded value
TIM2->CR1 = TIM_CR1_OPM; // one pulse mode
This looks like a quite dangerous bug, which version of CubeMX and HAL do you have?
2020-01-10 02:52 AM
Thanks! Version 5.3.0
2020-01-10 03:00 AM
> But is there an appnote where you can read about setting up timers in CubeMx.
Haven't found any yet.
There are a couple of application notes on timers, dealing with advanced use cases, using the register interface.
The register interface is extensively documented in the reference manual, with step-by step instructions in the timer feature description chapter, and code examples for simpler use cases in Appendix A.
Using CubeMX is rather trial-and-error, or you must examine the generated code, look into HAL function calls, follow #defines to arrive at the actual peripheral register bit definitions to finally be able to look up in the reference manual what the generated code is actually doing.