Subversion Repositories Tronxy-X3A-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
/**
24
 * mcp4728.cpp - Arduino library for MicroChip MCP4728 I2C D/A converter
25
 *
26
 * For implementation details, please take a look at the datasheet:
27
 * http://ww1.microchip.com/downloads/en/DeviceDoc/22187a.pdf
28
 *
29
 * For discussion and feedback, please go to:
30
 * http://arduino.cc/forum/index.php/topic,51842.0.html
31
 */
32
 
33
#include "MarlinConfig.h"
34
 
35
#if ENABLED(DAC_STEPPER_CURRENT)
36
 
37
#include "dac_mcp4728.h"
38
#include "enum.h"
39
 
40
uint16_t mcp4728_values[XYZE];
41
 
42
/**
43
 * Begin I2C, get current values (input register and eeprom) of mcp4728
44
 */
45
void mcp4728_init() {
46
  Wire.begin();
47
  Wire.requestFrom(int(DAC_DEV_ADDRESS), 24);
48
  while (Wire.available()) {
49
    char deviceID = Wire.read(),
50
         hiByte = Wire.read(),
51
         loByte = Wire.read();
52
 
53
    if (!(deviceID & 0x08))
54
      mcp4728_values[(deviceID & 0x30) >> 4] = word((hiByte & 0x0F), loByte);
55
  }
56
}
57
 
58
/**
59
 * Write input resister value to specified channel using fastwrite method.
60
 * Channel : 0-3, Values : 0-4095
61
 */
62
uint8_t mcp4728_analogWrite(uint8_t channel, uint16_t value) {
63
  mcp4728_values[channel] = value;
64
  return mcp4728_fastWrite();
65
}
66
 
67
/**
68
 * Write all input resistor values to EEPROM using SequencialWrite method.
69
 * This will update both input register and EEPROM value
70
 * This will also write current Vref, PowerDown, Gain settings to EEPROM
71
 */
72
uint8_t mcp4728_eepromWrite() {
73
  Wire.beginTransmission(DAC_DEV_ADDRESS);
74
  Wire.write(SEQWRITE);
75
  LOOP_XYZE(i) {
76
    Wire.write(DAC_STEPPER_VREF << 7 | DAC_STEPPER_GAIN << 4 | highByte(mcp4728_values[i]));
77
    Wire.write(lowByte(mcp4728_values[i]));
78
  }
79
  return Wire.endTransmission();
80
}
81
 
82
/**
83
 * Write Voltage reference setting to all input regiters
84
 */
85
uint8_t mcp4728_setVref_all(uint8_t value) {
86
  Wire.beginTransmission(DAC_DEV_ADDRESS);
87
  Wire.write(VREFWRITE | (value ? 0x0F : 0x00));
88
  return Wire.endTransmission();
89
}
90
/**
91
 * Write Gain setting to all input regiters
92
 */
93
uint8_t mcp4728_setGain_all(uint8_t value) {
94
  Wire.beginTransmission(DAC_DEV_ADDRESS);
95
  Wire.write(GAINWRITE | (value ? 0x0F : 0x00));
96
  return Wire.endTransmission();
97
}
98
 
99
/**
100
 * Return Input Register value
101
 */
102
uint16_t mcp4728_getValue(uint8_t channel) { return mcp4728_values[channel]; }
103
 
104
/**
105
 * Steph: Might be useful in the future
106
 * Return Vout
107
 *
108
uint16_t mcp4728_getVout(uint8_t channel) {
109
  uint32_t vref = 2048,
110
           vOut = (vref * mcp4728_values[channel] * (_DAC_STEPPER_GAIN + 1)) / 4096;
111
  if (vOut > defaultVDD) vOut = defaultVDD;
112
  return vOut;
113
}
114
*/
115
 
116
/**
117
 * Returns DAC values as a 0-100 percentage of drive strength
118
 */
119
uint8_t mcp4728_getDrvPct(uint8_t channel) { return uint8_t(100.0 * mcp4728_values[channel] / (DAC_STEPPER_MAX) + 0.5); }
120
 
121
/**
122
 * Receives all Drive strengths as 0-100 percent values, updates
123
 * DAC Values array and calls fastwrite to update the DAC.
124
 */
125
void mcp4728_setDrvPct(uint8_t pct[XYZE]) {
126
  LOOP_XYZE(i) mcp4728_values[i] = 0.01 * pct[i] * (DAC_STEPPER_MAX);
127
  mcp4728_fastWrite();
128
}
129
 
130
/**
131
 * FastWrite input register values - All DAC ouput update. refer to DATASHEET 5.6.1
132
 * DAC Input and PowerDown bits update.
133
 * No EEPROM update
134
 */
135
uint8_t mcp4728_fastWrite() {
136
  Wire.beginTransmission(DAC_DEV_ADDRESS);
137
  LOOP_XYZE(i) {
138
    Wire.write(highByte(mcp4728_values[i]));
139
    Wire.write(lowByte(mcp4728_values[i]));
140
  }
141
  return Wire.endTransmission();
142
}
143
 
144
/**
145
 * Common function for simple general commands
146
 */
147
uint8_t mcp4728_simpleCommand(byte simpleCommand) {
148
  Wire.beginTransmission(GENERALCALL);
149
  Wire.write(simpleCommand);
150
  return Wire.endTransmission();
151
}
152
 
153
#endif // DAC_STEPPER_CURRENT