aboutsummaryrefslogtreecommitdiff
path: root/Marlin/src/feature/dac
diff options
context:
space:
mode:
authorGeorgiy Bondarenko <69736697+nehilo@users.noreply.github.com>2021-03-04 20:54:23 +0300
committerGeorgiy Bondarenko <69736697+nehilo@users.noreply.github.com>2021-03-04 20:54:23 +0300
commite8701195e66f2d27ffe17fb514eae8173795aaf7 (patch)
tree9f519c4abf6556b9ae7190a6210d87ead1dfadde /Marlin/src/feature/dac
downloadkp3s-lgvl-e8701195e66f2d27ffe17fb514eae8173795aaf7.tar.xz
kp3s-lgvl-e8701195e66f2d27ffe17fb514eae8173795aaf7.zip
Initial commit
Diffstat (limited to 'Marlin/src/feature/dac')
-rw-r--r--Marlin/src/feature/dac/dac_dac084s085.cpp98
-rw-r--r--Marlin/src/feature/dac/dac_dac084s085.h31
-rw-r--r--Marlin/src/feature/dac/dac_mcp4728.cpp154
-rw-r--r--Marlin/src/feature/dac/dac_mcp4728.h82
-rw-r--r--Marlin/src/feature/dac/stepper_dac.cpp99
-rw-r--r--Marlin/src/feature/dac/stepper_dac.h41
6 files changed, 505 insertions, 0 deletions
diff --git a/Marlin/src/feature/dac/dac_dac084s085.cpp b/Marlin/src/feature/dac/dac_dac084s085.cpp
new file mode 100644
index 0000000..649aa55
--- /dev/null
+++ b/Marlin/src/feature/dac/dac_dac084s085.cpp
@@ -0,0 +1,98 @@
+/***************************************************************
+ *
+ * External DAC for Alligator Board
+ *
+ ****************************************************************/
+
+#include "../../inc/MarlinConfig.h"
+
+#if MB(ALLIGATOR)
+
+#include "dac_dac084s085.h"
+
+#include "../../MarlinCore.h"
+#include "../../module/stepper.h"
+#include "../../HAL/shared/Delay.h"
+
+dac084s085::dac084s085() { }
+
+void dac084s085::begin() {
+ uint8_t externalDac_buf[] = { 0x20, 0x00 }; // all off
+
+ // All SPI chip-select HIGH
+ SET_OUTPUT(DAC0_SYNC);
+ #if HAS_MULTI_EXTRUDER
+ SET_OUTPUT(DAC1_SYNC);
+ #endif
+ cshigh();
+ spiBegin();
+
+ //init onboard DAC
+ DELAY_US(2);
+ WRITE(DAC0_SYNC, LOW);
+ DELAY_US(2);
+ WRITE(DAC0_SYNC, HIGH);
+ DELAY_US(2);
+ WRITE(DAC0_SYNC, LOW);
+
+ spiSend(SPI_CHAN_DAC, externalDac_buf, COUNT(externalDac_buf));
+ WRITE(DAC0_SYNC, HIGH);
+
+ #if HAS_MULTI_EXTRUDER
+ //init Piggy DAC
+ DELAY_US(2);
+ WRITE(DAC1_SYNC, LOW);
+ DELAY_US(2);
+ WRITE(DAC1_SYNC, HIGH);
+ DELAY_US(2);
+ WRITE(DAC1_SYNC, LOW);
+
+ spiSend(SPI_CHAN_DAC, externalDac_buf, COUNT(externalDac_buf));
+ WRITE(DAC1_SYNC, HIGH);
+ #endif
+
+ return;
+}
+
+void dac084s085::setValue(const uint8_t channel, const uint8_t value) {
+ if (channel >= 7) return; // max channel (X,Y,Z,E0,E1,E2,E3)
+
+ const uint8_t externalDac_buf[] = {
+ 0x10 | ((channel > 3 ? 7 : 3) - channel << 6) | (value >> 4),
+ 0x00 | (value << 4)
+ };
+
+ // All SPI chip-select HIGH
+ cshigh();
+
+ if (channel > 3) { // DAC Piggy E1,E2,E3
+ WRITE(DAC1_SYNC, LOW);
+ DELAY_US(2);
+ WRITE(DAC1_SYNC, HIGH);
+ DELAY_US(2);
+ WRITE(DAC1_SYNC, LOW);
+ }
+ else { // DAC onboard X,Y,Z,E0
+ WRITE(DAC0_SYNC, LOW);
+ DELAY_US(2);
+ WRITE(DAC0_SYNC, HIGH);
+ DELAY_US(2);
+ WRITE(DAC0_SYNC, LOW);
+ }
+
+ DELAY_US(2);
+ spiSend(SPI_CHAN_DAC, externalDac_buf, COUNT(externalDac_buf));
+}
+
+void dac084s085::cshigh() {
+ WRITE(DAC0_SYNC, HIGH);
+ #if HAS_MULTI_EXTRUDER
+ WRITE(DAC1_SYNC, HIGH);
+ #endif
+ WRITE(SPI_EEPROM1_CS, HIGH);
+ WRITE(SPI_EEPROM2_CS, HIGH);
+ WRITE(SPI_FLASH_CS, HIGH);
+ WRITE(SD_SS_PIN, HIGH);
+}
+
+#endif // MB(ALLIGATOR)
diff --git a/Marlin/src/feature/dac/dac_dac084s085.h b/Marlin/src/feature/dac/dac_dac084s085.h
new file mode 100644
index 0000000..5be0129
--- /dev/null
+++ b/Marlin/src/feature/dac/dac_dac084s085.h
@@ -0,0 +1,31 @@
+/**
+ * Marlin 3D Printer Firmware
+ * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
+ *
+ * Based on Sprinter and grbl.
+ * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
+ *
+ * This program 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.
+ *
+ * This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+#pragma once
+
+class dac084s085 {
+ public:
+ dac084s085();
+ static void begin();
+ static void setValue(const uint8_t channel, const uint8_t value);
+ private:
+ static void cshigh();
+};
diff --git a/Marlin/src/feature/dac/dac_mcp4728.cpp b/Marlin/src/feature/dac/dac_mcp4728.cpp
new file mode 100644
index 0000000..4f33c4e
--- /dev/null
+++ b/Marlin/src/feature/dac/dac_mcp4728.cpp
@@ -0,0 +1,154 @@
+/**
+ * Marlin 3D Printer Firmware
+ * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
+ *
+ * Based on Sprinter and grbl.
+ * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
+ *
+ * This program 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.
+ *
+ * This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+/**
+ * mcp4728.cpp - Arduino library for MicroChip MCP4728 I2C D/A converter
+ *
+ * For implementation details, please take a look at the datasheet:
+ * https://ww1.microchip.com/downloads/en/DeviceDoc/22187a.pdf
+ *
+ * For discussion and feedback, please go to:
+ * https://forum.arduino.cc/index.php/topic,51842.0.html
+ */
+
+#include "../../inc/MarlinConfig.h"
+
+#if ENABLED(HAS_MOTOR_CURRENT_DAC)
+
+#include "dac_mcp4728.h"
+
+MCP4728 mcp4728;
+
+xyze_uint_t dac_values;
+
+/**
+ * Begin I2C, get current values (input register and eeprom) of mcp4728
+ */
+void MCP4728::init() {
+ Wire.begin();
+ Wire.requestFrom(I2C_ADDRESS(DAC_DEV_ADDRESS), uint8_t(24));
+ while (Wire.available()) {
+ char deviceID = Wire.read(),
+ hiByte = Wire.read(),
+ loByte = Wire.read();
+
+ if (!(deviceID & 0x08))
+ dac_values[(deviceID & 0x30) >> 4] = word((hiByte & 0x0F), loByte);
+ }
+}
+
+/**
+ * Write input resister value to specified channel using fastwrite method.
+ * Channel : 0-3, Values : 0-4095
+ */
+uint8_t MCP4728::analogWrite(const uint8_t channel, const uint16_t value) {
+ dac_values[channel] = value;
+ return fastWrite();
+}
+
+/**
+ * Write all input resistor values to EEPROM using SequencialWrite method.
+ * This will update both input register and EEPROM value
+ * This will also write current Vref, PowerDown, Gain settings to EEPROM
+ */
+uint8_t MCP4728::eepromWrite() {
+ Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS));
+ Wire.write(SEQWRITE);
+ LOOP_XYZE(i) {
+ Wire.write(DAC_STEPPER_VREF << 7 | DAC_STEPPER_GAIN << 4 | highByte(dac_values[i]));
+ Wire.write(lowByte(dac_values[i]));
+ }
+ return Wire.endTransmission();
+}
+
+/**
+ * Write Voltage reference setting to all input regiters
+ */
+uint8_t MCP4728::setVref_all(const uint8_t value) {
+ Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS));
+ Wire.write(VREFWRITE | (value ? 0x0F : 0x00));
+ return Wire.endTransmission();
+}
+/**
+ * Write Gain setting to all input regiters
+ */
+uint8_t MCP4728::setGain_all(const uint8_t value) {
+ Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS));
+ Wire.write(GAINWRITE | (value ? 0x0F : 0x00));
+ return Wire.endTransmission();
+}
+
+/**
+ * Return Input Register value
+ */
+uint16_t MCP4728::getValue(const uint8_t channel) { return dac_values[channel]; }
+
+#if 0
+/**
+ * Steph: Might be useful in the future
+ * Return Vout
+ */
+uint16_t MCP4728::getVout(const uint8_t channel) {
+ const uint32_t vref = 2048,
+ vOut = (vref * dac_values[channel] * (_DAC_STEPPER_GAIN + 1)) / 4096;
+ return _MIN(vOut, defaultVDD);
+}
+#endif
+
+/**
+ * Returns DAC values as a 0-100 percentage of drive strength
+ */
+uint8_t MCP4728::getDrvPct(const uint8_t channel) { return uint8_t(100.0 * dac_values[channel] / (DAC_STEPPER_MAX) + 0.5); }
+
+/**
+ * Receives all Drive strengths as 0-100 percent values, updates
+ * DAC Values array and calls fastwrite to update the DAC.
+ */
+void MCP4728::setDrvPct(xyze_uint_t &pct) {
+ dac_values = pct * (DAC_STEPPER_MAX) * 0.01f;
+ fastWrite();
+}
+
+/**
+ * FastWrite input register values - All DAC ouput update. refer to DATASHEET 5.6.1
+ * DAC Input and PowerDown bits update.
+ * No EEPROM update
+ */
+uint8_t MCP4728::fastWrite() {
+ Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS));
+ LOOP_XYZE(i) {
+ Wire.write(highByte(dac_values[i]));
+ Wire.write(lowByte(dac_values[i]));
+ }
+ return Wire.endTransmission();
+}
+
+/**
+ * Common function for simple general commands
+ */
+uint8_t MCP4728::simpleCommand(const byte simpleCommand) {
+ Wire.beginTransmission(I2C_ADDRESS(GENERALCALL));
+ Wire.write(simpleCommand);
+ return Wire.endTransmission();
+}
+
+#endif // HAS_MOTOR_CURRENT_DAC
diff --git a/Marlin/src/feature/dac/dac_mcp4728.h b/Marlin/src/feature/dac/dac_mcp4728.h
new file mode 100644
index 0000000..3a7d5f1
--- /dev/null
+++ b/Marlin/src/feature/dac/dac_mcp4728.h
@@ -0,0 +1,82 @@
+/**
+ * Marlin 3D Printer Firmware
+ * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
+ *
+ * Based on Sprinter and grbl.
+ * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
+ *
+ * This program 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.
+ *
+ * This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+#pragma once
+
+/**
+ * Arduino library for MicroChip MCP4728 I2C D/A converter.
+ */
+
+#include "../../core/types.h"
+
+#include <Wire.h>
+
+/**
+ * The following three macros are only used in this piece of code related to mcp4728.
+ * They are defined in the standard Arduino framework but could be undefined in 32 bits Arduino frameworks.
+ * (For instance not defined in Arduino lpc176x framework)
+ * So we have to define them if needed.
+ */
+#ifndef word
+ #define word(h, l) ((uint8_t) ((h << 8) | l))
+#endif
+
+#ifndef lowByte
+ #define lowByte(w) ((uint8_t) ((w) & 0xFF))
+#endif
+
+#ifndef highByte
+ #define highByte(w) ((uint8_t) ((w) >> 8))
+#endif
+
+#define defaultVDD DAC_STEPPER_MAX //was 5000 but differs with internal Vref
+#define BASE_ADDR 0x60
+#define RESET 0b00000110
+#define WAKE 0b00001001
+#define UPDATE 0b00001000
+#define MULTIWRITE 0b01000000
+#define SINGLEWRITE 0b01011000
+#define SEQWRITE 0b01010000
+#define VREFWRITE 0b10000000
+#define GAINWRITE 0b11000000
+#define POWERDOWNWRITE 0b10100000
+#define GENERALCALL 0b00000000
+#define GAINWRITE 0b11000000
+
+// This is taken from the original lib, makes it easy to edit if needed
+// DAC_OR_ADDRESS defined in pins_BOARD.h file
+#define DAC_DEV_ADDRESS (BASE_ADDR | DAC_OR_ADDRESS)
+
+class MCP4728 {
+public:
+ static void init();
+ static uint8_t analogWrite(const uint8_t channel, const uint16_t value);
+ static uint8_t eepromWrite();
+ static uint8_t setVref_all(const uint8_t value);
+ static uint8_t setGain_all(const uint8_t value);
+ static uint16_t getValue(const uint8_t channel);
+ static uint8_t fastWrite();
+ static uint8_t simpleCommand(const byte simpleCommand);
+ static uint8_t getDrvPct(const uint8_t channel);
+ static void setDrvPct(xyze_uint_t &pct);
+};
+
+extern MCP4728 mcp4728;
diff --git a/Marlin/src/feature/dac/stepper_dac.cpp b/Marlin/src/feature/dac/stepper_dac.cpp
new file mode 100644
index 0000000..5170a35
--- /dev/null
+++ b/Marlin/src/feature/dac/stepper_dac.cpp
@@ -0,0 +1,99 @@
+/**
+ * Marlin 3D Printer Firmware
+ * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
+ *
+ * Based on Sprinter and grbl.
+ * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
+ *
+ * This program 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.
+ *
+ * This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+/**
+ * stepper_dac.cpp - To set stepper current via DAC
+ */
+
+#include "../../inc/MarlinConfig.h"
+
+#if ENABLED(HAS_MOTOR_CURRENT_DAC)
+
+#include "stepper_dac.h"
+#include "../../MarlinCore.h" // for SP_X_LBL...
+
+bool dac_present = false;
+constexpr xyze_uint8_t dac_order = DAC_STEPPER_ORDER;
+xyze_uint_t dac_channel_pct = DAC_MOTOR_CURRENT_DEFAULT;
+
+StepperDAC stepper_dac;
+
+int StepperDAC::init() {
+ #if PIN_EXISTS(DAC_DISABLE)
+ OUT_WRITE(DAC_DISABLE_PIN, LOW); // set pin low to enable DAC
+ #endif
+
+ mcp4728.init();
+
+ if (mcp4728.simpleCommand(RESET)) return -1;
+
+ dac_present = true;
+
+ mcp4728.setVref_all(DAC_STEPPER_VREF);
+ mcp4728.setGain_all(DAC_STEPPER_GAIN);
+
+ if (mcp4728.getDrvPct(0) < 1 || mcp4728.getDrvPct(1) < 1 || mcp4728.getDrvPct(2) < 1 || mcp4728.getDrvPct(3) < 1 ) {
+ mcp4728.setDrvPct(dac_channel_pct);
+ mcp4728.eepromWrite();
+ }
+
+ return 0;
+}
+
+void StepperDAC::set_current_value(const uint8_t channel, uint16_t val) {
+ if (!dac_present) return;
+
+ NOMORE(val, uint16_t(DAC_STEPPER_MAX));
+
+ mcp4728.analogWrite(dac_order[channel], val);
+ mcp4728.simpleCommand(UPDATE);
+}
+
+void StepperDAC::set_current_percent(const uint8_t channel, float val) {
+ set_current_value(channel, _MIN(val, 100.0f) * (DAC_STEPPER_MAX) / 100.0f);
+}
+
+static float dac_perc(int8_t n) { return mcp4728.getDrvPct(dac_order[n]); }
+static float dac_amps(int8_t n) { return mcp4728.getValue(dac_order[n]) * 0.125 * RECIPROCAL(DAC_STEPPER_SENSE * 1000); }
+
+uint8_t StepperDAC::get_current_percent(const AxisEnum axis) { return mcp4728.getDrvPct(dac_order[axis]); }
+void StepperDAC::set_current_percents(xyze_uint8_t &pct) {
+ LOOP_XYZE(i) dac_channel_pct[i] = pct[dac_order[i]];
+ mcp4728.setDrvPct(dac_channel_pct);
+}
+
+void StepperDAC::print_values() {
+ if (!dac_present) return;
+ SERIAL_ECHO_MSG("Stepper current values in % (Amps):");
+ SERIAL_ECHO_START();
+ SERIAL_ECHOPAIR_P( SP_X_LBL, dac_perc(X_AXIS), PSTR(" ("), dac_amps(X_AXIS), PSTR(")"));
+ SERIAL_ECHOPAIR_P( SP_Y_LBL, dac_perc(Y_AXIS), PSTR(" ("), dac_amps(Y_AXIS), PSTR(")"));
+ SERIAL_ECHOPAIR_P( SP_Z_LBL, dac_perc(Z_AXIS), PSTR(" ("), dac_amps(Z_AXIS), PSTR(")"));
+ SERIAL_ECHOLNPAIR_P(SP_E_LBL, dac_perc(E_AXIS), PSTR(" ("), dac_amps(E_AXIS), PSTR(")"));
+}
+
+void StepperDAC::commit_eeprom() {
+ if (!dac_present) return;
+ mcp4728.eepromWrite();
+}
+
+#endif // HAS_MOTOR_CURRENT_DAC
diff --git a/Marlin/src/feature/dac/stepper_dac.h b/Marlin/src/feature/dac/stepper_dac.h
new file mode 100644
index 0000000..6836335
--- /dev/null
+++ b/Marlin/src/feature/dac/stepper_dac.h
@@ -0,0 +1,41 @@
+/**
+ * Marlin 3D Printer Firmware
+ * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
+ *
+ * Based on Sprinter and grbl.
+ * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
+ *
+ * This program 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.
+ *
+ * This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+#pragma once
+
+/**
+ * stepper_dac.h - To set stepper current via DAC
+ */
+
+#include "dac_mcp4728.h"
+
+class StepperDAC {
+public:
+ static int init();
+ static void set_current_percent(const uint8_t channel, float val);
+ static void set_current_value(const uint8_t channel, uint16_t val);
+ static void print_values();
+ static void commit_eeprom();
+ static uint8_t get_current_percent(AxisEnum axis);
+ static void set_current_percents(xyze_uint8_t &pct);
+};
+
+extern StepperDAC stepper_dac;