[STM32GENERIC] HAL PWM

Discussions about the STM32generic core
Post Reply
ChrisMicro
Posts: 308
Joined: Fri Mar 24, 2017 4:51 pm
Location: Germany

[STM32GENERIC] HAL PWM

Post by ChrisMicro » Wed Jun 14, 2017 8:50 am

Is there a simple way to change this HAL-timer example into a PWM example?

Could such an example run without modifications on a F407 Black VE or an BluePill?

ChrisMicro
Posts: 308
Joined: Fri Mar 24, 2017 4:51 pm
Location: Germany

Re: [STM32GENERIC] HAL PWM

Post by ChrisMicro » Wed Jun 14, 2017 3:25 pm

I just realize that the analogWrite function has all the necessary "guts".

Code: Select all

typedef struct {
    GPIO_TypeDef *port;
    uint32_t pin_mask;
    uint16_t frequency;
    uint16_t duty_cycle;
} stm32_pwm_type;
What range has frequency and duty_cycle?
I'm thinking of patching the function to be able to add a callback function external.
Something like

Code: Select all

setAnalogWriteCallBack(uint8_t channel, (void *)myfunction); 

User avatar
Pito
Posts: 1627
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: [STM32GENERIC] HAL PWM

Post by Pito » Wed Jun 14, 2017 4:25 pm

Pukao Hats Cleaning Services Ltd.

ChrisMicro
Posts: 308
Joined: Fri Mar 24, 2017 4:51 pm
Location: Germany

Re: [STM32GENERIC] HAL PWM

Post by ChrisMicro » Fri Jun 16, 2017 9:46 am

viewtopic.php?f=3&t=1041&hilit=pito+low+frequency&start=10#p12453
That looks like a lot of trouble using the hardware PWM.

I just realized the Daniel circumvented the problem by simply implementing a timer driven software PWM.

The reason why I want to have a PWM with interrupt call back is to be able to implement some audio PWM to generate sounds.

Why that?

First, there are MCUs without I2S therefore it is difficult to connect a audio I2Cs DAC.
E.g. the BluePill has no I2S hence it is not possible to connect aMAX98357a audio amplifier.

danieleff
Posts: 336
Joined: Thu Sep 01, 2016 8:52 pm
Location: Hungary
Contact:

Re: [STM32GENERIC] HAL PWM

Post by danieleff » Sat Jun 17, 2017 5:34 am

For the exact example, `extern "C" void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { ... }` is callback.
You can see them in https://github.com/danieleff/STM32GENER ... im.h#L1723

Do you want to change PWM with time? Or send I2S data out?

The best bet is to use CubeMX to generate the code.

ChrisMicro
Posts: 308
Joined: Fri Mar 24, 2017 4:51 pm
Location: Germany

Re: [STM32GENERIC] HAL PWM

Post by ChrisMicro » Sat Jun 17, 2017 6:40 am

Do you want to change PWM with time? Or send I2S data out?
I want to create sound with PWM.
Here is an example which I made for the BluePill. It just generates the sine wave sound.

Such a thing could be useful when you don't have a DAC like in your audio/video example for the F407 black.
The best thing would be to have an abstraction layer where you can simple exchange the PWM for a real DAC then everybody could use the examples.

User avatar
Pito
Posts: 1627
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: [STM32GENERIC] HAL PWM

Post by Pito » Sat Jun 17, 2017 6:57 am

FYI - here is an example how to do a dac with pwm
http://www.stm32duino.com/viewtopic.php ... 001#p11792
1. configure the pwm period and get maxduty parameter
2. scale your amplitude such it fits in 0..maxduty
3. output your value
It is libmaple but to rewrite setPeriod() would be easy.
Mind you cannot pwm 44.1kHz sampled audio at full 16bit of course. With pwm periods around 25us (40kHz) your resolution will be maybe 11-12bit with F407.

Code: Select all

// setup the timer
 max_duty =  timer.setPeriod (25); // in microseconds == 40kHz
 ..
// your dac loop
 // scale a 12bit amplitude value such it fits into 0..max_duty count
 pwmValue = map(amplitude, 0, 4095, 0, max_duty); 
 // make DAC
 pwmWrite(pwmPin, pwmValue);
Pukao Hats Cleaning Services Ltd.

danieleff
Posts: 336
Joined: Thu Sep 01, 2016 8:52 pm
Location: Hungary
Contact:

Re: [STM32GENERIC] HAL PWM

Post by danieleff » Sun Jun 18, 2017 7:26 am

ChrisMicro wrote:
Sat Jun 17, 2017 6:40 am

The best thing would be to have an abstraction layer where you can simple exchange the PWM for a real DAC then everybody could use the examples.
Well the abstraction layer (and arduino-like) is a class with `write(sample)` (Basically Stream) that blocks if its buffer is full.

ChrisMicro
Posts: 308
Joined: Fri Mar 24, 2017 4:51 pm
Location: Germany

Re: [STM32GENERIC] HAL PWM

Post by ChrisMicro » Sun Jun 18, 2017 9:21 am

Well the abstraction layer ..
I mean something like the following is missing

base class: AudioDac::write(int16_t value)

derived classes
- AudioDacPwmF407Black::wrie(int16_t value)
- AudioDacI2sF407Black::write ..
- AudioDacPwmF103..
...

Example code:

AudioDacPwmF407Black Dac(44100);
SoundPlayer.begin(Dac);

To become hardware independent.

Post Reply