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.22k stars 19.22k forks source link

[FR] Get SPEAKER working on SKR Mini E3 boards #18203

Closed thisiskeithb closed 3 years ago

thisiskeithb commented 4 years ago

Description

Due to timer conflicts/lack of available timers, SPEAKER can’t be enabled on a couple of the SKR Mini E3 boards. This Feature Request is a continuation of comments from https://github.com/MarlinFirmware/Marlin/pull/18011.

sjasonsmith commented 4 years ago

The limited timers and inefficiency with which they are used by Arduino-based firmware causes a shortage of timers when using all the possible features on these boards.

For example, on the SKR Mini E3 boards they are allocated as below:

1 - Used for Fan PWM 2 - Temperature ISR 3 - Disabled because its pins are used by the SD slot. This might not truly be necessary, but it is what happens today. 4 - Software Serial (Needed for Mini E3 1.2 and E3 DIP boards) 5 - Step ISR 6 - Basic Timer (Currently unusable) 7 - Basic Timer (Unusable by most libraries, but might be used by something 8 - Servo / BLTouch

This accounts for every timer on a STM32F103 MPU. As can be seen, there is nothing left for the TONE_TIMER needed by the SPEAKER feature, unless we can use the Basic Timers.

Tone, Servo, and the Marlin ISRs are not written to work with a basic timer. SoftwareSerialM might work on timer 6, but I have not yet tried it.

I have changes in progress which allow moving the temperature ISR to timer 6, freeing up timer 2. I have been unable to use Timer 7. Attempting to use it fails. I suspect it is being used by something already, but I do not know what.

xC0000005 commented 4 years ago

Something similar to STM32GENERIC's PWM would work - a single timer provides PWM -

https://github.com/huaweiwx/STM32GENERIC/blob/master/STM32/cores/arduino/stm32/stm32_PWM.c

sjasonsmith commented 4 years ago

I had all this working and figured out some timer conflict detection mechanisms and everything, but it kind of fell apart once I started trying to make it general enough to apply to all STM32F1 boards.

I'm having to remove that conflict detection logic (for now), due to the complications involved in knowing which timers will be used for TONE and FANs. Even if you specify TONE_TIMER in the platformio.ini file, the Maple tone.cpp library will auto-select a different timer if the pin aligns with a timer channel.

In light of the information thinkyhead added to this PR regarding the removal of Maple support in PlatformIO, it may be more valuable to focus on moving these boards to HAL/STM32, rather than investing in any improvements in HAL/STM32F1. https://github.com/MarlinFirmware/Marlin/pull/17970#issuecomment-640299776

sjasonsmith commented 4 years ago

@xC0000005 how is your timer auto-select code developing for HAL/STM32? Is that something you anticipate contributing any time soon/

xC0000005 commented 4 years ago

Yes, timer select is working. I have a few bugs to shake out and time’s hard to come by during the week, but it’s reasonable. I really also think I’m going to try and retrofit STM32GENERIC’s PWM method into Marlin, going through a PWM class. One timer for PWM IOs seems like a decent trade.

On Jun 7, 2020, at 10:09 PM, Jason Smith notifications@github.com wrote:

@xC0000005 https://github.com/xC0000005 how is your timer auto-select code developing for HAL/STM32? Is that something you anticipate contributing any time soon/

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/MarlinFirmware/Marlin/issues/18203#issuecomment-640370299, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHVGS4PO757X6KSOAK4LM43RVRW67ANCNFSM4NTHJT5Q.

thinkyhead commented 4 years ago

I seriously need a cheat-sheet for all these timers and things, and which platforms and libraries are stealing them….

thinkyhead commented 3 years ago

Please give #21079 a test to see whether it fixes the issue, which is most likely a timer conflict.

qwewer0 commented 3 years ago

Latest bugfix-2.0.x, SKR Mini E3 v1.2, (Ender 3 LCD)

Without https://github.com/MarlinFirmware/Marlin/pull/21079:

With https://github.com/MarlinFirmware/Marlin/pull/21079:

So on the latest bugfix-2.0.x, for some reason M300 now does more than is did before, but it is still not useable.

I really hope that others too can test the speaker functionality, as it is really hard to describe how it is working right now.

qwewer0 commented 3 years ago

When BUZZER enabled the sound is the same on https://github.com/MarlinFirmware/Marlin/pull/21079 as on bugfix, but with SPEAKER the sound is so quiet that I wasn't able to record it. With https://github.com/MarlinFirmware/Marlin/pull/21079 and SPEAKER enable, the board restarts when selecting Restore Defaults.

STM32F103RC_btt, SKR Mini E3 v1.2 configs: Configuration.zip

Here are 2-2 audio examples to the difference of SPEAKER and BUZZER on the latest bugfix-2.0.x.

Audio_semples_SPEAKER_bugfix2.0.x.zip Audio_semples_BUZZER_bugfix2.0.x.zip (selecting restore defaults and store settings)

Misterke commented 3 years ago

At some point I had the SPEAKER working with the change in tone.cpp (TONE_CHANNEL from 8 to 4), but somewhere down the line it seems to have broken again. I'm unsure whether I had it working before I added a 3DTOUCH (and now my own opto-servo probe) and that this affected it or that just more recent builds (with newer libraries / framework) broke it, but when I now try to play a tone, I do notice some sort of dependency on the M300 S parameter, but it is very weird - always pretty low in frequency and slightly going up in frequency as I have the S parameter go down.

So, is there any progress here? Especially being able to use a single timer for all PWM pins mentioned in one of the above comments sounds like the ideal solution, right?

Misterke commented 3 years ago

I've also tried out the -DTONE_CHANNEL=4 -DTONE_TIMER=4 build_flags change of #21079 . Unfortunately it didn't fix my problems with SPEAKER. Using M300 S1000 gives a tone far below 1000Hz, going to S2000 and higher does now seem to play a higher tone, but weirdly enough, going down to S900 or lower also gives a higher tone (with quite some distortion).

Note that I'm on an SKR Mini E3 v2.0 and if I understand the definitions in $HOME/.platformio/packages/framework-arduinoststm32-maple/STM32F1/variants/generic_stm32f103r/board.cpp, some of the pins using this timer are different between v1.2 and v2.0. But I'm confused as the values in that board.cpp do not seem to match the stuff mentioned by @sjasonsmith :

1 - Used for Fan PWM 2 - Temperature ISR 3 - Disabled because its pins are used by the SD slot. This might not truly be necessary, but it is what happens today. 4 - Software Serial (Needed for Mini E3 1.2 and E3 DIP boards) 5 - Step ISR 6 - Basic Timer (Currently unusable) 7 - Basic Timer (Unusable by most libraries, but might be used by something 8 - Servo / BLTouch

So, @sjasonsmith , can you educate me on how your table can be determined from the code and how it matches the pin-to-timer mapping in Maple's board.cpp? I'd love to understand this better - even if just for education ... Thanks in advance!

thisiskeithb commented 3 years ago

With all of the affected BTT SKR Mini E3 boards now running in the STM32 HAL by default & STM32F1/maple going away soon, I'm going to consider this resolved.

github-actions[bot] commented 3 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.