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.32k stars 19.25k forks source link

Marlin2.0 LPC1768 Software Serial(UART) for TMC2208, it seems to work fine #13197

Closed Msq001 closed 5 years ago

Msq001 commented 5 years ago

I have modified the firmware for the TMC2208 UART and LPC1768 software serial lib sections, Now it's not necessary to use interrupt IO for RX, Does anyone want to test it? I can send you the complete project.

VanessaE commented 5 years ago

Initial impressions after a couple of test/calibration objects and a small wade's gear that I've been needing to print:

It's working really well for me. Short and sweet :smiley:

There is one thing that I think should be addressed, but it's not specific to the 2208 (rather, to all TMCs I guess): if accelerating/decelerating during a move would cross the stealthChop/spreadCycle hybrid threshold, find some way to avoid confusing the TMC's math -- they get noisy when close to the threshold because of double/quad stepping. Maybe split the accel/decel periods into many short moves and let the TMC driver itself handle them, instead of banging on step/dir?

gloomyandy commented 5 years ago

@VanessaE Good to hear that it is working. Thanks for trying it.

As to your comment about the acceleration/deceleration and TMC driver, you may want to create a new issue and explain the problem and possible solution there. I'm not at all familiar with that side of how the drivers function, I'm sure @teemuatlut and others could comment, but I'd rather keep this thread focused on the software UART if we can.

VanessaE commented 5 years ago

Fair enough. Comment moved to a new issue. (https://github.com/MarlinFirmware/Marlin/issues/13316)

VanessaE commented 5 years ago

Can we get the pins definitions in @gloomyandy's post (step 4) officially added to pins_BIQU_SKR_V1.1 ? They "seem to work fine", after all :wink: (and in fact, do; it would be good to establish a standard).

gloomyandy commented 5 years ago

I intend to create a PR for this (there needs to be an alternative for when using a TFT screen, which I'd really like to test before creating the PR). There are other changes also needed to finish this off completely. Like for instance you will not be running the monitoring code unless you have modified tmc_util to remove the requirement for a hardware UART. There are also changes needed to make the baud rate more suitable for use with this modification (at the moment there is a hack in the framework code to do this).

If all goes well I hope to get to this later this week.

gretel commented 5 years ago

Are you using 12V or 24V? If 24V you may want to change CHOPPER_TIMING as well. Lots of folks seem to have to run E steppers in spreadCycle mode (especially with linear advance), but I guess that is all down to fine tuning.

agreed, spreadCycle on E steppers is good for me at least.

jokejoke commented 5 years ago

Ok so I've got a first pass of my solution to this problem. It basically uses the RIT timer to generate the output stream and to sample (using oversampling) the input stream. It supports the use of any pin and supports half duplex operation so only a single pin is needed per TMC2208. It supports the use of MONITOR_DRIVER_STATUS (though changes are needed to Marlin to enable this). No changes are needed to the TMC library to use it (though it does include a bit of a hack to make this possible and I'll be working to put together a PR for the TMC lib in the future to clean this up). The approach used is based on one originally suggested by @p3p and uses input from this thread and a discussion over on discord.

I have tested it a little with four TMC2208s on an SKR V1.1 board (with an LCD display) and on my "production" printer that uses an SBase board with two external TMS2208s. I have not seen any issue with various test prints. I have not tested this code using separate TX/RX pins nor have I tested it as general purpose software serial code.

The changes are based upon a version of Marlin from about a week or so ago. I will be updating and testing with the latest version as soon as I get chance but I would expect things to work with the current version. Note that my LPC1768 framework tree contains other changes. I provide instructions on how to use my SoftwareSerial code below, you do not need to use my trees.

My Marlin tree is here: https://github.com/gloomyandy/Marlin/tree/myskr My LPC2768 tree is here: https://github.com/gloomyandy/pio-framework-arduino-lpc176x/tree/software-serial

The two files that contain the new implementation are: https://github.com/gloomyandy/pio-framework-arduino-lpc176x/blob/software-serial/cores/arduino/SoftwareSerial.cpp https://github.com/gloomyandy/pio-framework-arduino-lpc176x/blob/software-serial/cores/arduino/SoftwareSerial.h

NOTE: The SoftwareSerial code is now part of the LPC1768 framework version 0.1.0 so you should no longer need to perform steps 1 and 2 below. You may need to force an update of the platformio libs.

If you wish to try this code then these are the steps you will need. Note that this is not final code and you use it at your own risk! You should make a backup of any files you overwrite or change.

  1. You need to locate the LPC1768 framework directory on your machine. This is stored at location that will look like: C:\Users.platformio\packages\framework-arduino-lpc176x
  2. locate the directory cores\arduino which should contain files called SoftwareSerial.cpp and SoftwareSerial.h. Make a copy of these files in a safe location and replace them with my version given above.
  3. Edit your configuration file to define which TMC2208 drivers you have (set them to be of type TMC2208).
  4. Edit your pins.h file to define what pins you are going to use for the UART connections. For the SKR V1.1 I use the following:
  #define X_SERIAL_TX_PIN    P2_06
  #define X_SERIAL_RX_PIN    P2_06
  #define Y_SERIAL_TX_PIN    P1_31
  #define Y_SERIAL_RX_PIN    P1_31
  #define Z_SERIAL_TX_PIN    P1_23
  #define Z_SERIAL_RX_PIN    P1_23
  #define E0_SERIAL_TX_PIN   P0_03
  #define E0_SERIAL_RX_PIN   P0_03
  //#define E1_SERIAL_TX_PIN   P0_02
  //#define E1_SERIAL_RX_PIN   P0_02
  1. Wire up a single connection between each of the selected pins and the PDN pin on the TMC2208. I used a direct connection and so far this has worked fine. However it may make sense to include a 1K resistor in the connection. You do not need a Y lead.
  2. Edit the Configuration_adv.h file and define #define TMC_DEBUG to enable the use of the m122 command.
  3. Build Marlin and install the new version. You should not get any sort of error message on the display. Issuing m122 should give output that ends with something like:
22:31:24.530 : Driver registers:
22:31:24.537 : X  0xC0:0C:00:00
22:31:24.544 : Y  0xC0:0C:00:00
22:31:24.552 : Z  0xC0:0C:00:00
22:31:24.559 : E  0xC0:0C:00:00
22:31:24.567 : Testing X connection... OK
22:31:24.574 : Testing Y connection... OK
22:31:24.581 : Testing Z connection... OK
22:31:24.589 : Testing E connection... OK
  1. If you wish to use the MONITOR_DRIVER_STATUS option you can set this in Configuration_adv.h you will also need to edit the tmc_util.cpp file and change the HAS_HW_COMMS macro to remove the requirement for a hardware UART. You can see my version of the file here: https://github.com/gloomyandy/Marlin/blob/myskr/Marlin/src/feature/tmc_util.cpp
  2. Let me know how you get on. Please note I will have limited time over the next week or so, so may not be able to help debug issues very much, so please don't try this unless you are happy to wait for help!

Here is my SKR configured as above.... img_20190224_235344

@gloomyandy Nice work :) Is this change of SoftwareSerial library usable also for 8bit microcontrollers (Atmega2560)? Because FYSETC F6 1.3 board has problem with RX pins without PCINT https://github.com/MarlinFirmware/Marlin/issues/12800#issuecomment-487448235. Thank you :)

gloomyandy commented 5 years ago

Probably not and certainly not without re-implementation. This code is really LPC176x specific.

lightface79 commented 5 years ago

Hi!! is this 1 pin serial sullution inplemented in the latest 2.0 builds u can download? or do i need to use the serial files from u @gloomyandy

Bulding a new 3dprinter with Sbase 1.3 with the lc1768 chip and TMC2208 drivers..

best regards Jan // Sweden

ManuelMcLure commented 5 years ago

At least on Re-ARM it looks like it's implemented - the pins.h file for Re-ARM shows the same pin being used for TX and RX.

lightface79 commented 5 years ago

hmm... weerd... its the same lpc1768 chip on both re-arm and my mks sbase it should work... wonder whats the difference between the 2 cards sowftware wise,...

gloomyandy commented 5 years ago

This should work fine on the sbase board (that's what I originally developed it on!). What exactly are you trying to do and what pins on the Sbase are you trying to use for the software serial interface? What makes you think it is not working? I assume you are trying to talk to a TMC device, which one? Are you sure you have the driver modules configured correctly? Also the Sbase 1.3 has onboard drivers so are you not using them?

lightface79 commented 5 years ago

Hi and thnks for anwsering. In the middle of the build of my new 3d printer... Using tmc2208 drivers. Not going to use the sbase internal drivers sins they ar terrible... So i made a Extennsion board with the 5 tmc2208 with separate 24v feeding them. Used the same setup on my Delta printer but with Klipper FW with the 1 wire uart and without a 1K resistor and that worked perfect..

So when u say half duplex comms is it just Write or Read depending on the resistore or is it Both on a single wire on the sbase?

Should i just re config the sbase/pins.h so that both RX/tx is on the same pins?

Is there som pins that is better to use for the serial com to the drivers?

Best regads Janne

lightface79 commented 5 years ago

Whitch ones do u recomend? have 5 tmc2208

Have a double Z steppers.. or shoud i scrap 1 driver and just go Y-Harnes on 1 Z driver To 2 pcs of z stepper motors and ramp up the current on that driver?

gloomyandy commented 5 years ago

It does read/write on the same pin. You just need to define both the RX and TX pins to be the same pin. I'd use 5 pins on the J8 connector so..

#define X_SERIAL_TX_PIN P1_22 // J8-2
#define X_SERIAL_RX_PIN P1_22
#define Y_SERIAL_TX_PIN P1_23 // J8-3
#define Y_SERIAL_RX_PIN P1_23
#define Z_SERIAL_TX_PIN P2_12 // J8-4
#define Z_SERIAL_RX_PIN P2_12
#define E0_SERIAL_TX_PIN P4_28 // J8-6
#define E0_SERIAL_RX_PIN P4_28

Note I've not tested this! If you want to use E1/Z2 then you will need to find another suitable pin. You could use pin P0_02 or P0_03 if you are not planning on using the UART interface to talk to any sort of touch screen. I'd avoid trying to use the endstop/thermistor pins if you possibly can as they will have extra pullups etc. on them that may cause problems.

lightface79 commented 5 years ago

ok... thanks alot... =) will put this in my fW and have a go on the test bench =)

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