2016-03-25 07:19:46 +01:00
|
|
|
/**
|
2016-03-24 19:01:20 +01:00
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 15:00:57 +01:00
|
|
|
* Copyright (c) 2020 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
|
2020-07-23 05:20:14 +02:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2016-03-24 19:01:20 +01:00
|
|
|
*
|
|
|
|
*/
|
2016-03-10 23:33:53 +01:00
|
|
|
|
2016-03-25 07:19:46 +01:00
|
|
|
/**
|
2016-03-24 19:01:20 +01:00
|
|
|
* mcp4728.cpp - Arduino library for MicroChip MCP4728 I2C D/A converter
|
|
|
|
*
|
|
|
|
* For implementation details, please take a look at the datasheet:
|
2020-07-24 03:46:11 +02:00
|
|
|
* https://ww1.microchip.com/downloads/en/DeviceDoc/22187a.pdf
|
2016-03-24 19:01:20 +01:00
|
|
|
*
|
|
|
|
* For discussion and feedback, please go to:
|
2020-07-28 08:04:44 +02:00
|
|
|
* https://forum.arduino.cc/index.php/topic,51842.0.html
|
2016-03-24 19:01:20 +01:00
|
|
|
*/
|
2016-03-10 23:33:53 +01:00
|
|
|
|
2017-09-06 13:28:32 +02:00
|
|
|
#include "../../inc/MarlinConfig.h"
|
2016-03-10 23:33:53 +01:00
|
|
|
|
2021-07-10 00:09:58 +02:00
|
|
|
#if HAS_MOTOR_CURRENT_DAC
|
2016-03-10 23:33:53 +01:00
|
|
|
|
2017-09-06 13:28:32 +02:00
|
|
|
#include "dac_mcp4728.h"
|
|
|
|
|
2020-10-11 21:58:35 +02:00
|
|
|
MCP4728 mcp4728;
|
|
|
|
|
|
|
|
xyze_uint_t dac_values;
|
2016-03-10 23:33:53 +01:00
|
|
|
|
2016-03-25 07:19:46 +01:00
|
|
|
/**
|
|
|
|
* Begin I2C, get current values (input register and eeprom) of mcp4728
|
|
|
|
*/
|
2020-10-11 21:58:35 +02:00
|
|
|
void MCP4728::init() {
|
2016-03-10 23:33:53 +01:00
|
|
|
Wire.begin();
|
2020-04-29 10:13:29 +02:00
|
|
|
Wire.requestFrom(I2C_ADDRESS(DAC_DEV_ADDRESS), uint8_t(24));
|
2016-10-07 22:57:24 +02:00
|
|
|
while (Wire.available()) {
|
|
|
|
char deviceID = Wire.read(),
|
|
|
|
hiByte = Wire.read(),
|
|
|
|
loByte = Wire.read();
|
2016-03-10 23:33:53 +01:00
|
|
|
|
2016-10-07 22:57:24 +02:00
|
|
|
if (!(deviceID & 0x08))
|
2020-10-11 21:58:35 +02:00
|
|
|
dac_values[(deviceID & 0x30) >> 4] = word((hiByte & 0x0F), loByte);
|
2016-03-10 23:33:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 07:19:46 +01:00
|
|
|
/**
|
|
|
|
* Write input resister value to specified channel using fastwrite method.
|
|
|
|
* Channel : 0-3, Values : 0-4095
|
|
|
|
*/
|
2020-10-11 21:58:35 +02:00
|
|
|
uint8_t MCP4728::analogWrite(const uint8_t channel, const uint16_t value) {
|
|
|
|
dac_values[channel] = value;
|
|
|
|
return fastWrite();
|
2016-03-10 23:33:53 +01:00
|
|
|
}
|
2016-10-10 21:08:04 +02:00
|
|
|
|
2016-03-25 07:19:46 +01:00
|
|
|
/**
|
2021-05-11 17:47:32 +02:00
|
|
|
* Write all input resistor values to EEPROM using SequentialWrite method.
|
2016-03-25 07:19:46 +01:00
|
|
|
* This will update both input register and EEPROM value
|
|
|
|
* This will also write current Vref, PowerDown, Gain settings to EEPROM
|
|
|
|
*/
|
2020-10-11 21:58:35 +02:00
|
|
|
uint8_t MCP4728::eepromWrite() {
|
2019-02-20 13:26:36 +01:00
|
|
|
Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS));
|
2016-06-27 21:51:50 +02:00
|
|
|
Wire.write(SEQWRITE);
|
2021-05-19 05:51:19 +02:00
|
|
|
LOOP_LOGICAL_AXES(i) {
|
2020-10-11 21:58:35 +02:00
|
|
|
Wire.write(DAC_STEPPER_VREF << 7 | DAC_STEPPER_GAIN << 4 | highByte(dac_values[i]));
|
|
|
|
Wire.write(lowByte(dac_values[i]));
|
2016-03-10 23:33:53 +01:00
|
|
|
}
|
|
|
|
return Wire.endTransmission();
|
|
|
|
}
|
|
|
|
|
2016-03-25 07:19:46 +01:00
|
|
|
/**
|
|
|
|
* Write Voltage reference setting to all input regiters
|
|
|
|
*/
|
2020-10-11 21:58:35 +02:00
|
|
|
uint8_t MCP4728::setVref_all(const uint8_t value) {
|
2019-02-20 13:26:36 +01:00
|
|
|
Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS));
|
2016-10-09 23:45:00 +02:00
|
|
|
Wire.write(VREFWRITE | (value ? 0x0F : 0x00));
|
2016-03-10 23:33:53 +01:00
|
|
|
return Wire.endTransmission();
|
|
|
|
}
|
2016-03-25 07:19:46 +01:00
|
|
|
/**
|
|
|
|
* Write Gain setting to all input regiters
|
|
|
|
*/
|
2020-10-11 21:58:35 +02:00
|
|
|
uint8_t MCP4728::setGain_all(const uint8_t value) {
|
2019-02-20 13:26:36 +01:00
|
|
|
Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS));
|
2016-10-07 22:57:24 +02:00
|
|
|
Wire.write(GAINWRITE | (value ? 0x0F : 0x00));
|
2016-03-10 23:33:53 +01:00
|
|
|
return Wire.endTransmission();
|
|
|
|
}
|
|
|
|
|
2016-03-25 07:19:46 +01:00
|
|
|
/**
|
2016-10-09 23:45:29 +02:00
|
|
|
* Return Input Register value
|
2016-03-25 07:19:46 +01:00
|
|
|
*/
|
2020-10-11 21:58:35 +02:00
|
|
|
uint16_t MCP4728::getValue(const uint8_t channel) { return dac_values[channel]; }
|
2016-03-10 23:33:53 +01:00
|
|
|
|
2017-11-18 09:08:03 +01:00
|
|
|
#if 0
|
2016-03-25 07:19:46 +01:00
|
|
|
/**
|
|
|
|
* Steph: Might be useful in the future
|
|
|
|
* Return Vout
|
2017-11-18 09:08:03 +01:00
|
|
|
*/
|
2020-10-11 21:58:35 +02:00
|
|
|
uint16_t MCP4728::getVout(const uint8_t channel) {
|
2019-05-26 10:02:23 +02:00
|
|
|
const uint32_t vref = 2048,
|
2020-10-11 21:58:35 +02:00
|
|
|
vOut = (vref * dac_values[channel] * (_DAC_STEPPER_GAIN + 1)) / 4096;
|
2019-07-06 01:01:21 +02:00
|
|
|
return _MIN(vOut, defaultVDD);
|
2016-03-10 23:33:53 +01:00
|
|
|
}
|
2017-11-18 09:08:03 +01:00
|
|
|
#endif
|
2016-03-10 23:33:53 +01:00
|
|
|
|
2016-10-10 21:08:04 +02:00
|
|
|
/**
|
|
|
|
* Returns DAC values as a 0-100 percentage of drive strength
|
|
|
|
*/
|
2020-10-11 21:58:35 +02:00
|
|
|
uint8_t MCP4728::getDrvPct(const uint8_t channel) { return uint8_t(100.0 * dac_values[channel] / (DAC_STEPPER_MAX) + 0.5); }
|
2016-10-04 00:15:29 +02:00
|
|
|
|
2016-10-10 21:08:04 +02:00
|
|
|
/**
|
|
|
|
* Receives all Drive strengths as 0-100 percent values, updates
|
|
|
|
* DAC Values array and calls fastwrite to update the DAC.
|
|
|
|
*/
|
2020-11-20 01:08:21 +01:00
|
|
|
void MCP4728::setDrvPct(xyze_uint_t &pct) {
|
|
|
|
dac_values = pct * (DAC_STEPPER_MAX) * 0.01f;
|
2020-10-11 21:58:35 +02:00
|
|
|
fastWrite();
|
2016-10-04 00:15:29 +02:00
|
|
|
}
|
|
|
|
|
2016-03-25 07:19:46 +01:00
|
|
|
/**
|
|
|
|
* FastWrite input register values - All DAC ouput update. refer to DATASHEET 5.6.1
|
|
|
|
* DAC Input and PowerDown bits update.
|
|
|
|
* No EEPROM update
|
|
|
|
*/
|
2020-10-11 21:58:35 +02:00
|
|
|
uint8_t MCP4728::fastWrite() {
|
2019-02-20 13:26:36 +01:00
|
|
|
Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS));
|
2021-05-19 05:51:19 +02:00
|
|
|
LOOP_LOGICAL_AXES(i) {
|
2020-10-11 21:58:35 +02:00
|
|
|
Wire.write(highByte(dac_values[i]));
|
|
|
|
Wire.write(lowByte(dac_values[i]));
|
2016-03-10 23:33:53 +01:00
|
|
|
}
|
|
|
|
return Wire.endTransmission();
|
|
|
|
}
|
|
|
|
|
2016-03-25 07:19:46 +01:00
|
|
|
/**
|
|
|
|
* Common function for simple general commands
|
|
|
|
*/
|
2020-10-11 21:58:35 +02:00
|
|
|
uint8_t MCP4728::simpleCommand(const byte simpleCommand) {
|
2019-02-20 13:26:36 +01:00
|
|
|
Wire.beginTransmission(I2C_ADDRESS(GENERALCALL));
|
2016-06-27 21:51:50 +02:00
|
|
|
Wire.write(simpleCommand);
|
2016-03-10 23:33:53 +01:00
|
|
|
return Wire.endTransmission();
|
|
|
|
}
|
|
|
|
|
2020-10-11 21:58:35 +02:00
|
|
|
#endif // HAS_MOTOR_CURRENT_DAC
|