My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

digipot_mcp4018.cpp 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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. #include "../../inc/MarlinConfig.h"
  23. #if BOTH(DIGIPOT_I2C, DIGIPOT_MCP4018)
  24. #include "Stream.h"
  25. #include "utility/twi.h"
  26. #include <SlowSoftI2CMaster.h> //https://github.com/stawel/SlowSoftI2CMaster
  27. // Settings for the I2C based DIGIPOT (MCP4018) based on WT150
  28. #define DIGIPOT_A4988_Rsx 0.250
  29. #define DIGIPOT_A4988_Vrefmax 1.666
  30. #define DIGIPOT_A4988_MAX_VALUE 127
  31. #define DIGIPOT_A4988_Itripmax(Vref) ((Vref)/(8.0*DIGIPOT_A4988_Rsx))
  32. #define DIGIPOT_A4988_FACTOR ((DIGIPOT_A4988_MAX_VALUE)/DIGIPOT_A4988_Itripmax(DIGIPOT_A4988_Vrefmax))
  33. #define DIGIPOT_A4988_MAX_CURRENT 2.0
  34. static byte current_to_wiper(const float current) {
  35. const int16_t value = ceil(float(DIGIPOT_A4988_FACTOR) * current);
  36. return byte(constrain(value, 0, DIGIPOT_A4988_MAX_VALUE));
  37. }
  38. const uint8_t sda_pins[DIGIPOT_I2C_NUM_CHANNELS] = {
  39. DIGIPOTS_I2C_SDA_X
  40. #if DIGIPOT_I2C_NUM_CHANNELS > 1
  41. , DIGIPOTS_I2C_SDA_Y
  42. #if DIGIPOT_I2C_NUM_CHANNELS > 2
  43. , DIGIPOTS_I2C_SDA_Z
  44. #if DIGIPOT_I2C_NUM_CHANNELS > 3
  45. , DIGIPOTS_I2C_SDA_E0
  46. #if DIGIPOT_I2C_NUM_CHANNELS > 4
  47. , DIGIPOTS_I2C_SDA_E1
  48. #endif
  49. #endif
  50. #endif
  51. #endif
  52. };
  53. static SlowSoftI2CMaster pots[DIGIPOT_I2C_NUM_CHANNELS] = {
  54. SlowSoftI2CMaster { sda_pins[X_AXIS], DIGIPOTS_I2C_SCL }
  55. #if DIGIPOT_I2C_NUM_CHANNELS > 1
  56. , SlowSoftI2CMaster { sda_pins[Y_AXIS], DIGIPOTS_I2C_SCL }
  57. #if DIGIPOT_I2C_NUM_CHANNELS > 2
  58. , SlowSoftI2CMaster { sda_pins[Z_AXIS], DIGIPOTS_I2C_SCL }
  59. #if DIGIPOT_I2C_NUM_CHANNELS > 3
  60. , SlowSoftI2CMaster { sda_pins[E_AXIS], DIGIPOTS_I2C_SCL }
  61. #if DIGIPOT_I2C_NUM_CHANNELS > 4
  62. , SlowSoftI2CMaster { sda_pins[E_AXIS + 1], DIGIPOTS_I2C_SCL }
  63. #endif
  64. #endif
  65. #endif
  66. #endif
  67. };
  68. static void i2c_send(const uint8_t channel, const byte v) {
  69. if (WITHIN(channel, 0, DIGIPOT_I2C_NUM_CHANNELS - 1)) {
  70. pots[channel].i2c_start(((DIGIPOT_I2C_ADDRESS_A) << 1) | I2C_WRITE);
  71. pots[channel].i2c_write(v);
  72. pots[channel].i2c_stop();
  73. }
  74. }
  75. // This is for the MCP4018 I2C based digipot
  76. void digipot_i2c_set_current(const uint8_t channel, const float current) {
  77. i2c_send(channel, current_to_wiper(_MIN(_MAX(current, 0), float(DIGIPOT_A4988_MAX_CURRENT))));
  78. }
  79. void digipot_i2c_init() {
  80. static const float digipot_motor_current[] PROGMEM = DIGIPOT_I2C_MOTOR_CURRENTS;
  81. LOOP_L_N(i, DIGIPOT_I2C_NUM_CHANNELS)
  82. pots[i].i2c_init();
  83. // setup initial currents as defined in Configuration_adv.h
  84. LOOP_L_N(i, COUNT(digipot_motor_current))
  85. digipot_i2c_set_current(i, pgm_read_float(&digipot_motor_current[i]));
  86. }
  87. #endif // DIGIPOT_I2C && DIGIPOT_MCP4018