Subversion Repositories MK-Marlin

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ron 1
/**
2
 * Marlin 3D Printer Firmware
3
 * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
 *
5
 * Based on Sprinter and grbl.
6
 * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
 
23
#include "MarlinConfig.h"
24
 
25
#if ENABLED(DIGIPOT_I2C) && ENABLED(DIGIPOT_MCP4018)
26
 
27
#include "enum.h"
28
#include "Stream.h"
29
#include "utility/twi.h"
30
#include <SlowSoftI2CMaster.h>  //https://github.com/stawel/SlowSoftI2CMaster
31
 
32
// Settings for the I2C based DIGIPOT (MCP4018) based on WT150
33
 
34
#define DIGIPOT_I2C_ADDRESS             0x2F
35
 
36
#define DIGIPOT_A4988_Rsx               0.250
37
#define DIGIPOT_A4988_Vrefmax           1.666
38
#define DIGIPOT_A4988_MAX_VALUE         127
39
 
40
#define DIGIPOT_A4988_Itripmax(Vref)    ((Vref)/(8.0*DIGIPOT_A4988_Rsx))
41
 
42
#define DIGIPOT_A4988_FACTOR            ((DIGIPOT_A4988_MAX_VALUE)/DIGIPOT_A4988_Itripmax(DIGIPOT_A4988_Vrefmax))
43
#define DIGIPOT_A4988_MAX_CURRENT       2.0
44
 
45
static byte current_to_wiper(const float current) {
46
  const int16_t value = ceil(float(DIGIPOT_A4988_FACTOR) * current);
47
  return byte(constrain(value, 0, DIGIPOT_A4988_MAX_VALUE));
48
}
49
 
50
const uint8_t sda_pins[DIGIPOT_I2C_NUM_CHANNELS] = {
51
  DIGIPOTS_I2C_SDA_X
52
  #if DIGIPOT_I2C_NUM_CHANNELS > 1
53
    , DIGIPOTS_I2C_SDA_Y
54
    #if DIGIPOT_I2C_NUM_CHANNELS > 2
55
      , DIGIPOTS_I2C_SDA_Z
56
      #if DIGIPOT_I2C_NUM_CHANNELS > 3
57
        , DIGIPOTS_I2C_SDA_E0
58
        #if DIGIPOT_I2C_NUM_CHANNELS > 4
59
          , DIGIPOTS_I2C_SDA_E1
60
        #endif
61
      #endif
62
    #endif
63
  #endif
64
};
65
 
66
static SlowSoftI2CMaster pots[DIGIPOT_I2C_NUM_CHANNELS] = {
67
  SlowSoftI2CMaster { sda_pins[X_AXIS], DIGIPOTS_I2C_SCL }
68
  #if DIGIPOT_I2C_NUM_CHANNELS > 1
69
    , SlowSoftI2CMaster { sda_pins[Y_AXIS], DIGIPOTS_I2C_SCL }
70
    #if DIGIPOT_I2C_NUM_CHANNELS > 2
71
      , SlowSoftI2CMaster { sda_pins[Z_AXIS], DIGIPOTS_I2C_SCL }
72
      #if DIGIPOT_I2C_NUM_CHANNELS > 3
73
        , SlowSoftI2CMaster { sda_pins[E_AXIS], DIGIPOTS_I2C_SCL }
74
        #if DIGIPOT_I2C_NUM_CHANNELS > 4
75
          , SlowSoftI2CMaster { sda_pins[E_AXIS + 1], DIGIPOTS_I2C_SCL }
76
        #endif
77
      #endif
78
    #endif
79
  #endif
80
};
81
 
82
static void i2c_send(const uint8_t channel, const byte v) {
83
  if (WITHIN(channel, 0, DIGIPOT_I2C_NUM_CHANNELS - 1)) {
84
    pots[channel].i2c_start(((DIGIPOT_I2C_ADDRESS) << 1) | I2C_WRITE);
85
    pots[channel].i2c_write(v);
86
    pots[channel].i2c_stop();
87
  }
88
}
89
 
90
// This is for the MCP4018 I2C based digipot
91
void digipot_i2c_set_current(uint8_t channel, float current) {
92
  i2c_send(channel, current_to_wiper(MIN(MAX(current, 0), float(DIGIPOT_A4988_MAX_CURRENT))));
93
}
94
 
95
void digipot_i2c_init() {
96
  static const float digipot_motor_current[] PROGMEM = DIGIPOT_I2C_MOTOR_CURRENTS;
97
 
98
  for (uint8_t i = 0; i < DIGIPOT_I2C_NUM_CHANNELS; i++)
99
    pots[i].i2c_init();
100
 
101
  // setup initial currents as defined in Configuration_adv.h
102
  for (uint8_t i = 0; i < COUNT(digipot_motor_current); i++)
103
    digipot_i2c_set_current(i, pgm_read_float(&digipot_motor_current[i]));
104
}
105
 
106
#endif // DIGIPOT_I2C && DIGIPOT_MCP4018