From e8701195e66f2d27ffe17fb514eae8173795aaf7 Mon Sep 17 00:00:00 2001
From: Georgiy Bondarenko <69736697+nehilo@users.noreply.github.com>
Date: Thu, 4 Mar 2021 22:54:23 +0500
Subject: Initial commit
---
Marlin/src/feature/digipot/digipot.h | 33 ++++++++
Marlin/src/feature/digipot/digipot_mcp4018.cpp | 104 +++++++++++++++++++++++++
Marlin/src/feature/digipot/digipot_mcp4451.cpp | 100 ++++++++++++++++++++++++
3 files changed, 237 insertions(+)
create mode 100644 Marlin/src/feature/digipot/digipot.h
create mode 100644 Marlin/src/feature/digipot/digipot_mcp4018.cpp
create mode 100644 Marlin/src/feature/digipot/digipot_mcp4451.cpp
(limited to 'Marlin/src/feature/digipot')
diff --git a/Marlin/src/feature/digipot/digipot.h b/Marlin/src/feature/digipot/digipot.h
new file mode 100644
index 0000000..3fbd1f3
--- /dev/null
+++ b/Marlin/src/feature/digipot/digipot.h
@@ -0,0 +1,33 @@
+/**
+ * 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 .
+ *
+ */
+#pragma once
+
+//
+// Header for MCP4018 and MCP4451 current control i2c devices
+//
+class DigipotI2C {
+public:
+ static void init();
+ static void set_current(const uint8_t channel, const float current);
+};
+
+extern DigipotI2C digipot_i2c;
diff --git a/Marlin/src/feature/digipot/digipot_mcp4018.cpp b/Marlin/src/feature/digipot/digipot_mcp4018.cpp
new file mode 100644
index 0000000..37853ff
--- /dev/null
+++ b/Marlin/src/feature/digipot/digipot_mcp4018.cpp
@@ -0,0 +1,104 @@
+/**
+ * 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 .
+ *
+ */
+
+#include "../../inc/MarlinConfig.h"
+
+#if ENABLED(DIGIPOT_MCP4018)
+
+#include "digipot.h"
+
+#include
+#include // https://github.com/felias-fogg/SlowSoftI2CMaster
+
+// Settings for the I2C based DIGIPOT (MCP4018) based on WT150
+
+#define DIGIPOT_A4988_Rsx 0.250
+#define DIGIPOT_A4988_Vrefmax 1.666
+#define DIGIPOT_MCP4018_MAX_VALUE 127
+
+#define DIGIPOT_A4988_Itripmax(Vref) ((Vref) / (8.0 * DIGIPOT_A4988_Rsx))
+
+#define DIGIPOT_A4988_FACTOR ((DIGIPOT_MCP4018_MAX_VALUE) / DIGIPOT_A4988_Itripmax(DIGIPOT_A4988_Vrefmax))
+#define DIGIPOT_A4988_MAX_CURRENT 2.0
+
+static byte current_to_wiper(const float current) {
+ const int16_t value = TERN(DIGIPOT_USE_RAW_VALUES, current, CEIL(current * DIGIPOT_A4988_FACTOR));
+ return byte(constrain(value, 0, DIGIPOT_MCP4018_MAX_VALUE));
+}
+
+static SlowSoftI2CMaster pots[DIGIPOT_I2C_NUM_CHANNELS] = {
+ SlowSoftI2CMaster(DIGIPOTS_I2C_SDA_X, DIGIPOTS_I2C_SCL, ENABLED(DIGIPOT_ENABLE_I2C_PULLUPS))
+ #if DIGIPOT_I2C_NUM_CHANNELS > 1
+ , SlowSoftI2CMaster(DIGIPOTS_I2C_SDA_Y, DIGIPOTS_I2C_SCL, ENABLED(DIGIPOT_ENABLE_I2C_PULLUPS))
+ #if DIGIPOT_I2C_NUM_CHANNELS > 2
+ , SlowSoftI2CMaster(DIGIPOTS_I2C_SDA_Z, DIGIPOTS_I2C_SCL, ENABLED(DIGIPOT_ENABLE_I2C_PULLUPS))
+ #if DIGIPOT_I2C_NUM_CHANNELS > 3
+ , SlowSoftI2CMaster(DIGIPOTS_I2C_SDA_E0, DIGIPOTS_I2C_SCL, ENABLED(DIGIPOT_ENABLE_I2C_PULLUPS))
+ #if DIGIPOT_I2C_NUM_CHANNELS > 4
+ , SlowSoftI2CMaster(DIGIPOTS_I2C_SDA_E1, DIGIPOTS_I2C_SCL, ENABLED(DIGIPOT_ENABLE_I2C_PULLUPS))
+ #if DIGIPOT_I2C_NUM_CHANNELS > 5
+ , SlowSoftI2CMaster(DIGIPOTS_I2C_SDA_E2, DIGIPOTS_I2C_SCL, ENABLED(DIGIPOT_ENABLE_I2C_PULLUPS))
+ #if DIGIPOT_I2C_NUM_CHANNELS > 6
+ , SlowSoftI2CMaster(DIGIPOTS_I2C_SDA_E3, DIGIPOTS_I2C_SCL, ENABLED(DIGIPOT_ENABLE_I2C_PULLUPS))
+ #if DIGIPOT_I2C_NUM_CHANNELS > 7
+ , SlowSoftI2CMaster(DIGIPOTS_I2C_SDA_E4, DIGIPOTS_I2C_SCL, ENABLED(DIGIPOT_ENABLE_I2C_PULLUPS))
+ #endif
+ #endif
+ #endif
+ #endif
+ #endif
+ #endif
+ #endif
+};
+
+static void digipot_i2c_send(const uint8_t channel, const byte v) {
+ if (WITHIN(channel, 0, DIGIPOT_I2C_NUM_CHANNELS - 1)) {
+ pots[channel].i2c_start(((DIGIPOT_I2C_ADDRESS_A) << 1) | I2C_WRITE);
+ pots[channel].i2c_write(v);
+ pots[channel].i2c_stop();
+ }
+}
+
+// This is for the MCP4018 I2C based digipot
+void DigipotI2C::set_current(const uint8_t channel, const float current) {
+ const float ival = _MIN(_MAX(current, 0), float(DIGIPOT_MCP4018_MAX_VALUE));
+ digipot_i2c_send(channel, current_to_wiper(ival));
+}
+
+void DigipotI2C::init() {
+ LOOP_L_N(i, DIGIPOT_I2C_NUM_CHANNELS) pots[i].i2c_init();
+
+ // Init currents according to Configuration_adv.h
+ static const float digipot_motor_current[] PROGMEM =
+ #if ENABLED(DIGIPOT_USE_RAW_VALUES)
+ DIGIPOT_MOTOR_CURRENT
+ #else
+ DIGIPOT_I2C_MOTOR_CURRENTS
+ #endif
+ ;
+ LOOP_L_N(i, COUNT(digipot_motor_current))
+ set_current(i, pgm_read_float(&digipot_motor_current[i]));
+}
+
+DigipotI2C digipot_i2c;
+
+#endif // DIGIPOT_MCP4018
diff --git a/Marlin/src/feature/digipot/digipot_mcp4451.cpp b/Marlin/src/feature/digipot/digipot_mcp4451.cpp
new file mode 100644
index 0000000..1b4cf43
--- /dev/null
+++ b/Marlin/src/feature/digipot/digipot_mcp4451.cpp
@@ -0,0 +1,100 @@
+/**
+ * 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 .
+ *
+ */
+
+#include "../../inc/MarlinConfig.h"
+
+#if ENABLED(DIGIPOT_MCP4451)
+
+#include "digipot.h"
+
+#include
+#include
+
+#if MB(MKS_SBASE)
+ #include "digipot_mcp4451_I2C_routines.h"
+#endif
+
+// Settings for the I2C based DIGIPOT (MCP4451) on Azteeg X3 Pro
+#if MB(5DPRINT)
+ #define DIGIPOT_I2C_FACTOR 117.96f
+ #define DIGIPOT_I2C_MAX_CURRENT 1.736f
+#elif MB(AZTEEG_X5_MINI, AZTEEG_X5_MINI_WIFI)
+ #define DIGIPOT_I2C_FACTOR 113.5f
+ #define DIGIPOT_I2C_MAX_CURRENT 2.0f
+#else
+ #define DIGIPOT_I2C_FACTOR 106.7f
+ #define DIGIPOT_I2C_MAX_CURRENT 2.5f
+#endif
+
+static byte current_to_wiper(const float current) {
+ return byte(TERN(DIGIPOT_USE_RAW_VALUES, current, CEIL(DIGIPOT_I2C_FACTOR * current)));
+}
+
+static void digipot_i2c_send(const byte addr, const byte a, const byte b) {
+ #if MB(MKS_SBASE)
+ digipot_mcp4451_start(addr);
+ digipot_mcp4451_send_byte(a);
+ digipot_mcp4451_send_byte(b);
+ #else
+ Wire.beginTransmission(I2C_ADDRESS(addr));
+ Wire.write(a);
+ Wire.write(b);
+ Wire.endTransmission();
+ #endif
+}
+
+// This is for the MCP4451 I2C based digipot
+void DigipotI2C::set_current(const uint8_t channel, const float current) {
+ // These addresses are specific to Azteeg X3 Pro, can be set to others.
+ // In this case first digipot is at address A0=0, A1=0, second one is at A0=0, A1=1
+ const byte addr = channel < 4 ? DIGIPOT_I2C_ADDRESS_A : DIGIPOT_I2C_ADDRESS_B; // channel 0-3 vs 4-7
+
+ // Initial setup
+ digipot_i2c_send(addr, 0x40, 0xFF);
+ digipot_i2c_send(addr, 0xA0, 0xFF);
+
+ // Set actual wiper value
+ byte addresses[4] = { 0x00, 0x10, 0x60, 0x70 };
+ digipot_i2c_send(addr, addresses[channel & 0x3], current_to_wiper(_MIN(float(_MAX(current, 0)), DIGIPOT_I2C_MAX_CURRENT)));
+}
+
+void DigipotI2C::init() {
+ #if MB(MKS_SBASE)
+ configure_i2c(16); // Set clock_option to 16 ensure I2C is initialized at 400kHz
+ #else
+ Wire.begin();
+ #endif
+ // Set up initial currents as defined in Configuration_adv.h
+ static const float digipot_motor_current[] PROGMEM =
+ #if ENABLED(DIGIPOT_USE_RAW_VALUES)
+ DIGIPOT_MOTOR_CURRENT
+ #else
+ DIGIPOT_I2C_MOTOR_CURRENTS
+ #endif
+ ;
+ LOOP_L_N(i, COUNT(digipot_motor_current))
+ set_current(i, pgm_read_float(&digipot_motor_current[i]));
+}
+
+DigipotI2C digipot_i2c;
+
+#endif // DIGIPOT_MCP4451
--
cgit v1.2.3