wip: grbl: hook up ADC module
diff --git a/grbl/config.h b/grbl/config.h
index f48d958..29f695f 100644
--- a/grbl/config.h
+++ b/grbl/config.h
@@ -674,7 +674,52 @@
// updating lots of code to ensure everything is running correctly.
// #define DUAL_AXIS_CONFIG_CNC_SHIELD_CLONE // Uncomment to select. Comment other configs.
+// General switch to turn the ADC module on, further tuning below
+#ifndef USE_ADC // USE_ADC not passed in from make
+// Uncomment for enabling ADC by default, off by default
+//#define USE_ADC
+#endif
+
+#ifdef USE_ADC
+ // Supported modes. On 328p there are 9 different available modes for the ADC. Here only
+ // those actually supported in adc.h and adc.c. The mode is locked at compile time and is
+ // not possible to switch mode at runtime, making all ADC_CONFIG_MODE_ options mutually
+ // exclusive.
+ #define ADC_CONFIG_MODE_SW_TRIGGER 0 // Kick the next conversion manually in software
+ #define ADC_CONFIG_MODE_HW_TRIGGER 1 // HW kicks automatically the next conversion
+
+ // Supported vref are 2 internal and 1 external. If the external is wired to anything, never
+ // use any internal reference. 10uF capacitor between power anc gnd is suggested in all cases.
+ // Locked at compile time, mutually exclusive.
+ #define ADC_CONFIG_REFERENCE_INTERNAL_1VREF 0
+ #define ADC_CONFIG_REFERENCE_INTERNAL_5VREF 1
+ #define ADC_CONFIG_REFERENCE_EXTERNAL_AVCC 2
+
+ // There are 11 available channels on 328p, including GND and VBG
+ #define ADC_CONFIG_CHANNELS_COUNT 11
+
+ // 11 bit bitmask, set the bit corresponding to the channel to be enabled. Add more by or-ing,
+ // ie: (bit(0) | bit(3) | bit(6)). ADC8 has an internal temperature sensor plugged in, this
+ // is enabled by default.
+ #define ADC_CONFIG_ENABLED_CHANNELS (bit(8))
+
+ // Flag system alarm on abnormal readings, off by default
+ //#define ADC_CONFIG_SYSTEM_ALARM
+
+ // Needed if ADC_CONFIG_SYSTEM_ALARM is enabled.
+ //
+ // For each enabled channel the build will check is it is also defined a range
+ // of accepted values, this is used at runtime for raising a system alarms and
+ // for reporting information to the user. The sintax for defining a range is:
+ // ADC_CONFIG_CHANNEL_ ## NUMBER ## _MIN (i.e: ADC_CONFIG_RANGE_CHANNEL_8_MIN)
+ // and
+ // ADC_CONFIG_CHANNEL_ ## NUMBER ## _MAX (i.e: ADC_CONFIG_RANGE_CHANNEL_8_MAX)
+ #ifdef ADC_CONFIG_SYSTEM_ALARM
+ #define ADC_CONFIG_RANGE_CHANNEL_8_MIN 0
+ #define ADC_CONFIG_RANGE_CHANNEL_8_MAX 70
+ #endif
+#endif
/* ---------------------------------------------------------------------------------------
OEM Single File Configuration Option
diff --git a/grbl/defaults.h b/grbl/defaults.h
index 2e46130..46fee6a 100644
--- a/grbl/defaults.h
+++ b/grbl/defaults.h
@@ -63,6 +63,19 @@
#define DEFAULT_HOMING_SEEK_RATE 500.0 // mm/min
#define DEFAULT_HOMING_DEBOUNCE_DELAY 250 // msec (0-65k)
#define DEFAULT_HOMING_PULLOFF 1.0 // mm
+ #ifdef USE_ADC
+ #define DEFAULT_ENABLE_ADC 1 // Setting bit for marking ADC as on
+ #define DEFAULT_ADC_MODE ADC_CONFIG_MODE_SW_TRIGGER // Software triggering next conversion
+ #define DEFAULT_ADC_REFERENCE ADC_CONFIG_REFERENCE_INTERNAL_1VREF // AVcc reference input voltage
+ #define DEFAULT_ADC_READINGS 4 // Times each channel is to be read
+ #define DEFAULT_ADC_RESOLUTION 8 // Integer number of bits
+ #define DEFAULT_ADC_CLOCK 125 // Integer KHz value
+ #define DEFAULT_ADC_CHANNELS_MASK ADC_CONFIG_ENABLED_CHANNELS
+ #ifdef ADC_CONFIG_SYSTEM_ALARM
+ #define DEFAULT_ADC_RANGE_CHANNEL_8_MIN ADC_CONFIG_RANGE_CHANNEL_8_MIN // Celsius degrees
+ #define DEFAULT_ADC_RANGE_CHANNEL_8_MAX ADC_CONFIG_RANGE_CHANNEL_8_MAX // Celsius degrees
+ #endif
+ #endif
#endif
#ifdef DEFAULTS_SHERLINE_5400
diff --git a/grbl/grbl.h b/grbl/grbl.h
index 89db829..1f7bf75 100644
--- a/grbl/grbl.h
+++ b/grbl/grbl.h
@@ -60,6 +60,10 @@
#include "spindle_control.h"
#include "stepper.h"
#include "jog.h"
+#ifdef USE_ADC
+ #include "cpu_map_adc.h"
+ #include "adc.h"
+#endif
// ---------------------------------------------------------------------------------------
// COMPILE-TIME ERROR CHECKING OF DEFINE VALUES:
diff --git a/grbl/main.c b/grbl/main.c
index 96f2aba..4e5b66e 100644
--- a/grbl/main.c
+++ b/grbl/main.c
@@ -42,6 +42,10 @@
serial_init(); // Setup serial baud rate and interrupts
settings_init(); // Load Grbl settings from EEPROM
stepper_init(); // Configure stepper pins and interrupt timers
+ #ifdef USE_ADC
+ adc_data adc;
+ adc_init(&adc); // Setup ADC module with defaults
+ #endif
system_init(); // Configure pinout pins and pin-change interrupt
memset(sys_position,0,sizeof(sys_position)); // Clear machine position.
diff --git a/grbl/report.c b/grbl/report.c
index 666608a..414d215 100644
--- a/grbl/report.c
+++ b/grbl/report.c
@@ -208,6 +208,9 @@
#else
report_util_uint8_setting(32,0);
#endif
+ #ifdef USE_ADC
+ report_util_uint8_setting(33,bit_istrue(settings.flags,BITFLAG_ADC_ENABLE));
+ #endif
// Print axis settings
uint8_t idx, set_idx;
uint8_t val = AXIS_SETTINGS_START_VAL;
@@ -439,6 +442,9 @@
#ifdef ENABLE_DUAL_AXIS
serial_write('2');
#endif
+ #ifdef USE_ADC
+ serial_write('@');
+ #endif
// NOTE: Compiled values, like override increments/max/min values, may be added at some point later.
serial_write(',');
print_uint8_base10(BLOCK_BUFFER_SIZE-1);
@@ -653,6 +659,25 @@
report_util_line_feed();
}
+#ifdef USE_ADC
+ void report_adc_status()
+ {
+ uint8_t i;
+
+ printPgmString(PSTR("ADC Buffer ["));
+
+ for (i = 0; i < ADC_CHANNELS_ENABLED_COUNT; i++) {
+ printPgmString(PSTR(" channel:"));
+ print_uint32_base10((uint32_t)i);
+ printPgmString(PSTR(", value:"));
+ //print_uint32_base10((uint32_t) adc_read_unsigned(i));
+ if (i + 1 < ADC_CHANNELS_ENABLED_COUNT) serial_write(',');
+ }
+
+ printPgmString(PSTR(" ]"));
+ report_util_line_feed();
+ }
+#endif
#ifdef DEBUG
void report_realtime_debug()
diff --git a/grbl/report.h b/grbl/report.h
index f148002..c5e065b 100644
--- a/grbl/report.h
+++ b/grbl/report.h
@@ -124,6 +124,11 @@
// Prints build info and user info
void report_build_info(char *line);
+#ifdef USE_ADC
+// Prints ADC buffer
+void report_adc_status();
+#endif
+
#ifdef DEBUG
void report_realtime_debug();
#endif
diff --git a/grbl/settings.c b/grbl/settings.c
index a9c830e..e607506 100644
--- a/grbl/settings.c
+++ b/grbl/settings.c
@@ -45,7 +45,11 @@
(DEFAULT_HOMING_ENABLE << BIT_HOMING_ENABLE) | \
(DEFAULT_SOFT_LIMIT_ENABLE << BIT_SOFT_LIMIT_ENABLE) | \
(DEFAULT_INVERT_LIMIT_PINS << BIT_INVERT_LIMIT_PINS) | \
- (DEFAULT_INVERT_PROBE_PIN << BIT_INVERT_PROBE_PIN),
+ (DEFAULT_INVERT_PROBE_PIN << BIT_INVERT_PROBE_PIN)
+ #ifdef USE_ADC
+ | (DEFAULT_ENABLE_ADC << BIT_ADC_ENABLE)
+ #endif
+ ,
.steps_per_mm[X_AXIS] = DEFAULT_X_STEPS_PER_MM,
.steps_per_mm[Y_AXIS] = DEFAULT_Y_STEPS_PER_MM,
.steps_per_mm[Z_AXIS] = DEFAULT_Z_STEPS_PER_MM,
@@ -294,6 +298,12 @@
return(STATUS_SETTING_DISABLED_LASER);
#endif
break;
+ #ifdef USE_ADC
+ case 33:
+ if (int_value) { settings.flags |= BITFLAG_ADC_ENABLE; }
+ else { settings.flags &= ~BITFLAG_ADC_ENABLE; }
+ break;
+ #endif
default:
return(STATUS_INVALID_STATEMENT);
}
diff --git a/grbl/settings.h b/grbl/settings.h
index 2d83ba9..9cac6c1 100644
--- a/grbl/settings.h
+++ b/grbl/settings.h
@@ -38,6 +38,9 @@
#define BIT_SOFT_LIMIT_ENABLE 5
#define BIT_INVERT_LIMIT_PINS 6
#define BIT_INVERT_PROBE_PIN 7
+#ifdef USE_ADC
+ #define BIT_ADC_ENABLE 8
+#endif
#define BITFLAG_REPORT_INCHES bit(BIT_REPORT_INCHES)
#define BITFLAG_LASER_MODE bit(BIT_LASER_MODE)
@@ -47,6 +50,9 @@
#define BITFLAG_SOFT_LIMIT_ENABLE bit(BIT_SOFT_LIMIT_ENABLE)
#define BITFLAG_INVERT_LIMIT_PINS bit(BIT_INVERT_LIMIT_PINS)
#define BITFLAG_INVERT_PROBE_PIN bit(BIT_INVERT_PROBE_PIN)
+#ifdef USE_ADC
+ #define BITFLAG_ADC_ENABLE bit(BIT_ADC_ENABLE)
+#endif
// Define status reporting boolean enable bit flags in settings.status_report_mask
#define BITFLAG_RT_STATUS_POSITION_TYPE bit(0)
@@ -103,7 +109,11 @@
float rpm_max;
float rpm_min;
- uint8_t flags; // Contains default boolean settings
+ #ifdef USE_ADC
+ uint16_t flags; // Contains default boolean settings
+ #else
+ uint8_t flags;
+ #endif
uint8_t homing_dir_mask;
float homing_feed_rate;
diff --git a/grbl/system.c b/grbl/system.c
index 55e6c40..009caca 100644
--- a/grbl/system.c
+++ b/grbl/system.c
@@ -166,6 +166,13 @@
// Don't run startup script. Prevents stored moves in startup from causing accidents.
} // Otherwise, no effect.
break;
+ #ifdef USE_ADC
+ case '@' :
+ // Block during cycle. Takes too long to print.
+ if ( sys.state & (STATE_CYCLE | STATE_HOLD) ) { return(STATUS_IDLE_ERROR); }
+ else { report_adc_status(); }
+ break;
+ #endif
}
break;
default :
diff --git a/grbl/system.h b/grbl/system.h
index cfc9273..a27351b 100644
--- a/grbl/system.h
+++ b/grbl/system.h
@@ -48,6 +48,9 @@
#define EXEC_ALARM_HOMING_FAIL_PULLOFF 8
#define EXEC_ALARM_HOMING_FAIL_APPROACH 9
#define EXEC_ALARM_HOMING_FAIL_DUAL_APPROACH 10
+#ifdef ADC_CONFIG_SYSTEM_ALARM
+ #define EXEC_ALARM_ADC 11
+#endif
// Override bit maps. Realtime bitflags to control feed, rapid, spindle, and coolant overrides.
// Spindle/coolant and feed/rapids are separated into two controlling flag variables.
@@ -122,7 +125,6 @@
#define SPINDLE_STOP_OVR_RESTORE bit(2)
#define SPINDLE_STOP_OVR_RESTORE_CYCLE bit(3)
-
// Define global system variables
typedef struct {
uint8_t state; // Tracks the current system state of Grbl.