Skip to content

Add stm32f1 FAST_PWM feature #19952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 29, 2020

Conversation

victorrar
Copy link
Contributor

Description

Implement stm32f1 FAST_PWM feature

Benefits

Stm32f1 FAST_PWM feature

Configurations

Configuration.h
Any with enabled #define FAST_PWM_FAN

Configuration_adv.h
Tested frequencies from 1 Hz to 60kHz
#define FAST_PWM_FAN_FREQUENCY 31400

Comment on lines 38 to 41
//protect used timers
if(timer == get_timer_dev(STEP_TIMER_NUM)) return;
if(timer == get_timer_dev(PULSE_TIMER_NUM)) return;
if(timer == get_timer_dev(TEMP_TIMER_NUM)) return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PULSE timer doesn't really exist, it is just the STEP_TIMER on this platform.

There are more timers than this which could be interfered with:

  1. Servo Timer
  2. Tone Timer
  3. Software Serial Timer
  4. Timers connected to SPI pins

The SPI bit might seem weird, but I believe on STM32F1 chips pins are not actually disconnected from the timers even if you are using their other functions. This means that running the timer connected to SPI interferes with SPI, even if you are running it for other purposes.

Detecting all the possible timer conflicts is hard, which is why there have been so many mistakes made in this area.
Using your feature won't be any less safe than "normal" PWM on these pins, so perhaps is little to be gained from these partial checks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Servo Timer
  2. Tone Timer
  3. Software Serial Timer
  4. Timers connected to SPI pins

Am I right, that all timers, allocated by Marlin are listed in timers.h? Or there any implicit usage? (except libraries, but it will be good to know what timers exactly they allocates)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some are implicit. I think that dealing with timers is a little more mature in HAL/STM32. Maple (HAL/STM32F1) doesn't always give a good way to override the default behavior.

Tone is the hardest to deal with in Maple. It default to 4 or 8 (depending on the chip), but will use the timer connected to a pin if available. This means it is entirely up to the board layout whether enabling SPEAKER will interfere with some other timer.

@sjasonsmith
Copy link
Contributor

Could you look at HAL/STM32 to see how hard the same change would be there?

Some HAL/STM32F1 boards are being migrated to HAL/STM32, with the eventual goal of moving everything and deleting HAL/STM32F1. Adding new functionality to STM32F1 which is missing from STM32 makes this transition harder.

Which board are you using for your testing?

@victorrar
Copy link
Contributor Author

MKS Robin Nano V1.2 with TFT35 (no tone) and BLTouch

@rhapsodyv
Copy link
Member

MKS Robin Nano V1.2 with TFT35 (no tone) and BLTouch

You can use mks_robin_nano35_stm32 to test.

@sjasonsmith
Copy link
Contributor

At a glance, I think this looks ok. Maybe there are bugs, but that doesn't even worry me very much for a brand new feature.

My larger concern is with adding any new features to the STM32F1 HAL. I would love to see this added to the STM32 HAL instead. This could then be a good incentive for people to help migrate boards from STM32F1 to STM32.

@victorrar
Copy link
Contributor Author

Ok, I will try to migrate it to "STM32".

MKS Robin Nano V1.2 with TFT35 (no tone) and BLTouch

You can use mks_robin_nano35_stm32 to test.

Can you, please, explain more? I've can't find the config for that
image
image

I'm new here, and is there any guide about the project structure?

@thisiskeithb
Copy link
Member

Can you, please, explain more? I've can't find the config for that

It's in bugfix-2.0.x:

Marlin/platformio.ini

Lines 1325 to 1333 in 1247f73

#
# MKS Robin Nano V1.2 and V2 using hal STM32
#
[env:mks_robin_nano35_stm32]
platform = ${common_stm32.platform}
extends = common_stm32
build_flags = ${common_stm32.build_flags} -DMCU_STM32F103VE -DSS_TIMER=4 -DENABLE_HWSERIAL3 -DTRANSFER_CLOCK_DIV=8
board = genericSTM32F103VE
board_build.core = stm32

You'll want to use/check bugfix-2.0.x as it's the most up to date branch.

@sjasonsmith
Copy link
Contributor

It's in bugfix-2.0.x:

And it was just added. Maybe this morning?

@sjasonsmith
Copy link
Contributor

I'm new here, and is there any guide about the project structure?

For someone who is "new here" I am impressed that you added this feature and got it working!

I am not aware of any good explanatory resources. If you encounter questions while adapting the implementation, please post them here.

This might be even easier in HAL/STM32. HardwareTimer has a setPWM function with frequency and duty cycle arguments.

One caution is that not all STM32 timers use the same base clock, even on the same chip. Make sure you use the framework functions to set rates, and don't divide from the CPU clock yourself.

Once you make it work for your STM32F1 board, we can help you test it on a larger variety of hardware.

@thinkyhead thinkyhead added A: STM32 PR: Improvement T: HAL & APIs Topic related to the HAL and internal APIs. labels Oct 29, 2020
@thinkyhead thinkyhead merged commit de315c9 into MarlinFirmware:bugfix-2.0.x Oct 29, 2020
vgadreau pushed a commit to vgadreau/Marlin that referenced this pull request Dec 9, 2020
@zbyrek
Copy link

zbyrek commented Jan 2, 2021

I have problem with enabling it (creality v4.2.2 board), when i disable soft pwm and enable it (it's on PA0) it's just DC output on pin, always enabled, even for 0% fan is spinning at max speed.

@sjasonsmith
Copy link
Contributor

@zbyrek many fans don’t respond well to PWM. You can try adjusting the frequency (somewhere in Configuration_adv.h).

For most of my fans they behave best with soft PWM.

@zbyrek
Copy link

zbyrek commented Jan 2, 2021

@zbyrek many fans don’t respond well to PWM. You can try adjusting the frequency (somewhere in Configuration_adv.h).

For most of my fans they behave best with soft PWM.

Unfortunately that's not it, i measured frequency and with that option on it's just DC, also i can see it when i connect engraving laser (that's the reason why i need it in the first place), with soft pwm it's just 7.8Hz which is definetly too low for laser engraving (if i want control power and not just hard ON/OFF), however thanks for reply.

@sjasonsmith
Copy link
Contributor

I haven’t actually measured it on a board, but it looks like it probably has a conflict on timer 2 between the fan PWM and the timer used for Marlin’s temperature ISR.

@zbyrek
Copy link

zbyrek commented Jan 2, 2021

Here, I made some proof measurments, that's definetly not generating any PWM (sorry for crude photo of screen as proof), I'm not here to just complain, I could help fix that, i have some knowledge in programming and in electronics, however never used STM32, I looked at code but without debugger is hard to find what's what.

HW 50% duty with screen of printer:
HW50_with_printer_screen
SW 100% duty:
SW_0
SW 0% duty:
SW100
SW 50% duty:
SW50
HW 0% duty:
HW0
HW 50% duty:
HW50

@zbyrek
Copy link

zbyrek commented Jan 2, 2021

Ok, there is definetly problem with timer2 conflict, since PA0 is controled by timer2 which unfortunately cannon't be change:
image

safe switch just returns from pwm code:
image

I tried to remap TEMP timer to timer4:
image

but without success, need to dig in more.

edit:
I found that PA0 could be also controlled by timer5:
image

@sjasonsmith
Copy link
Contributor

It's probably better for you to open an issue and record details there, rather than in this closed PR.

@zbyrek
Copy link

zbyrek commented Jan 2, 2021

Right.

tharts pushed a commit to tharts/Marlin that referenced this pull request Jan 6, 2021
kpishere pushed a commit to kpishere/Marlin that referenced this pull request Feb 19, 2021
W4tel-BiDi pushed a commit to W4tel-BiDi/Marlin that referenced this pull request Apr 5, 2021
chrisjenda pushed a commit to chrisjenda/Marlin that referenced this pull request Apr 5, 2021
thinkyhead pushed a commit to thinkyhead/Marlin that referenced this pull request Apr 28, 2021
thinkyhead pushed a commit to thinkyhead/Marlin that referenced this pull request Apr 29, 2021
thinkyhead pushed a commit that referenced this pull request Apr 30, 2021
@rajhlinux

This comment was marked as off-topic.

@thisiskeithb
Copy link
Member

@rajhlinux: This Issue Queue is for Marlin bug reports and development-related issues, and we prefer not to handle user-support questions here. (As noted on this page.) For best results getting help with configuration and troubleshooting, please use the following resources:

@rajhlinux

This comment was marked as off-topic.

Lyrth pushed a commit to Lyrth/Aquila_X2_N32 that referenced this pull request Sep 6, 2023
@Lyrth

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A: STM32 PR: Improvement T: HAL & APIs Topic related to the HAL and internal APIs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants