From 59483221d510a7171d76d9b5460fc663629d4c02 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 19 Mar 2016 19:03:17 -0700 Subject: [PATCH] Move stepper DAC to a separate file --- Marlin/Marlin_main.cpp | 4 ++ Marlin/stepper.cpp | 65 +------------------------------ Marlin/stepper_dac.cpp | 87 ++++++++++++++++++++++++++++++++++++++++++ Marlin/stepper_dac.h | 33 ++++++++++++++++ 4 files changed, 125 insertions(+), 64 deletions(-) create mode 100644 Marlin/stepper_dac.cpp create mode 100644 Marlin/stepper_dac.h diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp index 9ba8e9a368..7d6d3c27c9 100644 --- a/Marlin/Marlin_main.cpp +++ b/Marlin/Marlin_main.cpp @@ -68,6 +68,10 @@ #include #endif +#if ENABLED(DAC_STEPPER_CURRENT) + #include "stepper_dac.h" +#endif + /** * Look here for descriptions of G-codes: * - http://linuxcnc.org/handbook/gcode/g-code.html diff --git a/Marlin/stepper.cpp b/Marlin/stepper.cpp index a88d7c80cb..c6391c2280 100644 --- a/Marlin/stepper.cpp +++ b/Marlin/stepper.cpp @@ -30,7 +30,7 @@ #include "language.h" #include "cardreader.h" #include "speed_lookuptable.h" -#include "dac_mcp4728.h" + #if HAS_DIGIPOTSS #include #endif @@ -1227,69 +1227,6 @@ void digipot_current(uint8_t driver, int current) { #endif } -#if ENABLED(DAC_STEPPER_CURRENT) - - bool dac_present = false; - const uint8_t dac_order[NUM_AXIS] = DAC_STEPPER_ORDER; - - int dac_init() { - mcp4728_init(); - - if (mcp4728_simpleCommand(RESET)) return -1; - - dac_present = true; - - mcp4728_setVref_all(DAC_STEPPER_VREF); - mcp4728_setGain_all(DAC_STEPPER_GAIN); - - return 0; - } - - void dac_current_percent(uint8_t channel, float val) { - if (!dac_present) return; - - NOMORE(val, 100); - - mcp4728_analogWrite(dac_order[channel], val * DAC_STEPPER_MAX / 100); - mcp4728_simpleCommand(UPDATE); - } - - void dac_current_raw(uint8_t channel, uint16_t val) { - if (!dac_present) return; - - NOMORE(val, DAC_STEPPER_MAX); - - mcp4728_analogWrite(dac_order[channel], val); - mcp4728_simpleCommand(UPDATE); - } - - static float dac_perc(int8_t n) { return 100.0 * mcp4728_getValue(dac_order[n]) / DAC_STEPPER_MAX; } - static float dac_amps(int8_t n) { return ((2.048 * mcp4728_getValue(dac_order[n])) / 4096.0) / (8.0 * DAC_STEPPER_SENSE); } - - void dac_print_values() { - if (!dac_present) return; - - SERIAL_ECHO_START; - SERIAL_ECHOLNPGM("Stepper current values in % (Amps):"); - SERIAL_ECHO_START; - SERIAL_ECHOPAIR(" X:", dac_perc(0)); - SERIAL_ECHOPAIR(" (", dac_amps(0)); - SERIAL_ECHOPAIR(") Y:", dac_perc(1)); - SERIAL_ECHOPAIR(" (", dac_amps(1)); - SERIAL_ECHOPAIR(") Z:", dac_perc(2)); - SERIAL_ECHOPAIR(" (", dac_amps(2)); - SERIAL_ECHOPAIR(") E:", dac_perc(3)); - SERIAL_ECHOPAIR(" (", dac_amps(3)); - SERIAL_ECHOLN(")"); - } - - void dac_commit_eeprom() { - if (!dac_present) return; - mcp4728_eepromWrite(); - } - -#endif // DAC_STEPPER_CURRENT - void microstep_init() { #if HAS_MICROSTEPS_E1 pinMode(E1_MS1_PIN, OUTPUT); diff --git a/Marlin/stepper_dac.cpp b/Marlin/stepper_dac.cpp new file mode 100644 index 0000000000..6b8ae8f3c2 --- /dev/null +++ b/Marlin/stepper_dac.cpp @@ -0,0 +1,87 @@ +/* + stepper_dac.cpp - To set stepper current via DAC + + Part of Marlin + + Copyright (c) 2016 MarlinFirmware + + Marlin is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Marlin is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Marlin. If not, see . +*/ + +#include "Marlin.h" + +#if ENABLED(DAC_STEPPER_CURRENT) + + #include "stepper_dac.h" + + bool dac_present = false; + const uint8_t dac_order[NUM_AXIS] = DAC_STEPPER_ORDER; + + int dac_init() { + mcp4728_init(); + + if (mcp4728_simpleCommand(RESET)) return -1; + + dac_present = true; + + mcp4728_setVref_all(DAC_STEPPER_VREF); + mcp4728_setGain_all(DAC_STEPPER_GAIN); + + return 0; + } + + void dac_current_percent(uint8_t channel, float val) { + if (!dac_present) return; + + NOMORE(val, 100); + + mcp4728_analogWrite(dac_order[channel], val * DAC_STEPPER_MAX / 100); + mcp4728_simpleCommand(UPDATE); + } + + void dac_current_raw(uint8_t channel, uint16_t val) { + if (!dac_present) return; + + NOMORE(val, DAC_STEPPER_MAX); + + mcp4728_analogWrite(dac_order[channel], val); + mcp4728_simpleCommand(UPDATE); + } + + static float dac_perc(int8_t n) { return 100.0 * mcp4728_getValue(dac_order[n]) / DAC_STEPPER_MAX; } + static float dac_amps(int8_t n) { return ((2.048 * mcp4728_getValue(dac_order[n])) / 4096.0) / (8.0 * DAC_STEPPER_SENSE); } + + void dac_print_values() { + if (!dac_present) return; + + SERIAL_ECHO_START; + SERIAL_ECHOLNPGM("Stepper current values in % (Amps):"); + SERIAL_ECHO_START; + SERIAL_ECHOPAIR(" X:", dac_perc(0)); + SERIAL_ECHOPAIR(" (", dac_amps(0)); + SERIAL_ECHOPAIR(") Y:", dac_perc(1)); + SERIAL_ECHOPAIR(" (", dac_amps(1)); + SERIAL_ECHOPAIR(") Z:", dac_perc(2)); + SERIAL_ECHOPAIR(" (", dac_amps(2)); + SERIAL_ECHOPAIR(") E:", dac_perc(3)); + SERIAL_ECHOPAIR(" (", dac_amps(3)); + SERIAL_ECHOLN(")"); + } + + void dac_commit_eeprom() { + if (!dac_present) return; + mcp4728_eepromWrite(); + } + +#endif // DAC_STEPPER_CURRENT diff --git a/Marlin/stepper_dac.h b/Marlin/stepper_dac.h new file mode 100644 index 0000000000..9fcf97c609 --- /dev/null +++ b/Marlin/stepper_dac.h @@ -0,0 +1,33 @@ +/* + stepper_dac.h - To set stepper current via DAC + + Part of Marlin + + Copyright (c) 2016 MarlinFirmware + + Marlin is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Marlin is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Marlin. If not, see . +*/ + +#ifndef STEPPER_DAC_H +#define STEPPER_DAC_H + +#include "dac_mcp4728.h" + +int dac_init(); +void dac_current_percent(uint8_t channel, float val); +void dac_current_raw(uint8_t channel, uint16_t val); +void dac_print_values(); +void dac_commit_eeprom(); + +#endif // STEPPER_DAC_H