Marlin_Firmware/Marlin/src/feature/digipot/digipot_mcp4018.cpp

105 lines
3.9 KiB
C++
Raw Normal View History

2017-01-08 10:01:07 -06:00
/**
* Marlin 3D Printer Firmware
2020-02-03 08:00:57 -06:00
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
2017-01-08 10:01:07 -06:00
*
* Based on Sprinter and grbl.
2019-06-27 23:57:50 -05:00
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
2017-01-08 10:01:07 -06:00
*
* 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
2020-07-22 22:20:14 -05:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2017-01-08 10:01:07 -06:00
*
*/
2017-09-17 18:45:21 -05:00
#include "../../inc/MarlinConfig.h"
2017-01-08 10:01:07 -06:00
#if ENABLED(DIGIPOT_MCP4018)
2017-01-08 10:01:07 -06:00
2020-10-11 14:58:35 -05:00
#include "digipot.h"
2020-04-01 23:50:08 -05:00
#include <Stream.h>
2020-12-17 17:22:59 -06:00
#include <SlowSoftI2CMaster.h> // https://github.com/felias-fogg/SlowSoftI2CMaster
2017-01-08 10:01:07 -06:00
// 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
2017-01-08 10:01:07 -06:00
#define DIGIPOT_A4988_Itripmax(Vref) ((Vref) / (8.0 * DIGIPOT_A4988_Rsx))
2017-01-08 10:01:07 -06:00
#define DIGIPOT_A4988_FACTOR ((DIGIPOT_MCP4018_MAX_VALUE) / DIGIPOT_A4988_Itripmax(DIGIPOT_A4988_Vrefmax))
#define DIGIPOT_A4988_MAX_CURRENT 2.0
2017-01-08 10:01:07 -06:00
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));
2017-01-08 10:01:07 -06:00
}
static SlowSoftI2CMaster pots[DIGIPOT_I2C_NUM_CHANNELS] = {
2020-12-17 17:22:59 -06:00
SlowSoftI2CMaster(DIGIPOTS_I2C_SDA_X, DIGIPOTS_I2C_SCL, ENABLED(DIGIPOT_ENABLE_I2C_PULLUPS))
#if DIGIPOT_I2C_NUM_CHANNELS > 1
2020-12-17 17:22:59 -06:00
, SlowSoftI2CMaster(DIGIPOTS_I2C_SDA_Y, DIGIPOTS_I2C_SCL, ENABLED(DIGIPOT_ENABLE_I2C_PULLUPS))
#if DIGIPOT_I2C_NUM_CHANNELS > 2
2020-12-17 17:22:59 -06:00
, SlowSoftI2CMaster(DIGIPOTS_I2C_SDA_Z, DIGIPOTS_I2C_SCL, ENABLED(DIGIPOT_ENABLE_I2C_PULLUPS))
#if DIGIPOT_I2C_NUM_CHANNELS > 3
2020-12-17 17:22:59 -06:00
, SlowSoftI2CMaster(DIGIPOTS_I2C_SDA_E0, DIGIPOTS_I2C_SCL, ENABLED(DIGIPOT_ENABLE_I2C_PULLUPS))
#if DIGIPOT_I2C_NUM_CHANNELS > 4
2020-12-17 17:22:59 -06:00
, SlowSoftI2CMaster(DIGIPOTS_I2C_SDA_E1, DIGIPOTS_I2C_SCL, ENABLED(DIGIPOT_ENABLE_I2C_PULLUPS))
#if DIGIPOT_I2C_NUM_CHANNELS > 5
2020-12-17 17:22:59 -06:00
, SlowSoftI2CMaster(DIGIPOTS_I2C_SDA_E2, DIGIPOTS_I2C_SCL, ENABLED(DIGIPOT_ENABLE_I2C_PULLUPS))
#if DIGIPOT_I2C_NUM_CHANNELS > 6
2020-12-17 17:22:59 -06:00
, SlowSoftI2CMaster(DIGIPOTS_I2C_SDA_E3, DIGIPOTS_I2C_SCL, ENABLED(DIGIPOT_ENABLE_I2C_PULLUPS))
#if DIGIPOT_I2C_NUM_CHANNELS > 7
2020-12-17 17:22:59 -06:00
, SlowSoftI2CMaster(DIGIPOTS_I2C_SDA_E4, DIGIPOTS_I2C_SCL, ENABLED(DIGIPOT_ENABLE_I2C_PULLUPS))
#endif
#endif
#endif
#endif
#endif
#endif
#endif
2017-01-08 10:01:07 -06:00
};
2020-10-11 14:58:35 -05:00
static void digipot_i2c_send(const uint8_t channel, const byte v) {
2017-01-08 10:01:07 -06:00
if (WITHIN(channel, 0, DIGIPOT_I2C_NUM_CHANNELS - 1)) {
pots[channel].i2c_start(((DIGIPOT_I2C_ADDRESS_A) << 1) | I2C_WRITE);
2017-01-08 10:01:07 -06:00
pots[channel].i2c_write(v);
pots[channel].i2c_stop();
}
}
// This is for the MCP4018 I2C based digipot
2020-10-11 14:58:35 -05:00
void DigipotI2C::set_current(const uint8_t channel, const float current) {
const float ival = _MIN(_MAX(current, 0), float(DIGIPOT_MCP4018_MAX_VALUE));
2020-10-11 14:58:35 -05:00
digipot_i2c_send(channel, current_to_wiper(ival));
2017-01-08 10:01:07 -06:00
}
2020-10-11 14:58:35 -05:00
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
;
2020-03-13 23:18:16 -05:00
LOOP_L_N(i, COUNT(digipot_motor_current))
2020-10-11 14:58:35 -05:00
set_current(i, pgm_read_float(&digipot_motor_current[i]));
2017-01-08 10:01:07 -06:00
}
2020-10-14 13:44:03 -05:00
DigipotI2C digipot_i2c;
#endif // DIGIPOT_MCP4018