Project

General

Profile

PWM @ STM32

Added by arwed about 1 year ago

Hi,

after having understood more about the RFM69 handling I'm now a bit struggling with the PWM functionality. 6 timers and a lot of functions even in my little STM32 --- wow!

I've loaded io-stm32f1.fs, hal-stm32f1.fs, timer-stm32f1.fs, pwm-stm32f1.fs (as shown in rnw/h) and can switch LEDs on and off by setting pins with OMODE-PP and iox!-- so hardware seems to be ok. But when I try the PWM with
~~~
120 PB1 +pwm
5000 PB1 pwm
~~~
as described in flib/pwm.md I get an ok and would expect a dimmed LED -- but nothing happens.
Is it possible that something with the timer is not yet working? How could I check this?
On the other hand: systick is working fine: ticks is contimuously increasing ...
Interrupts are enabled, yet again by sending eint.

Thanks a lot in advance!
arwed


Replies (14)

RE: PWM @ STM32 - Added by jcw about 1 year ago

Could you try another pin on another timer? There are differences between these timers, maybe the logic is not 100% right yet.

RE: PWM @ STM32 - Added by jcw about 1 year ago

Other things to look at, but you'll need to go through the STM32F10x reference manual to make sense of this:

io.all  \ to check whether the proper pin is in "alternate" mode (9)
rcc-apb2enr @ hex.  \ peripheral enable bits on the APB2 bus
rcc-apb1enr @ hex.  \ peripheral enable bits on the APB1 bus
3 timer-base $30 dump  \ register settings for TIM3

Byte offsets $24..$25 will be changing with that last command, if the timer is running:

3 timer-base $30 dump 
40000400   01 00 00 00 20 00 00 00   00 00 00 00 00 01 00 00   .... ... ........
40000410   1F 00 00 00 00 00 00 00   00 00 00 00 00 78 00 00   ........ .....x..
40000420   10 00 00 00 F0 1D 00 00   1F 1C 00 00 10 27 00 00   ........ .....'..
 ok.
3 timer-base $30 dump 
40000400   01 00 00 00 20 00 00 00   00 00 00 00 00 01 00 00   .... ... ........
40000410   1F 00 00 00 00 00 00 00   00 00 00 00 00 78 00 00   ........ .....x..
40000420   10 00 00 00 BF 01 00 00   1F 1C 00 00 10 27 00 00   ........ .....'..
 ok.

Interrupts are not needed for PWM, the timers will run regardless.

RE: PWM @ STM32 - Added by arwed about 1 year ago

yes, of course, I already did:
120 PB1 +pwm ok. --> should be TIM3
500 PB1 pwm ok.
120 PA0 +pwm ok. --> should be TIM2
500 PA0 pwm ok.
120 PB6 +pwm ok. --> should be TIM4
500 PB6 pwm ok.

RE: PWM @ STM32 - Added by arwed about 1 year ago

io.all
PIN 0 PORT A CRL 44444449 CRH 000004B0 IDR 24FE ODR A000
PIN 0 PORT B CRL 49484494 CRH 44444444 IDR FF9D ODR 0010
PIN 0 PORT C CRL 44444444 CRH 44444444 IDR E000 ODR 0000
PIN 0 PORT D CRL 44444444 CRH 44444444 IDR 0000 ODR 0000 ok.

So PA7, PB1, PB6 are in alternate mode?

RE: PWM @ STM32 - Added by arwed about 1 year ago

bytes are obviously changing:
3 timer-base $30 dump
40000400 01 00 00 00 20 00 00 00 00 00 00 00 00 01 00 00 .... ... ........
40000410 1F 00 00 00 00 00 00 00 00 00 00 00 00 78 00 00 ........ .....x..
40000420 10 00 00 00 43 13 00 00 3B 00 00 00 10 27 00 00 ....+$.. ;....'..
ok.
3 timer-base $30 dump
40000400 01 00 00 00 20 00 00 00 00 00 00 00 00 01 00 00 .... ... ........
40000410 1F 00 00 00 00 00 00 00 00 00 00 00 00 78 00 00 ........ .....x..
40000420 10 00 00 00 EB 11 00 00 3B 00 00 00 10 27 00 00 .....".. ;....'..
ok.
3 timer-base $30 dump
40000400 01 00 00 00 20 00 00 00 00 00 00 00 00 01 00 00 .... ... ........
40000410 1F 00 00 00 00 00 00 00 00 00 00 00 00 78 00 00 ........ .....x..
40000420 10 00 00 00 CC 1C 00 00 3B 00 00 00 10 27 00 00 ........ ;....'..
ok.

So Timer 3 is running, correct?
And it stays all 0 before I start +pwm

RE: PWM @ STM32 - Added by jcw about 1 year ago

PA0, PB1, are PB6 are in alternate mode (so is PA9, serial TX).
Yep, looks like timer 3 is indeed running.

RE: PWM @ STM32 - Added by arwed 12 months ago

Hi,

I'm sorry that I have to come back with this topic ...

Meanwhile I've bought a second STM32 board (this one: http://jeelabs.org/article/1613c/) and flashed Mecrisp (Thanks, Jean Claude for the really nice folie tool! Makes FORTHer's live much easier!). But the result when I try to use PWM with an LED is the same: no light at all.

My LED is connected from PB1 to GND. When I connect it from 3.3V to PB1 it's continuously shining. So the pin seems to stay continuously low (which it isn't after reset).

Hope not to bother anybody ...
arwed

RE: PWM @ STM32 - Added by jcw 12 months ago

I'm sorry

On the contrary - thank you for taking the plunge and trying things out at this early stage!

Hm, will need to investigate further. I have not tested all the timer pins w.r.t. PWM yet.

RE: PWM @ STM32 - Added by jcw 12 months ago

My LED is connected from PB1 to GND

With a 470Ω..1kΩ series resistor, I hope? Did you try both orientations?

RE: PWM @ STM32 - Added by arwed 12 months ago

I'm using a very low current LED with a 20kΩ resistor (as in all my Jee- and other nodes). And I let blink it based on sys-tick -- all very well.
And yes, I tried it both ways.

I already tried some of the pins with some of the timers with the same result. I believe there is something systematic wrong in my setup -- just like the pins do not get the pulses from timer or so. Is there something to be enabled in addition to what is done in +pwm? Some kind of "gating" or so?

Toggling LEDs with iox! in OMODE-PP works fine.

To verify the timer itself: how would I have to assign a word to a timer interrupt, e.g. such that I can let call it a word like
~~~
: ledToggle PB1 iox! ;
~~~
once a second from e.g. TIM3?

I will try all timers as listed in pwm.md. (BTW: such an md file is a quite good way to document usage and examples, IMHO.) And I found a quite good tutorial regarding STM32 -- in German and for C but probably it helps me understandingsome more details.

Regards
arwed

RE: PWM @ STM32 - Added by doewie 12 months ago

FYI: I have exactly the same problems on a STM32nucleo board, running at the onboard LED and/or external LED's on several ports.

On the other hand: on the HY-TinySTM103T with the on-board LED (port A1) everything works as expected!

RE: PWM @ STM32 - Added by jcw 12 months ago

Ahaaaa - that could be a hint!

RE: PWM @ STM32 - Added by jcw 12 months ago

Ok, found and fixed the problem - at least for timers 2, 3, and 4.
Timer 1 (PA8..PA11) is different, but also less important, as PA9/PA10 are used for serial anyway, at the moment.
Latest check-in should work a lot better now.

Previous code only worked for PA1, PA7, and PB7. Apologies for wasting everyone's time on this.

RE: PWM @ STM32 - Added by arwed 12 months ago

Yeah, thanks!
I cannot check with the built in LED, because it's connected to PC13. But PB1 works fine now!

Great.

    (1-14/14)