MarlinFirmware / Marlin

Marlin is an optimized firmware for RepRap 3D printers based on the Arduino platform. Many commercial 3D printers come with Marlin installed. Check with your vendor if you need source code for your specific machine.
https://marlinfw.org
GNU General Public License v3.0
16.28k stars 19.24k forks source link

[BUG] HW PWM not working for stm32f1 PA0 #20638

Closed zbyrek closed 2 years ago

zbyrek commented 3 years ago

Bug Description

In boards which use PA0 of STM32F1 (like creality V4) hardware PWM is not working due to conflict with timer2 which is used for temp interrupt, changing temp timer to timer4 didn't resolve issue.

Configuration Files

config.zip

Uncommented: #define FAST_PWM_FAN

Commented out: #define FAN_SOFT_PWM

also one in: pins_CREALITY_V4.h

Steps to Reproduce

  1. Compile code for creality board (or other STM32F1 which uses PA0 for fan)
  2. Upload to printer
  3. Try to change cooling fan percantage

Expected behavior:

There should be PWM signal at gnd fan connection (positive one is always +24V it is controled by N-channel mosfet) with duty depending on set percentage.

Actual behavior:

Solid GND signal, even for 0%, fan always run at 100% nevertheless of setting

.

Additional Information

Sorry for crude photo, but i wanted to show both: printer lcd and scope: 103463813-8f6cb580-4d2f-11eb-922e-b12cb3a435ee

Software pwm is working alright however i need hardware pwm, because sw is only 7,8Hz which is too low for laser.

I think about two solutions possible:

  1. Change pwm timer of port PA0 to timer5 or timer8: image

EDIT: timer8 is not capable of generating PWM, so only timer5 could be used in replacement for timer2

  1. Change temp timer to other than 2, however i think (correct me if i'm wrong) that interrupt from that timer is also used in other parts of fw.
zbyrek commented 3 years ago

Ok, I did everything I was certain that would work. Compile completed with no errors but when I attempted to execute M9891 I still receive "echo:Unknown command: "M9891 PA0 F10"".

Attached is a zipped folder of only those files I added or modified including their paths.

Please review and see where I missed something. I'm not a programmer but I'm learning how to modify files and compile them. Still not clear how to read or develop code.

Thanks again. Marlin-For Ender-3 Using Laser.zip

You shouldn't surround that new command block with if statement since you don't define it anywhere else, remove these preprocessor directives.

- #if ENABLED(9891)
  { case 9891:M9891();
    break;
  }
- #endif
luchomasco commented 3 years ago

Hi @vojto1980 , Where do you set the frequency value? Sorry for my noob question. I'm now with this stuff. Greetings, Luciano.

dcwalmsley commented 3 years ago

Ok, this is a bit frustrating. LOL

So I reviewed @HofaTheRipper "FastPWM.txt" file from is repository and noted that all steps he has listed have been performed. I even went so far as to copy and paste each requirement into appropriate file and their locations.

But I still receive errors when compiling. See next line.

Marlin\src\gcode\gcode.cpp:1131:5: error: expected unqualified-id before 'case' case 9891: M9891(); break; ^~~~ Marlin\src\gcode\gcode.cpp:1131:25: error: expected unqualified-id before 'break' case 9891: M9891(); break; ^~~~~ *** [.pio\build\STM32F103RET6_creality\src\src\gcode\gcode.cpp.o] Error 1

And to be clear, here is the verbatim list of tasks I followed from Hofa's list.


Set {&gpioa, &timer5, &adc1, 0, 1, 0}, / PA0 / in /Users/%USERPROFILE%/.platformio/packages/framework-arduinoststm32-maple/STM32F1/variants/generic_stm32f103r/boards.cpp

Commented #define FAN_SOFT_PWM in Marlin/src/pins/stm32f1/pins_CREALITY_V4.h

Uncommented #define FAST_PWM_FAN in Marlin/Configuration.h

Commented #define FAN_SOFT_PWM in Marlin/Configuration.h (DO NOT comment out #define SOFT_PWM_SCALE 0)

Set #define STEP_TIMER_NUM 4 // for other boards, five is fine. in Marlin/src/HAL/STM32F1/timers.h


gcode.h:


...


gcode.cpp:


...


Add file gcode\M9891.cpp:


include "../../inc/MarlinConfig.h"

include "../gcode.h"

void GcodeSuite::M9891() { const uint8_t pport = parser.byteval('P');

const uint16_t dfreq = parser.ushortval('F');

// Set frequency for port

set_pwm_frequency((pin_t)pport, (int)dfreq);

}

zbyrek commented 3 years ago

I guess (because i didn't see whole code) you put case outside switch (after switch closing } )

dcwalmsley commented 3 years ago

After doing some reviewing, I now understand what you were referring to. I moved the line "case 9891: M9891(); break;" up to and between

 #if ENABLED(MAX7219_GCODE)
    case 7219: M7219(); break;                                // M7219: Set LEDs, columns, and rows
  #endif

  case 9891: M9891(); break;                                 //Send GCode command to printer to set port and frequency desired (P<portname> F<frequency in Hz>)

    default:
      parser.unknown_command_warning();
      break;

This allowed the complying to complete with no error message.

I also performed via serial connection and received and "ok" message one I hit return on M9891. Is there a process to see the entered command was successful other than an "ok" response?

abhinavjha86 commented 3 years ago

want to thank @zbyrek for creating this bug and explaining the issue with a provided fix(work-around), what really helped me getting grayscale laser engraving to work on my Ender 3 v2 in addition to @zybrek changes was:

uncommenting '#define LASER_SYNCHRONOUS_M106_M107' suggested by @vojto1980.

cheers guys👍😊

VukIgor commented 2 years ago

@descipher are those fixes included in bugfix or Marlin firmware ? Have Creality 4.2.2 board and after compiling bugfix and also Marlin HW pin is still dead ,board is new ,everything works fine even that k1 fan but when i connect laser to it just blinks laser and nothing else

descipher commented 2 years ago

@VukIgor #23125 was merged after your compile, please use bugfix-2.0.x which now contains the fix.

VukIgor commented 2 years ago

@descipher thank you very much :) Hope it won't need any aditional coding becouse i'm noob at this ,have few laser engravers running on marlin and ramps 1.4 for more than year without issue

VukIgor commented 2 years ago

Bugfix works for my creality 4.2.2 boards ,thank you

thisiskeithb commented 2 years ago

Fixed in https://github.com/MarlinFirmware/Marlin/pull/23125.

MichalsonCZ commented 2 years ago

Hi there, I want to share my observations with the Ender 3 v2 and 500mW laser. I also had a problem with slow PWM. So I did all the tweaks suggested by @zybrek and @vojto1980. Awesome, it worked! Thank you. Nice and precise engraving with the possibility to control the laser power.

BUT, then I started to burn 15 min program. After ca. 10 mins, something happened and the output from the connector now gives 100% power (24V) all the time. It is no more possible to control this output. I also tried stock firmware, but it still gives 100%.

For me, it looks, something may be burned in the mainboard. For that "fatal" engraving I had used about 5% of laser power and the mainboard fan (which was connected to the same connector) was not running at all, so maybe it got overheated, I do not know. Fortunately, the other features are OK, I can 3D print as before, only the fan still rotates at full speed now.

Take care, Michal

zbyrek commented 2 years ago

Hi there, I want to share my observations with the Ender 3 v2 and 500mW laser. I also had a problem with slow PWM. So I did all the tweaks suggested by @zybrek and @vojto1980. Awesome, it worked! Thank you. Nice and precise engraving with the possibility to control the laser power.

BUT, then I started to burn 15 min program. After ca. 10 mins, something happened and the output from the connector now gives 100% power (24V) all the time. It is no more possible to control this output. I also tried stock firmware, but it still gives 100%.

For me, it looks, something may be burned in the mainboard. For that "fatal" engraving I had used about 5% of laser power and the mainboard fan (which was connected to the same connector) was not running at all, so maybe it got overheated, I do not know. Fortunately, the other features are OK, I can 3D print as before, only the fan still rotates at full speed now.

Take care, Michal

You probably burned transistor, higher frequency switching create bigger losses due to capacitance of transistor which leads to more power dissipated, so it could overheat, only way to check this is to desolder transistor and measure it outside circuit. That disconnected mainboard fun was terrible idea.

tpruvot commented 2 years ago

some vendors/boards use OD (open drain) mode to drive mosfets, which allow more power on outputs... But to know that, you need a real schematic (or reverse/inspect the pcb to make one).... basically, the output is linked to 5V with a resistor to be able to give more juice to the mosfet... but in result, the pwm/output signal may be reversed... give juice when set to 0

sample: image H1/2/3 are STM32 3.3V outputs

descipher commented 2 years ago

gives 100% power (24V) all the time.

Sorry to hear it failed.

Depending on what device you are driving a voltage of 24 may be too high for it. This could easily damage a diode laser pwm control signal input.

But yes regardless of that, there is the high frequency issue that can cause a MOSFET or Transistor to overheat and short out.

23319

MichalsonCZ commented 2 years ago

I used the official Creality "laser set" that allows 12-24V on input. The laser is still working. Most probably the transistor is dead. My board is v4.2.2.
Since the other functions are OK, I decided to not play with a laser on my 3D printer anymore and I purchased Vigotech VG-L7X, since there is a firmware allowing to use of GRBL protocol. But this is a different story.

I would like to thank you once again for your awesome job on github.

Michal

descipher commented 2 years ago

I used the official Creality "laser set" that allows 12-24V on input. The laser is still working. Most probably the transistor is dead. My board is v4.2.2. Since the other functions are OK, I decided to not play with a laser on my 3D printer anymore and I purchased Vigotech VG-L7X, since there is a firmware allowing to use of GRBL protocol. But this is a different story.

I would like to thank you once again for your awesome job on github.

Michal

You are welcome.

FYI Marlin will be more GRBL compatible soon. #22690

einymm commented 2 years ago

I greet you all, I have a question: compiling the firmware according to the modifications from @zbyrek and @ vojto1980 will work fine, however I found a warning there. Can you advise me how to eliminate it? Thank you

`Compiling .pio\build\STM32F103RET6_creality_maple\src\src\inc\Warnings.cpp.o Compiling .pio\build\STM32F103RET6_creality_maple\src\src\lcd\e3v2\common\dwin_api.cpp.o Compiling .pio\build\STM32F103RET6_creality_maple\src\src\lcd\e3v2\common\encoder.cpp.o Marlin\src\inc\Warnings.cpp:485:4: warning: #warning "Creality 4.2.2 boards may have A4988 or TMC2208_STANDALONE drivers. Check your board and make sure to select the correct DRIVER_TYPE!" [-Wcpp]

warning "Creality 4.2.2 boards may have A4988 or TMC2208_STANDALONE drivers. Check your board and make sure to select the correct DRIVER_TYPE!"

^~~~~~~

Compiling .pio\build\STM32F103RET6_creality_maple\src\src\lcd\e3v2\jyersui\dwin.cpp.o Compiling .pio\build\STM32F103RET6_creality_maple\src\src\lcd\e3v2\jyersui\dwin_lcd.cpp.o`

einymm commented 2 years ago

Thank you @descipher, "Set FAST_PWM_FAN default frequency to 1KHz # 23326" was very important to me. I don't know why (maybe it's related to my "warning" above), but after the adjustments described above, my PWM output for the fan ran at 281kHz. After adjustment #define FAST_PWM_FAN_FREQUENCY 1000 in Configuration_adv.h I already need 1kHz. Thanks a lot

P.S. Sorry, I don't speak English => google translator

tpruvot commented 2 years ago

dont forget there is in a "normal" hardware PWM between SOFT and FAST.... which use (only) 1kHz :p

descipher commented 2 years ago

dont forget there is in a "normal" hardware PWM between SOFT and FAST.... which use (only) 1kHz :p

Are you referring to the lib 1kHz default? Even so, if there need to change the FAN default it will be clear that the intent is for FAN PWM frequency.

tpruvot commented 2 years ago

if you dont set FAN_SOFT_PWM nor FAST_PWM_FAN, its using normal PWM... with default "framework" PWM frequency of 1000Hz

descipher commented 2 years ago

Ah, yes it will be safe there as well.

VukIgor commented 2 years ago

Hi ,here am I again ,got probably firmware settings issue ,your bugfix worked when I've installed it on creality 4.2.2 board but few minutes into real job my laser just didn't have enough power even to scratch papper .I need some help if posible ,i have 12v laser that uses 5v pwm from 4.2.2 creality board that is hooked up on 24v power brick . Is it possible that i'm missing some config within fast_pwm options like frequency ? Did disasemble my laser electronics and put my old ones back (ramps 1.4) and works like charm just slower becouse you know ,8bit Thanks in advance for any help :)

VukIgor commented 2 years ago

Just to remove any confusion i have separate 12v power supply for my laser module ,just using pwm pin from creality board

descipher commented 2 years ago

Uncomment https://github.com/MarlinFirmware/Marlin/blob/e17d710c5c4c96e069f64854e3fcdb77abcf90e1/Marlin/Configuration_adv.h#L578

VukIgor commented 2 years ago

@descipher it can stay at 31400 frequency ?

VukIgor commented 2 years ago

Infact this is enabled and with that 31400 value on compiling bugfix last time , can it be that pwm is to low ,it is pwm from 24v board and laser is declared to 12v power

descipher commented 2 years ago

Support is generally handled on Discord. https://discord.gg/V8Dy54Se Most likely your dealing with 3.3v driving a 5v laser. You need to upload the config files to get help.

VukIgor commented 2 years ago

that is the case ,it's 3.1v not even 3.3 ,however thank you for fast reply @descipher :)

github-actions[bot] commented 2 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.