Save lots of PROGMEM, ~20b SRAM with DIGIPOT_I2C

2.0.x
Scott Lahteine 8 years ago
parent 4134a6b526
commit c9e3caf928

@ -782,7 +782,7 @@ extern "C" {
#endif // !SDSUPPORT #endif // !SDSUPPORT
#if ENABLED(DIGIPOT_I2C) #if ENABLED(DIGIPOT_I2C)
extern void digipot_i2c_set_current(int channel, float current); extern void digipot_i2c_set_current(uint8_t channel, float current);
extern void digipot_i2c_init(); extern void digipot_i2c_init();
#endif #endif

@ -1165,6 +1165,16 @@ static_assert(1 >= 0
#error "Enable STEALTHCHOP to use HYBRID_THRESHOLD." #error "Enable STEALTHCHOP to use HYBRID_THRESHOLD."
#endif #endif
/**
* Digipot requirement
*/
#if ENABLED(DIGIPOT_MCP4018)
#if !defined(DIGIPOTS_I2C_SDA_X) || !defined(DIGIPOTS_I2C_SDA_Y) || !defined(DIGIPOTS_I2C_SDA_Z) \
|| !defined(DIGIPOTS_I2C_SDA_E0) || !defined(DIGIPOTS_I2C_SDA_E1)
#error "DIGIPOT_MCP4018 requires DIGIPOTS_I2C_SDA_* pins to be defined."
#endif
#endif
/** /**
* Require 4 or more elements in per-axis initializers * Require 4 or more elements in per-axis initializers
*/ */

@ -24,6 +24,7 @@
#if ENABLED(DIGIPOT_I2C) && ENABLED(DIGIPOT_MCP4018) #if ENABLED(DIGIPOT_I2C) && ENABLED(DIGIPOT_MCP4018)
#include "enum.h"
#include "Stream.h" #include "Stream.h"
#include "utility/twi.h" #include "utility/twi.h"
#include <SlowSoftI2CMaster.h> //https://github.com/stawel/SlowSoftI2CMaster #include <SlowSoftI2CMaster.h> //https://github.com/stawel/SlowSoftI2CMaster
@ -38,31 +39,47 @@
#define DIGIPOT_A4988_Itripmax(Vref) ((Vref)/(8.0*DIGIPOT_A4988_Rsx)) #define DIGIPOT_A4988_Itripmax(Vref) ((Vref)/(8.0*DIGIPOT_A4988_Rsx))
#define DIGIPOT_A4988_FACTOR (DIGIPOT_A4988_MAX_VALUE/DIGIPOT_A4988_Itripmax(DIGIPOT_A4988_Vrefmax)) #define DIGIPOT_A4988_FACTOR ((DIGIPOT_A4988_MAX_VALUE)/DIGIPOT_A4988_Itripmax(DIGIPOT_A4988_Vrefmax))
//TODO: MAX_CURRENT -0.5A ?? (currently set to 2A, max possible current 2.5A) //TODO: MAX_CURRENT -0.5A ?? (currently set to 2A, max possible current 2.5A)
#define DIGIPOT_A4988_MAX_CURRENT (DIGIPOT_A4988_Itripmax(DIGIPOT_A4988_Vrefmax) - 0.5) #define DIGIPOT_A4988_MAX_CURRENT (DIGIPOT_A4988_Itripmax(DIGIPOT_A4988_Vrefmax) - 0.5)
static byte current_to_wiper(float current) { static byte current_to_wiper(const float current) {
return byte(ceil(float((DIGIPOT_A4988_FACTOR * current)))); return byte(ceil(float(DIGIPOT_A4988_FACTOR) * current));
} }
static uint8_t sda_pins[DIGIPOT_I2C_NUM_CHANNELS] = { const uint8_t sda_pins[DIGIPOT_I2C_NUM_CHANNELS] = {
DIGIPOTS_I2C_SDA_X, DIGIPOTS_I2C_SDA_X
DIGIPOTS_I2C_SDA_Y, #if DIGIPOT_I2C_NUM_CHANNELS > 1
DIGIPOTS_I2C_SDA_Z, , DIGIPOTS_I2C_SDA_Y
DIGIPOTS_I2C_SDA_E0, #if DIGIPOT_I2C_NUM_CHANNELS > 2
DIGIPOTS_I2C_SDA_E1, , DIGIPOTS_I2C_SDA_Z
#if DIGIPOT_I2C_NUM_CHANNELS > 3
, DIGIPOTS_I2C_SDA_E0
#if DIGIPOT_I2C_NUM_CHANNELS > 4
, DIGIPOTS_I2C_SDA_E1
#endif
#endif
#endif
#endif
}; };
static SlowSoftI2CMaster pots[DIGIPOT_I2C_NUM_CHANNELS] = { static SlowSoftI2CMaster pots[DIGIPOT_I2C_NUM_CHANNELS] = {
SlowSoftI2CMaster { sda_pins[0], DIGIPOTS_I2C_SCL }, SlowSoftI2CMaster { sda_pins[X_AXIS], DIGIPOTS_I2C_SCL }
SlowSoftI2CMaster { sda_pins[1], DIGIPOTS_I2C_SCL }, #if DIGIPOT_I2C_NUM_CHANNELS > 1
SlowSoftI2CMaster { sda_pins[2], DIGIPOTS_I2C_SCL }, , SlowSoftI2CMaster { sda_pins[Y_AXIS], DIGIPOTS_I2C_SCL }
SlowSoftI2CMaster { sda_pins[3], DIGIPOTS_I2C_SCL }, #if DIGIPOT_I2C_NUM_CHANNELS > 2
SlowSoftI2CMaster { sda_pins[4], DIGIPOTS_I2C_SCL } , SlowSoftI2CMaster { sda_pins[Z_AXIS], DIGIPOTS_I2C_SCL }
#if DIGIPOT_I2C_NUM_CHANNELS > 3
, SlowSoftI2CMaster { sda_pins[E_AXIS], DIGIPOTS_I2C_SCL }
#if DIGIPOT_I2C_NUM_CHANNELS > 4
, SlowSoftI2CMaster { sda_pins[E_AXIS + 1], DIGIPOTS_I2C_SCL }
#endif
#endif
#endif
#endif
}; };
static void i2c_send(int channel, byte v) { static void i2c_send(const uint8_t channel, const byte v) {
if (WITHIN(channel, 0, DIGIPOT_I2C_NUM_CHANNELS - 1)) { if (WITHIN(channel, 0, DIGIPOT_I2C_NUM_CHANNELS - 1)) {
pots[channel].i2c_start(((DIGIPOT_I2C_ADDRESS) << 1) | I2C_WRITE); pots[channel].i2c_start(((DIGIPOT_I2C_ADDRESS) << 1) | I2C_WRITE);
pots[channel].i2c_write(v); pots[channel].i2c_write(v);
@ -71,21 +88,19 @@ static void i2c_send(int channel, byte v) {
} }
// This is for the MCP4018 I2C based digipot // This is for the MCP4018 I2C based digipot
void digipot_i2c_set_current(int channel, float current) { void digipot_i2c_set_current(uint8_t channel, float current) {
current = min(max(current, 0.0f), float(DIGIPOT_A4988_MAX_CURRENT)); i2c_send(channel, current_to_wiper(min(max(current, 0.0f), float(DIGIPOT_A4988_MAX_CURRENT))));
i2c_send(channel, current_to_wiper(current));
} }
void digipot_i2c_init() { void digipot_i2c_init() {
const float digipot_motor_current[] = DIGIPOT_I2C_MOTOR_CURRENTS; static const float digipot_motor_current[] PROGMEM = DIGIPOT_I2C_MOTOR_CURRENTS;
for (uint8_t i = 0; i < DIGIPOT_I2C_NUM_CHANNELS; i++) for (uint8_t i = 0; i < DIGIPOT_I2C_NUM_CHANNELS; i++)
pots[i].i2c_init(); pots[i].i2c_init();
// setup initial currents as defined in Configuration_adv.h // setup initial currents as defined in Configuration_adv.h
for (uint8_t i = 0; i < COUNT(digipot_motor_current); i++) for (uint8_t i = 0; i < COUNT(digipot_motor_current); i++)
digipot_i2c_set_current(i, digipot_motor_current[i]); digipot_i2c_set_current(i, pgm_read_float(&digipot_motor_current[i]));
} }
#endif // DIGIPOT_I2C && DIGIPOT_MCP4018 #endif // DIGIPOT_I2C && DIGIPOT_MCP4018

@ -37,11 +37,11 @@
#define DIGIPOT_I2C_MAX_CURRENT 2.5 #define DIGIPOT_I2C_MAX_CURRENT 2.5
#endif #endif
static byte current_to_wiper(float current) { static byte current_to_wiper(const float current) {
return byte(ceil(float((DIGIPOT_I2C_FACTOR * current)))); return byte(ceil(float((DIGIPOT_I2C_FACTOR * current))));
} }
static void i2c_send(byte addr, byte a, byte b) { static void i2c_send(const byte addr, const byte a, const byte b) {
Wire.beginTransmission(addr); Wire.beginTransmission(addr);
Wire.write(a); Wire.write(a);
Wire.write(b); Wire.write(b);
@ -49,7 +49,7 @@ static void i2c_send(byte addr, byte a, byte b) {
} }
// This is for the MCP4451 I2C based digipot // This is for the MCP4451 I2C based digipot
void digipot_i2c_set_current(int channel, float current) { void digipot_i2c_set_current(uint8_t channel, float current) {
current = min((float) max(current, 0.0f), DIGIPOT_I2C_MAX_CURRENT); current = min((float) max(current, 0.0f), DIGIPOT_I2C_MAX_CURRENT);
// these addresses are specific to Azteeg X3 Pro, can be set to others, // 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 // In this case first digipot is at address A0=0, A1= 0, second one is at A0=0, A1= 1
@ -69,11 +69,11 @@ void digipot_i2c_set_current(int channel, float current) {
} }
void digipot_i2c_init() { void digipot_i2c_init() {
const float digipot_motor_current[] = DIGIPOT_I2C_MOTOR_CURRENTS; static const float digipot_motor_current[] PROGMEM = DIGIPOT_I2C_MOTOR_CURRENTS;
Wire.begin(); Wire.begin();
// setup initial currents as defined in Configuration_adv.h // setup initial currents as defined in Configuration_adv.h
for (int i = 0; i < COUNT(digipot_motor_current); i++) for (uint8_t i = 0; i < COUNT(digipot_motor_current); i++)
digipot_i2c_set_current(i, digipot_motor_current[i]); digipot_i2c_set_current(i, pgm_read_float(&digipot_motor_current[i]));
} }
#endif // DIGIPOT_I2C #endif // DIGIPOT_I2C

Loading…
Cancel
Save