M3426 to read i2c MCP3426 ADC (#23184)

This commit is contained in:
Stuart Pittaway 2021-12-04 23:44:10 +00:00 committed by Scott Lahteine
parent 05b57278d4
commit c0addd1d33
9 changed files with 234 additions and 5 deletions

View File

@ -0,0 +1,104 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* 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
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
/**
* adc_mcp3426.cpp - library for MicroChip MCP3426 I2C A/D converter
*
* For implementation details, please take a look at the datasheet:
* https://www.microchip.com/en-us/product/MCP3426
*/
#include "../../inc/MarlinConfig.h"
#if ENABLED(HAS_MCP3426_ADC)
#include "adc_mcp3426.h"
// Read the ADC value from MCP342X on a specific channel
int16_t MCP3426::ReadValue(uint8_t channel, uint8_t gain) {
Error = false;
#if PINS_EXIST(I2C_SCL, I2C_SDA) && DISABLED(SOFT_I2C_EEPROM)
Wire.setSDA(pin_t(I2C_SDA_PIN));
Wire.setSCL(pin_t(I2C_SCL_PIN));
#endif
Wire.begin(); // No address joins the BUS as the master
Wire.beginTransmission(I2C_ADDRESS(MCP342X_ADC_I2C_ADDRESS));
// Continuous Conversion Mode, 16 bit, Channel 1, Gain x4
// 26 = 0b00011000
// RXXCSSGG
// R = Ready Bit
// XX = Channel (00=1, 01=2, 10=3 (MCP3428), 11=4 (MCP3428))
// C = Conversion Mode Bit (1= Continuous Conversion Mode (Default))
// SS = Sample rate, 10=15 samples per second @ 16 bits
// GG = Gain 00 =x1
uint8_t controlRegister = 0b00011000;
if (channel == 2) controlRegister |= 0b00100000; // Select channel 2
if (gain == 2)
controlRegister |= 0b00000001;
else if (gain == 4)
controlRegister |= 0b00000010;
else if (gain == 8)
controlRegister |= 0b00000011;
Wire.write(controlRegister);
if (Wire.endTransmission() != 0) {
Error = true;
return 0;
}
const uint8_t len = 3;
uint8_t buffer[len] = {};
do {
Wire.requestFrom(I2C_ADDRESS(MCP342X_ADC_I2C_ADDRESS), len);
if (Wire.available() != len) {
Error = true;
return 0;
}
for (uint8_t i = 0; i < len; ++i)
buffer[i] = Wire.read();
// Is conversion ready, if not loop around again
} while ((buffer[2] & 0x80) != 0);
union TwoBytesToInt16 {
uint8_t bytes[2];
int16_t integervalue;
};
TwoBytesToInt16 ConversionUnion;
ConversionUnion.bytes[1] = buffer[0];
ConversionUnion.bytes[0] = buffer[1];
return ConversionUnion.integervalue;
}
MCP3426 mcp3426;
#endif // HAS_MCP3426_ADC

View File

@ -0,0 +1,41 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* 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
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#pragma once
/**
* Arduino library for MicroChip MCP3426 I2C A/D converter.
* https://www.microchip.com/en-us/product/MCP3426
*/
#include <stdint.h>
#include <Wire.h>
// Address of MCP342X chip
#define MCP342X_ADC_I2C_ADDRESS 104
class MCP3426 {
public:
int16_t ReadValue(uint8_t channel, uint8_t gain);
bool Error;
};
extern MCP3426 mcp3426;

View File

@ -34,13 +34,18 @@ TWIBus i2c;
TWIBus::TWIBus() { TWIBus::TWIBus() {
#if I2C_SLAVE_ADDRESS == 0 #if I2C_SLAVE_ADDRESS == 0
Wire.begin( // No address joins the BUS as the master
#if PINS_EXIST(I2C_SCL, I2C_SDA) && DISABLED(SOFT_I2C_EEPROM) #if PINS_EXIST(I2C_SCL, I2C_SDA) && DISABLED(SOFT_I2C_EEPROM)
pin_t(I2C_SDA_PIN), pin_t(I2C_SCL_PIN) Wire.setSDA(pin_t(I2C_SDA_PIN));
#endif Wire.setSCL(pin_t(I2C_SCL_PIN));
); #endif
Wire.begin(); // No address joins the BUS as the master
#else #else
Wire.begin(I2C_SLAVE_ADDRESS); // Join the bus as a slave Wire.begin(I2C_SLAVE_ADDRESS); // Join the bus as a slave
#endif #endif
reset(); reset();
} }

View File

@ -0,0 +1,63 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* 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
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include "../../../inc/MarlinConfig.h"
#if ENABLED(HAS_MCP3426_ADC)
#include "../../gcode.h"
#include "../../../feature/adc/adc_mcp3426.h"
/**
* M3426: Read 16 bit (signed) value from I2C MCP3426 ADC device
*
* M3426 C<byte-1 value in base 10> channel 1 or 2
* M3426 G<byte-1 value in base 10> gain 1, 2, 4 or 8
* M3426 I<byte-2 value in base 10> 0 or 1, invert reply
*/
void GcodeSuite::M3426() {
uint8_t channel = parser.byteval('C', 1), // Select the channel 1 or 2
gain = parser.byteval('G', 1);
const bool inverted = parser.byteval('I') == 1;
if (channel <= 2 && (gain == 1 || gain == 2 || gain == 4 || gain == 8)) {
int16_t result = mcp3426.ReadValue(channel, gain);
if (mcp3426.Error == false) {
if (inverted) {
// Should we invert the reading (32767 - ADC value) ?
// Caters to end devices that expect values to increase when in reality they decrease.
// e.g., A pressure sensor in a vacuum when the end device expects a positive pressure.
result = INT16_MAX - result;
}
//SERIAL_ECHOPGM(STR_OK);
SERIAL_ECHOLNPGM("V:", result, " C:", channel, " G:", gain, " I:", inverted ? 1 : 0);
}
else
SERIAL_ERROR_MSG("MCP342X i2c error");
}
else
SERIAL_ERROR_MSG("MCP342X Bad request");
}
#endif // HAS_MCP3426_ADC

View File

@ -1044,6 +1044,10 @@ void GcodeSuite::process_parsed_command(const bool no_ok/*=false*/) {
case 7219: M7219(); break; // M7219: Set LEDs, columns, and rows case 7219: M7219(); break; // M7219: Set LEDs, columns, and rows
#endif #endif
#if ENABLED(HAS_MCP3426_ADC)
case 3426: M3426(); break; // M3426: Read MCP3426 ADC (over i2c)
#endif
default: parser.unknown_command_warning(); break; default: parser.unknown_command_warning(); break;
} }
break; break;

View File

@ -296,6 +296,7 @@
* M917 - L6470 tuning: Find minimum current thresholds. (Requires at least one _DRIVER_TYPE L6470) * M917 - L6470 tuning: Find minimum current thresholds. (Requires at least one _DRIVER_TYPE L6470)
* M918 - L6470 tuning: Increase speed until max or error. (Requires at least one _DRIVER_TYPE L6470) * M918 - L6470 tuning: Increase speed until max or error. (Requires at least one _DRIVER_TYPE L6470)
* M951 - Set Magnetic Parking Extruder parameters. (Requires MAGNETIC_PARKING_EXTRUDER) * M951 - Set Magnetic Parking Extruder parameters. (Requires MAGNETIC_PARKING_EXTRUDER)
* M3426 - Read MCP3426 ADC over I2C. (Requires HAS_MCP3426_ADC)
* M7219 - Control Max7219 Matrix LEDs. (Requires MAX7219_GCODE) * M7219 - Control Max7219 Matrix LEDs. (Requires MAX7219_GCODE)
* *
*** SCARA *** *** SCARA ***
@ -1199,6 +1200,10 @@ private:
static void M1004(); static void M1004();
#endif #endif
#if ENABLED(HAS_MCP3426_ADC)
static void M3426();
#endif
#if ENABLED(MAX7219_GCODE) #if ENABLED(MAX7219_GCODE)
static void M7219(); static void M7219();
#endif #endif

View File

@ -42,6 +42,9 @@
#define SRAM_EEPROM_EMULATION #define SRAM_EEPROM_EMULATION
#define MARLIN_EEPROM_SIZE 0x2000 // 8KB #define MARLIN_EEPROM_SIZE 0x2000 // 8KB
// I2C MCP3426 (16-Bit, 240SPS, dual-channel ADC)
#define HAS_MCP3426_ADC
// //
// Servos // Servos
// //
@ -116,6 +119,8 @@
#define FAN2_PIN PE4 #define FAN2_PIN PE4
#define FAN3_PIN PE5 #define FAN3_PIN PE5
#define FAN_SOFT_PWM
// Neopixel Rings // Neopixel Rings
#define NEOPIXEL_PIN PC7 #define NEOPIXEL_PIN PC7
#define NEOPIXEL2_PIN PC8 #define NEOPIXEL2_PIN PC8

View File

@ -94,6 +94,7 @@ NEXTION_TFT = src_filter=+<src/lcd/extui/nextion>
USE_UHS2_USB = src_filter=+<src/sd/usb_flashdrive/lib-uhs2> USE_UHS2_USB = src_filter=+<src/sd/usb_flashdrive/lib-uhs2>
USE_UHS3_USB = src_filter=+<src/sd/usb_flashdrive/lib-uhs3> USE_UHS3_USB = src_filter=+<src/sd/usb_flashdrive/lib-uhs3>
USB_FLASH_DRIVE_SUPPORT = src_filter=+<src/sd/usb_flashdrive/Sd2Card_FlashDrive.cpp> USB_FLASH_DRIVE_SUPPORT = src_filter=+<src/sd/usb_flashdrive/Sd2Card_FlashDrive.cpp>
HAS_MCP3426_ADC = src_filter=+<src/feature/adc> +<src/gcode/feature/adc>
AUTO_BED_LEVELING_BILINEAR = src_filter=+<src/feature/bedlevel/abl> AUTO_BED_LEVELING_BILINEAR = src_filter=+<src/feature/bedlevel/abl>
AUTO_BED_LEVELING_(3POINT|(BI)?LINEAR) = src_filter=+<src/gcode/bedlevel/abl> AUTO_BED_LEVELING_(3POINT|(BI)?LINEAR) = src_filter=+<src/gcode/bedlevel/abl>
MESH_BED_LEVELING = src_filter=+<src/feature/bedlevel/mbl> +<src/gcode/bedlevel/mbl> MESH_BED_LEVELING = src_filter=+<src/feature/bedlevel/mbl> +<src/gcode/bedlevel/mbl>

View File

@ -92,6 +92,7 @@ default_src_filter = +<src/*> -<src/config> -<src/HAL> +<src/HAL/shared>
-<src/HAL/shared/cpu_exception> -<src/HAL/shared/cpu_exception>
-<src/HAL/shared/eeprom_if_i2c.cpp> -<src/HAL/shared/eeprom_if_i2c.cpp>
-<src/HAL/shared/eeprom_if_spi.cpp> -<src/HAL/shared/eeprom_if_spi.cpp>
-<src/feature/adc> -<src/gcode/feature/adc>
-<src/feature/babystep.cpp> -<src/feature/babystep.cpp>
-<src/feature/backlash.cpp> -<src/feature/backlash.cpp>
-<src/feature/baricuda.cpp> -<src/gcode/feature/baricuda> -<src/feature/baricuda.cpp> -<src/gcode/feature/baricuda>