2016-03-25 07:19:46 +01:00
|
|
|
/**
|
2016-03-24 19:01:20 +01:00
|
|
|
* Marlin 3D Printer Firmware
|
2019-06-28 06:57:50 +02:00
|
|
|
* Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2016-03-24 19:01:20 +01:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-28 06:57:50 +02:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2016-03-24 19:01:20 +01: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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-09-18 01:45:21 +02:00
|
|
|
#include "../../inc/MarlinConfig.h"
|
2014-02-05 10:47:12 +01:00
|
|
|
|
2017-01-08 17:01:07 +01:00
|
|
|
#if ENABLED(DIGIPOT_I2C) && DISABLED(DIGIPOT_MCP4018)
|
2015-03-02 16:06:01 +01:00
|
|
|
|
2014-02-05 10:47:12 +01:00
|
|
|
#include "Stream.h"
|
2017-09-18 01:45:21 +02:00
|
|
|
#include <Wire.h>
|
2014-02-05 10:47:12 +01:00
|
|
|
|
2018-03-04 22:14:47 +01:00
|
|
|
#if MB(MKS_SBASE)
|
|
|
|
#include "digipot_mcp4451_I2C_routines.h"
|
|
|
|
#endif
|
|
|
|
|
2014-02-05 10:47:12 +01:00
|
|
|
// Settings for the I2C based DIGIPOT (MCP4451) on Azteeg X3 Pro
|
2014-12-18 21:02:36 +01:00
|
|
|
#if MB(5DPRINT)
|
2015-03-02 16:06:01 +01:00
|
|
|
#define DIGIPOT_I2C_FACTOR 117.96
|
|
|
|
#define DIGIPOT_I2C_MAX_CURRENT 1.736
|
2019-04-09 06:10:45 +02:00
|
|
|
#elif MB(AZTEEG_X5_MINI) || MB(AZTEEG_X5_MINI_WIFI)
|
|
|
|
#define DIGIPOT_I2C_FACTOR 113.5
|
|
|
|
#define DIGIPOT_I2C_MAX_CURRENT 2.0
|
2014-04-24 06:56:46 +02:00
|
|
|
#else
|
2015-03-02 16:06:01 +01:00
|
|
|
#define DIGIPOT_I2C_FACTOR 106.7
|
|
|
|
#define DIGIPOT_I2C_MAX_CURRENT 2.5
|
2014-04-24 06:56:46 +02:00
|
|
|
#endif
|
2014-02-05 10:47:12 +01:00
|
|
|
|
2017-06-06 13:10:10 +02:00
|
|
|
static byte current_to_wiper(const float current) {
|
2017-06-20 05:39:23 +02:00
|
|
|
return byte(CEIL(float((DIGIPOT_I2C_FACTOR * current))));
|
2014-02-05 10:47:12 +01:00
|
|
|
}
|
|
|
|
|
2017-06-06 13:10:10 +02:00
|
|
|
static void i2c_send(const byte addr, const byte a, const byte b) {
|
2018-03-04 22:14:47 +01:00
|
|
|
#if MB(MKS_SBASE)
|
|
|
|
digipot_mcp4451_start(addr);
|
|
|
|
digipot_mcp4451_send_byte(a);
|
|
|
|
digipot_mcp4451_send_byte(b);
|
|
|
|
#else
|
2019-02-20 13:26:36 +01:00
|
|
|
Wire.beginTransmission(I2C_ADDRESS(addr));
|
2018-03-04 22:14:47 +01:00
|
|
|
Wire.write(a);
|
|
|
|
Wire.write(b);
|
|
|
|
Wire.endTransmission();
|
|
|
|
#endif
|
2014-02-05 10:47:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is for the MCP4451 I2C based digipot
|
2017-09-18 01:45:21 +02:00
|
|
|
void digipot_i2c_set_current(const uint8_t channel, const float current) {
|
2015-03-02 16:06:01 +01:00
|
|
|
// 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
|
2017-10-11 22:42:24 +02:00
|
|
|
const byte addr = channel < 4 ? DIGIPOT_I2C_ADDRESS_A : DIGIPOT_I2C_ADDRESS_B; // channel 0-3 vs 4-7
|
2015-03-02 16:06:01 +01:00
|
|
|
|
|
|
|
// Initial setup
|
2017-04-22 04:42:27 +02:00
|
|
|
i2c_send(addr, 0x40, 0xFF);
|
|
|
|
i2c_send(addr, 0xA0, 0xFF);
|
2015-03-02 16:06:01 +01:00
|
|
|
|
|
|
|
// Set actual wiper value
|
|
|
|
byte addresses[4] = { 0x00, 0x10, 0x60, 0x70 };
|
2019-09-08 08:02:04 +02:00
|
|
|
i2c_send(addr, addresses[channel & 0x3], current_to_wiper(_MIN(float(_MAX(current, 0)), DIGIPOT_I2C_MAX_CURRENT)));
|
2014-02-05 10:47:12 +01:00
|
|
|
}
|
|
|
|
|
2015-03-02 16:06:01 +01:00
|
|
|
void digipot_i2c_init() {
|
2018-03-04 22:14:47 +01:00
|
|
|
#if MB(MKS_SBASE)
|
|
|
|
digipot_mcp4451_init();
|
|
|
|
#else
|
|
|
|
Wire.begin();
|
|
|
|
#endif
|
2015-03-02 16:06:01 +01:00
|
|
|
// setup initial currents as defined in Configuration_adv.h
|
2018-03-04 22:14:47 +01:00
|
|
|
static const float digipot_motor_current[] PROGMEM = DIGIPOT_I2C_MOTOR_CURRENTS;
|
2017-06-06 13:10:10 +02:00
|
|
|
for (uint8_t i = 0; i < COUNT(digipot_motor_current); i++)
|
|
|
|
digipot_i2c_set_current(i, pgm_read_float(&digipot_motor_current[i]));
|
2014-02-05 10:47:12 +01:00
|
|
|
}
|
2015-03-02 16:06:01 +01:00
|
|
|
|
2017-05-09 19:35:43 +02:00
|
|
|
#endif // DIGIPOT_I2C
|